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);
991 put_bits(pb, 2, cp->codebook );
992 put_bits(pb, 5, cp->huff_lsbs);
999 /** Writes the residuals to the bitstream. That is, the VLC codes from the
1000 * codebooks (if any is used), and then the residual.
1002 static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
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];
1012 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1013 ChannelParams *cp = &ctx->cur_channel_params[ch];
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;
1020 sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
1022 if (cp->codebook > 0)
1023 sign_huff_offset[ch] -= 7 << lsb_bits[ch];
1025 /* Unsign if needed. */
1026 if (sign_shift >= 0)
1027 sign_huff_offset[ch] -= 1 << sign_shift;
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];
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]);
1041 put_sbits(pb, lsb_bits[ch], sample);
1043 sample_buffer += 2; /* noise channels */
1046 ctx->write_buffer = sample_buffer;
1049 /** Writes the substreams data to the bitstream. */
1050 static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
1052 uint16_t substream_data_len[MAX_SUBSTREAMS])
1054 int32_t *lossless_check_data = ctx->lossless_check_data;
1055 unsigned int substr;
1058 lossless_check_data += ctx->frame_index * ctx->num_substreams;
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;
1070 ctx->cur_restart_header = rh;
1072 init_put_bits(&pb, buf, buf_size);
1074 for (subblock = 0; subblock <= num_subblocks; subblock++) {
1075 unsigned int subblock_index;
1077 subblock_index = cur_subblock_index++;
1079 ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
1080 ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
1082 params_changed = ctx->major_params_changed[subblock_index][substr];
1084 if (substr_restart_frame || params_changed) {
1085 put_bits(&pb, 1, 1);
1087 if (substr_restart_frame) {
1088 put_bits(&pb, 1, 1);
1090 write_restart_header(ctx, &pb);
1091 rh->lossless_check_data = 0;
1093 put_bits(&pb, 1, 0);
1096 write_decoding_params(ctx, &pb, params_changed);
1098 put_bits(&pb, 1, 0);
1101 write_block_data(ctx, &pb);
1103 put_bits(&pb, 1, !substr_restart_frame);
1105 substr_restart_frame = 0;
1108 put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
1110 rh->lossless_check_data ^= *lossless_check_data++;
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);
1117 /* Data must be flushed for the checksum and parity to be correct. */
1119 flush_put_bits(&tmpb);
1121 parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
1122 checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3);
1124 put_bits(&pb, 8, parity );
1125 put_bits(&pb, 8, checksum);
1127 flush_put_bits(&pb);
1129 end += put_bits_count(&pb) >> 3;
1130 substream_data_len[substr] = end;
1132 buf += put_bits_count(&pb) >> 3;
1135 ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
1136 ctx->major_filter_state_subblock = 0;
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,
1145 uint16_t substream_data_len[MAX_SUBSTREAMS])
1147 uint16_t access_unit_header = 0;
1148 uint16_t parity_nibble = 0;
1149 unsigned int substr;
1151 parity_nibble = ctx->dts;
1152 parity_nibble ^= length;
1154 for (substr = 0; substr < ctx->num_substreams; substr++) {
1155 uint16_t substr_hdr = 0;
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;
1163 AV_WB16(substream_headers, substr_hdr);
1165 parity_nibble ^= *substream_headers++;
1166 parity_nibble ^= *substream_headers++;
1169 parity_nibble ^= parity_nibble >> 8;
1170 parity_nibble ^= parity_nibble >> 4;
1171 parity_nibble &= 0xF;
1173 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1174 access_unit_header |= length & 0xFFF;
1176 AV_WB16(frame_header , access_unit_header);
1177 AV_WB16(frame_header+2, ctx->dts );
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)
1184 uint16_t substream_data_len[MAX_SUBSTREAMS];
1185 uint8_t *buf1, *buf0 = buf;
1186 unsigned int substr;
1192 /* Frame header will be written at the end. */
1196 if (restart_frame) {
1199 write_major_sync(ctx, buf, buf_size);
1206 /* Substream headers will be written at the end. */
1207 for (substr = 0; substr < ctx->num_substreams; substr++) {
1212 buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
1214 total_length = buf - buf0;
1216 write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1218 return total_length;
1221 /****************************************************************************
1222 ****************** Functions that input data to context ********************
1223 ****************************************************************************/
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.
1229 static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
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;
1237 lossless_check_data += ctx->frame_index * ctx->num_substreams;
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;
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;
1252 sample = is24 ? *samples_32++ >> 8 : *samples_16++ * 256U;
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;
1259 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1260 *sample_buffer++ = sample;
1263 sample_buffer += 2; /* noise channels */
1266 ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
1268 *lossless_check_data++ = temp_lossless_check_data;
1272 /** Wrapper function for inputting data in two different bit-depths. */
1273 static void input_data(MLPEncodeContext *ctx, void *samples)
1275 if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1276 input_data_internal(ctx, samples, 1);
1278 input_data_internal(ctx, samples, 0);
1281 static void input_to_sample_buffer(MLPEncodeContext *ctx)
1283 int32_t *sample_buffer = ctx->sample_buffer;
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;
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 */
1300 /****************************************************************************
1301 ********* Functions that analyze the data and set the parameters ***********
1302 ****************************************************************************/
1304 /** Counts the number of trailing zeroes in a value */
1305 static int number_trailing_zeroes(int32_t sample)
1309 for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
1311 /* All samples are 0. TODO Return previous quant_step_size to avoid
1312 * writing a new header. */
1319 /** Determines how many bits are zero at the end of all samples so they can be
1322 static void determine_quant_step_size(MLPEncodeContext *ctx)
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;
1332 memset(sample_mask, 0x00, sizeof(sample_mask));
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++;
1338 sample_buffer += 2; /* noise channels */
1341 for (channel = 0; channel <= rh->max_channel; channel++)
1342 dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
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.
1349 static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
1351 int min = INT_MAX, max = INT_MIN;
1356 for (order = 0; order < fp->order; order++) {
1357 int coeff = fcoeff[order];
1364 coeff_mask |= coeff;
1367 bits = FFMAX(number_sbits(min), number_sbits(max));
1369 for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
1371 fp->coeff_bits = bits;
1372 fp->coeff_shift = shift;
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!
1379 static void set_filter_params(MLPEncodeContext *ctx,
1380 unsigned int channel, unsigned int filter,
1383 ChannelParams *cp = &ctx->cur_channel_params[channel];
1384 FilterParams *fp = &cp->filter_params[filter];
1386 if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
1389 } else if (filter == IIR) {
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];
1402 for (i = 0; i < ctx->number_of_samples; i++) {
1403 *lpc_samples++ = *sample_buffer;
1404 sample_buffer += ctx->num_channels;
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);
1414 fp->shift = shift[order-1];
1416 for (i = 0; i < order; i++)
1417 fcoeff[i] = coefs[order-1][i];
1419 code_filter_coeffs(ctx, fp, fcoeff);
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.
1427 static void determine_filters(MLPEncodeContext *ctx)
1429 RestartHeader *rh = ctx->cur_restart_header;
1430 int channel, filter;
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);
1439 MLP_CHMODE_LEFT_RIGHT,
1440 MLP_CHMODE_LEFT_SIDE,
1441 MLP_CHMODE_RIGHT_SIDE,
1442 MLP_CHMODE_MID_SIDE,
1445 static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
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;
1451 enum MLPChMode best = 0;
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];
1457 sum[0] += FFABS( left );
1458 sum[1] += FFABS( right);
1459 sum[2] += FFABS((left + right) >> 1);
1460 sum[3] += FFABS( left - right);
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];
1468 for(i = 1; i < 3; i++)
1469 if(score[i] < score[best])
1475 /** Determines how many fractional bits are needed to encode matrix
1476 * coefficients. Also shifts the coefficients to fit within 2.14 bits.
1478 static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
1480 DecodingParams *dp = ctx->cur_decoding_params;
1481 MatrixParams *mp = &dp->matrix_params;
1482 int32_t coeff_mask = 0;
1483 unsigned int channel;
1486 for (channel = 0; channel < ctx->num_channels; channel++) {
1487 int32_t coeff = mp->coeff[mat][channel];
1488 coeff_mask |= coeff;
1491 for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
1493 mp->fbits [mat] = 14 - bits;
1496 /** Determines best coefficients to use for the lossless matrix. */
1497 static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
1499 DecodingParams *dp = ctx->cur_decoding_params;
1500 MatrixParams *mp = &dp->matrix_params;
1501 unsigned int shift = 0;
1502 unsigned int channel;
1504 enum MLPChMode mode;
1506 /* No decorrelation for non-stereo. */
1507 if (ctx->num_channels - 2 != 2) {
1512 mode = estimate_stereo_mode(ctx);
1515 /* TODO: add matrix for MID_SIDE */
1516 case MLP_CHMODE_MID_SIDE:
1517 case MLP_CHMODE_LEFT_RIGHT:
1520 case MLP_CHMODE_LEFT_SIDE:
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;
1528 case MLP_CHMODE_RIGHT_SIDE:
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;
1538 for (mat = 0; mat < mp->count; mat++)
1539 code_matrix_coeffs(ctx, mat);
1541 for (channel = 0; channel < ctx->num_channels; channel++)
1542 mp->shift[channel] = shift;
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.
1550 static const int codebook_extremes[3][2] = {
1551 {-9, 8}, {-8, 7}, {-15, 14},
1554 /** Determines the amount of bits needed to encode the samples using no
1555 * codebooks and a specified offset.
1557 static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1558 unsigned int channel, int16_t offset,
1559 int32_t min, int32_t max,
1562 DecodingParams *dp = ctx->cur_decoding_params;
1569 lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
1571 lsb_bits += !!lsb_bits;
1574 unsign = 1 << (lsb_bits - 1);
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;
1583 /** Determines the least amount of bits needed to encode the samples using no
1586 static void no_codebook_bits(MLPEncodeContext *ctx,
1587 unsigned int channel,
1588 int32_t min, int32_t max,
1591 DecodingParams *dp = ctx->cur_decoding_params;
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);
1604 /* Determine offset and minimum number of bits. */
1607 lsb_bits = number_sbits(diff) - 1;
1610 unsign = 1 << (lsb_bits - 1);
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;
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;
1623 /** Determines the least amount of bits needed to encode the samples using a
1624 * given codebook and a given offset.
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)
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;
1642 sample_min -= offset;
1643 sample_max -= offset;
1645 while (sample_min < codebook_min || sample_max > codebook_max) {
1651 unsign = 1 << lsb_bits;
1654 if (codebook == 2) {
1655 unsign_offset -= unsign;
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;
1663 sample -= unsign_offset;
1665 temp_min = sample & mask;
1666 if (temp_min < offset_min)
1667 offset_min = temp_min;
1669 temp_max = unsign - temp_min - 1;
1670 if (temp_max < offset_max)
1671 offset_max = temp_max;
1673 sample >>= lsb_bits;
1675 bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
1677 sample_buffer += ctx->num_channels;
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);
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.
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)
1695 int previous_count = INT_MAX;
1696 int offset_min, offset_max;
1699 offset_min = FFMAX(min, HUFF_OFFSET_MIN);
1700 offset_max = FFMIN(max, HUFF_OFFSET_MAX);
1702 while (offset <= offset_max && offset >= offset_min) {
1705 codebook_bits_offset(ctx, channel, codebook,
1709 if (temp_bo.bitcount < previous_count) {
1710 if (temp_bo.bitcount < bo->bitcount)
1714 } else if (++is_greater >= ctx->max_codebook_search)
1717 previous_count = temp_bo.bitcount;
1720 offset = temp_bo.max + 1;
1722 offset = temp_bo.min - 1;
1727 /** Determines the least amount of bits needed to encode the samples using
1728 * any or no codebook.
1730 static void determine_bits(MLPEncodeContext *ctx)
1732 DecodingParams *dp = ctx->cur_decoding_params;
1733 RestartHeader *rh = ctx->cur_restart_header;
1734 unsigned int channel;
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;
1745 /* Determine extremes and average. */
1746 for (i = 0; i < dp->blocksize; i++) {
1747 int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1753 sample_buffer += ctx->num_channels;
1755 average /= dp->blocksize;
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);
1763 no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
1766 for (i = 1; i < NUM_CODEBOOKS; i++) {
1767 BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1770 codebook_bits_offset(ctx, channel, i - 1,
1774 if (no_filters_used) {
1775 offset_max = temp_bo.max;
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);
1783 ctx->cur_best_offset[channel][i] = temp_bo;
1788 /****************************************************************************
1789 *************** Functions that process the data in some way ****************
1790 ****************************************************************************/
1792 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1793 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1795 #define MSB_MASK(bits) (-(1u << (bits)))
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.
1802 static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
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;
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");
1824 for (i = 0; i < 8; i++) {
1825 filter_state_buffer[FIR][i] = *sample_buffer;
1826 filter_state_buffer[IIR][i] = *sample_buffer;
1828 sample_buffer += ctx->num_channels;
1831 for (i = 8; i < number_of_samples; i++) {
1832 int32_t sample = *sample_buffer;
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] *
1844 accum >>= filter_shift;
1845 residual = sample - (accum & mask);
1847 if (residual < SAMPLE_MIN(24) || residual > SAMPLE_MAX(24)) {
1849 goto free_and_return;
1852 filter_state_buffer[FIR][i] = sample;
1853 filter_state_buffer[IIR][i] = (int32_t) residual;
1855 sample_buffer += ctx->num_channels;
1858 sample_buffer = ctx->sample_buffer + channel;
1859 for (i = 0; i < number_of_samples; i++) {
1860 *sample_buffer = filter_state_buffer[IIR][i];
1862 sample_buffer += ctx->num_channels;
1866 for (i = 0; i < NUM_FILTERS; i++) {
1867 av_freep(&filter_state_buffer[i]);
1873 static void apply_filters(MLPEncodeContext *ctx)
1875 RestartHeader *rh = ctx->cur_restart_header;
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);
1889 /** Generates two noise channels worth of data. */
1890 static void generate_2_noise_channels(MLPEncodeContext *ctx)
1892 int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
1893 RestartHeader *rh = ctx->cur_restart_header;
1895 uint32_t seed = rh->noisegen_seed;
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);
1902 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1904 sample_buffer += ctx->num_channels - 2;
1907 rh->noisegen_seed = seed & ((1 << 24)-1);
1910 /** Rematrixes all channels using chosen coefficients. */
1911 static void rematrix_channels(MLPEncodeContext *ctx)
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;
1918 maxchan = ctx->num_channels;
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];
1925 sample_buffer = ctx->sample_buffer;
1926 for (i = 0; i < ctx->number_of_samples; i++) {
1927 unsigned int src_ch;
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];
1934 sample_buffer[outch] = (accum >> 14) & mask;
1936 sample_buffer += ctx->num_channels;
1941 /****************************************************************************
1942 **** Functions that deal with determining the best parameters and output ***
1943 ****************************************************************************/
1946 char path[MAJOR_HEADER_INTERVAL + 3];
1950 static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
1952 #define ZERO_PATH '0'
1953 #define CODEBOOK_CHANGE_BITS 21
1955 static void clear_path_counter(PathCounter *path_counter)
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;
1966 static int compare_best_offset(BestOffset *prev, BestOffset *cur)
1968 if (prev->lsb_bits != cur->lsb_bits)
1974 static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1975 PathCounter *src, int cur_codebook)
1977 BestOffset *cur_bo, *prev_bo = restart_best_offset;
1978 int bitcount = src->bitcount;
1979 char *path = src->path + 1;
1983 for (i = 0; path[i]; i++)
1984 prev_bo = ctx->best_offset[i][channel];
1986 prev_codebook = path[i - 1] - ZERO_PATH;
1988 cur_bo = ctx->best_offset[i][channel];
1990 bitcount += cur_bo[cur_codebook].bitcount;
1992 if (prev_codebook != cur_codebook ||
1993 compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
1994 bitcount += CODEBOOK_CHANGE_BITS;
1999 static void set_best_codebook(MLPEncodeContext *ctx)
2001 DecodingParams *dp = ctx->cur_decoding_params;
2002 RestartHeader *rh = ctx->cur_restart_header;
2003 unsigned int channel;
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;
2012 clear_path_counter(path_counter);
2014 for (index = 0; index < ctx->number_of_subblocks; index++) {
2015 unsigned int best_bitcount = INT_MAX;
2016 unsigned int codebook;
2018 cur_bo = ctx->best_offset[index][channel];
2020 for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
2021 int prev_best_bitcount = INT_MAX;
2024 for (last_best = 0; last_best < 2; last_best++) {
2025 PathCounter *dst_path = &path_counter[codebook];
2026 PathCounter *src_path;
2029 /* First test last path with same headers,
2030 * then with last best. */
2032 src_path = &path_counter[NUM_CODEBOOKS];
2034 if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
2037 src_path = &path_counter[codebook];
2040 temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
2042 if (temp_bitcount < best_bitcount) {
2043 best_bitcount = temp_bitcount;
2044 best_codebook = codebook;
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;
2059 memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
2062 best_path = path_counter[NUM_CODEBOOKS].path + 1;
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;
2068 best_codebook = *best_path++ - ZERO_PATH;
2069 cur_bo = &ctx->best_offset[index][channel][best_codebook];
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;
2078 /** Analyzes all collected bitcounts and selects the best parameters for each
2079 * individual access unit.
2080 * TODO This is just a stub!
2082 static void set_major_params(MLPEncodeContext *ctx)
2084 RestartHeader *rh = ctx->cur_restart_header;
2086 unsigned int substr;
2087 uint8_t max_huff_lsbs = 0;
2088 uint8_t max_output_bits = 0;
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);
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));
2112 rh->max_huff_lsbs = max_huff_lsbs;
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;
2119 for (substr = 0; substr < ctx->num_substreams; substr++) {
2121 ctx->cur_restart_header = &ctx->restart_header[substr];
2123 ctx->prev_decoding_params = &restart_decoding_params[substr];
2124 ctx->prev_channel_params = restart_channel_params;
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];
2130 ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
2132 ctx->prev_decoding_params = ctx->cur_decoding_params;
2133 ctx->prev_channel_params = ctx->cur_channel_params;
2137 ctx->major_number_of_subblocks = ctx->number_of_subblocks;
2138 ctx->major_filter_state_subblock = 1;
2139 ctx->major_cur_subblock_index = 0;
2142 static void analyze_sample_buffer(MLPEncodeContext *ctx)
2144 ChannelParams *seq_cp = ctx->seq_channel_params;
2145 DecodingParams *seq_dp = ctx->seq_decoding_params;
2147 unsigned int substr;
2149 for (substr = 0; substr < ctx->num_substreams; substr++) {
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);
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);
2162 copy_restart_frame_params(ctx, substr);
2164 /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2165 * decoding_params[0] is for the filter state subblock.
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];
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.
2175 (seq_dp + substr)->blocksize = 8;
2176 (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
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;
2186 set_best_codebook(ctx);
2190 static void process_major_frame(MLPEncodeContext *ctx)
2192 unsigned int substr;
2194 ctx->sample_buffer = ctx->major_inout_buffer;
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;
2200 for (substr = 0; substr < ctx->num_substreams; substr++) {
2201 ctx->cur_restart_header = &ctx->restart_header[substr];
2203 ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
2204 ctx->cur_channel_params = ctx->major_channel_params[1];
2206 generate_2_noise_channels(ctx);
2207 rematrix_channels (ctx);
2213 /****************************************************************************/
2215 static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2216 const AVFrame *frame, int *got_packet)
2218 MLPEncodeContext *ctx = avctx->priv_data;
2219 unsigned int bytes_written = 0;
2220 int restart_frame, ret;
2223 if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
2229 /* add current frame to queue */
2230 if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
2233 data = frame->data[0];
2235 ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
2237 ctx->inout_buffer = ctx->major_inout_buffer
2238 + ctx->frame_index * ctx->one_sample_buffer_size;
2240 if (ctx->last_frame == ctx->inout_buffer) {
2244 ctx->sample_buffer = ctx->major_scratch_buffer
2245 + ctx->frame_index * ctx->one_sample_buffer_size;
2247 ctx->write_buffer = ctx->inout_buffer;
2249 if (avctx->frame_number < ctx->max_restart_interval) {
2251 goto input_and_return;
2253 /* There are less frames than the requested major header interval.
2254 * Update the context to reflect this.
2256 ctx->max_restart_interval = avctx->frame_number;
2257 ctx->frame_index = 0;
2259 ctx->sample_buffer = ctx->major_scratch_buffer;
2260 ctx->inout_buffer = ctx->major_inout_buffer;
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);
2270 restart_frame = !ctx->frame_index;
2272 if (restart_frame) {
2273 set_major_params(ctx);
2274 if (ctx->min_restart_interval != ctx->max_restart_interval)
2275 process_major_frame(ctx);
2278 if (ctx->min_restart_interval == ctx->max_restart_interval)
2279 ctx->write_buffer = ctx->sample_buffer;
2281 bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
2283 ctx->timestamp += ctx->frame_size[ctx->frame_index];
2284 ctx->dts += ctx->frame_size[ctx->frame_index];
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;
2297 restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
2299 if (!restart_frame) {
2303 seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
2305 unsigned int number_of_samples = 0;
2308 ctx->sample_buffer = ctx->major_scratch_buffer;
2309 ctx->inout_buffer = ctx->major_inout_buffer;
2310 ctx->seq_index = seq_index;
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;
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);
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);
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];
2328 ctx->number_of_samples = number_of_samples;
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));
2335 input_to_sample_buffer(ctx);
2337 analyze_sample_buffer(ctx);
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;
2346 if (!ctx->major_frame_size)
2353 ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
2355 avpkt->size = bytes_written;
2360 static av_cold int mlp_encode_close(AVCodecContext *avctx)
2362 MLPEncodeContext *ctx = avctx->priv_data;
2364 ff_lpc_end(&ctx->lpc_ctx);
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);
2378 #if CONFIG_MLP_ENCODER
2379 AVCodec ff_mlp_encoder = {
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,
2394 #if CONFIG_TRUEHD_ENCODER
2395 AVCodec ff_truehd_encoder = {
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},