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