3 * Copyright (c) 2008 Ramiro Polla
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "audio_frame_queue.h"
26 #include "libavutil/crc.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/samplefmt.h"
32 #define MAJOR_HEADER_INTERVAL 16
34 #define MLP_MIN_LPC_ORDER 1
35 #define MLP_MAX_LPC_ORDER 8
36 #define MLP_MIN_LPC_SHIFT 8
37 #define MLP_MAX_LPC_SHIFT 15
40 uint8_t min_channel; ///< The index of the first channel coded in this substream.
41 uint8_t max_channel; ///< The index of the last channel coded in this substream.
42 uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
44 uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
45 uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
47 int data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
49 int32_t lossless_check_data; ///< XOR of all output samples
51 uint8_t max_huff_lsbs; ///< largest huff_lsbs
52 uint8_t max_output_bits; ///< largest output bit-depth
56 uint8_t count; ///< number of matrices to apply
58 uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
59 int32_t forco[MAX_MATRICES][MAX_CHANNELS+2]; ///< forward coefficients
60 int32_t coeff[MAX_MATRICES][MAX_CHANNELS+2]; ///< decoding coefficients
61 uint8_t fbits[MAX_CHANNELS]; ///< fraction bits
63 int8_t shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
67 PARAMS_DEFAULT = 0xff,
68 PARAM_PRESENCE_FLAGS = 1 << 8,
69 PARAM_BLOCKSIZE = 1 << 7,
70 PARAM_MATRIX = 1 << 6,
71 PARAM_OUTSHIFT = 1 << 5,
72 PARAM_QUANTSTEP = 1 << 4,
75 PARAM_HUFFOFFSET = 1 << 1,
76 PARAM_PRESENT = 1 << 0,
80 uint16_t blocksize; ///< number of PCM samples in current audio block
81 uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
83 MatrixParams matrix_params;
85 uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
88 typedef struct BestOffset {
96 #define HUFF_OFFSET_MIN -16384
97 #define HUFF_OFFSET_MAX 16383
99 /** Number of possible codebooks (counting "no codebooks") */
100 #define NUM_CODEBOOKS 4
103 AVCodecContext *avctx;
105 int num_substreams; ///< Number of substreams contained within this stream.
107 int num_channels; /**< Number of channels in major_scratch_buffer.
108 * Normal channels + noise channels. */
110 int coded_sample_fmt [2]; ///< sample format encoded for MLP
111 int coded_sample_rate[2]; ///< sample rate encoded for MLP
112 int coded_peak_bitrate; ///< peak bitrate for this major sync header
114 int flags; ///< major sync info flags
116 /* channel_meaning */
120 int channel_occupancy;
123 int32_t *inout_buffer; ///< Pointer to data currently being read from lavc or written to bitstream.
124 int32_t *major_inout_buffer; ///< Buffer with all in/out data for one entire major frame interval.
125 int32_t *write_buffer; ///< Pointer to data currently being written to bitstream.
126 int32_t *sample_buffer; ///< Pointer to current access unit samples.
127 int32_t *major_scratch_buffer; ///< Scratch buffer big enough to fit all data for one entire major frame interval.
128 int32_t *last_frame; ///< Pointer to last frame with data to encode.
130 int32_t *lpc_sample_buffer;
132 unsigned int major_number_of_frames;
133 unsigned int next_major_number_of_frames;
135 unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
136 unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
138 int32_t *lossless_check_data; ///< Array with lossless_check_data for each access unit.
140 unsigned int *max_output_bits; ///< largest output bit-depth
141 unsigned int *frame_size; ///< Array with number of samples/channel in each access unit.
142 unsigned int frame_index; ///< Index of current frame being encoded.
144 unsigned int one_sample_buffer_size; ///< Number of samples*channel for one access unit.
146 unsigned int max_restart_interval; ///< Max interval of access units in between two major frames.
147 unsigned int min_restart_interval; ///< Min interval of access units in between two major frames.
148 unsigned int restart_intervals; ///< Number of possible major frame sizes.
150 uint16_t timestamp; ///< Timestamp of current access unit.
151 uint16_t dts; ///< Decoding timestamp of current access unit.
153 uint8_t channel_arrangement; ///< channel arrangement for MLP streams
155 uint8_t ch_modifier_thd0; ///< channel modifier for TrueHD stream 0
156 uint8_t ch_modifier_thd1; ///< channel modifier for TrueHD stream 1
157 uint8_t ch_modifier_thd2; ///< channel modifier for TrueHD stream 2
159 unsigned int seq_size [MAJOR_HEADER_INTERVAL];
160 unsigned int seq_offset[MAJOR_HEADER_INTERVAL];
161 unsigned int sequence_size;
163 ChannelParams *channel_params;
165 BestOffset best_offset[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS][NUM_CODEBOOKS];
167 DecodingParams *decoding_params;
168 RestartHeader restart_header [MAX_SUBSTREAMS];
170 ChannelParams major_channel_params[MAJOR_HEADER_INTERVAL+1][MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
171 DecodingParams major_decoding_params[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< DecodingParams to be written to bitstream.
172 int major_params_changed[MAJOR_HEADER_INTERVAL+1][MAX_SUBSTREAMS]; ///< params_changed to be written to bitstream.
174 unsigned int major_cur_subblock_index;
175 unsigned int major_filter_state_subblock;
176 unsigned int major_number_of_subblocks;
178 BestOffset (*cur_best_offset)[NUM_CODEBOOKS];
179 ChannelParams *cur_channel_params;
180 DecodingParams *cur_decoding_params;
181 RestartHeader *cur_restart_header;
185 /* Analysis stage. */
186 unsigned int starting_frame_index;
187 unsigned int number_of_frames;
188 unsigned int number_of_samples;
189 unsigned int number_of_subblocks;
190 unsigned int seq_index; ///< Sequence index for high compression levels.
192 ChannelParams *prev_channel_params;
193 DecodingParams *prev_decoding_params;
195 ChannelParams *seq_channel_params;
196 DecodingParams *seq_decoding_params;
198 unsigned int max_codebook_search;
203 static ChannelParams restart_channel_params[MAX_CHANNELS];
204 static DecodingParams restart_decoding_params[MAX_SUBSTREAMS];
205 static BestOffset restart_best_offset[NUM_CODEBOOKS] = {{0}};
207 #define SYNC_MAJOR 0xf8726f
208 #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
210 #define SYNC_MLP 0xbb
211 #define SYNC_TRUEHD 0xba
213 /* must be set for DVD-A */
214 #define FLAGS_DVDA 0x4000
215 /* FIFO delay must be constant */
216 #define FLAGS_CONST 0x8000
218 #define SUBSTREAM_INFO_MAX_2_CHAN 0x01
219 #define SUBSTREAM_INFO_HIGH_RATE 0x02
220 #define SUBSTREAM_INFO_ALWAYS_SET 0x04
221 #define SUBSTREAM_INFO_2_SUBSTREAMS 0x08
223 /****************************************************************************
224 ************ Functions that copy, clear, or compare parameters *************
225 ****************************************************************************/
227 /** Compares two FilterParams structures and returns 1 if anything has
228 * changed. Returns 0 if they are both equal.
230 static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
232 const FilterParams *prev = &prev_cp->filter_params[filter];
233 const FilterParams *fp = &cp->filter_params[filter];
236 if (prev->order != fp->order)
242 if (prev->shift != fp->shift)
245 for (i = 0; i < fp->order; i++)
246 if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
252 /** Compare two primitive matrices and returns 1 if anything has changed.
253 * Returns 0 if they are both equal.
255 static int compare_matrix_params(MLPEncodeContext *ctx, const MatrixParams *prev, const MatrixParams *mp)
257 RestartHeader *rh = ctx->cur_restart_header;
258 unsigned int channel, mat;
260 if (prev->count != mp->count)
266 for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
267 if (prev->fbits[channel] != mp->fbits[channel])
270 for (mat = 0; mat < mp->count; mat++) {
271 if (prev->outch[mat] != mp->outch[mat])
274 for (channel = 0; channel < ctx->num_channels; channel++)
275 if (prev->coeff[mat][channel] != mp->coeff[mat][channel])
282 /** Compares two DecodingParams and ChannelParams structures to decide if a
283 * new decoding params header has to be written.
285 static int compare_decoding_params(MLPEncodeContext *ctx)
287 DecodingParams *prev = ctx->prev_decoding_params;
288 DecodingParams *dp = ctx->cur_decoding_params;
289 MatrixParams *prev_mp = &prev->matrix_params;
290 MatrixParams *mp = &dp->matrix_params;
291 RestartHeader *rh = ctx->cur_restart_header;
295 if (prev->param_presence_flags != dp->param_presence_flags)
296 retval |= PARAM_PRESENCE_FLAGS;
298 if (prev->blocksize != dp->blocksize)
299 retval |= PARAM_BLOCKSIZE;
301 if (compare_matrix_params(ctx, prev_mp, mp))
302 retval |= PARAM_MATRIX;
304 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
305 if (prev_mp->shift[ch] != mp->shift[ch]) {
306 retval |= PARAM_OUTSHIFT;
310 for (ch = 0; ch <= rh->max_channel; ch++)
311 if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
312 retval |= PARAM_QUANTSTEP;
316 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
317 ChannelParams *prev_cp = &ctx->prev_channel_params[ch];
318 ChannelParams *cp = &ctx->cur_channel_params[ch];
320 if (!(retval & PARAM_FIR) &&
321 compare_filter_params(prev_cp, cp, FIR))
324 if (!(retval & PARAM_IIR) &&
325 compare_filter_params(prev_cp, cp, IIR))
328 if (prev_cp->huff_offset != cp->huff_offset)
329 retval |= PARAM_HUFFOFFSET;
331 if (prev_cp->codebook != cp->codebook ||
332 prev_cp->huff_lsbs != cp->huff_lsbs )
339 static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
341 FilterParams *dst = &dst_cp->filter_params[filter];
342 FilterParams *src = &src_cp->filter_params[filter];
345 dst->order = src->order;
348 dst->shift = src->shift;
350 dst->coeff_shift = src->coeff_shift;
351 dst->coeff_bits = src->coeff_bits;
354 for (order = 0; order < dst->order; order++)
355 dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
358 static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
360 dst->count = src->count;
363 unsigned int channel, count;
365 for (channel = 0; channel < MAX_CHANNELS; channel++) {
367 dst->fbits[channel] = src->fbits[channel];
368 dst->shift[channel] = src->shift[channel];
370 for (count = 0; count < MAX_MATRICES; count++)
371 dst->coeff[count][channel] = src->coeff[count][channel];
374 for (count = 0; count < MAX_MATRICES; count++)
375 dst->outch[count] = src->outch[count];
379 static void copy_restart_frame_params(MLPEncodeContext *ctx,
384 for (index = 0; index < ctx->number_of_subblocks; index++) {
385 DecodingParams *dp = ctx->seq_decoding_params + index*(ctx->num_substreams) + substr;
386 unsigned int channel;
388 copy_matrix_params(&dp->matrix_params, &ctx->cur_decoding_params->matrix_params);
390 for (channel = 0; channel < ctx->avctx->channels; channel++) {
391 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
394 dp->quant_step_size[channel] = ctx->cur_decoding_params->quant_step_size[channel];
395 dp->matrix_params.shift[channel] = ctx->cur_decoding_params->matrix_params.shift[channel];
398 for (filter = 0; filter < NUM_FILTERS; filter++)
399 copy_filter_params(cp, &ctx->cur_channel_params[channel], filter);
404 /** Clears a DecodingParams struct the way it should be after a restart header. */
405 static void clear_decoding_params(MLPEncodeContext *ctx, DecodingParams decoding_params[MAX_SUBSTREAMS])
409 for (substr = 0; substr < ctx->num_substreams; substr++) {
410 DecodingParams *dp = &decoding_params[substr];
412 dp->param_presence_flags = 0xff;
415 memset(&dp->matrix_params , 0, sizeof(MatrixParams ));
416 memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
420 /** Clears a ChannelParams struct the way it should be after a restart header. */
421 static void clear_channel_params(MLPEncodeContext *ctx, ChannelParams channel_params[MAX_CHANNELS])
423 unsigned int channel;
425 for (channel = 0; channel < ctx->avctx->channels; channel++) {
426 ChannelParams *cp = &channel_params[channel];
428 memset(&cp->filter_params, 0, sizeof(cp->filter_params));
430 /* Default audio coding is 24-bit raw PCM. */
437 /** Sets default vales in our encoder for a DecodingParams struct. */
438 static void default_decoding_params(MLPEncodeContext *ctx,
439 DecodingParams decoding_params[MAX_SUBSTREAMS])
443 clear_decoding_params(ctx, decoding_params);
445 for (substr = 0; substr < ctx->num_substreams; substr++) {
446 DecodingParams *dp = &decoding_params[substr];
447 uint8_t param_presence_flags = 0;
449 param_presence_flags |= PARAM_BLOCKSIZE;
450 param_presence_flags |= PARAM_MATRIX;
451 param_presence_flags |= PARAM_OUTSHIFT;
452 param_presence_flags |= PARAM_QUANTSTEP;
453 param_presence_flags |= PARAM_FIR;
454 /* param_presence_flags |= PARAM_IIR; */
455 param_presence_flags |= PARAM_HUFFOFFSET;
456 param_presence_flags |= PARAM_PRESENT;
458 dp->param_presence_flags = param_presence_flags;
462 /****************************************************************************/
464 /** Calculates the smallest number of bits it takes to encode a given signed
465 * value in two's complement.
467 static int inline number_sbits(int number)
472 return av_log2(FFABS(number)) + 1 + !!number;
481 static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
483 return ((peak_bitrate << 4) - 8) / sample_rate;
486 static av_cold int mlp_encode_init(AVCodecContext *avctx)
488 MLPEncodeContext *ctx = avctx->priv_data;
489 unsigned int substr, index;
490 unsigned int sum = 0;
496 switch (avctx->sample_rate) {
498 avctx->frame_size = 40 << 0;
499 ctx->coded_sample_rate[0] = 0x08 + 0;
503 avctx->frame_size = 40 << 1;
504 ctx->coded_sample_rate[0] = 0x08 + 1;
508 ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
509 avctx->frame_size = 40 << 2;
510 ctx->coded_sample_rate[0] = 0x08 + 2;
514 avctx->frame_size = 40 << 0;
515 ctx->coded_sample_rate[0] = 0x00 + 0;
519 avctx->frame_size = 40 << 1;
520 ctx->coded_sample_rate[0] = 0x00 + 1;
524 ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
525 avctx->frame_size = 40 << 2;
526 ctx->coded_sample_rate[0] = 0x00 + 2;
530 av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
531 "sample rates are 44100, 88200, 176400, 48000, "
532 "96000, and 192000.\n", avctx->sample_rate);
535 ctx->coded_sample_rate[1] = -1 & 0xf;
537 /* TODO Keep count of bitrate and calculate real value. */
538 ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
540 /* TODO support more channels. */
541 if (avctx->channels > 2) {
542 av_log(avctx, AV_LOG_WARNING,
543 "Only mono and stereo are supported at the moment.\n");
546 ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
547 if (avctx->channels <= 2) {
548 ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
551 switch (avctx->sample_fmt) {
552 case AV_SAMPLE_FMT_S16:
553 ctx->coded_sample_fmt[0] = BITS_16;
554 ctx->wordlength = 16;
555 avctx->bits_per_raw_sample = 16;
558 case AV_SAMPLE_FMT_S32:
559 ctx->coded_sample_fmt[0] = BITS_24;
560 ctx->wordlength = 24;
561 avctx->bits_per_raw_sample = 24;
564 av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
565 "Only 16- and 24-bit samples are supported.\n");
568 ctx->coded_sample_fmt[1] = -1 & 0xf;
570 ctx->dts = -avctx->frame_size;
572 ctx->num_channels = avctx->channels + 2; /* +2 noise channels */
573 ctx->one_sample_buffer_size = avctx->frame_size
575 /* TODO Let user pass major header interval as parameter. */
576 ctx->max_restart_interval = MAJOR_HEADER_INTERVAL;
578 ctx->max_codebook_search = 3;
579 ctx->min_restart_interval = MAJOR_HEADER_INTERVAL;
580 ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
582 /* TODO Let user pass parameters for LPC filter. */
584 size = avctx->frame_size * ctx->max_restart_interval;
586 ctx->lpc_sample_buffer = av_malloc_array(size, sizeof(int32_t));
587 if (!ctx->lpc_sample_buffer) {
588 av_log(avctx, AV_LOG_ERROR,
589 "Not enough memory for buffering samples.\n");
590 return AVERROR(ENOMEM);
593 size = ctx->one_sample_buffer_size * ctx->max_restart_interval;
595 ctx->major_scratch_buffer = av_malloc_array(size, sizeof(int32_t));
596 if (!ctx->major_scratch_buffer) {
597 av_log(avctx, AV_LOG_ERROR,
598 "Not enough memory for buffering samples.\n");
599 return AVERROR(ENOMEM);
602 ctx->major_inout_buffer = av_malloc_array(size, sizeof(int32_t));
603 if (!ctx->major_inout_buffer) {
604 av_log(avctx, AV_LOG_ERROR,
605 "Not enough memory for buffering samples.\n");
606 return AVERROR(ENOMEM);
611 ctx->num_substreams = 1; // TODO: change this after adding multi-channel support for TrueHD
613 if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
615 switch(avctx->channel_layout) {
616 case AV_CH_LAYOUT_MONO:
617 ctx->channel_arrangement = 0; break;
618 case AV_CH_LAYOUT_STEREO:
619 ctx->channel_arrangement = 1; break;
620 case AV_CH_LAYOUT_2_1:
621 ctx->channel_arrangement = 2; break;
622 case AV_CH_LAYOUT_QUAD:
623 ctx->channel_arrangement = 3; break;
624 case AV_CH_LAYOUT_2POINT1:
625 ctx->channel_arrangement = 4; break;
626 case AV_CH_LAYOUT_SURROUND:
627 ctx->channel_arrangement = 7; break;
628 case AV_CH_LAYOUT_4POINT0:
629 ctx->channel_arrangement = 8; break;
630 case AV_CH_LAYOUT_5POINT0_BACK:
631 ctx->channel_arrangement = 9; break;
632 case AV_CH_LAYOUT_3POINT1:
633 ctx->channel_arrangement = 10; break;
634 case AV_CH_LAYOUT_4POINT1:
635 ctx->channel_arrangement = 11; break;
636 case AV_CH_LAYOUT_5POINT1_BACK:
637 ctx->channel_arrangement = 12; break;
639 av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
642 ctx->flags = FLAGS_DVDA;
643 ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
644 ctx->summary_info = ff_mlp_ch_info[ctx->channel_arrangement].summary_info ;
647 switch(avctx->channel_layout) {
648 case AV_CH_LAYOUT_STEREO:
649 ctx->ch_modifier_thd0 = 0;
650 ctx->ch_modifier_thd1 = 0;
651 ctx->ch_modifier_thd2 = 0;
652 ctx->channel_arrangement = 1;
654 case AV_CH_LAYOUT_5POINT0_BACK:
655 ctx->ch_modifier_thd0 = 1;
656 ctx->ch_modifier_thd1 = 1;
657 ctx->ch_modifier_thd2 = 1;
658 ctx->channel_arrangement = 11;
660 case AV_CH_LAYOUT_5POINT1_BACK:
661 ctx->ch_modifier_thd0 = 2;
662 ctx->ch_modifier_thd1 = 1;
663 ctx->ch_modifier_thd2 = 2;
664 ctx->channel_arrangement = 15;
667 av_log(avctx, AV_LOG_ERROR, "Unsupported channel arrangement\n");
671 ctx->channel_occupancy = 0;
672 ctx->summary_info = 0;
675 size = sizeof(unsigned int) * ctx->max_restart_interval;
677 ctx->frame_size = av_malloc(size);
678 if (!ctx->frame_size)
679 return AVERROR(ENOMEM);
681 ctx->max_output_bits = av_malloc(size);
682 if (!ctx->max_output_bits)
683 return AVERROR(ENOMEM);
685 size = sizeof(int32_t)
686 * ctx->num_substreams * ctx->max_restart_interval;
688 ctx->lossless_check_data = av_malloc(size);
689 if (!ctx->lossless_check_data)
690 return AVERROR(ENOMEM);
692 for (index = 0; index < ctx->restart_intervals; index++) {
693 ctx->seq_offset[index] = sum;
694 ctx->seq_size [index] = ((index + 1) * ctx->min_restart_interval) + 1;
695 sum += ctx->seq_size[index];
697 ctx->sequence_size = sum;
698 size = sizeof(ChannelParams)
699 * ctx->restart_intervals * ctx->sequence_size * ctx->avctx->channels;
700 ctx->channel_params = av_malloc(size);
701 if (!ctx->channel_params) {
702 av_log(avctx, AV_LOG_ERROR,
703 "Not enough memory for analysis context.\n");
704 return AVERROR(ENOMEM);
707 size = sizeof(DecodingParams)
708 * ctx->restart_intervals * ctx->sequence_size * ctx->num_substreams;
709 ctx->decoding_params = av_malloc(size);
710 if (!ctx->decoding_params) {
711 av_log(avctx, AV_LOG_ERROR,
712 "Not enough memory for analysis context.\n");
713 return AVERROR(ENOMEM);
716 for (substr = 0; substr < ctx->num_substreams; substr++) {
717 RestartHeader *rh = &ctx->restart_header [substr];
719 /* TODO see if noisegen_seed is really worth it. */
720 rh->noisegen_seed = 0;
723 rh->max_channel = avctx->channels - 1;
724 /* FIXME: this works for 1 and 2 channels, but check for more */
725 rh->max_matrix_channel = rh->max_channel;
728 clear_channel_params(ctx, restart_channel_params);
729 clear_decoding_params(ctx, restart_decoding_params);
731 if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->number_of_samples,
732 MLP_MAX_LPC_ORDER, FF_LPC_TYPE_LEVINSON)) < 0) {
733 av_log(avctx, AV_LOG_ERROR,
734 "Not enough memory for LPC context.\n");
738 ff_af_queue_init(avctx, &ctx->afq);
743 /****************************************************************************
744 ****************** Functions that write to the bitstream *******************
745 ****************************************************************************/
747 /** Writes a major sync header to the bitstream. */
748 static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
752 init_put_bits(&pb, buf, buf_size);
754 put_bits(&pb, 24, SYNC_MAJOR );
756 if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
757 put_bits(&pb, 8, SYNC_MLP );
758 put_bits(&pb, 4, ctx->coded_sample_fmt [0]);
759 put_bits(&pb, 4, ctx->coded_sample_fmt [1]);
760 put_bits(&pb, 4, ctx->coded_sample_rate[0]);
761 put_bits(&pb, 4, ctx->coded_sample_rate[1]);
762 put_bits(&pb, 4, 0 ); /* ignored */
763 put_bits(&pb, 4, 0 ); /* multi_channel_type */
764 put_bits(&pb, 3, 0 ); /* ignored */
765 put_bits(&pb, 5, ctx->channel_arrangement );
766 } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
767 put_bits(&pb, 8, SYNC_TRUEHD );
768 put_bits(&pb, 4, ctx->coded_sample_rate[0]);
769 put_bits(&pb, 4, 0 ); /* ignored */
770 put_bits(&pb, 2, ctx->ch_modifier_thd0 );
771 put_bits(&pb, 2, ctx->ch_modifier_thd1 );
772 put_bits(&pb, 5, ctx->channel_arrangement );
773 put_bits(&pb, 2, ctx->ch_modifier_thd2 );
774 put_bits(&pb, 13, ctx->channel_arrangement );
777 put_bits(&pb, 16, MAJOR_SYNC_INFO_SIGNATURE);
778 put_bits(&pb, 16, ctx->flags );
779 put_bits(&pb, 16, 0 ); /* ignored */
780 put_bits(&pb, 1, 1 ); /* is_vbr */
781 put_bits(&pb, 15, ctx->coded_peak_bitrate );
782 put_bits(&pb, 4, 1 ); /* num_substreams */
783 put_bits(&pb, 4, 0x1 ); /* ignored */
785 /* channel_meaning */
786 put_bits(&pb, 8, ctx->substream_info );
787 put_bits(&pb, 5, ctx->fs );
788 put_bits(&pb, 5, ctx->wordlength );
789 put_bits(&pb, 6, ctx->channel_occupancy );
790 put_bits(&pb, 3, 0 ); /* ignored */
791 put_bits(&pb, 10, 0 ); /* speaker_layout */
792 put_bits(&pb, 3, 0 ); /* copy_protection */
793 put_bits(&pb, 16, 0x8080 ); /* ignored */
794 put_bits(&pb, 7, 0 ); /* ignored */
795 put_bits(&pb, 4, 0 ); /* source_format */
796 put_bits(&pb, 5, ctx->summary_info );
800 AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
803 /** Writes a restart header to the bitstream. Damaged streams can start being
804 * decoded losslessly again after such a header and the subsequent decoding
807 static void write_restart_header(MLPEncodeContext *ctx, PutBitContext *pb)
809 RestartHeader *rh = ctx->cur_restart_header;
810 int32_t lossless_check = xor_32_to_8(rh->lossless_check_data);
811 unsigned int start_count = put_bits_count(pb);
816 put_bits(pb, 14, 0x31ea ); /* TODO 0x31eb */
817 put_bits(pb, 16, ctx->timestamp );
818 put_bits(pb, 4, rh->min_channel );
819 put_bits(pb, 4, rh->max_channel );
820 put_bits(pb, 4, rh->max_matrix_channel);
821 put_bits(pb, 4, rh->noise_shift );
822 put_bits(pb, 23, rh->noisegen_seed );
823 put_bits(pb, 4, 0 ); /* TODO max_shift */
824 put_bits(pb, 5, rh->max_huff_lsbs );
825 put_bits(pb, 5, rh->max_output_bits );
826 put_bits(pb, 5, rh->max_output_bits );
827 put_bits(pb, 1, rh->data_check_present);
828 put_bits(pb, 8, lossless_check );
829 put_bits(pb, 16, 0 ); /* ignored */
831 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
834 /* Data must be flushed for the checksum to be correct. */
836 flush_put_bits(&tmpb);
838 checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
840 put_bits(pb, 8, checksum);
843 /** Writes matrix params for all primitive matrices to the bitstream. */
844 static void write_matrix_params(MLPEncodeContext *ctx, PutBitContext *pb)
846 DecodingParams *dp = ctx->cur_decoding_params;
847 MatrixParams *mp = &dp->matrix_params;
850 put_bits(pb, 4, mp->count);
852 for (mat = 0; mat < mp->count; mat++) {
853 unsigned int channel;
855 put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
856 put_bits(pb, 4, mp->fbits[mat]);
857 put_bits(pb, 1, 0 ); /* lsb_bypass */
859 for (channel = 0; channel < ctx->num_channels; channel++) {
860 int32_t coeff = mp->coeff[mat][channel];
865 coeff >>= 14 - mp->fbits[mat];
867 put_sbits(pb, mp->fbits[mat] + 2, coeff);
875 /** Writes filter parameters for one filter to the bitstream. */
876 static void write_filter_params(MLPEncodeContext *ctx, PutBitContext *pb,
877 unsigned int channel, unsigned int filter)
879 FilterParams *fp = &ctx->cur_channel_params[channel].filter_params[filter];
881 put_bits(pb, 4, fp->order);
885 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
887 put_bits(pb, 4, fp->shift );
888 put_bits(pb, 5, fp->coeff_bits );
889 put_bits(pb, 3, fp->coeff_shift);
891 for (i = 0; i < fp->order; i++) {
892 put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
895 /* TODO state data for IIR filter. */
900 /** Writes decoding parameters to the bitstream. These change very often,
901 * usually at almost every frame.
903 static void write_decoding_params(MLPEncodeContext *ctx, PutBitContext *pb,
906 DecodingParams *dp = ctx->cur_decoding_params;
907 RestartHeader *rh = ctx->cur_restart_header;
908 MatrixParams *mp = &dp->matrix_params;
911 if (dp->param_presence_flags != PARAMS_DEFAULT &&
912 params_changed & PARAM_PRESENCE_FLAGS) {
914 put_bits(pb, 8, dp->param_presence_flags);
919 if (dp->param_presence_flags & PARAM_BLOCKSIZE) {
920 if (params_changed & PARAM_BLOCKSIZE) {
922 put_bits(pb, 9, dp->blocksize);
928 if (dp->param_presence_flags & PARAM_MATRIX) {
929 if (params_changed & PARAM_MATRIX) {
931 write_matrix_params(ctx, pb);
937 if (dp->param_presence_flags & PARAM_OUTSHIFT) {
938 if (params_changed & PARAM_OUTSHIFT) {
940 for (ch = 0; ch <= rh->max_matrix_channel; ch++)
941 put_sbits(pb, 4, mp->shift[ch]);
947 if (dp->param_presence_flags & PARAM_QUANTSTEP) {
948 if (params_changed & PARAM_QUANTSTEP) {
950 for (ch = 0; ch <= rh->max_channel; ch++)
951 put_bits(pb, 4, dp->quant_step_size[ch]);
957 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
958 ChannelParams *cp = &ctx->cur_channel_params[ch];
960 if (dp->param_presence_flags & 0xF) {
963 if (dp->param_presence_flags & PARAM_FIR) {
964 if (params_changed & PARAM_FIR) {
966 write_filter_params(ctx, pb, ch, FIR);
972 if (dp->param_presence_flags & PARAM_IIR) {
973 if (params_changed & PARAM_IIR) {
975 write_filter_params(ctx, pb, ch, IIR);
981 if (dp->param_presence_flags & PARAM_HUFFOFFSET) {
982 if (params_changed & PARAM_HUFFOFFSET) {
984 put_sbits(pb, 15, cp->huff_offset);
990 put_bits(pb, 2, cp->codebook );
991 put_bits(pb, 5, cp->huff_lsbs);
998 /** Writes the residuals to the bitstream. That is, the VLC codes from the
999 * codebooks (if any is used), and then the residual.
1001 static void write_block_data(MLPEncodeContext *ctx, PutBitContext *pb)
1003 DecodingParams *dp = ctx->cur_decoding_params;
1004 RestartHeader *rh = ctx->cur_restart_header;
1005 int32_t *sample_buffer = ctx->write_buffer;
1006 int32_t sign_huff_offset[MAX_CHANNELS];
1007 int codebook_index [MAX_CHANNELS];
1008 int lsb_bits [MAX_CHANNELS];
1011 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1012 ChannelParams *cp = &ctx->cur_channel_params[ch];
1015 lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
1016 codebook_index [ch] = cp->codebook - 1;
1017 sign_huff_offset[ch] = cp->huff_offset;
1019 sign_shift = lsb_bits[ch] - 1;
1021 if (cp->codebook > 0) {
1022 sign_huff_offset[ch] -= 7 << lsb_bits[ch];
1023 sign_shift += 3 - cp->codebook;
1026 /* Unsign if needed. */
1027 if (sign_shift >= 0)
1028 sign_huff_offset[ch] -= 1 << sign_shift;
1031 for (i = 0; i < dp->blocksize; i++) {
1032 for (ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1033 int32_t sample = *sample_buffer++ >> dp->quant_step_size[ch];
1035 sample -= sign_huff_offset[ch];
1037 if (codebook_index[ch] >= 0) {
1038 int vlc = sample >> lsb_bits[ch];
1039 put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1040 ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
1043 put_sbits(pb, lsb_bits[ch], sample);
1045 sample_buffer += 2; /* noise channels */
1048 ctx->write_buffer = sample_buffer;
1051 /** Writes the substreams data to the bitstream. */
1052 static uint8_t *write_substrs(MLPEncodeContext *ctx, uint8_t *buf, int buf_size,
1054 uint16_t substream_data_len[MAX_SUBSTREAMS])
1056 int32_t *lossless_check_data = ctx->lossless_check_data;
1057 unsigned int substr;
1060 lossless_check_data += ctx->frame_index * ctx->num_substreams;
1062 for (substr = 0; substr < ctx->num_substreams; substr++) {
1063 unsigned int cur_subblock_index = ctx->major_cur_subblock_index;
1064 unsigned int num_subblocks = ctx->major_filter_state_subblock;
1065 unsigned int subblock;
1066 RestartHeader *rh = &ctx->restart_header [substr];
1067 int substr_restart_frame = restart_frame;
1068 uint8_t parity, checksum;
1069 PutBitContext pb, tmpb;
1072 ctx->cur_restart_header = rh;
1074 init_put_bits(&pb, buf, buf_size);
1076 for (subblock = 0; subblock <= num_subblocks; subblock++) {
1077 unsigned int subblock_index;
1079 subblock_index = cur_subblock_index++;
1081 ctx->cur_decoding_params = &ctx->major_decoding_params[subblock_index][substr];
1082 ctx->cur_channel_params = ctx->major_channel_params[subblock_index];
1084 params_changed = ctx->major_params_changed[subblock_index][substr];
1086 if (substr_restart_frame || params_changed) {
1087 put_bits(&pb, 1, 1);
1089 if (substr_restart_frame) {
1090 put_bits(&pb, 1, 1);
1092 write_restart_header(ctx, &pb);
1093 rh->lossless_check_data = 0;
1095 put_bits(&pb, 1, 0);
1098 write_decoding_params(ctx, &pb, params_changed);
1100 put_bits(&pb, 1, 0);
1103 write_block_data(ctx, &pb);
1105 put_bits(&pb, 1, !substr_restart_frame);
1107 substr_restart_frame = 0;
1110 put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
1112 rh->lossless_check_data ^= *lossless_check_data++;
1114 if (ctx->last_frame == ctx->inout_buffer) {
1115 /* TODO find a sample and implement shorten_by. */
1116 put_bits(&pb, 32, END_OF_STREAM);
1119 /* Data must be flushed for the checksum and parity to be correct. */
1121 flush_put_bits(&tmpb);
1123 parity = ff_mlp_calculate_parity(buf, put_bits_count(&pb) >> 3) ^ 0xa9;
1124 checksum = ff_mlp_checksum8 (buf, put_bits_count(&pb) >> 3);
1126 put_bits(&pb, 8, parity );
1127 put_bits(&pb, 8, checksum);
1129 flush_put_bits(&pb);
1131 end += put_bits_count(&pb) >> 3;
1132 substream_data_len[substr] = end;
1134 buf += put_bits_count(&pb) >> 3;
1137 ctx->major_cur_subblock_index += ctx->major_filter_state_subblock + 1;
1138 ctx->major_filter_state_subblock = 0;
1143 /** Writes the access unit and substream headers to the bitstream. */
1144 static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header,
1145 uint8_t *substream_headers, unsigned int length,
1147 uint16_t substream_data_len[MAX_SUBSTREAMS])
1149 uint16_t access_unit_header = 0;
1150 uint16_t parity_nibble = 0;
1151 unsigned int substr;
1153 parity_nibble = ctx->dts;
1154 parity_nibble ^= length;
1156 for (substr = 0; substr < ctx->num_substreams; substr++) {
1157 uint16_t substr_hdr = 0;
1159 substr_hdr |= (0 << 15); /* extraword */
1160 substr_hdr |= (!restart_frame << 14); /* !restart_frame */
1161 substr_hdr |= (1 << 13); /* checkdata */
1162 substr_hdr |= (0 << 12); /* ??? */
1163 substr_hdr |= (substream_data_len[substr] / 2) & 0x0FFF;
1165 AV_WB16(substream_headers, substr_hdr);
1167 parity_nibble ^= *substream_headers++;
1168 parity_nibble ^= *substream_headers++;
1171 parity_nibble ^= parity_nibble >> 8;
1172 parity_nibble ^= parity_nibble >> 4;
1173 parity_nibble &= 0xF;
1175 access_unit_header |= (parity_nibble ^ 0xF) << 12;
1176 access_unit_header |= length & 0xFFF;
1178 AV_WB16(frame_header , access_unit_header);
1179 AV_WB16(frame_header+2, ctx->dts );
1182 /** Writes an entire access unit to the bitstream. */
1183 static unsigned int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
1184 int buf_size, int restart_frame)
1186 uint16_t substream_data_len[MAX_SUBSTREAMS];
1187 uint8_t *buf1, *buf0 = buf;
1188 unsigned int substr;
1194 /* Frame header will be written at the end. */
1198 if (restart_frame) {
1201 write_major_sync(ctx, buf, buf_size);
1208 /* Substream headers will be written at the end. */
1209 for (substr = 0; substr < ctx->num_substreams; substr++) {
1214 buf = write_substrs(ctx, buf, buf_size, restart_frame, substream_data_len);
1216 total_length = buf - buf0;
1218 write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1220 return total_length;
1223 /****************************************************************************
1224 ****************** Functions that input data to context ********************
1225 ****************************************************************************/
1227 /** Inputs data from the samples passed by lavc into the context, shifts them
1228 * appropriately depending on the bit-depth, and calculates the
1229 * lossless_check_data that will be written to the restart header.
1231 static void input_data_internal(MLPEncodeContext *ctx, const uint8_t *samples,
1234 int32_t *lossless_check_data = ctx->lossless_check_data;
1235 const int32_t *samples_32 = (const int32_t *) samples;
1236 const int16_t *samples_16 = (const int16_t *) samples;
1237 unsigned int substr;
1239 lossless_check_data += ctx->frame_index * ctx->num_substreams;
1241 for (substr = 0; substr < ctx->num_substreams; substr++) {
1242 RestartHeader *rh = &ctx->restart_header [substr];
1243 int32_t *sample_buffer = ctx->inout_buffer;
1244 int32_t temp_lossless_check_data = 0;
1245 uint32_t greatest = 0;
1246 unsigned int channel;
1249 for (i = 0; i < ctx->frame_size[ctx->frame_index]; i++) {
1250 for (channel = 0; channel <= rh->max_channel; channel++) {
1251 uint32_t abs_sample;
1254 sample = is24 ? *samples_32++ >> 8 : *samples_16++ << 8;
1256 /* TODO Find out if number_sbits can be used for negative values. */
1257 abs_sample = FFABS(sample);
1258 if (greatest < abs_sample)
1259 greatest = abs_sample;
1261 temp_lossless_check_data ^= (sample & 0x00ffffff) << channel;
1262 *sample_buffer++ = sample;
1265 sample_buffer += 2; /* noise channels */
1268 ctx->max_output_bits[ctx->frame_index] = number_sbits(greatest);
1270 *lossless_check_data++ = temp_lossless_check_data;
1274 /** Wrapper function for inputting data in two different bit-depths. */
1275 static void input_data(MLPEncodeContext *ctx, void *samples)
1277 if (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32)
1278 input_data_internal(ctx, samples, 1);
1280 input_data_internal(ctx, samples, 0);
1283 static void input_to_sample_buffer(MLPEncodeContext *ctx)
1285 int32_t *sample_buffer = ctx->sample_buffer;
1288 for (index = 0; index < ctx->number_of_frames; index++) {
1289 unsigned int cur_index = (ctx->starting_frame_index + index) % ctx->max_restart_interval;
1290 int32_t *input_buffer = ctx->inout_buffer + cur_index * ctx->one_sample_buffer_size;
1291 unsigned int i, channel;
1293 for (i = 0; i < ctx->frame_size[cur_index]; i++) {
1294 for (channel = 0; channel < ctx->avctx->channels; channel++)
1295 *sample_buffer++ = *input_buffer++;
1296 sample_buffer += 2; /* noise_channels */
1297 input_buffer += 2; /* noise_channels */
1302 /****************************************************************************
1303 ********* Functions that analyze the data and set the parameters ***********
1304 ****************************************************************************/
1306 /** Counts the number of trailing zeroes in a value */
1307 static int number_trailing_zeroes(int32_t sample)
1311 for (bits = 0; bits < 24 && !(sample & (1<<bits)); bits++);
1313 /* All samples are 0. TODO Return previous quant_step_size to avoid
1314 * writing a new header. */
1321 /** Determines how many bits are zero at the end of all samples so they can be
1324 static void determine_quant_step_size(MLPEncodeContext *ctx)
1326 DecodingParams *dp = ctx->cur_decoding_params;
1327 RestartHeader *rh = ctx->cur_restart_header;
1328 MatrixParams *mp = &dp->matrix_params;
1329 int32_t *sample_buffer = ctx->sample_buffer;
1330 int32_t sample_mask[MAX_CHANNELS];
1331 unsigned int channel;
1334 memset(sample_mask, 0x00, sizeof(sample_mask));
1336 for (i = 0; i < ctx->number_of_samples; i++) {
1337 for (channel = 0; channel <= rh->max_channel; channel++)
1338 sample_mask[channel] |= *sample_buffer++;
1340 sample_buffer += 2; /* noise channels */
1343 for (channel = 0; channel <= rh->max_channel; channel++)
1344 dp->quant_step_size[channel] = number_trailing_zeroes(sample_mask[channel]) - mp->shift[channel];
1347 /** Determines the smallest number of bits needed to encode the filter
1348 * coefficients, and if it's possible to right-shift their values without
1349 * losing any precision.
1351 static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, int32_t *fcoeff)
1353 int min = INT_MAX, max = INT_MIN;
1358 for (order = 0; order < fp->order; order++) {
1359 int coeff = fcoeff[order];
1366 coeff_mask |= coeff;
1369 bits = FFMAX(number_sbits(min), number_sbits(max));
1371 for (shift = 0; shift < 7 && bits + shift < 16 && !(coeff_mask & (1<<shift)); shift++);
1373 fp->coeff_bits = bits;
1374 fp->coeff_shift = shift;
1377 /** Determines the best filter parameters for the given data and writes the
1378 * necessary information to the context.
1379 * TODO Add IIR filter predictor!
1381 static void set_filter_params(MLPEncodeContext *ctx,
1382 unsigned int channel, unsigned int filter,
1385 ChannelParams *cp = &ctx->cur_channel_params[channel];
1386 FilterParams *fp = &cp->filter_params[filter];
1388 if ((filter == IIR && ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE) ||
1391 } else if (filter == IIR) {
1393 } else if (filter == FIR) {
1394 const int max_order = (ctx->substream_info & SUBSTREAM_INFO_HIGH_RATE)
1395 ? 4 : MLP_MAX_LPC_ORDER;
1396 int32_t *sample_buffer = ctx->sample_buffer + channel;
1397 int32_t coefs[MAX_LPC_ORDER][MAX_LPC_ORDER];
1398 int32_t *lpc_samples = ctx->lpc_sample_buffer;
1399 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1400 int shift[MLP_MAX_LPC_ORDER];
1404 for (i = 0; i < ctx->number_of_samples; i++) {
1405 *lpc_samples++ = *sample_buffer;
1406 sample_buffer += ctx->num_channels;
1409 order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
1410 ctx->number_of_samples, MLP_MIN_LPC_ORDER,
1411 max_order, 11, coefs, shift, FF_LPC_TYPE_LEVINSON, 0,
1412 ORDER_METHOD_EST, MLP_MIN_LPC_SHIFT,
1413 MLP_MAX_LPC_SHIFT, MLP_MIN_LPC_SHIFT);
1416 fp->shift = shift[order-1];
1418 for (i = 0; i < order; i++)
1419 fcoeff[i] = coefs[order-1][i];
1421 code_filter_coeffs(ctx, fp, fcoeff);
1425 /** Tries to determine a good prediction filter, and applies it to the samples
1426 * buffer if the filter is good enough. Sets the filter data to be cleared if
1427 * no good filter was found.
1429 static void determine_filters(MLPEncodeContext *ctx)
1431 RestartHeader *rh = ctx->cur_restart_header;
1432 int channel, filter;
1434 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1435 for (filter = 0; filter < NUM_FILTERS; filter++)
1436 set_filter_params(ctx, channel, filter, 0);
1441 MLP_CHMODE_LEFT_RIGHT,
1442 MLP_CHMODE_LEFT_SIDE,
1443 MLP_CHMODE_RIGHT_SIDE,
1444 MLP_CHMODE_MID_SIDE,
1447 static enum MLPChMode estimate_stereo_mode(MLPEncodeContext *ctx)
1449 uint64_t score[4], sum[4] = { 0, 0, 0, 0, };
1450 int32_t *right_ch = ctx->sample_buffer + 1;
1451 int32_t *left_ch = ctx->sample_buffer;
1453 enum MLPChMode best = 0;
1455 for(i = 2; i < ctx->number_of_samples; i++) {
1456 int32_t left = left_ch [i * ctx->num_channels] - 2 * left_ch [(i - 1) * ctx->num_channels] + left_ch [(i - 2) * ctx->num_channels];
1457 int32_t right = right_ch[i * ctx->num_channels] - 2 * right_ch[(i - 1) * ctx->num_channels] + right_ch[(i - 2) * ctx->num_channels];
1459 sum[0] += FFABS( left );
1460 sum[1] += FFABS( right);
1461 sum[2] += FFABS((left + right) >> 1);
1462 sum[3] += FFABS( left - right);
1465 score[MLP_CHMODE_LEFT_RIGHT] = sum[0] + sum[1];
1466 score[MLP_CHMODE_LEFT_SIDE] = sum[0] + sum[3];
1467 score[MLP_CHMODE_RIGHT_SIDE] = sum[1] + sum[3];
1468 score[MLP_CHMODE_MID_SIDE] = sum[2] + sum[3];
1470 for(i = 1; i < 3; i++)
1471 if(score[i] < score[best])
1477 /** Determines how many fractional bits are needed to encode matrix
1478 * coefficients. Also shifts the coefficients to fit within 2.14 bits.
1480 static void code_matrix_coeffs(MLPEncodeContext *ctx, unsigned int mat)
1482 DecodingParams *dp = ctx->cur_decoding_params;
1483 MatrixParams *mp = &dp->matrix_params;
1484 int32_t coeff_mask = 0;
1485 unsigned int channel;
1488 for (channel = 0; channel < ctx->num_channels; channel++) {
1489 int32_t coeff = mp->coeff[mat][channel];
1490 coeff_mask |= coeff;
1493 for (bits = 0; bits < 14 && !(coeff_mask & (1<<bits)); bits++);
1495 mp->fbits [mat] = 14 - bits;
1498 /** Determines best coefficients to use for the lossless matrix. */
1499 static void lossless_matrix_coeffs(MLPEncodeContext *ctx)
1501 DecodingParams *dp = ctx->cur_decoding_params;
1502 MatrixParams *mp = &dp->matrix_params;
1503 unsigned int shift = 0;
1504 unsigned int channel;
1506 enum MLPChMode mode;
1508 /* No decorrelation for non-stereo. */
1509 if (ctx->num_channels - 2 != 2) {
1514 mode = estimate_stereo_mode(ctx);
1517 /* TODO: add matrix for MID_SIDE */
1518 case MLP_CHMODE_MID_SIDE:
1519 case MLP_CHMODE_LEFT_RIGHT:
1522 case MLP_CHMODE_LEFT_SIDE:
1525 mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = -(1 << 14);
1526 mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
1527 mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
1528 mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
1530 case MLP_CHMODE_RIGHT_SIDE:
1533 mp->coeff[0][0] = 1 << 14; mp->coeff[0][1] = 1 << 14;
1534 mp->coeff[0][2] = 0 << 14; mp->coeff[0][2] = 0 << 14;
1535 mp->forco[0][0] = 1 << 14; mp->forco[0][1] = -(1 << 14);
1536 mp->forco[0][2] = 0 << 14; mp->forco[0][2] = 0 << 14;
1540 for (mat = 0; mat < mp->count; mat++)
1541 code_matrix_coeffs(ctx, mat);
1543 for (channel = 0; channel < ctx->num_channels; channel++)
1544 mp->shift[channel] = shift;
1547 /** Min and max values that can be encoded with each codebook. The values for
1548 * the third codebook take into account the fact that the sign shift for this
1549 * codebook is outside the coded value, so it has one more bit of precision.
1550 * It should actually be -7 -> 7, shifted down by 0.5.
1552 static const int codebook_extremes[3][2] = {
1553 {-9, 8}, {-8, 7}, {-15, 14},
1556 /** Determines the amount of bits needed to encode the samples using no
1557 * codebooks and a specified offset.
1559 static void no_codebook_bits_offset(MLPEncodeContext *ctx,
1560 unsigned int channel, int16_t offset,
1561 int32_t min, int32_t max,
1564 DecodingParams *dp = ctx->cur_decoding_params;
1571 lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
1573 lsb_bits += !!lsb_bits;
1575 unsign = 1 << (lsb_bits - 1);
1577 bo->offset = offset;
1578 bo->lsb_bits = lsb_bits;
1579 bo->bitcount = lsb_bits * dp->blocksize;
1580 bo->min = offset - unsign + 1;
1581 bo->max = offset + unsign;
1584 /** Determines the least amount of bits needed to encode the samples using no
1587 static void no_codebook_bits(MLPEncodeContext *ctx,
1588 unsigned int channel,
1589 int32_t min, int32_t max,
1592 DecodingParams *dp = ctx->cur_decoding_params;
1598 /* Set offset inside huffoffset's boundaries by adjusting extremes
1599 * so that more bits are used, thus shifting the offset. */
1600 if (min < HUFF_OFFSET_MIN)
1601 max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
1602 if (max > HUFF_OFFSET_MAX)
1603 min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
1605 /* Determine offset and minimum number of bits. */
1608 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);
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;
1721 if (offset > offset_max)
1724 offset = temp_bo.min - 1;
1725 if (offset < offset_min)
1731 /** Determines the least amount of bits needed to encode the samples using
1732 * any or no codebook.
1734 static void determine_bits(MLPEncodeContext *ctx)
1736 DecodingParams *dp = ctx->cur_decoding_params;
1737 RestartHeader *rh = ctx->cur_restart_header;
1738 unsigned int channel;
1740 for (channel = 0; channel <= rh->max_channel; channel++) {
1741 ChannelParams *cp = &ctx->cur_channel_params[channel];
1742 int32_t *sample_buffer = ctx->sample_buffer + channel;
1743 int32_t min = INT32_MAX, max = INT32_MIN;
1744 int no_filters_used = !cp->filter_params[FIR].order;
1749 /* Determine extremes and average. */
1750 for (i = 0; i < dp->blocksize; i++) {
1751 int32_t sample = *sample_buffer >> dp->quant_step_size[channel];
1757 sample_buffer += ctx->num_channels;
1759 average /= dp->blocksize;
1761 /* If filtering is used, we always set the offset to zero, otherwise
1762 * we search for the offset that minimizes the bitcount. */
1763 if (no_filters_used) {
1764 no_codebook_bits(ctx, channel, min, max, &ctx->cur_best_offset[channel][0]);
1765 offset = av_clip(average, HUFF_OFFSET_MIN, HUFF_OFFSET_MAX);
1767 no_codebook_bits_offset(ctx, channel, offset, min, max, &ctx->cur_best_offset[channel][0]);
1770 for (i = 1; i < NUM_CODEBOOKS; i++) {
1771 BestOffset temp_bo = { 0, INT_MAX, 0, 0, 0, };
1774 codebook_bits_offset(ctx, channel, i - 1,
1778 if (no_filters_used) {
1779 offset_max = temp_bo.max;
1781 codebook_bits(ctx, channel, i - 1, temp_bo.min - 1,
1782 min, max, &temp_bo, 0);
1783 codebook_bits(ctx, channel, i - 1, offset_max + 1,
1784 min, max, &temp_bo, 1);
1787 ctx->cur_best_offset[channel][i] = temp_bo;
1792 /****************************************************************************
1793 *************** Functions that process the data in some way ****************
1794 ****************************************************************************/
1796 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1797 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1799 #define MSB_MASK(bits) (-1u << bits)
1801 /** Applies the filter to the current samples, and saves the residual back
1802 * into the samples buffer. If the filter is too bad and overflows the
1803 * maximum amount of bits allowed (16 or 24), the samples buffer is left as is and
1804 * the function returns -1.
1806 static int apply_filter(MLPEncodeContext *ctx, unsigned int channel)
1808 FilterParams *fp[NUM_FILTERS] = { &ctx->cur_channel_params[channel].filter_params[FIR],
1809 &ctx->cur_channel_params[channel].filter_params[IIR], };
1810 int32_t *filter_state_buffer[NUM_FILTERS];
1811 int32_t mask = MSB_MASK(ctx->cur_decoding_params->quant_step_size[channel]);
1812 int32_t *sample_buffer = ctx->sample_buffer + channel;
1813 unsigned int number_of_samples = ctx->number_of_samples;
1814 unsigned int filter_shift = fp[FIR]->shift;
1818 for (i = 0; i < NUM_FILTERS; i++) {
1819 unsigned int size = ctx->number_of_samples;
1820 filter_state_buffer[i] = av_malloc(size*sizeof(int32_t));
1821 if (!filter_state_buffer[i]) {
1822 av_log(ctx->avctx, AV_LOG_ERROR,
1823 "Not enough memory for applying filters.\n");
1828 for (i = 0; i < 8; i++) {
1829 filter_state_buffer[FIR][i] = *sample_buffer;
1830 filter_state_buffer[IIR][i] = *sample_buffer;
1832 sample_buffer += ctx->num_channels;
1835 for (i = 8; i < number_of_samples; i++) {
1836 int32_t sample = *sample_buffer;
1841 for (filter = 0; filter < NUM_FILTERS; filter++) {
1842 int32_t *fcoeff = ctx->cur_channel_params[channel].coeff[filter];
1843 for (order = 0; order < fp[filter]->order; order++)
1844 accum += (int64_t)filter_state_buffer[filter][i - 1 - order] *
1848 accum >>= filter_shift;
1849 residual = sample - (accum & mask);
1851 if (residual < SAMPLE_MIN(ctx->wordlength) || residual > SAMPLE_MAX(ctx->wordlength))
1854 filter_state_buffer[FIR][i] = sample;
1855 filter_state_buffer[IIR][i] = residual;
1857 sample_buffer += ctx->num_channels;
1860 sample_buffer = ctx->sample_buffer + channel;
1861 for (i = 0; i < number_of_samples; i++) {
1862 *sample_buffer = filter_state_buffer[IIR][i];
1864 sample_buffer += ctx->num_channels;
1867 for (i = 0; i < NUM_FILTERS; i++) {
1868 av_freep(&filter_state_buffer[i]);
1874 static void apply_filters(MLPEncodeContext *ctx)
1876 RestartHeader *rh = ctx->cur_restart_header;
1879 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1880 if (apply_filter(ctx, channel) < 0) {
1881 /* Filter is horribly wrong.
1882 * Clear filter params and update state. */
1883 set_filter_params(ctx, channel, FIR, 1);
1884 set_filter_params(ctx, channel, IIR, 1);
1885 apply_filter(ctx, channel);
1890 /** Generates two noise channels worth of data. */
1891 static void generate_2_noise_channels(MLPEncodeContext *ctx)
1893 int32_t *sample_buffer = ctx->sample_buffer + ctx->num_channels - 2;
1894 RestartHeader *rh = ctx->cur_restart_header;
1896 uint32_t seed = rh->noisegen_seed;
1898 for (i = 0; i < ctx->number_of_samples; i++) {
1899 uint16_t seed_shr7 = seed >> 7;
1900 *sample_buffer++ = ((int8_t)(seed >> 15)) << rh->noise_shift;
1901 *sample_buffer++ = ((int8_t) seed_shr7) << rh->noise_shift;
1903 seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1905 sample_buffer += ctx->num_channels - 2;
1908 rh->noisegen_seed = seed & ((1 << 24)-1);
1911 /** Rematrixes all channels using chosen coefficients. */
1912 static void rematrix_channels(MLPEncodeContext *ctx)
1914 DecodingParams *dp = ctx->cur_decoding_params;
1915 MatrixParams *mp = &dp->matrix_params;
1916 int32_t *sample_buffer = ctx->sample_buffer;
1917 unsigned int mat, i, maxchan;
1919 maxchan = ctx->num_channels;
1921 for (mat = 0; mat < mp->count; mat++) {
1922 unsigned int msb_mask_bits = (ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S16 ? 8 : 0) - mp->shift[mat];
1923 int32_t mask = MSB_MASK(msb_mask_bits);
1924 unsigned int outch = mp->outch[mat];
1926 sample_buffer = ctx->sample_buffer;
1927 for (i = 0; i < ctx->number_of_samples; i++) {
1928 unsigned int src_ch;
1931 for (src_ch = 0; src_ch < maxchan; src_ch++) {
1932 int32_t sample = *(sample_buffer + src_ch);
1933 accum += (int64_t) sample * mp->forco[mat][src_ch];
1935 sample_buffer[outch] = (accum >> 14) & mask;
1937 sample_buffer += ctx->num_channels;
1942 /****************************************************************************
1943 **** Functions that deal with determining the best parameters and output ***
1944 ****************************************************************************/
1947 char path[MAJOR_HEADER_INTERVAL + 3];
1951 static const char *path_counter_codebook[] = { "0", "1", "2", "3", };
1953 #define ZERO_PATH '0'
1954 #define CODEBOOK_CHANGE_BITS 21
1956 static void clear_path_counter(PathCounter *path_counter)
1960 for (i = 0; i < NUM_CODEBOOKS + 1; i++) {
1961 path_counter[i].path[0] = ZERO_PATH;
1962 path_counter[i].path[1] = 0x00;
1963 path_counter[i].bitcount = 0;
1967 static int compare_best_offset(BestOffset *prev, BestOffset *cur)
1969 if (prev->lsb_bits != cur->lsb_bits)
1975 static int best_codebook_path_cost(MLPEncodeContext *ctx, unsigned int channel,
1976 PathCounter *src, int cur_codebook)
1978 BestOffset *cur_bo, *prev_bo = restart_best_offset;
1979 int bitcount = src->bitcount;
1980 char *path = src->path + 1;
1984 for (i = 0; path[i]; i++)
1985 prev_bo = ctx->best_offset[i][channel];
1987 prev_codebook = path[i - 1] - ZERO_PATH;
1989 cur_bo = ctx->best_offset[i][channel];
1991 bitcount += cur_bo[cur_codebook].bitcount;
1993 if (prev_codebook != cur_codebook ||
1994 compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
1995 bitcount += CODEBOOK_CHANGE_BITS;
2000 static void set_best_codebook(MLPEncodeContext *ctx)
2002 DecodingParams *dp = ctx->cur_decoding_params;
2003 RestartHeader *rh = ctx->cur_restart_header;
2004 unsigned int channel;
2006 for (channel = rh->min_channel; channel <= rh->max_channel; channel++) {
2007 BestOffset *cur_bo, *prev_bo = restart_best_offset;
2008 PathCounter path_counter[NUM_CODEBOOKS + 1];
2009 unsigned int best_codebook;
2013 clear_path_counter(path_counter);
2015 for (index = 0; index < ctx->number_of_subblocks; index++) {
2016 unsigned int best_bitcount = INT_MAX;
2017 unsigned int codebook;
2019 cur_bo = ctx->best_offset[index][channel];
2021 for (codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
2022 int prev_best_bitcount = INT_MAX;
2025 for (last_best = 0; last_best < 2; last_best++) {
2026 PathCounter *dst_path = &path_counter[codebook];
2027 PathCounter *src_path;
2030 /* First test last path with same headers,
2031 * then with last best. */
2033 src_path = &path_counter[NUM_CODEBOOKS];
2035 if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
2038 src_path = &path_counter[codebook];
2041 temp_bitcount = best_codebook_path_cost(ctx, channel, src_path, codebook);
2043 if (temp_bitcount < best_bitcount) {
2044 best_bitcount = temp_bitcount;
2045 best_codebook = codebook;
2048 if (temp_bitcount < prev_best_bitcount) {
2049 prev_best_bitcount = temp_bitcount;
2050 if (src_path != dst_path)
2051 memcpy(dst_path, src_path, sizeof(PathCounter));
2052 av_strlcat(dst_path->path, path_counter_codebook[codebook], sizeof(dst_path->path));
2053 dst_path->bitcount = temp_bitcount;
2060 memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
2063 best_path = path_counter[NUM_CODEBOOKS].path + 1;
2065 /* Update context. */
2066 for (index = 0; index < ctx->number_of_subblocks; index++) {
2067 ChannelParams *cp = ctx->seq_channel_params + index*(ctx->avctx->channels) + channel;
2069 best_codebook = *best_path++ - ZERO_PATH;
2070 cur_bo = &ctx->best_offset[index][channel][best_codebook];
2072 cp->huff_offset = cur_bo->offset;
2073 cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
2074 cp->codebook = best_codebook;
2079 /** Analyzes all collected bitcounts and selects the best parameters for each
2080 * individual access unit.
2081 * TODO This is just a stub!
2083 static void set_major_params(MLPEncodeContext *ctx)
2085 RestartHeader *rh = ctx->cur_restart_header;
2087 unsigned int substr;
2088 uint8_t max_huff_lsbs = 0;
2089 uint8_t max_output_bits = 0;
2091 for (substr = 0; substr < ctx->num_substreams; substr++) {
2092 DecodingParams *seq_dp = (DecodingParams *) ctx->decoding_params+
2093 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2094 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2096 ChannelParams *seq_cp = (ChannelParams *) ctx->channel_params +
2097 (ctx->restart_intervals - 1)*(ctx->sequence_size)*(ctx->avctx->channels) +
2098 (ctx->seq_offset[ctx->restart_intervals - 1])*(ctx->avctx->channels);
2099 unsigned int channel;
2100 for (index = 0; index < ctx->seq_size[ctx->restart_intervals-1]; index++) {
2101 memcpy(&ctx->major_decoding_params[index][substr], seq_dp + index*(ctx->num_substreams) + substr, sizeof(DecodingParams));
2102 for (channel = 0; channel < ctx->avctx->channels; channel++) {
2103 uint8_t huff_lsbs = (seq_cp + index*(ctx->avctx->channels) + channel)->huff_lsbs;
2104 if (max_huff_lsbs < huff_lsbs)
2105 max_huff_lsbs = huff_lsbs;
2106 memcpy(&ctx->major_channel_params[index][channel],
2107 (seq_cp + index*(ctx->avctx->channels) + channel),
2108 sizeof(ChannelParams));
2113 rh->max_huff_lsbs = max_huff_lsbs;
2115 for (index = 0; index < ctx->number_of_frames; index++)
2116 if (max_output_bits < ctx->max_output_bits[index])
2117 max_output_bits = ctx->max_output_bits[index];
2118 rh->max_output_bits = max_output_bits;
2120 for (substr = 0; substr < ctx->num_substreams; substr++) {
2122 ctx->cur_restart_header = &ctx->restart_header[substr];
2124 ctx->prev_decoding_params = &restart_decoding_params[substr];
2125 ctx->prev_channel_params = restart_channel_params;
2127 for (index = 0; index < MAJOR_HEADER_INTERVAL + 1; index++) {
2128 ctx->cur_decoding_params = &ctx->major_decoding_params[index][substr];
2129 ctx->cur_channel_params = ctx->major_channel_params[index];
2131 ctx->major_params_changed[index][substr] = compare_decoding_params(ctx);
2133 ctx->prev_decoding_params = ctx->cur_decoding_params;
2134 ctx->prev_channel_params = ctx->cur_channel_params;
2138 ctx->major_number_of_subblocks = ctx->number_of_subblocks;
2139 ctx->major_filter_state_subblock = 1;
2140 ctx->major_cur_subblock_index = 0;
2143 static void analyze_sample_buffer(MLPEncodeContext *ctx)
2145 ChannelParams *seq_cp = ctx->seq_channel_params;
2146 DecodingParams *seq_dp = ctx->seq_decoding_params;
2148 unsigned int substr;
2150 for (substr = 0; substr < ctx->num_substreams; substr++) {
2152 ctx->cur_restart_header = &ctx->restart_header[substr];
2153 ctx->cur_decoding_params = seq_dp + 1*(ctx->num_substreams) + substr;
2154 ctx->cur_channel_params = seq_cp + 1*(ctx->avctx->channels);
2156 determine_quant_step_size(ctx);
2157 generate_2_noise_channels(ctx);
2158 lossless_matrix_coeffs (ctx);
2159 rematrix_channels (ctx);
2160 determine_filters (ctx);
2161 apply_filters (ctx);
2163 copy_restart_frame_params(ctx, substr);
2165 /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2166 * decoding_params[0] is for the filter state subblock.
2168 for (index = 0; index < ctx->number_of_frames; index++) {
2169 DecodingParams *dp = seq_dp + (index + 1)*(ctx->num_substreams) + substr;
2170 dp->blocksize = ctx->frame_size[index];
2172 /* The official encoder seems to always encode a filter state subblock
2173 * even if there are no filters. TODO check if it is possible to skip
2174 * the filter state subblock for no filters.
2176 (seq_dp + substr)->blocksize = 8;
2177 (seq_dp + 1*(ctx->num_substreams) + substr)->blocksize -= 8;
2179 for (index = 0; index < ctx->number_of_subblocks; index++) {
2180 ctx->cur_decoding_params = seq_dp + index*(ctx->num_substreams) + substr;
2181 ctx->cur_channel_params = seq_cp + index*(ctx->avctx->channels);
2182 ctx->cur_best_offset = ctx->best_offset[index];
2183 determine_bits(ctx);
2184 ctx->sample_buffer += ctx->cur_decoding_params->blocksize * ctx->num_channels;
2187 set_best_codebook(ctx);
2191 static void process_major_frame(MLPEncodeContext *ctx)
2193 unsigned int substr;
2195 ctx->sample_buffer = ctx->major_inout_buffer;
2197 ctx->starting_frame_index = 0;
2198 ctx->number_of_frames = ctx->major_number_of_frames;
2199 ctx->number_of_samples = ctx->major_frame_size;
2201 for (substr = 0; substr < ctx->num_substreams; substr++) {
2202 RestartHeader *rh = ctx->cur_restart_header;
2203 unsigned int channel;
2205 ctx->cur_restart_header = &ctx->restart_header[substr];
2207 ctx->cur_decoding_params = &ctx->major_decoding_params[1][substr];
2208 ctx->cur_channel_params = ctx->major_channel_params[1];
2210 generate_2_noise_channels(ctx);
2211 rematrix_channels (ctx);
2213 for (channel = rh->min_channel; channel <= rh->max_channel; channel++)
2214 apply_filter(ctx, channel);
2218 /****************************************************************************/
2220 static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2221 const AVFrame *frame, int *got_packet)
2223 MLPEncodeContext *ctx = avctx->priv_data;
2224 unsigned int bytes_written = 0;
2225 int restart_frame, ret;
2228 if ((ret = ff_alloc_packet2(avctx, avpkt, 87500 * avctx->channels, 0)) < 0)
2234 /* add current frame to queue */
2236 if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
2240 data = frame->data[0];
2242 ctx->frame_index = avctx->frame_number % ctx->max_restart_interval;
2244 ctx->inout_buffer = ctx->major_inout_buffer
2245 + ctx->frame_index * ctx->one_sample_buffer_size;
2247 if (ctx->last_frame == ctx->inout_buffer) {
2251 ctx->sample_buffer = ctx->major_scratch_buffer
2252 + ctx->frame_index * ctx->one_sample_buffer_size;
2254 ctx->write_buffer = ctx->inout_buffer;
2256 if (avctx->frame_number < ctx->max_restart_interval) {
2258 goto input_and_return;
2260 /* There are less frames than the requested major header interval.
2261 * Update the context to reflect this.
2263 ctx->max_restart_interval = avctx->frame_number;
2264 ctx->frame_index = 0;
2266 ctx->sample_buffer = ctx->major_scratch_buffer;
2267 ctx->inout_buffer = ctx->major_inout_buffer;
2271 if (ctx->frame_size[ctx->frame_index] > MAX_BLOCKSIZE) {
2272 av_log(avctx, AV_LOG_ERROR, "Invalid frame size (%d > %d)\n",
2273 ctx->frame_size[ctx->frame_index], MAX_BLOCKSIZE);
2277 restart_frame = !ctx->frame_index;
2279 if (restart_frame) {
2280 set_major_params(ctx);
2281 if (ctx->min_restart_interval != ctx->max_restart_interval)
2282 process_major_frame(ctx);
2285 if (ctx->min_restart_interval == ctx->max_restart_interval)
2286 ctx->write_buffer = ctx->sample_buffer;
2288 bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
2290 ctx->timestamp += ctx->frame_size[ctx->frame_index];
2291 ctx->dts += ctx->frame_size[ctx->frame_index];
2296 ctx->frame_size[ctx->frame_index] = avctx->frame_size;
2297 ctx->next_major_frame_size += avctx->frame_size;
2298 ctx->next_major_number_of_frames++;
2299 input_data(ctx, data);
2300 } else if (!ctx->last_frame) {
2301 ctx->last_frame = ctx->inout_buffer;
2304 restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
2306 if (!restart_frame) {
2310 seq_index < ctx->restart_intervals && (seq_index * ctx->min_restart_interval) <= ctx->avctx->frame_number;
2312 unsigned int number_of_samples = 0;
2315 ctx->sample_buffer = ctx->major_scratch_buffer;
2316 ctx->inout_buffer = ctx->major_inout_buffer;
2317 ctx->seq_index = seq_index;
2319 ctx->starting_frame_index = (ctx->avctx->frame_number - (ctx->avctx->frame_number % ctx->min_restart_interval)
2320 - (seq_index * ctx->min_restart_interval)) % ctx->max_restart_interval;
2321 ctx->number_of_frames = ctx->next_major_number_of_frames;
2322 ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
2324 ctx->seq_channel_params = (ChannelParams *) ctx->channel_params +
2325 (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->avctx->channels) +
2326 (ctx->seq_offset[seq_index])*(ctx->avctx->channels);
2328 ctx->seq_decoding_params = (DecodingParams *) ctx->decoding_params +
2329 (ctx->frame_index / ctx->min_restart_interval)*(ctx->sequence_size)*(ctx->num_substreams) +
2330 (ctx->seq_offset[seq_index])*(ctx->num_substreams);
2332 for (index = 0; index < ctx->number_of_frames; index++) {
2333 number_of_samples += ctx->frame_size[(ctx->starting_frame_index + index) % ctx->max_restart_interval];
2335 ctx->number_of_samples = number_of_samples;
2337 for (index = 0; index < ctx->seq_size[seq_index]; index++) {
2338 clear_channel_params(ctx, ctx->seq_channel_params + index*(ctx->avctx->channels));
2339 default_decoding_params(ctx, ctx->seq_decoding_params + index*(ctx->num_substreams));
2342 input_to_sample_buffer(ctx);
2344 analyze_sample_buffer(ctx);
2347 if (ctx->frame_index == (ctx->max_restart_interval - 1)) {
2348 ctx->major_frame_size = ctx->next_major_frame_size;
2349 ctx->next_major_frame_size = 0;
2350 ctx->major_number_of_frames = ctx->next_major_number_of_frames;
2351 ctx->next_major_number_of_frames = 0;
2353 if (!ctx->major_frame_size)
2360 ff_af_queue_remove(&ctx->afq, avctx->frame_size, &avpkt->pts,
2362 avpkt->size = bytes_written;
2367 static av_cold int mlp_encode_close(AVCodecContext *avctx)
2369 MLPEncodeContext *ctx = avctx->priv_data;
2371 ff_lpc_end(&ctx->lpc_ctx);
2373 av_freep(&ctx->lossless_check_data);
2374 av_freep(&ctx->major_scratch_buffer);
2375 av_freep(&ctx->major_inout_buffer);
2376 av_freep(&ctx->lpc_sample_buffer);
2377 av_freep(&ctx->decoding_params);
2378 av_freep(&ctx->channel_params);
2379 av_freep(&ctx->frame_size);
2380 ff_af_queue_close(&ctx->afq);
2385 #if CONFIG_MLP_ENCODER
2386 AVCodec ff_mlp_encoder = {
2388 .long_name = NULL_IF_CONFIG_SMALL("MLP (Meridian Lossless Packing)"),
2389 .type = AVMEDIA_TYPE_AUDIO,
2390 .id = AV_CODEC_ID_MLP,
2391 .priv_data_size = sizeof(MLPEncodeContext),
2392 .init = mlp_encode_init,
2393 .encode2 = mlp_encode_frame,
2394 .close = mlp_encode_close,
2395 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
2396 .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2397 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2398 .channel_layouts = ff_mlp_channel_layouts,
2401 #if CONFIG_TRUEHD_ENCODER
2402 AVCodec ff_truehd_encoder = {
2404 .long_name = NULL_IF_CONFIG_SMALL("TrueHD"),
2405 .type = AVMEDIA_TYPE_AUDIO,
2406 .id = AV_CODEC_ID_TRUEHD,
2407 .priv_data_size = sizeof(MLPEncodeContext),
2408 .init = mlp_encode_init,
2409 .encode2 = mlp_encode_frame,
2410 .close = mlp_encode_close,
2411 .capabilities = AV_CODEC_CAP_SMALL_LAST_FRAME | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_EXPERIMENTAL,
2412 .sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE},
2413 .supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2414 .channel_layouts = (const uint64_t[]) {AV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_5POINT0_BACK, AV_CH_LAYOUT_5POINT1_BACK, 0},