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