LCOV - code coverage report
Current view: top level - libavcodec - mlpenc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1185 0.0 %
Date: 2017-12-15 18:13:28 Functions: 0 50 0.0 %

          Line data    Source code
       1             : /**
       2             :  * MLP encoder
       3             :  * Copyright (c) 2008 Ramiro Polla
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  */
      21             : 
      22             : #include "avcodec.h"
      23             : #include "internal.h"
      24             : #include "put_bits.h"
      25             : #include "audio_frame_queue.h"
      26             : #include "libavutil/crc.h"
      27             : #include "libavutil/avstring.h"
      28             : #include "libavutil/samplefmt.h"
      29             : #include "mlp.h"
      30             : #include "lpc.h"
      31             : 
      32             : #define MAJOR_HEADER_INTERVAL 16
      33             : 
      34             : #define MLP_MIN_LPC_ORDER      1
      35             : #define MLP_MAX_LPC_ORDER      8
      36             : #define MLP_MIN_LPC_SHIFT      8
      37             : #define MLP_MAX_LPC_SHIFT     15
      38             : 
      39             : typedef struct {
      40             :     uint8_t         min_channel;         ///< The index of the first channel coded in this substream.
      41             :     uint8_t         max_channel;         ///< The index of the last channel coded in this substream.
      42             :     uint8_t         max_matrix_channel;  ///< The number of channels input into the rematrix stage.
      43             : 
      44             :     uint8_t         noise_shift;         ///< The left shift applied to random noise in 0x31ea substreams.
      45             :     uint32_t        noisegen_seed;       ///< The current seed value for the pseudorandom noise generator(s).
      46             : 
      47             :     int             data_check_present;  ///< Set if the substream contains extra info to check the size of VLC blocks.
      48             : 
      49             :     int32_t         lossless_check_data; ///< XOR of all output samples
      50             : 
      51             :     uint8_t         max_huff_lsbs;       ///< largest huff_lsbs
      52             :     uint8_t         max_output_bits;     ///< largest output bit-depth
      53             : } RestartHeader;
      54             : 
      55             : typedef struct {
      56             :     uint8_t         count;                  ///< number of matrices to apply
      57             : 
      58             :     uint8_t         outch[MAX_MATRICES];    ///< output channel for each matrix
      59             :     int32_t         forco[MAX_MATRICES][MAX_CHANNELS+2];    ///< forward coefficients
      60             :     int32_t         coeff[MAX_MATRICES][MAX_CHANNELS+2];    ///< decoding coefficients
      61             :     uint8_t         fbits[MAX_CHANNELS];    ///< fraction bits
      62             : 
      63             :     int8_t          shift[MAX_CHANNELS];    ///< Left shift to apply to decoded PCM values to get final 24-bit output.
      64             : } MatrixParams;
      65             : 
      66             : enum ParamFlags {
      67             :     PARAMS_DEFAULT       = 0xff,
      68             :     PARAM_PRESENCE_FLAGS = 1 << 8,
      69             :     PARAM_BLOCKSIZE      = 1 << 7,
      70             :     PARAM_MATRIX         = 1 << 6,
      71             :     PARAM_OUTSHIFT       = 1 << 5,
      72             :     PARAM_QUANTSTEP      = 1 << 4,
      73             :     PARAM_FIR            = 1 << 3,
      74             :     PARAM_IIR            = 1 << 2,
      75             :     PARAM_HUFFOFFSET     = 1 << 1,
      76             :     PARAM_PRESENT        = 1 << 0,
      77             : };
      78             : 
      79             : typedef struct {
      80             :     uint16_t        blocksize;                  ///< number of PCM samples in current audio block
      81             :     uint8_t         quant_step_size[MAX_CHANNELS];  ///< left shift to apply to Huffman-decoded residuals
      82             : 
      83             :     MatrixParams    matrix_params;
      84             : 
      85             :     uint8_t         param_presence_flags;       ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
      86             : } DecodingParams;
      87             : 
      88             : typedef struct BestOffset {
      89             :     int16_t offset;
      90             :     int bitcount;
      91             :     int lsb_bits;
      92             :     int16_t min;
      93             :     int16_t max;
      94             : } BestOffset;
      95             : 
      96             : #define HUFF_OFFSET_MIN    -16384
      97             : #define HUFF_OFFSET_MAX     16383
      98             : 
      99             : /** Number of possible codebooks (counting "no codebooks") */
     100             : #define NUM_CODEBOOKS       4
     101             : 
     102             : typedef struct {
     103             :     AVCodecContext *avctx;
     104             : 
     105             :     int             num_substreams;         ///< Number of substreams contained within this stream.
     106             : 
     107             :     int             num_channels;   /**< Number of channels in major_scratch_buffer.
     108             :                                      *   Normal channels + noise channels. */
     109             : 
     110             :     int             coded_sample_fmt [2];   ///< sample format encoded for MLP
     111             :     int             coded_sample_rate[2];   ///< sample rate encoded for MLP
     112             :     int             coded_peak_bitrate;     ///< peak bitrate for this major sync header
     113             : 
     114             :     int             flags;                  ///< major sync info flags
     115             : 
     116             :     /* channel_meaning */
     117             :     int             substream_info;
     118             :     int             fs;
     119             :     int             wordlength;
     120             :     int             channel_occupancy;
     121             :     int             summary_info;
     122             : 
     123             :     int32_t        *inout_buffer;           ///< Pointer to data currently being read from lavc or written to bitstream.
     124             :     int32_t        *major_inout_buffer;     ///< Buffer with all in/out data for one entire major frame interval.
     125             :     int32_t        *write_buffer;           ///< Pointer to data currently being written to bitstream.
     126             :     int32_t        *sample_buffer;          ///< Pointer to current access unit samples.
     127             :     int32_t        *major_scratch_buffer;   ///< Scratch buffer big enough to fit all data for one entire major frame interval.
     128             :     int32_t        *last_frame;             ///< Pointer to last frame with data to encode.
     129             : 
     130             :     int32_t        *lpc_sample_buffer;
     131             : 
     132             :     unsigned int    major_number_of_frames;
     133             :     unsigned int    next_major_number_of_frames;
     134             : 
     135             :     unsigned int    major_frame_size;       ///< Number of samples in current major frame being encoded.
     136             :     unsigned int    next_major_frame_size;  ///< Counter of number of samples for next major frame.
     137             : 
     138             :     int32_t        *lossless_check_data;    ///< Array with lossless_check_data for each access unit.
     139             : 
     140             :     unsigned int   *max_output_bits;        ///< largest output bit-depth
     141             :     unsigned int   *frame_size;             ///< Array with number of samples/channel in each access unit.
     142             :     unsigned int    frame_index;            ///< Index of current frame being encoded.
     143             : 
     144             :     unsigned int    one_sample_buffer_size; ///< Number of samples*channel for one access unit.
     145             : 
     146             :     unsigned int    max_restart_interval;   ///< Max interval of access units in between two major frames.
     147             :     unsigned int    min_restart_interval;   ///< Min interval of access units in between two major frames.
     148             :     unsigned int    restart_intervals;      ///< Number of possible major frame sizes.
     149             : 
     150             :     uint16_t        timestamp;              ///< Timestamp of current access unit.
     151             :     uint16_t        dts;                    ///< Decoding timestamp of current access unit.
     152             : 
     153             :     uint8_t         channel_arrangement;    ///< channel arrangement for MLP streams
     154             : 
     155             :     uint8_t         ch_modifier_thd0;       ///< channel modifier for TrueHD stream 0
     156             :     uint8_t         ch_modifier_thd1;       ///< channel modifier for TrueHD stream 1
     157             :     uint8_t         ch_modifier_thd2;       ///< channel modifier for TrueHD stream 2
     158             : 
     159             :     unsigned int    seq_size  [MAJOR_HEADER_INTERVAL];
     160             :     unsigned int    seq_offset[MAJOR_HEADER_INTERVAL];
     161             :     unsigned int    sequence_size;
     162             : 
     163             :     ChannelParams  *channel_params;
     164             : 
     165             :     BestOffset      best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
     166             : 
     167             :     DecodingParams *decoding_params;
     168             :     RestartHeader   restart_header [MAX_SUBSTREAMS];
     169             : 
     170             :     ChannelParams   major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS];       ///< ChannelParams to be written to bitstream.
     171             :     DecodingParams  major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS];    ///< DecodingParams to be written to bitstream.
     172             :     int             major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS];     ///< params_changed to be written to bitstream.
     173             : 
     174             :     unsigned int    major_cur_subblock_index;
     175             :     unsigned int    major_filter_state_subblock;
     176             :     unsigned int    major_number_of_subblocks;
     177             : 
     178             :     BestOffset    (*cur_best_offset)[NUM_CODEBOOKS];
     179             :     ChannelParams  *cur_channel_params;
     180             :     DecodingParams *cur_decoding_params;
     181             :     RestartHeader  *cur_restart_header;
     182             : 
     183             :     AudioFrameQueue afq;
     184             : 
     185             :     /* Analysis stage. */
     186             :     unsigned int    starting_frame_index;
     187             :     unsigned int    number_of_frames;
     188             :     unsigned int    number_of_samples;
     189             :     unsigned int    number_of_subblocks;
     190             :     unsigned int    seq_index;              ///< Sequence index for high compression levels.
     191             : 
     192             :     ChannelParams  *prev_channel_params;
     193             :     DecodingParams *prev_decoding_params;
     194             : 
     195             :     ChannelParams  *seq_channel_params;
     196             :     DecodingParams *seq_decoding_params;
     197             : 
     198             :     unsigned int    max_codebook_search;
     199             : 
     200             :     LPCContext      lpc_ctx;
     201             : } MLPEncodeContext;
     202             : 
     203             : static ChannelParams   restart_channel_params[MAX_CHANNELS];
     204             : static DecodingParams  restart_decoding_params[MAX_SUBSTREAMS];
     205             : static BestOffset      restart_best_offset[NUM_CODEBOOKS] = {{0}};
     206             : 
     207             : #define SYNC_MAJOR      0xf8726f
     208             : #define MAJOR_SYNC_INFO_SIGNATURE   0xB752
     209             : 
     210             : #define SYNC_MLP        0xbb
     211             : #define SYNC_TRUEHD     0xba
     212             : 
     213             : /* must be set for DVD-A */
     214             : #define FLAGS_DVDA      0x4000
     215             : /* FIFO delay must be constant */
     216             : #define FLAGS_CONST     0x8000
     217             : 
     218             : #define SUBSTREAM_INFO_MAX_2_CHAN   0x01
     219             : #define SUBSTREAM_INFO_HIGH_RATE    0x02
     220             : #define SUBSTREAM_INFO_ALWAYS_SET   0x04
     221             : #define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
     222             : 
     223             : /****************************************************************************
     224             :  ************ Functions that copy, clear, or compare parameters *************
     225             :  ****************************************************************************/
     226             : 
     227             : /** Compares two FilterParams structures and returns 1 if anything has
     228             :  *  changed. Returns 0 if they are both equal.
     229             :  */
     230           0 : static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
     231             : {
     232           0 :     const FilterParams *prev = &prev_cp->filter_params[filter];
     233           0 :     const FilterParams *fp = &cp->filter_params[filter];
     234             :     int i;
     235             : 
     236           0 :     if (prev->order != fp->order)
     237           0 :         return 1;
     238             : 
     239           0 :     if (!prev->order)
     240           0 :         return 0;
     241             : 
     242           0 :     if (prev->shift != fp->shift)
     243           0 :         return 1;
     244             : 
     245           0 :     for (i = 0; i < fp->order; i++)
     246           0 :         if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
     247           0 :             return 1;
     248             : 
     249           0 :     return 0;
     250             : }
     251             : 
     252             : /** Compare two primitive matrices and returns 1 if anything has changed.
     253             :  *  Returns 0 if they are both equal.
     254             :  */
     255           0 : static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
     256             : {
     257           0 :     RestartHeader *rh = ctx->cur_restart_header;
     258             :     unsigned int channel, mat;
     259             : 
     260           0 :     if (prev->count != mp->count)
     261           0 :         return 1;
     262             : 
     263           0 :     if (!prev->count)
     264           0 :         return 0;
     265             : 
     266           0 :     for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
     267           0 :         if (prev->fbits[channel] != mp->fbits[channel])
     268           0 :             return 1;
     269             : 
     270           0 :     for (mat = 0; mat < mp->count; mat++) {
     271           0 :         if (prev->outch[mat] != mp->outch[mat])
     272           0 :             return 1;
     273             : 
     274           0 :         for (channel = 0; channel < ctx->num_channels; channel++)
     275           0 :             if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
     276           0 :                 return 1;
     277             :     }
     278             : 
     279           0 :     return 0;
     280             : }
     281             : 
     282             : /** Compares two DecodingParams and ChannelParams structures to decide if a
     283             :  *  new decoding params header has to be written.
     284             :  */
     285           0 : static int compare_decoding_params(MLPEncodeContext *ctx)
     286             : {
     287           0 :     DecodingParams *prev = ctx->prev_decoding_params;
     288           0 :     DecodingParams *dp = ctx->cur_decoding_params;
     289           0 :     MatrixParams *prev_mp = &prev->matrix_params;
     290           0 :     MatrixParams *mp = &dp->matrix_params;
     291           0 :     RestartHeader  *rh = ctx->cur_restart_header;
     292             :     unsigned int ch;
     293           0 :     int retval = 0;
     294             : 
     295           0 :     if (prev->param_presence_flags != dp->param_presence_flags)
     296           0 :         retval |= PARAM_PRESENCE_FLAGS;
     297             : 
     298           0 :     if (prev->blocksize != dp->blocksize)
     299           0 :         retval |= PARAM_BLOCKSIZE;
     300             : 
     301           0 :     if (compare_matrix_params(ctx, prev_mp, mp))
     302           0 :         retval |= PARAM_MATRIX;
     303             : 
     304           0 :     for (ch = 0; ch <= rh->max_matrix_channel; ch++)
     305           0 :         if (prev_mp->shift[ch] != mp->shift[ch]) {
     306           0 :             retval |= PARAM_OUTSHIFT;
     307           0 :             break;
     308             :         }
     309             : 
     310           0 :     for (ch = 0; ch <= rh->max_channel; ch++)
     311           0 :         if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
     312           0 :             retval |= PARAM_QUANTSTEP;
     313           0 :             break;
     314             :         }
     315             : 
     316           0 :     for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
     317           0 :         ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
     318           0 :         ChannelParams *cp = &ctx->cur_channel_params[ch];
     319             : 
     320           0 :         if (!(retval & PARAM_FIR) &&
     321           0 :             compare_filter_params(prev_cp, cp, FIR))
     322           0 :             retval |= PARAM_FIR;
     323             : 
     324           0 :         if (!(retval & PARAM_IIR) &&
     325           0 :             compare_filter_params(prev_cp, cp, IIR))
     326           0 :             retval |= PARAM_IIR;
     327             : 
     328           0 :         if (prev_cp->huff_offset != cp->huff_offset)
     329           0 :             retval |= PARAM_HUFFOFFSET;
     330             : 
     331           0 :         if (prev_cp->codebook    != cp->codebook  ||
     332           0 :             prev_cp->huff_lsbs   != cp->huff_lsbs  )
     333           0 :             retval |= 0x1;
     334             :     }
     335             : 
     336           0 :     return retval;
     337             : }
     338             : 
     339           0 : static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
     340             : {
     341           0 :     FilterParams *dst = &dst_cp->filter_params[filter];
     342           0 :     FilterParams *src = &src_cp->filter_params[filter];
     343             :     unsigned int order;
     344             : 
     345           0 :     dst->order = src->order;
     346             : 
     347           0 :     if (dst->order) {
     348           0 :         dst->shift = src->shift;
     349             : 
     350           0 :         dst->coeff_shift = src->coeff_shift;
     351           0 :         dst->coeff_bits = src->coeff_bits;
     352             :     }
     353             : 
     354           0 :     for (order = 0; order < dst->order; order++)
     355           0 :         dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
     356           0 : }
     357             : 
     358           0 : static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
     359             : {
     360           0 :     dst->count = src->count;
     361             : 
     362           0 :     if (dst->count) {
     363             :         unsigned int channel, count;
     364             : 
     365           0 :         for (channel = 0; channel < MAX_CHANNELS; channel++) {
     366             : 
     367           0 :             dst->fbits[channel] = src->fbits[channel];
     368           0 :             dst->shift[channel] = src->shift[channel];
     369             : 
     370           0 :             for (count = 0; count < MAX_MATRICES; count++)
     371           0 :                 dst->coeff[count][channel] = src->coeff[count][channel];
     372             :         }
     373             : 
     374           0 :         for (count = 0; count < MAX_MATRICES; count++)
     375           0 :             dst->outch[count] = src->outch[count];
     376             :     }
     377           0 : }
     378             : 
     379           0 : static void copy_restart_frame_params(MLPEncodeContext *ctx,
     380             :                                       unsigned int substr)
     381             : {
     382             :     unsigned int index;
     383             : 
     384           0 :     for (index = 0; index < ctx->number_of_subblocks; index++) {
     385           0 :         DecodingParams *dp = ctx->seq_decoding_params + index*(ctx->num_substreams) + substr;
     386             :         unsigned int channel;
     387             : 
     388           0 :         copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
     389             : 
     390           0 :         for (channel = 0; channel < ctx->avctx->channels; channel++) {
     391           0 :             ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
     392             :             unsigned int filter;
     393             : 
     394           0 :             dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
     395           0 :             dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
     396             : 
     397           0 :             if (index)
     398           0 :                 for (filter = 0; filter < NUM_FILTERS; filter++)
     399           0 :                     copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
     400             :         }
     401             :     }
     402           0 : }
     403             : 
     404             : /** Clears a DecodingParams struct the way it should be after a restart header. */
     405           0 : static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
     406             : {
     407             :     unsigned int substr;
     408             : 
     409           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
     410           0 :         DecodingParams *dp = &decoding_params[substr];
     411             : 
     412           0 :         dp->param_presence_flags   = 0xff;
     413           0 :         dp->blocksize              = 8;
     414             : 
     415           0 :         memset(&dp->matrix_params , 0, sizeof(MatrixParams       ));
     416           0 :         memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
     417             :     }
     418           0 : }
     419             : 
     420             : /** Clears a ChannelParams struct the way it should be after a restart header. */
     421           0 : static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
     422             : {
     423             :     unsigned int channel;
     424             : 
     425           0 :     for (channel = 0; channel < ctx->avctx->channels; channel++) {
     426           0 :         ChannelParams *cp = &channel_params[channel];
     427             : 
     428           0 :         memset(&cp->filter_params, 0, sizeof(cp->filter_params));
     429             : 
     430             :         /* Default audio coding is 24-bit raw PCM. */
     431           0 :         cp->huff_offset      =  0;
     432           0 :         cp->codebook         =  0;
     433           0 :         cp->huff_lsbs        = 24;
     434             :     }
     435           0 : }
     436             : 
     437             : /** Sets default vales in our encoder for a DecodingParams struct. */
     438           0 : static void default_decoding_params(MLPEncodeContext *ctx,
     439             :      DecodingParams decoding_params[MAX_SUBSTREAMS])
     440             : {
     441             :     unsigned int substr;
     442             : 
     443           0 :     clear_decoding_params(ctx, decoding_params);
     444             : 
     445           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
     446           0 :         DecodingParams *dp = &decoding_params[substr];
     447           0 :         uint8_t param_presence_flags = 0;
     448             : 
     449           0 :         param_presence_flags |= PARAM_BLOCKSIZE;
     450           0 :         param_presence_flags |= PARAM_MATRIX;
     451           0 :         param_presence_flags |= PARAM_OUTSHIFT;
     452           0 :         param_presence_flags |= PARAM_QUANTSTEP;
     453           0 :         param_presence_flags |= PARAM_FIR;
     454             : /*      param_presence_flags |= PARAM_IIR; */
     455           0 :         param_presence_flags |= PARAM_HUFFOFFSET;
     456           0 :         param_presence_flags |= PARAM_PRESENT;
     457             : 
     458           0 :         dp->param_presence_flags = param_presence_flags;
     459             :     }
     460           0 : }
     461             : 
     462             : /****************************************************************************/
     463             : 
     464             : /** Calculates the smallest number of bits it takes to encode a given signed
     465             :  *  value in two's complement.
     466             :  */
     467           0 : static int inline number_sbits(int number)
     468             : {
     469           0 :     if (number < 0)
     470           0 :         number++;
     471             : 
     472           0 :     return av_log2(FFABS(number)) + 1 + !!number;
     473             : }
     474             : 
     475             : enum InputBitDepth {
     476             :     BITS_16,
     477             :     BITS_20,
     478             :     BITS_24,
     479             : };
     480             : 
     481           0 : static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
     482             : {
     483           0 :     return ((peak_bitrate << 4) - 8) / sample_rate;
     484             : }
     485             : 
     486           0 : static av_cold int mlp_encode_init(AVCodecContext *avctx)
     487             : {
     488           0 :     MLPEncodeContext *ctx = avctx->priv_data;
     489             :     unsigned int substr, index;
     490           0 :     unsigned int sum = 0;
     491             :     unsigned int size;
     492             :     int ret;
     493             : 
     494           0 :     ctx->avctx = avctx;
     495             : 
     496           0 :     switch (avctx->sample_rate) {
     497           0 :     case 44100 << 0:
     498           0 :         avctx->frame_size         = 40  << 0;
     499           0 :         ctx->coded_sample_rate[0] = 0x08 + 0;
     500           0 :         ctx->fs                   = 0x08 + 1;
     501           0 :         break;
     502           0 :     case 44100 << 1:
     503           0 :         avctx->frame_size         = 40  << 1;
     504           0 :         ctx->coded_sample_rate[0] = 0x08 + 1;
     505           0 :         ctx->fs                   = 0x0C + 1;
     506           0 :         break;
     507           0 :     case 44100 << 2:
     508           0 :         ctx->substream_info      |= SUBSTREAM_INFO_HIGH_RATE;
     509           0 :         avctx->frame_size         = 40  << 2;
     510           0 :         ctx->coded_sample_rate[0] = 0x08 + 2;
     511           0 :         ctx->fs                   = 0x10 + 1;
     512           0 :         break;
     513           0 :     case 48000 << 0:
     514           0 :         avctx->frame_size         = 40  << 0;
     515           0 :         ctx->coded_sample_rate[0] = 0x00 + 0;
     516           0 :         ctx->fs                   = 0x08 + 2;
     517           0 :         break;
     518           0 :     case 48000 << 1:
     519           0 :         avctx->frame_size         = 40  << 1;
     520           0 :         ctx->coded_sample_rate[0] = 0x00 + 1;
     521           0 :         ctx->fs                   = 0x0C + 2;
     522           0 :         break;
     523           0 :     case 48000 << 2:
     524           0 :         ctx->substream_info      |= SUBSTREAM_INFO_HIGH_RATE;
     525           0 :         avctx->frame_size         = 40  << 2;
     526           0 :         ctx->coded_sample_rate[0] = 0x00 + 2;
     527           0 :         ctx->fs                   = 0x10 + 2;
     528           0 :         break;
     529           0 :     default:
     530           0 :         av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
     531             :                             "sample rates are 44100, 88200, 176400, 48000, "
     532             :                             "96000, and 192000.\n", avctx->sample_rate);
     533           0 :         return -1;
     534             :     }
     535           0 :     ctx->coded_sample_rate[1] = -1 & 0xf;
     536             : 
     537             :     /* TODO Keep count of bitrate and calculate real value. */
     538           0 :     ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
     539             : 
     540             :     /* TODO support more channels. */
     541           0 :     if (avctx->channels > 2) {
     542           0 :         av_log(avctx, AV_LOG_WARNING,
     543             :                "Only mono and stereo are supported at the moment.\n");
     544             :     }
     545             : 
     546           0 :     ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
     547           0 :     if (avctx->channels <= 2) {
     548           0 :         ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
     549             :     }
     550             : 
     551           0 :     switch (avctx->sample_fmt) {
     552           0 :     case AV_SAMPLE_FMT_S16:
     553           0 :         ctx->coded_sample_fmt[0] = BITS_16;
     554           0 :         ctx->wordlength = 16;
     555           0 :         avctx->bits_per_raw_sample = 16;
     556           0 :         break;
     557             :     /* TODO 20 bits: */
     558           0 :     case AV_SAMPLE_FMT_S32:
     559           0 :         ctx->coded_sample_fmt[0] = BITS_24;
     560           0 :         ctx->wordlength = 24;
     561           0 :         avctx->bits_per_raw_sample = 24;
     562           0 :         break;
     563           0 :     default:
     564           0 :         av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
     565             :                "Only 16- and 24-bit samples are supported.\n");
     566           0 :         return -1;
     567             :     }
     568           0 :     ctx->coded_sample_fmt[1] = -1 & 0xf;
     569             : 
     570           0 :     ctx->dts = -avctx->frame_size;
     571             : 
     572           0 :     ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
     573           0 :     ctx->one_sample_buffer_size = avctx->frame_size
     574           0 :                                 * ctx->num_channels;
     575             :     /* TODO Let user pass major header interval as parameter. */
     576           0 :     ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
     577             : 
     578           0 :     ctx->max_codebook_search = 3;
     579           0 :     ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
     580           0 :     ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
     581             : 
     582             :     /* TODO Let user pass parameters for LPC filter. */
     583             : 
     584           0 :     size = avctx->frame_size * ctx->max_restart_interval;
     585             : 
     586           0 :     ctx->lpc_sample_buffer = av_malloc_array(size, sizeof(int32_t));
     587           0 :     if (!ctx->lpc_sample_buffer) {
     588           0 :         av_log(avctx, AV_LOG_ERROR,
     589             :                "Not enough memory for buffering samples.\n");
     590           0 :         return AVERROR(ENOMEM);
     591             :     }
     592             : 
     593           0 :     size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
     594             : 
     595           0 :     ctx->major_scratch_buffer = av_malloc_array(size, sizeof(int32_t));
     596           0 :     if (!ctx->major_scratch_buffer) {
     597           0 :         av_log(avctx, AV_LOG_ERROR,
     598             :                "Not enough memory for buffering samples.\n");
     599           0 :         return AVERROR(ENOMEM);
     600             :     }
     601             : 
     602           0 :     ctx->major_inout_buffer = av_malloc_array(size, sizeof(int32_t));
     603           0 :     if (!ctx->major_inout_buffer) {
     604           0 :         av_log(avctx, AV_LOG_ERROR,
     605             :                "Not enough memory for buffering samples.\n");
     606           0 :         return AVERROR(ENOMEM);
     607             :     }
     608             : 
     609           0 :     ff_mlp_init_crc();
     610             : 
     611           0 :     ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
     612             : 
     613           0 :     if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
     614             :         /* MLP */
     615           0 :         switch(avctx->channel_layout) {
     616           0 :         case AV_CH_LAYOUT_MONO:
     617           0 :             ctx->channel_arrangement = 0; break;
     618           0 :         case AV_CH_LAYOUT_STEREO:
     619           0 :             ctx->channel_arrangement = 1; break;
     620           0 :         case AV_CH_LAYOUT_2_1:
     621           0 :             ctx->channel_arrangement = 2; break;
     622           0 :         case AV_CH_LAYOUT_QUAD:
     623           0 :             ctx->channel_arrangement = 3; break;
     624           0 :         case AV_CH_LAYOUT_2POINT1:
     625           0 :             ctx->channel_arrangement = 4; break;
     626           0 :         case AV_CH_LAYOUT_SURROUND:
     627           0 :             ctx->channel_arrangement = 7; break;
     628           0 :         case AV_CH_LAYOUT_4POINT0:
     629           0 :             ctx->channel_arrangement = 8; break;
     630           0 :         case AV_CH_LAYOUT_5POINT0_BACK:
     631           0 :             ctx->channel_arrangement = 9; break;
     632           0 :         case AV_CH_LAYOUT_3POINT1:
     633           0 :             ctx->channel_arrangement = 10; break;
     634           0 :         case AV_CH_LAYOUT_4POINT1:
     635           0 :             ctx->channel_arrangement = 11; break;
     636           0 :         case AV_CH_LAYOUT_5POINT1_BACK:
     637           0 :             ctx->channel_arrangement = 12; break;
     638           0 :         default:
     639           0 :             av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
     640           0 :             return -1;
     641             :         }
     642           0 :         ctx->flags = FLAGS_DVDA;
     643           0 :         ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
     644           0 :         ctx->summary_info      = ff_mlp_ch_info[ctx->channel_arrangement].summary_info     ;
     645             :     } else {
     646             :         /* TrueHD */
     647           0 :         switch(avctx->channel_layout) {
     648           0 :         case AV_CH_LAYOUT_STEREO:
     649           0 :             ctx->ch_modifier_thd0    = 0;
     650           0 :             ctx->ch_modifier_thd1    = 0;
     651           0 :             ctx->ch_modifier_thd2    = 0;
     652           0 :             ctx->channel_arrangement = 1;
     653           0 :             break;
     654           0 :         case AV_CH_LAYOUT_5POINT0_BACK:
     655           0 :             ctx->ch_modifier_thd0    = 1;
     656           0 :             ctx->ch_modifier_thd1    = 1;
     657           0 :             ctx->ch_modifier_thd2    = 1;
     658           0 :             ctx->channel_arrangement = 11;
     659           0 :             break;
     660           0 :         case AV_CH_LAYOUT_5POINT1_BACK:
     661           0 :             ctx->ch_modifier_thd0    = 2;
     662           0 :             ctx->ch_modifier_thd1    = 1;
     663           0 :             ctx->ch_modifier_thd2    = 2;
     664           0 :             ctx->channel_arrangement = 15;
     665           0 :             break;
     666           0 :         default:
     667           0 :             av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
     668           0 :             return -1;
     669             :         }
     670           0 :         ctx->flags = 0;
     671           0 :         ctx->channel_occupancy = 0;
     672           0 :         ctx->summary_info = 0;
     673             :     }
     674             : 
     675           0 :     size = sizeof(unsigned int) * ctx->max_restart_interval;
     676             : 
     677           0 :     ctx->frame_size = av_malloc(size);
     678           0 :     if (!ctx->frame_size)
     679           0 :         return AVERROR(ENOMEM);
     680             : 
     681           0 :     ctx->max_output_bits = av_malloc(size);
     682           0 :     if (!ctx->max_output_bits)
     683           0 :         return AVERROR(ENOMEM);
     684             : 
     685           0 :     size = sizeof(int32_t)
     686           0 :          * ctx->num_substreams * ctx->max_restart_interval;
     687             : 
     688           0 :     ctx->lossless_check_data = av_malloc(size);
     689           0 :     if (!ctx->lossless_check_data)
     690           0 :         return AVERROR(ENOMEM);
     691             : 
     692           0 :     for (index = 0; index < ctx->restart_intervals; index++) {
     693           0 :         ctx->seq_offset[index] = sum;
     694           0 :         ctx->seq_size  [index] = ((index + 1) * ctx->min_restart_interval) + 1;
     695           0 :         sum += ctx->seq_size[index];
     696             :     }
     697           0 :     ctx->sequence_size = sum;
     698           0 :     size = sizeof(ChannelParams)
     699           0 :          * ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
     700           0 :     ctx->channel_params = av_malloc(size);
     701           0 :     if (!ctx->channel_params) {
     702           0 :         av_log(avctx, AV_LOG_ERROR,
     703             :                "Not enough memory for analysis context.\n");
     704           0 :         return AVERROR(ENOMEM);
     705             :     }
     706             : 
     707           0 :     size = sizeof(DecodingParams)
     708           0 :          * ctx->restart_intervals * ctx->sequence_size * ctx->num_substreams;
     709           0 :     ctx->decoding_params = av_malloc(size);
     710           0 :     if (!ctx->decoding_params) {
     711           0 :         av_log(avctx, AV_LOG_ERROR,
     712             :                "Not enough memory for analysis context.\n");
     713           0 :         return AVERROR(ENOMEM);
     714             :     }
     715             : 
     716           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
     717           0 :         RestartHeader  *rh = &ctx->restart_header [substr];
     718             : 
     719             :         /* TODO see if noisegen_seed is really worth it. */
     720           0 :         rh->noisegen_seed      = 0;
     721             : 
     722           0 :         rh->min_channel        = 0;
     723           0 :         rh->max_channel        = avctx->channels - 1;
     724             :         /* FIXME: this works for 1 and 2 channels, but check for more */
     725           0 :         rh->max_matrix_channel = rh->max_channel;
     726             :     }
     727             : 
     728           0 :     clear_channel_params(ctx, restart_channel_params);
     729           0 :     clear_decoding_params(ctx, restart_decoding_params);
     730             : 
     731           0 :     if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
     732             :                     MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) {
     733           0 :         av_log(avctx, AV_LOG_ERROR,
     734             :                "Not enough memory for LPC context.\n");
     735           0 :         return ret;
     736             :     }
     737             : 
     738           0 :     ff_af_queue_init(avctx, &ctx->afq);
     739             : 
     740           0 :     return 0;
     741             : }
     742             : 
     743             : /****************************************************************************
     744             :  ****************** Functions that write to the bitstream *******************
     745             :  ****************************************************************************/
     746             : 
     747             : /** Writes a major sync header to the bitstream. */
     748           0 : static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
     749             : {
     750             :     PutBitContext pb;
     751             : 
     752           0 :     init_put_bits(&pb, buf, buf_size);
     753             : 
     754           0 :     put_bits(&pb, 24, SYNC_MAJOR               );
     755             : 
     756           0 :     if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
     757           0 :         put_bits(&pb,  8, SYNC_MLP                 );
     758           0 :         put_bits(&pb,  4, ctx->coded_sample_fmt [0]);
     759           0 :         put_bits(&pb,  4, ctx->coded_sample_fmt [1]);
     760           0 :         put_bits(&pb,  4, ctx->coded_sample_rate[0]);
     761           0 :         put_bits(&pb,  4, ctx->coded_sample_rate[1]);
     762           0 :         put_bits(&pb,  4, 0                        ); /* ignored */
     763           0 :         put_bits(&pb,  4, 0                        ); /* multi_channel_type */
     764           0 :         put_bits(&pb,  3, 0                        ); /* ignored */
     765           0 :         put_bits(&pb,  5, ctx->channel_arrangement );
     766           0 :     } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
     767           0 :         put_bits(&pb,  8, SYNC_TRUEHD              );
     768           0 :         put_bits(&pb,  4, ctx->coded_sample_rate[0]);
     769           0 :         put_bits(&pb,  4, 0                        ); /* ignored */
     770           0 :         put_bits(&pb,  2, ctx->ch_modifier_thd0    );
     771           0 :         put_bits(&pb,  2, ctx->ch_modifier_thd1    );
     772           0 :         put_bits(&pb,  5, ctx->channel_arrangement );
     773           0 :         put_bits(&pb,  2, ctx->ch_modifier_thd2    );
     774           0 :         put_bits(&pb, 13, ctx->channel_arrangement );
     775             :     }
     776             : 
     777           0 :     put_bits(&pb, 16, MAJOR_SYNC_INFO_SIGNATURE);
     778           0 :     put_bits(&pb, 16, ctx->flags               );
     779           0 :     put_bits(&pb, 16, 0                        ); /* ignored */
     780           0 :     put_bits(&pb,  1, 1                        ); /* is_vbr */
     781           0 :     put_bits(&pb, 15, ctx->coded_peak_bitrate  );
     782           0 :     put_bits(&pb,  4, 1                        ); /* num_substreams */
     783           0 :     put_bits(&pb,  4, 0x1                      ); /* ignored */
     784             : 
     785             :     /* channel_meaning */
     786           0 :     put_bits(&pb,  8, ctx->substream_info      );
     787           0 :     put_bits(&pb,  5, ctx->fs                  );
     788           0 :     put_bits(&pb,  5, ctx->wordlength          );
     789           0 :     put_bits(&pb,  6, ctx->channel_occupancy   );
     790           0 :     put_bits(&pb,  3, 0                        ); /* ignored */
     791           0 :     put_bits(&pb, 10, 0                        ); /* speaker_layout */
     792           0 :     put_bits(&pb,  3, 0                        ); /* copy_protection */
     793           0 :     put_bits(&pb, 16, 0x8080                   ); /* ignored */
     794           0 :     put_bits(&pb,  7, 0                        ); /* ignored */
     795           0 :     put_bits(&pb,  4, 0                        ); /* source_format */
     796           0 :     put_bits(&pb,  5, ctx->summary_info        );
     797             : 
     798           0 :     flush_put_bits(&pb);
     799             : 
     800           0 :     AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
     801           0 : }
     802             : 
     803             : /** Writes a restart header to the bitstream. Damaged streams can start being
     804             :  *  decoded losslessly again after such a header and the subsequent decoding
     805             :  *  params header.
     806             :  */
     807           0 : static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
     808             : {
     809           0 :     RestartHeader *rh = ctx->cur_restart_header;
     810           0 :     int32_t lossless_check = xor_32_to_8(rh->lossless_check_data);
     811           0 :     unsigned int start_count = put_bits_count(pb);
     812             :     PutBitContext tmpb;
     813             :     uint8_t checksum;
     814             :     unsigned int ch;
     815             : 
     816           0 :     put_bits(pb, 14, 0x31ea                ); /* TODO 0x31eb */
     817           0 :     put_bits(pb, 16, ctx->timestamp        );
     818           0 :     put_bits(pb,  4, rh->min_channel       );
     819           0 :     put_bits(pb,  4, rh->max_channel       );
     820           0 :     put_bits(pb,  4, rh->max_matrix_channel);
     821           0 :     put_bits(pb,  4, rh->noise_shift       );
     822           0 :     put_bits(pb, 23, rh->noisegen_seed     );
     823           0 :     put_bits(pb,  4, 0                     ); /* TODO max_shift */
     824           0 :     put_bits(pb,  5, rh->max_huff_lsbs     );
     825           0 :     put_bits(pb,  5, rh->max_output_bits   );
     826           0 :     put_bits(pb,  5, rh->max_output_bits   );
     827           0 :     put_bits(pb,  1, rh->data_check_present);
     828           0 :     put_bits(pb,  8, lossless_check        );
     829           0 :     put_bits(pb, 16, 0                     ); /* ignored */
     830             : 
     831           0 :     for (ch = 0; ch <= rh->max_matrix_channel; ch++)
     832           0 :         put_bits(pb, 6, ch);
     833             : 
     834             :     /* Data must be flushed for the checksum to be correct. */
     835           0 :     tmpb = *pb;
     836           0 :     flush_put_bits(&tmpb);
     837             : 
     838           0 :     checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
     839             : 
     840           0 :     put_bits(pb,  8, checksum);
     841           0 : }
     842             : 
     843             : /** Writes matrix params for all primitive matrices to the bitstream. */
     844           0 : static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
     845             : {
     846           0 :     DecodingParams *dp = ctx->cur_decoding_params;
     847           0 :     MatrixParams *mp = &dp->matrix_params;
     848             :     unsigned int mat;
     849             : 
     850           0 :     put_bits(pb, 4, mp->count);
     851             : 
     852           0 :     for (mat = 0; mat < mp->count; mat++) {
     853             :         unsigned int channel;
     854             : 
     855           0 :         put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
     856           0 :         put_bits(pb, 4, mp->fbits[mat]);
     857           0 :         put_bits(pb, 1, 0             ); /* lsb_bypass */
     858             : 
     859           0 :         for (channel = 0; channel < ctx->num_channels; channel++) {
     860           0 :             int32_t coeff = mp->coeff[mat][channel];
     861             : 
     862           0 :             if (coeff) {
     863           0 :                 put_bits(pb, 1, 1);
     864             : 
     865           0 :                 coeff >>= 14 - mp->fbits[mat];
     866             : 
     867           0 :                 put_sbits(pb, mp->fbits[mat] + 2, coeff);
     868             :             } else {
     869           0 :                 put_bits(pb, 1, 0);
     870             :             }
     871             :         }
     872             :     }
     873           0 : }
     874             : 
     875             : /** Writes filter parameters for one filter to the bitstream. */
     876           0 : static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
     877             :                                 unsigned int channel, unsigned int filter)
     878             : {
     879           0 :     FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
     880             : 
     881           0 :     put_bits(pb, 4, fp->order);
     882             : 
     883           0 :     if (fp->order > 0) {
     884             :         int i;
     885           0 :         int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
     886             : 
     887           0 :         put_bits(pb, 4, fp->shift      );
     888           0 :         put_bits(pb, 5, fp->coeff_bits );
     889           0 :         put_bits(pb, 3, fp->coeff_shift);
     890             : 
     891           0 :         for (i = 0; i < fp->order; i++) {
     892           0 :             put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
     893             :         }
     894             : 
     895             :         /* TODO state data for IIR filter. */
     896           0 :         put_bits(pb, 1, 0);
     897             :     }
     898           0 : }
     899             : 
     900             : /** Writes decoding parameters to the bitstream. These change very often,
     901             :  *  usually at almost every frame.
     902             :  */
     903           0 : static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
     904             :                                   int params_changed)
     905             : {
     906           0 :     DecodingParams *dp = ctx->cur_decoding_params;
     907           0 :     RestartHeader  *rh = ctx->cur_restart_header;
     908           0 :     MatrixParams *mp = &dp->matrix_params;
     909             :     unsigned int ch;
     910             : 
     911           0 :     if (dp->param_presence_flags != PARAMS_DEFAULT &&
     912           0 :         params_changed & PARAM_PRESENCE_FLAGS) {
     913           0 :         put_bits(pb, 1, 1);
     914           0 :         put_bits(pb, 8, dp->param_presence_flags);
     915             :     } else {
     916           0 :         put_bits(pb, 1, 0);
     917             :     }
     918             : 
     919           0 :     if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
     920           0 :         if (params_changed       & PARAM_BLOCKSIZE) {
     921           0 :             put_bits(pb, 1, 1);
     922           0 :             put_bits(pb, 9, dp->blocksize);
     923             :         } else {
     924           0 :             put_bits(pb, 1, 0);
     925             :         }
     926             :     }
     927             : 
     928           0 :     if (dp->param_presence_flags & PARAM_MATRIX) {
     929           0 :         if (params_changed       & PARAM_MATRIX) {
     930           0 :             put_bits(pb, 1, 1);
     931           0 :             write_matrix_params(ctx, pb);
     932             :         } else {
     933           0 :             put_bits(pb, 1, 0);
     934             :         }
     935             :     }
     936             : 
     937           0 :     if (dp->param_presence_flags & PARAM_OUTSHIFT) {
     938           0 :         if (params_changed       & PARAM_OUTSHIFT) {
     939           0 :             put_bits(pb, 1, 1);
     940           0 :             for (ch = 0; ch <= rh->max_matrix_channel; ch++)
     941           0 :                 put_sbits(pb, 4, mp->shift[ch]);
     942             :         } else {
     943           0 :             put_bits(pb, 1, 0);
     944             :         }
     945             :     }
     946             : 
     947           0 :     if (dp->param_presence_flags & PARAM_QUANTSTEP) {
     948           0 :         if (params_changed       & PARAM_QUANTSTEP) {
     949           0 :             put_bits(pb, 1, 1);
     950           0 :             for (ch = 0; ch <= rh->max_channel; ch++)
     951           0 :                 put_bits(pb, 4, dp->quant_step_size[ch]);
     952             :         } else {
     953           0 :             put_bits(pb, 1, 0);
     954             :         }
     955             :     }
     956             : 
     957           0 :     for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
     958           0 :         ChannelParams *cp = &ctx->cur_channel_params[ch];
     959             : 
     960           0 :         if (dp->param_presence_flags & 0xF) {
     961           0 :             put_bits(pb, 1, 1);
     962             : 
     963           0 :             if (dp->param_presence_flags & PARAM_FIR) {
     964           0 :                 if (params_changed       & PARAM_FIR) {
     965           0 :                     put_bits(pb, 1, 1);
     966           0 :                     write_filter_params(ctx, pb, ch, FIR);
     967             :                 } else {
     968           0 :                     put_bits(pb, 1, 0);
     969             :                 }
     970             :             }
     971             : 
     972           0 :             if (dp->param_presence_flags & PARAM_IIR) {
     973           0 :                 if (params_changed       & PARAM_IIR) {
     974           0 :                     put_bits(pb, 1, 1);
     975           0 :                     write_filter_params(ctx, pb, ch, IIR);
     976             :                 } else {
     977           0 :                     put_bits(pb, 1, 0);
     978             :                 }
     979             :             }
     980             : 
     981           0 :             if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
     982           0 :                 if (params_changed       & PARAM_HUFFOFFSET) {
     983           0 :                     put_bits (pb,  1, 1);
     984           0 :                     put_sbits(pb, 15, cp->huff_offset);
     985             :                 } else {
     986           0 :                     put_bits(pb, 1, 0);
     987             :                 }
     988             :             }
     989             : 
     990           0 :             put_bits(pb, 2, cp->codebook );
     991           0 :             put_bits(pb, 5, cp->huff_lsbs);
     992             :         } else {
     993           0 :             put_bits(pb, 1, 0);
     994             :         }
     995             :     }
     996           0 : }
     997             : 
     998             : /** Writes the residuals to the bitstream. That is, the VLC codes from the
     999             :  *  codebooks (if any is used), and then the residual.
    1000             :  */
    1001           0 : static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
    1002             : {
    1003           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1004           0 :     RestartHeader  *rh = ctx->cur_restart_header;
    1005           0 :     int32_t *sample_buffer = ctx->write_buffer;
    1006             :     int32_t sign_huff_offset[MAX_CHANNELS];
    1007             :     int codebook_index      [MAX_CHANNELS];
    1008             :     int lsb_bits            [MAX_CHANNELS];
    1009             :     unsigned int i, ch;
    1010             : 
    1011           0 :     for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
    1012           0 :         ChannelParams *cp = &ctx->cur_channel_params[ch];
    1013             :         int sign_shift;
    1014             : 
    1015           0 :         lsb_bits        [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
    1016           0 :         codebook_index  [ch] = cp->codebook  - 1;
    1017           0 :         sign_huff_offset[ch] = cp->huff_offset;
    1018             : 
    1019           0 :         sign_shift = lsb_bits[ch] - 1;
    1020             : 
    1021           0 :         if (cp->codebook > 0) {
    1022           0 :             sign_huff_offset[ch] -= 7 << lsb_bits[ch];
    1023           0 :             sign_shift += 3 - cp->codebook;
    1024             :         }
    1025             : 
    1026             :         /* Unsign if needed. */
    1027           0 :         if (sign_shift >= 0)
    1028           0 :             sign_huff_offset[ch] -= 1 << sign_shift;
    1029             :     }
    1030             : 
    1031           0 :     for (i = 0; i < dp->blocksize; i++) {
    1032           0 :         for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
    1033           0 :             int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
    1034             : 
    1035           0 :             sample -= sign_huff_offset[ch];
    1036             : 
    1037           0 :             if (codebook_index[ch] >= 0) {
    1038           0 :                 int vlc = sample >> lsb_bits[ch];
    1039           0 :                 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
    1040           0 :                              ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
    1041             :             }
    1042             : 
    1043           0 :             put_sbits(pb, lsb_bits[ch], sample);
    1044             :         }
    1045           0 :         sample_buffer += 2; /* noise channels */
    1046             :     }
    1047             : 
    1048           0 :     ctx->write_buffer = sample_buffer;
    1049           0 : }
    1050             : 
    1051             : /** Writes the substreams data to the bitstream. */
    1052           0 : static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
    1053             :                               int restart_frame,
    1054             :                               uint16_t substream_data_len[MAX_SUBSTREAMS])
    1055             : {
    1056           0 :     int32_t *lossless_check_data = ctx->lossless_check_data;
    1057             :     unsigned int substr;
    1058           0 :     int end = 0;
    1059             : 
    1060           0 :     lossless_check_data += ctx->frame_index * ctx->num_substreams;
    1061             : 
    1062           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    1063           0 :         unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
    1064           0 :         unsigned int num_subblocks = ctx->major_filter_state_subblock;
    1065             :         unsigned int subblock;
    1066           0 :         RestartHeader  *rh = &ctx->restart_header [substr];
    1067           0 :         int substr_restart_frame = restart_frame;
    1068             :         uint8_t parity, checksum;
    1069             :         PutBitContext pb, tmpb;
    1070             :         int params_changed;
    1071             : 
    1072           0 :         ctx->cur_restart_header = rh;
    1073             : 
    1074           0 :         init_put_bits(&pb, buf, buf_size);
    1075             : 
    1076           0 :         for (subblock = 0; subblock <= num_subblocks; subblock++) {
    1077             :             unsigned int subblock_index;
    1078             : 
    1079           0 :             subblock_index = cur_subblock_index++;
    1080             : 
    1081           0 :             ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
    1082           0 :             ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
    1083             : 
    1084           0 :             params_changed = ctx->major_params_changed[subblock_index][substr];
    1085             : 
    1086           0 :             if (substr_restart_frame || params_changed) {
    1087           0 :                 put_bits(&pb, 1, 1);
    1088             : 
    1089           0 :                 if (substr_restart_frame) {
    1090           0 :                     put_bits(&pb, 1, 1);
    1091             : 
    1092           0 :                     write_restart_header(ctx, &pb);
    1093           0 :                     rh->lossless_check_data = 0;
    1094             :                 } else {
    1095           0 :                     put_bits(&pb, 1, 0);
    1096             :                 }
    1097             : 
    1098           0 :                 write_decoding_params(ctx, &pb, params_changed);
    1099             :             } else {
    1100           0 :                 put_bits(&pb, 1, 0);
    1101             :             }
    1102             : 
    1103           0 :             write_block_data(ctx, &pb);
    1104             : 
    1105           0 :             put_bits(&pb, 1, !substr_restart_frame);
    1106             : 
    1107           0 :             substr_restart_frame = 0;
    1108             :         }
    1109             : 
    1110           0 :         put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
    1111             : 
    1112           0 :         rh->lossless_check_data ^= *lossless_check_data++;
    1113             : 
    1114           0 :         if (ctx->last_frame == ctx->inout_buffer) {
    1115             :             /* TODO find a sample and implement shorten_by. */
    1116           0 :             put_bits(&pb, 32, END_OF_STREAM);
    1117             :         }
    1118             : 
    1119             :         /* Data must be flushed for the checksum and parity to be correct. */
    1120           0 :         tmpb = pb;
    1121           0 :         flush_put_bits(&tmpb);
    1122             : 
    1123           0 :         parity   = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
    1124           0 :         checksum = ff_mlp_checksum8       (buf, put_bits_count(&pb) >> 3);
    1125             : 
    1126           0 :         put_bits(&pb, 8, parity  );
    1127           0 :         put_bits(&pb, 8, checksum);
    1128             : 
    1129           0 :         flush_put_bits(&pb);
    1130             : 
    1131           0 :         end += put_bits_count(&pb) >> 3;
    1132           0 :         substream_data_len[substr] = end;
    1133             : 
    1134           0 :         buf += put_bits_count(&pb) >> 3;
    1135             :     }
    1136             : 
    1137           0 :     ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
    1138           0 :     ctx->major_filter_state_subblock = 0;
    1139             : 
    1140           0 :     return buf;
    1141             : }
    1142             : 
    1143             : /** Writes the access unit and substream headers to the bitstream. */
    1144           0 : static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
    1145             :                                 uint8_t *substream_headers, unsigned int length,
    1146             :                                 int restart_frame,
    1147             :                                 uint16_t substream_data_len[MAX_SUBSTREAMS])
    1148             : {
    1149           0 :     uint16_t access_unit_header = 0;
    1150           0 :     uint16_t parity_nibble = 0;
    1151             :     unsigned int substr;
    1152             : 
    1153           0 :     parity_nibble  = ctx->dts;
    1154           0 :     parity_nibble ^= length;
    1155             : 
    1156           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    1157           0 :         uint16_t substr_hdr = 0;
    1158             : 
    1159           0 :         substr_hdr |= (0 << 15); /* extraword */
    1160           0 :         substr_hdr |= (!restart_frame << 14); /* !restart_frame */
    1161           0 :         substr_hdr |= (1 << 13); /* checkdata */
    1162           0 :         substr_hdr |= (0 << 12); /* ??? */
    1163           0 :         substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
    1164             : 
    1165           0 :         AV_WB16(substream_headers, substr_hdr);
    1166             : 
    1167           0 :         parity_nibble ^= *substream_headers++;
    1168           0 :         parity_nibble ^= *substream_headers++;
    1169             :     }
    1170             : 
    1171           0 :     parity_nibble ^= parity_nibble >> 8;
    1172           0 :     parity_nibble ^= parity_nibble >> 4;
    1173           0 :     parity_nibble &= 0xF;
    1174             : 
    1175           0 :     access_unit_header |= (parity_nibble ^ 0xF) << 12;
    1176           0 :     access_unit_header |= length & 0xFFF;
    1177             : 
    1178           0 :     AV_WB16(frame_header  , access_unit_header);
    1179           0 :     AV_WB16(frame_header+2, ctx->dts          );
    1180           0 : }
    1181             : 
    1182             : /** Writes an entire access unit to the bitstream. */
    1183           0 : static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
    1184             :                                       int buf_size, int restart_frame)
    1185             : {
    1186             :     uint16_t substream_data_len[MAX_SUBSTREAMS];
    1187           0 :     uint8_t *buf1, *buf0 = buf;
    1188             :     unsigned int substr;
    1189             :     int total_length;
    1190             : 
    1191           0 :     if (buf_size < 4)
    1192           0 :         return -1;
    1193             : 
    1194             :     /* Frame header will be written at the end. */
    1195           0 :     buf      += 4;
    1196           0 :     buf_size -= 4;
    1197             : 
    1198           0 :     if (restart_frame) {
    1199           0 :         if (buf_size < 28)
    1200           0 :             return -1;
    1201           0 :         write_major_sync(ctx, buf, buf_size);
    1202           0 :         buf      += 28;
    1203           0 :         buf_size -= 28;
    1204             :     }
    1205             : 
    1206           0 :     buf1 = buf;
    1207             : 
    1208             :     /* Substream headers will be written at the end. */
    1209           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    1210           0 :         buf      += 2;
    1211           0 :         buf_size -= 2;
    1212             :     }
    1213             : 
    1214           0 :     buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
    1215             : 
    1216           0 :     total_length = buf - buf0;
    1217             : 
    1218           0 :     write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
    1219             : 
    1220           0 :     return total_length;
    1221             : }
    1222             : 
    1223             : /****************************************************************************
    1224             :  ****************** Functions that input data to context ********************
    1225             :  ****************************************************************************/
    1226             : 
    1227             : /** Inputs data from the samples passed by lavc into the context, shifts them
    1228             :  *  appropriately depending on the bit-depth, and calculates the
    1229             :  *  lossless_check_data that will be written to the restart header.
    1230             :  */
    1231           0 : static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
    1232             :                                 int is24)
    1233             : {
    1234           0 :     int32_t *lossless_check_data = ctx->lossless_check_data;
    1235           0 :     const int32_t *samples_32 = (const int32_t *) samples;
    1236           0 :     const int16_t *samples_16 = (const int16_t *) samples;
    1237             :     unsigned int substr;
    1238             : 
    1239           0 :     lossless_check_data += ctx->frame_index * ctx->num_substreams;
    1240             : 
    1241           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    1242           0 :         RestartHeader  *rh = &ctx->restart_header [substr];
    1243           0 :         int32_t *sample_buffer = ctx->inout_buffer;
    1244           0 :         int32_t temp_lossless_check_data = 0;
    1245           0 :         uint32_t greatest = 0;
    1246             :         unsigned int channel;
    1247             :         int i;
    1248             : 
    1249           0 :         for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
    1250           0 :             for (channel = 0; channel <= rh->max_channel; channel++) {
    1251             :                 uint32_t abs_sample;
    1252             :                 int32_t sample;
    1253             : 
    1254           0 :                 sample = is24 ? *samples_32++ >> 8 : *samples_16++ << 8;
    1255             : 
    1256             :                 /* TODO Find out if number_sbits can be used for negative values. */
    1257           0 :                 abs_sample = FFABS(sample);
    1258           0 :                 if (greatest < abs_sample)
    1259           0 :                     greatest = abs_sample;
    1260             : 
    1261           0 :                 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
    1262           0 :                 *sample_buffer++ = sample;
    1263             :             }
    1264             : 
    1265           0 :             sample_buffer += 2; /* noise channels */
    1266             :         }
    1267             : 
    1268           0 :         ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
    1269             : 
    1270           0 :         *lossless_check_data++ = temp_lossless_check_data;
    1271             :     }
    1272           0 : }
    1273             : 
    1274             : /** Wrapper function for inputting data in two different bit-depths. */
    1275           0 : static void input_data(MLPEncodeContext *ctx, void *samples)
    1276             : {
    1277           0 :     if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
    1278           0 :         input_data_internal(ctx, samples, 1);
    1279             :     else
    1280           0 :         input_data_internal(ctx, samples, 0);
    1281           0 : }
    1282             : 
    1283           0 : static void input_to_sample_buffer(MLPEncodeContext *ctx)
    1284             : {
    1285           0 :     int32_t *sample_buffer = ctx->sample_buffer;
    1286             :     unsigned int index;
    1287             : 
    1288           0 :     for (index = 0; index < ctx->number_of_frames; index++) {
    1289           0 :         unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
    1290           0 :         int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
    1291             :         unsigned int i, channel;
    1292             : 
    1293           0 :         for (i = 0; i < ctx->frame_size[cur_index]; i++) {
    1294           0 :             for (channel = 0; channel < ctx->avctx->channels; channel++)
    1295           0 :                 *sample_buffer++ = *input_buffer++;
    1296           0 :             sample_buffer += 2; /* noise_channels */
    1297           0 :             input_buffer += 2; /* noise_channels */
    1298             :         }
    1299             :     }
    1300           0 : }
    1301             : 
    1302             : /****************************************************************************
    1303             :  ********* Functions that analyze the data and set the parameters ***********
    1304             :  ****************************************************************************/
    1305             : 
    1306             : /** Counts the number of trailing zeroes in a value */
    1307           0 : static int number_trailing_zeroes(int32_t sample)
    1308             : {
    1309             :     int bits;
    1310             : 
    1311           0 :     for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
    1312             : 
    1313             :     /* All samples are 0. TODO Return previous quant_step_size to avoid
    1314             :      * writing a new header. */
    1315           0 :     if (bits == 24)
    1316           0 :         return 0;
    1317             : 
    1318           0 :     return bits;
    1319             : }
    1320             : 
    1321             : /** Determines how many bits are zero at the end of all samples so they can be
    1322             :  *  shifted out.
    1323             :  */
    1324           0 : static void determine_quant_step_size(MLPEncodeContext *ctx)
    1325             : {
    1326           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1327           0 :     RestartHeader  *rh = ctx->cur_restart_header;
    1328           0 :     MatrixParams *mp = &dp->matrix_params;
    1329           0 :     int32_t *sample_buffer = ctx->sample_buffer;
    1330             :     int32_t sample_mask[MAX_CHANNELS];
    1331             :     unsigned int channel;
    1332             :     int i;
    1333             : 
    1334           0 :     memset(sample_mask, 0x00, sizeof(sample_mask));
    1335             : 
    1336           0 :     for (i = 0; i < ctx->number_of_samples; i++) {
    1337           0 :         for (channel = 0; channel <= rh->max_channel; channel++)
    1338           0 :             sample_mask[channel] |= *sample_buffer++;
    1339             : 
    1340           0 :         sample_buffer += 2; /* noise channels */
    1341             :     }
    1342             : 
    1343           0 :     for (channel = 0; channel <= rh->max_channel; channel++)
    1344           0 :         dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
    1345           0 : }
    1346             : 
    1347             : /** Determines the smallest number of bits needed to encode the filter
    1348             :  *  coefficients, and if it's possible to right-shift their values without
    1349             :  *  losing any precision.
    1350             :  */
    1351           0 : static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
    1352             : {
    1353           0 :     int min = INT_MAX, max = INT_MIN;
    1354             :     int bits, shift;
    1355           0 :     int coeff_mask = 0;
    1356             :     int order;
    1357             : 
    1358           0 :     for (order = 0; order < fp->order; order++) {
    1359           0 :         int coeff = fcoeff[order];
    1360             : 
    1361           0 :         if (coeff < min)
    1362           0 :             min = coeff;
    1363           0 :         if (coeff > max)
    1364           0 :             max = coeff;
    1365             : 
    1366           0 :         coeff_mask |= coeff;
    1367             :     }
    1368             : 
    1369           0 :     bits = FFMAX(number_sbits(min), number_sbits(max));
    1370             : 
    1371           0 :     for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
    1372             : 
    1373           0 :     fp->coeff_bits  = bits;
    1374           0 :     fp->coeff_shift = shift;
    1375           0 : }
    1376             : 
    1377             : /** Determines the best filter parameters for the given data and writes the
    1378             :  *  necessary information to the context.
    1379             :  *  TODO Add IIR filter predictor!
    1380             :  */
    1381           0 : static void set_filter_params(MLPEncodeContext *ctx,
    1382             :                               unsigned int channel, unsigned int filter,
    1383             :                               int clear_filter)
    1384             : {
    1385           0 :     ChannelParams *cp = &ctx->cur_channel_params[channel];
    1386           0 :     FilterParams *fp = &cp->filter_params[filter];
    1387             : 
    1388           0 :     if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
    1389             :         clear_filter) {
    1390           0 :         fp->order = 0;
    1391           0 :     } else if (filter == IIR) {
    1392           0 :         fp->order = 0;
    1393           0 :     } else if (filter == FIR) {
    1394           0 :         const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
    1395           0 :                               ? 4 : MLP_MAX_LPC_ORDER;
    1396           0 :         int32_t *sample_buffer = ctx->sample_buffer + channel;
    1397             :         int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
    1398           0 :         int32_t *lpc_samples = ctx->lpc_sample_buffer;
    1399           0 :         int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
    1400             :         int shift[MLP_MAX_LPC_ORDER];
    1401             :         unsigned int i;
    1402             :         int order;
    1403             : 
    1404           0 :         for (i = 0; i < ctx->number_of_samples; i++) {
    1405           0 :             *lpc_samples++ = *sample_buffer;
    1406           0 :             sample_buffer += ctx->num_channels;
    1407             :         }
    1408             : 
    1409           0 :         order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
    1410           0 :                                   ctx->number_of_samples, MLP_MIN_LPC_ORDER,
    1411             :                                   max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
    1412             :                                   ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
    1413             :                                   MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
    1414             : 
    1415           0 :         fp->order = order;
    1416           0 :         fp->shift = shift[order-1];
    1417             : 
    1418           0 :         for (i = 0; i < order; i++)
    1419           0 :             fcoeff[i] = coefs[order-1][i];
    1420             : 
    1421           0 :         code_filter_coeffs(ctx, fp, fcoeff);
    1422             :     }
    1423           0 : }
    1424             : 
    1425             : /** Tries to determine a good prediction filter, and applies it to the samples
    1426             :  *  buffer if the filter is good enough. Sets the filter data to be cleared if
    1427             :  *  no good filter was found.
    1428             :  */
    1429           0 : static void determine_filters(MLPEncodeContext *ctx)
    1430             : {
    1431           0 :     RestartHeader *rh = ctx->cur_restart_header;
    1432             :     int channel, filter;
    1433             : 
    1434           0 :     for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
    1435           0 :         for (filter = 0; filter < NUM_FILTERS; filter++)
    1436           0 :             set_filter_params(ctx, channel, filter, 0);
    1437             :     }
    1438           0 : }
    1439             : 
    1440             : enum MLPChMode {
    1441             :     MLP_CHMODE_LEFT_RIGHT,
    1442             :     MLP_CHMODE_LEFT_SIDE,
    1443             :     MLP_CHMODE_RIGHT_SIDE,
    1444             :     MLP_CHMODE_MID_SIDE,
    1445             : };
    1446             : 
    1447           0 : static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
    1448             : {
    1449           0 :     uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
    1450           0 :     int32_t *right_ch = ctx->sample_buffer + 1;
    1451           0 :     int32_t *left_ch  = ctx->sample_buffer;
    1452             :     int i;
    1453           0 :     enum MLPChMode best = 0;
    1454             : 
    1455           0 :     for(i = 2; i < ctx->number_of_samples; i++) {
    1456           0 :         int32_t left  = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
    1457           0 :         int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
    1458             : 
    1459           0 :         sum[0] += FFABS( left        );
    1460           0 :         sum[1] += FFABS(        right);
    1461           0 :         sum[2] += FFABS((left + right) >> 1);
    1462           0 :         sum[3] += FFABS( left - right);
    1463             :     }
    1464             : 
    1465           0 :     score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
    1466           0 :     score[MLP_CHMODE_LEFT_SIDE]  = sum[0] + sum[3];
    1467           0 :     score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
    1468           0 :     score[MLP_CHMODE_MID_SIDE]   = sum[2] + sum[3];
    1469             : 
    1470           0 :     for(i = 1; i < 3; i++)
    1471           0 :         if(score[i] < score[best])
    1472           0 :             best = i;
    1473             : 
    1474           0 :     return best;
    1475             : }
    1476             : 
    1477             : /** Determines how many fractional bits are needed to encode matrix
    1478             :  *  coefficients. Also shifts the coefficients to fit within 2.14 bits.
    1479             :  */
    1480           0 : static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
    1481             : {
    1482           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1483           0 :     MatrixParams *mp = &dp->matrix_params;
    1484           0 :     int32_t coeff_mask = 0;
    1485             :     unsigned int channel;
    1486             :     unsigned int bits;
    1487             : 
    1488           0 :     for (channel = 0; channel < ctx->num_channels; channel++) {
    1489           0 :         int32_t coeff = mp->coeff[mat][channel];
    1490           0 :         coeff_mask |= coeff;
    1491             :     }
    1492             : 
    1493           0 :     for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
    1494             : 
    1495           0 :     mp->fbits   [mat] = 14 - bits;
    1496           0 : }
    1497             : 
    1498             : /** Determines best coefficients to use for the lossless matrix. */
    1499           0 : static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
    1500             : {
    1501           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1502           0 :     MatrixParams *mp = &dp->matrix_params;
    1503           0 :     unsigned int shift = 0;
    1504             :     unsigned int channel;
    1505             :     int mat;
    1506             :     enum MLPChMode mode;
    1507             : 
    1508             :     /* No decorrelation for non-stereo. */
    1509           0 :     if (ctx->num_channels - 2 != 2) {
    1510           0 :         mp->count = 0;
    1511           0 :         return;
    1512             :     }
    1513             : 
    1514           0 :     mode = estimate_stereo_mode(ctx);
    1515             : 
    1516           0 :     switch(mode) {
    1517             :         /* TODO: add matrix for MID_SIDE */
    1518           0 :         case MLP_CHMODE_MID_SIDE:
    1519             :         case MLP_CHMODE_LEFT_RIGHT:
    1520           0 :             mp->count    = 0;
    1521           0 :             break;
    1522           0 :         case MLP_CHMODE_LEFT_SIDE:
    1523           0 :             mp->count    = 1;
    1524           0 :             mp->outch[0] = 1;
    1525           0 :             mp->coeff[0][0] =  1 << 14; mp->coeff[0][1] = -(1 << 14);
    1526           0 :             mp->coeff[0][2] =  0 << 14; mp->coeff[0][2] =   0 << 14;
    1527           0 :             mp->forco[0][0] =  1 << 14; mp->forco[0][1] = -(1 << 14);
    1528           0 :             mp->forco[0][2] =  0 << 14; mp->forco[0][2] =   0 << 14;
    1529           0 :             break;
    1530           0 :         case MLP_CHMODE_RIGHT_SIDE:
    1531           0 :             mp->count    = 1;
    1532           0 :             mp->outch[0] = 0;
    1533           0 :             mp->coeff[0][0] =  1 << 14; mp->coeff[0][1] =   1 << 14;
    1534           0 :             mp->coeff[0][2] =  0 << 14; mp->coeff[0][2] =   0 << 14;
    1535           0 :             mp->forco[0][0] =  1 << 14; mp->forco[0][1] = -(1 << 14);
    1536           0 :             mp->forco[0][2] =  0 << 14; mp->forco[0][2] =   0 << 14;
    1537           0 :             break;
    1538             :     }
    1539             : 
    1540           0 :     for (mat = 0; mat < mp->count; mat++)
    1541           0 :         code_matrix_coeffs(ctx, mat);
    1542             : 
    1543           0 :     for (channel = 0; channel < ctx->num_channels; channel++)
    1544           0 :         mp->shift[channel] = shift;
    1545             : }
    1546             : 
    1547             : /** Min and max values that can be encoded with each codebook. The values for
    1548             :  *  the third codebook take into account the fact that the sign shift for this
    1549             :  *  codebook is outside the coded value, so it has one more bit of precision.
    1550             :  *  It should actually be -7 -> 7, shifted down by 0.5.
    1551             :  */
    1552             : static const int codebook_extremes[3][2] = {
    1553             :     {-9, 8}, {-8, 7}, {-15, 14},
    1554             : };
    1555             : 
    1556             : /** Determines the amount of bits needed to encode the samples using no
    1557             :  *  codebooks and a specified offset.
    1558             :  */
    1559           0 : static void no_codebook_bits_offset(MLPEncodeContext *ctx,
    1560             :                                     unsigned int channel, int16_t offset,
    1561             :                                     int32_t min, int32_t max,
    1562             :                                     BestOffset *bo)
    1563             : {
    1564           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1565             :     int32_t unsign;
    1566             :     int lsb_bits;
    1567             : 
    1568           0 :     min -= offset;
    1569           0 :     max -= offset;
    1570             : 
    1571           0 :     lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
    1572             : 
    1573           0 :     lsb_bits += !!lsb_bits;
    1574             : 
    1575           0 :     unsign = 1 << (lsb_bits - 1);
    1576             : 
    1577           0 :     bo->offset   = offset;
    1578           0 :     bo->lsb_bits = lsb_bits;
    1579           0 :     bo->bitcount = lsb_bits * dp->blocksize;
    1580           0 :     bo->min      = offset - unsign + 1;
    1581           0 :     bo->max      = offset + unsign;
    1582           0 : }
    1583             : 
    1584             : /** Determines the least amount of bits needed to encode the samples using no
    1585             :  *  codebooks.
    1586             :  */
    1587           0 : static void no_codebook_bits(MLPEncodeContext *ctx,
    1588             :                              unsigned int channel,
    1589             :                              int32_t min, int32_t max,
    1590             :                              BestOffset *bo)
    1591             : {
    1592           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1593             :     int16_t offset;
    1594             :     int32_t unsign;
    1595             :     uint32_t diff;
    1596             :     int lsb_bits;
    1597             : 
    1598             :     /* Set offset inside huffoffset's boundaries by adjusting extremes
    1599             :      * so that more bits are used, thus shifting the offset. */
    1600           0 :     if (min < HUFF_OFFSET_MIN)
    1601           0 :         max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
    1602           0 :     if (max > HUFF_OFFSET_MAX)
    1603           0 :         min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
    1604             : 
    1605             :     /* Determine offset and minimum number of bits. */
    1606           0 :     diff = max - min;
    1607             : 
    1608           0 :     lsb_bits = number_sbits(diff) - 1;
    1609             : 
    1610           0 :     unsign = 1 << (lsb_bits - 1);
    1611             : 
    1612             :     /* If all samples are the same (lsb_bits == 0), offset must be
    1613             :      * adjusted because of sign_shift. */
    1614           0 :     offset = min + diff / 2 + !!lsb_bits;
    1615             : 
    1616           0 :     bo->offset   = offset;
    1617           0 :     bo->lsb_bits = lsb_bits;
    1618           0 :     bo->bitcount = lsb_bits * dp->blocksize;
    1619           0 :     bo->min      = max - unsign + 1;
    1620           0 :     bo->max      = min + unsign;
    1621           0 : }
    1622             : 
    1623             : /** Determines the least amount of bits needed to encode the samples using a
    1624             :  *  given codebook and a given offset.
    1625             :  */
    1626           0 : static inline void codebook_bits_offset(MLPEncodeContext *ctx,
    1627             :                                         unsigned int channel, int codebook,
    1628             :                                         int32_t sample_min, int32_t sample_max,
    1629             :                                         int16_t offset, BestOffset *bo)
    1630             : {
    1631           0 :     int32_t codebook_min = codebook_extremes[codebook][0];
    1632           0 :     int32_t codebook_max = codebook_extremes[codebook][1];
    1633           0 :     int32_t *sample_buffer = ctx->sample_buffer + channel;
    1634           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1635           0 :     int codebook_offset  = 7 + (2 - codebook);
    1636           0 :     int32_t unsign_offset = offset;
    1637           0 :     int lsb_bits = 0, bitcount = 0;
    1638           0 :     int offset_min = INT_MAX, offset_max = INT_MAX;
    1639             :     int unsign, mask;
    1640             :     int i;
    1641             : 
    1642           0 :     sample_min -= offset;
    1643           0 :     sample_max -= offset;
    1644             : 
    1645           0 :     while (sample_min < codebook_min || sample_max > codebook_max) {
    1646           0 :         lsb_bits++;
    1647           0 :         sample_min >>= 1;
    1648           0 :         sample_max >>= 1;
    1649             :     }
    1650             : 
    1651           0 :     unsign = 1 << lsb_bits;
    1652           0 :     mask   = unsign - 1;
    1653             : 
    1654           0 :     if (codebook == 2) {
    1655           0 :         unsign_offset -= unsign;
    1656           0 :         lsb_bits++;
    1657             :     }
    1658             : 
    1659           0 :     for (i = 0; i < dp->blocksize; i++) {
    1660           0 :         int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
    1661             :         int temp_min, temp_max;
    1662             : 
    1663           0 :         sample -= unsign_offset;
    1664             : 
    1665           0 :         temp_min = sample & mask;
    1666           0 :         if (temp_min < offset_min)
    1667           0 :             offset_min = temp_min;
    1668             : 
    1669           0 :         temp_max = unsign - temp_min - 1;
    1670           0 :         if (temp_max < offset_max)
    1671           0 :             offset_max = temp_max;
    1672             : 
    1673           0 :         sample >>= lsb_bits;
    1674             : 
    1675           0 :         bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
    1676             : 
    1677           0 :         sample_buffer += ctx->num_channels;
    1678             :     }
    1679             : 
    1680           0 :     bo->offset   = offset;
    1681           0 :     bo->lsb_bits = lsb_bits;
    1682           0 :     bo->bitcount = lsb_bits * dp->blocksize + bitcount;
    1683           0 :     bo->min      = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
    1684           0 :     bo->max      = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
    1685           0 : }
    1686             : 
    1687             : /** Determines the least amount of bits needed to encode the samples using a
    1688             :  *  given codebook. Searches for the best offset to minimize the bits.
    1689             :  */
    1690           0 : static inline void codebook_bits(MLPEncodeContext *ctx,
    1691             :                                  unsigned int channel, int codebook,
    1692             :                                  int offset, int32_t min, int32_t max,
    1693             :                                  BestOffset *bo, int direction)
    1694             : {
    1695           0 :     int previous_count = INT_MAX;
    1696             :     int offset_min, offset_max;
    1697           0 :     int is_greater = 0;
    1698             : 
    1699           0 :     offset_min = FFMAX(min, HUFF_OFFSET_MIN);
    1700           0 :     offset_max = FFMIN(max, HUFF_OFFSET_MAX);
    1701             : 
    1702           0 :     for (;;) {
    1703             :         BestOffset temp_bo;
    1704             : 
    1705           0 :         codebook_bits_offset(ctx, channel, codebook,
    1706             :                              min, max, offset,
    1707             :                              &temp_bo);
    1708             : 
    1709           0 :         if (temp_bo.bitcount < previous_count) {
    1710           0 :             if (temp_bo.bitcount < bo->bitcount)
    1711           0 :                 *bo = temp_bo;
    1712             : 
    1713           0 :             is_greater = 0;
    1714           0 :         } else if (++is_greater >= ctx->max_codebook_search)
    1715           0 :             break;
    1716             : 
    1717           0 :         previous_count = temp_bo.bitcount;
    1718             : 
    1719           0 :         if (direction) {
    1720           0 :             offset = temp_bo.max + 1;
    1721           0 :             if (offset > offset_max)
    1722           0 :                 break;
    1723             :         } else {
    1724           0 :             offset = temp_bo.min - 1;
    1725           0 :             if (offset < offset_min)
    1726           0 :                 break;
    1727             :         }
    1728             :     }
    1729           0 : }
    1730             : 
    1731             : /** Determines the least amount of bits needed to encode the samples using
    1732             :  *  any or no codebook.
    1733             :  */
    1734           0 : static void determine_bits(MLPEncodeContext *ctx)
    1735             : {
    1736           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1737           0 :     RestartHeader  *rh = ctx->cur_restart_header;
    1738             :     unsigned int channel;
    1739             : 
    1740           0 :     for (channel = 0; channel <= rh->max_channel; channel++) {
    1741           0 :         ChannelParams *cp = &ctx->cur_channel_params[channel];
    1742           0 :         int32_t *sample_buffer = ctx->sample_buffer + channel;
    1743           0 :         int32_t min = INT32_MAX, max = INT32_MIN;
    1744           0 :         int no_filters_used = !cp->filter_params[FIR].order;
    1745           0 :         int average = 0;
    1746           0 :         int offset = 0;
    1747             :         int i;
    1748             : 
    1749             :         /* Determine extremes and average. */
    1750           0 :         for (i = 0; i < dp->blocksize; i++) {
    1751           0 :             int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
    1752           0 :             if (sample < min)
    1753           0 :                 min = sample;
    1754           0 :             if (sample > max)
    1755           0 :                 max = sample;
    1756           0 :             average += sample;
    1757           0 :             sample_buffer += ctx->num_channels;
    1758             :         }
    1759           0 :         average /= dp->blocksize;
    1760             : 
    1761             :         /* If filtering is used, we always set the offset to zero, otherwise
    1762             :          * we search for the offset that minimizes the bitcount. */
    1763           0 :         if (no_filters_used) {
    1764           0 :             no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
    1765           0 :             offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
    1766             :         } else {
    1767           0 :             no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
    1768             :         }
    1769             : 
    1770           0 :         for (i = 1; i < NUM_CODEBOOKS; i++) {
    1771           0 :             BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
    1772             :             int16_t offset_max;
    1773             : 
    1774           0 :             codebook_bits_offset(ctx, channel, i - 1,
    1775             :                                  min, max, offset,
    1776             :                                  &temp_bo);
    1777             : 
    1778           0 :             if (no_filters_used) {
    1779           0 :                 offset_max = temp_bo.max;
    1780             : 
    1781           0 :                 codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
    1782             :                             min, max, &temp_bo, 0);
    1783           0 :                 codebook_bits(ctx, channel, i - 1, offset_max + 1,
    1784             :                             min, max, &temp_bo, 1);
    1785             :             }
    1786             : 
    1787           0 :             ctx->cur_best_offset[channel][i] = temp_bo;
    1788             :         }
    1789             :     }
    1790           0 : }
    1791             : 
    1792             : /****************************************************************************
    1793             :  *************** Functions that process the data in some way ****************
    1794             :  ****************************************************************************/
    1795             : 
    1796             : #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
    1797             : #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
    1798             : 
    1799             : #define MSB_MASK(bits)  (-1u << bits)
    1800             : 
    1801             : /** Applies the filter to the current samples, and saves the residual back
    1802             :  *  into the samples buffer. If the filter is too bad and overflows the
    1803             :  *  maximum amount of bits allowed (16 or 24), the samples buffer is left as is and
    1804             :  *  the function returns -1.
    1805             :  */
    1806           0 : static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
    1807             : {
    1808           0 :     FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
    1809           0 :                                       &ctx->cur_channel_params[channel].filter_params[IIR], };
    1810             :     int32_t *filter_state_buffer[NUM_FILTERS];
    1811           0 :     int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
    1812           0 :     int32_t *sample_buffer = ctx->sample_buffer + channel;
    1813           0 :     unsigned int number_of_samples = ctx->number_of_samples;
    1814           0 :     unsigned int filter_shift = fp[FIR]->shift;
    1815             :     int filter;
    1816             :     int i;
    1817             : 
    1818           0 :     for (i = 0; i < NUM_FILTERS; i++) {
    1819           0 :         unsigned int size = ctx->number_of_samples;
    1820           0 :         filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
    1821           0 :         if (!filter_state_buffer[i]) {
    1822           0 :             av_log(ctx->avctx, AV_LOG_ERROR,
    1823             :                    "Not enough memory for applying filters.\n");
    1824           0 :             return -1;
    1825             :         }
    1826             :     }
    1827             : 
    1828           0 :     for (i = 0; i < 8; i++) {
    1829           0 :         filter_state_buffer[FIR][i] = *sample_buffer;
    1830           0 :         filter_state_buffer[IIR][i] = *sample_buffer;
    1831             : 
    1832           0 :         sample_buffer += ctx->num_channels;
    1833             :     }
    1834             : 
    1835           0 :     for (i = 8; i < number_of_samples; i++) {
    1836           0 :         int32_t sample = *sample_buffer;
    1837             :         unsigned int order;
    1838           0 :         int64_t accum = 0;
    1839             :         int32_t residual;
    1840             : 
    1841           0 :         for (filter = 0; filter < NUM_FILTERS; filter++) {
    1842           0 :             int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
    1843           0 :             for (order = 0; order < fp[filter]->order; order++)
    1844           0 :                 accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
    1845           0 :                          fcoeff[order];
    1846             :         }
    1847             : 
    1848           0 :         accum  >>= filter_shift;
    1849           0 :         residual = sample - (accum & mask);
    1850             : 
    1851           0 :         if (residual < SAMPLE_MIN(ctx->wordlength) || residual > SAMPLE_MAX(ctx->wordlength))
    1852           0 :             return -1;
    1853             : 
    1854           0 :         filter_state_buffer[FIR][i] = sample;
    1855           0 :         filter_state_buffer[IIR][i] = residual;
    1856             : 
    1857           0 :         sample_buffer += ctx->num_channels;
    1858             :     }
    1859             : 
    1860           0 :     sample_buffer = ctx->sample_buffer + channel;
    1861           0 :     for (i = 0; i < number_of_samples; i++) {
    1862           0 :         *sample_buffer = filter_state_buffer[IIR][i];
    1863             : 
    1864           0 :         sample_buffer += ctx->num_channels;
    1865             :     }
    1866             : 
    1867           0 :     for (i = 0; i < NUM_FILTERS; i++) {
    1868           0 :         av_freep(&filter_state_buffer[i]);
    1869             :     }
    1870             : 
    1871           0 :     return 0;
    1872             : }
    1873             : 
    1874           0 : static void apply_filters(MLPEncodeContext *ctx)
    1875             : {
    1876           0 :     RestartHeader *rh = ctx->cur_restart_header;
    1877             :     int channel;
    1878             : 
    1879           0 :     for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
    1880           0 :         if (apply_filter(ctx, channel) < 0) {
    1881             :             /* Filter is horribly wrong.
    1882             :              * Clear filter params and update state. */
    1883           0 :             set_filter_params(ctx, channel, FIR, 1);
    1884           0 :             set_filter_params(ctx, channel, IIR, 1);
    1885           0 :             apply_filter(ctx, channel);
    1886             :         }
    1887             :     }
    1888           0 : }
    1889             : 
    1890             : /** Generates two noise channels worth of data. */
    1891           0 : static void generate_2_noise_channels(MLPEncodeContext *ctx)
    1892             : {
    1893           0 :     int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
    1894           0 :     RestartHeader *rh = ctx->cur_restart_header;
    1895             :     unsigned int i;
    1896           0 :     uint32_t seed = rh->noisegen_seed;
    1897             : 
    1898           0 :     for (i = 0; i < ctx->number_of_samples; i++) {
    1899           0 :         uint16_t seed_shr7 = seed >> 7;
    1900           0 :         *sample_buffer++ = ((int8_t)(seed >> 15)) << rh->noise_shift;
    1901           0 :         *sample_buffer++ = ((int8_t) seed_shr7)   << rh->noise_shift;
    1902             : 
    1903           0 :         seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
    1904             : 
    1905           0 :         sample_buffer += ctx->num_channels - 2;
    1906             :     }
    1907             : 
    1908           0 :     rh->noisegen_seed = seed & ((1 << 24)-1);
    1909           0 : }
    1910             : 
    1911             : /** Rematrixes all channels using chosen coefficients. */
    1912           0 : static void rematrix_channels(MLPEncodeContext *ctx)
    1913             : {
    1914           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    1915           0 :     MatrixParams *mp = &dp->matrix_params;
    1916           0 :     int32_t *sample_buffer = ctx->sample_buffer;
    1917             :     unsigned int mat, i, maxchan;
    1918             : 
    1919           0 :     maxchan = ctx->num_channels;
    1920             : 
    1921           0 :     for (mat = 0; mat < mp->count; mat++) {
    1922           0 :         unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
    1923           0 :         int32_t mask = MSB_MASK(msb_mask_bits);
    1924           0 :         unsigned int outch = mp->outch[mat];
    1925             : 
    1926           0 :         sample_buffer = ctx->sample_buffer;
    1927           0 :         for (i = 0; i < ctx->number_of_samples; i++) {
    1928             :             unsigned int src_ch;
    1929           0 :             int64_t accum = 0;
    1930             : 
    1931           0 :             for (src_ch = 0; src_ch < maxchan; src_ch++) {
    1932           0 :                 int32_t sample = *(sample_buffer + src_ch);
    1933           0 :                 accum += (int64_t) sample * mp->forco[mat][src_ch];
    1934             :             }
    1935           0 :             sample_buffer[outch] = (accum >> 14) & mask;
    1936             : 
    1937           0 :             sample_buffer += ctx->num_channels;
    1938             :         }
    1939             :     }
    1940           0 : }
    1941             : 
    1942             : /****************************************************************************
    1943             :  **** Functions that deal with determining the best parameters and output ***
    1944             :  ****************************************************************************/
    1945             : 
    1946             : typedef struct {
    1947             :     char    path[MAJOR_HEADER_INTERVAL + 3];
    1948             :     int     bitcount;
    1949             : } PathCounter;
    1950             : 
    1951             : static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
    1952             : 
    1953             : #define ZERO_PATH               '0'
    1954             : #define CODEBOOK_CHANGE_BITS    21
    1955             : 
    1956           0 : static void clear_path_counter(PathCounter *path_counter)
    1957             : {
    1958             :     unsigned int i;
    1959             : 
    1960           0 :     for (i = 0; i < NUM_CODEBOOKS + 1; i++) {
    1961           0 :         path_counter[i].path[0]  = ZERO_PATH;
    1962           0 :         path_counter[i].path[1]  =      0x00;
    1963           0 :         path_counter[i].bitcount =         0;
    1964             :     }
    1965           0 : }
    1966             : 
    1967           0 : static int compare_best_offset(BestOffset *prev, BestOffset *cur)
    1968             : {
    1969           0 :     if (prev->lsb_bits != cur->lsb_bits)
    1970           0 :         return 1;
    1971             : 
    1972           0 :     return 0;
    1973             : }
    1974             : 
    1975           0 : static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
    1976             :                                    PathCounter *src, int cur_codebook)
    1977             : {
    1978           0 :     BestOffset *cur_bo, *prev_bo = restart_best_offset;
    1979           0 :     int bitcount = src->bitcount;
    1980           0 :     char *path = src->path + 1;
    1981             :     int prev_codebook;
    1982             :     int i;
    1983             : 
    1984           0 :     for (i = 0; path[i]; i++)
    1985           0 :         prev_bo = ctx->best_offset[i][channel];
    1986             : 
    1987           0 :     prev_codebook = path[i - 1] - ZERO_PATH;
    1988             : 
    1989           0 :     cur_bo = ctx->best_offset[i][channel];
    1990             : 
    1991           0 :     bitcount += cur_bo[cur_codebook].bitcount;
    1992             : 
    1993           0 :     if (prev_codebook != cur_codebook ||
    1994           0 :         compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
    1995           0 :         bitcount += CODEBOOK_CHANGE_BITS;
    1996             : 
    1997           0 :     return bitcount;
    1998             : }
    1999             : 
    2000           0 : static void set_best_codebook(MLPEncodeContext *ctx)
    2001             : {
    2002           0 :     DecodingParams *dp = ctx->cur_decoding_params;
    2003           0 :     RestartHeader *rh = ctx->cur_restart_header;
    2004             :     unsigned int channel;
    2005             : 
    2006           0 :     for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
    2007           0 :         BestOffset *cur_bo, *prev_bo = restart_best_offset;
    2008             :         PathCounter path_counter[NUM_CODEBOOKS + 1];
    2009             :         unsigned int best_codebook;
    2010             :         unsigned int index;
    2011             :         char *best_path;
    2012             : 
    2013           0 :         clear_path_counter(path_counter);
    2014             : 
    2015           0 :         for (index = 0; index < ctx->number_of_subblocks; index++) {
    2016           0 :             unsigned int best_bitcount = INT_MAX;
    2017             :             unsigned int codebook;
    2018             : 
    2019           0 :             cur_bo = ctx->best_offset[index][channel];
    2020             : 
    2021           0 :             for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
    2022           0 :                 int prev_best_bitcount = INT_MAX;
    2023             :                 int last_best;
    2024             : 
    2025           0 :                 for (last_best = 0; last_best < 2; last_best++) {
    2026           0 :                     PathCounter *dst_path = &path_counter[codebook];
    2027             :                     PathCounter *src_path;
    2028             :                     int  temp_bitcount;
    2029             : 
    2030             :                     /* First test last path with same headers,
    2031             :                      * then with last best. */
    2032           0 :                     if (last_best) {
    2033           0 :                         src_path = &path_counter[NUM_CODEBOOKS];
    2034             :                     } else {
    2035           0 :                         if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
    2036           0 :                             continue;
    2037             :                         else
    2038           0 :                             src_path = &path_counter[codebook];
    2039             :                     }
    2040             : 
    2041           0 :                     temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
    2042             : 
    2043           0 :                     if (temp_bitcount < best_bitcount) {
    2044           0 :                         best_bitcount = temp_bitcount;
    2045           0 :                         best_codebook = codebook;
    2046             :                     }
    2047             : 
    2048           0 :                     if (temp_bitcount < prev_best_bitcount) {
    2049           0 :                         prev_best_bitcount = temp_bitcount;
    2050           0 :                         if (src_path != dst_path)
    2051           0 :                             memcpy(dst_path, src_path, sizeof(PathCounter));
    2052           0 :                         av_strlcat(dst_path->path, path_counter_codebook[codebook], sizeof(dst_path->path));
    2053           0 :                         dst_path->bitcount = temp_bitcount;
    2054             :                     }
    2055             :                 }
    2056             :             }
    2057             : 
    2058           0 :             prev_bo = cur_bo;
    2059             : 
    2060           0 :             memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
    2061             :         }
    2062             : 
    2063           0 :         best_path = path_counter[NUM_CODEBOOKS].path + 1;
    2064             : 
    2065             :         /* Update context. */
    2066           0 :         for (index = 0; index < ctx->number_of_subblocks; index++) {
    2067           0 :             ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
    2068             : 
    2069           0 :             best_codebook = *best_path++ - ZERO_PATH;
    2070           0 :             cur_bo = &ctx->best_offset[index][channel][best_codebook];
    2071             : 
    2072           0 :             cp->huff_offset = cur_bo->offset;
    2073           0 :             cp->huff_lsbs   = cur_bo->lsb_bits + dp->quant_step_size[channel];
    2074           0 :             cp->codebook    = best_codebook;
    2075             :         }
    2076             :     }
    2077           0 : }
    2078             : 
    2079             : /** Analyzes all collected bitcounts and selects the best parameters for each
    2080             :  *  individual access unit.
    2081             :  *  TODO This is just a stub!
    2082             :  */
    2083           0 : static void set_major_params(MLPEncodeContext *ctx)
    2084             : {
    2085           0 :     RestartHeader *rh = ctx->cur_restart_header;
    2086             :     unsigned int index;
    2087             :     unsigned int substr;
    2088           0 :     uint8_t max_huff_lsbs = 0;
    2089           0 :     uint8_t max_output_bits = 0;
    2090             : 
    2091           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    2092           0 :         DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
    2093           0 :                                  (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
    2094           0 :                                  (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
    2095             : 
    2096           0 :         ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
    2097           0 :                                 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
    2098           0 :                                 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
    2099             :         unsigned int channel;
    2100           0 :         for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
    2101           0 :             memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
    2102           0 :             for (channel = 0; channel < ctx->avctx->channels; channel++) {
    2103           0 :                 uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
    2104           0 :                 if (max_huff_lsbs < huff_lsbs)
    2105           0 :                     max_huff_lsbs = huff_lsbs;
    2106           0 :                 memcpy(&ctx->major_channel_params[index][channel],
    2107           0 :                        (seq_cp + index*(ctx->avctx->channels) + channel),
    2108             :                        sizeof(ChannelParams));
    2109             :             }
    2110             :         }
    2111             :     }
    2112             : 
    2113           0 :     rh->max_huff_lsbs = max_huff_lsbs;
    2114             : 
    2115           0 :     for (index = 0; index < ctx->number_of_frames; index++)
    2116           0 :         if (max_output_bits < ctx->max_output_bits[index])
    2117           0 :             max_output_bits = ctx->max_output_bits[index];
    2118           0 :     rh->max_output_bits = max_output_bits;
    2119             : 
    2120           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    2121             : 
    2122           0 :         ctx->cur_restart_header = &ctx->restart_header[substr];
    2123             : 
    2124           0 :         ctx->prev_decoding_params = &restart_decoding_params[substr];
    2125           0 :         ctx->prev_channel_params = restart_channel_params;
    2126             : 
    2127           0 :         for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
    2128           0 :                 ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
    2129           0 :                 ctx->cur_channel_params = ctx->major_channel_params[index];
    2130             : 
    2131           0 :                 ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
    2132             : 
    2133           0 :                 ctx->prev_decoding_params = ctx->cur_decoding_params;
    2134           0 :                 ctx->prev_channel_params = ctx->cur_channel_params;
    2135             :         }
    2136             :     }
    2137             : 
    2138           0 :     ctx->major_number_of_subblocks = ctx->number_of_subblocks;
    2139           0 :     ctx->major_filter_state_subblock = 1;
    2140           0 :     ctx->major_cur_subblock_index = 0;
    2141           0 : }
    2142             : 
    2143           0 : static void analyze_sample_buffer(MLPEncodeContext *ctx)
    2144             : {
    2145           0 :     ChannelParams *seq_cp = ctx->seq_channel_params;
    2146           0 :     DecodingParams *seq_dp = ctx->seq_decoding_params;
    2147             :     unsigned int index;
    2148             :     unsigned int substr;
    2149             : 
    2150           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    2151             : 
    2152           0 :         ctx->cur_restart_header = &ctx->restart_header[substr];
    2153           0 :         ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
    2154           0 :         ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
    2155             : 
    2156           0 :         determine_quant_step_size(ctx);
    2157           0 :         generate_2_noise_channels(ctx);
    2158           0 :         lossless_matrix_coeffs   (ctx);
    2159           0 :         rematrix_channels        (ctx);
    2160           0 :         determine_filters        (ctx);
    2161           0 :         apply_filters            (ctx);
    2162             : 
    2163           0 :         copy_restart_frame_params(ctx, substr);
    2164             : 
    2165             :         /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
    2166             :          * decoding_params[0] is for the filter state subblock.
    2167             :          */
    2168           0 :         for (index = 0; index < ctx->number_of_frames; index++) {
    2169           0 :             DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
    2170           0 :             dp->blocksize = ctx->frame_size[index];
    2171             :         }
    2172             :         /* The official encoder seems to always encode a filter state subblock
    2173             :          * even if there are no filters. TODO check if it is possible to skip
    2174             :          * the filter state subblock for no filters.
    2175             :          */
    2176           0 :         (seq_dp + substr)->blocksize  = 8;
    2177           0 :         (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
    2178             : 
    2179           0 :         for (index = 0; index < ctx->number_of_subblocks; index++) {
    2180           0 :                 ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
    2181           0 :                 ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
    2182           0 :                 ctx->cur_best_offset = ctx->best_offset[index];
    2183           0 :                 determine_bits(ctx);
    2184           0 :                 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
    2185             :         }
    2186             : 
    2187           0 :         set_best_codebook(ctx);
    2188             :     }
    2189           0 : }
    2190             : 
    2191           0 : static void process_major_frame(MLPEncodeContext *ctx)
    2192             : {
    2193             :     unsigned int substr;
    2194             : 
    2195           0 :     ctx->sample_buffer = ctx->major_inout_buffer;
    2196             : 
    2197           0 :     ctx->starting_frame_index = 0;
    2198           0 :     ctx->number_of_frames = ctx->major_number_of_frames;
    2199           0 :     ctx->number_of_samples = ctx->major_frame_size;
    2200             : 
    2201           0 :     for (substr = 0; substr < ctx->num_substreams; substr++) {
    2202           0 :         RestartHeader *rh = ctx->cur_restart_header;
    2203             :         unsigned int channel;
    2204             : 
    2205           0 :         ctx->cur_restart_header = &ctx->restart_header[substr];
    2206             : 
    2207           0 :         ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
    2208           0 :         ctx->cur_channel_params = ctx->major_channel_params[1];
    2209             : 
    2210           0 :         generate_2_noise_channels(ctx);
    2211           0 :         rematrix_channels        (ctx);
    2212             : 
    2213           0 :         for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
    2214           0 :             apply_filter(ctx, channel);
    2215             :     }
    2216           0 : }
    2217             : 
    2218             : /****************************************************************************/
    2219             : 
    2220           0 : static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
    2221             :                             const AVFrame *frame, int *got_packet)
    2222             : {
    2223           0 :     MLPEncodeContext *ctx = avctx->priv_data;
    2224           0 :     unsigned int bytes_written = 0;
    2225             :     int restart_frame, ret;
    2226             :     uint8_t *data;
    2227             : 
    2228           0 :     if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
    2229           0 :         return ret;
    2230             : 
    2231           0 :     if (!frame)
    2232           0 :         return 1;
    2233             : 
    2234             :     /* add current frame to queue */
    2235           0 :     if (frame) {
    2236           0 :         if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
    2237           0 :             return ret;
    2238             :     }
    2239             : 
    2240           0 :     data = frame->data[0];
    2241             : 
    2242           0 :     ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
    2243             : 
    2244           0 :     ctx->inout_buffer = ctx->major_inout_buffer
    2245           0 :                       + ctx->frame_index * ctx->one_sample_buffer_size;
    2246             : 
    2247           0 :     if (ctx->last_frame == ctx->inout_buffer) {
    2248           0 :         return 0;
    2249             :     }
    2250             : 
    2251           0 :     ctx->sample_buffer = ctx->major_scratch_buffer
    2252           0 :                        + ctx->frame_index * ctx->one_sample_buffer_size;
    2253             : 
    2254           0 :     ctx->write_buffer = ctx->inout_buffer;
    2255             : 
    2256           0 :     if (avctx->frame_number < ctx->max_restart_interval) {
    2257           0 :         if (data) {
    2258           0 :             goto input_and_return;
    2259             :         } else {
    2260             :             /* There are less frames than the requested major header interval.
    2261             :              * Update the context to reflect this.
    2262             :              */
    2263           0 :             ctx->max_restart_interval = avctx->frame_number;
    2264           0 :             ctx->frame_index = 0;
    2265             : 
    2266           0 :             ctx->sample_buffer = ctx->major_scratch_buffer;
    2267           0 :             ctx->inout_buffer = ctx->major_inout_buffer;
    2268             :         }
    2269             :     }
    2270             : 
    2271           0 :     if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
    2272           0 :         av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
    2273           0 :                ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
    2274           0 :         return -1;
    2275             :     }
    2276             : 
    2277           0 :     restart_frame = !ctx->frame_index;
    2278             : 
    2279           0 :     if (restart_frame) {
    2280           0 :         set_major_params(ctx);
    2281           0 :         if (ctx->min_restart_interval != ctx->max_restart_interval)
    2282           0 :         process_major_frame(ctx);
    2283             :     }
    2284             : 
    2285           0 :     if (ctx->min_restart_interval == ctx->max_restart_interval)
    2286           0 :         ctx->write_buffer = ctx->sample_buffer;
    2287             : 
    2288           0 :     bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
    2289             : 
    2290           0 :     ctx->timestamp += ctx->frame_size[ctx->frame_index];
    2291           0 :     ctx->dts       += ctx->frame_size[ctx->frame_index];
    2292             : 
    2293           0 : input_and_return:
    2294             : 
    2295           0 :     if (data) {
    2296           0 :         ctx->frame_size[ctx->frame_index] = avctx->frame_size;
    2297           0 :         ctx->next_major_frame_size += avctx->frame_size;
    2298           0 :         ctx->next_major_number_of_frames++;
    2299           0 :         input_data(ctx, data);
    2300           0 :     } else if (!ctx->last_frame) {
    2301           0 :         ctx->last_frame = ctx->inout_buffer;
    2302             :     }
    2303             : 
    2304           0 :     restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
    2305             : 
    2306           0 :     if (!restart_frame) {
    2307             :         int seq_index;
    2308             : 
    2309           0 :         for (seq_index = 0;
    2310           0 :              seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
    2311           0 :              seq_index++) {
    2312           0 :             unsigned int number_of_samples = 0;
    2313             :             unsigned int index;
    2314             : 
    2315           0 :             ctx->sample_buffer = ctx->major_scratch_buffer;
    2316           0 :             ctx->inout_buffer = ctx->major_inout_buffer;
    2317           0 :             ctx->seq_index = seq_index;
    2318             : 
    2319           0 :             ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
    2320           0 :                                       - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
    2321           0 :             ctx->number_of_frames = ctx->next_major_number_of_frames;
    2322           0 :             ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
    2323             : 
    2324           0 :             ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
    2325           0 :                                       (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
    2326           0 :                                       (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
    2327             : 
    2328           0 :             ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
    2329           0 :                                        (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
    2330           0 :                                        (ctx->seq_offset[seq_index])*(ctx->num_substreams);
    2331             : 
    2332           0 :             for (index = 0; index < ctx->number_of_frames; index++) {
    2333           0 :                 number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
    2334             :             }
    2335           0 :             ctx->number_of_samples = number_of_samples;
    2336             : 
    2337           0 :             for (index = 0; index < ctx->seq_size[seq_index]; index++) {
    2338           0 :                 clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
    2339           0 :                 default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
    2340             :             }
    2341             : 
    2342           0 :             input_to_sample_buffer(ctx);
    2343             : 
    2344           0 :             analyze_sample_buffer(ctx);
    2345             :         }
    2346             : 
    2347           0 :         if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
    2348           0 :             ctx->major_frame_size = ctx->next_major_frame_size;
    2349           0 :             ctx->next_major_frame_size = 0;
    2350           0 :             ctx->major_number_of_frames = ctx->next_major_number_of_frames;
    2351           0 :             ctx->next_major_number_of_frames = 0;
    2352             : 
    2353           0 :             if (!ctx->major_frame_size)
    2354           0 :                 goto no_data_left;
    2355             :         }
    2356             :     }
    2357             : 
    2358           0 : no_data_left:
    2359             : 
    2360           0 :     ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
    2361             :                        &avpkt->duration);
    2362           0 :     avpkt->size = bytes_written;
    2363           0 :     *got_packet = 1;
    2364           0 :     return 0;
    2365             : }
    2366             : 
    2367           0 : static av_cold int mlp_encode_close(AVCodecContext *avctx)
    2368             : {
    2369           0 :     MLPEncodeContext *ctx = avctx->priv_data;
    2370             : 
    2371           0 :     ff_lpc_end(&ctx->lpc_ctx);
    2372             : 
    2373           0 :     av_freep(&ctx->lossless_check_data);
    2374           0 :     av_freep(&ctx->major_scratch_buffer);
    2375           0 :     av_freep(&ctx->major_inout_buffer);
    2376           0 :     av_freep(&ctx->lpc_sample_buffer);
    2377           0 :     av_freep(&ctx->decoding_params);
    2378           0 :     av_freep(&ctx->channel_params);
    2379           0 :     av_freep(&ctx->frame_size);
    2380           0 :     ff_af_queue_close(&ctx->afq);
    2381             : 
    2382           0 :     return 0;
    2383             : }
    2384             : 
    2385             : #if CONFIG_MLP_ENCODER
    2386             : AVCodec ff_mlp_encoder = {
    2387             :     .name                   ="mlp",
    2388             :     .long_name              = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
    2389             :     .type                   = AVMEDIA_TYPE_AUDIO,
    2390             :     .id                     = AV_CODEC_ID_MLP,
    2391             :     .priv_data_size         = sizeof(MLPEncodeContext),
    2392             :     .init                   = mlp_encode_init,
    2393             :     .encode2                = mlp_encode_frame,
    2394             :     .close                  = mlp_encode_close,
    2395             :     .capabilities           = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
    2396             :     .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
    2397             :     .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
    2398             :     .channel_layouts        = ff_mlp_channel_layouts,
    2399             : };
    2400             : #endif
    2401             : #if CONFIG_TRUEHD_ENCODER
    2402             : AVCodec ff_truehd_encoder = {
    2403             :     .name                   ="truehd",
    2404             :     .long_name              = NULL_IF_CONFIG_SMALL("TrueHD"),
    2405             :     .type                   = AVMEDIA_TYPE_AUDIO,
    2406             :     .id                     = AV_CODEC_ID_TRUEHD,
    2407             :     .priv_data_size         = sizeof(MLPEncodeContext),
    2408             :     .init                   = mlp_encode_init,
    2409             :     .encode2                = mlp_encode_frame,
    2410             :     .close                  = mlp_encode_close,
    2411             :     .capabilities           = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
    2412             :     .sample_fmts            = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
    2413             :     .supported_samplerates  = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
    2414             :     .channel_layouts        = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},
    2415             : };
    2416             : #endif

Generated by: LCOV version 1.13