• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/flacenc.c

Go to the documentation of this file.
00001 
00022 #include "libavutil/crc.h"
00023 #include "libavutil/lls.h"
00024 #include "libavutil/md5.h"
00025 #include "avcodec.h"
00026 #include "bitstream.h"
00027 #include "dsputil.h"
00028 #include "golomb.h"
00029 #include "lpc.h"
00030 
00031 #define FLAC_MAX_CH  8
00032 #define FLAC_MIN_BLOCKSIZE  16
00033 #define FLAC_MAX_BLOCKSIZE  65535
00034 
00035 #define FLAC_SUBFRAME_CONSTANT  0
00036 #define FLAC_SUBFRAME_VERBATIM  1
00037 #define FLAC_SUBFRAME_FIXED     8
00038 #define FLAC_SUBFRAME_LPC      32
00039 
00040 #define FLAC_CHMODE_NOT_STEREO      0
00041 #define FLAC_CHMODE_LEFT_RIGHT      1
00042 #define FLAC_CHMODE_LEFT_SIDE       8
00043 #define FLAC_CHMODE_RIGHT_SIDE      9
00044 #define FLAC_CHMODE_MID_SIDE       10
00045 
00046 #define FLAC_STREAMINFO_SIZE  34
00047 
00048 #define MAX_FIXED_ORDER     4
00049 #define MAX_PARTITION_ORDER 8
00050 #define MAX_PARTITIONS     (1 << MAX_PARTITION_ORDER)
00051 #define MAX_LPC_PRECISION  15
00052 #define MAX_LPC_SHIFT      15
00053 #define MAX_RICE_PARAM     14
00054 
00055 typedef struct CompressionOptions {
00056     int compression_level;
00057     int block_time_ms;
00058     int use_lpc;
00059     int lpc_coeff_precision;
00060     int min_prediction_order;
00061     int max_prediction_order;
00062     int prediction_order_method;
00063     int min_partition_order;
00064     int max_partition_order;
00065 } CompressionOptions;
00066 
00067 typedef struct RiceContext {
00068     int porder;
00069     int params[MAX_PARTITIONS];
00070 } RiceContext;
00071 
00072 typedef struct FlacSubframe {
00073     int type;
00074     int type_code;
00075     int obits;
00076     int order;
00077     int32_t coefs[MAX_LPC_ORDER];
00078     int shift;
00079     RiceContext rc;
00080     int32_t samples[FLAC_MAX_BLOCKSIZE];
00081     int32_t residual[FLAC_MAX_BLOCKSIZE+1];
00082 } FlacSubframe;
00083 
00084 typedef struct FlacFrame {
00085     FlacSubframe subframes[FLAC_MAX_CH];
00086     int blocksize;
00087     int bs_code[2];
00088     uint8_t crc8;
00089     int ch_mode;
00090 } FlacFrame;
00091 
00092 typedef struct FlacEncodeContext {
00093     PutBitContext pb;
00094     int channels;
00095     int ch_code;
00096     int samplerate;
00097     int sr_code[2];
00098     int min_framesize;
00099     int min_encoded_framesize;
00100     int max_framesize;
00101     int max_encoded_framesize;
00102     uint32_t frame_count;
00103     uint64_t sample_count;
00104     uint8_t md5sum[16];
00105     FlacFrame frame;
00106     CompressionOptions options;
00107     AVCodecContext *avctx;
00108     DSPContext dsp;
00109     struct AVMD5 *md5ctx;
00110 } FlacEncodeContext;
00111 
00112 static const int flac_samplerates[16] = {
00113     0, 0, 0, 0,
00114     8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
00115     0, 0, 0, 0
00116 };
00117 
00118 static const int flac_blocksizes[16] = {
00119     0,
00120     192,
00121     576, 1152, 2304, 4608,
00122     0, 0,
00123     256, 512, 1024, 2048, 4096, 8192, 16384, 32768
00124 };
00125 
00129 static void write_streaminfo(FlacEncodeContext *s, uint8_t *header)
00130 {
00131     PutBitContext pb;
00132 
00133     memset(header, 0, FLAC_STREAMINFO_SIZE);
00134     init_put_bits(&pb, header, FLAC_STREAMINFO_SIZE);
00135 
00136     /* streaminfo metadata block */
00137     put_bits(&pb, 16, s->avctx->frame_size);
00138     put_bits(&pb, 16, s->avctx->frame_size);
00139     put_bits(&pb, 24, s->min_framesize);
00140     put_bits(&pb, 24, s->max_framesize);
00141     put_bits(&pb, 20, s->samplerate);
00142     put_bits(&pb, 3, s->channels-1);
00143     put_bits(&pb, 5, 15);       /* bits per sample - 1 */
00144     /* write 36-bit sample count in 2 put_bits() calls */
00145     put_bits(&pb, 24, (s->sample_count & 0xFFFFFF000LL) >> 12);
00146     put_bits(&pb, 12,  s->sample_count & 0x000000FFFLL);
00147     flush_put_bits(&pb);
00148     memcpy(&header[18], s->md5sum, 16);
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 av_cold 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     /* find samplerate in table */
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     /* if not in table, samplerate is non-standard */
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     /* set compression option defaults based on avctx->compression_level */
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     /* set compression option overrides from AVCodecContext */
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     } else {
00355         s->avctx->frame_size = select_blocksize(s->samplerate, s->options.block_time_ms);
00356     }
00357     av_log(avctx, AV_LOG_DEBUG, " block size: %d\n", s->avctx->frame_size);
00358 
00359     /* set LPC precision */
00360     if(avctx->lpc_coeff_precision > 0) {
00361         if(avctx->lpc_coeff_precision > MAX_LPC_PRECISION) {
00362             av_log(avctx, AV_LOG_ERROR, "invalid lpc coeff precision: %d\n",
00363                    avctx->lpc_coeff_precision);
00364             return -1;
00365         }
00366         s->options.lpc_coeff_precision = avctx->lpc_coeff_precision;
00367     } else {
00368         /* default LPC precision */
00369         s->options.lpc_coeff_precision = 15;
00370     }
00371     av_log(avctx, AV_LOG_DEBUG, " lpc precision: %d\n",
00372            s->options.lpc_coeff_precision);
00373 
00374     /* set maximum encoded frame size in verbatim mode */
00375     if(s->channels == 2) {
00376         s->max_framesize = 14 + ((s->avctx->frame_size * 33 + 7) >> 3);
00377     } else {
00378         s->max_framesize = 14 + (s->avctx->frame_size * s->channels * 2);
00379     }
00380     s->min_encoded_framesize = 0xFFFFFF;
00381 
00382     /* initialize MD5 context */
00383     s->md5ctx = av_malloc(av_md5_size);
00384     if(!s->md5ctx)
00385         return AVERROR_NOMEM;
00386     av_md5_init(s->md5ctx);
00387 
00388     streaminfo = av_malloc(FLAC_STREAMINFO_SIZE);
00389     write_streaminfo(s, streaminfo);
00390     avctx->extradata = streaminfo;
00391     avctx->extradata_size = FLAC_STREAMINFO_SIZE;
00392 
00393     s->frame_count = 0;
00394 
00395     avctx->coded_frame = avcodec_alloc_frame();
00396     avctx->coded_frame->key_frame = 1;
00397 
00398     return 0;
00399 }
00400 
00401 static void init_frame(FlacEncodeContext *s)
00402 {
00403     int i, ch;
00404     FlacFrame *frame;
00405 
00406     frame = &s->frame;
00407 
00408     for(i=0; i<16; i++) {
00409         if(s->avctx->frame_size == flac_blocksizes[i]) {
00410             frame->blocksize = flac_blocksizes[i];
00411             frame->bs_code[0] = i;
00412             frame->bs_code[1] = 0;
00413             break;
00414         }
00415     }
00416     if(i == 16) {
00417         frame->blocksize = s->avctx->frame_size;
00418         if(frame->blocksize <= 256) {
00419             frame->bs_code[0] = 6;
00420             frame->bs_code[1] = frame->blocksize-1;
00421         } else {
00422             frame->bs_code[0] = 7;
00423             frame->bs_code[1] = frame->blocksize-1;
00424         }
00425     }
00426 
00427     for(ch=0; ch<s->channels; ch++) {
00428         frame->subframes[ch].obits = 16;
00429     }
00430 }
00431 
00435 static void copy_samples(FlacEncodeContext *s, int16_t *samples)
00436 {
00437     int i, j, ch;
00438     FlacFrame *frame;
00439 
00440     frame = &s->frame;
00441     for(i=0,j=0; i<frame->blocksize; i++) {
00442         for(ch=0; ch<s->channels; ch++,j++) {
00443             frame->subframes[ch].samples[i] = samples[j];
00444         }
00445     }
00446 }
00447 
00448 
00449 #define rice_encode_count(sum, n, k) (((n)*((k)+1))+((sum-(n>>1))>>(k)))
00450 
00454 static int find_optimal_param(uint32_t sum, int n)
00455 {
00456     int k;
00457     uint32_t sum2;
00458 
00459     if(sum <= n>>1)
00460         return 0;
00461     sum2 = sum-(n>>1);
00462     k = av_log2(n<256 ? FASTDIV(sum2,n) : sum2/n);
00463     return FFMIN(k, MAX_RICE_PARAM);
00464 }
00465 
00466 static uint32_t calc_optimal_rice_params(RiceContext *rc, int porder,
00467                                          uint32_t *sums, int n, int pred_order)
00468 {
00469     int i;
00470     int k, cnt, part;
00471     uint32_t all_bits;
00472 
00473     part = (1 << porder);
00474     all_bits = 4 * part;
00475 
00476     cnt = (n >> porder) - pred_order;
00477     for(i=0; i<part; i++) {
00478         k = find_optimal_param(sums[i], cnt);
00479         rc->params[i] = k;
00480         all_bits += rice_encode_count(sums[i], cnt, k);
00481         cnt = n >> porder;
00482     }
00483 
00484     rc->porder = porder;
00485 
00486     return all_bits;
00487 }
00488 
00489 static void calc_sums(int pmin, int pmax, uint32_t *data, int n, int pred_order,
00490                       uint32_t sums[][MAX_PARTITIONS])
00491 {
00492     int i, j;
00493     int parts;
00494     uint32_t *res, *res_end;
00495 
00496     /* sums for highest level */
00497     parts = (1 << pmax);
00498     res = &data[pred_order];
00499     res_end = &data[n >> pmax];
00500     for(i=0; i<parts; i++) {
00501         uint32_t sum = 0;
00502         while(res < res_end){
00503             sum += *(res++);
00504         }
00505         sums[pmax][i] = sum;
00506         res_end+= n >> pmax;
00507     }
00508     /* sums for lower levels */
00509     for(i=pmax-1; i>=pmin; i--) {
00510         parts = (1 << i);
00511         for(j=0; j<parts; j++) {
00512             sums[i][j] = sums[i+1][2*j] + sums[i+1][2*j+1];
00513         }
00514     }
00515 }
00516 
00517 static uint32_t calc_rice_params(RiceContext *rc, int pmin, int pmax,
00518                                  int32_t *data, int n, int pred_order)
00519 {
00520     int i;
00521     uint32_t bits[MAX_PARTITION_ORDER+1];
00522     int opt_porder;
00523     RiceContext tmp_rc;
00524     uint32_t *udata;
00525     uint32_t sums[MAX_PARTITION_ORDER+1][MAX_PARTITIONS];
00526 
00527     assert(pmin >= 0 && pmin <= MAX_PARTITION_ORDER);
00528     assert(pmax >= 0 && pmax <= MAX_PARTITION_ORDER);
00529     assert(pmin <= pmax);
00530 
00531     udata = av_malloc(n * sizeof(uint32_t));
00532     for(i=0; i<n; i++) {
00533         udata[i] = (2*data[i]) ^ (data[i]>>31);
00534     }
00535 
00536     calc_sums(pmin, pmax, udata, n, pred_order, sums);
00537 
00538     opt_porder = pmin;
00539     bits[pmin] = UINT32_MAX;
00540     for(i=pmin; i<=pmax; i++) {
00541         bits[i] = calc_optimal_rice_params(&tmp_rc, i, sums[i], n, pred_order);
00542         if(bits[i] <= bits[opt_porder]) {
00543             opt_porder = i;
00544             *rc= tmp_rc;
00545         }
00546     }
00547 
00548     av_freep(&udata);
00549     return bits[opt_porder];
00550 }
00551 
00552 static int get_max_p_order(int max_porder, int n, int order)
00553 {
00554     int porder = FFMIN(max_porder, av_log2(n^(n-1)));
00555     if(order > 0)
00556         porder = FFMIN(porder, av_log2(n/order));
00557     return porder;
00558 }
00559 
00560 static uint32_t calc_rice_params_fixed(RiceContext *rc, int pmin, int pmax,
00561                                        int32_t *data, int n, int pred_order,
00562                                        int bps)
00563 {
00564     uint32_t bits;
00565     pmin = get_max_p_order(pmin, n, pred_order);
00566     pmax = get_max_p_order(pmax, n, pred_order);
00567     bits = pred_order*bps + 6;
00568     bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
00569     return bits;
00570 }
00571 
00572 static uint32_t calc_rice_params_lpc(RiceContext *rc, int pmin, int pmax,
00573                                      int32_t *data, int n, int pred_order,
00574                                      int bps, int precision)
00575 {
00576     uint32_t bits;
00577     pmin = get_max_p_order(pmin, n, pred_order);
00578     pmax = get_max_p_order(pmax, n, pred_order);
00579     bits = pred_order*bps + 4 + 5 + pred_order*precision + 6;
00580     bits += calc_rice_params(rc, pmin, pmax, data, n, pred_order);
00581     return bits;
00582 }
00583 
00587 static void apply_welch_window(const int32_t *data, int len, double *w_data)
00588 {
00589     int i, n2;
00590     double w;
00591     double c;
00592 
00593     assert(!(len&1)); //the optimization in r11881 does not support odd len
00594                       //if someone wants odd len extend the change in r11881
00595 
00596     n2 = (len >> 1);
00597     c = 2.0 / (len - 1.0);
00598 
00599     w_data+=n2;
00600       data+=n2;
00601     for(i=0; i<n2; i++) {
00602         w = c - n2 + i;
00603         w = 1.0 - (w * w);
00604         w_data[-i-1] = data[-i-1] * w;
00605         w_data[+i  ] = data[+i  ] * w;
00606     }
00607 }
00608 
00613 void ff_flac_compute_autocorr(const int32_t *data, int len, int lag,
00614                               double *autoc)
00615 {
00616     int i, j;
00617     double tmp[len + lag + 1];
00618     double *data1= tmp + lag;
00619 
00620     apply_welch_window(data, len, data1);
00621 
00622     for(j=0; j<lag; j++)
00623         data1[j-lag]= 0.0;
00624     data1[len] = 0.0;
00625 
00626     for(j=0; j<lag; j+=2){
00627         double sum0 = 1.0, sum1 = 1.0;
00628         for(i=0; i<len; i++){
00629             sum0 += data1[i] * data1[i-j];
00630             sum1 += data1[i] * data1[i-j-1];
00631         }
00632         autoc[j  ] = sum0;
00633         autoc[j+1] = sum1;
00634     }
00635 
00636     if(j==lag){
00637         double sum = 1.0;
00638         for(i=0; i<len; i+=2){
00639             sum += data1[i  ] * data1[i-j  ]
00640                  + data1[i+1] * data1[i-j+1];
00641         }
00642         autoc[j] = sum;
00643     }
00644 }
00645 
00646 
00647 static void encode_residual_verbatim(int32_t *res, int32_t *smp, int n)
00648 {
00649     assert(n > 0);
00650     memcpy(res, smp, n * sizeof(int32_t));
00651 }
00652 
00653 static void encode_residual_fixed(int32_t *res, const int32_t *smp, int n,
00654                                   int order)
00655 {
00656     int i;
00657 
00658     for(i=0; i<order; i++) {
00659         res[i] = smp[i];
00660     }
00661 
00662     if(order==0){
00663         for(i=order; i<n; i++)
00664             res[i]= smp[i];
00665     }else if(order==1){
00666         for(i=order; i<n; i++)
00667             res[i]= smp[i] - smp[i-1];
00668     }else if(order==2){
00669         int a = smp[order-1] - smp[order-2];
00670         for(i=order; i<n; i+=2) {
00671             int b = smp[i] - smp[i-1];
00672             res[i]= b - a;
00673             a = smp[i+1] - smp[i];
00674             res[i+1]= a - b;
00675         }
00676     }else if(order==3){
00677         int a = smp[order-1] - smp[order-2];
00678         int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00679         for(i=order; i<n; i+=2) {
00680             int b = smp[i] - smp[i-1];
00681             int d = b - a;
00682             res[i]= d - c;
00683             a = smp[i+1] - smp[i];
00684             c = a - b;
00685             res[i+1]= c - d;
00686         }
00687     }else{
00688         int a = smp[order-1] - smp[order-2];
00689         int c = smp[order-1] - 2*smp[order-2] + smp[order-3];
00690         int e = smp[order-1] - 3*smp[order-2] + 3*smp[order-3] - smp[order-4];
00691         for(i=order; i<n; i+=2) {
00692             int b = smp[i] - smp[i-1];
00693             int d = b - a;
00694             int f = d - c;
00695             res[i]= f - e;
00696             a = smp[i+1] - smp[i];
00697             c = a - b;
00698             e = c - d;
00699             res[i+1]= e - f;
00700         }
00701     }
00702 }
00703 
00704 #define LPC1(x) {\
00705     int c = coefs[(x)-1];\
00706     p0 += c*s;\
00707     s = smp[i-(x)+1];\
00708     p1 += c*s;\
00709 }
00710 
00711 static av_always_inline void encode_residual_lpc_unrolled(
00712     int32_t *res, const int32_t *smp, int n,
00713     int order, const int32_t *coefs, int shift, int big)
00714 {
00715     int i;
00716     for(i=order; i<n; i+=2) {
00717         int s = smp[i-order];
00718         int p0 = 0, p1 = 0;
00719         if(big) {
00720             switch(order) {
00721                 case 32: LPC1(32)
00722                 case 31: LPC1(31)
00723                 case 30: LPC1(30)
00724                 case 29: LPC1(29)
00725                 case 28: LPC1(28)
00726                 case 27: LPC1(27)
00727                 case 26: LPC1(26)
00728                 case 25: LPC1(25)
00729                 case 24: LPC1(24)
00730                 case 23: LPC1(23)
00731                 case 22: LPC1(22)
00732                 case 21: LPC1(21)
00733                 case 20: LPC1(20)
00734                 case 19: LPC1(19)
00735                 case 18: LPC1(18)
00736                 case 17: LPC1(17)
00737                 case 16: LPC1(16)
00738                 case 15: LPC1(15)
00739                 case 14: LPC1(14)
00740                 case 13: LPC1(13)
00741                 case 12: LPC1(12)
00742                 case 11: LPC1(11)
00743                 case 10: LPC1(10)
00744                 case  9: LPC1( 9)
00745                          LPC1( 8)
00746                          LPC1( 7)
00747                          LPC1( 6)
00748                          LPC1( 5)
00749                          LPC1( 4)
00750                          LPC1( 3)
00751                          LPC1( 2)
00752                          LPC1( 1)
00753             }
00754         } else {
00755             switch(order) {
00756                 case  8: LPC1( 8)
00757                 case  7: LPC1( 7)
00758                 case  6: LPC1( 6)
00759                 case  5: LPC1( 5)
00760                 case  4: LPC1( 4)
00761                 case  3: LPC1( 3)
00762                 case  2: LPC1( 2)
00763                 case  1: LPC1( 1)
00764             }
00765         }
00766         res[i  ] = smp[i  ] - (p0 >> shift);
00767         res[i+1] = smp[i+1] - (p1 >> shift);
00768     }
00769 }
00770 
00771 static void encode_residual_lpc(int32_t *res, const int32_t *smp, int n,
00772                                 int order, const int32_t *coefs, int shift)
00773 {
00774     int i;
00775     for(i=0; i<order; i++) {
00776         res[i] = smp[i];
00777     }
00778 #if CONFIG_SMALL
00779     for(i=order; i<n; i+=2) {
00780         int j;
00781         int s = smp[i];
00782         int p0 = 0, p1 = 0;
00783         for(j=0; j<order; j++) {
00784             int c = coefs[j];
00785             p1 += c*s;
00786             s = smp[i-j-1];
00787             p0 += c*s;
00788         }
00789         res[i  ] = smp[i  ] - (p0 >> shift);
00790         res[i+1] = smp[i+1] - (p1 >> shift);
00791     }
00792 #else
00793     switch(order) {
00794         case  1: encode_residual_lpc_unrolled(res, smp, n, 1, coefs, shift, 0); break;
00795         case  2: encode_residual_lpc_unrolled(res, smp, n, 2, coefs, shift, 0); break;
00796         case  3: encode_residual_lpc_unrolled(res, smp, n, 3, coefs, shift, 0); break;
00797         case  4: encode_residual_lpc_unrolled(res, smp, n, 4, coefs, shift, 0); break;
00798         case  5: encode_residual_lpc_unrolled(res, smp, n, 5, coefs, shift, 0); break;
00799         case  6: encode_residual_lpc_unrolled(res, smp, n, 6, coefs, shift, 0); break;
00800         case  7: encode_residual_lpc_unrolled(res, smp, n, 7, coefs, shift, 0); break;
00801         case  8: encode_residual_lpc_unrolled(res, smp, n, 8, coefs, shift, 0); break;
00802         default: encode_residual_lpc_unrolled(res, smp, n, order, coefs, shift, 1); break;
00803     }
00804 #endif
00805 }
00806 
00807 static int encode_residual(FlacEncodeContext *ctx, int ch)
00808 {
00809     int i, n;
00810     int min_order, max_order, opt_order, precision, omethod;
00811     int min_porder, max_porder;
00812     FlacFrame *frame;
00813     FlacSubframe *sub;
00814     int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
00815     int shift[MAX_LPC_ORDER];
00816     int32_t *res, *smp;
00817 
00818     frame = &ctx->frame;
00819     sub = &frame->subframes[ch];
00820     res = sub->residual;
00821     smp = sub->samples;
00822     n = frame->blocksize;
00823 
00824     /* CONSTANT */
00825     for(i=1; i<n; i++) {
00826         if(smp[i] != smp[0]) break;
00827     }
00828     if(i == n) {
00829         sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
00830         res[0] = smp[0];
00831         return sub->obits;
00832     }
00833 
00834     /* VERBATIM */
00835     if(n < 5) {
00836         sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
00837         encode_residual_verbatim(res, smp, n);
00838         return sub->obits * n;
00839     }
00840 
00841     min_order = ctx->options.min_prediction_order;
00842     max_order = ctx->options.max_prediction_order;
00843     min_porder = ctx->options.min_partition_order;
00844     max_porder = ctx->options.max_partition_order;
00845     precision = ctx->options.lpc_coeff_precision;
00846     omethod = ctx->options.prediction_order_method;
00847 
00848     /* FIXED */
00849     if(!ctx->options.use_lpc || max_order == 0 || (n <= max_order)) {
00850         uint32_t bits[MAX_FIXED_ORDER+1];
00851         if(max_order > MAX_FIXED_ORDER) max_order = MAX_FIXED_ORDER;
00852         opt_order = 0;
00853         bits[0] = UINT32_MAX;
00854         for(i=min_order; i<=max_order; i++) {
00855             encode_residual_fixed(res, smp, n, i);
00856             bits[i] = calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res,
00857                                              n, i, sub->obits);
00858             if(bits[i] < bits[opt_order]) {
00859                 opt_order = i;
00860             }
00861         }
00862         sub->order = opt_order;
00863         sub->type = FLAC_SUBFRAME_FIXED;
00864         sub->type_code = sub->type | sub->order;
00865         if(sub->order != max_order) {
00866             encode_residual_fixed(res, smp, n, sub->order);
00867             return calc_rice_params_fixed(&sub->rc, min_porder, max_porder, res, n,
00868                                           sub->order, sub->obits);
00869         }
00870         return bits[sub->order];
00871     }
00872 
00873     /* LPC */
00874     opt_order = ff_lpc_calc_coefs(&ctx->dsp, smp, n, min_order, max_order,
00875                                   precision, coefs, shift, ctx->options.use_lpc,
00876                                   omethod, MAX_LPC_SHIFT, 0);
00877 
00878     if(omethod == ORDER_METHOD_2LEVEL ||
00879        omethod == ORDER_METHOD_4LEVEL ||
00880        omethod == ORDER_METHOD_8LEVEL) {
00881         int levels = 1 << omethod;
00882         uint32_t bits[levels];
00883         int order;
00884         int opt_index = levels-1;
00885         opt_order = max_order-1;
00886         bits[opt_index] = UINT32_MAX;
00887         for(i=levels-1; i>=0; i--) {
00888             order = min_order + (((max_order-min_order+1) * (i+1)) / levels)-1;
00889             if(order < 0) order = 0;
00890             encode_residual_lpc(res, smp, n, order+1, coefs[order], shift[order]);
00891             bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
00892                                            res, n, order+1, sub->obits, precision);
00893             if(bits[i] < bits[opt_index]) {
00894                 opt_index = i;
00895                 opt_order = order;
00896             }
00897         }
00898         opt_order++;
00899     } else if(omethod == ORDER_METHOD_SEARCH) {
00900         // brute-force optimal order search
00901         uint32_t bits[MAX_LPC_ORDER];
00902         opt_order = 0;
00903         bits[0] = UINT32_MAX;
00904         for(i=min_order-1; i<max_order; i++) {
00905             encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00906             bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
00907                                            res, n, i+1, sub->obits, precision);
00908             if(bits[i] < bits[opt_order]) {
00909                 opt_order = i;
00910             }
00911         }
00912         opt_order++;
00913     } else if(omethod == ORDER_METHOD_LOG) {
00914         uint32_t bits[MAX_LPC_ORDER];
00915         int step;
00916 
00917         opt_order= min_order - 1 + (max_order-min_order)/3;
00918         memset(bits, -1, sizeof(bits));
00919 
00920         for(step=16 ;step; step>>=1){
00921             int last= opt_order;
00922             for(i=last-step; i<=last+step; i+= step){
00923                 if(i<min_order-1 || i>=max_order || bits[i] < UINT32_MAX)
00924                     continue;
00925                 encode_residual_lpc(res, smp, n, i+1, coefs[i], shift[i]);
00926                 bits[i] = calc_rice_params_lpc(&sub->rc, min_porder, max_porder,
00927                                             res, n, i+1, sub->obits, precision);
00928                 if(bits[i] < bits[opt_order])
00929                     opt_order= i;
00930             }
00931         }
00932         opt_order++;
00933     }
00934 
00935     sub->order = opt_order;
00936     sub->type = FLAC_SUBFRAME_LPC;
00937     sub->type_code = sub->type | (sub->order-1);
00938     sub->shift = shift[sub->order-1];
00939     for(i=0; i<sub->order; i++) {
00940         sub->coefs[i] = coefs[sub->order-1][i];
00941     }
00942     encode_residual_lpc(res, smp, n, sub->order, sub->coefs, sub->shift);
00943     return calc_rice_params_lpc(&sub->rc, min_porder, max_porder, res, n, sub->order,
00944                                 sub->obits, precision);
00945 }
00946 
00947 static int encode_residual_v(FlacEncodeContext *ctx, int ch)
00948 {
00949     int i, n;
00950     FlacFrame *frame;
00951     FlacSubframe *sub;
00952     int32_t *res, *smp;
00953 
00954     frame = &ctx->frame;
00955     sub = &frame->subframes[ch];
00956     res = sub->residual;
00957     smp = sub->samples;
00958     n = frame->blocksize;
00959 
00960     /* CONSTANT */
00961     for(i=1; i<n; i++) {
00962         if(smp[i] != smp[0]) break;
00963     }
00964     if(i == n) {
00965         sub->type = sub->type_code = FLAC_SUBFRAME_CONSTANT;
00966         res[0] = smp[0];
00967         return sub->obits;
00968     }
00969 
00970     /* VERBATIM */
00971     sub->type = sub->type_code = FLAC_SUBFRAME_VERBATIM;
00972     encode_residual_verbatim(res, smp, n);
00973     return sub->obits * n;
00974 }
00975 
00976 static int estimate_stereo_mode(int32_t *left_ch, int32_t *right_ch, int n)
00977 {
00978     int i, best;
00979     int32_t lt, rt;
00980     uint64_t sum[4];
00981     uint64_t score[4];
00982     int k;
00983 
00984     /* calculate sum of 2nd order residual for each channel */
00985     sum[0] = sum[1] = sum[2] = sum[3] = 0;
00986     for(i=2; i<n; i++) {
00987         lt = left_ch[i] - 2*left_ch[i-1] + left_ch[i-2];
00988         rt = right_ch[i] - 2*right_ch[i-1] + right_ch[i-2];
00989         sum[2] += FFABS((lt + rt) >> 1);
00990         sum[3] += FFABS(lt - rt);
00991         sum[0] += FFABS(lt);
00992         sum[1] += FFABS(rt);
00993     }
00994     /* estimate bit counts */
00995     for(i=0; i<4; i++) {
00996         k = find_optimal_param(2*sum[i], n);
00997         sum[i] = rice_encode_count(2*sum[i], n, k);
00998     }
00999 
01000     /* calculate score for each mode */
01001     score[0] = sum[0] + sum[1];
01002     score[1] = sum[0] + sum[3];
01003     score[2] = sum[1] + sum[3];
01004     score[3] = sum[2] + sum[3];
01005 
01006     /* return mode with lowest score */
01007     best = 0;
01008     for(i=1; i<4; i++) {
01009         if(score[i] < score[best]) {
01010             best = i;
01011         }
01012     }
01013     if(best == 0) {
01014         return FLAC_CHMODE_LEFT_RIGHT;
01015     } else if(best == 1) {
01016         return FLAC_CHMODE_LEFT_SIDE;
01017     } else if(best == 2) {
01018         return FLAC_CHMODE_RIGHT_SIDE;
01019     } else {
01020         return FLAC_CHMODE_MID_SIDE;
01021     }
01022 }
01023 
01027 static void channel_decorrelation(FlacEncodeContext *ctx)
01028 {
01029     FlacFrame *frame;
01030     int32_t *left, *right;
01031     int i, n;
01032 
01033     frame = &ctx->frame;
01034     n = frame->blocksize;
01035     left  = frame->subframes[0].samples;
01036     right = frame->subframes[1].samples;
01037 
01038     if(ctx->channels != 2) {
01039         frame->ch_mode = FLAC_CHMODE_NOT_STEREO;
01040         return;
01041     }
01042 
01043     frame->ch_mode = estimate_stereo_mode(left, right, n);
01044 
01045     /* perform decorrelation and adjust bits-per-sample */
01046     if(frame->ch_mode == FLAC_CHMODE_LEFT_RIGHT) {
01047         return;
01048     }
01049     if(frame->ch_mode == FLAC_CHMODE_MID_SIDE) {
01050         int32_t tmp;
01051         for(i=0; i<n; i++) {
01052             tmp = left[i];
01053             left[i] = (tmp + right[i]) >> 1;
01054             right[i] = tmp - right[i];
01055         }
01056         frame->subframes[1].obits++;
01057     } else if(frame->ch_mode == FLAC_CHMODE_LEFT_SIDE) {
01058         for(i=0; i<n; i++) {
01059             right[i] = left[i] - right[i];
01060         }
01061         frame->subframes[1].obits++;
01062     } else {
01063         for(i=0; i<n; i++) {
01064             left[i] -= right[i];
01065         }
01066         frame->subframes[0].obits++;
01067     }
01068 }
01069 
01070 static void write_utf8(PutBitContext *pb, uint32_t val)
01071 {
01072     uint8_t tmp;
01073     PUT_UTF8(val, tmp, put_bits(pb, 8, tmp);)
01074 }
01075 
01076 static void output_frame_header(FlacEncodeContext *s)
01077 {
01078     FlacFrame *frame;
01079     int crc;
01080 
01081     frame = &s->frame;
01082 
01083     put_bits(&s->pb, 16, 0xFFF8);
01084     put_bits(&s->pb, 4, frame->bs_code[0]);
01085     put_bits(&s->pb, 4, s->sr_code[0]);
01086     if(frame->ch_mode == FLAC_CHMODE_NOT_STEREO) {
01087         put_bits(&s->pb, 4, s->ch_code);
01088     } else {
01089         put_bits(&s->pb, 4, frame->ch_mode);
01090     }
01091     put_bits(&s->pb, 3, 4); /* bits-per-sample code */
01092     put_bits(&s->pb, 1, 0);
01093     write_utf8(&s->pb, s->frame_count);
01094     if(frame->bs_code[0] == 6) {
01095         put_bits(&s->pb, 8, frame->bs_code[1]);
01096     } else if(frame->bs_code[0] == 7) {
01097         put_bits(&s->pb, 16, frame->bs_code[1]);
01098     }
01099     if(s->sr_code[0] == 12) {
01100         put_bits(&s->pb, 8, s->sr_code[1]);
01101     } else if(s->sr_code[0] > 12) {
01102         put_bits(&s->pb, 16, s->sr_code[1]);
01103     }
01104     flush_put_bits(&s->pb);
01105     crc = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
01106                  s->pb.buf, put_bits_count(&s->pb)>>3);
01107     put_bits(&s->pb, 8, crc);
01108 }
01109 
01110 static void output_subframe_constant(FlacEncodeContext *s, int ch)
01111 {
01112     FlacSubframe *sub;
01113     int32_t res;
01114 
01115     sub = &s->frame.subframes[ch];
01116     res = sub->residual[0];
01117     put_sbits(&s->pb, sub->obits, res);
01118 }
01119 
01120 static void output_subframe_verbatim(FlacEncodeContext *s, int ch)
01121 {
01122     int i;
01123     FlacFrame *frame;
01124     FlacSubframe *sub;
01125     int32_t res;
01126 
01127     frame = &s->frame;
01128     sub = &frame->subframes[ch];
01129 
01130     for(i=0; i<frame->blocksize; i++) {
01131         res = sub->residual[i];
01132         put_sbits(&s->pb, sub->obits, res);
01133     }
01134 }
01135 
01136 static void output_residual(FlacEncodeContext *ctx, int ch)
01137 {
01138     int i, j, p, n, parts;
01139     int k, porder, psize, res_cnt;
01140     FlacFrame *frame;
01141     FlacSubframe *sub;
01142     int32_t *res;
01143 
01144     frame = &ctx->frame;
01145     sub = &frame->subframes[ch];
01146     res = sub->residual;
01147     n = frame->blocksize;
01148 
01149     /* rice-encoded block */
01150     put_bits(&ctx->pb, 2, 0);
01151 
01152     /* partition order */
01153     porder = sub->rc.porder;
01154     psize = n >> porder;
01155     parts = (1 << porder);
01156     put_bits(&ctx->pb, 4, porder);
01157     res_cnt = psize - sub->order;
01158 
01159     /* residual */
01160     j = sub->order;
01161     for(p=0; p<parts; p++) {
01162         k = sub->rc.params[p];
01163         put_bits(&ctx->pb, 4, k);
01164         if(p == 1) res_cnt = psize;
01165         for(i=0; i<res_cnt && j<n; i++, j++) {
01166             set_sr_golomb_flac(&ctx->pb, res[j], k, INT32_MAX, 0);
01167         }
01168     }
01169 }
01170 
01171 static void output_subframe_fixed(FlacEncodeContext *ctx, int ch)
01172 {
01173     int i;
01174     FlacFrame *frame;
01175     FlacSubframe *sub;
01176 
01177     frame = &ctx->frame;
01178     sub = &frame->subframes[ch];
01179 
01180     /* warm-up samples */
01181     for(i=0; i<sub->order; i++) {
01182         put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
01183     }
01184 
01185     /* residual */
01186     output_residual(ctx, ch);
01187 }
01188 
01189 static void output_subframe_lpc(FlacEncodeContext *ctx, int ch)
01190 {
01191     int i, cbits;
01192     FlacFrame *frame;
01193     FlacSubframe *sub;
01194 
01195     frame = &ctx->frame;
01196     sub = &frame->subframes[ch];
01197 
01198     /* warm-up samples */
01199     for(i=0; i<sub->order; i++) {
01200         put_sbits(&ctx->pb, sub->obits, sub->residual[i]);
01201     }
01202 
01203     /* LPC coefficients */
01204     cbits = ctx->options.lpc_coeff_precision;
01205     put_bits(&ctx->pb, 4, cbits-1);
01206     put_sbits(&ctx->pb, 5, sub->shift);
01207     for(i=0; i<sub->order; i++) {
01208         put_sbits(&ctx->pb, cbits, sub->coefs[i]);
01209     }
01210 
01211     /* residual */
01212     output_residual(ctx, ch);
01213 }
01214 
01215 static void output_subframes(FlacEncodeContext *s)
01216 {
01217     FlacFrame *frame;
01218     FlacSubframe *sub;
01219     int ch;
01220 
01221     frame = &s->frame;
01222 
01223     for(ch=0; ch<s->channels; ch++) {
01224         sub = &frame->subframes[ch];
01225 
01226         /* subframe header */
01227         put_bits(&s->pb, 1, 0);
01228         put_bits(&s->pb, 6, sub->type_code);
01229         put_bits(&s->pb, 1, 0); /* no wasted bits */
01230 
01231         /* subframe */
01232         if(sub->type == FLAC_SUBFRAME_CONSTANT) {
01233             output_subframe_constant(s, ch);
01234         } else if(sub->type == FLAC_SUBFRAME_VERBATIM) {
01235             output_subframe_verbatim(s, ch);
01236         } else if(sub->type == FLAC_SUBFRAME_FIXED) {
01237             output_subframe_fixed(s, ch);
01238         } else if(sub->type == FLAC_SUBFRAME_LPC) {
01239             output_subframe_lpc(s, ch);
01240         }
01241     }
01242 }
01243 
01244 static void output_frame_footer(FlacEncodeContext *s)
01245 {
01246     int crc;
01247     flush_put_bits(&s->pb);
01248     crc = bswap_16(av_crc(av_crc_get_table(AV_CRC_16_ANSI), 0,
01249                           s->pb.buf, put_bits_count(&s->pb)>>3));
01250     put_bits(&s->pb, 16, crc);
01251     flush_put_bits(&s->pb);
01252 }
01253 
01254 static void update_md5_sum(FlacEncodeContext *s, int16_t *samples)
01255 {
01256 #ifdef WORDS_BIGENDIAN
01257     int i;
01258     for(i = 0; i < s->frame.blocksize*s->channels; i++) {
01259         int16_t smp = le2me_16(samples[i]);
01260         av_md5_update(s->md5ctx, (uint8_t *)&smp, 2);
01261     }
01262 #else
01263     av_md5_update(s->md5ctx, (uint8_t *)samples, s->frame.blocksize*s->channels*2);
01264 #endif
01265 }
01266 
01267 static int flac_encode_frame(AVCodecContext *avctx, uint8_t *frame,
01268                              int buf_size, void *data)
01269 {
01270     int ch;
01271     FlacEncodeContext *s;
01272     int16_t *samples = data;
01273     int out_bytes;
01274     int reencoded=0;
01275 
01276     s = avctx->priv_data;
01277 
01278     if(buf_size < s->max_framesize*2) {
01279         av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
01280         return 0;
01281     }
01282 
01283     /* when the last block is reached, update the header in extradata */
01284     if (!data) {
01285         s->min_framesize = s->min_encoded_framesize;
01286         s->max_framesize = s->max_encoded_framesize;
01287         av_md5_final(s->md5ctx, s->md5sum);
01288         write_streaminfo(s, avctx->extradata);
01289         return 0;
01290     }
01291 
01292     init_frame(s);
01293 
01294     copy_samples(s, samples);
01295 
01296     channel_decorrelation(s);
01297 
01298     for(ch=0; ch<s->channels; ch++) {
01299         encode_residual(s, ch);
01300     }
01301 
01302 write_frame:
01303     init_put_bits(&s->pb, frame, buf_size);
01304     output_frame_header(s);
01305     output_subframes(s);
01306     output_frame_footer(s);
01307     out_bytes = put_bits_count(&s->pb) >> 3;
01308 
01309     if(out_bytes > s->max_framesize) {
01310         if(reencoded) {
01311             /* still too large. must be an error. */
01312             av_log(avctx, AV_LOG_ERROR, "error encoding frame\n");
01313             return -1;
01314         }
01315 
01316         /* frame too large. use verbatim mode */
01317         for(ch=0; ch<s->channels; ch++) {
01318             encode_residual_v(s, ch);
01319         }
01320         reencoded = 1;
01321         goto write_frame;
01322     }
01323 
01324     s->frame_count++;
01325     s->sample_count += avctx->frame_size;
01326     update_md5_sum(s, samples);
01327     if (out_bytes > s->max_encoded_framesize)
01328         s->max_encoded_framesize = out_bytes;
01329     if (out_bytes < s->min_encoded_framesize)
01330         s->min_encoded_framesize = out_bytes;
01331 
01332     return out_bytes;
01333 }
01334 
01335 static av_cold int flac_encode_close(AVCodecContext *avctx)
01336 {
01337     if (avctx->priv_data) {
01338         FlacEncodeContext *s = avctx->priv_data;
01339         av_freep(&s->md5ctx);
01340     }
01341     av_freep(&avctx->extradata);
01342     avctx->extradata_size = 0;
01343     av_freep(&avctx->coded_frame);
01344     return 0;
01345 }
01346 
01347 AVCodec flac_encoder = {
01348     "flac",
01349     CODEC_TYPE_AUDIO,
01350     CODEC_ID_FLAC,
01351     sizeof(FlacEncodeContext),
01352     flac_encode_init,
01353     flac_encode_frame,
01354     flac_encode_close,
01355     NULL,
01356     .capabilities = CODEC_CAP_SMALL_LAST_FRAME | CODEC_CAP_DELAY,
01357     .sample_fmts = (enum SampleFormat[]){SAMPLE_FMT_S16,SAMPLE_FMT_NONE},
01358     .long_name = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
01359 };

Generated on Tue Nov 4 2014 12:59:21 for ffmpeg by  doxygen 1.7.1