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