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