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