00001
00022 #include "libavutil/crc.h"
00023 #include "libavutil/md5.h"
00024 #include "libavutil/opt.h"
00025 #include "avcodec.h"
00026 #include "get_bits.h"
00027 #include "golomb.h"
00028 #include "lpc.h"
00029 #include "flac.h"
00030 #include "flacdata.h"
00031
00032 #define FLAC_SUBFRAME_CONSTANT 0
00033 #define FLAC_SUBFRAME_VERBATIM 1
00034 #define FLAC_SUBFRAME_FIXED 8
00035 #define FLAC_SUBFRAME_LPC 32
00036
00037 #define MAX_FIXED_ORDER 4
00038 #define MAX_PARTITION_ORDER 8
00039 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
00040 #define MAX_LPC_PRECISION 15
00041 #define MAX_LPC_SHIFT 15
00042 #define MAX_RICE_PARAM 14
00043
00044 typedef struct CompressionOptions {
00045 int compression_level;
00046 int block_time_ms;
00047 enum FFLPCType lpc_type;
00048 int lpc_passes;
00049 int lpc_coeff_precision;
00050 int min_prediction_order;
00051 int max_prediction_order;
00052 int prediction_order_method;
00053 int min_partition_order;
00054 int max_partition_order;
00055 } CompressionOptions;
00056
00057 typedef struct RiceContext {
00058 int porder;
00059 int params[MAX_PARTITIONS];
00060 } RiceContext;
00061
00062 typedef struct FlacSubframe {
00063 int type;
00064 int type_code;
00065 int obits;
00066 int order;
00067 int32_t coefs[MAX_LPC_ORDER];
00068 int shift;
00069 RiceContext rc;
00070 int32_t samples[FLAC_MAX_BLOCKSIZE];
00071 int32_t residual[FLAC_MAX_BLOCKSIZE+1];
00072 } FlacSubframe;
00073
00074 typedef struct FlacFrame {
00075 FlacSubframe subframes[FLAC_MAX_CHANNELS];
00076 int blocksize;
00077 int bs_code[2];
00078 uint8_t crc8;
00079 int ch_mode;
00080 int verbatim_only;
00081 } FlacFrame;
00082
00083 typedef struct FlacEncodeContext {
00084 AVClass *class;
00085 PutBitContext pb;
00086 int channels;
00087 int samplerate;
00088 int sr_code[2];
00089 int max_blocksize;
00090 int min_framesize;
00091 int max_framesize;
00092 int max_encoded_framesize;
00093 uint32_t frame_count;
00094 uint64_t sample_count;
00095 uint8_t md5sum[16];
00096 FlacFrame frame;
00097 CompressionOptions options;
00098 AVCodecContext *avctx;
00099 LPCContext lpc_ctx;
00100 struct AVMD5 *md5ctx;
00101 } FlacEncodeContext;
00102
00103
00107 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
00108 {
00109 PutBitContext pb;
00110
00111 memset(header, 0, FLAC_STREAMINFO_SIZE);
00112 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
00113
00114
00115 put_bits(&pb, 16, s->max_blocksize);
00116 put_bits(&pb, 16, s->max_blocksize);
00117 put_bits(&pb, 24, s->min_framesize);
00118 put_bits(&pb, 24, s->max_framesize);
00119 put_bits(&pb, 20, s->samplerate);
00120 put_bits(&pb, 3, s->channels-1);
00121 put_bits(&pb, 5, 15);
00122
00123 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
00124 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
00125 flush_put_bits(&pb);
00126 memcpy(&header[18], s->md5sum, 16);
00127 }
00128
00129
00134 static int select_blocksize(int samplerate, int block_time_ms)
00135 {
00136 int i;
00137 int target;
00138 int blocksize;
00139
00140 assert(samplerate > 0);
00141 blocksize = ff_flac_blocksize_table[1];
00142 target = (samplerate * block_time_ms) / 1000;
00143 for (i = 0; i < 16; i++) {
00144 if (target >= ff_flac_blocksize_table[i] &&
00145 ff_flac_blocksize_table[i] > blocksize) {
00146 blocksize = ff_flac_blocksize_table[i];
00147 }
00148 }
00149 return blocksize;
00150 }
00151
00152
00153 static av_cold void dprint_compression_options(FlacEncodeContext *s)
00154 {
00155 AVCodecContext *avctx = s->avctx;
00156 CompressionOptions *opt = &s->options;
00157
00158 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
00159
00160 switch (opt->lpc_type) {
00161 case FF_LPC_TYPE_NONE:
00162 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
00163 break;
00164 case FF_LPC_TYPE_FIXED:
00165 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
00166 break;
00167 case FF_LPC_TYPE_LEVINSON:
00168 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
00169 break;
00170 case FF_LPC_TYPE_CHOLESKY:
00171 av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
00172 opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
00173 break;
00174 }
00175
00176 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
00177 opt->min_prediction_order, opt->max_prediction_order);
00178
00179 switch (opt->prediction_order_method) {
00180 case ORDER_METHOD_EST:
00181 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
00182 break;
00183 case ORDER_METHOD_2LEVEL:
00184 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
00185 break;
00186 case ORDER_METHOD_4LEVEL:
00187 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
00188 break;
00189 case ORDER_METHOD_8LEVEL:
00190 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
00191 break;
00192 case ORDER_METHOD_SEARCH:
00193 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
00194 break;
00195 case ORDER_METHOD_LOG:
00196 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
00197 break;
00198 }
00199
00200
00201 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
00202 opt->min_partition_order, opt->max_partition_order);
00203
00204 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
00205
00206 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
00207 opt->lpc_coeff_precision);
00208 }
00209
00210
00211 static av_cold int flac_encode_init(AVCodecContext *avctx)
00212 {
00213 int freq = avctx->sample_rate;
00214 int channels = avctx->channels;
00215 FlacEncodeContext *s = avctx->priv_data;
00216 int i, level, ret;
00217 uint8_t *streaminfo;
00218
00219 s->avctx = avctx;
00220
00221 if (avctx->sample_fmt != AV_SAMPLE_FMT_S16)
00222 return -1;
00223
00224 if (channels < 1 || channels > FLAC_MAX_CHANNELS)
00225 return -1;
00226 s->channels = channels;
00227
00228
00229 if (freq < 1)
00230 return -1;
00231 for (i = 4; i < 12; i++) {
00232 if (freq == ff_flac_sample_rate_table[i]) {
00233 s->samplerate = ff_flac_sample_rate_table[i];
00234 s->sr_code[0] = i;
00235 s->sr_code[1] = 0;
00236 break;
00237 }
00238 }
00239
00240 if (i == 12) {
00241 if (freq % 1000 == 0 && freq < 255000) {
00242 s->sr_code[0] = 12;
00243 s->sr_code[1] = freq / 1000;
00244 } else if (freq % 10 == 0 && freq < 655350) {
00245 s->sr_code[0] = 14;
00246 s->sr_code[1] = freq / 10;
00247 } else if (freq < 65535) {
00248 s->sr_code[0] = 13;
00249 s->sr_code[1] = freq;
00250 } else {
00251 return -1;
00252 }
00253 s->samplerate = freq;
00254 }
00255
00256
00257 if (avctx->compression_level < 0)
00258 s->options.compression_level = 5;
00259 else
00260 s->options.compression_level = avctx->compression_level;
00261
00262 level = s->options.compression_level;
00263 if (level > 12) {
00264 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
00265 s->options.compression_level);
00266 return -1;
00267 }
00268
00269 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
00270
00271 if (s->options.lpc_type == FF_LPC_TYPE_DEFAULT)
00272 s->options.lpc_type = ((int[]){ FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED, FF_LPC_TYPE_FIXED,
00273 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
00274 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
00275 FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_LEVINSON,
00276 FF_LPC_TYPE_LEVINSON})[level];
00277
00278 s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
00279 s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
00280
00281 if (s->options.prediction_order_method < 0)
00282 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00283 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00284 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
00285 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
00286 ORDER_METHOD_SEARCH})[level];
00287
00288 if (s->options.min_partition_order > s->options.max_partition_order) {
00289 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
00290 s->options.min_partition_order, s->options.max_partition_order);
00291 return AVERROR(EINVAL);
00292 }
00293 if (s->options.min_partition_order < 0)
00294 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
00295 if (s->options.max_partition_order < 0)
00296 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
00297
00298
00299 #if FF_API_FLAC_GLOBAL_OPTS
00300 if (avctx->lpc_type > FF_LPC_TYPE_DEFAULT) {
00301 if (avctx->lpc_type > FF_LPC_TYPE_CHOLESKY) {
00302 av_log(avctx, AV_LOG_ERROR, "unknown lpc type: %d\n", avctx->lpc_type);
00303 return -1;
00304 }
00305 s->options.lpc_type = avctx->lpc_type;
00306 if (s->options.lpc_type == FF_LPC_TYPE_CHOLESKY) {
00307 if (avctx->lpc_passes < 0) {
00308
00309 s->options.lpc_passes = 2;
00310 } else if (avctx->lpc_passes == 0) {
00311 av_log(avctx, AV_LOG_ERROR, "invalid number of lpc passes: %d\n",
00312 avctx->lpc_passes);
00313 return -1;
00314 } else {
00315 s->options.lpc_passes = avctx->lpc_passes;
00316 }
00317 }
00318 }
00319 #endif
00320
00321 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
00322 s->options.min_prediction_order = 0;
00323 } else if (avctx->min_prediction_order >= 0) {
00324 if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
00325 if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
00326 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00327 avctx->min_prediction_order);
00328 return -1;
00329 }
00330 } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
00331 avctx->min_prediction_order > MAX_LPC_ORDER) {
00332 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00333 avctx->min_prediction_order);
00334 return -1;
00335 }
00336 s->options.min_prediction_order = avctx->min_prediction_order;
00337 }
00338 if (s->options.lpc_type == FF_LPC_TYPE_NONE) {
00339 s->options.max_prediction_order = 0;
00340 } else if (avctx->max_prediction_order >= 0) {
00341 if (s->options.lpc_type == FF_LPC_TYPE_FIXED) {
00342 if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
00343 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00344 avctx->max_prediction_order);
00345 return -1;
00346 }
00347 } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
00348 avctx->max_prediction_order > MAX_LPC_ORDER) {
00349 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00350 avctx->max_prediction_order);
00351 return -1;
00352 }
00353 s->options.max_prediction_order = avctx->max_prediction_order;
00354 }
00355 if (s->options.max_prediction_order < s->options.min_prediction_order) {
00356 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
00357 s->options.min_prediction_order, s->options.max_prediction_order);
00358 return -1;
00359 }
00360
00361 #if FF_API_FLAC_GLOBAL_OPTS
00362 if (avctx->prediction_order_method >= 0) {
00363 if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
00364 av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
00365 avctx->prediction_order_method);
00366 return -1;
00367 }
00368 s->options.prediction_order_method = avctx->prediction_order_method;
00369 }
00370
00371 if (avctx->min_partition_order >= 0) {
00372 if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
00373 av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
00374 avctx->min_partition_order);
00375 return -1;
00376 }
00377 s->options.min_partition_order = avctx->min_partition_order;
00378 }
00379 if (avctx->max_partition_order >= 0) {
00380 if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
00381 av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
00382 avctx->max_partition_order);
00383 return -1;
00384 }
00385 s->options.max_partition_order = avctx->max_partition_order;
00386 }
00387 if (s->options.max_partition_order < s->options.min_partition_order) {
00388 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
00389 s->options.min_partition_order, s->options.max_partition_order);
00390 return -1;
00391 }
00392 #endif
00393
00394 if (avctx->frame_size > 0) {
00395 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
00396 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
00397 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
00398 avctx->frame_size);
00399 return -1;
00400 }
00401 } else {
00402 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
00403 }
00404 s->max_blocksize = s->avctx->frame_size;
00405
00406 #if FF_API_FLAC_GLOBAL_OPTS
00407
00408 if (avctx->lpc_coeff_precision > 0) {
00409 if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
00410 av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
00411 avctx->lpc_coeff_precision);
00412 return -1;
00413 }
00414 s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
00415 }
00416 #endif
00417
00418
00419 s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
00420 s->channels, 16);
00421
00422
00423 s->md5ctx = av_malloc(av_md5_size);
00424 if (!s->md5ctx)
00425 return AVERROR(ENOMEM);
00426 av_md5_init(s->md5ctx);
00427
00428 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
00429 if (!streaminfo)
00430 return AVERROR(ENOMEM);
00431 write_streaminfo(s, streaminfo);
00432 avctx->extradata = streaminfo;
00433 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
00434
00435 s->frame_count = 0;
00436 s->min_framesize = s->max_framesize;
00437
00438 avctx->coded_frame = avcodec_alloc_frame();
00439 if (!avctx->coded_frame)
00440 return AVERROR(ENOMEM);
00441
00442 if (channels == 3 &&
00443 avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
00444 channels == 4 &&
00445 avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
00446 avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
00447 channels == 5 &&
00448 avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
00449 avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
00450 channels == 6 &&
00451 avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
00452 avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK) {
00453 if (avctx->channel_layout) {
00454 av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
00455 "output stream will have incorrect "
00456 "channel layout.\n");
00457 } else {
00458 av_log(avctx, AV_LOG_WARNING, "No channel layout specified. The encoder "
00459 "will use Flac channel layout for "
00460 "%d channels.\n", channels);
00461 }
00462 }
00463
00464 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
00465 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
00466
00467 dprint_compression_options(s);
00468
00469 return ret;
00470 }
00471
00472
00473 static void init_frame(FlacEncodeContext *s)
00474 {
00475 int i, ch;
00476 FlacFrame *frame;
00477
00478 frame = &s->frame;
00479
00480 for (i = 0; i < 16; i++) {
00481 if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
00482 frame->blocksize = ff_flac_blocksize_table[i];
00483 frame->bs_code[0] = i;
00484 frame->bs_code[1] = 0;
00485 break;
00486 }
00487 }
00488 if (i == 16) {
00489 frame->blocksize = s->avctx->frame_size;
00490 if (frame->blocksize <= 256) {
00491 frame->bs_code[0] = 6;
00492 frame->bs_code[1] = frame->blocksize-1;
00493 } else {
00494 frame->bs_code[0] = 7;
00495 frame->bs_code[1] = frame->blocksize-1;
00496 }
00497 }
00498
00499 for (ch = 0; ch < s->channels; ch++)
00500 frame->subframes[ch].obits = 16;
00501
00502 frame->verbatim_only = 0;
00503 }
00504
00505
00509 static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
00510 {
00511 int i, j, ch;
00512 FlacFrame *frame;
00513
00514 frame = &s->frame;
00515 for (i = 0, j = 0; i < frame->blocksize; i++)
00516 for (ch = 0; ch < s->channels; ch++, j++)
00517 frame->subframes[ch].samples[i] = samples[j];
00518 }
00519
00520
00521 static int rice_count_exact(int32_t *res, int n, int k)
00522 {
00523 int i;
00524 int count = 0;
00525
00526 for (i = 0; i < n; i++) {
00527 int32_t v = -2 * res[i] - 1;
00528 v ^= v >> 31;
00529 count += (v >> k) + 1 + k;
00530 }
00531 return count;
00532 }
00533
00534
00535 static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
00536 int pred_order)
00537 {
00538 int p, porder, psize;
00539 int i, part_end;
00540 int count = 0;
00541
00542
00543 count += 8;
00544
00545
00546 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
00547 count += sub->obits;
00548 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
00549 count += s->frame.blocksize * sub->obits;
00550 } else {
00551
00552 count += pred_order * sub->obits;
00553
00554
00555 if (sub->type == FLAC_SUBFRAME_LPC)
00556 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
00557
00558
00559 count += 2;
00560
00561
00562 porder = sub->rc.porder;
00563 psize = s->frame.blocksize >> porder;
00564 count += 4;
00565
00566
00567 i = pred_order;
00568 part_end = psize;
00569 for (p = 0; p < 1 << porder; p++) {
00570 int k = sub->rc.params[p];
00571 count += 4;
00572 count += rice_count_exact(&sub->residual[i], part_end - i, k);
00573 i = part_end;
00574 part_end = FFMIN(s->frame.blocksize, part_end + psize);
00575 }
00576 }
00577
00578 return count;
00579 }
00580
00581
00582 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
00583
00587 static int find_optimal_param(uint32_t sum, int n)
00588 {
00589 int k;
00590 uint32_t sum2;
00591
00592 if (sum <= n >> 1)
00593 return 0;
00594 sum2 = sum - (n >> 1);
00595 k = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
00596 return FFMIN(k, MAX_RICE_PARAM);
00597 }
00598
00599
00600 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
00601 uint32_t *sums, int n, int pred_order)
00602 {
00603 int i;
00604 int k, cnt, part;
00605 uint32_t all_bits;
00606
00607 part = (1 << porder);
00608 all_bits = 4 * part;
00609
00610 cnt = (n >> porder) - pred_order;
00611 for (i = 0; i < part; i++) {
00612 k = find_optimal_param(sums[i], cnt);
00613 rc->params[i] = k;
00614 all_bits += rice_encode_count(sums[i], cnt, k);
00615 cnt = n >> porder;
00616 }
00617
00618 rc->porder = porder;
00619
00620 return all_bits;
00621 }
00622
00623
00624 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
00625 uint32_t sums[][MAX_PARTITIONS])
00626 {
00627 int i, j;
00628 int parts;
00629 uint32_t *res, *res_end;
00630
00631
00632 parts = (1 << pmax);
00633 res = &data[pred_order];
00634 res_end = &data[n >> pmax];
00635 for (i = 0; i < parts; i++) {
00636 uint32_t sum = 0;
00637 while (res < res_end)
00638 sum += *(res++);
00639 sums[pmax][i] = sum;
00640 res_end += n >> pmax;
00641 }
00642
00643 for (i = pmax - 1; i >= pmin; i--) {
00644 parts = (1 << i);
00645 for (j = 0; j < parts; j++)
00646 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
00647 }
00648 }
00649
00650
00651 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
00652 int32_t *data, int n, int pred_order)
00653 {
00654 int i;
00655 uint32_t bits[MAX_PARTITION_ORDER+1];
00656 int opt_porder;
00657 RiceContext tmp_rc;
00658 uint32_t *udata;
00659 uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
00660
00661 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
00662 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
00663 assert(pmin <= pmax);
00664
00665 udata = av_malloc(n * sizeof(uint32_t));
00666 for (i = 0; i < n; i++)
00667 udata[i] = (2*data[i]) ^ (data[i]>>31);
00668
00669 calc_sums(pmin, pmax, udata, n, pred_order, sums);
00670
00671 opt_porder = pmin;
00672 bits[pmin] = UINT32_MAX;
00673 for (i = pmin; i <= pmax; i++) {
00674 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
00675 if (bits[i] <= bits[opt_porder]) {
00676 opt_porder = i;
00677 *rc = tmp_rc;
00678 }
00679 }
00680
00681 av_freep(&udata);
00682 return bits[opt_porder];
00683 }
00684
00685
00686 static int get_max_p_order(int max_porder, int n, int order)
00687 {
00688 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
00689 if (order > 0)
00690 porder = FFMIN(porder, av_log2(n/order));
00691 return porder;
00692 }
00693
00694
00695 static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
00696 FlacSubframe *sub, int pred_order)
00697 {
00698 int pmin = get_max_p_order(s->options.min_partition_order,
00699 s->frame.blocksize, pred_order);
00700 int pmax = get_max_p_order(s->options.max_partition_order,
00701 s->frame.blocksize, pred_order);
00702
00703 uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
00704 if (sub->type == FLAC_SUBFRAME_LPC)
00705 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
00706 bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
00707 s->frame.blocksize, pred_order);
00708 return bits;
00709 }
00710
00711
00712 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
00713 int order)
00714 {
00715 int i;
00716
00717 for (i = 0; i < order; i++)
00718 res[i] = smp[i];
00719
00720 if (order == 0) {
00721 for (i = order; i < n; i++)
00722 res[i] = smp[i];
00723 } else if (order == 1) {
00724 for (i = order; i < n; i++)
00725 res[i] = smp[i] - smp[i-1];
00726 } else if (order == 2) {
00727 int a = smp[order-1] - smp[order-2];
00728 for (i = order; i < n; i += 2) {
00729 int b = smp[i ] - smp[i-1];
00730 res[i] = b - a;
00731 a = smp[i+1] - smp[i ];
00732 res[i+1] = a - b;
00733 }
00734 } else if (order == 3) {
00735 int a = smp[order-1] - smp[order-2];
00736 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00737 for (i = order; i < n; i += 2) {
00738 int b = smp[i ] - smp[i-1];
00739 int d = b - a;
00740 res[i] = d - c;
00741 a = smp[i+1] - smp[i ];
00742 c = a - b;
00743 res[i+1] = c - d;
00744 }
00745 } else {
00746 int a = smp[order-1] - smp[order-2];
00747 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00748 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
00749 for (i = order; i < n; i += 2) {
00750 int b = smp[i ] - smp[i-1];
00751 int d = b - a;
00752 int f = d - c;
00753 res[i ] = f - e;
00754 a = smp[i+1] - smp[i ];
00755 c = a - b;
00756 e = c - d;
00757 res[i+1] = e - f;
00758 }
00759 }
00760 }
00761
00762
00763 #define LPC1(x) {\
00764 int c = coefs[(x)-1];\
00765 p0 += c * s;\
00766 s = smp[i-(x)+1];\
00767 p1 += c * s;\
00768 }
00769
00770 static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
00771 const int32_t *smp, int n, int order,
00772 const int32_t *coefs, int shift, int big)
00773 {
00774 int i;
00775 for (i = order; i < n; i += 2) {
00776 int s = smp[i-order];
00777 int p0 = 0, p1 = 0;
00778 if (big) {
00779 switch (order) {
00780 case 32: LPC1(32)
00781 case 31: LPC1(31)
00782 case 30: LPC1(30)
00783 case 29: LPC1(29)
00784 case 28: LPC1(28)
00785 case 27: LPC1(27)
00786 case 26: LPC1(26)
00787 case 25: LPC1(25)
00788 case 24: LPC1(24)
00789 case 23: LPC1(23)
00790 case 22: LPC1(22)
00791 case 21: LPC1(21)
00792 case 20: LPC1(20)
00793 case 19: LPC1(19)
00794 case 18: LPC1(18)
00795 case 17: LPC1(17)
00796 case 16: LPC1(16)
00797 case 15: LPC1(15)
00798 case 14: LPC1(14)
00799 case 13: LPC1(13)
00800 case 12: LPC1(12)
00801 case 11: LPC1(11)
00802 case 10: LPC1(10)
00803 case 9: LPC1( 9)
00804 LPC1( 8)
00805 LPC1( 7)
00806 LPC1( 6)
00807 LPC1( 5)
00808 LPC1( 4)
00809 LPC1( 3)
00810 LPC1( 2)
00811 LPC1( 1)
00812 }
00813 } else {
00814 switch (order) {
00815 case 8: LPC1( 8)
00816 case 7: LPC1( 7)
00817 case 6: LPC1( 6)
00818 case 5: LPC1( 5)
00819 case 4: LPC1( 4)
00820 case 3: LPC1( 3)
00821 case 2: LPC1( 2)
00822 case 1: LPC1( 1)
00823 }
00824 }
00825 res[i ] = smp[i ] - (p0 >> shift);
00826 res[i+1] = smp[i+1] - (p1 >> shift);
00827 }
00828 }
00829
00830
00831 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
00832 int order, const int32_t *coefs, int shift)
00833 {
00834 int i;
00835 for (i = 0; i < order; i++)
00836 res[i] = smp[i];
00837 #if CONFIG_SMALL
00838 for (i = order; i < n; i += 2) {
00839 int j;
00840 int s = smp[i];
00841 int p0 = 0, p1 = 0;
00842 for (j = 0; j < order; j++) {
00843 int c = coefs[j];
00844 p1 += c * s;
00845 s = smp[i-j-1];
00846 p0 += c * s;
00847 }
00848 res[i ] = smp[i ] - (p0 >> shift);
00849 res[i+1] = smp[i+1] - (p1 >> shift);
00850 }
00851 #else
00852 switch (order) {
00853 case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
00854 case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
00855 case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
00856 case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
00857 case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
00858 case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
00859 case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
00860 case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
00861 default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
00862 }
00863 #endif
00864 }
00865
00866
00867 static int encode_residual_ch(FlacEncodeContext *s, int ch)
00868 {
00869 int i, n;
00870 int min_order, max_order, opt_order, omethod;
00871 FlacFrame *frame;
00872 FlacSubframe *sub;
00873 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
00874 int shift[MAX_LPC_ORDER];
00875 int32_t *res, *smp;
00876
00877 frame = &s->frame;
00878 sub = &frame->subframes[ch];
00879 res = sub->residual;
00880 smp = sub->samples;
00881 n = frame->blocksize;
00882
00883
00884 for (i = 1; i < n; i++)
00885 if(smp[i] != smp[0])
00886 break;
00887 if (i == n) {
00888 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
00889 res[0] = smp[0];
00890 return subframe_count_exact(s, sub, 0);
00891 }
00892
00893
00894 if (frame->verbatim_only || n < 5) {
00895 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
00896 memcpy(res, smp, n * sizeof(int32_t));
00897 return subframe_count_exact(s, sub, 0);
00898 }
00899
00900 min_order = s->options.min_prediction_order;
00901 max_order = s->options.max_prediction_order;
00902 omethod = s->options.prediction_order_method;
00903
00904
00905 sub->type = FLAC_SUBFRAME_FIXED;
00906 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
00907 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
00908 uint32_t bits[MAX_FIXED_ORDER+1];
00909 if (max_order > MAX_FIXED_ORDER)
00910 max_order = MAX_FIXED_ORDER;
00911 opt_order = 0;
00912 bits[0] = UINT32_MAX;
00913 for (i = min_order; i <= max_order; i++) {
00914 encode_residual_fixed(res, smp, n, i);
00915 bits[i] = find_subframe_rice_params(s, sub, i);
00916 if (bits[i] < bits[opt_order])
00917 opt_order = i;
00918 }
00919 sub->order = opt_order;
00920 sub->type_code = sub->type | sub->order;
00921 if (sub->order != max_order) {
00922 encode_residual_fixed(res, smp, n, sub->order);
00923 find_subframe_rice_params(s, sub, sub->order);
00924 }
00925 return subframe_count_exact(s, sub, sub->order);
00926 }
00927
00928
00929 sub->type = FLAC_SUBFRAME_LPC;
00930 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
00931 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
00932 s->options.lpc_passes, omethod,
00933 MAX_LPC_SHIFT, 0);
00934
00935 if (omethod == ORDER_METHOD_2LEVEL ||
00936 omethod == ORDER_METHOD_4LEVEL ||
00937 omethod == ORDER_METHOD_8LEVEL) {
00938 int levels = 1 << omethod;
00939 uint32_t bits[1 << ORDER_METHOD_8LEVEL];
00940 int order;
00941 int opt_index = levels-1;
00942 opt_order = max_order-1;
00943 bits[opt_index] = UINT32_MAX;
00944 for (i = levels-1; i >= 0; i--) {
00945 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
00946 if (order < 0)
00947 order = 0;
00948 encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
00949 bits[i] = find_subframe_rice_params(s, sub, order+1);
00950 if (bits[i] < bits[opt_index]) {
00951 opt_index = i;
00952 opt_order = order;
00953 }
00954 }
00955 opt_order++;
00956 } else if (omethod == ORDER_METHOD_SEARCH) {
00957
00958 uint32_t bits[MAX_LPC_ORDER];
00959 opt_order = 0;
00960 bits[0] = UINT32_MAX;
00961 for (i = min_order-1; i < max_order; i++) {
00962 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00963 bits[i] = find_subframe_rice_params(s, sub, i+1);
00964 if (bits[i] < bits[opt_order])
00965 opt_order = i;
00966 }
00967 opt_order++;
00968 } else if (omethod == ORDER_METHOD_LOG) {
00969 uint32_t bits[MAX_LPC_ORDER];
00970 int step;
00971
00972 opt_order = min_order - 1 + (max_order-min_order)/3;
00973 memset(bits, -1, sizeof(bits));
00974
00975 for (step = 16; step; step >>= 1) {
00976 int last = opt_order;
00977 for (i = last-step; i <= last+step; i += step) {
00978 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
00979 continue;
00980 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00981 bits[i] = find_subframe_rice_params(s, sub, i+1);
00982 if (bits[i] < bits[opt_order])
00983 opt_order = i;
00984 }
00985 }
00986 opt_order++;
00987 }
00988
00989 sub->order = opt_order;
00990 sub->type_code = sub->type | (sub->order-1);
00991 sub->shift = shift[sub->order-1];
00992 for (i = 0; i < sub->order; i++)
00993 sub->coefs[i] = coefs[sub->order-1][i];
00994
00995 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
00996
00997 find_subframe_rice_params(s, sub, sub->order);
00998
00999 return subframe_count_exact(s, sub, sub->order);
01000 }
01001
01002
01003 static int count_frame_header(FlacEncodeContext *s)
01004 {
01005 uint8_t av_unused tmp;
01006 int count;
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018 count = 32;
01019
01020
01021 PUT_UTF8(s->frame_count, tmp, count += 8;)
01022
01023
01024 if (s->frame.bs_code[0] == 6)
01025 count += 8;
01026 else if (s->frame.bs_code[0] == 7)
01027 count += 16;
01028
01029
01030 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
01031
01032
01033 count += 8;
01034
01035 return count;
01036 }
01037
01038
01039 static int encode_frame(FlacEncodeContext *s)
01040 {
01041 int ch, count;
01042
01043 count = count_frame_header(s);
01044
01045 for (ch = 0; ch < s->channels; ch++)
01046 count += encode_residual_ch(s, ch);
01047
01048 count += (8 - (count & 7)) & 7;
01049 count += 16;
01050
01051 return count >> 3;
01052 }
01053
01054
01055 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
01056 {
01057 int i, best;
01058 int32_t lt, rt;
01059 uint64_t sum[4];
01060 uint64_t score[4];
01061 int k;
01062
01063
01064 sum[0] = sum[1] = sum[2] = sum[3] = 0;
01065 for (i = 2; i < n; i++) {
01066 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
01067 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
01068 sum[2] += FFABS((lt + rt) >> 1);
01069 sum[3] += FFABS(lt - rt);
01070 sum[0] += FFABS(lt);
01071 sum[1] += FFABS(rt);
01072 }
01073
01074 for (i = 0; i < 4; i++) {
01075 k = find_optimal_param(2 * sum[i], n);
01076 sum[i] = rice_encode_count( 2 * sum[i], n, k);
01077 }
01078
01079
01080 score[0] = sum[0] + sum[1];
01081 score[1] = sum[0] + sum[3];
01082 score[2] = sum[1] + sum[3];
01083 score[3] = sum[2] + sum[3];
01084
01085
01086 best = 0;
01087 for (i = 1; i < 4; i++)
01088 if (score[i] < score[best])
01089 best = i;
01090 if (best == 0) {
01091 return FLAC_CHMODE_INDEPENDENT;
01092 } else if (best == 1) {
01093 return FLAC_CHMODE_LEFT_SIDE;
01094 } else if (best == 2) {
01095 return FLAC_CHMODE_RIGHT_SIDE;
01096 } else {
01097 return FLAC_CHMODE_MID_SIDE;
01098 }
01099 }
01100
01101
01105 static void channel_decorrelation(FlacEncodeContext *s)
01106 {
01107 FlacFrame *frame;
01108 int32_t *left, *right;
01109 int i, n;
01110
01111 frame = &s->frame;
01112 n = frame->blocksize;
01113 left = frame->subframes[0].samples;
01114 right = frame->subframes[1].samples;
01115
01116 if (s->channels != 2) {
01117 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
01118 return;
01119 }
01120
01121 frame->ch_mode = estimate_stereo_mode(left, right, n);
01122
01123
01124 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
01125 return;
01126 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
01127 int32_t tmp;
01128 for (i = 0; i < n; i++) {
01129 tmp = left[i];
01130 left[i] = (tmp + right[i]) >> 1;
01131 right[i] = tmp - right[i];
01132 }
01133 frame->subframes[1].obits++;
01134 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
01135 for (i = 0; i < n; i++)
01136 right[i] = left[i] - right[i];
01137 frame->subframes[1].obits++;
01138 } else {
01139 for (i = 0; i < n; i++)
01140 left[i] -= right[i];
01141 frame->subframes[0].obits++;
01142 }
01143 }
01144
01145
01146 static void write_utf8(PutBitContext *pb, uint32_t val)
01147 {
01148 uint8_t tmp;
01149 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
01150 }
01151
01152
01153 static void write_frame_header(FlacEncodeContext *s)
01154 {
01155 FlacFrame *frame;
01156 int crc;
01157
01158 frame = &s->frame;
01159
01160 put_bits(&s->pb, 16, 0xFFF8);
01161 put_bits(&s->pb, 4, frame->bs_code[0]);
01162 put_bits(&s->pb, 4, s->sr_code[0]);
01163
01164 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
01165 put_bits(&s->pb, 4, s->channels-1);
01166 else
01167 put_bits(&s->pb, 4, frame->ch_mode);
01168
01169 put_bits(&s->pb, 3, 4);
01170 put_bits(&s->pb, 1, 0);
01171 write_utf8(&s->pb, s->frame_count);
01172
01173 if (frame->bs_code[0] == 6)
01174 put_bits(&s->pb, 8, frame->bs_code[1]);
01175 else if (frame->bs_code[0] == 7)
01176 put_bits(&s->pb, 16, frame->bs_code[1]);
01177
01178 if (s->sr_code[0] == 12)
01179 put_bits(&s->pb, 8, s->sr_code[1]);
01180 else if (s->sr_code[0] > 12)
01181 put_bits(&s->pb, 16, s->sr_code[1]);
01182
01183 flush_put_bits(&s->pb);
01184 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
01185 put_bits_count(&s->pb) >> 3);
01186 put_bits(&s->pb, 8, crc);
01187 }
01188
01189
01190 static void write_subframes(FlacEncodeContext *s)
01191 {
01192 int ch;
01193
01194 for (ch = 0; ch < s->channels; ch++) {
01195 FlacSubframe *sub = &s->frame.subframes[ch];
01196 int i, p, porder, psize;
01197 int32_t *part_end;
01198 int32_t *res = sub->residual;
01199 int32_t *frame_end = &sub->residual[s->frame.blocksize];
01200
01201
01202 put_bits(&s->pb, 1, 0);
01203 put_bits(&s->pb, 6, sub->type_code);
01204 put_bits(&s->pb, 1, 0);
01205
01206
01207 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
01208 put_sbits(&s->pb, sub->obits, res[0]);
01209 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
01210 while (res < frame_end)
01211 put_sbits(&s->pb, sub->obits, *res++);
01212 } else {
01213
01214 for (i = 0; i < sub->order; i++)
01215 put_sbits(&s->pb, sub->obits, *res++);
01216
01217
01218 if (sub->type == FLAC_SUBFRAME_LPC) {
01219 int cbits = s->options.lpc_coeff_precision;
01220 put_bits( &s->pb, 4, cbits-1);
01221 put_sbits(&s->pb, 5, sub->shift);
01222 for (i = 0; i < sub->order; i++)
01223 put_sbits(&s->pb, cbits, sub->coefs[i]);
01224 }
01225
01226
01227 put_bits(&s->pb, 2, 0);
01228
01229
01230 porder = sub->rc.porder;
01231 psize = s->frame.blocksize >> porder;
01232 put_bits(&s->pb, 4, porder);
01233
01234
01235 part_end = &sub->residual[psize];
01236 for (p = 0; p < 1 << porder; p++) {
01237 int k = sub->rc.params[p];
01238 put_bits(&s->pb, 4, k);
01239 while (res < part_end)
01240 set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
01241 part_end = FFMIN(frame_end, part_end + psize);
01242 }
01243 }
01244 }
01245 }
01246
01247
01248 static void write_frame_footer(FlacEncodeContext *s)
01249 {
01250 int crc;
01251 flush_put_bits(&s->pb);
01252 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
01253 put_bits_count(&s->pb)>>3));
01254 put_bits(&s->pb, 16, crc);
01255 flush_put_bits(&s->pb);
01256 }
01257
01258
01259 static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
01260 {
01261 init_put_bits(&s->pb, frame, buf_size);
01262 write_frame_header(s);
01263 write_subframes(s);
01264 write_frame_footer(s);
01265 return put_bits_count(&s->pb) >> 3;
01266 }
01267
01268
01269 static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
01270 {
01271 #if HAVE_BIGENDIAN
01272 int i;
01273 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
01274 int16_t smp = av_le2ne16(samples[i]);
01275 av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
01276 }
01277 #else
01278 av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
01279 #endif
01280 }
01281
01282
01283 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
01284 int buf_size, void *data)
01285 {
01286 FlacEncodeContext *s;
01287 const int16_t *samples = data;
01288 int frame_bytes, out_bytes;
01289
01290 s = avctx->priv_data;
01291
01292
01293 if (!data) {
01294 s->max_framesize = s->max_encoded_framesize;
01295 av_md5_final(s->md5ctx, s->md5sum);
01296 write_streaminfo(s, avctx->extradata);
01297 return 0;
01298 }
01299
01300
01301 if (avctx->frame_size < s->frame.blocksize) {
01302 s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
01303 s->channels, 16);
01304 }
01305
01306 init_frame(s);
01307
01308 copy_samples(s, samples);
01309
01310 channel_decorrelation(s);
01311
01312 frame_bytes = encode_frame(s);
01313
01314
01315
01316 if (frame_bytes > s->max_framesize) {
01317 s->frame.verbatim_only = 1;
01318 frame_bytes = encode_frame(s);
01319 }
01320
01321 if (buf_size < frame_bytes) {
01322 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
01323 return 0;
01324 }
01325 out_bytes = write_frame(s, frame, buf_size);
01326
01327 s->frame_count++;
01328 avctx->coded_frame->pts = s->sample_count;
01329 s->sample_count += avctx->frame_size;
01330 update_md5_sum(s, samples);
01331 if (out_bytes > s->max_encoded_framesize)
01332 s->max_encoded_framesize = out_bytes;
01333 if (out_bytes < s->min_framesize)
01334 s->min_framesize = out_bytes;
01335
01336 return out_bytes;
01337 }
01338
01339
01340 static av_cold int flac_encode_close(AVCodecContext *avctx)
01341 {
01342 if (avctx->priv_data) {
01343 FlacEncodeContext *s = avctx->priv_data;
01344 av_freep(&s->md5ctx);
01345 ff_lpc_end(&s->lpc_ctx);
01346 }
01347 av_freep(&avctx->extradata);
01348 avctx->extradata_size = 0;
01349 av_freep(&avctx->coded_frame);
01350 return 0;
01351 }
01352
01353 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
01354 static const AVOption options[] = {
01355 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
01356 { "lpc_type", "LPC algorithm", offsetof(FlacEncodeContext, options.lpc_type), AV_OPT_TYPE_INT, {.dbl = FF_LPC_TYPE_DEFAULT }, FF_LPC_TYPE_DEFAULT, FF_LPC_TYPE_NB-1, FLAGS, "lpc_type" },
01357 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01358 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01359 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01360 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01361 { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", offsetof(FlacEncodeContext, options.lpc_passes), AV_OPT_TYPE_INT, {.dbl = -1 }, INT_MIN, INT_MAX, FLAGS },
01362 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
01363 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
01364 { "prediction_order_method", "Search method for selecting prediction order", offsetof(FlacEncodeContext, options.prediction_order_method), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, ORDER_METHOD_LOG, FLAGS, "predm" },
01365 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
01366 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01367 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01368 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01369 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
01370 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
01371 { NULL },
01372 };
01373
01374 static const AVClass flac_encoder_class = {
01375 "FLAC encoder",
01376 av_default_item_name,
01377 options,
01378 LIBAVUTIL_VERSION_INT,
01379 };
01380
01381 AVCodec ff_flac_encoder = {
01382 .name = "flac",
01383 .type = AVMEDIA_TYPE_AUDIO,
01384 .id = CODEC_ID_FLAC,
01385 .priv_data_size = sizeof(FlacEncodeContext),
01386 .init = flac_encode_init,
01387 .encode = flac_encode_frame,
01388 .close = flac_encode_close,
01389 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY | CODEC_CAP_LOSSLESS,
01390 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
01391 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
01392 .priv_class = &flac_encoder_class,
01393 };