FFmpeg
mlpenc.c
Go to the documentation of this file.
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 "config_components.h"
24 
25 #include "avcodec.h"
26 #include "codec_internal.h"
27 #include "encode.h"
28 #include "put_bits.h"
29 #include "audio_frame_queue.h"
30 #include "libavutil/avassert.h"
32 #include "libavutil/crc.h"
33 #include "libavutil/avstring.h"
34 #include "libavutil/intmath.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/samplefmt.h"
37 #include "libavutil/thread.h"
38 #include "mlp_parse.h"
39 #include "mlp.h"
40 #include "lpc.h"
41 
42 #define MAX_NCHANNELS (MAX_CHANNELS + 2)
43 
44 #define MIN_HEADER_INTERVAL 8
45 #define MAX_HEADER_INTERVAL 128
46 
47 #define MLP_MIN_LPC_ORDER 1
48 #define MLP_MAX_LPC_ORDER 8
49 #define MLP_MIN_LPC_SHIFT 0
50 #define MLP_MAX_LPC_SHIFT 15
51 
52 typedef struct RestartHeader {
53  uint8_t min_channel; ///< The index of the first channel coded in this substream.
54  uint8_t max_channel; ///< The index of the last channel coded in this substream.
55  uint8_t max_matrix_channel; ///< The number of channels input into the rematrix stage.
56 
57  int8_t max_shift;
58  uint8_t noise_shift; ///< The left shift applied to random noise in 0x31ea substreams.
59  uint32_t noisegen_seed; ///< The current seed value for the pseudorandom noise generator(s).
60 
61  uint8_t data_check_present; ///< Set if the substream contains extra info to check the size of VLC blocks.
62 
63  int32_t lossless_check_data; ///< XOR of all output samples
64 
65  uint8_t max_huff_lsbs; ///< largest huff_lsbs
66  uint8_t max_output_bits; ///< largest output bit-depth
68 
69 typedef struct MatrixParams {
70  uint8_t count; ///< number of matrices to apply
71 
72  uint8_t outch[MAX_MATRICES]; ///< output channel for each matrix
73  int32_t forco[MAX_MATRICES][MAX_NCHANNELS]; ///< forward coefficients
74  int32_t coeff[MAX_MATRICES][MAX_NCHANNELS]; ///< decoding coefficients
75  uint8_t fbits[MAX_MATRICES]; ///< fraction bits
76 
80 } MatrixParams;
81 
82 #define PARAMS_DEFAULT (0xff)
83 #define PARAM_PRESENCE_FLAGS (1 << 8)
84 
85 typedef struct DecodingParams {
86  uint16_t blocksize; ///< number of PCM samples in current audio block
87  uint8_t quant_step_size[MAX_CHANNELS]; ///< left shift to apply to Huffman-decoded residuals
88  int8_t output_shift[MAX_CHANNELS]; ///< Left shift to apply to decoded PCM values to get final 24-bit output.
90 
92 
93  uint8_t param_presence_flags; ///< Bitmask of which parameter sets are conveyed in a decoding parameter block.
96 
97 typedef struct BestOffset {
99  uint32_t bitcount;
100  uint8_t lsb_bits;
103 } BestOffset;
104 
105 #define HUFF_OFFSET_MIN (-16384)
106 #define HUFF_OFFSET_MAX ( 16383)
107 
108 /** Number of possible codebooks (counting "no codebooks") */
109 #define NUM_CODEBOOKS 4
110 
111 typedef struct MLPBlock {
112  unsigned int seq_size;
116  unsigned int max_output_bits; ///< largest output bit-depth
118  ChannelParams major_channel_params[MAX_CHANNELS]; ///< ChannelParams to be written to bitstream.
119  DecodingParams major_decoding_params; ///< DecodingParams to be written to bitstream.
120  int major_params_changed; ///< params_changed to be written to bitstream.
122 } MLPBlock;
123 
124 typedef struct MLPSubstream {
131 } MLPSubstream;
132 
133 typedef struct MLPEncodeContext {
134  AVClass *class;
136 
137  int max_restart_interval; ///< Max interval of access units in between two major frames.
138  int min_restart_interval; ///< Min interval of access units in between two major frames.
142  int lpc_type;
146 
147  int num_substreams; ///< Number of substreams contained within this stream.
148 
149  int num_channels; /**< Number of channels in major_scratch_buffer.
150  * Normal channels + noise channels. */
151 
152  int coded_sample_fmt [2]; ///< sample format encoded for MLP
153  int coded_sample_rate[2]; ///< sample rate encoded for MLP
154  int coded_peak_bitrate; ///< peak bitrate for this major sync header
155 
156  int flags; ///< major sync info flags
157 
158  /* channel_meaning */
161  int fs;
165 
166  int32_t last_frames; ///< Signal last frames.
167 
170 
171  unsigned int major_frame_size; ///< Number of samples in current major frame being encoded.
172  unsigned int next_major_frame_size; ///< Counter of number of samples for next major frame.
173 
174  unsigned int frame_index; ///< Index of current frame being encoded.
175 
176  unsigned int restart_intervals; ///< Number of possible major frame sizes.
177 
178  uint16_t output_timing; ///< Timestamp of current access unit.
179  uint16_t input_timing; ///< Decoding timestamp of current access unit.
180 
181  uint8_t noise_type;
182  uint8_t channel_arrangement; ///< channel arrangement for MLP streams
183  uint16_t channel_arrangement8; ///< 8 channel arrangement for THD streams
184 
185  uint8_t multichannel_type6ch; ///< channel modifier for TrueHD stream 0
186  uint8_t multichannel_type8ch; ///< channel modifier for TrueHD stream 0
187  uint8_t ch2_presentation_mod; ///< channel modifier for TrueHD stream 0
188  uint8_t ch6_presentation_mod; ///< channel modifier for TrueHD stream 1
189  uint8_t ch8_presentation_mod; ///< channel modifier for TrueHD stream 2
190 
194 
196 
197  /* Analysis stage. */
198  unsigned int number_of_frames;
199  unsigned int number_of_subblocks;
200 
202 
205 
209 
210 #define SYNC_MAJOR 0xf8726f
211 #define MAJOR_SYNC_INFO_SIGNATURE 0xB752
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  */
230 static 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 
235  if (prev->order != fp->order)
236  return 1;
237 
238  if (!fp->order)
239  return 0;
240 
241  if (prev->shift != fp->shift)
242  return 1;
243 
244  for (int i = 0; i < fp->order; i++)
245  if (prev_cp->coeff[filter][i] != cp->coeff[filter][i])
246  return 1;
247 
248  return 0;
249 }
250 
251 /** Compare two primitive matrices and returns 1 if anything has changed.
252  * Returns 0 if they are both equal.
253  */
255  const MatrixParams *prev, const MatrixParams *mp)
256 {
257  RestartHeader *rh = s->cur_restart_header;
258 
259  if (prev->count != mp->count)
260  return 1;
261 
262  if (!mp->count)
263  return 0;
264 
265  for (unsigned int mat = 0; mat < mp->count; mat++) {
266  if (prev->outch[mat] != mp->outch[mat])
267  return 1;
268 
269  if (prev->fbits[mat] != mp->fbits[mat])
270  return 1;
271 
272  if (prev->noise_shift[mat] != mp->noise_shift[mat])
273  return 1;
274 
275  if (prev->lsb_bypass[mat] != mp->lsb_bypass[mat])
276  return 1;
277 
278  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
279  if (prev->coeff[mat][ch] != mp->coeff[mat][ch])
280  return 1;
281  }
282 
283  return 0;
284 }
285 
286 /** Compares two DecodingParams and ChannelParams structures to decide if a
287  * new decoding params header has to be written.
288  */
290  MLPSubstream *s,
291  unsigned int index)
292 {
293  const DecodingParams *prev = index ? &s->b[index-1].major_decoding_params : restart_decoding_params;
294  DecodingParams *dp = &s->b[index].major_decoding_params;
295  const MatrixParams *prev_mp = &prev->matrix_params;
296  MatrixParams *mp = &dp->matrix_params;
297  RestartHeader *rh = s->cur_restart_header;
298  int retval = 0;
299 
301  retval |= PARAM_PRESENCE_FLAGS;
302 
303  if (prev->blocksize != dp->blocksize)
304  retval |= PARAM_BLOCKSIZE;
305 
306  if (compare_matrix_params(ctx, s, prev_mp, mp))
307  retval |= PARAM_MATRIX;
308 
309  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
310  if (prev->output_shift[ch] != dp->output_shift[ch]) {
311  retval |= PARAM_OUTSHIFT;
312  break;
313  }
314 
315  for (int ch = 0; ch <= rh->max_channel; ch++)
316  if (prev->quant_step_size[ch] != dp->quant_step_size[ch]) {
317  retval |= PARAM_QUANTSTEP;
318  break;
319  }
320 
321  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
322  const ChannelParams *prev_cp = index ? &s->b[index-1].major_channel_params[ch] : &restart_channel_params[ch];
323  ChannelParams *cp = &s->b[index].major_channel_params[ch];
324 
325  if (!(retval & PARAM_FIR) &&
326  compare_filter_params(prev_cp, cp, FIR))
327  retval |= PARAM_FIR;
328 
329  if (!(retval & PARAM_IIR) &&
330  compare_filter_params(prev_cp, cp, IIR))
331  retval |= PARAM_IIR;
332 
333  if (prev_cp->huff_offset != cp->huff_offset)
334  retval |= PARAM_HUFFOFFSET;
335 
336  if (prev_cp->codebook != cp->codebook ||
337  prev_cp->huff_lsbs != cp->huff_lsbs )
338  retval |= PARAM_PRESENCE;
339  }
340 
341  return retval;
342 }
343 
344 static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
345 {
346  FilterParams *dst = &dst_cp->filter_params[filter];
347  FilterParams *src = &src_cp->filter_params[filter];
348 
349  dst->order = src->order;
350 
351  if (dst->order) {
352  dst->shift = src->shift;
353 
354  dst->coeff_shift = src->coeff_shift;
355  dst->coeff_bits = src->coeff_bits;
356  }
357 
358  for (int order = 0; order < dst->order; order++)
359  dst_cp->coeff[filter][order] = src_cp->coeff[filter][order];
360 }
361 
363 {
364  dst->count = src->count;
365 
366  if (!dst->count)
367  return;
368 
369  for (int count = 0; count < MAX_MATRICES; count++) {
370  dst->outch[count] = src->outch[count];
371  dst->fbits[count] = src->fbits[count];
372  dst->noise_shift[count] = src->noise_shift[count];
373  dst->lsb_bypass[count] = src->lsb_bypass[count];
374 
375  for (int channel = 0; channel < MAX_NCHANNELS; channel++)
376  dst->coeff[count][channel] = src->coeff[count][channel];
377  }
378 }
379 
381 {
382  RestartHeader *rh = s->cur_restart_header;
383 
384  for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
385  DecodingParams *dp = &s->b[index].decoding_params;
386 
387  copy_matrix_params(&dp->matrix_params, &s->b[1].decoding_params.matrix_params);
388 
389  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
390  dp->output_shift[ch] = s->b[1].decoding_params.output_shift[ch];
391 
392  for (int ch = 0; ch <= rh->max_channel; ch++) {
393  ChannelParams *cp = &s->b[index].channel_params[ch];
394 
395  dp->quant_step_size[ch] = s->b[1].decoding_params.quant_step_size[ch];
396 
397  if (index)
398  for (unsigned int filter = 0; filter < NUM_FILTERS; filter++)
399  copy_filter_params(cp, &s->b[1].channel_params[ch], filter);
400  }
401  }
402 }
403 
404 /** Clears a DecodingParams struct the way it should be after a restart header. */
405 static void clear_decoding_params(DecodingParams *decoding_params)
406 {
407  DecodingParams *dp = decoding_params;
408 
409  dp->param_presence_flags = 0xff;
410  dp->blocksize = 0;
411 
412  memset(&dp->matrix_params, 0, sizeof(dp->matrix_params ));
413  memset(dp->quant_step_size, 0, sizeof(dp->quant_step_size));
414  memset(dp->sample_buffer, 0, sizeof(dp->sample_buffer ));
415  memset(dp->output_shift, 0, sizeof(dp->output_shift ));
416  memset(dp->max_order, MAX_FIR_ORDER, sizeof(dp->max_order));
417 }
418 
419 /** Clears a ChannelParams struct the way it should be after a restart header. */
420 static void clear_channel_params(ChannelParams *channel_params, int nb_channels)
421 {
422  for (unsigned channel = 0; channel < nb_channels; channel++) {
423  ChannelParams *cp = &channel_params[channel];
424 
425  memset(&cp->filter_params, 0, sizeof(cp->filter_params));
426 
427  /* Default audio coding is 24-bit raw PCM. */
428  cp->huff_offset = 0;
429  cp->codebook = 0;
430  cp->huff_lsbs = 24;
431  }
432 }
433 
434 /** Sets default vales in our encoder for a DecodingParams struct. */
436 {
437  uint8_t param_presence_flags = 0;
438 
440 
441  param_presence_flags |= PARAM_BLOCKSIZE;
442  param_presence_flags |= PARAM_MATRIX;
443  param_presence_flags |= PARAM_OUTSHIFT;
444  param_presence_flags |= PARAM_QUANTSTEP;
445  param_presence_flags |= PARAM_FIR;
446  param_presence_flags |= PARAM_IIR;
447  param_presence_flags |= PARAM_HUFFOFFSET;
448  param_presence_flags |= PARAM_PRESENCE;
449 
450  dp->param_presence_flags = param_presence_flags;
451 }
452 
453 /****************************************************************************/
454 
455 /** Calculates the smallest number of bits it takes to encode a given signed
456  * value in two's complement.
457  */
458 static int inline number_sbits(int32_t n)
459 {
460  return 33 - ff_clz(FFABS(n)|1) - !n;
461 }
462 
467 };
468 
469 static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
470 {
471  return ((peak_bitrate << 4) - 8) / sample_rate;
472 }
473 
475 {
478  ff_mlp_init_crc();
479 }
480 
482 {
483  static AVOnce init_static_once = AV_ONCE_INIT;
484  MLPEncodeContext *ctx = avctx->priv_data;
485  uint64_t channels_present;
486  int ret;
487 
488  ctx->avctx = avctx;
489 
490  switch (avctx->sample_rate) {
491  case 44100 << 0:
492  avctx->frame_size = 40 << 0;
493  ctx->coded_sample_rate[0] = 0x08 + 0;
494  ctx->fs = 0x08 + 1;
495  break;
496  case 44100 << 1:
497  avctx->frame_size = 40 << 1;
498  ctx->coded_sample_rate[0] = 0x08 + 1;
499  ctx->fs = 0x0C + 1;
500  break;
501  case 44100 << 2:
502  ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
503  avctx->frame_size = 40 << 2;
504  ctx->coded_sample_rate[0] = 0x08 + 2;
505  ctx->fs = 0x10 + 1;
506  break;
507  case 48000 << 0:
508  avctx->frame_size = 40 << 0;
509  ctx->coded_sample_rate[0] = 0x00 + 0;
510  ctx->fs = 0x08 + 2;
511  break;
512  case 48000 << 1:
513  avctx->frame_size = 40 << 1;
514  ctx->coded_sample_rate[0] = 0x00 + 1;
515  ctx->fs = 0x0C + 2;
516  break;
517  case 48000 << 2:
518  ctx->substream_info |= SUBSTREAM_INFO_HIGH_RATE;
519  avctx->frame_size = 40 << 2;
520  ctx->coded_sample_rate[0] = 0x00 + 2;
521  ctx->fs = 0x10 + 2;
522  break;
523  default:
524  av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d. Supported "
525  "sample rates are 44100, 88200, 176400, 48000, "
526  "96000, and 192000.\n", avctx->sample_rate);
527  return AVERROR(EINVAL);
528  }
529  ctx->coded_sample_rate[1] = -1 & 0xf;
530 
531  ctx->coded_peak_bitrate = mlp_peak_bitrate(9600000, avctx->sample_rate);
532 
533  ctx->substream_info |= SUBSTREAM_INFO_ALWAYS_SET;
534  if (avctx->ch_layout.nb_channels <= 2)
535  ctx->substream_info |= SUBSTREAM_INFO_MAX_2_CHAN;
536 
537  switch (avctx->sample_fmt) {
538  case AV_SAMPLE_FMT_S16P:
539  ctx->coded_sample_fmt[0] = BITS_16;
540  ctx->wordlength = 16;
541  avctx->bits_per_raw_sample = 16;
542  break;
543  /* TODO 20 bits: */
544  case AV_SAMPLE_FMT_S32P:
545  ctx->coded_sample_fmt[0] = BITS_24;
546  ctx->wordlength = 24;
547  avctx->bits_per_raw_sample = 24;
548  break;
549  default:
550  av_log(avctx, AV_LOG_ERROR, "Sample format not supported. "
551  "Only 16- and 24-bit samples are supported.\n");
552  return AVERROR(EINVAL);
553  }
554  ctx->coded_sample_fmt[1] = -1 & 0xf;
555 
556  ctx->input_timing = -avctx->frame_size;
557 
558  ctx->num_channels = avctx->ch_layout.nb_channels + 2; /* +2 noise channels */
559 
560  ctx->min_restart_interval = ctx->cur_restart_interval = ctx->max_restart_interval;
561  ctx->restart_intervals = ctx->max_restart_interval / ctx->min_restart_interval;
562 
563  ctx->num_substreams = 1;
564 
565  channels_present = av_channel_layout_subset(&avctx->ch_layout, ~(uint64_t)0);
566  if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
567  static const uint64_t layout_arrangement[] = {
570  AV_CH_LAYOUT_2POINT1, 0, 0,
574  };
575  int i;
576 
577  for (i = 0;; i++) {
578  av_assert1(i < FF_ARRAY_ELEMS(layout_arrangement) ||
579  !"Impossible channel layout");
580  if (channels_present == layout_arrangement[i])
581  break;
582  }
583  ctx->channel_arrangement = i;
584  ctx->flags = FLAGS_DVDA;
585  ctx->channel_occupancy = ff_mlp_ch_info[ctx->channel_arrangement].channel_occupancy;
586  ctx->summary_info = ff_mlp_ch_info[ctx->channel_arrangement].summary_info ;
587  } else {
588  /* TrueHD */
589  ctx->num_substreams = 1 + (avctx->ch_layout.nb_channels > 2);
590  switch (channels_present) {
591  case AV_CH_LAYOUT_MONO:
592  ctx->ch2_presentation_mod= 3;
593  ctx->ch6_presentation_mod= 3;
594  ctx->ch8_presentation_mod= 3;
595  ctx->thd_substream_info = 0x14;
596  break;
597  case AV_CH_LAYOUT_STEREO:
598  ctx->ch2_presentation_mod= 1;
599  ctx->ch6_presentation_mod= 1;
600  ctx->ch8_presentation_mod= 1;
601  ctx->thd_substream_info = 0x14;
602  break;
610  ctx->ch2_presentation_mod= 0;
611  ctx->ch6_presentation_mod= 0;
612  ctx->ch8_presentation_mod= 0;
613  ctx->thd_substream_info = 0x3C;
614  break;
615  default:
616  av_assert1(!"AVCodec.ch_layouts needs to be updated");
617  }
618  ctx->flags = 0;
619  ctx->channel_occupancy = 0;
620  ctx->summary_info = 0;
621  ctx->channel_arrangement =
622  ctx->channel_arrangement8 = layout_truehd(channels_present);
623  }
624 
625  for (unsigned int index = 0; index < ctx->restart_intervals; index++) {
626  for (int n = 0; n < ctx->num_substreams; n++)
627  ctx->s[n].b[index].seq_size = ((index + 1) * ctx->min_restart_interval) + 1;
628  }
629 
630 
631  /* TODO see if noisegen_seed is really worth it. */
632  if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
633  RestartHeader *const rh = &ctx->s[0].restart_header;
634 
635  rh->noisegen_seed = 0;
636  rh->min_channel = 0;
637  rh->max_channel = avctx->ch_layout.nb_channels - 1;
639  } else {
640  RestartHeader *rh = &ctx->s[0].restart_header;
641 
642  rh->noisegen_seed = 0;
643  rh->min_channel = 0;
644  rh->max_channel = FFMIN(avctx->ch_layout.nb_channels, 2) - 1;
646 
647  if (avctx->ch_layout.nb_channels > 2) {
648  rh = &ctx->s[1].restart_header;
649 
650  rh->noisegen_seed = 0;
651  rh->min_channel = 2;
652  rh->max_channel = avctx->ch_layout.nb_channels - 1;
654  }
655  }
656 
657  if ((ret = ff_lpc_init(&ctx->lpc_ctx, ctx->avctx->frame_size,
658  MLP_MAX_LPC_ORDER, ctx->lpc_type)) < 0)
659  return ret;
660 
661  ff_af_queue_init(avctx, &ctx->afq);
662 
663  ff_thread_once(&init_static_once, mlp_encode_init_static);
664 
665  return 0;
666 }
667 
668 /****************************************************************************
669  ****************** Functions that write to the bitstream *******************
670  ****************************************************************************/
671 
672 /** Writes a major sync header to the bitstream. */
673 static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
674 {
675  PutBitContext pb;
676 
677  init_put_bits(&pb, buf, buf_size);
678 
679  put_bits(&pb, 24, SYNC_MAJOR );
680 
681  if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
682  put_bits(&pb, 8, SYNC_MLP );
683  put_bits(&pb, 4, ctx->coded_sample_fmt [0]);
684  put_bits(&pb, 4, ctx->coded_sample_fmt [1]);
685  put_bits(&pb, 4, ctx->coded_sample_rate[0]);
686  put_bits(&pb, 4, ctx->coded_sample_rate[1]);
687  put_bits(&pb, 4, 0 ); /* ignored */
688  put_bits(&pb, 4, 0 ); /* multi_channel_type */
689  put_bits(&pb, 3, 0 ); /* ignored */
690  put_bits(&pb, 5, ctx->channel_arrangement );
691  } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
692  put_bits(&pb, 8, SYNC_TRUEHD );
693  put_bits(&pb, 4, ctx->coded_sample_rate[0]);
694  put_bits(&pb, 1, ctx->multichannel_type6ch);
695  put_bits(&pb, 1, ctx->multichannel_type8ch);
696  put_bits(&pb, 2, 0 ); /* ignored */
697  put_bits(&pb, 2, ctx->ch2_presentation_mod);
698  put_bits(&pb, 2, ctx->ch6_presentation_mod);
699  put_bits(&pb, 5, ctx->channel_arrangement );
700  put_bits(&pb, 2, ctx->ch8_presentation_mod);
701  put_bits(&pb, 13, ctx->channel_arrangement8);
702  }
703 
705  put_bits(&pb, 16, ctx->flags );
706  put_bits(&pb, 16, 0 ); /* ignored */
707  put_bits(&pb, 1, 1 ); /* is_vbr */
708  put_bits(&pb, 15, ctx->coded_peak_bitrate );
709  put_bits(&pb, 4, ctx->num_substreams );
710  put_bits(&pb, 2, 0 ); /* ignored */
711  put_bits(&pb, 2, 0 ); /* extended substream info */
712 
713  /* channel_meaning */
714  if (ctx->avctx->codec_id == AV_CODEC_ID_MLP) {
715  put_bits(&pb, 8, ctx->substream_info );
716  put_bits(&pb, 5, ctx->fs );
717  put_bits(&pb, 5, ctx->wordlength );
718  put_bits(&pb, 6, ctx->channel_occupancy );
719  put_bits(&pb, 3, 0 ); /* ignored */
720  put_bits(&pb, 10, 0 ); /* speaker_layout */
721  put_bits(&pb, 3, 0 ); /* copy_protection */
722  put_bits(&pb, 16, 0x8080 ); /* ignored */
723  put_bits(&pb, 7, 0 ); /* ignored */
724  put_bits(&pb, 4, 0 ); /* source_format */
725  put_bits(&pb, 5, ctx->summary_info );
726  } else if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
727  put_bits(&pb, 8, ctx->thd_substream_info );
728  put_bits(&pb, 6, 0 ); /* reserved */
729  put_bits(&pb, 1, 0 ); /* 2ch control enabled */
730  put_bits(&pb, 1, 0 ); /* 6ch control enabled */
731  put_bits(&pb, 1, 0 ); /* 8ch control enabled */
732  put_bits(&pb, 1, 0 ); /* reserved */
733  put_bits(&pb, 7, 0 ); /* drc start up gain */
734  put_bits(&pb, 6, 0 ); /* 2ch dialogue norm */
735  put_bits(&pb, 6, 0 ); /* 2ch mix level */
736  put_bits(&pb, 5, 0 ); /* 6ch dialogue norm */
737  put_bits(&pb, 6, 0 ); /* 6ch mix level */
738  put_bits(&pb, 5, 0 ); /* 6ch source format */
739  put_bits(&pb, 5, 0 ); /* 8ch dialogue norm */
740  put_bits(&pb, 6, 0 ); /* 8ch mix level */
741  put_bits(&pb, 6, 0 ); /* 8ch source format */
742  put_bits(&pb, 1, 0 ); /* reserved */
743  put_bits(&pb, 1, 0 ); /* extra channel meaning present */
744  }
745 
746  flush_put_bits(&pb);
747 
748  AV_WL16(buf+26, ff_mlp_checksum16(buf, 26));
749 }
750 
751 /** Writes a restart header to the bitstream. Damaged streams can start being
752  * decoded losslessly again after such a header and the subsequent decoding
753  * params header.
754  */
756  PutBitContext *pb)
757 {
758  RestartHeader *rh = s->cur_restart_header;
759  uint8_t lossless_check = xor_32_to_8(rh->lossless_check_data);
760  unsigned int start_count = put_bits_count(pb);
761  PutBitContext tmpb;
762  uint8_t checksum;
763 
764  put_bits(pb, 14, 0x31ea ); /* TODO 0x31eb */
765  put_bits(pb, 16, ctx->output_timing );
766  put_bits(pb, 4, rh->min_channel );
767  put_bits(pb, 4, rh->max_channel );
768  put_bits(pb, 4, rh->max_matrix_channel);
769  put_bits(pb, 4, rh->noise_shift );
770  put_bits(pb, 23, rh->noisegen_seed );
771  put_bits(pb, 4, rh->max_shift );
772  put_bits(pb, 5, rh->max_huff_lsbs );
773  put_bits(pb, 5, rh->max_output_bits );
774  put_bits(pb, 5, rh->max_output_bits );
775  put_bits(pb, 1, rh->data_check_present);
776  put_bits(pb, 8, lossless_check );
777  put_bits(pb, 16, 0 ); /* ignored */
778 
779  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
780  put_bits(pb, 6, ch);
781 
782  /* Data must be flushed for the checksum to be correct. */
783  tmpb = *pb;
784  flush_put_bits(&tmpb);
785 
786  checksum = ff_mlp_restart_checksum(pb->buf, put_bits_count(pb) - start_count);
787 
788  put_bits(pb, 8, checksum);
789 }
790 
791 /** Writes matrix params for all primitive matrices to the bitstream. */
793  MLPSubstream *s,
794  DecodingParams *dp,
795  PutBitContext *pb)
796 {
797  RestartHeader *rh = s->cur_restart_header;
798  MatrixParams *mp = &dp->matrix_params;
799  int max_channel = rh->max_matrix_channel;
800 
801  put_bits(pb, 4, mp->count);
802 
803  if (!ctx->noise_type)
804  max_channel += 2;
805 
806  for (unsigned int mat = 0; mat < mp->count; mat++) {
807  put_bits(pb, 4, mp->outch[mat]); /* matrix_out_ch */
808  put_bits(pb, 4, mp->fbits[mat]);
809  put_bits(pb, 1, mp->lsb_bypass[mat]);
810 
811  for (int ch = 0; ch <= max_channel; ch++) {
812  int32_t coeff = mp->coeff[mat][ch];
813 
814  if (coeff) {
815  put_bits(pb, 1, 1);
816 
817  coeff >>= 14 - mp->fbits[mat];
818 
819  put_sbits(pb, mp->fbits[mat] + 2, coeff);
820  } else {
821  put_bits(pb, 1, 0);
822  }
823  }
824  }
825 }
826 
827 /** Writes filter parameters for one filter to the bitstream. */
829  ChannelParams *cp,
830  PutBitContext *pb,
831  int channel, unsigned int filter)
832 {
833  FilterParams *fp = &cp->filter_params[filter];
834 
835  put_bits(pb, 4, fp->order);
836 
837  if (fp->order > 0) {
838  int32_t *fcoeff = cp->coeff[filter];
839 
840  put_bits(pb, 4, fp->shift );
841  put_bits(pb, 5, fp->coeff_bits );
842  put_bits(pb, 3, fp->coeff_shift);
843 
844  for (int i = 0; i < fp->order; i++) {
845  put_sbits(pb, fp->coeff_bits, fcoeff[i] >> fp->coeff_shift);
846  }
847 
848  /* TODO state data for IIR filter. */
849  put_bits(pb, 1, 0);
850  }
851 }
852 
853 /** Writes decoding parameters to the bitstream. These change very often,
854  * usually at almost every frame.
855  */
857  PutBitContext *pb, int params_changed,
858  unsigned int subblock_index)
859 {
860  DecodingParams *dp = &s->b[subblock_index].major_decoding_params;
861  RestartHeader *rh = s->cur_restart_header;
862 
864  params_changed & PARAM_PRESENCE_FLAGS) {
865  put_bits(pb, 1, 1);
866  put_bits(pb, 8, dp->param_presence_flags);
867  } else {
868  put_bits(pb, 1, 0);
869  }
870 
872  if (params_changed & PARAM_BLOCKSIZE) {
873  put_bits(pb, 1, 1);
874  put_bits(pb, 9, dp->blocksize);
875  } else {
876  put_bits(pb, 1, 0);
877  }
878  }
879 
881  if (params_changed & PARAM_MATRIX) {
882  put_bits(pb, 1, 1);
883  write_matrix_params(ctx, s, dp, pb);
884  } else {
885  put_bits(pb, 1, 0);
886  }
887  }
888 
890  if (params_changed & PARAM_OUTSHIFT) {
891  put_bits(pb, 1, 1);
892  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
893  put_sbits(pb, 4, dp->output_shift[ch]);
894  } else {
895  put_bits(pb, 1, 0);
896  }
897  }
898 
900  if (params_changed & PARAM_QUANTSTEP) {
901  put_bits(pb, 1, 1);
902  for (int ch = 0; ch <= rh->max_channel; ch++)
903  put_bits(pb, 4, dp->quant_step_size[ch]);
904  } else {
905  put_bits(pb, 1, 0);
906  }
907  }
908 
909  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
910  ChannelParams *cp = &s->b[subblock_index].major_channel_params[ch];
911 
912  if (dp->param_presence_flags & 0xF) {
913  put_bits(pb, 1, 1);
914 
915  if (dp->param_presence_flags & PARAM_FIR) {
916  if (params_changed & PARAM_FIR) {
917  put_bits(pb, 1, 1);
918  write_filter_params(ctx, cp, pb, ch, FIR);
919  } else {
920  put_bits(pb, 1, 0);
921  }
922  }
923 
924  if (dp->param_presence_flags & PARAM_IIR) {
925  if (params_changed & PARAM_IIR) {
926  put_bits(pb, 1, 1);
927  write_filter_params(ctx, cp, pb, ch, IIR);
928  } else {
929  put_bits(pb, 1, 0);
930  }
931  }
932 
934  if (params_changed & PARAM_HUFFOFFSET) {
935  put_bits (pb, 1, 1);
936  put_sbits(pb, 15, cp->huff_offset);
937  } else {
938  put_bits(pb, 1, 0);
939  }
940  }
941  if (cp->codebook > 0 && cp->huff_lsbs > 24) {
942  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid Huff LSBs %d\n", cp->huff_lsbs);
943  }
944 
945  put_bits(pb, 2, cp->codebook );
946  put_bits(pb, 5, cp->huff_lsbs);
947  } else {
948  put_bits(pb, 1, 0);
949  }
950  }
951 }
952 
953 /** Writes the residuals to the bitstream. That is, the VLC codes from the
954  * codebooks (if any is used), and then the residual.
955  */
957  PutBitContext *pb, unsigned int subblock_index)
958 {
959  RestartHeader *rh = s->cur_restart_header;
960  DecodingParams *dp = &s->b[subblock_index].major_decoding_params;
961  MatrixParams *mp = &dp->matrix_params;
962  int32_t sign_huff_offset[MAX_CHANNELS];
963  int codebook_index [MAX_CHANNELS];
964  int lsb_bits [MAX_CHANNELS];
965 
966  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
967  ChannelParams *cp = &s->b[subblock_index].major_channel_params[ch];
968  int sign_shift;
969 
970  lsb_bits [ch] = cp->huff_lsbs - dp->quant_step_size[ch];
971  codebook_index [ch] = cp->codebook - 1;
972  sign_huff_offset[ch] = cp->huff_offset;
973 
974  sign_shift = lsb_bits[ch] + (cp->codebook ? 2 - cp->codebook : -1);
975 
976  if (cp->codebook > 0)
977  sign_huff_offset[ch] -= 7 << lsb_bits[ch];
978 
979  /* Unsign if needed. */
980  if (sign_shift >= 0)
981  sign_huff_offset[ch] -= 1 << sign_shift;
982  }
983 
984  for (unsigned int i = 0; i < dp->blocksize; i++) {
985  for (unsigned int mat = 0; mat < mp->count; mat++) {
986  if (mp->lsb_bypass[mat]) {
987  const int8_t *bypassed_lsbs = mp->bypassed_lsbs[mat];
988 
989  put_bits(pb, 1, bypassed_lsbs[i]);
990  }
991  }
992 
993  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
994  int32_t *sample_buffer = dp->sample_buffer[ch];
995  int32_t sample = sample_buffer[i] >> dp->quant_step_size[ch];
996  sample -= sign_huff_offset[ch];
997 
998  if (codebook_index[ch] >= 0) {
999  int vlc = sample >> lsb_bits[ch];
1000  put_bits(pb, ff_mlp_huffman_tables[codebook_index[ch]][vlc][1],
1001  ff_mlp_huffman_tables[codebook_index[ch]][vlc][0]);
1002  sample &= ((1 << lsb_bits[ch]) - 1);
1003  }
1004 
1005  put_bits(pb, lsb_bits[ch], sample);
1006  }
1007  }
1008 }
1009 
1010 /** Writes the substream data to the bitstream. */
1012  MLPSubstream *s,
1013  uint8_t *buf, int buf_size,
1014  int restart_frame,
1015  uint16_t *substream_data_len)
1016 {
1017  int32_t *lossless_check_data = &s->b[ctx->frame_index].lossless_check_data;
1018  unsigned int cur_subblock_index = s->major_cur_subblock_index;
1019  unsigned int num_subblocks = s->major_filter_state_subblock;
1020  RestartHeader *rh = &s->restart_header;
1021  int substr_restart_frame = restart_frame;
1022  uint8_t parity, checksum;
1023  PutBitContext pb;
1024  int params_changed;
1025 
1026  s->cur_restart_header = rh;
1027 
1028  init_put_bits(&pb, buf, buf_size);
1029 
1030  for (unsigned int subblock = 0; subblock <= num_subblocks; subblock++) {
1031  unsigned int subblock_index = cur_subblock_index++;
1032 
1033  params_changed = s->b[subblock_index].major_params_changed;
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, s, &pb);
1042  rh->lossless_check_data = 0;
1043  } else {
1044  put_bits(&pb, 1, 0);
1045  }
1046 
1047  write_decoding_params(ctx, s, &pb, params_changed,
1048  subblock_index);
1049  } else {
1050  put_bits(&pb, 1, 0);
1051  }
1052 
1053  write_block_data(ctx, s, &pb, subblock_index);
1054 
1055  put_bits(&pb, 1, !substr_restart_frame);
1056 
1057  substr_restart_frame = 0;
1058  }
1059 
1060  put_bits(&pb, (-put_bits_count(&pb)) & 15, 0);
1061 
1062  rh->lossless_check_data ^= lossless_check_data[0];
1063 
1064  if (ctx->last_frames == 0 && ctx->shorten_by) {
1065  if (ctx->avctx->codec_id == AV_CODEC_ID_TRUEHD) {
1066  put_bits(&pb, 16, END_OF_STREAM & 0xFFFF);
1067  put_bits(&pb, 16, (ctx->shorten_by & 0x1FFF) | 0xE000);
1068  } else {
1069  put_bits32(&pb, END_OF_STREAM);
1070  }
1071  }
1072 
1073  /* Data must be flushed for the checksum and parity to be correct;
1074  * notice that we already are word-aligned here. */
1075  flush_put_bits(&pb);
1076 
1077  parity = ff_mlp_calculate_parity(buf, put_bytes_output(&pb)) ^ 0xa9;
1078  checksum = ff_mlp_checksum8 (buf, put_bytes_output(&pb));
1079 
1080  put_bits(&pb, 8, parity );
1081  put_bits(&pb, 8, checksum);
1082 
1083  flush_put_bits(&pb);
1084 
1085  substream_data_len[0] = put_bytes_output(&pb);
1086 
1087  buf += substream_data_len[0];
1088 
1089  s->major_cur_subblock_index += s->major_filter_state_subblock + 1;
1090  s->major_filter_state_subblock = 0;
1091 
1092  return buf;
1093 }
1094 
1095 /** Writes the access unit and substream headers to the bitstream. */
1097  uint8_t *substream_headers, unsigned int length,
1098  int restart_frame,
1099  uint16_t substream_data_len[MAX_SUBSTREAMS])
1100 {
1101  uint16_t access_unit_header = 0;
1102  uint16_t substream_data_end = 0;
1103  uint16_t parity_nibble = 0;
1104 
1105  parity_nibble = ctx->input_timing;
1106  parity_nibble ^= length;
1107 
1108  for (unsigned int substr = 0; substr < ctx->num_substreams; substr++) {
1109  uint16_t substr_hdr = 0;
1110 
1111  substream_data_end += substream_data_len[substr];
1112 
1113  substr_hdr |= (0 << 15); /* extraword */
1114  substr_hdr |= (!restart_frame << 14); /* !restart_frame */
1115  substr_hdr |= (1 << 13); /* checkdata */
1116  substr_hdr |= (0 << 12); /* ??? */
1117  substr_hdr |= (substream_data_end / 2) & 0x0FFF;
1118 
1119  AV_WB16(substream_headers, substr_hdr);
1120 
1121  parity_nibble ^= *substream_headers++;
1122  parity_nibble ^= *substream_headers++;
1123  }
1124 
1125  parity_nibble ^= parity_nibble >> 8;
1126  parity_nibble ^= parity_nibble >> 4;
1127  parity_nibble &= 0xF;
1128 
1129  access_unit_header |= (parity_nibble ^ 0xF) << 12;
1130  access_unit_header |= length & 0xFFF;
1131 
1132  AV_WB16(frame_header , access_unit_header);
1133  AV_WB16(frame_header+2, ctx->input_timing );
1134 }
1135 
1136 /** Writes an entire access unit to the bitstream. */
1137 static int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf,
1138  int buf_size, int restart_frame)
1139 {
1140  uint16_t substream_data_len[MAX_SUBSTREAMS];
1141  uint8_t *buf1, *buf0 = buf;
1142  int total_length;
1143 
1144  /* Frame header will be written at the end. */
1145  buf += 4;
1146  buf_size -= 4;
1147 
1148  if (restart_frame) {
1149  write_major_sync(ctx, buf, buf_size);
1150  buf += 28;
1151  buf_size -= 28;
1152  }
1153 
1154  buf1 = buf;
1155 
1156  /* Substream headers will be written at the end. */
1157  for (unsigned int substr = 0; substr < ctx->num_substreams; substr++) {
1158  buf += 2;
1159  buf_size -= 2;
1160  }
1161 
1162  for (int substr = 0; substr < ctx->num_substreams; substr++) {
1163  MLPSubstream *s = &ctx->s[substr];
1164  uint8_t *buf0 = buf;
1165 
1166  buf = write_substr(ctx, s, buf, buf_size, restart_frame, &substream_data_len[substr]);
1167  buf_size -= buf - buf0;
1168  }
1169 
1170  total_length = buf - buf0;
1171 
1172  write_frame_headers(ctx, buf0, buf1, total_length / 2, restart_frame, substream_data_len);
1173 
1174  return total_length;
1175 }
1176 
1177 /****************************************************************************
1178  ****************** Functions that input data to context ********************
1179  ****************************************************************************/
1180 
1181 /** Inputs data from the samples passed by lavc into the context, shifts them
1182  * appropriately depending on the bit-depth, and calculates the
1183  * lossless_check_data that will be written to the restart header.
1184  */
1186  uint8_t **const samples,
1187  int nb_samples, int is24)
1188 {
1189  int32_t *lossless_check_data = &s->b[ctx->frame_index].lossless_check_data;
1190  RestartHeader *rh = &s->restart_header;
1191  int32_t temp_lossless_check_data = 0;
1192  uint32_t bits = 0;
1193 
1194  for (int i = 0; i < nb_samples; i++) {
1195  for (int ch = 0; ch <= rh->max_channel; ch++) {
1196  const int32_t *samples_32 = (const int32_t *)samples[ch];
1197  const int16_t *samples_16 = (const int16_t *)samples[ch];
1198  int32_t *sample_buffer = s->b[ctx->frame_index].inout_buffer[ch];
1199  int32_t sample;
1200 
1201  sample = is24 ? samples_32[i] >> 8 : samples_16[i] * 256;
1202 
1204 
1205  temp_lossless_check_data ^= (sample & 0x00ffffff) << ch;
1206  sample_buffer[i] = sample;
1207  }
1208  }
1209 
1210  for (int ch = 0; ch <= rh->max_channel; ch++) {
1211  for (int i = nb_samples; i < ctx->avctx->frame_size; i++) {
1212  int32_t *sample_buffer = s->b[ctx->frame_index].inout_buffer[ch];
1213 
1214  sample_buffer[i] = 0;
1215  }
1216  }
1217 
1218  s->b[ctx->frame_index].max_output_bits = bits;
1219 
1220  lossless_check_data[0] = temp_lossless_check_data;
1221 }
1222 
1223 /** Wrapper function for inputting data in two different bit-depths. */
1224 static void input_data(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples)
1225 {
1226  input_data_internal(ctx, s, samples, nb_samples, ctx->avctx->sample_fmt == AV_SAMPLE_FMT_S32P);
1227 }
1228 
1230 {
1231  RestartHeader *rh = &s->restart_header;
1232 
1233  for (unsigned int index = 0; index < ctx->number_of_frames; index++) {
1234  unsigned int cur_index = (ctx->frame_index + index + 1) % ctx->cur_restart_interval;
1235  DecodingParams *dp = &s->b[index+1].decoding_params;
1236 
1237  for (int ch = 0; ch <= rh->max_channel; ch++) {
1238  const int32_t *input_buffer = s->b[cur_index].inout_buffer[ch];
1239  int32_t *sample_buffer = dp->sample_buffer[ch];
1240  int off = 0;
1241 
1242  if (dp->blocksize < ctx->avctx->frame_size) {
1243  DecodingParams *dp = &s->b[index].decoding_params;
1244  int32_t *sample_buffer = dp->sample_buffer[ch];
1245  for (unsigned int i = 0; i < dp->blocksize; i++)
1246  sample_buffer[i] = input_buffer[i];
1247  off = dp->blocksize;
1248  }
1249 
1250  for (unsigned int i = 0; i < dp->blocksize; i++)
1251  sample_buffer[i] = input_buffer[i + off];
1252  }
1253  }
1254 }
1255 
1256 /****************************************************************************
1257  ********* Functions that analyze the data and set the parameters ***********
1258  ****************************************************************************/
1259 
1260 /** Counts the number of trailing zeroes in a value */
1261 static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int def)
1262 {
1263  return sample ? FFMIN(max, ff_ctz(sample)) : def;
1264 }
1265 
1267 {
1268  RestartHeader *rh = s->cur_restart_header;
1269  DecodingParams *dp1 = &s->b[1].decoding_params;
1270  int32_t sample_mask[MAX_CHANNELS];
1271 
1272  memset(sample_mask, 0, sizeof(sample_mask));
1273 
1274  for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1275  DecodingParams *dp = &s->b[j].decoding_params;
1276 
1277  for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
1278  int32_t *sample_buffer = dp->sample_buffer[ch];
1279 
1280  for (int i = 0; i < dp->blocksize; i++)
1281  sample_mask[ch] |= sample_buffer[i];
1282  }
1283  }
1284 
1285  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
1286  dp1->output_shift[ch] = number_trailing_zeroes(sample_mask[ch], 7, 0);
1287 
1288  for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1289  DecodingParams *dp = &s->b[j].decoding_params;
1290 
1291  for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
1292  int32_t *sample_buffer = dp->sample_buffer[ch];
1293  const int shift = dp1->output_shift[ch];
1294 
1295  for (int i = 0; i < dp->blocksize; i++)
1296  sample_buffer[i] >>= shift;
1297  }
1298  }
1299 }
1300 
1301 /** Determines how many bits are zero at the end of all samples so they can be
1302  * shifted out.
1303  */
1305 {
1306  RestartHeader *rh = s->cur_restart_header;
1307  DecodingParams *dp1 = &s->b[1].decoding_params;
1308  int32_t sample_mask[MAX_CHANNELS];
1309 
1310  memset(sample_mask, 0, sizeof(sample_mask));
1311 
1312  for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1313  DecodingParams *dp = &s->b[j].decoding_params;
1314 
1315  for (int ch = 0; ch <= rh->max_channel; ch++) {
1316  int32_t *sample_buffer = dp->sample_buffer[ch];
1317 
1318  for (int i = 0; i < dp->blocksize; i++)
1319  sample_mask[ch] |= sample_buffer[i];
1320  }
1321  }
1322 
1323  for (int ch = 0; ch <= rh->max_channel; ch++)
1324  dp1->quant_step_size[ch] = number_trailing_zeroes(sample_mask[ch], 15, 0);
1325 }
1326 
1327 /** Determines the smallest number of bits needed to encode the filter
1328  * coefficients, and if it's possible to right-shift their values without
1329  * losing any precision.
1330  */
1332 {
1333  uint32_t coeff_mask = 0;
1334  int bits = 0, shift;
1335 
1336  for (int order = 0; order < fp->order; order++) {
1337  int32_t coeff = fcoeff[order];
1338 
1340 
1341  coeff_mask |= coeff;
1342  }
1343 
1344  shift = FFMIN(7, coeff_mask ? ff_ctz(coeff_mask) : 0);
1345 
1346  fp->coeff_bits = FFMAX(1, bits - shift);
1347  fp->coeff_shift = FFMIN(shift, 16 - fp->coeff_bits);
1348 }
1349 
1350 /** Determines the best filter parameters for the given data and writes the
1351  * necessary information to the context.
1352  */
1354  int channel, int retry_filter)
1355 {
1356  ChannelParams *cp = &s->b[1].channel_params[channel];
1357  DecodingParams *dp1 = &s->b[1].decoding_params;
1358  FilterParams *fp = &cp->filter_params[FIR];
1359 
1360  if (retry_filter)
1361  dp1->max_order[channel]--;
1362 
1363  if (dp1->max_order[channel] == 0) {
1364  fp->order = 0;
1365  } else {
1366  int32_t *lpc_samples = ctx->lpc_sample_buffer;
1367  int32_t *fcoeff = cp->coeff[FIR];
1368  int shift[MAX_LPC_ORDER];
1369  int order;
1370 
1371  for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1372  DecodingParams *dp = &s->b[j].decoding_params;
1373  int32_t *sample_buffer = dp->sample_buffer[channel];
1374 
1375  for (unsigned int i = 0; i < dp->blocksize; i++)
1376  lpc_samples[i] = sample_buffer[i];
1377  lpc_samples += dp->blocksize;
1378  }
1379 
1380  order = ff_lpc_calc_coefs(&ctx->lpc_ctx, ctx->lpc_sample_buffer,
1381  lpc_samples - ctx->lpc_sample_buffer,
1383  ctx->lpc_coeff_precision,
1384  s->coefs[channel], shift, ctx->lpc_type, ctx->lpc_passes,
1385  ctx->prediction_order, MLP_MIN_LPC_SHIFT,
1386  MLP_MAX_LPC_SHIFT, 0);
1387 
1388  fp->order = order;
1389  fp->shift = order ? shift[order-1] : 0;
1390 
1391  for (unsigned int i = 0; i < order; i++)
1392  fcoeff[i] = s->coefs[channel][order-1][i];
1393 
1394  code_filter_coeffs(ctx, fp, fcoeff);
1395  }
1396 }
1397 
1398 /** Tries to determine a good prediction filter, and applies it to the samples
1399  * buffer if the filter is good enough. Sets the filter data to be cleared if
1400  * no good filter was found.
1401  */
1403 {
1404  RestartHeader *rh = s->cur_restart_header;
1405 
1406  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++)
1407  set_filter(ctx, s, ch, 0);
1408 }
1409 
1411  MatrixParams *mp,
1412  int ch0, int ch1)
1413 {
1414  int32_t maxl = INT32_MIN, maxr = INT32_MIN, minl = INT32_MAX, minr = INT32_MAX;
1415  int64_t summ = 0, sums = 0, suml = 0, sumr = 0, enl = 0, enr = 0;
1416  const int shift = 14 - ctx->rematrix_precision;
1417  int32_t cf0, cf1, e[4], d[4];
1418  int64_t ml, mr;
1419  int i, count = 0;
1420 
1421  for (int j = 0; j <= ctx->cur_restart_interval; j++) {
1422  DecodingParams *dp = &s->b[j].decoding_params;
1423  const int32_t *ch[2];
1424 
1425  ch[0] = dp->sample_buffer[ch0];
1426  ch[1] = dp->sample_buffer[ch1];
1427 
1428  for (int i = 0; i < dp->blocksize; i++) {
1429  int32_t lm = ch[0][i], rm = ch[1][i];
1430 
1431  enl += FFABS(lm);
1432  enr += FFABS(rm);
1433 
1434  summ += FFABS(lm + rm);
1435  sums += FFABS(lm - rm);
1436 
1437  suml += lm;
1438  sumr += rm;
1439 
1440  maxl = FFMAX(maxl, lm);
1441  maxr = FFMAX(maxr, rm);
1442 
1443  minl = FFMIN(minl, lm);
1444  minr = FFMIN(minr, rm);
1445  }
1446  }
1447 
1448  summ -= FFABS(suml + sumr);
1449  sums -= FFABS(suml - sumr);
1450 
1451  ml = maxl - (int64_t)minl;
1452  mr = maxr - (int64_t)minr;
1453 
1454  if (!summ && !sums)
1455  return 0;
1456 
1457  if (!ml || !mr)
1458  return 0;
1459 
1460  if ((FFABS(ml) + FFABS(mr)) >= (1 << 24))
1461  return 0;
1462 
1463  cf0 = (FFMIN(FFABS(mr), FFABS(ml)) * (1LL << 14)) / FFMAX(FFABS(ml), FFABS(mr));
1464  cf0 = (cf0 >> shift) << shift;
1465  cf1 = -cf0;
1466 
1467  if (sums > summ)
1468  FFSWAP(int32_t, cf0, cf1);
1469 
1470  count = 1;
1471  i = enl < enr;
1472  mp->outch[0] = ch0 + i;
1473 
1474  d[!i] = cf0;
1475  d[ i] = 1 << 14;
1476  e[!i] = cf1;
1477  e[ i] = 1 << 14;
1478 
1479  mp->coeff[0][ch0] = av_clip_intp2(d[0], 15);
1480  mp->coeff[0][ch1] = av_clip_intp2(d[1], 15);
1481 
1482  mp->forco[0][ch0] = av_clip_intp2(e[0], 15);
1483  mp->forco[0][ch1] = av_clip_intp2(e[1], 15);
1484 
1485  return count;
1486 }
1487 
1488 /** Determines how many fractional bits are needed to encode matrix
1489  * coefficients. Also shifts the coefficients to fit within 2.14 bits.
1490  */
1492  DecodingParams *dp,
1493  unsigned int mat)
1494 {
1495  RestartHeader *rh = s->cur_restart_header;
1496  MatrixParams *mp = &dp->matrix_params;
1497  int32_t coeff_mask = 0;
1498 
1499  for (int ch = 0; ch <= rh->max_matrix_channel; ch++)
1500  coeff_mask |= mp->coeff[mat][ch];
1501 
1502  mp->fbits[mat] = 14 - number_trailing_zeroes(coeff_mask, 14, 14);
1503 }
1504 
1505 /** Determines best coefficients to use for the lossless matrix. */
1507 {
1508  RestartHeader *rh = s->cur_restart_header;
1509  DecodingParams *dp = &s->b[1].decoding_params;
1510  MatrixParams *mp = &dp->matrix_params;
1511 
1512  mp->count = 0;
1513  if (ctx->num_channels - 2 != 2)
1514  return;
1515 
1516  mp->count = estimate_coeff(ctx, s, mp,
1517  rh->min_channel, rh->max_channel);
1518 
1519  for (int mat = 0; mat < mp->count; mat++)
1520  code_matrix_coeffs(ctx, s, dp, mat);
1521 }
1522 
1523 /** Min and max values that can be encoded with each codebook. The values for
1524  * the third codebook take into account the fact that the sign shift for this
1525  * codebook is outside the coded value, so it has one more bit of precision.
1526  * It should actually be -7 -> 7, shifted down by 0.5.
1527  */
1528 static const int8_t codebook_extremes[3][2] = {
1529  {-9, 8}, {-8, 7}, {-15, 14},
1530 };
1531 
1532 /** Determines the amount of bits needed to encode the samples using no
1533  * codebooks and a specified offset.
1534  */
1536  DecodingParams *dp,
1537  int channel, int32_t offset,
1539  BestOffset *bo)
1540 {
1541  int32_t unsign = 0;
1542  int lsb_bits;
1543 
1544  min -= offset;
1545  max -= offset;
1546 
1547  lsb_bits = FFMAX(number_sbits(min), number_sbits(max)) - 1;
1548 
1549  lsb_bits += !!lsb_bits;
1550 
1551  if (lsb_bits > 0)
1552  unsign = 1U << (lsb_bits - 1);
1553 
1554  bo->offset = offset;
1555  bo->lsb_bits = lsb_bits;
1556  bo->bitcount = lsb_bits * dp->blocksize;
1557  bo->min = offset - unsign + 1;
1558  bo->max = offset + unsign;
1559 }
1560 
1561 /** Determines the least amount of bits needed to encode the samples using no
1562  * codebooks.
1563  */
1565  DecodingParams *dp,
1566  int channel,
1568  BestOffset *bo)
1569 {
1570  int32_t offset, unsign = 0;
1571  uint8_t lsb_bits;
1572 
1573  /* Set offset inside huffoffset's boundaries by adjusting extremes
1574  * so that more bits are used, thus shifting the offset. */
1575  if (min < HUFF_OFFSET_MIN)
1576  max = FFMAX(max, 2 * HUFF_OFFSET_MIN - min + 1);
1577  if (max > HUFF_OFFSET_MAX)
1578  min = FFMIN(min, 2 * HUFF_OFFSET_MAX - max - 1);
1579 
1580  lsb_bits = FFMAX(number_sbits(min), number_sbits(max));
1581 
1582  if (lsb_bits > 0)
1583  unsign = 1 << (lsb_bits - 1);
1584 
1585  /* If all samples are the same (lsb_bits == 0), offset must be
1586  * adjusted because of sign_shift. */
1587  offset = min + (max - min) / 2 + !!lsb_bits;
1588 
1589  bo->offset = offset;
1590  bo->lsb_bits = lsb_bits;
1591  bo->bitcount = lsb_bits * dp->blocksize;
1592  bo->min = max - unsign + 1;
1593  bo->max = min + unsign;
1594  bo->min = FFMAX(bo->min, HUFF_OFFSET_MIN);
1595  bo->max = FFMIN(bo->max, HUFF_OFFSET_MAX);
1596 }
1597 
1598 /** Determines the least amount of bits needed to encode the samples using a
1599  * given codebook and a given offset.
1600  */
1602  DecodingParams *dp,
1603  int channel, int codebook,
1604  int32_t sample_min, int32_t sample_max,
1605  int32_t offset, BestOffset *bo)
1606 {
1607  int32_t codebook_min = codebook_extremes[codebook][0];
1608  int32_t codebook_max = codebook_extremes[codebook][1];
1609  int32_t *sample_buffer = dp->sample_buffer[channel];
1610  int codebook_offset = 7 + (2 - codebook);
1611  int32_t unsign_offset = offset;
1612  uint32_t bitcount = 0;
1613  int lsb_bits = 0;
1614  int offset_min = INT_MAX, offset_max = INT_MAX;
1615  int unsign, mask;
1616 
1617  sample_min -= offset;
1618  sample_max -= offset;
1619 
1620  while (sample_min < codebook_min || sample_max > codebook_max) {
1621  lsb_bits++;
1622  sample_min >>= 1;
1623  sample_max >>= 1;
1624  }
1625 
1626  unsign = 1 << lsb_bits;
1627  mask = unsign - 1;
1628 
1629  if (codebook == 2) {
1630  unsign_offset -= unsign;
1631  lsb_bits++;
1632  }
1633 
1634  for (int i = 0; i < dp->blocksize; i++) {
1635  int32_t sample = sample_buffer[i] >> dp->quant_step_size[channel];
1636  int temp_min, temp_max;
1637 
1638  sample -= unsign_offset;
1639 
1640  temp_min = sample & mask;
1641  if (temp_min < offset_min)
1642  offset_min = temp_min;
1643 
1644  temp_max = unsign - temp_min - 1;
1645  if (temp_max < offset_max)
1646  offset_max = temp_max;
1647 
1648  sample >>= lsb_bits;
1649 
1650  bitcount += ff_mlp_huffman_tables[codebook][sample + codebook_offset][1];
1651  }
1652 
1653  bo->offset = offset;
1654  bo->lsb_bits = lsb_bits;
1655  bo->bitcount = lsb_bits * dp->blocksize + bitcount;
1656  bo->min = FFMAX(offset - offset_min, HUFF_OFFSET_MIN);
1657  bo->max = FFMIN(offset + offset_max, HUFF_OFFSET_MAX);
1658 }
1659 
1660 /** Determines the least amount of bits needed to encode the samples using a
1661  * given codebook. Searches for the best offset to minimize the bits.
1662  */
1663 static inline void codebook_bits(MLPEncodeContext *ctx,
1664  DecodingParams *dp,
1665  int channel, int codebook,
1666  int offset, int32_t min, int32_t max,
1667  BestOffset *bo, int direction)
1668 {
1669  uint32_t previous_count = UINT32_MAX;
1670  int offset_min, offset_max;
1671  int is_greater = 0;
1672 
1673  offset_min = FFMAX(min, HUFF_OFFSET_MIN);
1674  offset_max = FFMIN(max, HUFF_OFFSET_MAX);
1675 
1676  while (offset <= offset_max && offset >= offset_min) {
1677  BestOffset temp_bo;
1678 
1680  min, max, offset,
1681  &temp_bo);
1682 
1683  if (temp_bo.bitcount < previous_count) {
1684  if (temp_bo.bitcount < bo->bitcount)
1685  *bo = temp_bo;
1686 
1687  is_greater = 0;
1688  } else if (++is_greater >= ctx->max_codebook_search)
1689  break;
1690 
1691  previous_count = temp_bo.bitcount;
1692 
1693  if (direction) {
1694  offset = temp_bo.max + 1;
1695  } else {
1696  offset = temp_bo.min - 1;
1697  }
1698  }
1699 }
1700 
1701 /** Determines the least amount of bits needed to encode the samples using
1702  * any or no codebook.
1703  */
1705 {
1706  RestartHeader *rh = s->cur_restart_header;
1707  for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1708  DecodingParams *dp = &s->b[index].decoding_params;
1709 
1710  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1711  ChannelParams *cp = &s->b[index].channel_params[ch];
1712  int32_t *sample_buffer = dp->sample_buffer[ch];
1713  int32_t min = INT32_MAX, max = INT32_MIN;
1714  int no_filters_used = !cp->filter_params[FIR].order;
1715  int average = 0;
1716  int offset = 0;
1717 
1718  /* Determine extremes and average. */
1719  for (int i = 0; i < dp->blocksize; i++) {
1720  int32_t sample = sample_buffer[i] >> dp->quant_step_size[ch];
1721  if (sample < min)
1722  min = sample;
1723  if (sample > max)
1724  max = sample;
1725  average += sample;
1726  }
1727  average /= dp->blocksize;
1728 
1729  /* If filtering is used, we always set the offset to zero, otherwise
1730  * we search for the offset that minimizes the bitcount. */
1731  if (no_filters_used) {
1732  no_codebook_bits(ctx, dp, ch, min, max, &s->b[index].best_offset[ch][0]);
1734  } else {
1735  no_codebook_bits_offset(ctx, dp, ch, offset, min, max, &s->b[index].best_offset[ch][0]);
1736  }
1737 
1738  for (int i = 1; i < NUM_CODEBOOKS; i++) {
1739  BestOffset temp_bo = { 0, UINT32_MAX, 0, 0, 0, };
1740  int32_t offset_max;
1741 
1742  codebook_bits_offset(ctx, dp, ch, i - 1,
1743  min, max, offset,
1744  &temp_bo);
1745 
1746  if (no_filters_used) {
1747  offset_max = temp_bo.max;
1748 
1749  codebook_bits(ctx, dp, ch, i - 1, temp_bo.min - 1,
1750  min, max, &temp_bo, 0);
1751  codebook_bits(ctx, dp, ch, i - 1, offset_max + 1,
1752  min, max, &temp_bo, 1);
1753  }
1754 
1755  s->b[index].best_offset[ch][i] = temp_bo;
1756  }
1757  }
1758  }
1759 }
1760 
1761 /****************************************************************************
1762  *************** Functions that process the data in some way ****************
1763  ****************************************************************************/
1764 
1765 #define SAMPLE_MAX(bitdepth) ((1 << (bitdepth - 1)) - 1)
1766 #define SAMPLE_MIN(bitdepth) (~SAMPLE_MAX(bitdepth))
1767 
1768 #define MSB_MASK(bits) (-(int)(1u << (bits)))
1769 
1770 /** Applies the filter to the current samples, and saves the residual back
1771  * into the samples buffer. If the filter is too bad and overflows the
1772  * maximum amount of bits allowed (24), the samples buffer is left as is and
1773  * the function returns -1.
1774  */
1776 {
1777  DecodingParams *dp = &s->b[1].decoding_params;
1778  ChannelParams *cp = &s->b[1].channel_params[channel];
1779  FilterParams *fp[NUM_FILTERS] = { &cp->filter_params[FIR],
1780  &cp->filter_params[IIR], };
1781  const uint8_t codebook = cp->codebook;
1783  int32_t *sample_buffer = s->b[0].decoding_params.sample_buffer[channel];
1784  unsigned int filter_shift = fp[FIR]->shift;
1785  int32_t *filter_state[NUM_FILTERS] = { ctx->filter_state[FIR],
1786  ctx->filter_state[IIR], };
1787  int i, j = 1, k = 0;
1788 
1789  for (i = 0; i < 8; i++) {
1790  filter_state[FIR][i] = sample_buffer[i];
1791  filter_state[IIR][i] = sample_buffer[i];
1792  }
1793 
1794  while (1) {
1795  int32_t *sample_buffer = s->b[j].decoding_params.sample_buffer[channel];
1796  unsigned int blocksize = s->b[j].decoding_params.blocksize;
1797  int32_t sample, residual;
1798  int64_t accum = 0;
1799 
1800  if (!blocksize)
1801  break;
1802 
1803  for (int filter = 0; filter < NUM_FILTERS; filter++) {
1804  int32_t *fcoeff = cp->coeff[filter];
1805  for (unsigned int order = 0; order < fp[filter]->order; order++)
1806  accum += (int64_t)filter_state[filter][i - 1 - order] *
1807  fcoeff[order];
1808  }
1809 
1810  sample = sample_buffer[k];
1811  accum >>= filter_shift;
1812  residual = sample - (accum & mask);
1813 
1814  if ((codebook > 0) &&
1815  (residual < SAMPLE_MIN(24) ||
1816  residual > SAMPLE_MAX(24)))
1817  return -1;
1818 
1819  filter_state[FIR][i] = sample;
1820  filter_state[IIR][i] = residual;
1821 
1822  i++;
1823  k++;
1824  if (k >= blocksize) {
1825  k = 0;
1826  j++;
1827  if (j > ctx->cur_restart_interval)
1828  break;
1829  }
1830  }
1831 
1832  for (int l = 0, j = 0; j <= ctx->cur_restart_interval; j++) {
1833  int32_t *sample_buffer = s->b[j].decoding_params.sample_buffer[channel];
1834  unsigned int blocksize = s->b[j].decoding_params.blocksize;
1835 
1836  for (int i = 0; i < blocksize; i++, l++)
1837  sample_buffer[i] = filter_state[IIR][l];
1838  }
1839 
1840  return 0;
1841 }
1842 
1844 {
1845  RestartHeader *rh = s->cur_restart_header;
1846 
1847  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
1848  while (apply_filter(ctx, s, ch) < 0) {
1849  /* Filter is horribly wrong. Retry. */
1850  set_filter(ctx, s, ch, 1);
1851  }
1852  }
1853 }
1854 
1855 /** Generates two noise channels worth of data. */
1857 {
1858  RestartHeader *rh = s->cur_restart_header;
1859  uint32_t seed = rh->noisegen_seed;
1860 
1861  for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1862  DecodingParams *dp = &s->b[j].decoding_params;
1863  int32_t *sample_buffer2 = dp->sample_buffer[ctx->num_channels-2];
1864  int32_t *sample_buffer1 = dp->sample_buffer[ctx->num_channels-1];
1865 
1866  for (unsigned int i = 0; i < dp->blocksize; i++) {
1867  uint16_t seed_shr7 = seed >> 7;
1868  sample_buffer2[i] = ((int8_t)(seed >> 15)) * (1 << rh->noise_shift);
1869  sample_buffer1[i] = ((int8_t) seed_shr7) * (1 << rh->noise_shift);
1870 
1871  seed = (seed << 16) ^ seed_shr7 ^ (seed_shr7 << 5);
1872  }
1873  }
1874 
1875  rh->noisegen_seed = seed & ((1 << 24)-1);
1876 }
1877 
1878 /** Rematrixes all channels using chosen coefficients. */
1880 {
1881  RestartHeader *rh = s->cur_restart_header;
1882  DecodingParams *dp1 = &s->b[1].decoding_params;
1883  MatrixParams *mp1 = &dp1->matrix_params;
1884  const int maxchan = rh->max_matrix_channel;
1885  int32_t orig_samples[MAX_NCHANNELS];
1886  int32_t rematrix_samples[MAX_NCHANNELS];
1887  uint8_t lsb_bypass[MAX_MATRICES] = { 0 };
1888 
1889  for (unsigned int j = 0; j <= ctx->cur_restart_interval; j++) {
1890  DecodingParams *dp = &s->b[j].decoding_params;
1891  MatrixParams *mp = &dp->matrix_params;
1892 
1893  for (unsigned int i = 0; i < dp->blocksize; i++) {
1894  for (int ch = 0; ch <= maxchan; ch++)
1895  orig_samples[ch] = rematrix_samples[ch] = dp->sample_buffer[ch][i];
1896 
1897  for (int mat = 0; mat < mp1->count; mat++) {
1898  unsigned int outch = mp1->outch[mat];
1899  int64_t accum = 0;
1900 
1901  for (int ch = 0; ch <= maxchan; ch++) {
1902  int32_t sample = rematrix_samples[ch];
1903 
1904  accum += (int64_t)sample * mp1->forco[mat][ch];
1905  }
1906 
1907  rematrix_samples[outch] = accum >> 14;
1908  }
1909 
1910  for (int ch = 0; ch <= maxchan; ch++)
1911  dp->sample_buffer[ch][i] = rematrix_samples[ch];
1912 
1913  for (unsigned int mat = 0; mat < mp1->count; mat++) {
1914  int8_t *bypassed_lsbs = mp->bypassed_lsbs[mat];
1915  unsigned int outch = mp1->outch[mat];
1916  int64_t accum = 0;
1917  int8_t bit;
1918 
1919  for (int ch = 0; ch <= maxchan; ch++) {
1920  int32_t sample = rematrix_samples[ch];
1921 
1922  accum += (int64_t)sample * mp1->coeff[mat][ch];
1923  }
1924 
1925  rematrix_samples[outch] = accum >> 14;
1926  bit = rematrix_samples[outch] != orig_samples[outch];
1927 
1928  bypassed_lsbs[i] = bit;
1929  lsb_bypass[mat] |= bit;
1930  }
1931  }
1932  }
1933 
1934  for (unsigned int mat = 0; mat < mp1->count; mat++)
1935  mp1->lsb_bypass[mat] = lsb_bypass[mat];
1936 }
1937 
1938 /****************************************************************************
1939  **** Functions that deal with determining the best parameters and output ***
1940  ****************************************************************************/
1941 
1942 typedef struct PathCounter {
1943  char path[MAX_HEADER_INTERVAL + 2];
1944  int cur_idx;
1945  uint32_t bitcount;
1948 #define CODEBOOK_CHANGE_BITS 21
1949 
1950 static void clear_path_counter(PathCounter *path_counter)
1951 {
1952  memset(path_counter, 0, (NUM_CODEBOOKS + 1) * sizeof(*path_counter));
1953 }
1954 
1955 static int compare_best_offset(const BestOffset *prev, const BestOffset *cur)
1956 {
1957  return prev->lsb_bits != cur->lsb_bits;
1958 }
1959 
1961  int channel,
1962  PathCounter *src, int cur_codebook)
1963 {
1964  int idx = src->cur_idx;
1965  const BestOffset *cur_bo = s->b[idx].best_offset[channel],
1966  *prev_bo = idx ? s->b[idx - 1].best_offset[channel] :
1968  uint32_t bitcount = src->bitcount;
1969  int prev_codebook = src->path[idx];
1970 
1971  bitcount += cur_bo[cur_codebook].bitcount;
1972 
1973  if (prev_codebook != cur_codebook ||
1974  compare_best_offset(&prev_bo[prev_codebook], &cur_bo[cur_codebook]))
1975  bitcount += CODEBOOK_CHANGE_BITS;
1976 
1977  return bitcount;
1978 }
1979 
1981 {
1982  RestartHeader *rh = s->cur_restart_header;
1983 
1984  for (int channel = rh->min_channel; channel <= rh->max_channel; channel++) {
1985  const BestOffset *prev_bo = restart_best_offset;
1986  BestOffset *cur_bo;
1987  PathCounter path_counter[NUM_CODEBOOKS + 1];
1988  unsigned int best_codebook;
1989  char *best_path;
1990 
1991  clear_path_counter(path_counter);
1992 
1993  for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
1994  uint32_t best_bitcount = UINT32_MAX;
1995 
1996  cur_bo = s->b[index].best_offset[channel];
1997 
1998  for (unsigned int codebook = 0; codebook < NUM_CODEBOOKS; codebook++) {
1999  uint32_t prev_best_bitcount = UINT32_MAX;
2000 
2001  for (unsigned int last_best = 0; last_best < 2; last_best++) {
2002  PathCounter *dst_path = &path_counter[codebook];
2003  PathCounter *src_path;
2004  uint32_t temp_bitcount;
2005 
2006  /* First test last path with same headers,
2007  * then with last best. */
2008  if (last_best) {
2009  src_path = &path_counter[NUM_CODEBOOKS];
2010  } else {
2011  if (compare_best_offset(&prev_bo[codebook], &cur_bo[codebook]))
2012  continue;
2013  else
2014  src_path = &path_counter[codebook];
2015  }
2016 
2017  temp_bitcount = best_codebook_path_cost(ctx, s, channel, src_path, codebook);
2018 
2019  if (temp_bitcount < best_bitcount) {
2020  best_bitcount = temp_bitcount;
2021  best_codebook = codebook;
2022  }
2023 
2024  if (temp_bitcount < prev_best_bitcount) {
2025  prev_best_bitcount = temp_bitcount;
2026  if (src_path != dst_path)
2027  memcpy(dst_path, src_path, sizeof(PathCounter));
2028  if (dst_path->cur_idx < FF_ARRAY_ELEMS(dst_path->path) - 1)
2029  dst_path->path[++dst_path->cur_idx] = codebook;
2030  dst_path->bitcount = temp_bitcount;
2031  }
2032  }
2033  }
2034 
2035  prev_bo = cur_bo;
2036 
2037  memcpy(&path_counter[NUM_CODEBOOKS], &path_counter[best_codebook], sizeof(PathCounter));
2038  }
2039 
2040  best_path = path_counter[NUM_CODEBOOKS].path + 1;
2041 
2042  /* Update context. */
2043  for (unsigned int index = 0; index < ctx->number_of_subblocks; index++) {
2044  ChannelParams *cp = &s->b[index].channel_params[channel];
2045  DecodingParams *dp = &s->b[index].decoding_params;
2046 
2047  best_codebook = *best_path++;
2048  cur_bo = &s->b[index].best_offset[channel][best_codebook];
2049 
2050  cp->huff_offset = cur_bo->offset;
2051  cp->huff_lsbs = cur_bo->lsb_bits + dp->quant_step_size[channel];
2052  cp->codebook = best_codebook;
2053  }
2054  }
2055 }
2056 
2057 /** Analyzes all collected bitcounts and selects the best parameters for each
2058  * individual access unit.
2059  * TODO This is just a stub!
2060  */
2062 {
2063  RestartHeader *rh = s->cur_restart_header;
2064  uint8_t max_huff_lsbs = 0, max_output_bits = 0;
2065  int8_t max_shift = 0;
2066 
2067  for (int index = 0; index < s->b[ctx->restart_intervals-1].seq_size; index++) {
2068  memcpy(&s->b[index].major_decoding_params,
2069  &s->b[index].decoding_params, sizeof(DecodingParams));
2070  for (int ch = 0; ch <= rh->max_matrix_channel; ch++) {
2071  int8_t shift = s->b[index].decoding_params.output_shift[ch];
2072 
2073  max_shift = FFMAX(max_shift, shift);
2074  }
2075  for (int ch = rh->min_channel; ch <= rh->max_channel; ch++) {
2076  uint8_t huff_lsbs = s->b[index].channel_params[ch].huff_lsbs;
2077 
2078  max_huff_lsbs = FFMAX(max_huff_lsbs, huff_lsbs);
2079 
2080  memcpy(&s->b[index].major_channel_params[ch],
2081  &s->b[index].channel_params[ch],
2082  sizeof(ChannelParams));
2083  }
2084  }
2085 
2086  rh->max_huff_lsbs = max_huff_lsbs;
2087  rh->max_shift = max_shift;
2088 
2089  for (int index = 0; index < ctx->number_of_frames; index++)
2090  if (max_output_bits < s->b[index].max_output_bits)
2091  max_output_bits = s->b[index].max_output_bits;
2092  rh->max_output_bits = max_output_bits;
2093 
2094  s->cur_restart_header = &s->restart_header;
2095 
2096  for (int index = 0; index <= ctx->cur_restart_interval; index++)
2097  s->b[index].major_params_changed = compare_decoding_params(ctx, s, index);
2098 
2099  s->major_filter_state_subblock = 1;
2100  s->major_cur_subblock_index = 0;
2101 }
2102 
2104 {
2105  s->cur_restart_header = &s->restart_header;
2106 
2107  /* Copy frame_size from frames 0...max to decoding_params 1...max + 1
2108  * decoding_params[0] is for the filter state subblock.
2109  */
2110  for (unsigned int index = 0; index < ctx->number_of_frames; index++) {
2111  DecodingParams *dp = &s->b[index+1].decoding_params;
2112  dp->blocksize = ctx->avctx->frame_size;
2113  }
2114  /* The official encoder seems to always encode a filter state subblock
2115  * even if there are no filters. TODO check if it is possible to skip
2116  * the filter state subblock for no filters.
2117  */
2118  s->b[0].decoding_params.blocksize = 8;
2119  s->b[1].decoding_params.blocksize -= 8;
2120 
2125  rematrix_channels (ctx, s);
2127  determine_filters (ctx, s);
2128  apply_filters (ctx, s);
2129 
2131 
2132  determine_bits(ctx, s);
2133 
2135 }
2136 
2138 {
2139  ctx->number_of_frames = ctx->major_number_of_frames;
2140 
2141  s->cur_restart_header = &s->restart_header;
2142 
2144  rematrix_channels (ctx, s);
2145 
2146  apply_filters(ctx, s);
2147 }
2148 
2149 /****************************************************************************/
2150 
2151 static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
2152  const AVFrame *frame, int *got_packet)
2153 {
2154  MLPEncodeContext *ctx = avctx->priv_data;
2155  int bytes_written = 0;
2156  int channels = avctx->ch_layout.nb_channels;
2157  int restart_frame, ret;
2158  const uint8_t *data;
2159 
2160  if (!frame && !ctx->last_frames)
2161  ctx->last_frames = (ctx->afq.remaining_samples + avctx->frame_size - 1) / avctx->frame_size;
2162 
2163  if (!frame && !ctx->last_frames--)
2164  return 0;
2165 
2166  if ((ret = ff_alloc_packet(avctx, avpkt, 87500 * channels)) < 0)
2167  return ret;
2168 
2169  if (frame) {
2170  /* add current frame to queue */
2171  if ((ret = ff_af_queue_add(&ctx->afq, frame)) < 0)
2172  return ret;
2173  }
2174 
2175  data = frame ? frame->data[0] : NULL;
2176 
2177  ctx->frame_index = avctx->frame_num % ctx->cur_restart_interval;
2178 
2179  if (avctx->frame_num < ctx->cur_restart_interval) {
2180  if (data)
2181  goto input_and_return;
2182  }
2183 
2184  restart_frame = !ctx->frame_index;
2185 
2186  if (restart_frame) {
2187  avpkt->flags |= AV_PKT_FLAG_KEY;
2188  for (int n = 0; n < ctx->num_substreams; n++)
2189  set_major_params(ctx, &ctx->s[n]);
2190 
2191  if (ctx->min_restart_interval != ctx->cur_restart_interval)
2192  process_major_frame(ctx, &ctx->s[0]);
2193  }
2194 
2195  bytes_written = write_access_unit(ctx, avpkt->data, avpkt->size, restart_frame);
2196 
2197  ctx->output_timing += avctx->frame_size;
2198  ctx->input_timing += avctx->frame_size;
2199 
2200 input_and_return:
2201 
2202  if (frame) {
2203  ctx->shorten_by = avctx->frame_size - frame->nb_samples;
2204  ctx->next_major_frame_size += avctx->frame_size;
2205  ctx->next_major_number_of_frames++;
2206  }
2207  if (data)
2208  for (int n = 0; n < ctx->num_substreams; n++)
2209  input_data(ctx, &ctx->s[n], frame->extended_data, frame->nb_samples);
2210 
2211  restart_frame = (ctx->frame_index + 1) % ctx->min_restart_interval;
2212 
2213  if (!restart_frame) {
2214  for (unsigned int seq_index = 0; seq_index < ctx->restart_intervals; seq_index++) {
2215  unsigned int number_of_samples;
2216 
2217  ctx->number_of_frames = ctx->next_major_number_of_frames;
2218  ctx->number_of_subblocks = ctx->next_major_number_of_frames + 1;
2219 
2220  number_of_samples = avctx->frame_size * ctx->number_of_frames;
2221 
2222  for (int n = 0; n < ctx->num_substreams; n++) {
2223  MLPSubstream *s = &ctx->s[n];
2224 
2225  for (int i = 0; i < s->b[seq_index].seq_size; i++) {
2226  clear_channel_params(s->b[i].channel_params, channels);
2227  default_decoding_params(ctx, &s->b[i].decoding_params);
2228  }
2229  }
2230 
2231  if (number_of_samples > 0) {
2232  for (int n = 0; n < ctx->num_substreams; n++)
2233  analyze_sample_buffer(ctx, &ctx->s[n]);
2234  }
2235  }
2236 
2237  if (ctx->frame_index == (ctx->cur_restart_interval - 1)) {
2238  ctx->major_frame_size = ctx->next_major_frame_size;
2239  ctx->next_major_frame_size = 0;
2240  ctx->major_number_of_frames = ctx->next_major_number_of_frames;
2241  ctx->next_major_number_of_frames = 0;
2242  }
2243  }
2244 
2245  if (!frame && ctx->last_frames < ctx->cur_restart_interval - 1)
2246  avctx->frame_num++;
2247 
2248  if (bytes_written > 0) {
2249  ff_af_queue_remove(&ctx->afq,
2250  FFMIN(avctx->frame_size, ctx->afq.remaining_samples),
2251  &avpkt->pts,
2252  &avpkt->duration);
2253 
2254  av_shrink_packet(avpkt, bytes_written);
2255 
2256  *got_packet = 1;
2257  } else {
2258  *got_packet = 0;
2259  }
2260 
2261  return 0;
2262 }
2263 
2265 {
2266  MLPEncodeContext *ctx = avctx->priv_data;
2267 
2268  ff_lpc_end(&ctx->lpc_ctx);
2269  ff_af_queue_close(&ctx->afq);
2270 
2271  return 0;
2272 }
2273 
2274 #define FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
2275 #define OFFSET(x) offsetof(MLPEncodeContext, x)
2276 static const AVOption mlp_options[] = {
2277 { "max_interval", "Max number of frames between each new header", OFFSET(max_restart_interval), AV_OPT_TYPE_INT, {.i64 = 16 }, MIN_HEADER_INTERVAL, MAX_HEADER_INTERVAL, FLAGS },
2278 { "lpc_coeff_precision", "LPC coefficient precision", OFFSET(lpc_coeff_precision), AV_OPT_TYPE_INT, {.i64 = 15 }, 0, 15, FLAGS },
2279 { "lpc_type", "LPC algorithm", OFFSET(lpc_type), AV_OPT_TYPE_INT, {.i64 = FF_LPC_TYPE_LEVINSON }, FF_LPC_TYPE_LEVINSON, FF_LPC_TYPE_CHOLESKY, FLAGS, .unit = "lpc_type" },
2280 { "levinson", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_LEVINSON }, 0, 0, FLAGS, .unit = "lpc_type" },
2281 { "cholesky", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_LPC_TYPE_CHOLESKY }, 0, 0, FLAGS, .unit = "lpc_type" },
2282 { "lpc_passes", "Number of passes to use for Cholesky factorization during LPC analysis", OFFSET(lpc_passes), AV_OPT_TYPE_INT, {.i64 = 2 }, 1, INT_MAX, FLAGS },
2283 { "codebook_search", "Max number of codebook searches", OFFSET(max_codebook_search), AV_OPT_TYPE_INT, {.i64 = 3 }, 1, 100, FLAGS },
2284 { "prediction_order", "Search method for selecting prediction order", OFFSET(prediction_order), AV_OPT_TYPE_INT, {.i64 = ORDER_METHOD_EST }, ORDER_METHOD_EST, ORDER_METHOD_SEARCH, FLAGS, .unit = "predm" },
2285 { "estimation", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_EST }, 0, 0, FLAGS, .unit = "predm" },
2286 { "search", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = ORDER_METHOD_SEARCH }, 0, 0, FLAGS, .unit = "predm" },
2287 { "rematrix_precision", "Rematrix coefficient precision", OFFSET(rematrix_precision), AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 14, FLAGS },
2288 { NULL },
2289 };
2290 
2291 static const AVClass mlp_class = {
2292  .class_name = "mlpenc",
2293  .item_name = av_default_item_name,
2294  .option = mlp_options,
2295  .version = LIBAVUTIL_VERSION_INT,
2296 };
2297 
2298 #if CONFIG_MLP_ENCODER
2299 const FFCodec ff_mlp_encoder = {
2300  .p.name ="mlp",
2301  CODEC_LONG_NAME("MLP (Meridian Lossless Packing)"),
2302  .p.type = AVMEDIA_TYPE_AUDIO,
2303  .p.id = AV_CODEC_ID_MLP,
2304  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
2306  .priv_data_size = sizeof(MLPEncodeContext),
2307  .init = mlp_encode_init,
2309  .close = mlp_encode_close,
2310  .p.priv_class = &mlp_class,
2311  .p.sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE},
2312  .p.supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2313  .p.ch_layouts = ff_mlp_ch_layouts,
2314  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2315 };
2316 #endif
2317 #if CONFIG_TRUEHD_ENCODER
2318 const FFCodec ff_truehd_encoder = {
2319  .p.name ="truehd",
2320  CODEC_LONG_NAME("TrueHD"),
2321  .p.type = AVMEDIA_TYPE_AUDIO,
2322  .p.id = AV_CODEC_ID_TRUEHD,
2323  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
2326  .priv_data_size = sizeof(MLPEncodeContext),
2327  .init = mlp_encode_init,
2329  .close = mlp_encode_close,
2330  .p.priv_class = &mlp_class,
2331  .p.sample_fmts = (const enum AVSampleFormat[]) {AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P, AV_SAMPLE_FMT_NONE},
2332  .p.supported_samplerates = (const int[]) {44100, 48000, 88200, 96000, 176400, 192000, 0},
2333  .p.ch_layouts = (const AVChannelLayout[]) {
2343  { 0 }
2344  },
2345  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
2346 };
2347 #endif
ChannelInformation::summary_info
uint8_t summary_info
Definition: mlp.h:118
MLPSubstream::major_cur_subblock_index
unsigned int major_cur_subblock_index
Definition: mlpenc.c:128
clear_decoding_params
static void clear_decoding_params(DecodingParams *decoding_params)
Clears a DecodingParams struct the way it should be after a restart header.
Definition: mlpenc.c:405
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1083
default_decoding_params
static void default_decoding_params(MLPEncodeContext *ctx, DecodingParams *dp)
Sets default vales in our encoder for a DecodingParams struct.
Definition: mlpenc.c:435
compare_matrix_params
static int compare_matrix_params(MLPEncodeContext *ctx, MLPSubstream *s, const MatrixParams *prev, const MatrixParams *mp)
Compare two primitive matrices and returns 1 if anything has changed.
Definition: mlpenc.c:254
MLPEncodeContext::thd_substream_info
int thd_substream_info
Definition: mlpenc.c:160
AV_CH_LAYOUT_5POINT0_BACK
#define AV_CH_LAYOUT_5POINT0_BACK
Definition: channel_layout.h:229
MLPEncodeContext::coded_sample_fmt
int coded_sample_fmt[2]
sample format encoded for MLP
Definition: mlpenc.c:152
av_clip
#define av_clip
Definition: common.h:100
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:43
MLP_MIN_LPC_ORDER
#define MLP_MIN_LPC_ORDER
Definition: mlpenc.c:47
DecodingParams::max_order
uint8_t max_order[MAX_CHANNELS]
Definition: mlpenc.c:89
ChannelParams::codebook
uint8_t codebook
Which VLC codebook to use to read residuals.
Definition: mlp.h:103
MLPEncodeContext::avctx
AVCodecContext * avctx
Definition: mlpenc.c:135
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
put_bits32
static void av_unused put_bits32(PutBitContext *s, uint32_t value)
Write exactly 32 bits into a bitstream.
Definition: put_bits.h:291
mlp_encode_init_static
static av_cold void mlp_encode_init_static(void)
Definition: mlpenc.c:474
xor_32_to_8
static uint8_t xor_32_to_8(uint32_t value)
XOR four bytes into one.
Definition: mlp.h:172
restart_channel_params
static ChannelParams restart_channel_params[MAX_CHANNELS]
Definition: mlpenc.c:206
ff_af_queue_remove
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
Definition: audio_frame_queue.c:75
AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_STEREO
Definition: channel_layout.h:393
ff_ctz
#define ff_ctz
Definition: intmath.h:107
put_bytes_output
static int put_bytes_output(const PutBitContext *s)
Definition: put_bits.h:89
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1056
MLPBlock::major_params_changed
int major_params_changed
params_changed to be written to bitstream.
Definition: mlpenc.c:120
MLPEncodeContext::afq
AudioFrameQueue afq
Definition: mlpenc.c:195
AV_CHANNEL_LAYOUT_4POINT1
#define AV_CHANNEL_LAYOUT_4POINT1
Definition: channel_layout.h:399
generate_2_noise_channels
static void generate_2_noise_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Generates two noise channels worth of data.
Definition: mlpenc.c:1856
number_sbits
static int number_sbits(int32_t n)
Calculates the smallest number of bits it takes to encode a given signed value in two's complement.
Definition: mlpenc.c:458
ff_af_queue_close
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
Definition: audio_frame_queue.c:36
MLPEncodeContext::rematrix_precision
int rematrix_precision
Definition: mlpenc.c:141
write_major_sync
static void write_major_sync(MLPEncodeContext *ctx, uint8_t *buf, int buf_size)
Writes a major sync header to the bitstream.
Definition: mlpenc.c:673
mlp_encode_close
static av_cold int mlp_encode_close(AVCodecContext *avctx)
Definition: mlpenc.c:2264
thread.h
codebook_bits_offset
static void codebook_bits_offset(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int codebook, int32_t sample_min, int32_t sample_max, int32_t offset, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using a given codebook and a given o...
Definition: mlpenc.c:1601
DecodingParams::blocksize
uint16_t blocksize
number of PCM samples in current audio block
Definition: mlpenc.c:86
SAMPLE_MAX
#define SAMPLE_MAX(bitdepth)
Definition: mlpenc.c:1765
MLPEncodeContext::coded_peak_bitrate
int coded_peak_bitrate
peak bitrate for this major sync header
Definition: mlpenc.c:154
AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_MONO
Definition: channel_layout.h:217
ff_lpc_calc_coefs
int ff_lpc_calc_coefs(LPCContext *s, const int32_t *samples, int blocksize, int min_order, int max_order, int precision, int32_t coefs[][MAX_LPC_ORDER], int *shift, enum FFLPCType lpc_type, int lpc_passes, int omethod, int min_shift, int max_shift, int zero_shift)
Calculate LPC coefficients for multiple orders.
Definition: lpc.c:238
int64_t
long long int64_t
Definition: coverity.c:34
put_sbits
static void put_sbits(PutBitContext *pb, int n, int32_t value)
Definition: put_bits.h:281
init_put_bits
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:62
ff_af_queue_init
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
Definition: audio_frame_queue.c:28
ff_lpc_init
av_cold int ff_lpc_init(LPCContext *s, int blocksize, int max_order, enum FFLPCType lpc_type)
Initialize LPCContext.
Definition: lpc.c:340
PARAM_HUFFOFFSET
#define PARAM_HUFFOFFSET
Definition: mlp.h:79
OFFSET
#define OFFSET(x)
Definition: mlpenc.c:2275
ff_clz
#define ff_clz
Definition: intmath.h:143
mask
int mask
Definition: mediacodecdec_common.c:154
mp
static double mp(int i, double w0, double r)
Definition: af_atilt.c:60
SUBSTREAM_INFO_HIGH_RATE
#define SUBSTREAM_INFO_HIGH_RATE
Definition: mlpenc.c:219
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
put_bits
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:223
FF_LPC_TYPE_CHOLESKY
@ FF_LPC_TYPE_CHOLESKY
Cholesky factorization.
Definition: lpc.h:47
AVPacket::data
uint8_t * data
Definition: packet.h:539
restart_best_offset
static const BestOffset restart_best_offset[NUM_CODEBOOKS]
Definition: mlpenc.c:208
write_frame_headers
static void write_frame_headers(MLPEncodeContext *ctx, uint8_t *frame_header, uint8_t *substream_headers, unsigned int length, int restart_frame, uint16_t substream_data_len[MAX_SUBSTREAMS])
Writes the access unit and substream headers to the bitstream.
Definition: mlpenc.c:1096
clear_path_counter
static void clear_path_counter(PathCounter *path_counter)
Definition: mlpenc.c:1950
MLPEncodeContext::number_of_subblocks
unsigned int number_of_subblocks
Definition: mlpenc.c:199
ff_mlp_calculate_parity
uint8_t ff_mlp_calculate_parity(const uint8_t *buf, unsigned int buf_size)
XOR together all the bytes of a buffer.
Definition: mlp.c:124
AVOption
AVOption.
Definition: opt.h:429
encode.h
b
#define b
Definition: input.c:41
MLPEncodeContext::number_of_frames
unsigned int number_of_frames
Definition: mlpenc.c:198
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
data
const char data[16]
Definition: mxf.c:149
FFCodec
Definition: codec_internal.h:127
no_codebook_bits_offset
static void no_codebook_bits_offset(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int32_t offset, int32_t min, int32_t max, BestOffset *bo)
Determines the amount of bits needed to encode the samples using no codebooks and a specified offset.
Definition: mlpenc.c:1535
filter
void(* filter)(uint8_t *src, int stride, int qscale)
Definition: h263dsp.c:29
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
MLPSubstream::restart_header
RestartHeader restart_header
Definition: mlpenc.c:125
max
#define max(a, b)
Definition: cuda_runtime.h:33
ff_mlp_ch_info
const ChannelInformation ff_mlp_ch_info[21]
Tables defining channel information.
Definition: mlp.c:46
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
lpc.h
MLPEncodeContext::ch8_presentation_mod
uint8_t ch8_presentation_mod
channel modifier for TrueHD stream 2
Definition: mlpenc.c:189
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:327
write_decoding_params
static void write_decoding_params(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, int params_changed, unsigned int subblock_index)
Writes decoding parameters to the bitstream.
Definition: mlpenc.c:856
MLPBlock::decoding_params
DecodingParams decoding_params
Definition: mlpenc.c:114
no_codebook_bits
static void no_codebook_bits(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int32_t min, int32_t max, BestOffset *bo)
Determines the least amount of bits needed to encode the samples using no codebooks.
Definition: mlpenc.c:1564
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:490
BestOffset
Definition: mlpenc.c:97
MLPEncodeContext::fs
int fs
Definition: mlpenc.c:161
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
MatrixParams::noise_shift
int8_t noise_shift[MAX_CHANNELS]
Definition: mlpenc.c:77
PARAM_PRESENCE_FLAGS
#define PARAM_PRESENCE_FLAGS
Definition: mlpenc.c:83
PARAM_MATRIX
#define PARAM_MATRIX
Definition: mlp.h:74
write_matrix_params
static void write_matrix_params(MLPEncodeContext *ctx, MLPSubstream *s, DecodingParams *dp, PutBitContext *pb)
Writes matrix params for all primitive matrices to the bitstream.
Definition: mlpenc.c:792
bit
#define bit(string, value)
Definition: cbs_mpeg2.c:56
codebook_extremes
static const int8_t codebook_extremes[3][2]
Min and max values that can be encoded with each codebook.
Definition: mlpenc.c:1528
crc.h
BITS_20
@ BITS_20
Definition: mlpenc.c:465
MLPBlock::lossless_check_data
int32_t lossless_check_data
Definition: mlpenc.c:115
MLPEncodeContext::output_timing
uint16_t output_timing
Timestamp of current access unit.
Definition: mlpenc.c:178
LPCContext
Definition: lpc.h:51
BITS_24
@ BITS_24
Definition: mlpenc.c:466
ff_mlp_checksum16
uint16_t ff_mlp_checksum16(const uint8_t *buf, unsigned int buf_size)
Definition: mlp.c:89
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ChannelParams::filter_params
FilterParams filter_params[NUM_FILTERS]
Definition: mlp.h:98
ff_mlp_checksum8
uint8_t ff_mlp_checksum8(const uint8_t *buf, unsigned int buf_size)
MLP uses checksums that seem to be based on the standard CRC algorithm, but are not (in implementatio...
Definition: mlp.c:98
MLPEncodeContext::lpc_passes
int lpc_passes
Definition: mlpenc.c:143
MLPEncodeContext::min_restart_interval
int min_restart_interval
Min interval of access units in between two major frames.
Definition: mlpenc.c:138
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1071
MAX_MATRICES
#define MAX_MATRICES
Definition: mlp.h:46
ChannelParams::huff_lsbs
uint8_t huff_lsbs
Size of residual suffix not encoded using VLC.
Definition: mlp.h:104
mlp_encode_frame
static int mlp_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
Definition: mlpenc.c:2151
MLPEncodeContext::lpc_ctx
LPCContext lpc_ctx
Definition: mlpenc.c:203
SYNC_MAJOR
#define SYNC_MAJOR
Definition: mlpenc.c:210
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: packet.c:113
MLPEncodeContext::major_number_of_frames
unsigned int major_number_of_frames
Definition: mlpenc.c:168
MLP_MAX_LPC_ORDER
#define MLP_MAX_LPC_ORDER
Definition: mlpenc.c:48
AV_CHANNEL_LAYOUT_2POINT1
#define AV_CHANNEL_LAYOUT_2POINT1
Definition: channel_layout.h:394
audio_frame_queue.h
samplefmt.h
apply_filters
static void apply_filters(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:1843
MLPEncodeContext::lpc_coeff_precision
int lpc_coeff_precision
Definition: mlpenc.c:140
clear_channel_params
static void clear_channel_params(ChannelParams *channel_params, int nb_channels)
Clears a ChannelParams struct the way it should be after a restart header.
Definition: mlpenc.c:420
SYNC_TRUEHD
#define SYNC_TRUEHD
Definition: mlp.h:30
determine_output_shift
static void determine_output_shift(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:1266
analyze_sample_buffer
static void analyze_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:2103
MLPBlock::inout_buffer
int32_t inout_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
Definition: mlpenc.c:121
MLPEncodeContext::next_major_frame_size
unsigned int next_major_frame_size
Counter of number of samples for next major frame.
Definition: mlpenc.c:172
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
MLPBlock::major_decoding_params
DecodingParams major_decoding_params
DecodingParams to be written to bitstream.
Definition: mlpenc.c:119
MatrixParams::coeff
int32_t coeff[MAX_MATRICES][MAX_NCHANNELS]
decoding coefficients
Definition: mlpenc.c:74
ff_af_queue_add
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
Definition: audio_frame_queue.c:44
MLPEncodeContext::substream_info
int substream_info
Definition: mlpenc.c:159
AV_CH_LAYOUT_STEREO
#define AV_CH_LAYOUT_STEREO
Definition: channel_layout.h:218
DecodingParams::output_shift
int8_t output_shift[MAX_CHANNELS]
Left shift to apply to decoded PCM values to get final 24-bit output.
Definition: mlpenc.c:88
AV_CHANNEL_LAYOUT_SURROUND
#define AV_CHANNEL_LAYOUT_SURROUND
Definition: channel_layout.h:396
MLPBlock::major_channel_params
ChannelParams major_channel_params[MAX_CHANNELS]
ChannelParams to be written to bitstream.
Definition: mlpenc.c:118
AV_CH_LAYOUT_QUAD
#define AV_CH_LAYOUT_QUAD
Definition: channel_layout.h:226
RestartHeader::max_output_bits
uint8_t max_output_bits
largest output bit-depth
Definition: mlpenc.c:66
avassert.h
END_OF_STREAM
#define END_OF_STREAM
Definition: libxavs.c:37
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
SUBSTREAM_INFO_MAX_2_CHAN
#define SUBSTREAM_INFO_MAX_2_CHAN
Definition: mlpenc.c:218
FIR
#define FIR
Definition: mlp.h:82
best_codebook_path_cost
static uint32_t best_codebook_path_cost(MLPEncodeContext *ctx, MLPSubstream *s, int channel, PathCounter *src, int cur_codebook)
Definition: mlpenc.c:1960
AV_CODEC_CAP_EXPERIMENTAL
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: codec.h:102
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
MLPEncodeContext::summary_info
int summary_info
Definition: mlpenc.c:164
BestOffset::lsb_bits
uint8_t lsb_bits
Definition: mlpenc.c:100
RestartHeader::min_channel
uint8_t min_channel
The index of the first channel coded in this substream.
Definition: mlpenc.c:53
AV_CHANNEL_LAYOUT_4POINT0
#define AV_CHANNEL_LAYOUT_4POINT0
Definition: channel_layout.h:398
estimate_coeff
static int estimate_coeff(MLPEncodeContext *ctx, MLPSubstream *s, MatrixParams *mp, int ch0, int ch1)
Definition: mlpenc.c:1410
lossless_matrix_coeffs
static void lossless_matrix_coeffs(MLPEncodeContext *ctx, MLPSubstream *s)
Determines best coefficients to use for the lossless matrix.
Definition: mlpenc.c:1506
MLPBlock::max_output_bits
unsigned int max_output_bits
largest output bit-depth
Definition: mlpenc.c:116
s
#define s(width, name)
Definition: cbs_vp9.c:198
PARAM_IIR
#define PARAM_IIR
Definition: mlp.h:78
copy_matrix_params
static void copy_matrix_params(MatrixParams *dst, MatrixParams *src)
Definition: mlpenc.c:362
MLPEncodeContext::max_restart_interval
int max_restart_interval
Max interval of access units in between two major frames.
Definition: mlpenc.c:137
write_access_unit
static int write_access_unit(MLPEncodeContext *ctx, uint8_t *buf, int buf_size, int restart_frame)
Writes an entire access unit to the bitstream.
Definition: mlpenc.c:1137
set_best_codebook
static void set_best_codebook(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:1980
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1451
MLPEncodeContext
Definition: mlpenc.c:133
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
MLPEncodeContext::num_channels
int num_channels
Number of channels in major_scratch_buffer.
Definition: mlpenc.c:149
bits
uint8_t bits
Definition: vp3data.h:128
MAX_NCHANNELS
#define MAX_NCHANNELS
MLP encoder Copyright (c) 2008 Ramiro Polla Copyright (c) 2016-2019 Jai Luthra.
Definition: mlpenc.c:42
codebook_bits
static void codebook_bits(MLPEncodeContext *ctx, DecodingParams *dp, int channel, int codebook, int offset, int32_t min, int32_t max, BestOffset *bo, int direction)
Determines the least amount of bits needed to encode the samples using a given codebook.
Definition: mlpenc.c:1663
AudioFrameQueue
Definition: audio_frame_queue.h:32
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1585
copy_restart_frame_params
static void copy_restart_frame_params(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:380
ctx
AVFormatContext * ctx
Definition: movenc.c:49
channels
channels
Definition: aptx.h:31
mlp_peak_bitrate
static int mlp_peak_bitrate(int peak_bitrate, int sample_rate)
Definition: mlpenc.c:469
PutBitContext
Definition: put_bits.h:50
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
AV_CH_LAYOUT_2_1
#define AV_CH_LAYOUT_2_1
Definition: channel_layout.h:220
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
MLPEncodeContext::multichannel_type8ch
uint8_t multichannel_type8ch
channel modifier for TrueHD stream 0
Definition: mlpenc.c:186
MLPEncodeContext::flags
int flags
major sync info flags
Definition: mlpenc.c:156
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
code_filter_coeffs
static void code_filter_coeffs(MLPEncodeContext *ctx, FilterParams *fp, const int32_t *fcoeff)
Determines the smallest number of bits needed to encode the filter coefficients, and if it's possible...
Definition: mlpenc.c:1331
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
MLPEncodeContext::last_frames
int32_t last_frames
Signal last frames.
Definition: mlpenc.c:166
PutBitContext::buf
uint8_t * buf
Definition: put_bits.h:53
NULL
#define NULL
Definition: coverity.c:32
av_clip_intp2
#define av_clip_intp2
Definition: common.h:121
BestOffset::max
int32_t max
Definition: mlpenc.c:102
AV_WB16
#define AV_WB16(p, v)
Definition: intreadwrite.h:401
MLPEncodeContext::noise_type
uint8_t noise_type
Definition: mlpenc.c:181
MLPEncodeContext::major_frame_size
unsigned int major_frame_size
Number of samples in current major frame being encoded.
Definition: mlpenc.c:171
MLPEncodeContext::num_substreams
int num_substreams
Number of substreams contained within this stream.
Definition: mlpenc.c:147
DecodingParams::sample_buffer
int32_t sample_buffer[MAX_NCHANNELS][MAX_BLOCKSIZE]
Definition: mlpenc.c:94
MatrixParams
Definition: mlpenc.c:69
RestartHeader::lossless_check_data
int32_t lossless_check_data
XOR of all output samples.
Definition: mlpenc.c:63
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
MLPEncodeContext::cur_restart_interval
int cur_restart_interval
Definition: mlpenc.c:139
ff_lpc_end
av_cold void ff_lpc_end(LPCContext *s)
Uninitialize LPCContext.
Definition: lpc.c:365
AV_CH_LAYOUT_5POINT1
#define AV_CH_LAYOUT_5POINT1
Definition: channel_layout.h:228
copy_filter_params
static void copy_filter_params(ChannelParams *dst_cp, ChannelParams *src_cp, int filter)
Definition: mlpenc.c:344
MatrixParams::fbits
uint8_t fbits[MAX_MATRICES]
fraction bits
Definition: mlpenc.c:75
ORDER_METHOD_SEARCH
#define ORDER_METHOD_SEARCH
Definition: lpc.h:33
ChannelParams::coeff
int32_t coeff[NUM_FILTERS][MAX_FIR_ORDER]
Definition: mlp.h:99
set_major_params
static void set_major_params(MLPEncodeContext *ctx, MLPSubstream *s)
Analyzes all collected bitcounts and selects the best parameters for each individual access unit.
Definition: mlpenc.c:2061
MLPEncodeContext::shorten_by
int shorten_by
Definition: mlpenc.c:201
seed
static unsigned int seed
Definition: videogen.c:78
AVOnce
#define AVOnce
Definition: thread.h:202
MLPEncodeContext::lpc_type
int lpc_type
Definition: mlpenc.c:142
index
int index
Definition: gxfenc.c:90
MLPSubstream::b
MLPBlock b[MAX_HEADER_INTERVAL+1]
Definition: mlpenc.c:127
MLPEncodeContext::coded_sample_rate
int coded_sample_rate[2]
sample rate encoded for MLP
Definition: mlpenc.c:153
FilterParams::coeff_shift
int coeff_shift
Definition: mlp.h:93
write_substr
static uint8_t * write_substr(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t *buf, int buf_size, int restart_frame, uint16_t *substream_data_len)
Writes the substream data to the bitstream.
Definition: mlpenc.c:1011
FilterParams
filter data
Definition: mlp.h:86
layout_truehd
static int layout_truehd(uint64_t layout)
Definition: mlp_parse.h:116
MLPEncodeContext::channel_arrangement
uint8_t channel_arrangement
channel arrangement for MLP streams
Definition: mlpenc.c:182
ff_mlp_restart_checksum
uint8_t ff_mlp_restart_checksum(const uint8_t *buf, unsigned int bit_size)
Calculate an 8-bit checksum over a restart header – a non-multiple-of-8 number of bits,...
Definition: mlp.c:105
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
InputBitDepth
InputBitDepth
Definition: mlpenc.c:463
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:317
codec_internal.h
FilterParams::coeff_bits
int coeff_bits
Definition: mlp.h:92
shift
static int shift(int a, int b)
Definition: bonk.c:261
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
MLPBlock::channel_params
ChannelParams channel_params[MAX_CHANNELS]
Definition: mlpenc.c:113
MAX_LPC_ORDER
#define MAX_LPC_ORDER
Definition: lpc.h:37
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1063
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
MAX_SUBSTREAMS
#define MAX_SUBSTREAMS
Maximum number of substreams that can be decoded.
Definition: mlp.h:51
parity
mcdeint parity
Definition: vf_mcdeint.c:281
ff_truehd_encoder
const FFCodec ff_truehd_encoder
NUM_FILTERS
#define NUM_FILTERS
number of allowed filters
Definition: mlp.h:64
FilterParams::order
uint8_t order
number of taps in filter
Definition: mlp.h:87
BITS_16
@ BITS_16
Definition: mlpenc.c:464
PathCounter::bitcount
uint32_t bitcount
Definition: mlpenc.c:1947
FLAGS
#define FLAGS
Definition: mlpenc.c:2274
AV_WL16
#define AV_WL16(p, v)
Definition: intreadwrite.h:408
DecodingParams::quant_step_size
uint8_t quant_step_size[MAX_CHANNELS]
left shift to apply to Huffman-decoded residuals
Definition: mlpenc.c:87
RestartHeader::max_shift
int8_t max_shift
Definition: mlpenc.c:57
rematrix_channels
static void rematrix_channels(MLPEncodeContext *ctx, MLPSubstream *s)
Rematrixes all channels using chosen coefficients.
Definition: mlpenc.c:1879
AV_CH_LAYOUT_5POINT1_BACK
#define AV_CH_LAYOUT_5POINT1_BACK
Definition: channel_layout.h:230
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
BestOffset::offset
int32_t offset
Definition: mlpenc.c:98
ChannelParams::huff_offset
int16_t huff_offset
Offset to apply to residual values.
Definition: mlp.h:101
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:545
BestOffset::min
int32_t min
Definition: mlpenc.c:101
ORDER_METHOD_EST
#define ORDER_METHOD_EST
Definition: lpc.h:29
MAX_CHANNELS
#define MAX_CHANNELS
Definition: aac.h:33
MAX_FIR_ORDER
#define MAX_FIR_ORDER
The maximum number of taps in IIR and FIR filters.
Definition: mlp.h:67
MLPEncodeContext::channel_arrangement8
uint16_t channel_arrangement8
8 channel arrangement for THD streams
Definition: mlpenc.c:183
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
AV_CH_LAYOUT_3POINT1
#define AV_CH_LAYOUT_3POINT1
Definition: channel_layout.h:222
compare_filter_params
static int compare_filter_params(const ChannelParams *prev_cp, const ChannelParams *cp, int filter)
Compares two FilterParams structures and returns 1 if anything has changed.
Definition: mlpenc.c:230
MatrixParams::lsb_bypass
uint8_t lsb_bypass[MAX_MATRICES]
Definition: mlpenc.c:78
AV_CH_LAYOUT_5POINT0
#define AV_CH_LAYOUT_5POINT0
Definition: channel_layout.h:227
MLPBlock::best_offset
BestOffset best_offset[MAX_CHANNELS][NUM_CODEBOOKS]
Definition: mlpenc.c:117
FLAGS_DVDA
#define FLAGS_DVDA
Definition: mlpenc.c:214
determine_bits
static void determine_bits(MLPEncodeContext *ctx, MLPSubstream *s)
Determines the least amount of bits needed to encode the samples using any or no codebook.
Definition: mlpenc.c:1704
MLPEncodeContext::restart_intervals
unsigned int restart_intervals
Number of possible major frame sizes.
Definition: mlpenc.c:176
mlp_parse.h
MatrixParams::outch
uint8_t outch[MAX_MATRICES]
output channel for each matrix
Definition: mlpenc.c:72
MLPEncodeContext::max_codebook_search
int max_codebook_search
Definition: mlpenc.c:145
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
put_bits_count
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:80
AV_CH_LAYOUT_2POINT1
#define AV_CH_LAYOUT_2POINT1
Definition: channel_layout.h:219
BestOffset::bitcount
uint32_t bitcount
Definition: mlpenc.c:99
SUBSTREAM_INFO_ALWAYS_SET
#define SUBSTREAM_INFO_ALWAYS_SET
Definition: mlpenc.c:220
apply_filter
static int apply_filter(MLPEncodeContext *ctx, MLPSubstream *s, int channel)
Applies the filter to the current samples, and saves the residual back into the samples buffer.
Definition: mlpenc.c:1775
restart_decoding_params
static DecodingParams restart_decoding_params[MAX_SUBSTREAMS]
Definition: mlpenc.c:207
RestartHeader::data_check_present
uint8_t data_check_present
Set if the substream contains extra info to check the size of VLC blocks.
Definition: mlpenc.c:61
PARAMS_DEFAULT
#define PARAMS_DEFAULT
Definition: mlpenc.c:82
PARAM_PRESENCE
#define PARAM_PRESENCE
Definition: mlp.h:80
compare_decoding_params
static int compare_decoding_params(MLPEncodeContext *ctx, MLPSubstream *s, unsigned int index)
Compares two DecodingParams and ChannelParams structures to decide if a new decoding params header ha...
Definition: mlpenc.c:289
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
AV_CH_LAYOUT_4POINT1
#define AV_CH_LAYOUT_4POINT1
Definition: channel_layout.h:224
determine_quant_step_size
static void determine_quant_step_size(MLPEncodeContext *ctx, MLPSubstream *s)
Determines how many bits are zero at the end of all samples so they can be shifted out.
Definition: mlpenc.c:1304
AV_CHANNEL_LAYOUT_3POINT1
#define AV_CHANNEL_LAYOUT_3POINT1
Definition: channel_layout.h:397
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
MLPEncodeContext::input_timing
uint16_t input_timing
Decoding timestamp of current access unit.
Definition: mlpenc.c:179
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ff_mlp_ch_layouts
const AVChannelLayout ff_mlp_ch_layouts[12]
Definition: mlp.c:60
ChannelParams
sample data coding information
Definition: mlp.h:97
MAX_BLOCKSIZE
#define MAX_BLOCKSIZE
Definition: diracdec.c:56
MLPEncodeContext::lpc_sample_buffer
int32_t lpc_sample_buffer[MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
Definition: mlpenc.c:193
code_matrix_coeffs
static void code_matrix_coeffs(MLPEncodeContext *ctx, MLPSubstream *s, DecodingParams *dp, unsigned int mat)
Determines how many fractional bits are needed to encode matrix coefficients.
Definition: mlpenc.c:1491
ff_mlp_init_crc
av_cold void ff_mlp_init_crc(void)
Definition: mlp.c:83
MLPEncodeContext::wordlength
int wordlength
Definition: mlpenc.c:162
MLPEncodeContext::ch6_presentation_mod
uint8_t ch6_presentation_mod
channel modifier for TrueHD stream 1
Definition: mlpenc.c:188
MLPBlock::seq_size
unsigned int seq_size
Definition: mlpenc.c:112
MAJOR_SYNC_INFO_SIGNATURE
#define MAJOR_SYNC_INFO_SIGNATURE
Definition: mlpenc.c:211
PathCounter::path
char path[MAX_HEADER_INTERVAL+2]
Definition: mlpenc.c:1945
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2041
NUM_CODEBOOKS
#define NUM_CODEBOOKS
Number of possible codebooks (counting "no codebooks")
Definition: mlpenc.c:109
RestartHeader::max_huff_lsbs
uint8_t max_huff_lsbs
largest huff_lsbs
Definition: mlpenc.c:65
ret
ret
Definition: filter_design.txt:187
number_trailing_zeroes
static int number_trailing_zeroes(int32_t sample, unsigned int max, unsigned int def)
Counts the number of trailing zeroes in a value.
Definition: mlpenc.c:1261
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:80
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
RestartHeader::max_matrix_channel
uint8_t max_matrix_channel
The number of channels input into the rematrix stage.
Definition: mlpenc.c:55
AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_SURROUND
Definition: channel_layout.h:221
ChannelInformation::channel_occupancy
uint8_t channel_occupancy
Definition: mlp.h:115
RestartHeader::noise_shift
uint8_t noise_shift
The left shift applied to random noise in 0x31ea substreams.
Definition: mlpenc.c:58
SYNC_MLP
#define SYNC_MLP
Definition: mlp.h:29
input_to_sample_buffer
static void input_to_sample_buffer(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:1229
U
#define U(x)
Definition: vpx_arith.h:37
compare_best_offset
static int compare_best_offset(const BestOffset *prev, const BestOffset *cur)
Definition: mlpenc.c:1955
AVCodecContext
main external API structure.
Definition: avcodec.h:451
HUFF_OFFSET_MAX
#define HUFF_OFFSET_MAX
Definition: mlpenc.c:106
frame_header
Definition: truemotion1.c:88
channel_layout.h
av_channel_layout_subset
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
Definition: channel_layout.c:863
ff_mlp_encoder
const FFCodec ff_mlp_encoder
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
MLPEncodeContext::ch2_presentation_mod
uint8_t ch2_presentation_mod
channel modifier for TrueHD stream 0
Definition: mlpenc.c:187
MLPEncodeContext::next_major_number_of_frames
unsigned int next_major_number_of_frames
Definition: mlpenc.c:169
PARAM_QUANTSTEP
#define PARAM_QUANTSTEP
Definition: mlp.h:76
DecodingParams::matrix_params
MatrixParams matrix_params
Definition: mlpenc.c:91
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
HUFF_OFFSET_MIN
#define HUFF_OFFSET_MIN
Definition: mlpenc.c:105
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
SAMPLE_MIN
#define SAMPLE_MIN(bitdepth)
Definition: mlpenc.c:1766
IIR
#define IIR
Definition: mlp.h:83
RestartHeader::noisegen_seed
uint32_t noisegen_seed
The current seed value for the pseudorandom noise generator(s).
Definition: mlpenc.c:59
set_filter
static void set_filter(MLPEncodeContext *ctx, MLPSubstream *s, int channel, int retry_filter)
Determines the best filter parameters for the given data and writes the necessary information to the ...
Definition: mlpenc.c:1353
PARAM_BLOCKSIZE
#define PARAM_BLOCKSIZE
Definition: mlp.h:73
MatrixParams::bypassed_lsbs
int8_t bypassed_lsbs[MAX_MATRICES][MAX_BLOCKSIZE]
Definition: mlpenc.c:79
MLPEncodeContext::frame_index
unsigned int frame_index
Index of current frame being encoded.
Definition: mlpenc.c:174
PARAM_FIR
#define PARAM_FIR
Definition: mlp.h:77
DecodingParams
Definition: mlpenc.c:85
input_data_internal
static void input_data_internal(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples, int is24)
Inputs data from the samples passed by lavc into the context, shifts them appropriately depending on ...
Definition: mlpenc.c:1185
RestartHeader::max_channel
uint8_t max_channel
The index of the last channel coded in this substream.
Definition: mlpenc.c:54
flush_put_bits
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:143
MSB_MASK
#define MSB_MASK(bits)
Definition: mlpenc.c:1768
MatrixParams::forco
int32_t forco[MAX_MATRICES][MAX_NCHANNELS]
forward coefficients
Definition: mlpenc.c:73
mlp.h
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:392
MIN_HEADER_INTERVAL
#define MIN_HEADER_INTERVAL
Definition: mlpenc.c:44
ff_mlp_huffman_tables
const uint8_t ff_mlp_huffman_tables[3][18][2]
Tables defining the Huffman codes.
Definition: mlp.c:30
mlp_encode_init
static av_cold int mlp_encode_init(AVCodecContext *avctx)
Definition: mlpenc.c:481
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
PARAM_OUTSHIFT
#define PARAM_OUTSHIFT
Definition: mlp.h:75
mlp_options
static const AVOption mlp_options[]
Definition: mlpenc.c:2276
PathCounter
Definition: mlpenc.c:1942
AV_CH_LAYOUT_4POINT0
#define AV_CH_LAYOUT_4POINT0
Definition: channel_layout.h:223
int32_t
int32_t
Definition: audioconvert.c:56
write_restart_header
static void write_restart_header(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb)
Writes a restart header to the bitstream.
Definition: mlpenc.c:755
CODEBOOK_CHANGE_BITS
#define CODEBOOK_CHANGE_BITS
Definition: mlpenc.c:1948
coeff
static const double coeff[2][5]
Definition: vf_owdenoise.c:80
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
process_major_frame
static void process_major_frame(MLPEncodeContext *ctx, MLPSubstream *s)
Definition: mlpenc.c:2137
MLPEncodeContext::filter_state
int32_t filter_state[NUM_FILTERS][MAX_HEADER_INTERVAL *MAX_BLOCKSIZE]
Definition: mlpenc.c:192
AV_CHANNEL_LAYOUT_5POINT0
#define AV_CHANNEL_LAYOUT_5POINT0
Definition: channel_layout.h:402
PathCounter::cur_idx
int cur_idx
Definition: mlpenc.c:1946
MLP_MIN_LPC_SHIFT
#define MLP_MIN_LPC_SHIFT
Definition: mlpenc.c:49
MLPSubstream::major_filter_state_subblock
unsigned int major_filter_state_subblock
Definition: mlpenc.c:129
avstring.h
MatrixParams::count
uint8_t count
number of matrices to apply
Definition: mlpenc.c:70
determine_filters
static void determine_filters(MLPEncodeContext *ctx, MLPSubstream *s)
Tries to determine a good prediction filter, and applies it to the samples buffer if the filter is go...
Definition: mlpenc.c:1402
input_data
static void input_data(MLPEncodeContext *ctx, MLPSubstream *s, uint8_t **const samples, int nb_samples)
Wrapper function for inputting data in two different bit-depths.
Definition: mlpenc.c:1224
MLPEncodeContext::s
MLPSubstream s[2]
Definition: mlpenc.c:191
AV_CODEC_CAP_SMALL_LAST_FRAME
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
Definition: codec.h:81
MLPSubstream
Definition: mlpenc.c:124
MLPEncodeContext::prediction_order
int prediction_order
Definition: mlpenc.c:144
AV_CHANNEL_LAYOUT_5POINT1
#define AV_CHANNEL_LAYOUT_5POINT1
Definition: channel_layout.h:403
put_bits.h
FilterParams::shift
uint8_t shift
Right shift to apply to output of filter.
Definition: mlp.h:88
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
write_block_data
static void write_block_data(MLPEncodeContext *ctx, MLPSubstream *s, PutBitContext *pb, unsigned int subblock_index)
Writes the residuals to the bitstream.
Definition: mlpenc.c:956
MLP_MAX_LPC_SHIFT
#define MLP_MAX_LPC_SHIFT
Definition: mlpenc.c:50
ff_alloc_packet
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
Definition: encode.c:62
FF_LPC_TYPE_LEVINSON
@ FF_LPC_TYPE_LEVINSON
Levinson-Durbin recursion.
Definition: lpc.h:46
DecodingParams::param_presence_flags
uint8_t param_presence_flags
Bitmask of which parameter sets are conveyed in a decoding parameter block.
Definition: mlpenc.c:93
write_filter_params
static void write_filter_params(MLPEncodeContext *ctx, ChannelParams *cp, PutBitContext *pb, int channel, unsigned int filter)
Writes filter parameters for one filter to the bitstream.
Definition: mlpenc.c:828
MAX_HEADER_INTERVAL
#define MAX_HEADER_INTERVAL
Definition: mlpenc.c:45
MLPEncodeContext::multichannel_type6ch
uint8_t multichannel_type6ch
channel modifier for TrueHD stream 0
Definition: mlpenc.c:185
MLPBlock
Definition: mlpenc.c:111
src
#define src
Definition: vp8dsp.c:248
channel
channel
Definition: ebur128.h:39
RestartHeader
Definition: mlpenc.c:52
codebook
static const unsigned codebook[256][2]
Definition: cfhdenc.c:41
AV_CODEC_ID_MLP
@ AV_CODEC_ID_MLP
Definition: codec_id.h:475
MLPSubstream::cur_restart_header
RestartHeader * cur_restart_header
Definition: mlpenc.c:126
mlp_class
static const AVClass mlp_class
Definition: mlpenc.c:2291
min
float min
Definition: vorbis_enc_data.h:429
MLPSubstream::coefs
int32_t coefs[MAX_CHANNELS][MAX_LPC_ORDER][MAX_LPC_ORDER]
Definition: mlpenc.c:130
MLPEncodeContext::channel_occupancy
int channel_occupancy
Definition: mlpenc.c:163
intmath.h