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