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