History

mlpenc: Working MLP/TrueHD encoder

* Multichannel support for TrueHD is experimental There should be downmix substreams present for 2+ channel bitstreams, but ffmpeg decoder doesn't need it. Will add support for this soon. * There might be lossless check failures on LFE channels * 32-bit sample support has been removed for now, will add it later While testing, some samples gave lossless check failures when enforcing s32. Probably this will also get solved with the LFE issues.

Jai Luthra committed 10 years ago

Commit 15b86f41 parent ee88dcb5 files · +2481 −2Browse files

Unified · Split

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

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t