3 * Copyright (c) 2006 Justin Ruggles <jruggle@earthlink.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "bitstream.h"
27 #define FLAC_MIN_BLOCKSIZE 16
28 #define FLAC_MAX_BLOCKSIZE 65535
30 #define FLAC_SUBFRAME_CONSTANT 0
31 #define FLAC_SUBFRAME_VERBATIM 1
32 #define FLAC_SUBFRAME_FIXED 8
33 #define FLAC_SUBFRAME_LPC 32
35 #define FLAC_CHMODE_NOT_STEREO 0
36 #define FLAC_CHMODE_LEFT_RIGHT 1
37 #define FLAC_CHMODE_LEFT_SIDE 8
38 #define FLAC_CHMODE_RIGHT_SIDE 9
39 #define FLAC_CHMODE_MID_SIDE 10
41 #define ORDER_METHOD_EST 0
42 #define ORDER_METHOD_2LEVEL 1
43 #define ORDER_METHOD_4LEVEL 2
44 #define ORDER_METHOD_8LEVEL 3
45 #define ORDER_METHOD_SEARCH 4
46 #define ORDER_METHOD_LOG 5
48 #define FLAC_STREAMINFO_SIZE 34
50 #define MIN_LPC_ORDER 1
51 #define MAX_LPC_ORDER 32
52 #define MAX_FIXED_ORDER 4
53 #define MAX_PARTITION_ORDER 8
54 #define MAX_PARTITIONS (1 << MAX_PARTITION_ORDER)
55 #define MAX_LPC_PRECISION 15
56 #define MAX_LPC_SHIFT 15
57 #define MAX_RICE_PARAM 14
59 typedef struct CompressionOptions {
60 int compression_level;
63 int lpc_coeff_precision;
64 int min_prediction_order;
65 int max_prediction_order;
66 int prediction_order_method;
67 int min_partition_order;
68 int max_partition_order;
71 typedef struct RiceContext {
73 int params[MAX_PARTITIONS];
76 typedef struct FlacSubframe {
81 int32_t coefs[MAX_LPC_ORDER];
84 int32_t samples[FLAC_MAX_BLOCKSIZE];
85 int32_t residual[FLAC_MAX_BLOCKSIZE];
88 typedef struct FlacFrame {
89 FlacSubframe subframes[FLAC_MAX_CH];
96 typedef struct FlacEncodeContext {
104 uint32_t frame_count;
106 CompressionOptions options;
107 AVCodecContext *avctx;
110 static const int flac_samplerates[16] = {
112 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
116 static const int flac_blocksizes[16] = {
119 576, 1152, 2304, 4608,
121 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
125 * Writes streaminfo metadata block to byte array
127 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
131 memset(header, 0, FLAC_STREAMINFO_SIZE);
132 init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
134 /* streaminfo metadata block */
135 put_bits(&pb, 16, s->blocksize);
136 put_bits(&pb, 16, s->blocksize);
137 put_bits(&pb, 24, 0);
138 put_bits(&pb, 24, s->max_framesize);
139 put_bits(&pb, 20, s->samplerate);
140 put_bits(&pb, 3, s->channels-1);
141 put_bits(&pb, 5, 15); /* bits per sample - 1 */
143 /* total samples = 0 */
144 /* MD5 signature = 0 */
148 * Sets blocksize based on samplerate
149 * Chooses the closest predefined blocksize >= BLOCK_TIME_MS milliseconds
151 static int select_blocksize(int samplerate, int block_time_ms)
157 assert(samplerate > 0);
158 blocksize = flac_blocksizes[1];
159 target = (samplerate * block_time_ms) / 1000;
160 for(i=0; i<16; i++) {
161 if(target >= flac_blocksizes[i] && flac_blocksizes[i] > blocksize) {
162 blocksize = flac_blocksizes[i];
168 static int flac_encode_init(AVCodecContext *avctx)
170 int freq = avctx->sample_rate;
171 int channels = avctx->channels;
172 FlacEncodeContext *s = avctx->priv_data;
178 if(avctx->sample_fmt != SAMPLE_FMT_S16) {
182 if(channels < 1 || channels > FLAC_MAX_CH) {
185 s->channels = channels;
186 s->ch_code = s->channels-1;
188 /* find samplerate in table */
191 for(i=4; i<12; i++) {
192 if(freq == flac_samplerates[i]) {
193 s->samplerate = flac_samplerates[i];
199 /* if not in table, samplerate is non-standard */
201 if(freq % 1000 == 0 && freq < 255000) {
203 s->sr_code[1] = freq / 1000;
204 } else if(freq % 10 == 0 && freq < 655350) {
206 s->sr_code[1] = freq / 10;
207 } else if(freq < 65535) {
209 s->sr_code[1] = freq;
213 s->samplerate = freq;
216 /* set compression option defaults based on avctx->compression_level */
217 if(avctx->compression_level < 0) {
218 s->options.compression_level = 5;
220 s->options.compression_level = avctx->compression_level;
222 av_log(avctx, AV_LOG_DEBUG, " compression: %d\n", s->options.compression_level);
224 level= s->options.compression_level;
226 av_log(avctx, AV_LOG_ERROR, "invalid compression level: %d\n",
227 s->options.compression_level);
231 s->options.block_time_ms = ((int[]){ 27, 27, 27,105,105,105,105,105,105,105,105,105,105})[level];
232 s->options.use_lpc = ((int[]){ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
233 s->options.min_prediction_order= ((int[]){ 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1})[level];
234 s->options.max_prediction_order= ((int[]){ 3, 4, 4, 6, 8, 8, 8, 8, 12, 12, 12, 32, 32})[level];
235 s->options.prediction_order_method = ((int[]){ ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
236 ORDER_METHOD_EST, ORDER_METHOD_EST, ORDER_METHOD_EST,
237 ORDER_METHOD_4LEVEL, ORDER_METHOD_LOG, ORDER_METHOD_4LEVEL,
238 ORDER_METHOD_LOG, ORDER_METHOD_SEARCH, ORDER_METHOD_LOG,
239 ORDER_METHOD_SEARCH})[level];
240 s->options.min_partition_order = ((int[]){ 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0})[level];
241 s->options.max_partition_order = ((int[]){ 2, 2, 3, 3, 3, 8, 8, 8, 8, 8, 8, 8, 8})[level];
243 /* set compression option overrides from AVCodecContext */
244 if(avctx->use_lpc >= 0) {
245 s->options.use_lpc = clip(avctx->use_lpc, 0, 11);
247 if(s->options.use_lpc == 1)
248 av_log(avctx, AV_LOG_DEBUG, " use lpc: Levinson-Durbin recursion with Welch window\n");
249 else if(s->options.use_lpc > 1)
250 av_log(avctx, AV_LOG_DEBUG, " use lpc: Cholesky factorization\n");
252 if(avctx->min_prediction_order >= 0) {
253 if(s->options.use_lpc) {
254 if(avctx->min_prediction_order < MIN_LPC_ORDER ||
255 avctx->min_prediction_order > MAX_LPC_ORDER) {
256 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
257 avctx->min_prediction_order);
261 if(avctx->min_prediction_order > MAX_FIXED_ORDER) {
262 av_log(avctx, AV_LOG_ERROR, "invalid min prediction order: %d\n",
263 avctx->min_prediction_order);
267 s->options.min_prediction_order = avctx->min_prediction_order;
269 if(avctx->max_prediction_order >= 0) {
270 if(s->options.use_lpc) {
271 if(avctx->max_prediction_order < MIN_LPC_ORDER ||
272 avctx->max_prediction_order > MAX_LPC_ORDER) {
273 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
274 avctx->max_prediction_order);
278 if(avctx->max_prediction_order > MAX_FIXED_ORDER) {
279 av_log(avctx, AV_LOG_ERROR, "invalid max prediction order: %d\n",
280 avctx->max_prediction_order);
284 s->options.max_prediction_order = avctx->max_prediction_order;
286 if(s->options.max_prediction_order < s->options.min_prediction_order) {
287 av_log(avctx, AV_LOG_ERROR, "invalid prediction orders: min=%d max=%d\n",
288 s->options.min_prediction_order, s->options.max_prediction_order);
291 av_log(avctx, AV_LOG_DEBUG, " prediction order: %d, %d\n",
292 s->options.min_prediction_order, s->options.max_prediction_order);
294 if(avctx->prediction_order_method >= 0) {
295 if(avctx->prediction_order_method > ORDER_METHOD_LOG) {
296 av_log(avctx, AV_LOG_ERROR, "invalid prediction order method: %d\n",
297 avctx->prediction_order_method);
300 s->options.prediction_order_method = avctx->prediction_order_method;
302 switch(s->options.prediction_order_method) {
303 case ORDER_METHOD_EST: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
305 case ORDER_METHOD_2LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
307 case ORDER_METHOD_4LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
309 case ORDER_METHOD_8LEVEL: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
311 case ORDER_METHOD_SEARCH: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
312 "full search"); break;
313 case ORDER_METHOD_LOG: av_log(avctx, AV_LOG_DEBUG, " order method: %s\n",
314 "log search"); break;
317 if(avctx->min_partition_order >= 0) {
318 if(avctx->min_partition_order > MAX_PARTITION_ORDER) {
319 av_log(avctx, AV_LOG_ERROR, "invalid min partition order: %d\n",
320 avctx->min_partition_order);
323 s->options.min_partition_order = avctx->min_partition_order;
325 if(avctx->max_partition_order >= 0) {
326 if(avctx->max_partition_order > MAX_PARTITION_ORDER) {
327 av_log(avctx, AV_LOG_ERROR, "invalid max partition order: %d\n",
328 avctx->max_partition_order);
331 s->options.max_partition_order = avctx->max_partition_order;
333 if(s->options.max_partition_order < s->options.min_partition_order) {
334 av_log(avctx, AV_LOG_ERROR, "invalid partition orders: min=%d max=%d\n",
335 s->options.min_partition_order, s->options.max_partition_order);
338 av_log(avctx, AV_LOG_DEBUG, " partition order: %d, %d\n",
339 s->options.min_partition_order, s->options.max_partition_order);
341 if(avctx->frame_size > 0) {
342 if(avctx->frame_size < FLAC_MIN_BLOCKSIZE ||
343 avctx->frame_size > FLAC_MAX_BLOCKSIZE) {
344 av_log(avctx, AV_LOG_ERROR, "invalid block size: %d\n",
348 s->blocksize = avctx->frame_size;
350 s->blocksize = select_blocksize(s->samplerate, s->options.block_time_ms);
351 avctx->frame_size = s->blocksize;
353 av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->blocksize);
355 /* set LPC precision */
356 if(avctx->lpc_coeff_precision > 0) {
357 if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
358 av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
359 avctx->lpc_coeff_precision);
362 s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
364 /* select LPC precision based on block size */
365 if( s->blocksize <= 192) s->options.lpc_coeff_precision = 7;
366 else if(s->blocksize <= 384) s->options.lpc_coeff_precision = 8;
367 else if(s->blocksize <= 576) s->options.lpc_coeff_precision = 9;
368 else if(s->blocksize <= 1152) s->options.lpc_coeff_precision = 10;
369 else if(s->blocksize <= 2304) s->options.lpc_coeff_precision = 11;
370 else if(s->blocksize <= 4608) s->options.lpc_coeff_precision = 12;
371 else if(s->blocksize <= 8192) s->options.lpc_coeff_precision = 13;
372 else if(s->blocksize <= 16384) s->options.lpc_coeff_precision = 14;
373 else s->options.lpc_coeff_precision = 15;
375 av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
376 s->options.lpc_coeff_precision);
378 /* set maximum encoded frame size in verbatim mode */
379 if(s->channels == 2) {
380 s->max_framesize = 14 + ((s->blocksize * 33 + 7) >> 3);
382 s->max_framesize = 14 + (s->blocksize * s->channels * 2);
385 streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
386 write_streaminfo(s, streaminfo);
387 avctx->extradata = streaminfo;
388 avctx->extradata_size = FLAC_STREAMINFO_SIZE;
392 avctx->coded_frame = avcodec_alloc_frame();
393 avctx->coded_frame->key_frame = 1;
398 static void init_frame(FlacEncodeContext *s)
405 for(i=0; i<16; i++) {
406 if(s->blocksize == flac_blocksizes[i]) {
407 frame->blocksize = flac_blocksizes[i];
408 frame->bs_code[0] = i;
409 frame->bs_code[1] = 0;
414 frame->blocksize = s->blocksize;
415 if(frame->blocksize <= 256) {
416 frame->bs_code[0] = 6;
417 frame->bs_code[1] = frame->blocksize-1;
419 frame->bs_code[0] = 7;
420 frame->bs_code[1] = frame->blocksize-1;
424 for(ch=0; ch<s->channels; ch++) {
425 frame->subframes[ch].obits = 16;
430 * Copy channel-interleaved input samples into separate subframes
432 static void copy_samples(FlacEncodeContext *s, int16_t *samples)
438 for(i=0,j=0; i<frame->blocksize; i++) {
439 for(ch=0; ch<s->channels; ch++,j++) {
440 frame->subframes[ch].samples[i] = samples[j];
446 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
448 static int find_optimal_param(uint32_t sum, int n)
451 uint32_t nbits[MAX_RICE_PARAM+1];
454 nbits[0] = UINT32_MAX;
455 for(k=0; k<=MAX_RICE_PARAM; k++) {
456 nbits[k] = rice_encode_count(sum, n, k);
457 if(nbits[k] < nbits[k_opt]) {
464 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
465 uint32_t *sums, int n, int pred_order)
471 part = (1 << porder);
474 cnt = (n >> porder) - pred_order;
475 for(i=0; i<part; i++) {
476 if(i == 1) cnt = (n >> porder);
477 k = find_optimal_param(sums[i], cnt);
479 all_bits += rice_encode_count(sums[i], cnt, k);
481 all_bits += (4 * part);
488 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
489 uint32_t sums[][MAX_PARTITIONS])
493 uint32_t *res, *res_end;
495 /* sums for highest level */
497 res = &data[pred_order];
498 res_end = &data[n >> pmax];
499 for(i=0; i<parts; i++) {
501 while(res < res_end){
502 sums[pmax][i] += *(res++);
506 /* sums for lower levels */
507 for(i=pmax-1; i>=pmin; i--) {
509 for(j=0; j<parts; j++) {
510 sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
515 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
516 int32_t *data, int n, int pred_order)
519 uint32_t bits[MAX_PARTITION_ORDER+1];
523 uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
525 assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
526 assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
527 assert(pmin <= pmax);
529 udata = av_malloc(n * sizeof(uint32_t));
531 udata[i] = (2*data[i]) ^ (data[i]>>31);
534 calc_sums(pmin, pmax, udata, n, pred_order, sums);
537 bits[pmin] = UINT32_MAX;
538 for(i=pmin; i<=pmax; i++) {
539 bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
540 if(bits[i] <= bits[opt_porder]) {
547 return bits[opt_porder];
550 static int get_max_p_order(int max_porder, int n, int order)
552 int porder = FFMIN(max_porder, av_log2(n^(n-1)));
554 porder = FFMIN(porder, av_log2(n/order));
558 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
559 int32_t *data, int n, int pred_order,
563 pmin = get_max_p_order(pmin, n, pred_order);
564 pmax = get_max_p_order(pmax, n, pred_order);
565 bits = pred_order*bps + 6;
566 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
570 static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
571 int32_t *data, int n, int pred_order,
572 int bps, int precision)
575 pmin = get_max_p_order(pmin, n, pred_order);
576 pmax = get_max_p_order(pmax, n, pred_order);
577 bits = pred_order*bps + 4 + 5 + pred_order*precision + 6;
578 bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
583 * Apply Welch window function to audio block
585 static void apply_welch_window(const int32_t *data, int len, double *w_data)
592 c = 2.0 / (len - 1.0);
593 for(i=0; i<n2; i++) {
596 w_data[i] = data[i] * w;
597 w_data[len-1-i] = data[len-1-i] * w;
602 * Calculates autocorrelation data from audio samples
603 * A Welch window function is applied before calculation.
605 static void compute_autocorr(const int32_t *data, int len, int lag,
609 double tmp[len + lag];
610 double *data1= tmp + lag;
612 apply_welch_window(data, len, data1);
614 for(i=0; i<lag; i++){
619 for(i=0; i<len; i++){
620 for(lag_ptr= i-lag; lag_ptr<=i; lag_ptr++){
621 autoc[i-lag_ptr] += data1[i] * data1[lag_ptr];
627 * Levinson-Durbin recursion.
628 * Produces LPC coefficients from autocorrelation data.
630 static void compute_lpc_coefs(const double *autoc, int max_order,
631 double lpc[][MAX_LPC_ORDER], double *ref)
635 double lpc_tmp[MAX_LPC_ORDER];
637 for(i=0; i<max_order; i++) lpc_tmp[i] = 0;
640 for(i=0; i<max_order; i++) {
643 r -= lpc_tmp[j] * autoc[i-j];
648 err *= 1.0 - (r * r);
652 for(j=0; j<i2; j++) {
654 lpc_tmp[j] += r * lpc_tmp[i-1-j];
655 lpc_tmp[i-1-j] += r * tmp;
658 lpc_tmp[j] += lpc_tmp[j] * r;
661 for(j=0; j<=i; j++) {
662 lpc[i][j] = -lpc_tmp[j];
668 * Quantize LPC coefficients
670 static void quantize_lpc_coefs(double *lpc_in, int order, int precision,
671 int32_t *lpc_out, int *shift)
678 /* define maximum levels */
679 qmax = (1 << (precision - 1)) - 1;
681 /* find maximum coefficient value */
683 for(i=0; i<order; i++) {
684 cmax= FFMAX(cmax, fabs(lpc_in[i]));
687 /* if maximum value quantizes to zero, return all zeros */
688 if(cmax * (1 << MAX_LPC_SHIFT) < 1.0) {
690 memset(lpc_out, 0, sizeof(int32_t) * order);
694 /* calculate level shift which scales max coeff to available bits */
696 while((cmax * (1 << sh) > qmax) && (sh > 0)) {
700 /* since negative shift values are unsupported in decoder, scale down
701 coefficients instead */
702 if(sh == 0 && cmax > qmax) {
703 double scale = ((double)qmax) / cmax;
704 for(i=0; i<order; i++) {
709 /* output quantized coefficients and level shift */
711 for(i=0; i<order; i++) {
712 error += lpc_in[i] * (1 << sh);
713 lpc_out[i] = clip(lrintf(error), -qmax, qmax);
719 static int estimate_best_order(double *ref, int max_order)
724 for(i=max_order-1; i>=0; i--) {
734 * Calculate LPC coefficients for multiple orders
736 static int lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order,
737 int precision, int32_t coefs[][MAX_LPC_ORDER],
738 int *shift, int use_lpc, int omethod)
740 double autoc[MAX_LPC_ORDER+1];
741 double ref[MAX_LPC_ORDER];
742 double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
746 assert(max_order >= MIN_LPC_ORDER && max_order <= MAX_LPC_ORDER);
749 compute_autocorr(samples, blocksize, max_order+1, autoc);
751 compute_lpc_coefs(autoc, max_order, lpc, ref);
754 double var[MAX_LPC_ORDER+1], eval, weight;
756 for(pass=0; pass<use_lpc-1; pass++){
757 av_init_lls(&m[pass&1], max_order);
760 for(i=max_order; i<blocksize; i++){
761 for(j=0; j<=max_order; j++)
762 var[j]= samples[i-j];
765 eval= av_evaluate_lls(&m[(pass-1)&1], var+1, max_order-1);
766 eval= (512>>pass) + fabs(eval - var[0]);
767 for(j=0; j<=max_order; j++)
773 av_update_lls(&m[pass&1], var, 1.0);
775 av_solve_lls(&m[pass&1], 0.001, 0);
778 for(i=0; i<max_order; i++){
779 for(j=0; j<max_order; j++)
780 lpc[i][j]= m[(pass-1)&1].coeff[i][j];
781 ref[i]= sqrt(m[(pass-1)&1].variance[i] / weight) * (blocksize - max_order) / 4000;
783 for(i=max_order-1; i>0; i--)
784 ref[i] = ref[i-1] - ref[i];
786 opt_order = max_order;
788 if(omethod == ORDER_METHOD_EST) {
789 opt_order = estimate_best_order(ref, max_order);
791 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
793 for(i=0; i<max_order; i++) {
794 quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
802 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
805 memcpy(res, smp, n * sizeof(int32_t));
808 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
813 for(i=0; i<order; i++) {
818 for(i=order; i<n; i++)
821 for(i=order; i<n; i++)
822 res[i]= smp[i] - smp[i-1];
824 for(i=order; i<n; i++)
825 res[i]= smp[i] - 2*smp[i-1] + smp[i-2];
827 for(i=order; i<n; i++)
828 res[i]= smp[i] - 3*smp[i-1] + 3*smp[i-2] - smp[i-3];
830 for(i=order; i<n; i++)
831 res[i]= smp[i] - 4*smp[i-1] + 6*smp[i-2] - 4*smp[i-3] + smp[i-4];
835 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
836 int order, const int32_t *coefs, int shift)
841 for(i=0; i<order; i++) {
844 for(i=order; i<n; i++) {
846 for(j=0; j<order; j++) {
847 pred += coefs[j] * smp[i-j-1];
849 res[i] = smp[i] - (pred >> shift);
853 static int encode_residual(FlacEncodeContext *ctx, int ch)
856 int min_order, max_order, opt_order, precision, omethod;
857 int min_porder, max_porder;
860 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
861 int shift[MAX_LPC_ORDER];
865 sub = &frame->subframes[ch];
868 n = frame->blocksize;
872 if(smp[i] != smp[0]) break;
875 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
882 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
883 encode_residual_verbatim(res, smp, n);
884 return sub->obits * n;
887 min_order = ctx->options.min_prediction_order;
888 max_order = ctx->options.max_prediction_order;
889 min_porder = ctx->options.min_partition_order;
890 max_porder = ctx->options.max_partition_order;
891 precision = ctx->options.lpc_coeff_precision;
892 omethod = ctx->options.prediction_order_method;
895 if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {
896 uint32_t bits[MAX_FIXED_ORDER+1];
897 if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;
899 bits[0] = UINT32_MAX;
900 for(i=min_order; i<=max_order; i++) {
901 encode_residual_fixed(res, smp, n, i);
902 bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
904 if(bits[i] < bits[opt_order]) {
908 sub->order = opt_order;
909 sub->type = FLAC_SUBFRAME_FIXED;
910 sub->type_code = sub->type | sub->order;
911 if(sub->order != max_order) {
912 encode_residual_fixed(res, smp, n, sub->order);
913 return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
914 sub->order, sub->obits);
916 return bits[sub->order];
920 opt_order = lpc_calc_coefs(smp, n, max_order, precision, coefs, shift, ctx->options.use_lpc, omethod);
922 if(omethod == ORDER_METHOD_2LEVEL ||
923 omethod == ORDER_METHOD_4LEVEL ||
924 omethod == ORDER_METHOD_8LEVEL) {
925 int levels = 1 << omethod;
926 uint32_t bits[levels];
928 int opt_index = levels-1;
929 opt_order = max_order-1;
930 bits[opt_index] = UINT32_MAX;
931 for(i=levels-1; i>=0; i--) {
932 order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
933 if(order < 0) order = 0;
934 encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
935 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
936 res, n, order+1, sub->obits, precision);
937 if(bits[i] < bits[opt_index]) {
943 } else if(omethod == ORDER_METHOD_SEARCH) {
944 // brute-force optimal order search
945 uint32_t bits[MAX_LPC_ORDER];
947 bits[0] = UINT32_MAX;
948 for(i=min_order-1; i<max_order; i++) {
949 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
950 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
951 res, n, i+1, sub->obits, precision);
952 if(bits[i] < bits[opt_order]) {
957 } else if(omethod == ORDER_METHOD_LOG) {
958 uint32_t bits[MAX_LPC_ORDER];
961 opt_order= min_order - 1 + (max_order-min_order)/3;
962 memset(bits, -1, sizeof(bits));
964 for(step=16 ;step; step>>=1){
966 for(i=last-step; i<=last+step; i+= step){
967 if(i<min_order-1 || i>=max_order || bits[i] < UINT32_MAX)
969 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
970 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
971 res, n, i+1, sub->obits, precision);
972 if(bits[i] < bits[opt_order])
979 sub->order = opt_order;
980 sub->type = FLAC_SUBFRAME_LPC;
981 sub->type_code = sub->type | (sub->order-1);
982 sub->shift = shift[sub->order-1];
983 for(i=0; i<sub->order; i++) {
984 sub->coefs[i] = coefs[sub->order-1][i];
986 encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
987 return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,
988 sub->obits, precision);
991 static int encode_residual_v(FlacEncodeContext *ctx, int ch)
999 sub = &frame->subframes[ch];
1000 res = sub->residual;
1002 n = frame->blocksize;
1005 for(i=1; i<n; i++) {
1006 if(smp[i] != smp[0]) break;
1009 sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
1015 sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
1016 encode_residual_verbatim(res, smp, n);
1017 return sub->obits * n;
1020 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
1028 /* calculate sum of 2nd order residual for each channel */
1029 sum[0] = sum[1] = sum[2] = sum[3] = 0;
1030 for(i=2; i<n; i++) {
1031 lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
1032 rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
1033 sum[2] += ABS((lt + rt) >> 1);
1034 sum[3] += ABS(lt - rt);
1038 /* estimate bit counts */
1039 for(i=0; i<4; i++) {
1040 k = find_optimal_param(2*sum[i], n);
1041 sum[i] = rice_encode_count(2*sum[i], n, k);
1044 /* calculate score for each mode */
1045 score[0] = sum[0] + sum[1];
1046 score[1] = sum[0] + sum[3];
1047 score[2] = sum[1] + sum[3];
1048 score[3] = sum[2] + sum[3];
1050 /* return mode with lowest score */
1052 for(i=1; i<4; i++) {
1053 if(score[i] < score[best]) {
1058 return FLAC_CHMODE_LEFT_RIGHT;
1059 } else if(best == 1) {
1060 return FLAC_CHMODE_LEFT_SIDE;
1061 } else if(best == 2) {
1062 return FLAC_CHMODE_RIGHT_SIDE;
1064 return FLAC_CHMODE_MID_SIDE;
1069 * Perform stereo channel decorrelation
1071 static void channel_decorrelation(FlacEncodeContext *ctx)
1074 int32_t *left, *right;
1077 frame = &ctx->frame;
1078 n = frame->blocksize;
1079 left = frame->subframes[0].samples;
1080 right = frame->subframes[1].samples;
1082 if(ctx->channels != 2) {
1083 frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
1087 frame->ch_mode = estimate_stereo_mode(left, right, n);
1089 /* perform decorrelation and adjust bits-per-sample */
1090 if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
1093 if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
1095 for(i=0; i<n; i++) {
1097 left[i] = (tmp + right[i]) >> 1;
1098 right[i] = tmp - right[i];
1100 frame->subframes[1].obits++;
1101 } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
1102 for(i=0; i<n; i++) {
1103 right[i] = left[i] - right[i];
1105 frame->subframes[1].obits++;
1107 for(i=0; i<n; i++) {
1108 left[i] -= right[i];
1110 frame->subframes[0].obits++;
1114 static void put_sbits(PutBitContext *pb, int bits, int32_t val)
1116 assert(bits >= 0 && bits <= 31);
1118 put_bits(pb, bits, val & ((1<<bits)-1));
1121 static void write_utf8(PutBitContext *pb, uint32_t val)
1126 put_bits(pb, 8, val);
1130 bytes= (av_log2(val)+4) / 5;
1131 shift = (bytes - 1) * 6;
1132 put_bits(pb, 8, (256 - (256>>bytes)) | (val >> shift));
1135 put_bits(pb, 8, 0x80 | ((val >> shift) & 0x3F));
1139 static void output_frame_header(FlacEncodeContext *s)
1146 put_bits(&s->pb, 16, 0xFFF8);
1147 put_bits(&s->pb, 4, frame->bs_code[0]);
1148 put_bits(&s->pb, 4, s->sr_code[0]);
1149 if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
1150 put_bits(&s->pb, 4, s->ch_code);
1152 put_bits(&s->pb, 4, frame->ch_mode);
1154 put_bits(&s->pb, 3, 4); /* bits-per-sample code */
1155 put_bits(&s->pb, 1, 0);
1156 write_utf8(&s->pb, s->frame_count);
1157 if(frame->bs_code[0] == 6) {
1158 put_bits(&s->pb, 8, frame->bs_code[1]);
1159 } else if(frame->bs_code[0] == 7) {
1160 put_bits(&s->pb, 16, frame->bs_code[1]);
1162 if(s->sr_code[0] == 12) {
1163 put_bits(&s->pb, 8, s->sr_code[1]);
1164 } else if(s->sr_code[0] > 12) {
1165 put_bits(&s->pb, 16, s->sr_code[1]);
1167 flush_put_bits(&s->pb);
1168 crc = av_crc(av_crc07, 0, s->pb.buf, put_bits_count(&s->pb)>>3);
1169 put_bits(&s->pb, 8, crc);
1172 static void output_subframe_constant(FlacEncodeContext *s, int ch)
1177 sub = &s->frame.subframes[ch];
1178 res = sub->residual[0];
1179 put_sbits(&s->pb, sub->obits, res);
1182 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
1190 sub = &frame->subframes[ch];
1192 for(i=0; i<frame->blocksize; i++) {
1193 res = sub->residual[i];
1194 put_sbits(&s->pb, sub->obits, res);
1198 static void output_residual(FlacEncodeContext *ctx, int ch)
1200 int i, j, p, n, parts;
1201 int k, porder, psize, res_cnt;
1206 frame = &ctx->frame;
1207 sub = &frame->subframes[ch];
1208 res = sub->residual;
1209 n = frame->blocksize;
1211 /* rice-encoded block */
1212 put_bits(&ctx->pb, 2, 0);
1214 /* partition order */
1215 porder = sub->rc.porder;
1216 psize = n >> porder;
1217 parts = (1 << porder);
1218 put_bits(&ctx->pb, 4, porder);
1219 res_cnt = psize - sub->order;
1223 for(p=0; p<parts; p++) {
1224 k = sub->rc.params[p];
1225 put_bits(&ctx->pb, 4, k);
1226 if(p == 1) res_cnt = psize;
1227 for(i=0; i<res_cnt && j<n; i++, j++) {
1228 set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
1233 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
1239 frame = &ctx->frame;
1240 sub = &frame->subframes[ch];
1242 /* warm-up samples */
1243 for(i=0; i<sub->order; i++) {
1244 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1248 output_residual(ctx, ch);
1251 static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
1257 frame = &ctx->frame;
1258 sub = &frame->subframes[ch];
1260 /* warm-up samples */
1261 for(i=0; i<sub->order; i++) {
1262 put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
1265 /* LPC coefficients */
1266 cbits = ctx->options.lpc_coeff_precision;
1267 put_bits(&ctx->pb, 4, cbits-1);
1268 put_sbits(&ctx->pb, 5, sub->shift);
1269 for(i=0; i<sub->order; i++) {
1270 put_sbits(&ctx->pb, cbits, sub->coefs[i]);
1274 output_residual(ctx, ch);
1277 static void output_subframes(FlacEncodeContext *s)
1285 for(ch=0; ch<s->channels; ch++) {
1286 sub = &frame->subframes[ch];
1288 /* subframe header */
1289 put_bits(&s->pb, 1, 0);
1290 put_bits(&s->pb, 6, sub->type_code);
1291 put_bits(&s->pb, 1, 0); /* no wasted bits */
1294 if(sub->type == FLAC_SUBFRAME_CONSTANT) {
1295 output_subframe_constant(s, ch);
1296 } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
1297 output_subframe_verbatim(s, ch);
1298 } else if(sub->type == FLAC_SUBFRAME_FIXED) {
1299 output_subframe_fixed(s, ch);
1300 } else if(sub->type == FLAC_SUBFRAME_LPC) {
1301 output_subframe_lpc(s, ch);
1306 static void output_frame_footer(FlacEncodeContext *s)
1309 flush_put_bits(&s->pb);
1310 crc = bswap_16(av_crc(av_crc8005, 0, s->pb.buf, put_bits_count(&s->pb)>>3));
1311 put_bits(&s->pb, 16, crc);
1312 flush_put_bits(&s->pb);
1315 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
1316 int buf_size, void *data)
1319 FlacEncodeContext *s;
1320 int16_t *samples = data;
1323 s = avctx->priv_data;
1325 s->blocksize = avctx->frame_size;
1328 copy_samples(s, samples);
1330 channel_decorrelation(s);
1332 for(ch=0; ch<s->channels; ch++) {
1333 encode_residual(s, ch);
1335 init_put_bits(&s->pb, frame, buf_size);
1336 output_frame_header(s);
1337 output_subframes(s);
1338 output_frame_footer(s);
1339 out_bytes = put_bits_count(&s->pb) >> 3;
1341 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
1342 /* frame too large. use verbatim mode */
1343 for(ch=0; ch<s->channels; ch++) {
1344 encode_residual_v(s, ch);
1346 init_put_bits(&s->pb, frame, buf_size);
1347 output_frame_header(s);
1348 output_subframes(s);
1349 output_frame_footer(s);
1350 out_bytes = put_bits_count(&s->pb) >> 3;
1352 if(out_bytes > s->max_framesize || out_bytes >= buf_size) {
1353 /* still too large. must be an error. */
1354 av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
1363 static int flac_encode_close(AVCodecContext *avctx)
1365 av_freep(&avctx->extradata);
1366 avctx->extradata_size = 0;
1367 av_freep(&avctx->coded_frame);
1371 AVCodec flac_encoder = {
1375 sizeof(FlacEncodeContext),
1380 .capabilities = CODEC_CAP_SMALL_LAST_FRAME,