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