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