00001
00022 #include "avcodec.h"
00023 #include "bitstream.h"
00024 #include "crc.h"
00025 #include "dsputil.h"
00026 #include "golomb.h"
00027 #include "lls.h"
00028
00029 #define FLAC_MAX_CH 8
00030 #define FLAC_MIN_BLOCKSIZE 16
00031 #define FLAC_MAX_BLOCKSIZE 65535
00032
00033 #define FLAC_SUBFRAME_CONSTANT 0
00034 #define FLAC_SUBFRAME_VERBATIM 1
00035 #define FLAC_SUBFRAME_FIXED 8
00036 #define FLAC_SUBFRAME_LPC 32
00037
00038 #define FLAC_CHMODE_NOT_STEREO 0
00039 #define FLAC_CHMODE_LEFT_RIGHT 1
00040 #define FLAC_CHMODE_LEFT_SIDE 8
00041 #define FLAC_CHMODE_RIGHT_SIDE 9
00042 #define FLAC_CHMODE_MID_SIDE 10
00043
00044 #define ORDER_METHOD_EST 0
00045 #define ORDER_METHOD_2LEVEL 1
00046 #define ORDER_METHOD_4LEVEL 2
00047 #define ORDER_METHOD_8LEVEL 3
00048 #define ORDER_METHOD_SEARCH 4
00049 #define ORDER_METHOD_LOG 5
00050
00051 #define FLAC_STREAMINFO_SIZE 34
00052
00053 #define MIN_LPC_ORDER 1
00054 #define MAX_LPC_ORDER 32
00055 #define MAX_FIXED_ORDER 4
00056 #define MAX_PARTITION_ORDER 8
00057 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
00058 #define MAX_LPC_PRECISION 15
00059 #define MAX_LPC_SHIFT 15
00060 #define MAX_RICE_PARAM 14
00061
00062 typedef struct CompressionOptions {
00063 int compression_level;
00064 int block_time_ms;
00065 int use_lpc;
00066 int lpc_coeff_precision;
00067 int min_prediction_order;
00068 int max_prediction_order;
00069 int prediction_order_method;
00070 int min_partition_order;
00071 int max_partition_order;
00072 } CompressionOptions;
00073
00074 typedef struct RiceContext {
00075 int porder;
00076 int params[MAX_PARTITIONS];
00077 } RiceContext;
00078
00079 typedef struct FlacSubframe {
00080 int type;
00081 int type_code;
00082 int obits;
00083 int order;
00084 int32_t coefs[MAX_LPC_ORDER];
00085 int shift;
00086 RiceContext rc;
00087 int32_t samples[FLAC_MAX_BLOCKSIZE];
00088 int32_t residual[FLAC_MAX_BLOCKSIZE+1];
00089 } FlacSubframe;
00090
00091 typedef struct FlacFrame {
00092 FlacSubframe subframes[FLAC_MAX_CH];
00093 int blocksize;
00094 int bs_code[2];
00095 uint8_t crc8;
00096 int ch_mode;
00097 } FlacFrame;
00098
00099 typedef struct FlacEncodeContext {
00100 PutBitContext pb;
00101 int channels;
00102 int ch_code;
00103 int samplerate;
00104 int sr_code[2];
00105 int blocksize;
00106 int max_framesize;
00107 uint32_t frame_count;
00108 FlacFrame frame;
00109 CompressionOptions options;
00110 AVCodecContext *avctx;
00111 DSPContext dsp;
00112 } FlacEncodeContext;
00113
00114 static const int flac_samplerates[16] = {
00115 0, 0, 0, 0,
00116 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
00117 0, 0, 0, 0
00118 };
00119
00120 static const int flac_blocksizes[16] = {
00121 0,
00122 192,
00123 576, 1152, 2304, 4608,
00124 0, 0,
00125 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
00126 };
00127
00131 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
00132 {
00133 PutBitContext pb;
00134
00135 memset(header, 0, FLAC_STREAMINFO_SIZE);
00136 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
00137
00138
00139 put_bits(&pb, 16, s->blocksize);
00140 put_bits(&pb, 16, s->blocksize);
00141 put_bits(&pb, 24, 0);
00142 put_bits(&pb, 24, s->max_framesize);
00143 put_bits(&pb, 20, s->samplerate);
00144 put_bits(&pb, 3, s->channels-1);
00145 put_bits(&pb, 5, 15);
00146 flush_put_bits(&pb);
00147
00148
00149 }
00150
00155 static int select_blocksize(int samplerate, int block_time_ms)
00156 {
00157 int i;
00158 int target;
00159 int blocksize;
00160
00161 assert(samplerate > 0);
00162 blocksize = flac_blocksizes[1];
00163 target = (samplerate * block_time_ms) / 1000;
00164 for(i=0; i<16; i++) {
00165 if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
00166 blocksize = flac_blocksizes[i];
00167 }
00168 }
00169 return blocksize;
00170 }
00171
00172 static int flac_encode_init(AVCodecContext *avctx)
00173 {
00174 int freq = avctx->sample_rate;
00175 int channels = avctx->channels;
00176 FlacEncodeContext *s = avctx->priv_data;
00177 int i, level;
00178 uint8_t *streaminfo;
00179
00180 s->avctx = avctx;
00181
00182 dsputil_init(&s->dsp, avctx);
00183
00184 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
00185 return -1;
00186 }
00187
00188 if(channels < 1 || channels > FLAC_MAX_CH) {
00189 return -1;
00190 }
00191 s->channels = channels;
00192 s->ch_code = s->channels-1;
00193
00194
00195 if(freq < 1)
00196 return -1;
00197 for(i=4; i<12; i++) {
00198 if(freq == flac_samplerates[i]) {
00199 s->samplerate = flac_samplerates[i];
00200 s->sr_code[0] = i;
00201 s->sr_code[1] = 0;
00202 break;
00203 }
00204 }
00205
00206 if(i == 12) {
00207 if(freq % 1000 == 0 && freq < 255000) {
00208 s->sr_code[0] = 12;
00209 s->sr_code[1] = freq / 1000;
00210 } else if(freq % 10 == 0 && freq < 655350) {
00211 s->sr_code[0] = 14;
00212 s->sr_code[1] = freq / 10;
00213 } else if(freq < 65535) {
00214 s->sr_code[0] = 13;
00215 s->sr_code[1] = freq;
00216 } else {
00217 return -1;
00218 }
00219 s->samplerate = freq;
00220 }
00221
00222
00223 if(avctx->compression_level < 0) {
00224 s->options.compression_level = 5;
00225 } else {
00226 s->options.compression_level = avctx->compression_level;
00227 }
00228 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level);
00229
00230 level= s->options.compression_level;
00231 if(level > 12) {
00232 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
00233 s->options.compression_level);
00234 return -1;
00235 }
00236
00237 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
00238 s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
00239 s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
00240 s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
00241 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00242 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
00243 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
00244 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
00245 ORDER_METHOD_SEARCH})[level];
00246 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
00247 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
00248
00249
00250 if(avctx->use_lpc >= 0) {
00251 s->options.use_lpc = av_clip(avctx->use_lpc, 0, 11);
00252 }
00253 if(s->options.use_lpc == 1)
00254 av_log(avctx, AV_LOG_DEBUG, " use lpc: Levinson-Durbin recursion with Welch window\n");
00255 else if(s->options.use_lpc > 1)
00256 av_log(avctx, AV_LOG_DEBUG, " use lpc: Cholesky factorization\n");
00257
00258 if(avctx->min_prediction_order >= 0) {
00259 if(s->options.use_lpc) {
00260 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
00261 avctx->min_prediction_order > MAX_LPC_ORDER) {
00262 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00263 avctx->min_prediction_order);
00264 return -1;
00265 }
00266 } else {
00267 if(avctx->min_prediction_order > MAX_FIXED_ORDER) {
00268 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
00269 avctx->min_prediction_order);
00270 return -1;
00271 }
00272 }
00273 s->options.min_prediction_order = avctx->min_prediction_order;
00274 }
00275 if(avctx->max_prediction_order >= 0) {
00276 if(s->options.use_lpc) {
00277 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
00278 avctx->max_prediction_order > MAX_LPC_ORDER) {
00279 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00280 avctx->max_prediction_order);
00281 return -1;
00282 }
00283 } else {
00284 if(avctx->max_prediction_order > MAX_FIXED_ORDER) {
00285 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
00286 avctx->max_prediction_order);
00287 return -1;
00288 }
00289 }
00290 s->options.max_prediction_order = avctx->max_prediction_order;
00291 }
00292 if(s->options.max_prediction_order < s->options.min_prediction_order) {
00293 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
00294 s->options.min_prediction_order, s->options.max_prediction_order);
00295 return -1;
00296 }
00297 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
00298 s->options.min_prediction_order, s->options.max_prediction_order);
00299
00300 if(avctx->prediction_order_method >= 0) {
00301 if(avctx->prediction_order_method > ORDER_METHOD_LOG) {
00302 av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
00303 avctx->prediction_order_method);
00304 return -1;
00305 }
00306 s->options.prediction_order_method = avctx->prediction_order_method;
00307 }
00308 switch(s->options.prediction_order_method) {
00309 case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00310 "estimate"); break;
00311 case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00312 "2-level"); break;
00313 case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00314 "4-level"); break;
00315 case ORDER_METHOD_8LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00316 "8-level"); break;
00317 case ORDER_METHOD_SEARCH: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00318 "full search"); break;
00319 case ORDER_METHOD_LOG: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
00320 "log search"); break;
00321 }
00322
00323 if(avctx->min_partition_order >= 0) {
00324 if(avctx->min_partition_order > MAX_PARTITION_ORDER) {
00325 av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
00326 avctx->min_partition_order);
00327 return -1;
00328 }
00329 s->options.min_partition_order = avctx->min_partition_order;
00330 }
00331 if(avctx->max_partition_order >= 0) {
00332 if(avctx->max_partition_order > MAX_PARTITION_ORDER) {
00333 av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
00334 avctx->max_partition_order);
00335 return -1;
00336 }
00337 s->options.max_partition_order = avctx->max_partition_order;
00338 }
00339 if(s->options.max_partition_order < s->options.min_partition_order) {
00340 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
00341 s->options.min_partition_order, s->options.max_partition_order);
00342 return -1;
00343 }
00344 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
00345 s->options.min_partition_order, s->options.max_partition_order);
00346
00347 if(avctx->frame_size > 0) {
00348 if(avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
00349 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
00350 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
00351 avctx->frame_size);
00352 return -1;
00353 }
00354 s->blocksize = avctx->frame_size;
00355 } else {
00356 s->blocksize = select_blocksize(s->samplerate, s->options.block_time_ms);
00357 avctx->frame_size = s->blocksize;
00358 }
00359 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->blocksize);
00360
00361
00362 if(avctx->lpc_coeff_precision > 0) {
00363 if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
00364 av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
00365 avctx->lpc_coeff_precision);
00366 return -1;
00367 }
00368 s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
00369 } else {
00370
00371 if( s->blocksize <= 192) s->options.lpc_coeff_precision = 7;
00372 else if(s->blocksize <= 384) s->options.lpc_coeff_precision = 8;
00373 else if(s->blocksize <= 576) s->options.lpc_coeff_precision = 9;
00374 else if(s->blocksize <= 1152) s->options.lpc_coeff_precision = 10;
00375 else if(s->blocksize <= 2304) s->options.lpc_coeff_precision = 11;
00376 else if(s->blocksize <= 4608) s->options.lpc_coeff_precision = 12;
00377 else if(s->blocksize <= 8192) s->options.lpc_coeff_precision = 13;
00378 else if(s->blocksize <= 16384) s->options.lpc_coeff_precision = 14;
00379 else s->options.lpc_coeff_precision = 15;
00380 }
00381 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
00382 s->options.lpc_coeff_precision);
00383
00384
00385 if(s->channels == 2) {
00386 s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3);
00387 } else {
00388 s->max_framesize = 14 + (s->blocksize * s->channels * 2);
00389 }
00390
00391 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
00392 write_streaminfo(s, streaminfo);
00393 avctx->extradata = streaminfo;
00394 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
00395
00396 s->frame_count = 0;
00397
00398 avctx->coded_frame = avcodec_alloc_frame();
00399 avctx->coded_frame->key_frame = 1;
00400
00401 return 0;
00402 }
00403
00404 static void init_frame(FlacEncodeContext *s)
00405 {
00406 int i, ch;
00407 FlacFrame *frame;
00408
00409 frame = &s->frame;
00410
00411 for(i=0; i<16; i++) {
00412 if(s->blocksize == flac_blocksizes[i]) {
00413 frame->blocksize = flac_blocksizes[i];
00414 frame->bs_code[0] = i;
00415 frame->bs_code[1] = 0;
00416 break;
00417 }
00418 }
00419 if(i == 16) {
00420 frame->blocksize = s->blocksize;
00421 if(frame->blocksize <= 256) {
00422 frame->bs_code[0] = 6;
00423 frame->bs_code[1] = frame->blocksize-1;
00424 } else {
00425 frame->bs_code[0] = 7;
00426 frame->bs_code[1] = frame->blocksize-1;
00427 }
00428 }
00429
00430 for(ch=0; ch<s->channels; ch++) {
00431 frame->subframes[ch].obits = 16;
00432 }
00433 }
00434
00438 static void copy_samples(FlacEncodeContext *s, int16_t *samples)
00439 {
00440 int i, j, ch;
00441 FlacFrame *frame;
00442
00443 frame = &s->frame;
00444 for(i=0,j=0; i<frame->blocksize; i++) {
00445 for(ch=0; ch<s->channels; ch++,j++) {
00446 frame->subframes[ch].samples[i] = samples[j];
00447 }
00448 }
00449 }
00450
00451
00452 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
00453
00457 static int find_optimal_param(uint32_t sum, int n)
00458 {
00459 int k;
00460 uint32_t sum2;
00461
00462 if(sum <= n>>1)
00463 return 0;
00464 sum2 = sum-(n>>1);
00465 k = av_log2(n<256 ? FASTDIV(sum2,n) : sum2/n);
00466 return FFMIN(k, MAX_RICE_PARAM);
00467 }
00468
00469 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
00470 uint32_t *sums, int n, int pred_order)
00471 {
00472 int i;
00473 int k, cnt, part;
00474 uint32_t all_bits;
00475
00476 part = (1 << porder);
00477 all_bits = 4 * part;
00478
00479 cnt = (n >> porder) - pred_order;
00480 for(i=0; i<part; i++) {
00481 k = find_optimal_param(sums[i], cnt);
00482 rc->params[i] = k;
00483 all_bits += rice_encode_count(sums[i], cnt, k);
00484 cnt = n >> porder;
00485 }
00486
00487 rc->porder = porder;
00488
00489 return all_bits;
00490 }
00491
00492 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
00493 uint32_t sums[][MAX_PARTITIONS])
00494 {
00495 int i, j;
00496 int parts;
00497 uint32_t *res, *res_end;
00498
00499
00500 parts = (1 << pmax);
00501 res = &data[pred_order];
00502 res_end = &data[n >> pmax];
00503 for(i=0; i<parts; i++) {
00504 uint32_t sum = 0;
00505 while(res < res_end){
00506 sum += *(res++);
00507 }
00508 sums[pmax][i] = sum;
00509 res_end+= n >> pmax;
00510 }
00511
00512 for(i=pmax-1; i>=pmin; i--) {
00513 parts = (1 << i);
00514 for(j=0; j<parts; j++) {
00515 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
00516 }
00517 }
00518 }
00519
00520 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
00521 int32_t *data, int n, int pred_order)
00522 {
00523 int i;
00524 uint32_t bits[MAX_PARTITION_ORDER+1];
00525 int opt_porder;
00526 RiceContext tmp_rc;
00527 uint32_t *udata;
00528 uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
00529
00530 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
00531 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
00532 assert(pmin <= pmax);
00533
00534 udata = av_malloc(n * sizeof(uint32_t));
00535 for(i=0; i<n; i++) {
00536 udata[i] = (2*data[i]) ^ (data[i]>>31);
00537 }
00538
00539 calc_sums(pmin, pmax, udata, n, pred_order, sums);
00540
00541 opt_porder = pmin;
00542 bits[pmin] = UINT32_MAX;
00543 for(i=pmin; i<=pmax; i++) {
00544 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
00545 if(bits[i] <= bits[opt_porder]) {
00546 opt_porder = i;
00547 *rc= tmp_rc;
00548 }
00549 }
00550
00551 av_freep(&udata);
00552 return bits[opt_porder];
00553 }
00554
00555 static int get_max_p_order(int max_porder, int n, int order)
00556 {
00557 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
00558 if(order > 0)
00559 porder = FFMIN(porder, av_log2(n/order));
00560 return porder;
00561 }
00562
00563 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
00564 int32_t *data, int n, int pred_order,
00565 int bps)
00566 {
00567 uint32_t bits;
00568 pmin = get_max_p_order(pmin, n, pred_order);
00569 pmax = get_max_p_order(pmax, n, pred_order);
00570 bits = pred_order*bps + 6;
00571 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
00572 return bits;
00573 }
00574
00575 static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
00576 int32_t *data, int n, int pred_order,
00577 int bps, int precision)
00578 {
00579 uint32_t bits;
00580 pmin = get_max_p_order(pmin, n, pred_order);
00581 pmax = get_max_p_order(pmax, n, pred_order);
00582 bits = pred_order*bps + 4 + 5 + pred_order*precision + 6;
00583 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
00584 return bits;
00585 }
00586
00590 static void apply_welch_window(const int32_t *data, int len, double *w_data)
00591 {
00592 int i, n2;
00593 double w;
00594 double c;
00595
00596 n2 = (len >> 1);
00597 c = 2.0 / (len - 1.0);
00598 for(i=0; i<n2; i++) {
00599 w = c - i - 1.0;
00600 w = 1.0 - (w * w);
00601 w_data[i] = data[i] * w;
00602 w_data[len-1-i] = data[len-1-i] * w;
00603 }
00604 }
00605
00610 void ff_flac_compute_autocorr(const int32_t *data, int len, int lag,
00611 double *autoc)
00612 {
00613 int i, j;
00614 double tmp[len + lag + 1];
00615 double *data1= tmp + lag;
00616
00617 apply_welch_window(data, len, data1);
00618
00619 for(j=0; j<lag; j++)
00620 data1[j-lag]= 0.0;
00621 data1[len] = 0.0;
00622
00623 for(j=0; j<lag; j+=2){
00624 double sum0 = 1.0, sum1 = 1.0;
00625 for(i=0; i<len; i++){
00626 sum0 += data1[i] * data1[i-j];
00627 sum1 += data1[i] * data1[i-j-1];
00628 }
00629 autoc[j ] = sum0;
00630 autoc[j+1] = sum1;
00631 }
00632
00633 if(j==lag){
00634 double sum = 1.0;
00635 for(i=0; i<len; i+=2){
00636 sum += data1[i ] * data1[i-j ]
00637 + data1[i+1] * data1[i-j+1];
00638 }
00639 autoc[j] = sum;
00640 }
00641 }
00642
00647 static void compute_lpc_coefs(const double *autoc, int max_order,
00648 double lpc[][MAX_LPC_ORDER], double *ref)
00649 {
00650 int i, j, i2;
00651 double r, err, tmp;
00652 double lpc_tmp[MAX_LPC_ORDER];
00653
00654 for(i=0; i<max_order; i++) lpc_tmp[i] = 0;
00655 err = autoc[0];
00656
00657 for(i=0; i<max_order; i++) {
00658 r = -autoc[i+1];
00659 for(j=0; j<i; j++) {
00660 r -= lpc_tmp[j] * autoc[i-j];
00661 }
00662 r /= err;
00663 ref[i] = fabs(r);
00664
00665 err *= 1.0 - (r * r);
00666
00667 i2 = (i >> 1);
00668 lpc_tmp[i] = r;
00669 for(j=0; j<i2; j++) {
00670 tmp = lpc_tmp[j];
00671 lpc_tmp[j] += r * lpc_tmp[i-1-j];
00672 lpc_tmp[i-1-j] += r * tmp;
00673 }
00674 if(i & 1) {
00675 lpc_tmp[j] += lpc_tmp[j] * r;
00676 }
00677
00678 for(j=0; j<=i; j++) {
00679 lpc[i][j] = -lpc_tmp[j];
00680 }
00681 }
00682 }
00683
00687 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
00688 int32_t *lpc_out, int *shift)
00689 {
00690 int i;
00691 double cmax, error;
00692 int32_t qmax;
00693 int sh;
00694
00695
00696 qmax = (1 << (precision - 1)) - 1;
00697
00698
00699 cmax = 0.0;
00700 for(i=0; i<order; i++) {
00701 cmax= FFMAX(cmax, fabs(lpc_in[i]));
00702 }
00703
00704
00705 if(cmax * (1 << MAX_LPC_SHIFT) < 1.0) {
00706 *shift = 0;
00707 memset(lpc_out, 0, sizeof(int32_t) * order);
00708 return;
00709 }
00710
00711
00712 sh = MAX_LPC_SHIFT;
00713 while((cmax * (1 << sh) > qmax) && (sh > 0)) {
00714 sh--;
00715 }
00716
00717
00718
00719 if(sh == 0 && cmax > qmax) {
00720 double scale = ((double)qmax) / cmax;
00721 for(i=0; i<order; i++) {
00722 lpc_in[i] *= scale;
00723 }
00724 }
00725
00726
00727 error=0;
00728 for(i=0; i<order; i++) {
00729 error += lpc_in[i] * (1 << sh);
00730 lpc_out[i] = av_clip(lrintf(error), -qmax, qmax);
00731 error -= lpc_out[i];
00732 }
00733 *shift = sh;
00734 }
00735
00736 static int estimate_best_order(double *ref, int max_order)
00737 {
00738 int i, est;
00739
00740 est = 1;
00741 for(i=max_order-1; i>=0; i--) {
00742 if(ref[i] > 0.10) {
00743 est = i+1;
00744 break;
00745 }
00746 }
00747 return est;
00748 }
00749
00753 static int lpc_calc_coefs(FlacEncodeContext *s,
00754 const int32_t *samples, int blocksize, int max_order,
00755 int precision, int32_t coefs[][MAX_LPC_ORDER],
00756 int *shift, int use_lpc, int omethod)
00757 {
00758 double autoc[MAX_LPC_ORDER+1];
00759 double ref[MAX_LPC_ORDER];
00760 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
00761 int i, j, pass;
00762 int opt_order;
00763
00764 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
00765
00766 if(use_lpc == 1){
00767 s->dsp.flac_compute_autocorr(samples, blocksize, max_order, autoc);
00768
00769 compute_lpc_coefs(autoc, max_order, lpc, ref);
00770 }else{
00771 LLSModel m[2];
00772 double var[MAX_LPC_ORDER+1], weight;
00773
00774 for(pass=0; pass<use_lpc-1; pass++){
00775 av_init_lls(&m[pass&1], max_order);
00776
00777 weight=0;
00778 for(i=max_order; i<blocksize; i++){
00779 for(j=0; j<=max_order; j++)
00780 var[j]= samples[i-j];
00781
00782 if(pass){
00783 double eval, inv, rinv;
00784 eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
00785 eval= (512>>pass) + fabs(eval - var[0]);
00786 inv = 1/eval;
00787 rinv = sqrt(inv);
00788 for(j=0; j<=max_order; j++)
00789 var[j] *= rinv;
00790 weight += inv;
00791 }else
00792 weight++;
00793
00794 av_update_lls(&m[pass&1], var, 1.0);
00795 }
00796 av_solve_lls(&m[pass&1], 0.001, 0);
00797 }
00798
00799 for(i=0; i<max_order; i++){
00800 for(j=0; j<max_order; j++)
00801 lpc[i][j]= m[(pass-1)&1].coeff[i][j];
00802 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
00803 }
00804 for(i=max_order-1; i>0; i--)
00805 ref[i] = ref[i-1] - ref[i];
00806 }
00807 opt_order = max_order;
00808
00809 if(omethod == ORDER_METHOD_EST) {
00810 opt_order = estimate_best_order(ref, max_order);
00811 i = opt_order-1;
00812 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
00813 } else {
00814 for(i=0; i<max_order; i++) {
00815 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
00816 }
00817 }
00818
00819 return opt_order;
00820 }
00821
00822
00823 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
00824 {
00825 assert(n > 0);
00826 memcpy(res, smp, n * sizeof(int32_t));
00827 }
00828
00829 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
00830 int order)
00831 {
00832 int i;
00833
00834 for(i=0; i<order; i++) {
00835 res[i] = smp[i];
00836 }
00837
00838 if(order==0){
00839 for(i=order; i<n; i++)
00840 res[i]= smp[i];
00841 }else if(order==1){
00842 for(i=order; i<n; i++)
00843 res[i]= smp[i] - smp[i-1];
00844 }else if(order==2){
00845 int a = smp[order-1] - smp[order-2];
00846 for(i=order; i<n; i+=2) {
00847 int b = smp[i] - smp[i-1];
00848 res[i]= b - a;
00849 a = smp[i+1] - smp[i];
00850 res[i+1]= a - b;
00851 }
00852 }else if(order==3){
00853 int a = smp[order-1] - smp[order-2];
00854 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00855 for(i=order; i<n; i+=2) {
00856 int b = smp[i] - smp[i-1];
00857 int d = b - a;
00858 res[i]= d - c;
00859 a = smp[i+1] - smp[i];
00860 c = a - b;
00861 res[i+1]= c - d;
00862 }
00863 }else{
00864 int a = smp[order-1] - smp[order-2];
00865 int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00866 int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
00867 for(i=order; i<n; i+=2) {
00868 int b = smp[i] - smp[i-1];
00869 int d = b - a;
00870 int f = d - c;
00871 res[i]= f - e;
00872 a = smp[i+1] - smp[i];
00873 c = a - b;
00874 e = c - d;
00875 res[i+1]= e - f;
00876 }
00877 }
00878 }
00879
00880 #define LPC1(x) {\
00881 int c = coefs[(x)-1];\
00882 p0 += c*s;\
00883 s = smp[i-(x)+1];\
00884 p1 += c*s;\
00885 }
00886
00887 static av_always_inline void encode_residual_lpc_unrolled(
00888 int32_t *res, const int32_t *smp, int n,
00889 int order, const int32_t *coefs, int shift, int big)
00890 {
00891 int i;
00892 for(i=order; i<n; i+=2) {
00893 int s = smp[i-order];
00894 int p0 = 0, p1 = 0;
00895 if(big) {
00896 switch(order) {
00897 case 32: LPC1(32)
00898 case 31: LPC1(31)
00899 case 30: LPC1(30)
00900 case 29: LPC1(29)
00901 case 28: LPC1(28)
00902 case 27: LPC1(27)
00903 case 26: LPC1(26)
00904 case 25: LPC1(25)
00905 case 24: LPC1(24)
00906 case 23: LPC1(23)
00907 case 22: LPC1(22)
00908 case 21: LPC1(21)
00909 case 20: LPC1(20)
00910 case 19: LPC1(19)
00911 case 18: LPC1(18)
00912 case 17: LPC1(17)
00913 case 16: LPC1(16)
00914 case 15: LPC1(15)
00915 case 14: LPC1(14)
00916 case 13: LPC1(13)
00917 case 12: LPC1(12)
00918 case 11: LPC1(11)
00919 case 10: LPC1(10)
00920 case 9: LPC1( 9)
00921 LPC1( 8)
00922 LPC1( 7)
00923 LPC1( 6)
00924 LPC1( 5)
00925 LPC1( 4)
00926 LPC1( 3)
00927 LPC1( 2)
00928 LPC1( 1)
00929 }
00930 } else {
00931 switch(order) {
00932 case 8: LPC1( 8)
00933 case 7: LPC1( 7)
00934 case 6: LPC1( 6)
00935 case 5: LPC1( 5)
00936 case 4: LPC1( 4)
00937 case 3: LPC1( 3)
00938 case 2: LPC1( 2)
00939 case 1: LPC1( 1)
00940 }
00941 }
00942 res[i ] = smp[i ] - (p0 >> shift);
00943 res[i+1] = smp[i+1] - (p1 >> shift);
00944 }
00945 }
00946
00947 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
00948 int order, const int32_t *coefs, int shift)
00949 {
00950 int i;
00951 for(i=0; i<order; i++) {
00952 res[i] = smp[i];
00953 }
00954 #ifdef CONFIG_SMALL
00955 for(i=order; i<n; i+=2) {
00956 int j;
00957 int s = smp[i];
00958 int p0 = 0, p1 = 0;
00959 for(j=0; j<order; j++) {
00960 int c = coefs[j];
00961 p1 += c*s;
00962 s = smp[i-j-1];
00963 p0 += c*s;
00964 }
00965 res[i ] = smp[i ] - (p0 >> shift);
00966 res[i+1] = smp[i+1] - (p1 >> shift);
00967 }
00968 #else
00969 switch(order) {
00970 case 1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
00971 case 2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
00972 case 3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
00973 case 4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
00974 case 5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
00975 case 6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
00976 case 7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
00977 case 8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
00978 default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
00979 }
00980 #endif
00981 }
00982
00983 static int encode_residual(FlacEncodeContext *ctx, int ch)
00984 {
00985 int i, n;
00986 int min_order, max_order, opt_order, precision, omethod;
00987 int min_porder, max_porder;
00988 FlacFrame *frame;
00989 FlacSubframe *sub;
00990 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
00991 int shift[MAX_LPC_ORDER];
00992 int32_t *res, *smp;
00993
00994 frame = &ctx->frame;
00995 sub = &frame->subframes[ch];
00996 res = sub->residual;
00997 smp = sub->samples;
00998 n = frame->blocksize;
00999
01000
01001 for(i=1; i<n; i++) {
01002 if(smp[i] != smp[0]) break;
01003 }
01004 if(i == n) {
01005 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
01006 res[0] = smp[0];
01007 return sub->obits;
01008 }
01009
01010
01011 if(n < 5) {
01012 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
01013 encode_residual_verbatim(res, smp, n);
01014 return sub->obits * n;
01015 }
01016
01017 min_order = ctx->options.min_prediction_order;
01018 max_order = ctx->options.max_prediction_order;
01019 min_porder = ctx->options.min_partition_order;
01020 max_porder = ctx->options.max_partition_order;
01021 precision = ctx->options.lpc_coeff_precision;
01022 omethod = ctx->options.prediction_order_method;
01023
01024
01025 if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {
01026 uint32_t bits[MAX_FIXED_ORDER+1];
01027 if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;
01028 opt_order = 0;
01029 bits[0] = UINT32_MAX;
01030 for(i=min_order; i<=max_order; i++) {
01031 encode_residual_fixed(res, smp, n, i);
01032 bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
01033 n, i, sub->obits);
01034 if(bits[i] < bits[opt_order]) {
01035 opt_order = i;
01036 }
01037 }
01038 sub->order = opt_order;
01039 sub->type = FLAC_SUBFRAME_FIXED;
01040 sub->type_code = sub->type | sub->order;
01041 if(sub->order != max_order) {
01042 encode_residual_fixed(res, smp, n, sub->order);
01043 return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
01044 sub->order, sub->obits);
01045 }
01046 return bits[sub->order];
01047 }
01048
01049
01050 opt_order = lpc_calc_coefs(ctx, smp, n, max_order, precision, coefs, shift, ctx->options.use_lpc, omethod);
01051
01052 if(omethod == ORDER_METHOD_2LEVEL ||
01053 omethod == ORDER_METHOD_4LEVEL ||
01054 omethod == ORDER_METHOD_8LEVEL) {
01055 int levels = 1 << omethod;
01056 uint32_t bits[levels];
01057 int order;
01058 int opt_index = levels-1;
01059 opt_order = max_order-1;
01060 bits[opt_index] = UINT32_MAX;
01061 for(i=levels-1; i>=0; i--) {
01062 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
01063 if(order < 0) order = 0;
01064 encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
01065 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
01066 res, n, order+1, sub->obits, precision);
01067 if(bits[i] < bits[opt_index]) {
01068 opt_index = i;
01069 opt_order = order;
01070 }
01071 }
01072 opt_order++;
01073 } else if(omethod == ORDER_METHOD_SEARCH) {
01074
01075 uint32_t bits[MAX_LPC_ORDER];
01076 opt_order = 0;
01077 bits[0] = UINT32_MAX;
01078 for(i=min_order-1; i<max_order; i++) {
01079 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
01080 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
01081 res, n, i+1, sub->obits, precision);
01082 if(bits[i] < bits[opt_order]) {
01083 opt_order = i;
01084 }
01085 }
01086 opt_order++;
01087 } else if(omethod == ORDER_METHOD_LOG) {
01088 uint32_t bits[MAX_LPC_ORDER];
01089 int step;
01090
01091 opt_order= min_order - 1 + (max_order-min_order)/3;
01092 memset(bits, -1, sizeof(bits));
01093
01094 for(step=16 ;step; step>>=1){
01095 int last= opt_order;
01096 for(i=last-step; i<=last+step; i+= step){
01097 if(i<min_order-1 || i>=max_order || bits[i] < UINT32_MAX)
01098 continue;
01099 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
01100 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
01101 res, n, i+1, sub->obits, precision);
01102 if(bits[i] < bits[opt_order])
01103 opt_order= i;
01104 }
01105 }
01106 opt_order++;
01107 }
01108
01109 sub->order = opt_order;
01110 sub->type = FLAC_SUBFRAME_LPC;
01111 sub->type_code = sub->type | (sub->order-1);
01112 sub->shift = shift[sub->order-1];
01113 for(i=0; i<sub->order; i++) {
01114 sub->coefs[i] = coefs[sub->order-1][i];
01115 }
01116 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
01117 return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,
01118 sub->obits, precision);
01119 }
01120
01121 static int encode_residual_v(FlacEncodeContext *ctx, int ch)
01122 {
01123 int i, n;
01124 FlacFrame *frame;
01125 FlacSubframe *sub;
01126 int32_t *res, *smp;
01127
01128 frame = &ctx->frame;
01129 sub = &frame->subframes[ch];
01130 res = sub->residual;
01131 smp = sub->samples;
01132 n = frame->blocksize;
01133
01134
01135 for(i=1; i<n; i++) {
01136 if(smp[i] != smp[0]) break;
01137 }
01138 if(i == n) {
01139 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
01140 res[0] = smp[0];
01141 return sub->obits;
01142 }
01143
01144
01145 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
01146 encode_residual_verbatim(res, smp, n);
01147 return sub->obits * n;
01148 }
01149
01150 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
01151 {
01152 int i, best;
01153 int32_t lt, rt;
01154 uint64_t sum[4];
01155 uint64_t score[4];
01156 int k;
01157
01158
01159 sum[0] = sum[1] = sum[2] = sum[3] = 0;
01160 for(i=2; i<n; i++) {
01161 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
01162 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
01163 sum[2] += FFABS((lt + rt) >> 1);
01164 sum[3] += FFABS(lt - rt);
01165 sum[0] += FFABS(lt);
01166 sum[1] += FFABS(rt);
01167 }
01168
01169 for(i=0; i<4; i++) {
01170 k = find_optimal_param(2*sum[i], n);
01171 sum[i] = rice_encode_count(2*sum[i], n, k);
01172 }
01173
01174
01175 score[0] = sum[0] + sum[1];
01176 score[1] = sum[0] + sum[3];
01177 score[2] = sum[1] + sum[3];
01178 score[3] = sum[2] + sum[3];
01179
01180
01181 best = 0;
01182 for(i=1; i<4; i++) {
01183 if(score[i] < score[best]) {
01184 best = i;
01185 }
01186 }
01187 if(best == 0) {
01188 return FLAC_CHMODE_LEFT_RIGHT;
01189 } else if(best == 1) {
01190 return FLAC_CHMODE_LEFT_SIDE;
01191 } else if(best == 2) {
01192 return FLAC_CHMODE_RIGHT_SIDE;
01193 } else {
01194 return FLAC_CHMODE_MID_SIDE;
01195 }
01196 }
01197
01201 static void channel_decorrelation(FlacEncodeContext *ctx)
01202 {
01203 FlacFrame *frame;
01204 int32_t *left, *right;
01205 int i, n;
01206
01207 frame = &ctx->frame;
01208 n = frame->blocksize;
01209 left = frame->subframes[0].samples;
01210 right = frame->subframes[1].samples;
01211
01212 if(ctx->channels != 2) {
01213 frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
01214 return;
01215 }
01216
01217 frame->ch_mode = estimate_stereo_mode(left, right, n);
01218
01219
01220 if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
01221 return;
01222 }
01223 if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
01224 int32_t tmp;
01225 for(i=0; i<n; i++) {
01226 tmp = left[i];
01227 left[i] = (tmp + right[i]) >> 1;
01228 right[i] = tmp - right[i];
01229 }
01230 frame->subframes[1].obits++;
01231 } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
01232 for(i=0; i<n; i++) {
01233 right[i] = left[i] - right[i];
01234 }
01235 frame->subframes[1].obits++;
01236 } else {
01237 for(i=0; i<n; i++) {
01238 left[i] -= right[i];
01239 }
01240 frame->subframes[0].obits++;
01241 }
01242 }
01243
01244 static void put_sbits(PutBitContext *pb, int bits, int32_t val)
01245 {
01246 assert(bits >= 0 && bits <= 31);
01247
01248 put_bits(pb, bits, val & ((1<<bits)-1));
01249 }
01250
01251 static void write_utf8(PutBitContext *pb, uint32_t val)
01252 {
01253 uint8_t tmp;
01254 PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
01255 }
01256
01257 static void output_frame_header(FlacEncodeContext *s)
01258 {
01259 FlacFrame *frame;
01260 int crc;
01261
01262 frame = &s->frame;
01263
01264 put_bits(&s->pb, 16, 0xFFF8);
01265 put_bits(&s->pb, 4, frame->bs_code[0]);
01266 put_bits(&s->pb, 4, s->sr_code[0]);
01267 if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
01268 put_bits(&s->pb, 4, s->ch_code);
01269 } else {
01270 put_bits(&s->pb, 4, frame->ch_mode);
01271 }
01272 put_bits(&s->pb, 3, 4);
01273 put_bits(&s->pb, 1, 0);
01274 write_utf8(&s->pb, s->frame_count);
01275 if(frame->bs_code[0] == 6) {
01276 put_bits(&s->pb, 8, frame->bs_code[1]);
01277 } else if(frame->bs_code[0] == 7) {
01278 put_bits(&s->pb, 16, frame->bs_code[1]);
01279 }
01280 if(s->sr_code[0] == 12) {
01281 put_bits(&s->pb, 8, s->sr_code[1]);
01282 } else if(s->sr_code[0] > 12) {
01283 put_bits(&s->pb, 16, s->sr_code[1]);
01284 }
01285 flush_put_bits(&s->pb);
01286 crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
01287 s->pb.buf, put_bits_count(&s->pb)>>3);
01288 put_bits(&s->pb, 8, crc);
01289 }
01290
01291 static void output_subframe_constant(FlacEncodeContext *s, int ch)
01292 {
01293 FlacSubframe *sub;
01294 int32_t res;
01295
01296 sub = &s->frame.subframes[ch];
01297 res = sub->residual[0];
01298 put_sbits(&s->pb, sub->obits, res);
01299 }
01300
01301 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
01302 {
01303 int i;
01304 FlacFrame *frame;
01305 FlacSubframe *sub;
01306 int32_t res;
01307
01308 frame = &s->frame;
01309 sub = &frame->subframes[ch];
01310
01311 for(i=0; i<frame->blocksize; i++) {
01312 res = sub->residual[i];
01313 put_sbits(&s->pb, sub->obits, res);
01314 }
01315 }
01316
01317 static void output_residual(FlacEncodeContext *ctx, int ch)
01318 {
01319 int i, j, p, n, parts;
01320 int k, porder, psize, res_cnt;
01321 FlacFrame *frame;
01322 FlacSubframe *sub;
01323 int32_t *res;
01324
01325 frame = &ctx->frame;
01326 sub = &frame->subframes[ch];
01327 res = sub->residual;
01328 n = frame->blocksize;
01329
01330
01331 put_bits(&ctx->pb, 2, 0);
01332
01333
01334 porder = sub->rc.porder;
01335 psize = n >> porder;
01336 parts = (1 << porder);
01337 put_bits(&ctx->pb, 4, porder);
01338 res_cnt = psize - sub->order;
01339
01340
01341 j = sub->order;
01342 for(p=0; p<parts; p++) {
01343 k = sub->rc.params[p];
01344 put_bits(&ctx->pb, 4, k);
01345 if(p == 1) res_cnt = psize;
01346 for(i=0; i<res_cnt && j<n; i++, j++) {
01347 set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
01348 }
01349 }
01350 }
01351
01352 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
01353 {
01354 int i;
01355 FlacFrame *frame;
01356 FlacSubframe *sub;
01357
01358 frame = &ctx->frame;
01359 sub = &frame->subframes[ch];
01360
01361
01362 for(i=0; i<sub->order; i++) {
01363 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
01364 }
01365
01366
01367 output_residual(ctx, ch);
01368 }
01369
01370 static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
01371 {
01372 int i, cbits;
01373 FlacFrame *frame;
01374 FlacSubframe *sub;
01375
01376 frame = &ctx->frame;
01377 sub = &frame->subframes[ch];
01378
01379
01380 for(i=0; i<sub->order; i++) {
01381 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
01382 }
01383
01384
01385 cbits = ctx->options.lpc_coeff_precision;
01386 put_bits(&ctx->pb, 4, cbits-1);
01387 put_sbits(&ctx->pb, 5, sub->shift);
01388 for(i=0; i<sub->order; i++) {
01389 put_sbits(&ctx->pb, cbits, sub->coefs[i]);
01390 }
01391
01392
01393 output_residual(ctx, ch);
01394 }
01395
01396 static void output_subframes(FlacEncodeContext *s)
01397 {
01398 FlacFrame *frame;
01399 FlacSubframe *sub;
01400 int ch;
01401
01402 frame = &s->frame;
01403
01404 for(ch=0; ch<s->channels; ch++) {
01405 sub = &frame->subframes[ch];
01406
01407
01408 put_bits(&s->pb, 1, 0);
01409 put_bits(&s->pb, 6, sub->type_code);
01410 put_bits(&s->pb, 1, 0);
01411
01412
01413 if(sub->type == FLAC_SUBFRAME_CONSTANT) {
01414 output_subframe_constant(s, ch);
01415 } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
01416 output_subframe_verbatim(s, ch);
01417 } else if(sub->type == FLAC_SUBFRAME_FIXED) {
01418 output_subframe_fixed(s, ch);
01419 } else if(sub->type == FLAC_SUBFRAME_LPC) {
01420 output_subframe_lpc(s, ch);
01421 }
01422 }
01423 }
01424
01425 static void output_frame_footer(FlacEncodeContext *s)
01426 {
01427 int crc;
01428 flush_put_bits(&s->pb);
01429 crc = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
01430 s->pb.buf, put_bits_count(&s->pb)>>3));
01431 put_bits(&s->pb, 16, crc);
01432 flush_put_bits(&s->pb);
01433 }
01434
01435 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
01436 int buf_size, void *data)
01437 {
01438 int ch;
01439 FlacEncodeContext *s;
01440 int16_t *samples = data;
01441 int out_bytes;
01442
01443 s = avctx->priv_data;
01444
01445 s->blocksize = avctx->frame_size;
01446 init_frame(s);
01447
01448 copy_samples(s, samples);
01449
01450 channel_decorrelation(s);
01451
01452 for(ch=0; ch<s->channels; ch++) {
01453 encode_residual(s, ch);
01454 }
01455 init_put_bits(&s->pb, frame, buf_size);
01456 output_frame_header(s);
01457 output_subframes(s);
01458 output_frame_footer(s);
01459 out_bytes = put_bits_count(&s->pb) >> 3;
01460
01461 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
01462
01463 for(ch=0; ch<s->channels; ch++) {
01464 encode_residual_v(s, ch);
01465 }
01466 init_put_bits(&s->pb, frame, buf_size);
01467 output_frame_header(s);
01468 output_subframes(s);
01469 output_frame_footer(s);
01470 out_bytes = put_bits_count(&s->pb) >> 3;
01471
01472 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
01473
01474 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
01475 return -1;
01476 }
01477 }
01478
01479 s->frame_count++;
01480 return out_bytes;
01481 }
01482
01483 static int flac_encode_close(AVCodecContext *avctx)
01484 {
01485 av_freep(&avctx->extradata);
01486 avctx->extradata_size = 0;
01487 av_freep(&avctx->coded_frame);
01488 return 0;
01489 }
01490
01491 AVCodec flac_encoder = {
01492 "flac",
01493 CODEC_TYPE_AUDIO,
01494 CODEC_ID_FLAC,
01495 sizeof(FlacEncodeContext),
01496 flac_encode_init,
01497 flac_encode_frame,
01498 flac_encode_close,
01499 NULL,
01500 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,
01501 };