00001
00022 #include "libavutil/crc.h"
00023 #include "libavutil/md5.h"
00024 #include "avcodec.h"
00025 #include "get_bits.h"
00026 #include "golomb.h"
00027 #include "lpc.h"
00028 #include "flac.h"
00029 #include "flacdata.h"
00030
00031 #define FLAC_SUBFRAME_CONSTANT 0
00032 #define FLAC_SUBFRAME_VERBATIM 1
00033 #define FLAC_SUBFRAME_FIXED 8
00034 #define FLAC_SUBFRAME_LPC 32
00035
00036 #define MAX_FIXED_ORDER 4
00037 #define MAX_PARTITION_ORDER 8
00038 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
00039 #define MAX_LPC_PRECISION 15
00040 #define MAX_LPC_SHIFT 15
00041 #define MAX_RICE_PARAM 14
00042
00043 typedef struct CompressionOptions {
00044 int compression_level;
00045 int block_time_ms;
00046 enum AVLPCType lpc_type;
00047 int lpc_passes;
00048 int lpc_coeff_precision;
00049 int min_prediction_order;
00050 int max_prediction_order;
00051 int prediction_order_method;
00052 int min_partition_order;
00053 int max_partition_order;
00054 } CompressionOptions;
00055
00056 typedef struct RiceContext {
00057 int porder;
00058 int params[MAX_PARTITIONS];
00059 } RiceContext;
00060
00061 typedef struct FlacSubframe {
00062 int type;
00063 int type_code;
00064 int obits;
00065 int order;
00066 int32_t coefs[MAX_LPC_ORDER];
00067 int shift;
00068 RiceContext rc;
00069 int32_t samples[FLAC_MAX_BLOCKSIZE];
00070 int32_t residual[FLAC_MAX_BLOCKSIZE+1];
00071 } FlacSubframe;
00072
00073 typedef struct FlacFrame {
00074 FlacSubframe subframes[FLAC_MAX_CHANNELS];
00075 int blocksize;
00076 int bs_code[2];
00077 uint8_t crc8;
00078 int ch_mode;
00079 int verbatim_only;
00080 } FlacFrame;
00081
00082 typedef struct FlacEncodeContext {
00083 PutBitContext pb;
00084 int channels;
00085 int samplerate;
00086 int sr_code[2];
00087 int max_blocksize;
00088 int min_framesize;
00089 int max_framesize;
00090 int max_encoded_framesize;
00091 uint32_t frame_count;
00092 uint64_t sample_count;
00093 uint8_t md5sum[16];
00094 FlacFrame frame;
00095 CompressionOptions options;
00096 AVCodecContext *avctx;
00097 LPCContext lpc_ctx;
00098 struct AVMD5 *md5ctx;
00099 } FlacEncodeContext;
00100
00101
00105 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
00106 {
00107 PutBitContext pb;
00108
00109 memset(header, 0, FLAC_STREAMINFO_SIZE);
00110 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
00111
00112
00113 put_bits(&pb, 16, s->max_blocksize);
00114 put_bits(&pb, 16, s->max_blocksize);
00115 put_bits(&pb, 24, s->min_framesize);
00116 put_bits(&pb, 24, s->max_framesize);
00117 put_bits(&pb, 20, s->samplerate);
00118 put_bits(&pb, 3, s->channels-1);
00119 put_bits(&pb, 5, 15);
00120
00121 put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
00122 put_bits(&pb, 12, s->sample_count & 0x000000FFFLL);
00123 flush_put_bits(&pb);
00124 memcpy(&header[18], s->md5sum, 16);
00125 }
00126
00127
00132 static int select_blocksize(int samplerate, int block_time_ms)
00133 {
00134 int i;
00135 int target;
00136 int blocksize;
00137
00138 assert(samplerate > 0);
00139 blocksize = ff_flac_blocksize_table[1];
00140 target = (samplerate * block_time_ms) / 1000;
00141 for (i = 0; i < 16; i++) {
00142 if (target >= ff_flac_blocksize_table[i] &&
00143 ff_flac_blocksize_table[i] > blocksize) {
00144 blocksize = ff_flac_blocksize_table[i];
00145 }
00146 }
00147 return blocksize;
00148 }
00149
00150
00151 static av_cold void dprint_compression_options(FlacEncodeContext *s)
00152 {
00153 AVCodecContext *avctx = s->avctx;
00154 CompressionOptions *opt = &s->options;
00155
00156 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", opt->compression_level);
00157
00158 switch (opt->lpc_type) {
00159 case AV_LPC_TYPE_NONE:
00160 av_log(avctx, AV_LOG_DEBUG, " lpc type: None\n");
00161 break;
00162 case AV_LPC_TYPE_FIXED:
00163 av_log(avctx, AV_LOG_DEBUG, " lpc type: Fixed pre-defined coefficients\n");
00164 break;
00165 case AV_LPC_TYPE_LEVINSON:
00166 av_log(avctx, AV_LOG_DEBUG, " lpc type: Levinson-Durbin recursion with Welch window\n");
00167 break;
00168 case AV_LPC_TYPE_CHOLESKY:
00169 av_log(avctx, AV_LOG_DEBUG, " lpc type: Cholesky factorization, %d pass%s\n",
00170 opt->lpc_passes, opt->lpc_passes == 1 ? "" : "es");
00171 break;
00172 }
00173
00174 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
00175 opt->min_prediction_order, opt->max_prediction_order);
00176
00177 switch (opt->prediction_order_method) {
00178 case ORDER_METHOD_EST:
00179 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "estimate");
00180 break;
00181 case ORDER_METHOD_2LEVEL:
00182 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "2-level");
00183 break;
00184 case ORDER_METHOD_4LEVEL:
00185 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "4-level");
00186 break;
00187 case ORDER_METHOD_8LEVEL:
00188 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "8-level");
00189 break;
00190 case ORDER_METHOD_SEARCH:
00191 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "full search");
00192 break;
00193 case ORDER_METHOD_LOG:
00194 av_log(avctx, AV_LOG_DEBUG, " order method: %s\n", "log search");
00195 break;
00196 }
00197
00198
00199 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
00200 opt->min_partition_order, opt->max_partition_order);
00201
00202 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", avctx->frame_size);
00203
00204 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
00205 opt->lpc_coeff_precision);
00206 }
00207
00208
00209 static av_cold int flac_encode_init(AVCodecContext *avctx)
00210 {
00211 int freq = avctx->sample_rate;
00212 int channels = avctx->channels;
00213 FlacEncodeContext *s = avctx->priv_data;
00214 int i, level, ret;
00215 uint8_t *streaminfo;
00216
00217 s->avctx = avctx;
00218
00219 if (avctx->sample_fmt != AV_SAMPLE_FMT_S16)
00220 return -1;
00221
00222 if (channels < 1 || channels > FLAC_MAX_CHANNELS)
00223 return -1;
00224 s->channels = channels;
00225
00226
00227 if (freq < 1)
00228 return -1;
00229 for (i = 4; i < 12; i++) {
00230 if (freq == ff_flac_sample_rate_table[i]) {
00231 s->samplerate = ff_flac_sample_rate_table[i];
00232 s->sr_code[0] = i;
00233 s->sr_code[1] = 0;
00234 break;
00235 }
00236 }
00237
00238 if (i == 12) {
00239 if (freq % 1000 == 0 && freq < 255000) {
00240 s->sr_code[0] = 12;
00241 s->sr_code[1] = freq / 1000;
00242 } else if (freq % 10 == 0 && freq < 655350) {
00243 s->sr_code[0] = 14;
00244 s->sr_code[1] = freq / 10;
00245 } else if (freq < 65535) {
00246 s->sr_code[0] = 13;
00247 s->sr_code[1] = freq;
00248 } else {
00249 return -1;
00250 }
00251 s->samplerate = freq;
00252 }
00253
00254
00255 if (avctx->compression_level < 0)
00256 s->options.compression_level = 5;
00257 else
00258 s->options.compression_level = avctx->compression_level;
00259
00260 level = s->options.compression_level;
00261 if (level > 12) {
00262 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
00263 s->options.compression_level);
00264 return -1;
00265 }
00266
00267 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
00268
00269 s->options.lpc_type = ((int[]){ AV_LPC_TYPE_FIXED, AV_LPC_TYPE_FIXED, AV_LPC_TYPE_FIXED,
00270 AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
00271 AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
00272 AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON, AV_LPC_TYPE_LEVINSON,
00273 AV_LPC_TYPE_LEVINSON})[level];
00274
00275 s->options.min_prediction_order = ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
00276 s->options.max_prediction_order = ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
00277
00278 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00279 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00280 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
00281 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
00282 ORDER_METHOD_SEARCH})[level];
00283
00284 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
00285 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
00286
00287
00288 #if FF_API_USE_LPC
00289
00290 if (avctx->use_lpc == 0) {
00291 s->options.lpc_type = AV_LPC_TYPE_FIXED;
00292 } else if (avctx->use_lpc == 1) {
00293 s->options.lpc_type = AV_LPC_TYPE_LEVINSON;
00294 } else if (avctx->use_lpc > 1) {
00295 s->options.lpc_type = AV_LPC_TYPE_CHOLESKY;
00296 s->options.lpc_passes = avctx->use_lpc - 1;
00297 }
00298 #endif
00299 if (avctx->lpc_type > AV_LPC_TYPE_DEFAULT) {
00300 if (avctx->lpc_type > AV_LPC_TYPE_CHOLESKY) {
00301 av_log(avctx, AV_LOG_ERROR, "unknown lpc type: %d\n", avctx->lpc_type);
00302 return -1;
00303 }
00304 s->options.lpc_type = avctx->lpc_type;
00305 if (s->options.lpc_type == AV_LPC_TYPE_CHOLESKY) {
00306 if (avctx->lpc_passes < 0) {
00307
00308 s->options.lpc_passes = 2;
00309 } else if (avctx->lpc_passes == 0) {
00310 av_log(avctx, AV_LOG_ERROR, "invalid number of lpc passes: %d\n",
00311 avctx->lpc_passes);
00312 return -1;
00313 } else {
00314 s->options.lpc_passes = avctx->lpc_passes;
00315 }
00316 }
00317 }
00318
00319 if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
00320 s->options.min_prediction_order = 0;
00321 } else if (avctx->min_prediction_order >= 0) {
00322 if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
00323 if (avctx->min_prediction_order > MAX_FIXED_ORDER) {
00324 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00325 avctx->min_prediction_order);
00326 return -1;
00327 }
00328 } else if (avctx->min_prediction_order < MIN_LPC_ORDER ||
00329 avctx->min_prediction_order > MAX_LPC_ORDER) {
00330 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00331 avctx->min_prediction_order);
00332 return -1;
00333 }
00334 s->options.min_prediction_order = avctx->min_prediction_order;
00335 }
00336 if (s->options.lpc_type == AV_LPC_TYPE_NONE) {
00337 s->options.max_prediction_order = 0;
00338 } else if (avctx->max_prediction_order >= 0) {
00339 if (s->options.lpc_type == AV_LPC_TYPE_FIXED) {
00340 if (avctx->max_prediction_order > MAX_FIXED_ORDER) {
00341 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00342 avctx->max_prediction_order);
00343 return -1;
00344 }
00345 } else if (avctx->max_prediction_order < MIN_LPC_ORDER ||
00346 avctx->max_prediction_order > MAX_LPC_ORDER) {
00347 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00348 avctx->max_prediction_order);
00349 return -1;
00350 }
00351 s->options.max_prediction_order = avctx->max_prediction_order;
00352 }
00353 if (s->options.max_prediction_order < s->options.min_prediction_order) {
00354 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
00355 s->options.min_prediction_order, s->options.max_prediction_order);
00356 return -1;
00357 }
00358
00359 if (avctx->prediction_order_method >= 0) {
00360 if (avctx->prediction_order_method > ORDER_METHOD_LOG) {
00361 av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
00362 avctx->prediction_order_method);
00363 return -1;
00364 }
00365 s->options.prediction_order_method = avctx->prediction_order_method;
00366 }
00367
00368 if (avctx->min_partition_order >= 0) {
00369 if (avctx->min_partition_order > MAX_PARTITION_ORDER) {
00370 av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
00371 avctx->min_partition_order);
00372 return -1;
00373 }
00374 s->options.min_partition_order = avctx->min_partition_order;
00375 }
00376 if (avctx->max_partition_order >= 0) {
00377 if (avctx->max_partition_order > MAX_PARTITION_ORDER) {
00378 av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
00379 avctx->max_partition_order);
00380 return -1;
00381 }
00382 s->options.max_partition_order = avctx->max_partition_order;
00383 }
00384 if (s->options.max_partition_order < s->options.min_partition_order) {
00385 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
00386 s->options.min_partition_order, s->options.max_partition_order);
00387 return -1;
00388 }
00389
00390 if (avctx->frame_size > 0) {
00391 if (avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
00392 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
00393 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
00394 avctx->frame_size);
00395 return -1;
00396 }
00397 } else {
00398 s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
00399 }
00400 s->max_blocksize = s->avctx->frame_size;
00401
00402
00403 if (avctx->lpc_coeff_precision > 0) {
00404 if (avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
00405 av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
00406 avctx->lpc_coeff_precision);
00407 return -1;
00408 }
00409 s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
00410 } else {
00411
00412 s->options.lpc_coeff_precision = 15;
00413 }
00414
00415
00416 s->max_framesize = ff_flac_get_max_frame_size(s->avctx->frame_size,
00417 s->channels, 16);
00418
00419
00420 s->md5ctx = av_malloc(av_md5_size);
00421 if (!s->md5ctx)
00422 return AVERROR(ENOMEM);
00423 av_md5_init(s->md5ctx);
00424
00425 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
00426 if (!streaminfo)
00427 return AVERROR(ENOMEM);
00428 write_streaminfo(s, streaminfo);
00429 avctx->extradata = streaminfo;
00430 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
00431
00432 s->frame_count = 0;
00433 s->min_framesize = s->max_framesize;
00434
00435 avctx->coded_frame = avcodec_alloc_frame();
00436 if (!avctx->coded_frame)
00437 return AVERROR(ENOMEM);
00438
00439 ret = ff_lpc_init(&s->lpc_ctx, avctx->frame_size,
00440 s->options.max_prediction_order, AV_LPC_TYPE_LEVINSON);
00441
00442 dprint_compression_options(s);
00443
00444 return ret;
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->lpc_ctx, 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 ff_lpc_end(&s->lpc_ctx);
01321 }
01322 av_freep(&avctx->extradata);
01323 avctx->extradata_size = 0;
01324 av_freep(&avctx->coded_frame);
01325 return 0;
01326 }
01327
01328
01329 AVCodec ff_flac_encoder = {
01330 "flac",
01331 AVMEDIA_TYPE_AUDIO,
01332 CODEC_ID_FLAC,
01333 sizeof(FlacEncodeContext),
01334 flac_encode_init,
01335 flac_encode_frame,
01336 flac_encode_close,
01337 NULL,
01338 .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
01339 .sample_fmts = (const enum AVSampleFormat[]){AV_SAMPLE_FMT_S16,AV_SAMPLE_FMT_NONE},
01340 .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
01341 };