3 * Copyright (c) 2008 Ramiro Polla
4 * Copyright (c) 2016-2019 Jai Luthra
6 * This file is part of FFmpeg.
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.
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.
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
26 #include "audio_frame_queue.h"
27 #include "libavutil/crc.h"
28 #include "libavutil/avstring.h"
29 #include "libavutil/samplefmt.h"
33 #define MAJOR_HEADER_INTERVAL 16
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
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.
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).
48 int data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
50 int32_t lossless_check_data; ///< XOR of all output samples
52 uint8_t max_huff_lsbs; ///< largest huff_lsbs
53 uint8_t max_output_bits; ///< largest output bit-depth
57 uint8_t count; ///< number of matrices to apply
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
64 int8_t shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
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,
76 PARAM_HUFFOFFSET = 1 << 1,
77 PARAM_PRESENT = 1 << 0,
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
84 MatrixParams matrix_params;
86 uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
89 typedef struct BestOffset {
97 #define HUFF_OFFSET_MIN (-16384)
98 #define HUFF_OFFSET_MAX ( 16383)
100 /** Number of possible codebooks (counting "no codebooks") */
101 #define NUM_CODEBOOKS 4
104 AVCodecContext *avctx;
106 int num_substreams; ///< Number of substreams contained within this stream.
108 int num_channels; /**< Number of channels in major_scratch_buffer.
109 * Normal channels + noise channels. */
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
115 int flags; ///< major sync info flags
117 /* channel_meaning */
121 int channel_occupancy;
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.
131 int32_t *lpc_sample_buffer;
133 unsigned int major_number_of_frames;
134 unsigned int next_major_number_of_frames;
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.
139 int32_t *lossless_check_data; ///< Array with lossless_check_data for each access unit.
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.
145 unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
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.
151 uint16_t timestamp; ///< Timestamp of current access unit.
152 uint16_t dts; ///< Decoding timestamp of current access unit.
154 uint8_t channel_arrangement; ///< channel arrangement for MLP streams
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
160 unsigned int seq_size [MAJOR_HEADER_INTERVAL];
161 unsigned int seq_offset[MAJOR_HEADER_INTERVAL];
162 unsigned int sequence_size;
164 ChannelParams *channel_params;
166 BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
168 DecodingParams *decoding_params;
169 RestartHeader restart_header [MAX_SUBSTREAMS];
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.
175 unsigned int major_cur_subblock_index;
176 unsigned int major_filter_state_subblock;
177 unsigned int major_number_of_subblocks;
179 BestOffset (*cur_best_offset)[NUM_CODEBOOKS];
180 ChannelParams *cur_channel_params;
181 DecodingParams *cur_decoding_params;
182 RestartHeader *cur_restart_header;
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.
193 ChannelParams *prev_channel_params;
194 DecodingParams *prev_decoding_params;
196 ChannelParams *seq_channel_params;
197 DecodingParams *seq_decoding_params;
199 unsigned int max_codebook_search;
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}};
208 #define SYNC_MAJOR 0xf8726f
209 #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
211 #define SYNC_MLP 0xbb
212 #define SYNC_TRUEHD 0xba
214 /* must be set for DVD-A */
215 #define FLAGS_DVDA 0x4000
216 /* FIFO delay must be constant */
217 #define FLAGS_CONST 0x8000
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
224 /****************************************************************************
225 ************ Functions that copy, clear, or compare parameters *************
226 ****************************************************************************/
228 /** Compares two FilterParams structures and returns 1 if anything has
229 * changed. Returns 0 if they are both equal.
231 static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
233 const FilterParams *prev = &prev_cp->filter_params[filter];
234 const FilterParams *fp = &cp->filter_params[filter];
237 if (prev->order != fp->order)
243 if (prev->shift != fp->shift)
246 for (i = 0; i < fp->order; i++)
247 if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
253 /** Compare two primitive matrices and returns 1 if anything has changed.
254 * Returns 0 if they are both equal.
256 static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
258 RestartHeader *rh = ctx->cur_restart_header;
259 unsigned int channel, mat;
261 if (prev->count != mp->count)
267 for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
268 if (prev->fbits[channel] != mp->fbits[channel])
271 for (mat = 0; mat < mp->count; mat++) {
272 if (prev->outch[mat] != mp->outch[mat])
275 for (channel = 0; channel < ctx->num_channels; channel++)
276 if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
283 /** Compares two DecodingParams and ChannelParams structures to decide if a
284 * new decoding params header has to be written.
286 static int compare_decoding_params(MLPEncodeContext *ctx)
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;
296 if (prev->param_presence_flags != dp->param_presence_flags)
297 retval |= PARAM_PRESENCE_FLAGS;
299 if (prev->blocksize != dp->blocksize)
300 retval |= PARAM_BLOCKSIZE;
302 if (compare_matrix_params(ctx, prev_mp, mp))
303 retval |= PARAM_MATRIX;
305 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
306 if (prev_mp->shift[ch] != mp->shift[ch]) {
307 retval |= PARAM_OUTSHIFT;
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;
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];
321 if (!(retval & PARAM_FIR) &&
322 compare_filter_params(prev_cp, cp, FIR))
325 if (!(retval & PARAM_IIR) &&
326 compare_filter_params(prev_cp, cp, IIR))
329 if (prev_cp->huff_offset != cp->huff_offset)
330 retval |= PARAM_HUFFOFFSET;
332 if (prev_cp->codebook != cp->codebook ||
333 prev_cp->huff_lsbs != cp->huff_lsbs )
340 static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
342 FilterParams *dst = &dst_cp->filter_params[filter];
343 FilterParams *src = &src_cp->filter_params[filter];
346 dst->order = src->order;
349 dst->shift = src->shift;
351 dst->coeff_shift = src->coeff_shift;
352 dst->coeff_bits = src->coeff_bits;
355 for (order = 0; order < dst->order; order++)
356 dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
359 static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
361 dst->count = src->count;
364 unsigned int channel, count;
366 for (channel = 0; channel < MAX_CHANNELS; channel++) {
368 dst->fbits[channel] = src->fbits[channel];
369 dst->shift[channel] = src->shift[channel];
371 for (count = 0; count < MAX_MATRICES; count++)
372 dst->coeff[count][channel] = src->coeff[count][channel];
375 for (count = 0; count < MAX_MATRICES; count++)
376 dst->outch[count] = src->outch[count];
380 static void copy_restart_frame_params(MLPEncodeContext *ctx,
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;
389 copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
391 for (channel = 0; channel < ctx->avctx->channels; channel++) {
392 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
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];
399 for (filter = 0; filter < NUM_FILTERS; filter++)
400 copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
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])
410 for (substr = 0; substr < ctx->num_substreams; substr++) {
411 DecodingParams *dp = &decoding_params[substr];
413 dp->param_presence_flags = 0xff;
416 memset(&dp->matrix_params , 0, sizeof(MatrixParams ));
417 memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
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])
424 unsigned int channel;
426 for (channel = 0; channel < ctx->avctx->channels; channel++) {
427 ChannelParams *cp = &channel_params[channel];
429 memset(&cp->filter_params, 0, sizeof(cp->filter_params));
431 /* Default audio coding is 24-bit raw PCM. */
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])
444 clear_decoding_params(ctx, decoding_params);
446 for (substr = 0; substr < ctx->num_substreams; substr++) {
447 DecodingParams *dp = &decoding_params[substr];
448 uint8_t param_presence_flags = 0;
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;
459 dp->param_presence_flags = param_presence_flags;
463 /****************************************************************************/
465 /** Calculates the smallest number of bits it takes to encode a given signed
466 * value in two's complement.
468 static int inline number_sbits(int number)
473 return av_log2(FFABS(number)) + 1 + !!number;
482 static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
484 return ((peak_bitrate << 4) - 8) / sample_rate;
487 static av_cold int mlp_encode_init(AVCodecContext *avctx)
489 MLPEncodeContext *ctx = avctx->priv_data;
490 unsigned int substr, index;
491 unsigned int sum = 0;
497 switch (avctx->sample_rate) {
499 avctx->frame_size = 40 << 0;
500 ctx->coded_sample_rate[0] = 0x08 + 0;
504 avctx->frame_size = 40 << 1;
505 ctx->coded_sample_rate[0] = 0x08 + 1;
509 ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
510 avctx->frame_size = 40 << 2;
511 ctx->coded_sample_rate[0] = 0x08 + 2;
515 avctx->frame_size = 40 << 0;
516 ctx->coded_sample_rate[0] = 0x00 + 0;
520 avctx->frame_size = 40 << 1;
521 ctx->coded_sample_rate[0] = 0x00 + 1;
525 ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
526 avctx->frame_size = 40 << 2;
527 ctx->coded_sample_rate[0] = 0x00 + 2;
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);
536 ctx->coded_sample_rate[1] = -1 & 0xf;
538 /* TODO Keep count of bitrate and calculate real value. */
539 ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
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");
547 ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
548 if (avctx->channels <= 2) {
549 ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
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;
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;
565 av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
566 "Only 16- and 24-bit samples are supported.\n");
569 ctx->coded_sample_fmt[1] = -1 & 0xf;
571 ctx->dts = -avctx->frame_size;
573 ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
574 ctx->one_sample_buffer_size = avctx->frame_size
576 /* TODO Let user pass major header interval as parameter. */
577 ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
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;
583 /* TODO Let user pass parameters for LPC filter. */
585 size = avctx->frame_size * ctx->max_restart_interval;
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);
594 size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
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);
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);
612 ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
614 if (ctx->avctx->codec_id == AV_CODEC_ID_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;
640 av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
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 ;
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;
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;
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;
668 av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
672 ctx->channel_occupancy = 0;
673 ctx->summary_info = 0;
676 size = sizeof(unsigned int) * ctx->max_restart_interval;
678 ctx->frame_size = av_malloc(size);
679 if (!ctx->frame_size)
680 return AVERROR(ENOMEM);
682 ctx->max_output_bits = av_malloc(size);
683 if (!ctx->max_output_bits)
684 return AVERROR(ENOMEM);
686 size = sizeof(int32_t)
687 * ctx->num_substreams * ctx->max_restart_interval;
689 ctx->lossless_check_data = av_malloc(size);
690 if (!ctx->lossless_check_data)
691 return AVERROR(ENOMEM);
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];
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);
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);
717 for (substr = 0; substr < ctx->num_substreams; substr++) {
718 RestartHeader *rh = &ctx->restart_header [substr];
720 /* TODO see if noisegen_seed is really worth it. */
721 rh->noisegen_seed = 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;
729 clear_channel_params(ctx, restart_channel_params);
730 clear_decoding_params(ctx, restart_decoding_params);
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");
739 ff_af_queue_init(avctx, &ctx->afq);
744 /****************************************************************************
745 ****************** Functions that write to the bitstream *******************
746 ****************************************************************************/
748 /** Writes a major sync header to the bitstream. */
749 static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
753 init_put_bits(&pb, buf, buf_size);
755 put_bits(&pb, 24, SYNC_MAJOR );
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 );
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 */
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 );
801 AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
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
808 static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
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);
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 */
832 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
835 /* Data must be flushed for the checksum to be correct. */
837 flush_put_bits(&tmpb);
839 checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
841 put_bits(pb, 8, checksum);
844 /** Writes matrix params for all primitive matrices to the bitstream. */
845 static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
847 DecodingParams *dp = ctx->cur_decoding_params;
848 MatrixParams *mp = &dp->matrix_params;
851 put_bits(pb, 4, mp->count);
853 for (mat = 0; mat < mp->count; mat++) {
854 unsigned int channel;
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 */
860 for (channel = 0; channel < ctx->num_channels; channel++) {
861 int32_t coeff = mp->coeff[mat][channel];
866 coeff >>= 14 - mp->fbits[mat];
868 put_sbits(pb, mp->fbits[mat] + 2, coeff);
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)
880 FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
882 put_bits(pb, 4, fp->order);
886 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
888 put_bits(pb, 4, fp->shift );
889 put_bits(pb, 5, fp->coeff_bits );
890 put_bits(pb, 3, fp->coeff_shift);
892 for (i = 0; i < fp->order; i++) {
893 put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
896 /* TODO state data for IIR filter. */
901 /** Writes decoding parameters to the bitstream. These change very often,
902 * usually at almost every frame.
904 static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
907 DecodingParams *dp = ctx->cur_decoding_params;
908 RestartHeader *rh = ctx->cur_restart_header;
909 MatrixParams *mp = &dp->matrix_params;
912 if (dp->param_presence_flags != PARAMS_DEFAULT &&
913 params_changed & PARAM_PRESENCE_FLAGS) {
915 put_bits(pb, 8, dp->param_presence_flags);
920 if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
921 if (params_changed & PARAM_BLOCKSIZE) {
923 put_bits(pb, 9, dp->blocksize);
929 if (dp->param_presence_flags & PARAM_MATRIX) {
930 if (params_changed & PARAM_MATRIX) {
932 write_matrix_params(ctx, pb);
938 if (dp->param_presence_flags & PARAM_OUTSHIFT) {
939 if (params_changed & PARAM_OUTSHIFT) {
941 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
942 put_sbits(pb, 4, mp->shift[ch]);
948 if (dp->param_presence_flags & PARAM_QUANTSTEP) {
949 if (params_changed & PARAM_QUANTSTEP) {
951 for (ch = 0; ch <= rh->max_channel; ch++)
952 put_bits(pb, 4, dp->quant_step_size[ch]);
958 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
959 ChannelParams *cp = &ctx->cur_channel_params[ch];
961 if (dp->param_presence_flags & 0xF) {
964 if (dp->param_presence_flags & PARAM_FIR) {
965 if (params_changed & PARAM_FIR) {
967 write_filter_params(ctx, pb, ch, FIR);
973 if (dp->param_presence_flags & PARAM_IIR) {
974 if (params_changed & PARAM_IIR) {
976 write_filter_params(ctx, pb, ch, IIR);
982 if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
983 if (params_changed & PARAM_HUFFOFFSET) {
985 put_sbits(pb, 15, cp->huff_offset);
990 if (cp->codebook > 0 && cp->huff_lsbs > 24) {
991 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs\n");
994 put_bits(pb, 2, cp->codebook );
995 put_bits(pb, 5, cp->huff_lsbs);
1002 /** Writes the residuals to the bitstream. That is, the VLC codes from the
1003 * codebooks (if any is used), and then the residual.
1005 static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
1007 DecodingParams *dp = ctx->cur_decoding_params;
1008 RestartHeader *rh = ctx->cur_restart_header;
1009 int32_t *sample_buffer = ctx->write_buffer;
1010 int32_t sign_huff_offset[MAX_CHANNELS];
1011 int codebook_index [MAX_CHANNELS];
1012 int lsb_bits [MAX_CHANNELS];
1015 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1016 ChannelParams *cp = &ctx->cur_channel_params[ch];
1019 lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
1020 codebook_index [ch] = cp->codebook - 1;
1021 sign_huff_offset[ch] = cp->huff_offset;
1023 sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
1025 if (cp->codebook > 0)
1026 sign_huff_offset[ch] -= 7 << lsb_bits[ch];
1028 /* Unsign if needed. */
1029 if (sign_shift >= 0)
1030 sign_huff_offset[ch] -= 1 << sign_shift;
1033 for (i = 0; i < dp->blocksize; i++) {
1034 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1035 int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
1036 sample -= sign_huff_offset[ch];
1038 if (codebook_index[ch] >= 0) {
1039 int vlc = sample >> lsb_bits[ch];
1040 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1041 ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
1044 put_sbits(pb, lsb_bits[ch], sample);
1046 sample_buffer += 2; /* noise channels */
1049 ctx->write_buffer = sample_buffer;
1052 /** Writes the substreams data to the bitstream. */
1053 static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
1055 uint16_t substream_data_len[MAX_SUBSTREAMS])
1057 int32_t *lossless_check_data = ctx->lossless_check_data;
1058 unsigned int substr;
1061 lossless_check_data += ctx->frame_index * ctx->num_substreams;
1063 for (substr = 0; substr < ctx->num_substreams; substr++) {
1064 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
1065 unsigned int num_subblocks = ctx->major_filter_state_subblock;
1066 unsigned int subblock;
1067 RestartHeader *rh = &ctx->restart_header [substr];
1068 int substr_restart_frame = restart_frame;
1069 uint8_t parity, checksum;
1070 PutBitContext pb, tmpb;
1073 ctx->cur_restart_header = rh;
1075 init_put_bits(&pb, buf, buf_size);
1077 for (subblock = 0; subblock <= num_subblocks; subblock++) {
1078 unsigned int subblock_index;
1080 subblock_index = cur_subblock_index++;
1082 ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
1083 ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
1085 params_changed = ctx->major_params_changed[subblock_index][substr];
1087 if (substr_restart_frame || params_changed) {
1088 put_bits(&pb, 1, 1);
1090 if (substr_restart_frame) {
1091 put_bits(&pb, 1, 1);
1093 write_restart_header(ctx, &pb);
1094 rh->lossless_check_data = 0;
1096 put_bits(&pb, 1, 0);
1099 write_decoding_params(ctx, &pb, params_changed);
1101 put_bits(&pb, 1, 0);
1104 write_block_data(ctx, &pb);
1106 put_bits(&pb, 1, !substr_restart_frame);
1108 substr_restart_frame = 0;
1111 put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
1113 rh->lossless_check_data ^= *lossless_check_data++;
1115 if (ctx->last_frame == ctx->inout_buffer) {
1116 /* TODO find a sample and implement shorten_by. */
1117 put_bits(&pb, 32, END_OF_STREAM);
1120 /* Data must be flushed for the checksum and parity to be correct. */
1122 flush_put_bits(&tmpb);
1124 parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
1125 checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3);
1127 put_bits(&pb, 8, parity );
1128 put_bits(&pb, 8, checksum);
1130 flush_put_bits(&pb);
1132 end += put_bits_count(&pb) >> 3;
1133 substream_data_len[substr] = end;
1135 buf += put_bits_count(&pb) >> 3;
1138 ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
1139 ctx->major_filter_state_subblock = 0;
1144 /** Writes the access unit and substream headers to the bitstream. */
1145 static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
1146 uint8_t *substream_headers, unsigned int length,
1148 uint16_t substream_data_len[MAX_SUBSTREAMS])
1150 uint16_t access_unit_header = 0;
1151 uint16_t parity_nibble = 0;
1152 unsigned int substr;
1154 parity_nibble = ctx->dts;
1155 parity_nibble ^= length;
1157 for (substr = 0; substr < ctx->num_substreams; substr++) {
1158 uint16_t substr_hdr = 0;
1160 substr_hdr |= (0 << 15); /* extraword */
1161 substr_hdr |= (!restart_frame << 14); /* !restart_frame */
1162 substr_hdr |= (1 << 13); /* checkdata */
1163 substr_hdr |= (0 << 12); /* ??? */
1164 substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
1166 AV_WB16(substream_headers, substr_hdr);
1168 parity_nibble ^= *substream_headers++;
1169 parity_nibble ^= *substream_headers++;
1172 parity_nibble ^= parity_nibble >> 8;
1173 parity_nibble ^= parity_nibble >> 4;
1174 parity_nibble &= 0xF;
1176 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1177 access_unit_header |= length & 0xFFF;
1179 AV_WB16(frame_header , access_unit_header);
1180 AV_WB16(frame_header+2, ctx->dts );
1183 /** Writes an entire access unit to the bitstream. */
1184 static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
1185 int buf_size, int restart_frame)
1187 uint16_t substream_data_len[MAX_SUBSTREAMS];
1188 uint8_t *buf1, *buf0 = buf;
1189 unsigned int substr;
1195 /* Frame header will be written at the end. */
1199 if (restart_frame) {
1202 write_major_sync(ctx, buf, buf_size);
1209 /* Substream headers will be written at the end. */
1210 for (substr = 0; substr < ctx->num_substreams; substr++) {
1215 buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
1217 total_length = buf - buf0;
1219 write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1221 return total_length;
1224 /****************************************************************************
1225 ****************** Functions that input data to context ********************
1226 ****************************************************************************/
1228 /** Inputs data from the samples passed by lavc into the context, shifts them
1229 * appropriately depending on the bit-depth, and calculates the
1230 * lossless_check_data that will be written to the restart header.
1232 static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
1235 int32_t *lossless_check_data = ctx->lossless_check_data;
1236 const int32_t *samples_32 = (const int32_t *) samples;
1237 const int16_t *samples_16 = (const int16_t *) samples;
1238 unsigned int substr;
1240 lossless_check_data += ctx->frame_index * ctx->num_substreams;
1242 for (substr = 0; substr < ctx->num_substreams; substr++) {
1243 RestartHeader *rh = &ctx->restart_header [substr];
1244 int32_t *sample_buffer = ctx->inout_buffer;
1245 int32_t temp_lossless_check_data = 0;
1246 uint32_t greatest = 0;
1247 unsigned int channel;
1250 for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
1251 for (channel = 0; channel <= rh->max_channel; channel++) {
1252 uint32_t abs_sample;
1255 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256;
1257 /* TODO Find out if number_sbits can be used for negative values. */
1258 abs_sample = FFABS(sample);
1259 if (greatest < abs_sample)
1260 greatest = abs_sample;
1262 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1263 *sample_buffer++ = sample;
1266 sample_buffer += 2; /* noise channels */
1269 ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
1271 *lossless_check_data++ = temp_lossless_check_data;
1275 /** Wrapper function for inputting data in two different bit-depths. */
1276 static void input_data(MLPEncodeContext *ctx, void *samples)
1278 if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1279 input_data_internal(ctx, samples, 1);
1281 input_data_internal(ctx, samples, 0);
1284 static void input_to_sample_buffer(MLPEncodeContext *ctx)
1286 int32_t *sample_buffer = ctx->sample_buffer;
1289 for (index = 0; index < ctx->number_of_frames; index++) {
1290 unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
1291 int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
1292 unsigned int i, channel;
1294 for (i = 0; i < ctx->frame_size[cur_index]; i++) {
1295 for (channel = 0; channel < ctx->avctx->channels; channel++)
1296 *sample_buffer++ = *input_buffer++;
1297 sample_buffer += 2; /* noise_channels */
1298 input_buffer += 2; /* noise_channels */
1303 /****************************************************************************
1304 ********* Functions that analyze the data and set the parameters ***********
1305 ****************************************************************************/
1307 /** Counts the number of trailing zeroes in a value */
1308 static int number_trailing_zeroes(int32_t sample)
1312 for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
1314 /* All samples are 0. TODO Return previous quant_step_size to avoid
1315 * writing a new header. */
1322 /** Determines how many bits are zero at the end of all samples so they can be
1325 static void determine_quant_step_size(MLPEncodeContext *ctx)
1327 DecodingParams *dp = ctx->cur_decoding_params;
1328 RestartHeader *rh = ctx->cur_restart_header;
1329 MatrixParams *mp = &dp->matrix_params;
1330 int32_t *sample_buffer = ctx->sample_buffer;
1331 int32_t sample_mask[MAX_CHANNELS];
1332 unsigned int channel;
1335 memset(sample_mask, 0x00, sizeof(sample_mask));
1337 for (i = 0; i < ctx->number_of_samples; i++) {
1338 for (channel = 0; channel <= rh->max_channel; channel++)
1339 sample_mask[channel] |= *sample_buffer++;
1341 sample_buffer += 2; /* noise channels */
1344 for (channel = 0; channel <= rh->max_channel; channel++)
1345 dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
1348 /** Determines the smallest number of bits needed to encode the filter
1349 * coefficients, and if it's possible to right-shift their values without
1350 * losing any precision.
1352 static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
1354 int min = INT_MAX, max = INT_MIN;
1359 for (order = 0; order < fp->order; order++) {
1360 int coeff = fcoeff[order];
1367 coeff_mask |= coeff;
1370 bits = FFMAX(number_sbits(min), number_sbits(max));
1372 for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
1374 fp->coeff_bits = bits;
1375 fp->coeff_shift = shift;
1378 /** Determines the best filter parameters for the given data and writes the
1379 * necessary information to the context.
1380 * TODO Add IIR filter predictor!
1382 static void set_filter_params(MLPEncodeContext *ctx,
1383 unsigned int channel, unsigned int filter,
1386 ChannelParams *cp = &ctx->cur_channel_params[channel];
1387 FilterParams *fp = &cp->filter_params[filter];
1389 if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
1392 } else if (filter == IIR) {
1394 } else if (filter == FIR) {
1395 const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
1396 ? 4 : MLP_MAX_LPC_ORDER;
1397 int32_t *sample_buffer = ctx->sample_buffer + channel;
1398 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
1399 int32_t *lpc_samples = ctx->lpc_sample_buffer;
1400 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1401 int shift[MLP_MAX_LPC_ORDER];
1405 for (i = 0; i < ctx->number_of_samples; i++) {
1406 *lpc_samples++ = *sample_buffer;
1407 sample_buffer += ctx->num_channels;
1410 order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
1411 ctx->number_of_samples, MLP_MIN_LPC_ORDER,
1412 max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
1413 ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
1414 MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
1417 fp->shift = shift[order-1];
1419 for (i = 0; i < order; i++)
1420 fcoeff[i] = coefs[order-1][i];
1422 code_filter_coeffs(ctx, fp, fcoeff);
1426 /** Tries to determine a good prediction filter, and applies it to the samples
1427 * buffer if the filter is good enough. Sets the filter data to be cleared if
1428 * no good filter was found.
1430 static void determine_filters(MLPEncodeContext *ctx)
1432 RestartHeader *rh = ctx->cur_restart_header;
1433 int channel, filter;
1435 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1436 for (filter = 0; filter < NUM_FILTERS; filter++)
1437 set_filter_params(ctx, channel, filter, 0);
1442 MLP_CHMODE_LEFT_RIGHT,
1443 MLP_CHMODE_LEFT_SIDE,
1444 MLP_CHMODE_RIGHT_SIDE,
1445 MLP_CHMODE_MID_SIDE,
1448 static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
1450 uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
1451 int32_t *right_ch = ctx->sample_buffer + 1;
1452 int32_t *left_ch = ctx->sample_buffer;
1454 enum MLPChMode best = 0;
1456 for(i = 2; i < ctx->number_of_samples; i++) {
1457 int32_t left = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
1458 int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
1460 sum[0] += FFABS( left );
1461 sum[1] += FFABS( right);
1462 sum[2] += FFABS((left + right) >> 1);
1463 sum[3] += FFABS( left - right);
1466 score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
1467 score[MLP_CHMODE_LEFT_SIDE] = sum[0] + sum[3];
1468 score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
1469 score[MLP_CHMODE_MID_SIDE] = sum[2] + sum[3];
1471 for(i = 1; i < 3; i++)
1472 if(score[i] < score[best])
1478 /** Determines how many fractional bits are needed to encode matrix
1479 * coefficients. Also shifts the coefficients to fit within 2.14 bits.
1481 static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
1483 DecodingParams *dp = ctx->cur_decoding_params;
1484 MatrixParams *mp = &dp->matrix_params;
1485 int32_t coeff_mask = 0;
1486 unsigned int channel;
1489 for (channel = 0; channel < ctx->num_channels; channel++) {
1490 int32_t coeff = mp->coeff[mat][channel];
1491 coeff_mask |= coeff;
1494 for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
1496 mp->fbits [mat] = 14 - bits;
1499 /** Determines best coefficients to use for the lossless matrix. */
1500 static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
1502 DecodingParams *dp = ctx->cur_decoding_params;
1503 MatrixParams *mp = &dp->matrix_params;
1504 unsigned int shift = 0;
1505 unsigned int channel;
1507 enum MLPChMode mode;
1509 /* No decorrelation for non-stereo. */
1510 if (ctx->num_channels - 2 != 2) {
1515 mode = estimate_stereo_mode(ctx);
1518 /* TODO: add matrix for MID_SIDE */
1519 case MLP_CHMODE_MID_SIDE:
1520 case MLP_CHMODE_LEFT_RIGHT:
1523 case MLP_CHMODE_LEFT_SIDE:
1526 mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = -(1 << 14);
1527 mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
1528 mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
1529 mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
1531 case MLP_CHMODE_RIGHT_SIDE:
1534 mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = 1 << 14;
1535 mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
1536 mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
1537 mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
1541 for (mat = 0; mat < mp->count; mat++)
1542 code_matrix_coeffs(ctx, mat);
1544 for (channel = 0; channel < ctx->num_channels; channel++)
1545 mp->shift[channel] = shift;
1548 /** Min and max values that can be encoded with each codebook. The values for
1549 * the third codebook take into account the fact that the sign shift for this
1550 * codebook is outside the coded value, so it has one more bit of precision.
1551 * It should actually be -7 -> 7, shifted down by 0.5.
1553 static const int codebook_extremes[3][2] = {
1554 {-9, 8}, {-8, 7}, {-15, 14},
1557 /** Determines the amount of bits needed to encode the samples using no
1558 * codebooks and a specified offset.
1560 static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1561 unsigned int channel, int16_t offset,
1562 int32_t min, int32_t max,
1565 DecodingParams *dp = ctx->cur_decoding_params;
1572 lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
1574 lsb_bits += !!lsb_bits;
1577 unsign = 1 << (lsb_bits - 1);
1579 bo->offset = offset;
1580 bo->lsb_bits = lsb_bits;
1581 bo->bitcount = lsb_bits * dp->blocksize;
1582 bo->min = offset - unsign + 1;
1583 bo->max = offset + unsign;
1586 /** Determines the least amount of bits needed to encode the samples using no
1589 static void no_codebook_bits(MLPEncodeContext *ctx,
1590 unsigned int channel,
1591 int32_t min, int32_t max,
1594 DecodingParams *dp = ctx->cur_decoding_params;
1600 /* Set offset inside huffoffset's boundaries by adjusting extremes
1601 * so that more bits are used, thus shifting the offset. */
1602 if (min < HUFF_OFFSET_MIN)
1603 max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
1604 if (max > HUFF_OFFSET_MAX)
1605 min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
1607 /* Determine offset and minimum number of bits. */
1610 lsb_bits = number_sbits(diff) - 1;
1613 unsign = 1 << (lsb_bits - 1);
1615 /* If all samples are the same (lsb_bits == 0), offset must be
1616 * adjusted because of sign_shift. */
1617 offset = min + diff / 2 + !!lsb_bits;
1619 bo->offset = offset;
1620 bo->lsb_bits = lsb_bits;
1621 bo->bitcount = lsb_bits * dp->blocksize;
1622 bo->min = max - unsign + 1;
1623 bo->max = min + unsign;
1626 /** Determines the least amount of bits needed to encode the samples using a
1627 * given codebook and a given offset.
1629 static inline void codebook_bits_offset(MLPEncodeContext *ctx,
1630 unsigned int channel, int codebook,
1631 int32_t sample_min, int32_t sample_max,
1632 int16_t offset, BestOffset *bo)
1634 int32_t codebook_min = codebook_extremes[codebook][0];
1635 int32_t codebook_max = codebook_extremes[codebook][1];
1636 int32_t *sample_buffer = ctx->sample_buffer + channel;
1637 DecodingParams *dp = ctx->cur_decoding_params;
1638 int codebook_offset = 7 + (2 - codebook);
1639 int32_t unsign_offset = offset;
1640 int lsb_bits = 0, bitcount = 0;
1641 int offset_min = INT_MAX, offset_max = INT_MAX;
1645 sample_min -= offset;
1646 sample_max -= offset;
1648 while (sample_min < codebook_min || sample_max > codebook_max) {
1654 unsign = 1 << lsb_bits;
1657 if (codebook == 2) {
1658 unsign_offset -= unsign;
1662 for (i = 0; i < dp->blocksize; i++) {
1663 int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1664 int temp_min, temp_max;
1666 sample -= unsign_offset;
1668 temp_min = sample & mask;
1669 if (temp_min < offset_min)
1670 offset_min = temp_min;
1672 temp_max = unsign - temp_min - 1;
1673 if (temp_max < offset_max)
1674 offset_max = temp_max;
1676 sample >>= lsb_bits;
1678 bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
1680 sample_buffer += ctx->num_channels;
1683 bo->offset = offset;
1684 bo->lsb_bits = lsb_bits;
1685 bo->bitcount = lsb_bits * dp->blocksize + bitcount;
1686 bo->min = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
1687 bo->max = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
1690 /** Determines the least amount of bits needed to encode the samples using a
1691 * given codebook. Searches for the best offset to minimize the bits.
1693 static inline void codebook_bits(MLPEncodeContext *ctx,
1694 unsigned int channel, int codebook,
1695 int offset, int32_t min, int32_t max,
1696 BestOffset *bo, int direction)
1698 int previous_count = INT_MAX;
1699 int offset_min, offset_max;
1702 offset_min = FFMAX(min, HUFF_OFFSET_MIN);
1703 offset_max = FFMIN(max, HUFF_OFFSET_MAX);
1705 while (offset <= offset_max && offset >= offset_min) {
1708 codebook_bits_offset(ctx, channel, codebook,
1712 if (temp_bo.bitcount < previous_count) {
1713 if (temp_bo.bitcount < bo->bitcount)
1717 } else if (++is_greater >= ctx->max_codebook_search)
1720 previous_count = temp_bo.bitcount;
1723 offset = temp_bo.max + 1;
1725 offset = temp_bo.min - 1;
1730 /** Determines the least amount of bits needed to encode the samples using
1731 * any or no codebook.
1733 static void determine_bits(MLPEncodeContext *ctx)
1735 DecodingParams *dp = ctx->cur_decoding_params;
1736 RestartHeader *rh = ctx->cur_restart_header;
1737 unsigned int channel;
1739 for (channel = 0; channel <= rh->max_channel; channel++) {
1740 ChannelParams *cp = &ctx->cur_channel_params[channel];
1741 int32_t *sample_buffer = ctx->sample_buffer + channel;
1742 int32_t min = INT32_MAX, max = INT32_MIN;
1743 int no_filters_used = !cp->filter_params[FIR].order;
1748 /* Determine extremes and average. */
1749 for (i = 0; i < dp->blocksize; i++) {
1750 int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1756 sample_buffer += ctx->num_channels;
1758 average /= dp->blocksize;
1760 /* If filtering is used, we always set the offset to zero, otherwise
1761 * we search for the offset that minimizes the bitcount. */
1762 if (no_filters_used) {
1763 no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
1764 offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
1766 no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
1769 for (i = 1; i < NUM_CODEBOOKS; i++) {
1770 BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1773 codebook_bits_offset(ctx, channel, i - 1,
1777 if (no_filters_used) {
1778 offset_max = temp_bo.max;
1780 codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
1781 min, max, &temp_bo, 0);
1782 codebook_bits(ctx, channel, i - 1, offset_max + 1,
1783 min, max, &temp_bo, 1);
1786 ctx->cur_best_offset[channel][i] = temp_bo;
1791 /****************************************************************************
1792 *************** Functions that process the data in some way ****************
1793 ****************************************************************************/
1795 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1796 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1798 #define MSB_MASK(bits) (-(int)(1u << (bits)))
1800 /** Applies the filter to the current samples, and saves the residual back
1801 * into the samples buffer. If the filter is too bad and overflows the
1802 * maximum amount of bits allowed (24), the samples buffer is left as is and
1803 * the function returns -1.
1805 static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
1807 FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
1808 &ctx->cur_channel_params[channel].filter_params[IIR], };
1809 int32_t *filter_state_buffer[NUM_FILTERS] = { NULL };
1810 int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
1811 int32_t *sample_buffer = ctx->sample_buffer + channel;
1812 unsigned int number_of_samples = ctx->number_of_samples;
1813 unsigned int filter_shift = fp[FIR]->shift;
1817 for (i = 0; i < NUM_FILTERS; i++) {
1818 unsigned int size = ctx->number_of_samples;
1819 filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
1820 if (!filter_state_buffer[i]) {
1821 av_log(ctx->avctx, AV_LOG_ERROR,
1822 "Not enough memory for applying filters.\n");
1827 for (i = 0; i < 8; i++) {
1828 filter_state_buffer[FIR][i] = *sample_buffer;
1829 filter_state_buffer[IIR][i] = *sample_buffer;
1831 sample_buffer += ctx->num_channels;
1834 for (i = 8; i < number_of_samples; i++) {
1835 int32_t sample = *sample_buffer;
1840 for (filter = 0; filter < NUM_FILTERS; filter++) {
1841 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1842 for (order = 0; order < fp[filter]->order; order++)
1843 accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
1847 accum >>= filter_shift;
1848 residual = sample - (accum & mask);
1850 if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
1852 goto free_and_return;
1855 filter_state_buffer[FIR][i] = sample;
1856 filter_state_buffer[IIR][i] = (int32_t) residual;
1858 sample_buffer += ctx->num_channels;
1861 sample_buffer = ctx->sample_buffer + channel;
1862 for (i = 0; i < number_of_samples; i++) {
1863 *sample_buffer = filter_state_buffer[IIR][i];
1865 sample_buffer += ctx->num_channels;
1869 for (i = 0; i < NUM_FILTERS; i++) {
1870 av_freep(&filter_state_buffer[i]);
1876 static void apply_filters(MLPEncodeContext *ctx)
1878 RestartHeader *rh = ctx->cur_restart_header;
1881 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1882 if (apply_filter(ctx, channel) < 0) {
1883 /* Filter is horribly wrong.
1884 * Clear filter params and update state. */
1885 set_filter_params(ctx, channel, FIR, 1);
1886 set_filter_params(ctx, channel, IIR, 1);
1887 apply_filter(ctx, channel);
1892 /** Generates two noise channels worth of data. */
1893 static void generate_2_noise_channels(MLPEncodeContext *ctx)
1895 int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
1896 RestartHeader *rh = ctx->cur_restart_header;
1898 uint32_t seed = rh->noisegen_seed;
1900 for (i = 0; i < ctx->number_of_samples; i++) {
1901 uint16_t seed_shr7 = seed >> 7;
1902 *sample_buffer++ = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
1903 *sample_buffer++ = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
1905 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1907 sample_buffer += ctx->num_channels - 2;
1910 rh->noisegen_seed = seed & ((1 << 24)-1);
1913 /** Rematrixes all channels using chosen coefficients. */
1914 static void rematrix_channels(MLPEncodeContext *ctx)
1916 DecodingParams *dp = ctx->cur_decoding_params;
1917 MatrixParams *mp = &dp->matrix_params;
1918 int32_t *sample_buffer = ctx->sample_buffer;
1919 unsigned int mat, i, maxchan;
1921 maxchan = ctx->num_channels;
1923 for (mat = 0; mat < mp->count; mat++) {
1924 unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
1925 int32_t mask = MSB_MASK(msb_mask_bits);
1926 unsigned int outch = mp->outch[mat];
1928 sample_buffer = ctx->sample_buffer;
1929 for (i = 0; i < ctx->number_of_samples; i++) {
1930 unsigned int src_ch;
1933 for (src_ch = 0; src_ch < maxchan; src_ch++) {
1934 int32_t sample = *(sample_buffer + src_ch);
1935 accum += (int64_t) sample * mp->forco[mat][src_ch];
1937 sample_buffer[outch] = (accum >> 14) & mask;
1939 sample_buffer += ctx->num_channels;
1944 /****************************************************************************
1945 **** Functions that deal with determining the best parameters and output ***
1946 ****************************************************************************/
1949 char path[MAJOR_HEADER_INTERVAL + 3];
1953 static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
1955 #define ZERO_PATH '0'
1956 #define CODEBOOK_CHANGE_BITS 21
1958 static void clear_path_counter(PathCounter *path_counter)
1962 for (i = 0; i < NUM_CODEBOOKS + 1; i++) {
1963 path_counter[i].path[0] = ZERO_PATH;
1964 path_counter[i].path[1] = 0x00;
1965 path_counter[i].bitcount = 0;
1969 static int compare_best_offset(BestOffset *prev, BestOffset *cur)
1971 if (prev->lsb_bits != cur->lsb_bits)
1977 static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1978 PathCounter *src, int cur_codebook)
1980 BestOffset *cur_bo, *prev_bo = restart_best_offset;
1981 int bitcount = src->bitcount;
1982 char *path = src->path + 1;
1986 for (i = 0; path[i]; i++)
1987 prev_bo = ctx->best_offset[i][channel];
1989 prev_codebook = path[i - 1] - ZERO_PATH;
1991 cur_bo = ctx->best_offset[i][channel];
1993 bitcount += cur_bo[cur_codebook].bitcount;
1995 if (prev_codebook != cur_codebook ||
1996 compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
1997 bitcount += CODEBOOK_CHANGE_BITS;
2002 static void set_best_codebook(MLPEncodeContext *ctx)
2004 DecodingParams *dp = ctx->cur_decoding_params;
2005 RestartHeader *rh = ctx->cur_restart_header;
2006 unsigned int channel;
2008 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
2009 BestOffset *cur_bo, *prev_bo = restart_best_offset;
2010 PathCounter path_counter[NUM_CODEBOOKS + 1];
2011 unsigned int best_codebook;
2015 clear_path_counter(path_counter);
2017 for (index = 0; index < ctx->number_of_subblocks; index++) {
2018 unsigned int best_bitcount = INT_MAX;
2019 unsigned int codebook;
2021 cur_bo = ctx->best_offset[index][channel];
2023 for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
2024 int prev_best_bitcount = INT_MAX;
2027 for (last_best = 0; last_best < 2; last_best++) {
2028 PathCounter *dst_path = &path_counter[codebook];
2029 PathCounter *src_path;
2032 /* First test last path with same headers,
2033 * then with last best. */
2035 src_path = &path_counter[NUM_CODEBOOKS];
2037 if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
2040 src_path = &path_counter[codebook];
2043 temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
2045 if (temp_bitcount < best_bitcount) {
2046 best_bitcount = temp_bitcount;
2047 best_codebook = codebook;
2050 if (temp_bitcount < prev_best_bitcount) {
2051 prev_best_bitcount = temp_bitcount;
2052 if (src_path != dst_path)
2053 memcpy(dst_path, src_path, sizeof(PathCounter));
2054 av_strlcat(dst_path->path, path_counter_codebook[codebook], sizeof(dst_path->path));
2055 dst_path->bitcount = temp_bitcount;
2062 memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
2065 best_path = path_counter[NUM_CODEBOOKS].path + 1;
2067 /* Update context. */
2068 for (index = 0; index < ctx->number_of_subblocks; index++) {
2069 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
2071 best_codebook = *best_path++ - ZERO_PATH;
2072 cur_bo = &ctx->best_offset[index][channel][best_codebook];
2074 cp->huff_offset = cur_bo->offset;
2075 cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
2076 cp->codebook = best_codebook;
2081 /** Analyzes all collected bitcounts and selects the best parameters for each
2082 * individual access unit.
2083 * TODO This is just a stub!
2085 static void set_major_params(MLPEncodeContext *ctx)
2087 RestartHeader *rh = ctx->cur_restart_header;
2089 unsigned int substr;
2090 uint8_t max_huff_lsbs = 0;
2091 uint8_t max_output_bits = 0;
2093 for (substr = 0; substr < ctx->num_substreams; substr++) {
2094 DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
2095 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2096 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2098 ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
2099 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2100 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2101 unsigned int channel;
2102 for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
2103 memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
2104 for (channel = 0; channel < ctx->avctx->channels; channel++) {
2105 uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
2106 if (max_huff_lsbs < huff_lsbs)
2107 max_huff_lsbs = huff_lsbs;
2108 memcpy(&ctx->major_channel_params[index][channel],
2109 (seq_cp + index*(ctx->avctx->channels) + channel),
2110 sizeof(ChannelParams));
2115 rh->max_huff_lsbs = max_huff_lsbs;
2117 for (index = 0; index < ctx->number_of_frames; index++)
2118 if (max_output_bits < ctx->max_output_bits[index])
2119 max_output_bits = ctx->max_output_bits[index];
2120 rh->max_output_bits = max_output_bits;
2122 for (substr = 0; substr < ctx->num_substreams; substr++) {
2124 ctx->cur_restart_header = &ctx->restart_header[substr];
2126 ctx->prev_decoding_params = &restart_decoding_params[substr];
2127 ctx->prev_channel_params = restart_channel_params;
2129 for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
2130 ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
2131 ctx->cur_channel_params = ctx->major_channel_params[index];
2133 ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
2135 ctx->prev_decoding_params = ctx->cur_decoding_params;
2136 ctx->prev_channel_params = ctx->cur_channel_params;
2140 ctx->major_number_of_subblocks = ctx->number_of_subblocks;
2141 ctx->major_filter_state_subblock = 1;
2142 ctx->major_cur_subblock_index = 0;
2145 static void analyze_sample_buffer(MLPEncodeContext *ctx)
2147 ChannelParams *seq_cp = ctx->seq_channel_params;
2148 DecodingParams *seq_dp = ctx->seq_decoding_params;
2150 unsigned int substr;
2152 for (substr = 0; substr < ctx->num_substreams; substr++) {
2154 ctx->cur_restart_header = &ctx->restart_header[substr];
2155 ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
2156 ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
2158 determine_quant_step_size(ctx);
2159 generate_2_noise_channels(ctx);
2160 lossless_matrix_coeffs (ctx);
2161 rematrix_channels (ctx);
2162 determine_filters (ctx);
2163 apply_filters (ctx);
2165 copy_restart_frame_params(ctx, substr);
2167 /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2168 * decoding_params[0] is for the filter state subblock.
2170 for (index = 0; index < ctx->number_of_frames; index++) {
2171 DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
2172 dp->blocksize = ctx->frame_size[index];
2174 /* The official encoder seems to always encode a filter state subblock
2175 * even if there are no filters. TODO check if it is possible to skip
2176 * the filter state subblock for no filters.
2178 (seq_dp + substr)->blocksize = 8;
2179 (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
2181 for (index = 0; index < ctx->number_of_subblocks; index++) {
2182 ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
2183 ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
2184 ctx->cur_best_offset = ctx->best_offset[index];
2185 determine_bits(ctx);
2186 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
2189 set_best_codebook(ctx);
2193 static void process_major_frame(MLPEncodeContext *ctx)
2195 unsigned int substr;
2197 ctx->sample_buffer = ctx->major_inout_buffer;
2199 ctx->starting_frame_index = 0;
2200 ctx->number_of_frames = ctx->major_number_of_frames;
2201 ctx->number_of_samples = ctx->major_frame_size;
2203 for (substr = 0; substr < ctx->num_substreams; substr++) {
2204 ctx->cur_restart_header = &ctx->restart_header[substr];
2206 ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
2207 ctx->cur_channel_params = ctx->major_channel_params[1];
2209 generate_2_noise_channels(ctx);
2210 rematrix_channels (ctx);
2216 /****************************************************************************/
2218 static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2219 const AVFrame *frame, int *got_packet)
2221 MLPEncodeContext *ctx = avctx->priv_data;
2222 unsigned int bytes_written = 0;
2223 int restart_frame, ret;
2226 if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
2232 /* add current frame to queue */
2233 if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
2236 data = frame->data[0];
2238 ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
2240 ctx->inout_buffer = ctx->major_inout_buffer
2241 + ctx->frame_index * ctx->one_sample_buffer_size;
2243 if (ctx->last_frame == ctx->inout_buffer) {
2247 ctx->sample_buffer = ctx->major_scratch_buffer
2248 + ctx->frame_index * ctx->one_sample_buffer_size;
2250 ctx->write_buffer = ctx->inout_buffer;
2252 if (avctx->frame_number < ctx->max_restart_interval) {
2254 goto input_and_return;
2256 /* There are less frames than the requested major header interval.
2257 * Update the context to reflect this.
2259 ctx->max_restart_interval = avctx->frame_number;
2260 ctx->frame_index = 0;
2262 ctx->sample_buffer = ctx->major_scratch_buffer;
2263 ctx->inout_buffer = ctx->major_inout_buffer;
2267 if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
2268 av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
2269 ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
2273 restart_frame = !ctx->frame_index;
2275 if (restart_frame) {
2276 set_major_params(ctx);
2277 if (ctx->min_restart_interval != ctx->max_restart_interval)
2278 process_major_frame(ctx);
2281 if (ctx->min_restart_interval == ctx->max_restart_interval)
2282 ctx->write_buffer = ctx->sample_buffer;
2284 bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
2286 ctx->timestamp += ctx->frame_size[ctx->frame_index];
2287 ctx->dts += ctx->frame_size[ctx->frame_index];
2292 ctx->frame_size[ctx->frame_index] = avctx->frame_size;
2293 ctx->next_major_frame_size += avctx->frame_size;
2294 ctx->next_major_number_of_frames++;
2295 input_data(ctx, data);
2296 } else if (!ctx->last_frame) {
2297 ctx->last_frame = ctx->inout_buffer;
2300 restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
2302 if (!restart_frame) {
2306 seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
2308 unsigned int number_of_samples = 0;
2311 ctx->sample_buffer = ctx->major_scratch_buffer;
2312 ctx->inout_buffer = ctx->major_inout_buffer;
2313 ctx->seq_index = seq_index;
2315 ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
2316 - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
2317 ctx->number_of_frames = ctx->next_major_number_of_frames;
2318 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
2320 ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
2321 (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
2322 (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
2324 ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
2325 (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
2326 (ctx->seq_offset[seq_index])*(ctx->num_substreams);
2328 for (index = 0; index < ctx->number_of_frames; index++) {
2329 number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
2331 ctx->number_of_samples = number_of_samples;
2333 for (index = 0; index < ctx->seq_size[seq_index]; index++) {
2334 clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
2335 default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
2338 input_to_sample_buffer(ctx);
2340 analyze_sample_buffer(ctx);
2343 if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
2344 ctx->major_frame_size = ctx->next_major_frame_size;
2345 ctx->next_major_frame_size = 0;
2346 ctx->major_number_of_frames = ctx->next_major_number_of_frames;
2347 ctx->next_major_number_of_frames = 0;
2349 if (!ctx->major_frame_size)
2356 ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
2358 avpkt->size = bytes_written;
2363 static av_cold int mlp_encode_close(AVCodecContext *avctx)
2365 MLPEncodeContext *ctx = avctx->priv_data;
2367 ff_lpc_end(&ctx->lpc_ctx);
2369 av_freep(&ctx->lossless_check_data);
2370 av_freep(&ctx->major_scratch_buffer);
2371 av_freep(&ctx->major_inout_buffer);
2372 av_freep(&ctx->lpc_sample_buffer);
2373 av_freep(&ctx->decoding_params);
2374 av_freep(&ctx->channel_params);
2375 av_freep(&ctx->frame_size);
2376 av_freep(&ctx->max_output_bits);
2377 ff_af_queue_close(&ctx->afq);
2382 #if CONFIG_MLP_ENCODER
2383 AVCodec ff_mlp_encoder = {
2385 .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
2386 .type = AVMEDIA_TYPE_AUDIO,
2387 .id = AV_CODEC_ID_MLP,
2388 .priv_data_size = sizeof(MLPEncodeContext),
2389 .init = mlp_encode_init,
2390 .encode2 = mlp_encode_frame,
2391 .close = mlp_encode_close,
2392 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
2393 .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2394 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2395 .channel_layouts = ff_mlp_channel_layouts,
2398 #if CONFIG_TRUEHD_ENCODER
2399 AVCodec ff_truehd_encoder = {
2401 .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
2402 .type = AVMEDIA_TYPE_AUDIO,
2403 .id = AV_CODEC_ID_TRUEHD,
2404 .priv_data_size = sizeof(MLPEncodeContext),
2405 .init = mlp_encode_init,
2406 .encode2 = mlp_encode_frame,
2407 .close = mlp_encode_close,
2408 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
2409 .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2410 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2411 .channel_layouts = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},