FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mlpenc.c
Date: 2021-09-26 18:22:30
Exec Total Coverage
Lines: 0 1098 0.0%
Branches: 0 478 0.0%

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