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 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
00443 s->options.max_prediction_order, FF_LPC_TYPE_LEVINSON);
00444
00445 dprint_compression_options(s);
00446
00447 return ret;
00448 }
00449
00450
00451 static void init_frame(FlacEncodeContext *s)
00452 {
00453 int i, ch;
00454 FlacFrame *frame;
00455
00456 frame = &s->frame;
00457
00458 for (i = 0; i < 16; i++) {
00459 if (s->avctx->frame_size == ff_flac_blocksize_table[i]) {
00460 frame->blocksize = ff_flac_blocksize_table[i];
00461 frame->bs_code[0] = i;
00462 frame->bs_code[1] = 0;
00463 break;
00464 }
00465 }
00466 if (i == 16) {
00467 frame->blocksize = s->avctx->frame_size;
00468 if (frame->blocksize <= 256) {
00469 frame->bs_code[0] = 6;
00470 frame->bs_code[1] = frame->blocksize-1;
00471 } else {
00472 frame->bs_code[0] = 7;
00473 frame->bs_code[1] = frame->blocksize-1;
00474 }
00475 }
00476
00477 for (ch = 0; ch < s->channels; ch++)
00478 frame->subframes[ch].obits = 16;
00479
00480 frame->verbatim_only = 0;
00481 }
00482
00483
00487 static void copy_samples(FlacEncodeContext *s, const int16_t *samples)
00488 {
00489 int i, j, ch;
00490 FlacFrame *frame;
00491
00492 frame = &s->frame;
00493 for (i = 0, j = 0; i < frame->blocksize; i++)
00494 for (ch = 0; ch < s->channels; ch++, j++)
00495 frame->subframes[ch].samples[i] = samples[j];
00496 }
00497
00498
00499 static int rice_count_exact(int32_t *res, int n, int k)
00500 {
00501 int i;
00502 int count = 0;
00503
00504 for (i = 0; i < n; i++) {
00505 int32_t v = -2 * res[i] - 1;
00506 v ^= v >> 31;
00507 count += (v >> k) + 1 + k;
00508 }
00509 return count;
00510 }
00511
00512
00513 static int subframe_count_exact(FlacEncodeContext *s, FlacSubframe *sub,
00514 int pred_order)
00515 {
00516 int p, porder, psize;
00517 int i, part_end;
00518 int count = 0;
00519
00520
00521 count += 8;
00522
00523
00524 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
00525 count += sub->obits;
00526 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
00527 count += s->frame.blocksize * sub->obits;
00528 } else {
00529
00530 count += pred_order * sub->obits;
00531
00532
00533 if (sub->type == FLAC_SUBFRAME_LPC)
00534 count += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
00535
00536
00537 count += 2;
00538
00539
00540 porder = sub->rc.porder;
00541 psize = s->frame.blocksize >> porder;
00542 count += 4;
00543
00544
00545 i = pred_order;
00546 part_end = psize;
00547 for (p = 0; p < 1 << porder; p++) {
00548 int k = sub->rc.params[p];
00549 count += 4;
00550 count += rice_count_exact(&sub->residual[i], part_end - i, k);
00551 i = part_end;
00552 part_end = FFMIN(s->frame.blocksize, part_end + psize);
00553 }
00554 }
00555
00556 return count;
00557 }
00558
00559
00560 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
00561
00565 static int find_optimal_param(uint32_t sum, int n)
00566 {
00567 int k;
00568 uint32_t sum2;
00569
00570 if (sum <= n >> 1)
00571 return 0;
00572 sum2 = sum - (n >> 1);
00573 k = av_log2(n < 256 ? FASTDIV(sum2, n) : sum2 / n);
00574 return FFMIN(k, MAX_RICE_PARAM);
00575 }
00576
00577
00578 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
00579 uint32_t *sums, int n, int pred_order)
00580 {
00581 int i;
00582 int k, cnt, part;
00583 uint32_t all_bits;
00584
00585 part = (1 << porder);
00586 all_bits = 4 * part;
00587
00588 cnt = (n >> porder) - pred_order;
00589 for (i = 0; i < part; i++) {
00590 k = find_optimal_param(sums[i], cnt);
00591 rc->params[i] = k;
00592 all_bits += rice_encode_count(sums[i], cnt, k);
00593 cnt = n >> porder;
00594 }
00595
00596 rc->porder = porder;
00597
00598 return all_bits;
00599 }
00600
00601
00602 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
00603 uint32_t sums[][MAX_PARTITIONS])
00604 {
00605 int i, j;
00606 int parts;
00607 uint32_t *res, *res_end;
00608
00609
00610 parts = (1 << pmax);
00611 res = &data[pred_order];
00612 res_end = &data[n >> pmax];
00613 for (i = 0; i < parts; i++) {
00614 uint32_t sum = 0;
00615 while (res < res_end)
00616 sum += *(res++);
00617 sums[pmax][i] = sum;
00618 res_end += n >> pmax;
00619 }
00620
00621 for (i = pmax - 1; i >= pmin; i--) {
00622 parts = (1 << i);
00623 for (j = 0; j < parts; j++)
00624 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
00625 }
00626 }
00627
00628
00629 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
00630 int32_t *data, int n, int pred_order)
00631 {
00632 int i;
00633 uint32_t bits[MAX_PARTITION_ORDER+1];
00634 int opt_porder;
00635 RiceContext tmp_rc;
00636 uint32_t *udata;
00637 uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
00638
00639 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
00640 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
00641 assert(pmin <= pmax);
00642
00643 udata = av_malloc(n * sizeof(uint32_t));
00644 for (i = 0; i < n; i++)
00645 udata[i] = (2*data[i]) ^ (data[i]>>31);
00646
00647 calc_sums(pmin, pmax, udata, n, pred_order, sums);
00648
00649 opt_porder = pmin;
00650 bits[pmin] = UINT32_MAX;
00651 for (i = pmin; i <= pmax; i++) {
00652 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
00653 if (bits[i] <= bits[opt_porder]) {
00654 opt_porder = i;
00655 *rc = tmp_rc;
00656 }
00657 }
00658
00659 av_freep(&udata);
00660 return bits[opt_porder];
00661 }
00662
00663
00664 static int get_max_p_order(int max_porder, int n, int order)
00665 {
00666 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
00667 if (order > 0)
00668 porder = FFMIN(porder, av_log2(n/order));
00669 return porder;
00670 }
00671
00672
00673 static uint32_t find_subframe_rice_params(FlacEncodeContext *s,
00674 FlacSubframe *sub, int pred_order)
00675 {
00676 int pmin = get_max_p_order(s->options.min_partition_order,
00677 s->frame.blocksize, pred_order);
00678 int pmax = get_max_p_order(s->options.max_partition_order,
00679 s->frame.blocksize, pred_order);
00680
00681 uint32_t bits = 8 + pred_order * sub->obits + 2 + 4;
00682 if (sub->type == FLAC_SUBFRAME_LPC)
00683 bits += 4 + 5 + pred_order * s->options.lpc_coeff_precision;
00684 bits += calc_rice_params(&sub->rc, pmin, pmax, sub->residual,
00685 s->frame.blocksize, pred_order);
00686 return bits;
00687 }
00688
00689
00690 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
00691 int order)
00692 {
00693 int i;
00694
00695 for (i = 0; i < order; i++)
00696 res[i] = smp[i];
00697
00698 if (order == 0) {
00699 for (i = order; i < n; i++)
00700 res[i] = smp[i];
00701 } else if (order == 1) {
00702 for (i = order; i < n; i++)
00703 res[i] = smp[i] - smp[i-1];
00704 } else if (order == 2) {
00705 int a = smp[order-1] - smp[order-2];
00706 for (i = order; i < n; i += 2) {
00707 int b = smp[i ] - smp[i-1];
00708 res[i] = b - a;
00709 a = smp[i+1] - smp[i ];
00710 res[i+1] = a - b;
00711 }
00712 } else if (order == 3) {
00713 int a = smp[order-1] - smp[order-2];
00714 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00715 for (i = order; i < n; i += 2) {
00716 int b = smp[i ] - smp[i-1];
00717 int d = b - a;
00718 res[i] = d - c;
00719 a = smp[i+1] - smp[i ];
00720 c = a - b;
00721 res[i+1] = c - d;
00722 }
00723 } else {
00724 int a = smp[order-1] - smp[order-2];
00725 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00726 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
00727 for (i = order; i < n; i += 2) {
00728 int b = smp[i ] - smp[i-1];
00729 int d = b - a;
00730 int f = d - c;
00731 res[i ] = f - e;
00732 a = smp[i+1] - smp[i ];
00733 c = a - b;
00734 e = c - d;
00735 res[i+1] = e - f;
00736 }
00737 }
00738 }
00739
00740
00741 #define LPC1(x) {\
00742 int c = coefs[(x)-1];\
00743 p0 += c * s;\
00744 s = smp[i-(x)+1];\
00745 p1 += c * s;\
00746 }
00747
00748 static av_always_inline void encode_residual_lpc_unrolled(int32_t *res,
00749 const int32_t *smp, int n, int order,
00750 const int32_t *coefs, int shift, int big)
00751 {
00752 int i;
00753 for (i = order; i < n; i += 2) {
00754 int s = smp[i-order];
00755 int p0 = 0, p1 = 0;
00756 if (big) {
00757 switch (order) {
00758 case 32: LPC1(32)
00759 case 31: LPC1(31)
00760 case 30: LPC1(30)
00761 case 29: LPC1(29)
00762 case 28: LPC1(28)
00763 case 27: LPC1(27)
00764 case 26: LPC1(26)
00765 case 25: LPC1(25)
00766 case 24: LPC1(24)
00767 case 23: LPC1(23)
00768 case 22: LPC1(22)
00769 case 21: LPC1(21)
00770 case 20: LPC1(20)
00771 case 19: LPC1(19)
00772 case 18: LPC1(18)
00773 case 17: LPC1(17)
00774 case 16: LPC1(16)
00775 case 15: LPC1(15)
00776 case 14: LPC1(14)
00777 case 13: LPC1(13)
00778 case 12: LPC1(12)
00779 case 11: LPC1(11)
00780 case 10: LPC1(10)
00781 case 9: LPC1( 9)
00782 LPC1( 8)
00783 LPC1( 7)
00784 LPC1( 6)
00785 LPC1( 5)
00786 LPC1( 4)
00787 LPC1( 3)
00788 LPC1( 2)
00789 LPC1( 1)
00790 }
00791 } else {
00792 switch (order) {
00793 case 8: LPC1( 8)
00794 case 7: LPC1( 7)
00795 case 6: LPC1( 6)
00796 case 5: LPC1( 5)
00797 case 4: LPC1( 4)
00798 case 3: LPC1( 3)
00799 case 2: LPC1( 2)
00800 case 1: LPC1( 1)
00801 }
00802 }
00803 res[i ] = smp[i ] - (p0 >> shift);
00804 res[i+1] = smp[i+1] - (p1 >> shift);
00805 }
00806 }
00807
00808
00809 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
00810 int order, const int32_t *coefs, int shift)
00811 {
00812 int i;
00813 for (i = 0; i < order; i++)
00814 res[i] = smp[i];
00815 #if CONFIG_SMALL
00816 for (i = order; i < n; i += 2) {
00817 int j;
00818 int s = smp[i];
00819 int p0 = 0, p1 = 0;
00820 for (j = 0; j < order; j++) {
00821 int c = coefs[j];
00822 p1 += c * s;
00823 s = smp[i-j-1];
00824 p0 += c * s;
00825 }
00826 res[i ] = smp[i ] - (p0 >> shift);
00827 res[i+1] = smp[i+1] - (p1 >> shift);
00828 }
00829 #else
00830 switch (order) {
00831 case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
00832 case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
00833 case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
00834 case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
00835 case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
00836 case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
00837 case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
00838 case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
00839 default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
00840 }
00841 #endif
00842 }
00843
00844
00845 static int encode_residual_ch(FlacEncodeContext *s, int ch)
00846 {
00847 int i, n;
00848 int min_order, max_order, opt_order, omethod;
00849 FlacFrame *frame;
00850 FlacSubframe *sub;
00851 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
00852 int shift[MAX_LPC_ORDER];
00853 int32_t *res, *smp;
00854
00855 frame = &s->frame;
00856 sub = &frame->subframes[ch];
00857 res = sub->residual;
00858 smp = sub->samples;
00859 n = frame->blocksize;
00860
00861
00862 for (i = 1; i < n; i++)
00863 if(smp[i] != smp[0])
00864 break;
00865 if (i == n) {
00866 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
00867 res[0] = smp[0];
00868 return subframe_count_exact(s, sub, 0);
00869 }
00870
00871
00872 if (frame->verbatim_only || n < 5) {
00873 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
00874 memcpy(res, smp, n * sizeof(int32_t));
00875 return subframe_count_exact(s, sub, 0);
00876 }
00877
00878 min_order = s->options.min_prediction_order;
00879 max_order = s->options.max_prediction_order;
00880 omethod = s->options.prediction_order_method;
00881
00882
00883 sub->type = FLAC_SUBFRAME_FIXED;
00884 if (s->options.lpc_type == FF_LPC_TYPE_NONE ||
00885 s->options.lpc_type == FF_LPC_TYPE_FIXED || n <= max_order) {
00886 uint32_t bits[MAX_FIXED_ORDER+1];
00887 if (max_order > MAX_FIXED_ORDER)
00888 max_order = MAX_FIXED_ORDER;
00889 opt_order = 0;
00890 bits[0] = UINT32_MAX;
00891 for (i = min_order; i <= max_order; i++) {
00892 encode_residual_fixed(res, smp, n, i);
00893 bits[i] = find_subframe_rice_params(s, sub, i);
00894 if (bits[i] < bits[opt_order])
00895 opt_order = i;
00896 }
00897 sub->order = opt_order;
00898 sub->type_code = sub->type | sub->order;
00899 if (sub->order != max_order) {
00900 encode_residual_fixed(res, smp, n, sub->order);
00901 find_subframe_rice_params(s, sub, sub->order);
00902 }
00903 return subframe_count_exact(s, sub, sub->order);
00904 }
00905
00906
00907 sub->type = FLAC_SUBFRAME_LPC;
00908 opt_order = ff_lpc_calc_coefs(&s->lpc_ctx, smp, n, min_order, max_order,
00909 s->options.lpc_coeff_precision, coefs, shift, s->options.lpc_type,
00910 s->options.lpc_passes, omethod,
00911 MAX_LPC_SHIFT, 0);
00912
00913 if (omethod == ORDER_METHOD_2LEVEL ||
00914 omethod == ORDER_METHOD_4LEVEL ||
00915 omethod == ORDER_METHOD_8LEVEL) {
00916 int levels = 1 << omethod;
00917 uint32_t bits[1 << ORDER_METHOD_8LEVEL];
00918 int order = -1;
00919 int opt_index = levels-1;
00920 opt_order = max_order-1;
00921 bits[opt_index] = UINT32_MAX;
00922 for (i = levels-1; i >= 0; i--) {
00923 int last_order = order;
00924 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
00925 order = av_clip(order, min_order - 1, max_order - 1);
00926 if (order == last_order)
00927 continue;
00928 encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
00929 bits[i] = find_subframe_rice_params(s, sub, order+1);
00930 if (bits[i] < bits[opt_index]) {
00931 opt_index = i;
00932 opt_order = order;
00933 }
00934 }
00935 opt_order++;
00936 } else if (omethod == ORDER_METHOD_SEARCH) {
00937
00938 uint32_t bits[MAX_LPC_ORDER];
00939 opt_order = 0;
00940 bits[0] = UINT32_MAX;
00941 for (i = min_order-1; i < max_order; i++) {
00942 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00943 bits[i] = find_subframe_rice_params(s, sub, i+1);
00944 if (bits[i] < bits[opt_order])
00945 opt_order = i;
00946 }
00947 opt_order++;
00948 } else if (omethod == ORDER_METHOD_LOG) {
00949 uint32_t bits[MAX_LPC_ORDER];
00950 int step;
00951
00952 opt_order = min_order - 1 + (max_order-min_order)/3;
00953 memset(bits, -1, sizeof(bits));
00954
00955 for (step = 16; step; step >>= 1) {
00956 int last = opt_order;
00957 for (i = last-step; i <= last+step; i += step) {
00958 if (i < min_order-1 || i >= max_order || bits[i] < UINT32_MAX)
00959 continue;
00960 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00961 bits[i] = find_subframe_rice_params(s, sub, i+1);
00962 if (bits[i] < bits[opt_order])
00963 opt_order = i;
00964 }
00965 }
00966 opt_order++;
00967 }
00968
00969 sub->order = opt_order;
00970 sub->type_code = sub->type | (sub->order-1);
00971 sub->shift = shift[sub->order-1];
00972 for (i = 0; i < sub->order; i++)
00973 sub->coefs[i] = coefs[sub->order-1][i];
00974
00975 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
00976
00977 find_subframe_rice_params(s, sub, sub->order);
00978
00979 return subframe_count_exact(s, sub, sub->order);
00980 }
00981
00982
00983 static int count_frame_header(FlacEncodeContext *s)
00984 {
00985 uint8_t av_unused tmp;
00986 int count;
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998 count = 32;
00999
01000
01001 PUT_UTF8(s->frame_count, tmp, count += 8;)
01002
01003
01004 if (s->frame.bs_code[0] == 6)
01005 count += 8;
01006 else if (s->frame.bs_code[0] == 7)
01007 count += 16;
01008
01009
01010 count += ((s->sr_code[0] == 12) + (s->sr_code[0] > 12)) * 8;
01011
01012
01013 count += 8;
01014
01015 return count;
01016 }
01017
01018
01019 static int encode_frame(FlacEncodeContext *s)
01020 {
01021 int ch, count;
01022
01023 count = count_frame_header(s);
01024
01025 for (ch = 0; ch < s->channels; ch++)
01026 count += encode_residual_ch(s, ch);
01027
01028 count += (8 - (count & 7)) & 7;
01029 count += 16;
01030
01031 return count >> 3;
01032 }
01033
01034
01035 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
01036 {
01037 int i, best;
01038 int32_t lt, rt;
01039 uint64_t sum[4];
01040 uint64_t score[4];
01041 int k;
01042
01043
01044 sum[0] = sum[1] = sum[2] = sum[3] = 0;
01045 for (i = 2; i < n; i++) {
01046 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
01047 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
01048 sum[2] += FFABS((lt + rt) >> 1);
01049 sum[3] += FFABS(lt - rt);
01050 sum[0] += FFABS(lt);
01051 sum[1] += FFABS(rt);
01052 }
01053
01054 for (i = 0; i < 4; i++) {
01055 k = find_optimal_param(2 * sum[i], n);
01056 sum[i] = rice_encode_count( 2 * sum[i], n, k);
01057 }
01058
01059
01060 score[0] = sum[0] + sum[1];
01061 score[1] = sum[0] + sum[3];
01062 score[2] = sum[1] + sum[3];
01063 score[3] = sum[2] + sum[3];
01064
01065
01066 best = 0;
01067 for (i = 1; i < 4; i++)
01068 if (score[i] < score[best])
01069 best = i;
01070 if (best == 0) {
01071 return FLAC_CHMODE_INDEPENDENT;
01072 } else if (best == 1) {
01073 return FLAC_CHMODE_LEFT_SIDE;
01074 } else if (best == 2) {
01075 return FLAC_CHMODE_RIGHT_SIDE;
01076 } else {
01077 return FLAC_CHMODE_MID_SIDE;
01078 }
01079 }
01080
01081
01085 static void channel_decorrelation(FlacEncodeContext *s)
01086 {
01087 FlacFrame *frame;
01088 int32_t *left, *right;
01089 int i, n;
01090
01091 frame = &s->frame;
01092 n = frame->blocksize;
01093 left = frame->subframes[0].samples;
01094 right = frame->subframes[1].samples;
01095
01096 if (s->channels != 2) {
01097 frame->ch_mode = FLAC_CHMODE_INDEPENDENT;
01098 return;
01099 }
01100
01101 frame->ch_mode = estimate_stereo_mode(left, right, n);
01102
01103
01104 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
01105 return;
01106 if (frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
01107 int32_t tmp;
01108 for (i = 0; i < n; i++) {
01109 tmp = left[i];
01110 left[i] = (tmp + right[i]) >> 1;
01111 right[i] = tmp - right[i];
01112 }
01113 frame->subframes[1].obits++;
01114 } else if (frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
01115 for (i = 0; i < n; i++)
01116 right[i] = left[i] - right[i];
01117 frame->subframes[1].obits++;
01118 } else {
01119 for (i = 0; i < n; i++)
01120 left[i] -= right[i];
01121 frame->subframes[0].obits++;
01122 }
01123 }
01124
01125
01126 static void write_utf8(PutBitContext *pb, uint32_t val)
01127 {
01128 uint8_t tmp;
01129 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
01130 }
01131
01132
01133 static void write_frame_header(FlacEncodeContext *s)
01134 {
01135 FlacFrame *frame;
01136 int crc;
01137
01138 frame = &s->frame;
01139
01140 put_bits(&s->pb, 16, 0xFFF8);
01141 put_bits(&s->pb, 4, frame->bs_code[0]);
01142 put_bits(&s->pb, 4, s->sr_code[0]);
01143
01144 if (frame->ch_mode == FLAC_CHMODE_INDEPENDENT)
01145 put_bits(&s->pb, 4, s->channels-1);
01146 else
01147 put_bits(&s->pb, 4, frame->ch_mode);
01148
01149 put_bits(&s->pb, 3, 4);
01150 put_bits(&s->pb, 1, 0);
01151 write_utf8(&s->pb, s->frame_count);
01152
01153 if (frame->bs_code[0] == 6)
01154 put_bits(&s->pb, 8, frame->bs_code[1]);
01155 else if (frame->bs_code[0] == 7)
01156 put_bits(&s->pb, 16, frame->bs_code[1]);
01157
01158 if (s->sr_code[0] == 12)
01159 put_bits(&s->pb, 8, s->sr_code[1]);
01160 else if (s->sr_code[0] > 12)
01161 put_bits(&s->pb, 16, s->sr_code[1]);
01162
01163 flush_put_bits(&s->pb);
01164 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, s->pb.buf,
01165 put_bits_count(&s->pb) >> 3);
01166 put_bits(&s->pb, 8, crc);
01167 }
01168
01169
01170 static void write_subframes(FlacEncodeContext *s)
01171 {
01172 int ch;
01173
01174 for (ch = 0; ch < s->channels; ch++) {
01175 FlacSubframe *sub = &s->frame.subframes[ch];
01176 int i, p, porder, psize;
01177 int32_t *part_end;
01178 int32_t *res = sub->residual;
01179 int32_t *frame_end = &sub->residual[s->frame.blocksize];
01180
01181
01182 put_bits(&s->pb, 1, 0);
01183 put_bits(&s->pb, 6, sub->type_code);
01184 put_bits(&s->pb, 1, 0);
01185
01186
01187 if (sub->type == FLAC_SUBFRAME_CONSTANT) {
01188 put_sbits(&s->pb, sub->obits, res[0]);
01189 } else if (sub->type == FLAC_SUBFRAME_VERBATIM) {
01190 while (res < frame_end)
01191 put_sbits(&s->pb, sub->obits, *res++);
01192 } else {
01193
01194 for (i = 0; i < sub->order; i++)
01195 put_sbits(&s->pb, sub->obits, *res++);
01196
01197
01198 if (sub->type == FLAC_SUBFRAME_LPC) {
01199 int cbits = s->options.lpc_coeff_precision;
01200 put_bits( &s->pb, 4, cbits-1);
01201 put_sbits(&s->pb, 5, sub->shift);
01202 for (i = 0; i < sub->order; i++)
01203 put_sbits(&s->pb, cbits, sub->coefs[i]);
01204 }
01205
01206
01207 put_bits(&s->pb, 2, 0);
01208
01209
01210 porder = sub->rc.porder;
01211 psize = s->frame.blocksize >> porder;
01212 put_bits(&s->pb, 4, porder);
01213
01214
01215 part_end = &sub->residual[psize];
01216 for (p = 0; p < 1 << porder; p++) {
01217 int k = sub->rc.params[p];
01218 put_bits(&s->pb, 4, k);
01219 while (res < part_end)
01220 set_sr_golomb_flac(&s->pb, *res++, k, INT32_MAX, 0);
01221 part_end = FFMIN(frame_end, part_end + psize);
01222 }
01223 }
01224 }
01225 }
01226
01227
01228 static void write_frame_footer(FlacEncodeContext *s)
01229 {
01230 int crc;
01231 flush_put_bits(&s->pb);
01232 crc = av_bswap16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0, s->pb.buf,
01233 put_bits_count(&s->pb)>>3));
01234 put_bits(&s->pb, 16, crc);
01235 flush_put_bits(&s->pb);
01236 }
01237
01238
01239 static int write_frame(FlacEncodeContext *s, uint8_t *frame, int buf_size)
01240 {
01241 init_put_bits(&s->pb, frame, buf_size);
01242 write_frame_header(s);
01243 write_subframes(s);
01244 write_frame_footer(s);
01245 return put_bits_count(&s->pb) >> 3;
01246 }
01247
01248
01249 static void update_md5_sum(FlacEncodeContext *s, const int16_t *samples)
01250 {
01251 #if HAVE_BIGENDIAN
01252 int i;
01253 for (i = 0; i < s->frame.blocksize * s->channels; i++) {
01254 int16_t smp = av_le2ne16(samples[i]);
01255 av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
01256 }
01257 #else
01258 av_md5_update(s->md5ctx, (const uint8_t *)samples, s->frame.blocksize*s->channels*2);
01259 #endif
01260 }
01261
01262
01263 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
01264 int buf_size, void *data)
01265 {
01266 FlacEncodeContext *s;
01267 const int16_t *samples = data;
01268 int frame_bytes, out_bytes;
01269
01270 s = avctx->priv_data;
01271
01272
01273 if (!data) {
01274 s->max_framesize = s->max_encoded_framesize;
01275 av_md5_final(s->md5ctx, s->md5sum);
01276 write_streaminfo(s, avctx->extradata);
01277 return 0;
01278 }
01279
01280
01281 if (avctx->frame_size < s->frame.blocksize) {
01282 s->max_framesize = ff_flac_get_max_frame_size(avctx->frame_size,
01283 s->channels, 16);
01284 }
01285
01286 init_frame(s);
01287
01288 copy_samples(s, samples);
01289
01290 channel_decorrelation(s);
01291
01292 frame_bytes = encode_frame(s);
01293
01294
01295
01296 if (frame_bytes > s->max_framesize) {
01297 s->frame.verbatim_only = 1;
01298 frame_bytes = encode_frame(s);
01299 }
01300
01301 if (buf_size < frame_bytes) {
01302 av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
01303 return 0;
01304 }
01305 out_bytes = write_frame(s, frame, buf_size);
01306
01307 s->frame_count++;
01308 avctx->coded_frame->pts = s->sample_count;
01309 s->sample_count += avctx->frame_size;
01310 update_md5_sum(s, samples);
01311 if (out_bytes > s->max_encoded_framesize)
01312 s->max_encoded_framesize = out_bytes;
01313 if (out_bytes < s->min_framesize)
01314 s->min_framesize = out_bytes;
01315
01316 return out_bytes;
01317 }
01318
01319
01320 static av_cold int flac_encode_close(AVCodecContext *avctx)
01321 {
01322 if (avctx->priv_data) {
01323 FlacEncodeContext *s = avctx->priv_data;
01324 av_freep(&s->md5ctx);
01325 ff_lpc_end(&s->lpc_ctx);
01326 }
01327 av_freep(&avctx->extradata);
01328 avctx->extradata_size = 0;
01329 av_freep(&avctx->coded_frame);
01330 return 0;
01331 }
01332
01333 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
01334 static const AVOption options[] = {
01335 { "lpc_coeff_precision", "LPC coefficient precision", offsetof(FlacEncodeContext, options.lpc_coeff_precision), AV_OPT_TYPE_INT, {.dbl = 15 }, 0, MAX_LPC_PRECISION, FLAGS },
01336 { "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" },
01337 { "none", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_NONE }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01338 { "fixed", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_FIXED }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01339 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_LEVINSON }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01340 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_LPC_TYPE_CHOLESKY }, INT_MIN, INT_MAX, FLAGS, "lpc_type" },
01341 { "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 },
01342 { "min_partition_order", NULL, offsetof(FlacEncodeContext, options.min_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
01343 { "max_partition_order", NULL, offsetof(FlacEncodeContext, options.max_partition_order), AV_OPT_TYPE_INT, {.dbl = -1 }, -1, MAX_PARTITION_ORDER, FLAGS },
01344 { "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" },
01345 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_EST }, INT_MIN, INT_MAX, FLAGS, "predm" },
01346 { "2level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_2LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01347 { "4level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_4LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01348 { "8level", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_8LEVEL }, INT_MIN, INT_MAX, FLAGS, "predm" },
01349 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_SEARCH }, INT_MIN, INT_MAX, FLAGS, "predm" },
01350 { "log", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = ORDER_METHOD_LOG }, INT_MIN, INT_MAX, FLAGS, "predm" },
01351 { NULL },
01352 };
01353
01354 static const AVClass flac_encoder_class = {
01355 "FLAC encoder",
01356 av_default_item_name,
01357 options,
01358 LIBAVUTIL_VERSION_INT,
01359 };
01360
01361 AVCodec ff_flac_encoder = {
01362 .name = "flac",
01363 .type = AVMEDIA_TYPE_AUDIO,
01364 .id = CODEC_ID_FLAC,
01365 .priv_data_size = sizeof(FlacEncodeContext),
01366 .init = flac_encode_init,
01367 .encode = flac_encode_frame,
01368 .close = flac_encode_close,
01369 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
01370 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
01371 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
01372 .priv_class = &flac_encoder_class,
01373 };