alsdec.c
Go to the documentation of this file.
1 /*
2  * MPEG-4 ALS decoder
3  * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
29 //#define DEBUG
30 
31 
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "unary.h"
35 #include "mpeg4audio.h"
36 #include "bytestream.h"
37 #include "bgmc.h"
38 #include "dsputil.h"
39 #include "internal.h"
40 #include "libavutil/samplefmt.h"
41 #include "libavutil/crc.h"
42 
43 #include <stdint.h>
44 
49 static const int8_t parcor_rice_table[3][20][2] = {
50  { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},
51  { 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3},
52  { -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2},
53  { 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} },
54  { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},
55  { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},
56  {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},
57  { 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} },
58  { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},
59  { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},
60  {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},
61  { 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} }
62 };
63 
64 
70 static const int16_t parcor_scaled_values[] = {
71  -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,
72  -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,
73  -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,
74  -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,
75  -1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32,
76  -994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32,
77  -971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32,
78  -944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32,
79  -913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32,
80  -878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32,
81  -838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32,
82  -795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32,
83  -747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32,
84  -695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32,
85  -639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32,
86  -580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32,
87  -516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32,
88  -447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32,
89  -375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32,
90  -299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32,
91  -219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32,
92  -134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32,
93  -46048 / 32, -23264 / 32, -224 / 32, 23072 / 32,
94  46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32,
95  143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32,
96  244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32,
97  349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32,
98  458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32,
99  571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32,
100  688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32,
101  810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32,
102  935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32
103 };
104 
105 
109 static const uint8_t ltp_gain_values [4][4] = {
110  { 0, 8, 16, 24},
111  {32, 40, 48, 56},
112  {64, 70, 76, 82},
113  {88, 92, 96, 100}
114 };
115 
116 
120 static const int16_t mcc_weightings[] = {
121  204, 192, 179, 166, 153, 140, 128, 115,
122  102, 89, 76, 64, 51, 38, 25, 12,
123  0, -12, -25, -38, -51, -64, -76, -89,
124  -102, -115, -128, -140, -153, -166, -179, -192
125 };
126 
127 
130 static const uint8_t tail_code[16][6] = {
131  { 74, 44, 25, 13, 7, 3},
132  { 68, 42, 24, 13, 7, 3},
133  { 58, 39, 23, 13, 7, 3},
134  {126, 70, 37, 19, 10, 5},
135  {132, 70, 37, 20, 10, 5},
136  {124, 70, 38, 20, 10, 5},
137  {120, 69, 37, 20, 11, 5},
138  {116, 67, 37, 20, 11, 5},
139  {108, 66, 36, 20, 10, 5},
140  {102, 62, 36, 20, 10, 5},
141  { 88, 58, 34, 19, 10, 5},
142  {162, 89, 49, 25, 13, 7},
143  {156, 87, 49, 26, 14, 7},
144  {150, 86, 47, 26, 14, 7},
145  {142, 84, 47, 26, 14, 7},
146  {131, 79, 46, 26, 14, 7}
147 };
148 
149 
150 enum RA_Flag {
154 };
155 
156 
157 typedef struct {
158  uint32_t samples;
160  int floating;
161  int msb_first;
164  enum RA_Flag ra_flag;
168  int max_order;
170  int bgmc;
171  int sb_part;
173  int mc_coding;
175  int chan_sort;
176  int rlslms;
178  int *chan_pos;
181 
182 
183 typedef struct {
189  int weighting[6];
191 
192 
193 typedef struct {
199  const AVCRC *crc_table;
200  uint32_t crc_org;
201  uint32_t crc;
202  unsigned int cur_frame_length;
203  unsigned int frame_id;
204  unsigned int js_switch;
205  unsigned int num_blocks;
206  unsigned int s_max;
210  int *const_block;
211  unsigned int *shift_lsbs;
212  unsigned int *opt_order;
214  int *use_ltp;
215  int *ltp_lag;
216  int **ltp_gain;
230 } ALSDecContext;
231 
232 
233 typedef struct {
234  unsigned int block_length;
235  unsigned int ra_block;
236  int *const_block;
237  int js_blocks;
238  unsigned int *shift_lsbs;
239  unsigned int *opt_order;
241  int *use_ltp;
242  int *ltp_lag;
243  int *ltp_gain;
249 } ALSBlockData;
250 
251 
253 {
254 #ifdef DEBUG
255  AVCodecContext *avctx = ctx->avctx;
256  ALSSpecificConfig *sconf = &ctx->sconf;
257 
258  av_dlog(avctx, "resolution = %i\n", sconf->resolution);
259  av_dlog(avctx, "floating = %i\n", sconf->floating);
260  av_dlog(avctx, "frame_length = %i\n", sconf->frame_length);
261  av_dlog(avctx, "ra_distance = %i\n", sconf->ra_distance);
262  av_dlog(avctx, "ra_flag = %i\n", sconf->ra_flag);
263  av_dlog(avctx, "adapt_order = %i\n", sconf->adapt_order);
264  av_dlog(avctx, "coef_table = %i\n", sconf->coef_table);
265  av_dlog(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
266  av_dlog(avctx, "max_order = %i\n", sconf->max_order);
267  av_dlog(avctx, "block_switching = %i\n", sconf->block_switching);
268  av_dlog(avctx, "bgmc = %i\n", sconf->bgmc);
269  av_dlog(avctx, "sb_part = %i\n", sconf->sb_part);
270  av_dlog(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
271  av_dlog(avctx, "mc_coding = %i\n", sconf->mc_coding);
272  av_dlog(avctx, "chan_config = %i\n", sconf->chan_config);
273  av_dlog(avctx, "chan_sort = %i\n", sconf->chan_sort);
274  av_dlog(avctx, "RLSLMS = %i\n", sconf->rlslms);
275  av_dlog(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
276 #endif
277 }
278 
279 
283 {
284  GetBitContext gb;
285  uint64_t ht_size;
286  int i, config_offset;
287  MPEG4AudioConfig m4ac;
288  ALSSpecificConfig *sconf = &ctx->sconf;
289  AVCodecContext *avctx = ctx->avctx;
290  uint32_t als_id, header_size, trailer_size;
291 
292  init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
293 
294  config_offset = avpriv_mpeg4audio_get_config(&m4ac, avctx->extradata,
295  avctx->extradata_size * 8, 1);
296 
297  if (config_offset < 0)
298  return AVERROR_INVALIDDATA;
299 
300  skip_bits_long(&gb, config_offset);
301 
302  if (get_bits_left(&gb) < (30 << 3))
303  return AVERROR_INVALIDDATA;
304 
305  // read the fixed items
306  als_id = get_bits_long(&gb, 32);
307  avctx->sample_rate = m4ac.sample_rate;
308  skip_bits_long(&gb, 32); // sample rate already known
309  sconf->samples = get_bits_long(&gb, 32);
310  avctx->channels = m4ac.channels;
311  skip_bits(&gb, 16); // number of channels already knwon
312  skip_bits(&gb, 3); // skip file_type
313  sconf->resolution = get_bits(&gb, 3);
314  sconf->floating = get_bits1(&gb);
315  sconf->msb_first = get_bits1(&gb);
316  sconf->frame_length = get_bits(&gb, 16) + 1;
317  sconf->ra_distance = get_bits(&gb, 8);
318  sconf->ra_flag = get_bits(&gb, 2);
319  sconf->adapt_order = get_bits1(&gb);
320  sconf->coef_table = get_bits(&gb, 2);
321  sconf->long_term_prediction = get_bits1(&gb);
322  sconf->max_order = get_bits(&gb, 10);
323  sconf->block_switching = get_bits(&gb, 2);
324  sconf->bgmc = get_bits1(&gb);
325  sconf->sb_part = get_bits1(&gb);
326  sconf->joint_stereo = get_bits1(&gb);
327  sconf->mc_coding = get_bits1(&gb);
328  sconf->chan_config = get_bits1(&gb);
329  sconf->chan_sort = get_bits1(&gb);
330  sconf->crc_enabled = get_bits1(&gb);
331  sconf->rlslms = get_bits1(&gb);
332  skip_bits(&gb, 5); // skip 5 reserved bits
333  skip_bits1(&gb); // skip aux_data_enabled
334 
335 
336  // check for ALSSpecificConfig struct
337  if (als_id != MKBETAG('A','L','S','\0'))
338  return AVERROR_INVALIDDATA;
339 
340  ctx->cur_frame_length = sconf->frame_length;
341 
342  // read channel config
343  if (sconf->chan_config)
344  sconf->chan_config_info = get_bits(&gb, 16);
345  // TODO: use this to set avctx->channel_layout
346 
347 
348  // read channel sorting
349  if (sconf->chan_sort && avctx->channels > 1) {
350  int chan_pos_bits = av_ceil_log2(avctx->channels);
351  int bits_needed = avctx->channels * chan_pos_bits + 7;
352  if (get_bits_left(&gb) < bits_needed)
353  return AVERROR_INVALIDDATA;
354 
355  if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
356  return AVERROR(ENOMEM);
357 
358  for (i = 0; i < avctx->channels; i++)
359  sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
360 
361  align_get_bits(&gb);
362  // TODO: use this to actually do channel sorting
363  } else {
364  sconf->chan_sort = 0;
365  }
366 
367 
368  // read fixed header and trailer sizes,
369  // if size = 0xFFFFFFFF then there is no data field!
370  if (get_bits_left(&gb) < 64)
371  return AVERROR_INVALIDDATA;
372 
373  header_size = get_bits_long(&gb, 32);
374  trailer_size = get_bits_long(&gb, 32);
375  if (header_size == 0xFFFFFFFF)
376  header_size = 0;
377  if (trailer_size == 0xFFFFFFFF)
378  trailer_size = 0;
379 
380  ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;
381 
382 
383  // skip the header and trailer data
384  if (get_bits_left(&gb) < ht_size)
385  return AVERROR_INVALIDDATA;
386 
387  if (ht_size > INT32_MAX)
388  return AVERROR_PATCHWELCOME;
389 
390  skip_bits_long(&gb, ht_size);
391 
392 
393  // initialize CRC calculation
394  if (sconf->crc_enabled) {
395  if (get_bits_left(&gb) < 32)
396  return AVERROR_INVALIDDATA;
397 
398  if (avctx->err_recognition & AV_EF_CRCCHECK) {
400  ctx->crc = 0xFFFFFFFF;
401  ctx->crc_org = ~get_bits_long(&gb, 32);
402  } else
403  skip_bits_long(&gb, 32);
404  }
405 
406 
407  // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
408 
410 
411  return 0;
412 }
413 
414 
418 {
419  ALSSpecificConfig *sconf = &ctx->sconf;
420  int error = 0;
421 
422  // report unsupported feature and set error value
423  #define MISSING_ERR(cond, str, errval) \
424  { \
425  if (cond) { \
426  av_log_missing_feature(ctx->avctx, str, 0); \
427  error = errval; \
428  } \
429  }
430 
431  MISSING_ERR(sconf->floating, "Floating point decoding", AVERROR_PATCHWELCOME);
432  MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", AVERROR_PATCHWELCOME);
433  MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
434 
435  return error;
436 }
437 
438 
442 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
443  unsigned int div, unsigned int **div_blocks,
444  unsigned int *num_blocks)
445 {
446  if (n < 31 && ((bs_info << n) & 0x40000000)) {
447  // if the level is valid and the investigated bit n is set
448  // then recursively check both children at bits (2n+1) and (2n+2)
449  n *= 2;
450  div += 1;
451  parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
452  parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
453  } else {
454  // else the bit is not set or the last level has been reached
455  // (bit implicitly not set)
456  **div_blocks = div;
457  (*div_blocks)++;
458  (*num_blocks)++;
459  }
460 }
461 
462 
465 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
466 {
467  int max = get_bits_left(gb) - k;
468  int q = get_unary(gb, 0, max);
469  int r = k ? get_bits1(gb) : !(q & 1);
470 
471  if (k > 1) {
472  q <<= (k - 1);
473  q += get_bits_long(gb, k - 1);
474  } else if (!k) {
475  q >>= 1;
476  }
477  return r ? q : ~q;
478 }
479 
480 
483 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
484 {
485  int i, j;
486 
487  for (i = 0, j = k - 1; i < j; i++, j--) {
488  int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
489  cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
490  cof[i] += tmp1;
491  }
492  if (i == j)
493  cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
494 
495  cof[k] = par[k];
496 }
497 
498 
503 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
504  uint32_t *bs_info)
505 {
506  ALSSpecificConfig *sconf = &ctx->sconf;
507  GetBitContext *gb = &ctx->gb;
508  unsigned int *ptr_div_blocks = div_blocks;
509  unsigned int b;
510 
511  if (sconf->block_switching) {
512  unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
513  *bs_info = get_bits_long(gb, bs_info_len);
514  *bs_info <<= (32 - bs_info_len);
515  }
516 
517  ctx->num_blocks = 0;
518  parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
519 
520  // The last frame may have an overdetermined block structure given in
521  // the bitstream. In that case the defined block structure would need
522  // more samples than available to be consistent.
523  // The block structure is actually used but the block sizes are adapted
524  // to fit the actual number of available samples.
525  // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
526  // This results in the actual block sizes: 2 2 1 0.
527  // This is not specified in 14496-3 but actually done by the reference
528  // codec RM22 revision 2.
529  // This appears to happen in case of an odd number of samples in the last
530  // frame which is actually not allowed by the block length switching part
531  // of 14496-3.
532  // The ALS conformance files feature an odd number of samples in the last
533  // frame.
534 
535  for (b = 0; b < ctx->num_blocks; b++)
536  div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
537 
538  if (ctx->cur_frame_length != ctx->sconf.frame_length) {
539  unsigned int remaining = ctx->cur_frame_length;
540 
541  for (b = 0; b < ctx->num_blocks; b++) {
542  if (remaining <= div_blocks[b]) {
543  div_blocks[b] = remaining;
544  ctx->num_blocks = b + 1;
545  break;
546  }
547 
548  remaining -= div_blocks[b];
549  }
550  }
551 }
552 
553 
557 {
558  ALSSpecificConfig *sconf = &ctx->sconf;
559  AVCodecContext *avctx = ctx->avctx;
560  GetBitContext *gb = &ctx->gb;
561 
562  *bd->raw_samples = 0;
563  *bd->const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
564  bd->js_blocks = get_bits1(gb);
565 
566  // skip 5 reserved bits
567  skip_bits(gb, 5);
568 
569  if (*bd->const_block) {
570  unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
571  *bd->raw_samples = get_sbits_long(gb, const_val_bits);
572  }
573 
574  // ensure constant block decoding by reusing this field
575  *bd->const_block = 1;
576 }
577 
578 
582 {
583  int smp = bd->block_length - 1;
584  int32_t val = *bd->raw_samples;
585  int32_t *dst = bd->raw_samples + 1;
586 
587  // write raw samples into buffer
588  for (; smp; smp--)
589  *dst++ = val;
590 }
591 
592 
596 {
597  ALSSpecificConfig *sconf = &ctx->sconf;
598  AVCodecContext *avctx = ctx->avctx;
599  GetBitContext *gb = &ctx->gb;
600  unsigned int k;
601  unsigned int s[8];
602  unsigned int sx[8];
603  unsigned int sub_blocks, log2_sub_blocks, sb_length;
604  unsigned int start = 0;
605  unsigned int opt_order;
606  int sb;
607  int32_t *quant_cof = bd->quant_cof;
608  int32_t *current_res;
609 
610 
611  // ensure variable block decoding by reusing this field
612  *bd->const_block = 0;
613 
614  *bd->opt_order = 1;
615  bd->js_blocks = get_bits1(gb);
616 
617  opt_order = *bd->opt_order;
618 
619  // determine the number of subblocks for entropy decoding
620  if (!sconf->bgmc && !sconf->sb_part) {
621  log2_sub_blocks = 0;
622  } else {
623  if (sconf->bgmc && sconf->sb_part)
624  log2_sub_blocks = get_bits(gb, 2);
625  else
626  log2_sub_blocks = 2 * get_bits1(gb);
627  }
628 
629  sub_blocks = 1 << log2_sub_blocks;
630 
631  // do not continue in case of a damaged stream since
632  // block_length must be evenly divisible by sub_blocks
633  if (bd->block_length & (sub_blocks - 1)) {
634  av_log(avctx, AV_LOG_WARNING,
635  "Block length is not evenly divisible by the number of subblocks.\n");
636  return AVERROR_INVALIDDATA;
637  }
638 
639  sb_length = bd->block_length >> log2_sub_blocks;
640 
641  if (sconf->bgmc) {
642  s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
643  for (k = 1; k < sub_blocks; k++)
644  s[k] = s[k - 1] + decode_rice(gb, 2);
645 
646  for (k = 0; k < sub_blocks; k++) {
647  sx[k] = s[k] & 0x0F;
648  s [k] >>= 4;
649  }
650  } else {
651  s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
652  for (k = 1; k < sub_blocks; k++)
653  s[k] = s[k - 1] + decode_rice(gb, 0);
654  }
655  for (k = 1; k < sub_blocks; k++)
656  if (s[k] > 32) {
657  av_log(avctx, AV_LOG_ERROR, "k invalid for rice code.\n");
658  return AVERROR_INVALIDDATA;
659  }
660 
661  if (get_bits1(gb))
662  *bd->shift_lsbs = get_bits(gb, 4) + 1;
663 
664  *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;
665 
666 
667  if (!sconf->rlslms) {
668  if (sconf->adapt_order) {
669  int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
670  2, sconf->max_order + 1));
671  *bd->opt_order = get_bits(gb, opt_order_length);
672  if (*bd->opt_order > sconf->max_order) {
673  *bd->opt_order = sconf->max_order;
674  av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\n");
675  return AVERROR_INVALIDDATA;
676  }
677  } else {
678  *bd->opt_order = sconf->max_order;
679  }
680 
681  opt_order = *bd->opt_order;
682 
683  if (opt_order) {
684  int add_base;
685 
686  if (sconf->coef_table == 3) {
687  add_base = 0x7F;
688 
689  // read coefficient 0
690  quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
691 
692  // read coefficient 1
693  if (opt_order > 1)
694  quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
695 
696  // read coefficients 2 to opt_order
697  for (k = 2; k < opt_order; k++)
698  quant_cof[k] = get_bits(gb, 7);
699  } else {
700  int k_max;
701  add_base = 1;
702 
703  // read coefficient 0 to 19
704  k_max = FFMIN(opt_order, 20);
705  for (k = 0; k < k_max; k++) {
706  int rice_param = parcor_rice_table[sconf->coef_table][k][1];
707  int offset = parcor_rice_table[sconf->coef_table][k][0];
708  quant_cof[k] = decode_rice(gb, rice_param) + offset;
709  if (quant_cof[k] < -64 || quant_cof[k] > 63) {
710  av_log(avctx, AV_LOG_ERROR, "quant_cof %d is out of range\n", quant_cof[k]);
711  return AVERROR_INVALIDDATA;
712  }
713  }
714 
715  // read coefficients 20 to 126
716  k_max = FFMIN(opt_order, 127);
717  for (; k < k_max; k++)
718  quant_cof[k] = decode_rice(gb, 2) + (k & 1);
719 
720  // read coefficients 127 to opt_order
721  for (; k < opt_order; k++)
722  quant_cof[k] = decode_rice(gb, 1);
723 
724  quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
725 
726  if (opt_order > 1)
727  quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
728  }
729 
730  for (k = 2; k < opt_order; k++)
731  quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
732  }
733  }
734 
735  // read LTP gain and lag values
736  if (sconf->long_term_prediction) {
737  *bd->use_ltp = get_bits1(gb);
738 
739  if (*bd->use_ltp) {
740  int r, c;
741 
742  bd->ltp_gain[0] = decode_rice(gb, 1) << 3;
743  bd->ltp_gain[1] = decode_rice(gb, 2) << 3;
744 
745  r = get_unary(gb, 0, 3);
746  c = get_bits(gb, 2);
747  bd->ltp_gain[2] = ltp_gain_values[r][c];
748 
749  bd->ltp_gain[3] = decode_rice(gb, 2) << 3;
750  bd->ltp_gain[4] = decode_rice(gb, 1) << 3;
751 
752  *bd->ltp_lag = get_bits(gb, ctx->ltp_lag_length);
753  *bd->ltp_lag += FFMAX(4, opt_order + 1);
754  }
755  }
756 
757  // read first value and residuals in case of a random access block
758  if (bd->ra_block) {
759  if (opt_order)
760  bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
761  if (opt_order > 1)
762  bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
763  if (opt_order > 2)
764  bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
765 
766  start = FFMIN(opt_order, 3);
767  }
768 
769  // read all residuals
770  if (sconf->bgmc) {
771  int delta[8];
772  unsigned int k [8];
773  unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
774 
775  // read most significant bits
776  unsigned int high;
777  unsigned int low;
778  unsigned int value;
779 
780  ff_bgmc_decode_init(gb, &high, &low, &value);
781 
782  current_res = bd->raw_samples + start;
783 
784  for (sb = 0; sb < sub_blocks; sb++) {
785  unsigned int sb_len = sb_length - (sb ? 0 : start);
786 
787  k [sb] = s[sb] > b ? s[sb] - b : 0;
788  delta[sb] = 5 - s[sb] + k[sb];
789 
790  ff_bgmc_decode(gb, sb_len, current_res,
791  delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
792 
793  current_res += sb_len;
794  }
795 
796  ff_bgmc_decode_end(gb);
797 
798 
799  // read least significant bits and tails
800  current_res = bd->raw_samples + start;
801 
802  for (sb = 0; sb < sub_blocks; sb++, start = 0) {
803  unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
804  unsigned int cur_k = k[sb];
805  unsigned int cur_s = s[sb];
806 
807  for (; start < sb_length; start++) {
808  int32_t res = *current_res;
809 
810  if (res == cur_tail_code) {
811  unsigned int max_msb = (2 + (sx[sb] > 2) + (sx[sb] > 10))
812  << (5 - delta[sb]);
813 
814  res = decode_rice(gb, cur_s);
815 
816  if (res >= 0) {
817  res += (max_msb ) << cur_k;
818  } else {
819  res -= (max_msb - 1) << cur_k;
820  }
821  } else {
822  if (res > cur_tail_code)
823  res--;
824 
825  if (res & 1)
826  res = -res;
827 
828  res >>= 1;
829 
830  if (cur_k) {
831  res <<= cur_k;
832  res |= get_bits_long(gb, cur_k);
833  }
834  }
835 
836  *current_res++ = res;
837  }
838  }
839  } else {
840  current_res = bd->raw_samples + start;
841 
842  for (sb = 0; sb < sub_blocks; sb++, start = 0)
843  for (; start < sb_length; start++)
844  *current_res++ = decode_rice(gb, s[sb]);
845  }
846 
847  if (!sconf->mc_coding || ctx->js_switch)
848  align_get_bits(gb);
849 
850  return 0;
851 }
852 
853 
857 {
858  ALSSpecificConfig *sconf = &ctx->sconf;
859  unsigned int block_length = bd->block_length;
860  unsigned int smp = 0;
861  unsigned int k;
862  int opt_order = *bd->opt_order;
863  int sb;
864  int64_t y;
865  int32_t *quant_cof = bd->quant_cof;
866  int32_t *lpc_cof = bd->lpc_cof;
867  int32_t *raw_samples = bd->raw_samples;
868  int32_t *raw_samples_end = bd->raw_samples + bd->block_length;
869  int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
870 
871  // reverse long-term prediction
872  if (*bd->use_ltp) {
873  int ltp_smp;
874 
875  for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
876  int center = ltp_smp - *bd->ltp_lag;
877  int begin = FFMAX(0, center - 2);
878  int end = center + 3;
879  int tab = 5 - (end - begin);
880  int base;
881 
882  y = 1 << 6;
883 
884  for (base = begin; base < end; base++, tab++)
885  y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
886 
887  raw_samples[ltp_smp] += y >> 7;
888  }
889  }
890 
891  // reconstruct all samples from residuals
892  if (bd->ra_block) {
893  for (smp = 0; smp < opt_order; smp++) {
894  y = 1 << 19;
895 
896  for (sb = 0; sb < smp; sb++)
897  y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
898 
899  *raw_samples++ -= y >> 20;
900  parcor_to_lpc(smp, quant_cof, lpc_cof);
901  }
902  } else {
903  for (k = 0; k < opt_order; k++)
904  parcor_to_lpc(k, quant_cof, lpc_cof);
905 
906  // store previous samples in case that they have to be altered
907  if (*bd->store_prev_samples)
908  memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
909  sizeof(*bd->prev_raw_samples) * sconf->max_order);
910 
911  // reconstruct difference signal for prediction (joint-stereo)
912  if (bd->js_blocks && bd->raw_other) {
913  int32_t *left, *right;
914 
915  if (bd->raw_other > raw_samples) { // D = R - L
916  left = raw_samples;
917  right = bd->raw_other;
918  } else { // D = R - L
919  left = bd->raw_other;
920  right = raw_samples;
921  }
922 
923  for (sb = -1; sb >= -sconf->max_order; sb--)
924  raw_samples[sb] = right[sb] - left[sb];
925  }
926 
927  // reconstruct shifted signal
928  if (*bd->shift_lsbs)
929  for (sb = -1; sb >= -sconf->max_order; sb--)
930  raw_samples[sb] >>= *bd->shift_lsbs;
931  }
932 
933  // reverse linear prediction coefficients for efficiency
934  lpc_cof = lpc_cof + opt_order;
935 
936  for (sb = 0; sb < opt_order; sb++)
937  lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
938 
939  // reconstruct raw samples
940  raw_samples = bd->raw_samples + smp;
941  lpc_cof = lpc_cof_reversed + opt_order;
942 
943  for (; raw_samples < raw_samples_end; raw_samples++) {
944  y = 1 << 19;
945 
946  for (sb = -opt_order; sb < 0; sb++)
947  y += MUL64(lpc_cof[sb], raw_samples[sb]);
948 
949  *raw_samples -= y >> 20;
950  }
951 
952  raw_samples = bd->raw_samples;
953 
954  // restore previous samples in case that they have been altered
955  if (*bd->store_prev_samples)
956  memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
957  sizeof(*raw_samples) * sconf->max_order);
958 
959  return 0;
960 }
961 
962 
965 static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
966 {
967  int ret = 0;
968  GetBitContext *gb = &ctx->gb;
969 
970  *bd->shift_lsbs = 0;
971  // read block type flag and read the samples accordingly
972  if (get_bits1(gb)) {
973  ret = read_var_block_data(ctx, bd);
974  } else {
975  read_const_block_data(ctx, bd);
976  }
977 
978  return ret;
979 }
980 
981 
985 {
986  unsigned int smp;
987  int ret = 0;
988 
989  // read block type flag and read the samples accordingly
990  if (*bd->const_block)
991  decode_const_block_data(ctx, bd);
992  else
993  ret = decode_var_block_data(ctx, bd); // always return 0
994 
995  if (ret < 0)
996  return ret;
997 
998  // TODO: read RLSLMS extension data
999 
1000  if (*bd->shift_lsbs)
1001  for (smp = 0; smp < bd->block_length; smp++)
1002  bd->raw_samples[smp] <<= *bd->shift_lsbs;
1003 
1004  return 0;
1005 }
1006 
1007 
1011 {
1012  int ret;
1013 
1014  if ((ret = read_block(ctx, bd)) < 0)
1015  return ret;
1016 
1017  return decode_block(ctx, bd);
1018 }
1019 
1020 
1024 static void zero_remaining(unsigned int b, unsigned int b_max,
1025  const unsigned int *div_blocks, int32_t *buf)
1026 {
1027  unsigned int count = 0;
1028 
1029  for (; b < b_max; b++)
1030  count += div_blocks[b];
1031 
1032  if (count)
1033  memset(buf, 0, sizeof(*buf) * count);
1034 }
1035 
1036 
1039 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
1040  unsigned int c, const unsigned int *div_blocks,
1041  unsigned int *js_blocks)
1042 {
1043  int ret;
1044  unsigned int b;
1045  ALSBlockData bd = { 0 };
1046 
1047  bd.ra_block = ra_frame;
1048  bd.const_block = ctx->const_block;
1049  bd.shift_lsbs = ctx->shift_lsbs;
1050  bd.opt_order = ctx->opt_order;
1052  bd.use_ltp = ctx->use_ltp;
1053  bd.ltp_lag = ctx->ltp_lag;
1054  bd.ltp_gain = ctx->ltp_gain[0];
1055  bd.quant_cof = ctx->quant_cof[0];
1056  bd.lpc_cof = ctx->lpc_cof[0];
1058  bd.raw_samples = ctx->raw_samples[c];
1059 
1060 
1061  for (b = 0; b < ctx->num_blocks; b++) {
1062  bd.block_length = div_blocks[b];
1063 
1064  if ((ret = read_decode_block(ctx, &bd)) < 0) {
1065  // damaged block, write zero for the rest of the frame
1066  zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
1067  return ret;
1068  }
1069  bd.raw_samples += div_blocks[b];
1070  bd.ra_block = 0;
1071  }
1072 
1073  return 0;
1074 }
1075 
1076 
1079 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
1080  unsigned int c, const unsigned int *div_blocks,
1081  unsigned int *js_blocks)
1082 {
1083  ALSSpecificConfig *sconf = &ctx->sconf;
1084  unsigned int offset = 0;
1085  unsigned int b;
1086  int ret;
1087  ALSBlockData bd[2] = { { 0 } };
1088 
1089  bd[0].ra_block = ra_frame;
1090  bd[0].const_block = ctx->const_block;
1091  bd[0].shift_lsbs = ctx->shift_lsbs;
1092  bd[0].opt_order = ctx->opt_order;
1094  bd[0].use_ltp = ctx->use_ltp;
1095  bd[0].ltp_lag = ctx->ltp_lag;
1096  bd[0].ltp_gain = ctx->ltp_gain[0];
1097  bd[0].quant_cof = ctx->quant_cof[0];
1098  bd[0].lpc_cof = ctx->lpc_cof[0];
1099  bd[0].prev_raw_samples = ctx->prev_raw_samples;
1100  bd[0].js_blocks = *js_blocks;
1101 
1102  bd[1].ra_block = ra_frame;
1103  bd[1].const_block = ctx->const_block;
1104  bd[1].shift_lsbs = ctx->shift_lsbs;
1105  bd[1].opt_order = ctx->opt_order;
1107  bd[1].use_ltp = ctx->use_ltp;
1108  bd[1].ltp_lag = ctx->ltp_lag;
1109  bd[1].ltp_gain = ctx->ltp_gain[0];
1110  bd[1].quant_cof = ctx->quant_cof[0];
1111  bd[1].lpc_cof = ctx->lpc_cof[0];
1112  bd[1].prev_raw_samples = ctx->prev_raw_samples;
1113  bd[1].js_blocks = *(js_blocks + 1);
1114 
1115  // decode all blocks
1116  for (b = 0; b < ctx->num_blocks; b++) {
1117  unsigned int s;
1118 
1119  bd[0].block_length = div_blocks[b];
1120  bd[1].block_length = div_blocks[b];
1121 
1122  bd[0].raw_samples = ctx->raw_samples[c ] + offset;
1123  bd[1].raw_samples = ctx->raw_samples[c + 1] + offset;
1124 
1125  bd[0].raw_other = bd[1].raw_samples;
1126  bd[1].raw_other = bd[0].raw_samples;
1127 
1128  if ((ret = read_decode_block(ctx, &bd[0])) < 0 ||
1129  (ret = read_decode_block(ctx, &bd[1])) < 0)
1130  goto fail;
1131 
1132  // reconstruct joint-stereo blocks
1133  if (bd[0].js_blocks) {
1134  if (bd[1].js_blocks)
1135  av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
1136 
1137  for (s = 0; s < div_blocks[b]; s++)
1138  bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
1139  } else if (bd[1].js_blocks) {
1140  for (s = 0; s < div_blocks[b]; s++)
1141  bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
1142  }
1143 
1144  offset += div_blocks[b];
1145  bd[0].ra_block = 0;
1146  bd[1].ra_block = 0;
1147  }
1148 
1149  // store carryover raw samples,
1150  // the others channel raw samples are stored by the calling function.
1151  memmove(ctx->raw_samples[c] - sconf->max_order,
1152  ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
1153  sizeof(*ctx->raw_samples[c]) * sconf->max_order);
1154 
1155  return 0;
1156 fail:
1157  // damaged block, write zero for the rest of the frame
1158  zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
1159  zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
1160  return ret;
1161 }
1162 
1163 static inline int als_weighting(GetBitContext *gb, int k, int off)
1164 {
1165  int idx = av_clip(decode_rice(gb, k) + off,
1166  0, FF_ARRAY_ELEMS(mcc_weightings) - 1);
1167  return mcc_weightings[idx];
1168 }
1169 
1172 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
1173 {
1174  GetBitContext *gb = &ctx->gb;
1175  ALSChannelData *current = cd;
1176  unsigned int channels = ctx->avctx->channels;
1177  int entries = 0;
1178 
1179  while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
1180  current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
1181 
1182  if (current->master_channel >= channels) {
1183  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
1184  return AVERROR_INVALIDDATA;
1185  }
1186 
1187  if (current->master_channel != c) {
1188  current->time_diff_flag = get_bits1(gb);
1189  current->weighting[0] = als_weighting(gb, 1, 16);
1190  current->weighting[1] = als_weighting(gb, 2, 14);
1191  current->weighting[2] = als_weighting(gb, 1, 16);
1192 
1193  if (current->time_diff_flag) {
1194  current->weighting[3] = als_weighting(gb, 1, 16);
1195  current->weighting[4] = als_weighting(gb, 1, 16);
1196  current->weighting[5] = als_weighting(gb, 1, 16);
1197 
1198  current->time_diff_sign = get_bits1(gb);
1199  current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
1200  }
1201  }
1202 
1203  current++;
1204  entries++;
1205  }
1206 
1207  if (entries == channels) {
1208  av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
1209  return AVERROR_INVALIDDATA;
1210  }
1211 
1212  align_get_bits(gb);
1213  return 0;
1214 }
1215 
1216 
1220  ALSChannelData **cd, int *reverted,
1221  unsigned int offset, int c)
1222 {
1223  ALSChannelData *ch = cd[c];
1224  unsigned int dep = 0;
1225  unsigned int channels = ctx->avctx->channels;
1226 
1227  if (reverted[c])
1228  return 0;
1229 
1230  reverted[c] = 1;
1231 
1232  while (dep < channels && !ch[dep].stop_flag) {
1233  revert_channel_correlation(ctx, bd, cd, reverted, offset,
1234  ch[dep].master_channel);
1235 
1236  dep++;
1237  }
1238 
1239  if (dep == channels) {
1240  av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
1241  return AVERROR_INVALIDDATA;
1242  }
1243 
1244  bd->const_block = ctx->const_block + c;
1245  bd->shift_lsbs = ctx->shift_lsbs + c;
1246  bd->opt_order = ctx->opt_order + c;
1247  bd->store_prev_samples = ctx->store_prev_samples + c;
1248  bd->use_ltp = ctx->use_ltp + c;
1249  bd->ltp_lag = ctx->ltp_lag + c;
1250  bd->ltp_gain = ctx->ltp_gain[c];
1251  bd->lpc_cof = ctx->lpc_cof[c];
1252  bd->quant_cof = ctx->quant_cof[c];
1253  bd->raw_samples = ctx->raw_samples[c] + offset;
1254 
1255  dep = 0;
1256  while (!ch[dep].stop_flag) {
1257  unsigned int smp;
1258  unsigned int begin = 1;
1259  unsigned int end = bd->block_length - 1;
1260  int64_t y;
1261  int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
1262 
1263  if (ch[dep].time_diff_flag) {
1264  int t = ch[dep].time_diff_index;
1265 
1266  if (ch[dep].time_diff_sign) {
1267  t = -t;
1268  begin -= t;
1269  } else {
1270  end -= t;
1271  }
1272 
1273  for (smp = begin; smp < end; smp++) {
1274  y = (1 << 6) +
1275  MUL64(ch[dep].weighting[0], master[smp - 1 ]) +
1276  MUL64(ch[dep].weighting[1], master[smp ]) +
1277  MUL64(ch[dep].weighting[2], master[smp + 1 ]) +
1278  MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
1279  MUL64(ch[dep].weighting[4], master[smp + t]) +
1280  MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
1281 
1282  bd->raw_samples[smp] += y >> 7;
1283  }
1284  } else {
1285  for (smp = begin; smp < end; smp++) {
1286  y = (1 << 6) +
1287  MUL64(ch[dep].weighting[0], master[smp - 1]) +
1288  MUL64(ch[dep].weighting[1], master[smp ]) +
1289  MUL64(ch[dep].weighting[2], master[smp + 1]);
1290 
1291  bd->raw_samples[smp] += y >> 7;
1292  }
1293  }
1294 
1295  dep++;
1296  }
1297 
1298  return 0;
1299 }
1300 
1301 
1304 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
1305 {
1306  ALSSpecificConfig *sconf = &ctx->sconf;
1307  AVCodecContext *avctx = ctx->avctx;
1308  GetBitContext *gb = &ctx->gb;
1309  unsigned int div_blocks[32];
1310  unsigned int c;
1311  unsigned int js_blocks[2];
1312  uint32_t bs_info = 0;
1313  int ret;
1314 
1315  // skip the size of the ra unit if present in the frame
1316  if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
1317  skip_bits_long(gb, 32);
1318 
1319  if (sconf->mc_coding && sconf->joint_stereo) {
1320  ctx->js_switch = get_bits1(gb);
1321  align_get_bits(gb);
1322  }
1323 
1324  if (!sconf->mc_coding || ctx->js_switch) {
1325  int independent_bs = !sconf->joint_stereo;
1326 
1327  for (c = 0; c < avctx->channels; c++) {
1328  js_blocks[0] = 0;
1329  js_blocks[1] = 0;
1330 
1331  get_block_sizes(ctx, div_blocks, &bs_info);
1332 
1333  // if joint_stereo and block_switching is set, independent decoding
1334  // is signaled via the first bit of bs_info
1335  if (sconf->joint_stereo && sconf->block_switching)
1336  if (bs_info >> 31)
1337  independent_bs = 2;
1338 
1339  // if this is the last channel, it has to be decoded independently
1340  if (c == avctx->channels - 1)
1341  independent_bs = 1;
1342 
1343  if (independent_bs) {
1344  ret = decode_blocks_ind(ctx, ra_frame, c,
1345  div_blocks, js_blocks);
1346  if (ret < 0)
1347  return ret;
1348  independent_bs--;
1349  } else {
1350  ret = decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks);
1351  if (ret < 0)
1352  return ret;
1353 
1354  c++;
1355  }
1356 
1357  // store carryover raw samples
1358  memmove(ctx->raw_samples[c] - sconf->max_order,
1359  ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
1360  sizeof(*ctx->raw_samples[c]) * sconf->max_order);
1361  }
1362  } else { // multi-channel coding
1363  ALSBlockData bd = { 0 };
1364  int b, ret;
1365  int *reverted_channels = ctx->reverted_channels;
1366  unsigned int offset = 0;
1367 
1368  for (c = 0; c < avctx->channels; c++)
1369  if (ctx->chan_data[c] < ctx->chan_data_buffer) {
1370  av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
1371  return AVERROR_INVALIDDATA;
1372  }
1373 
1374  memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
1375 
1376  bd.ra_block = ra_frame;
1378 
1379  get_block_sizes(ctx, div_blocks, &bs_info);
1380 
1381  for (b = 0; b < ctx->num_blocks; b++) {
1382  bd.block_length = div_blocks[b];
1383  if (bd.block_length <= 0) {
1384  av_log(ctx->avctx, AV_LOG_WARNING,
1385  "Invalid block length %d in channel data!\n", bd.block_length);
1386  continue;
1387  }
1388 
1389  for (c = 0; c < avctx->channels; c++) {
1390  bd.const_block = ctx->const_block + c;
1391  bd.shift_lsbs = ctx->shift_lsbs + c;
1392  bd.opt_order = ctx->opt_order + c;
1393  bd.store_prev_samples = ctx->store_prev_samples + c;
1394  bd.use_ltp = ctx->use_ltp + c;
1395  bd.ltp_lag = ctx->ltp_lag + c;
1396  bd.ltp_gain = ctx->ltp_gain[c];
1397  bd.lpc_cof = ctx->lpc_cof[c];
1398  bd.quant_cof = ctx->quant_cof[c];
1399  bd.raw_samples = ctx->raw_samples[c] + offset;
1400  bd.raw_other = NULL;
1401 
1402  if ((ret = read_block(ctx, &bd)) < 0)
1403  return ret;
1404  if ((ret = read_channel_data(ctx, ctx->chan_data[c], c)) < 0)
1405  return ret;
1406  }
1407 
1408  for (c = 0; c < avctx->channels; c++) {
1409  ret = revert_channel_correlation(ctx, &bd, ctx->chan_data,
1410  reverted_channels, offset, c);
1411  if (ret < 0)
1412  return ret;
1413  }
1414  for (c = 0; c < avctx->channels; c++) {
1415  bd.const_block = ctx->const_block + c;
1416  bd.shift_lsbs = ctx->shift_lsbs + c;
1417  bd.opt_order = ctx->opt_order + c;
1418  bd.store_prev_samples = ctx->store_prev_samples + c;
1419  bd.use_ltp = ctx->use_ltp + c;
1420  bd.ltp_lag = ctx->ltp_lag + c;
1421  bd.ltp_gain = ctx->ltp_gain[c];
1422  bd.lpc_cof = ctx->lpc_cof[c];
1423  bd.quant_cof = ctx->quant_cof[c];
1424  bd.raw_samples = ctx->raw_samples[c] + offset;
1425  if ((ret = decode_block(ctx, &bd)) < 0)
1426  return ret;
1427  }
1428 
1429  memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
1430  offset += div_blocks[b];
1431  bd.ra_block = 0;
1432  }
1433 
1434  // store carryover raw samples
1435  for (c = 0; c < avctx->channels; c++)
1436  memmove(ctx->raw_samples[c] - sconf->max_order,
1437  ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
1438  sizeof(*ctx->raw_samples[c]) * sconf->max_order);
1439  }
1440 
1441  // TODO: read_diff_float_data
1442 
1443  return 0;
1444 }
1445 
1446 
1449 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
1450  AVPacket *avpkt)
1451 {
1452  ALSDecContext *ctx = avctx->priv_data;
1453  ALSSpecificConfig *sconf = &ctx->sconf;
1454  const uint8_t *buffer = avpkt->data;
1455  int buffer_size = avpkt->size;
1456  int invalid_frame, ret;
1457  unsigned int c, sample, ra_frame, bytes_read, shift;
1458 
1459  init_get_bits(&ctx->gb, buffer, buffer_size * 8);
1460 
1461  // In the case that the distance between random access frames is set to zero
1462  // (sconf->ra_distance == 0) no frame is treated as a random access frame.
1463  // For the first frame, if prediction is used, all samples used from the
1464  // previous frame are assumed to be zero.
1465  ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
1466 
1467  // the last frame to decode might have a different length
1468  if (sconf->samples != 0xFFFFFFFF)
1469  ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
1470  sconf->frame_length);
1471  else
1472  ctx->cur_frame_length = sconf->frame_length;
1473 
1474  // decode the frame data
1475  if ((invalid_frame = read_frame_data(ctx, ra_frame)) < 0)
1476  av_log(ctx->avctx, AV_LOG_WARNING,
1477  "Reading frame data failed. Skipping RA unit.\n");
1478 
1479  ctx->frame_id++;
1480 
1481  /* get output buffer */
1482  ctx->frame.nb_samples = ctx->cur_frame_length;
1483  if ((ret = ff_get_buffer(avctx, &ctx->frame)) < 0) {
1484  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
1485  return ret;
1486  }
1487 
1488  // transform decoded frame into output format
1489  #define INTERLEAVE_OUTPUT(bps) \
1490  { \
1491  int##bps##_t *dest = (int##bps##_t*)ctx->frame.data[0]; \
1492  shift = bps - ctx->avctx->bits_per_raw_sample; \
1493  for (sample = 0; sample < ctx->cur_frame_length; sample++) \
1494  for (c = 0; c < avctx->channels; c++) \
1495  *dest++ = ctx->raw_samples[c][sample] << shift; \
1496  }
1497 
1498  if (ctx->avctx->bits_per_raw_sample <= 16) {
1499  INTERLEAVE_OUTPUT(16)
1500  } else {
1501  INTERLEAVE_OUTPUT(32)
1502  }
1503 
1504  // update CRC
1505  if (sconf->crc_enabled && (avctx->err_recognition & AV_EF_CRCCHECK)) {
1506  int swap = HAVE_BIGENDIAN != sconf->msb_first;
1507 
1508  if (ctx->avctx->bits_per_raw_sample == 24) {
1509  int32_t *src = (int32_t *)ctx->frame.data[0];
1510 
1511  for (sample = 0;
1512  sample < ctx->cur_frame_length * avctx->channels;
1513  sample++) {
1514  int32_t v;
1515 
1516  if (swap)
1517  v = av_bswap32(src[sample]);
1518  else
1519  v = src[sample];
1520  if (!HAVE_BIGENDIAN)
1521  v >>= 8;
1522 
1523  ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
1524  }
1525  } else {
1526  uint8_t *crc_source;
1527 
1528  if (swap) {
1529  if (ctx->avctx->bits_per_raw_sample <= 16) {
1530  int16_t *src = (int16_t*) ctx->frame.data[0];
1531  int16_t *dest = (int16_t*) ctx->crc_buffer;
1532  for (sample = 0;
1533  sample < ctx->cur_frame_length * avctx->channels;
1534  sample++)
1535  *dest++ = av_bswap16(src[sample]);
1536  } else {
1537  ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer,
1538  (uint32_t *)ctx->frame.data[0],
1539  ctx->cur_frame_length * avctx->channels);
1540  }
1541  crc_source = ctx->crc_buffer;
1542  } else {
1543  crc_source = ctx->frame.data[0];
1544  }
1545 
1546  ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source,
1547  ctx->cur_frame_length * avctx->channels *
1549  }
1550 
1551 
1552  // check CRC sums if this is the last frame
1553  if (ctx->cur_frame_length != sconf->frame_length &&
1554  ctx->crc_org != ctx->crc) {
1555  av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
1556  }
1557  }
1558 
1559  *got_frame_ptr = 1;
1560  *(AVFrame *)data = ctx->frame;
1561 
1562 
1563  bytes_read = invalid_frame ? buffer_size :
1564  (get_bits_count(&ctx->gb) + 7) >> 3;
1565 
1566  return bytes_read;
1567 }
1568 
1569 
1573 {
1574  ALSDecContext *ctx = avctx->priv_data;
1575 
1576  av_freep(&ctx->sconf.chan_pos);
1577 
1578  ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
1579 
1580  av_freep(&ctx->const_block);
1581  av_freep(&ctx->shift_lsbs);
1582  av_freep(&ctx->opt_order);
1584  av_freep(&ctx->use_ltp);
1585  av_freep(&ctx->ltp_lag);
1586  av_freep(&ctx->ltp_gain);
1587  av_freep(&ctx->ltp_gain_buffer);
1588  av_freep(&ctx->quant_cof);
1589  av_freep(&ctx->lpc_cof);
1590  av_freep(&ctx->quant_cof_buffer);
1591  av_freep(&ctx->lpc_cof_buffer);
1593  av_freep(&ctx->prev_raw_samples);
1594  av_freep(&ctx->raw_samples);
1595  av_freep(&ctx->raw_buffer);
1596  av_freep(&ctx->chan_data);
1597  av_freep(&ctx->chan_data_buffer);
1598  av_freep(&ctx->reverted_channels);
1599  av_freep(&ctx->crc_buffer);
1600 
1601  return 0;
1602 }
1603 
1604 
1608 {
1609  unsigned int c;
1610  unsigned int channel_size;
1611  int num_buffers, ret;
1612  ALSDecContext *ctx = avctx->priv_data;
1613  ALSSpecificConfig *sconf = &ctx->sconf;
1614  ctx->avctx = avctx;
1615 
1616  if (!avctx->extradata) {
1617  av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
1618  return AVERROR_INVALIDDATA;
1619  }
1620 
1621  if ((ret = read_specific_config(ctx)) < 0) {
1622  av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
1623  goto fail;
1624  }
1625 
1626  if ((ret = check_specific_config(ctx)) < 0) {
1627  goto fail;
1628  }
1629 
1630  if (sconf->bgmc) {
1631  ret = ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
1632  if (ret < 0)
1633  goto fail;
1634  }
1635  if (sconf->floating) {
1636  avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
1637  avctx->bits_per_raw_sample = 32;
1638  } else {
1639  avctx->sample_fmt = sconf->resolution > 1
1641  avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
1642  }
1643 
1644  // set maximum Rice parameter for progressive decoding based on resolution
1645  // This is not specified in 14496-3 but actually done by the reference
1646  // codec RM22 revision 2.
1647  ctx->s_max = sconf->resolution > 1 ? 31 : 15;
1648 
1649  // set lag value for long-term prediction
1650  ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
1651  (avctx->sample_rate >= 192000);
1652 
1653  // allocate quantized parcor coefficient buffer
1654  num_buffers = sconf->mc_coding ? avctx->channels : 1;
1655 
1656  ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
1657  ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * num_buffers);
1658  ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
1659  num_buffers * sconf->max_order);
1660  ctx->lpc_cof_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
1661  num_buffers * sconf->max_order);
1662  ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
1663  sconf->max_order);
1664 
1665  if (!ctx->quant_cof || !ctx->lpc_cof ||
1666  !ctx->quant_cof_buffer || !ctx->lpc_cof_buffer ||
1667  !ctx->lpc_cof_reversed_buffer) {
1668  av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1669  ret = AVERROR(ENOMEM);
1670  goto fail;
1671  }
1672 
1673  // assign quantized parcor coefficient buffers
1674  for (c = 0; c < num_buffers; c++) {
1675  ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
1676  ctx->lpc_cof[c] = ctx->lpc_cof_buffer + c * sconf->max_order;
1677  }
1678 
1679  // allocate and assign lag and gain data buffer for ltp mode
1680  ctx->const_block = av_malloc (sizeof(*ctx->const_block) * num_buffers);
1681  ctx->shift_lsbs = av_malloc (sizeof(*ctx->shift_lsbs) * num_buffers);
1682  ctx->opt_order = av_malloc (sizeof(*ctx->opt_order) * num_buffers);
1683  ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
1684  ctx->use_ltp = av_mallocz(sizeof(*ctx->use_ltp) * num_buffers);
1685  ctx->ltp_lag = av_malloc (sizeof(*ctx->ltp_lag) * num_buffers);
1686  ctx->ltp_gain = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
1687  ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
1688  num_buffers * 5);
1689 
1690  if (!ctx->const_block || !ctx->shift_lsbs ||
1691  !ctx->opt_order || !ctx->store_prev_samples ||
1692  !ctx->use_ltp || !ctx->ltp_lag ||
1693  !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
1694  av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1695  ret = AVERROR(ENOMEM);
1696  goto fail;
1697  }
1698 
1699  for (c = 0; c < num_buffers; c++)
1700  ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
1701 
1702  // allocate and assign channel data buffer for mcc mode
1703  if (sconf->mc_coding) {
1704  ctx->chan_data_buffer = av_malloc(sizeof(*ctx->chan_data_buffer) *
1705  num_buffers * num_buffers);
1706  ctx->chan_data = av_malloc(sizeof(*ctx->chan_data) *
1707  num_buffers);
1708  ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
1709  num_buffers);
1710 
1711  if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
1712  av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1713  ret = AVERROR(ENOMEM);
1714  goto fail;
1715  }
1716 
1717  for (c = 0; c < num_buffers; c++)
1718  ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
1719  } else {
1720  ctx->chan_data = NULL;
1721  ctx->chan_data_buffer = NULL;
1722  ctx->reverted_channels = NULL;
1723  }
1724 
1725  channel_size = sconf->frame_length + sconf->max_order;
1726 
1727  ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
1728  ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
1729  ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
1730 
1731  // allocate previous raw sample buffer
1732  if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
1733  av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1734  ret = AVERROR(ENOMEM);
1735  goto fail;
1736  }
1737 
1738  // assign raw samples buffers
1739  ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
1740  for (c = 1; c < avctx->channels; c++)
1741  ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
1742 
1743  // allocate crc buffer
1744  if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
1745  (avctx->err_recognition & AV_EF_CRCCHECK)) {
1746  ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
1747  ctx->cur_frame_length *
1748  avctx->channels *
1750  if (!ctx->crc_buffer) {
1751  av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1752  ret = AVERROR(ENOMEM);
1753  goto fail;
1754  }
1755  }
1756 
1757  ff_dsputil_init(&ctx->dsp, avctx);
1758 
1760  avctx->coded_frame = &ctx->frame;
1761 
1762  return 0;
1763 
1764 fail:
1765  decode_end(avctx);
1766  return ret;
1767 }
1768 
1769 
1772 static av_cold void flush(AVCodecContext *avctx)
1773 {
1774  ALSDecContext *ctx = avctx->priv_data;
1775 
1776  ctx->frame_id = 0;
1777 }
1778 
1779 
1781  .name = "als",
1782  .type = AVMEDIA_TYPE_AUDIO,
1783  .id = AV_CODEC_ID_MP4ALS,
1784  .priv_data_size = sizeof(ALSDecContext),
1785  .init = decode_init,
1786  .close = decode_end,
1787  .decode = decode_frame,
1788  .flush = flush,
1789  .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
1790  .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
1791 };
#define MUL64(a, b)
Definition: mathops.h:51
AVCodec ff_als_decoder
Definition: alsdec.c:1780
static int als_weighting(GetBitContext *gb, int k, int off)
Definition: alsdec.c:1163
static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
Decode the block data for a non-constant block.
Definition: alsdec.c:856
int msb_first
1 = original CRC calculated on big-endian system, 0 = little-endian
Definition: alsdec.c:161
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
av_cold void ff_dsputil_init(DSPContext *c, AVCodecContext *avctx)
Definition: dsputil.c:2656
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
int * use_ltp
contains use_ltp flags for all channels
Definition: alsdec.c:214
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:90
#define HAVE_BIGENDIAN
Definition: config.h:102
int32_t ** raw_samples
decoded raw samples for each channel
Definition: alsdec.c:227
uint8_t * crc_buffer
buffer of byte order corrected samples used for CRC check
Definition: alsdec.c:229
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:237
static const int16_t mcc_weightings[]
Inter-channel weighting factors for multi-channel correlation.
Definition: alsdec.c:120
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:197
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
static void align_get_bits(GetBitContext *s)
Definition: get_bits.h:412
int block_switching
number of block switching levels
Definition: alsdec.c:169
int rlslms
use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
Definition: alsdec.c:176
int size
Definition: avcodec.h:916
static int check_specific_config(ALSDecContext *ctx)
Check the ALSSpecificConfig for unsupported features.
Definition: alsdec.c:417
#define av_bswap16
Definition: bswap.h:31
int adapt_order
adaptive order: 1 = on, 0 = off
Definition: alsdec.c:165
static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
Read the frame data.
Definition: alsdec.c:1304
int32_t * lpc_cof_reversed_buffer
temporary buffer to set up a reversed versio of lpc_cof_buffer
Definition: alsdec.c:222
GetBitContext gb
Definition: alsdec.c:197
Block Gilbert-Moore decoder header.
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
unsigned int js_switch
if true, joint-stereo decoding is enforced
Definition: alsdec.c:204
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2711
static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
Read and decode block data successively.
Definition: alsdec.c:1010
#define INTERLEAVE_OUTPUT(bps)
signed 16 bits
Definition: samplefmt.h:52
#define sample
AVCodec.
Definition: avcodec.h:2960
static int32_t decode_rice(GetBitContext *gb, unsigned int k)
Read and decode a Rice codeword.
Definition: alsdec.c:465
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:337
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
int * ltp_lag
contains ltp lag values for all channels
Definition: alsdec.c:215
int * const_block
contains const_block flags for all channels
Definition: alsdec.c:210
static const uint8_t ltp_gain_values[4][4]
Gain values of p(0) for long-term prediction.
Definition: alsdec.c:109
static av_cold int decode_init(AVCodecContext *avctx)
Initialize the ALS decoder.
Definition: alsdec.c:1607
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
int32_t * lpc_cof
coefficients of the direct form prediction
Definition: alsdec.c:245
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
Decode the block data for a constant block.
Definition: alsdec.c:581
float delta
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:76
int ** ltp_gain
gain values for ltp 5-tap filter for a channel
Definition: alsdec.c:216
#define b
Definition: input.c:52
static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
Read the block data for a constant block.
Definition: alsdec.c:556
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
int chan_sort
channel rearrangement: 1 = on, 0 = off
Definition: alsdec.c:175
int joint_stereo
joint stereo: 1 = on, 0 = off
Definition: alsdec.c:172
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:192
bitstream reader API header.
static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame, unsigned int c, const unsigned int *div_blocks, unsigned int *js_blocks)
Decode blocks independently.
Definition: alsdec.c:1039
void ff_bgmc_decode_init(GetBitContext *gb, unsigned int *h, unsigned int *l, unsigned int *v)
Initialize decoding and reads the first value.
Definition: bgmc.c:486
unsigned int block_length
number of samples within the block
Definition: alsdec.c:234
static void zero_remaining(unsigned int b, unsigned int b_max, const unsigned int *div_blocks, int32_t *buf)
Compute the number of samples left to decode for the current frame and sets these samples to zero...
Definition: alsdec.c:1024
int ra_distance
distance between RA frames (in frames, 0...255)
Definition: alsdec.c:163
int weighting[6]
Definition: alsdec.c:189
int32_t * quant_cof_buffer
contains all quantized parcor coefficients
Definition: alsdec.c:219
ALSChannelData * chan_data_buffer
contains channel data for all channels
Definition: alsdec.c:224
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
int bgmc
"Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)
Definition: alsdec.c:170
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:547
int * use_ltp
if true, long-term prediction is used
Definition: alsdec.c:241
enum RA_Flag ra_flag
indicates where the size of ra units is stored
Definition: alsdec.c:164
int ltp_lag_length
number of bits used for ltp lag value
Definition: alsdec.c:209
void(* bswap_buf)(uint32_t *dst, const uint32_t *src, int w)
Definition: dsputil.h:343
static av_cold void dprint_specific_config(ALSDecContext *ctx)
Definition: alsdec.c:252
unsigned int * opt_order
prediction order of this block
Definition: alsdec.c:239
int * chan_pos
original channel positions
Definition: alsdec.c:178
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
AVCodecContext * avctx
Definition: alsdec.c:194
static const int16_t parcor_scaled_values[]
Scaled PARCOR values used for the first two PARCOR coefficients.
Definition: alsdec.c:70
int32_t ** lpc_cof
coefficients of the direct form prediction filter for a channel
Definition: alsdec.c:220
int chan_config_info
mapping of channels to loudspeaker locations. Unused until setting channel configuration is implement...
Definition: alsdec.c:177
unsigned int num_blocks
number of blocks used in the current frame
Definition: alsdec.c:205
void ff_bgmc_end(uint8_t **cf_lut, int **cf_lut_status)
Release the lookup table arrays.
Definition: bgmc.c:478
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
int32_t * prev_raw_samples
contains unshifted raw samples from the previous block
Definition: alsdec.c:226
static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame, unsigned int c, const unsigned int *div_blocks, unsigned int *js_blocks)
Decode blocks dependently.
Definition: alsdec.c:1079
void ff_bgmc_decode_end(GetBitContext *gb)
Finish decoding.
Definition: bgmc.c:496
const AVCRC * crc_table
Definition: alsdec.c:199
int * bgmc_lut_status
pointer at lookup table status flags used for BGMC
Definition: alsdec.c:208
int off
Definition: dsputil_bfin.c:28
ALSSpecificConfig sconf
Definition: alsdec.c:196
int * store_prev_samples
if true, carryover samples have to be stored
Definition: alsdec.c:240
signed 32 bits
Definition: samplefmt.h:53
int ff_bgmc_init(AVCodecContext *avctx, uint8_t **cf_lut, int **cf_lut_status)
Initialize the lookup table arrays.
Definition: bgmc.c:459
unsigned int * shift_lsbs
contains shift_lsbs flags for all channels
Definition: alsdec.c:211
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:2602
static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
Read the block data for a non-constant block.
Definition: alsdec.c:595
int chan_config
indicates that a chan_config_info field is present
Definition: alsdec.c:174
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
int32_t
static char buffer[20]
Definition: seek-test.c:31
void ff_bgmc_decode(GetBitContext *gb, unsigned int num, int32_t *dst, int delta, unsigned int sx, unsigned int *h, unsigned int *l, unsigned int *v, uint8_t *cf_lut, int *cf_lut_status)
Read and decode a block Gilbert-Moore coded symbol.
Definition: bgmc.c:503
static av_cold int decode_end(AVCodecContext *avctx)
Uninitialize the ALS decoder.
Definition: alsdec.c:1572
int * const_block
if true, this is a constant value block
Definition: alsdec.c:236
int floating
1 = IEEE 32-bit floating-point, 0 = integer
Definition: alsdec.c:160
int time_diff_flag
Definition: alsdec.c:186
int master_channel
Definition: alsdec.c:185
uint32_t crc
CRC value calculated from decoded data.
Definition: alsdec.c:201
int coef_table
table index of Rice code parameters
Definition: alsdec.c:166
int sb_part
sub-block partition
Definition: alsdec.c:171
DSPContext dsp
Definition: alsdec.c:198
int32_t * raw_other
decoded raw samples of the other channel of a channel pair
Definition: alsdec.c:248
uint8_t * bgmc_lut
pointer at lookup tables used for BGMC
Definition: alsdec.c:207
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:95
int * ltp_gain
gain values for ltp 5-tap filter
Definition: alsdec.c:243
NULL
Definition: eval.c:52
int js_blocks
true if this block contains a difference signal
Definition: alsdec.c:237
#define av_bswap32
Definition: bswap.h:33
unsigned int ra_block
if true, this is a random access block
Definition: alsdec.c:235
external API header
static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
Convert PARCOR coefficient k to direct filter coefficient.
Definition: alsdec.c:483
int sample_rate
samples per second
Definition: avcodec.h:2104
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
ALSChannelData ** chan_data
channel data for multi-channel correlation
Definition: alsdec.c:223
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Decode an ALS frame.
Definition: alsdec.c:1449
#define MISSING_ERR(cond, str, errval)
int extradata_size
Definition: avcodec.h:1455
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:268
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:293
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
unsigned int s_max
maximum Rice parameter allowed in entropy coding
Definition: alsdec.c:206
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:260
int * ltp_lag
lag value for long-term prediction
Definition: alsdec.c:242
int32_t * lpc_cof_buffer
contains all coefficients of the direct form prediction filter
Definition: alsdec.c:221
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:372
static const int8_t parcor_rice_table[3][20][2]
Rice parameters and corresponding index offsets for decoding the indices of scaled PARCOR values...
Definition: alsdec.c:49
RA_Flag
Definition: alsdec.c:150
static av_cold int read_specific_config(ALSDecContext *ctx)
Read an ALSSpecificConfig from a buffer into the output struct.
Definition: alsdec.c:282
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:301
int long_term_prediction
long term prediction (LTP): 1 = on, 0 = off
Definition: alsdec.c:167
int32_t * raw_samples
decoded raw samples / residuals for this block
Definition: alsdec.c:246
int * reverted_channels
stores a flag for each reverted channel
Definition: alsdec.c:225
unsigned int * opt_order
contains opt_order flags for all channels
Definition: alsdec.c:212
int32_t * raw_buffer
contains all decoded raw samples including carryover samples
Definition: alsdec.c:228
int max_order
maximum prediction order (0..1023)
Definition: alsdec.c:168
uint32_t samples
number of samples, 0xFFFFFFFF if unknown
Definition: alsdec.c:158
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int mc_coding
extended inter-channel coding (multi channel coding): 1 = on, 0 = off
Definition: alsdec.c:173
static const uint8_t tail_code[16][6]
Tail codes used in arithmetic coding using block Gilbert-Moore codes.
Definition: alsdec.c:130
common internal api header.
int32_t * prev_raw_samples
contains unshifted raw samples from the previous block
Definition: alsdec.c:247
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:33
static av_cold void flush(AVCodecContext *avctx)
Flush (reset) the frame ID after seeking.
Definition: alsdec.c:1772
int time_diff_index
Definition: alsdec.c:188
int * ltp_gain_buffer
contains all gain values for ltp 5-tap filter
Definition: alsdec.c:217
int32_t * quant_cof
quantized parcor coefficients
Definition: alsdec.c:244
uint32_t AVCRC
Definition: crc.h:28
int avpriv_mpeg4audio_get_config(MPEG4AudioConfig *c, const uint8_t *buf, int bit_size, int sync_extension)
Parse MPEG-4 systems extradata to retrieve audio configuration.
Definition: mpeg4audio.c:79
DSP utils.
static void parse_bs_info(const uint32_t bs_info, unsigned int n, unsigned int div, unsigned int **div_blocks, unsigned int *num_blocks)
Parse the bs_info field to extract the block partitioning used in block switching mode...
Definition: alsdec.c:442
void * priv_data
Definition: avcodec.h:1382
int32_t ** quant_cof
quantized parcor coefficients for a channel
Definition: alsdec.c:218
int channels
number of audio channels
Definition: avcodec.h:2105
int crc_enabled
enable Cyclic Redundancy Checksum
Definition: alsdec.c:179
uint32_t crc_org
CRC value of the original input data.
Definition: alsdec.c:200
static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
Decode the block data.
Definition: alsdec.c:984
static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
Read the block data.
Definition: alsdec.c:965
int frame_length
frame length for each frame (last frame may differ)
Definition: alsdec.c:162
int stop_flag
Definition: alsdec.c:184
static const struct twinvq_data tab
unsigned int * shift_lsbs
shift of values for this block
Definition: alsdec.c:238
static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
Read the channel data.
Definition: alsdec.c:1172
static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks, uint32_t *bs_info)
Read block switching field if necessary and set actual block sizes.
Definition: alsdec.c:503
int * store_prev_samples
contains store_prev_samples flags for all channels
Definition: alsdec.c:213
unsigned int frame_id
the frame ID / number of the current frame
Definition: alsdec.c:203
static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd, ALSChannelData **cd, int *reverted, unsigned int offset, int c)
Recursively reverts the inter-channel correlation for a block.
Definition: alsdec.c:1219
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
for(j=16;j >0;--j)
unsigned int cur_frame_length
length of the current frame to decode
Definition: alsdec.c:202
AVFrame frame
Definition: alsdec.c:195
DSPContext.
Definition: dsputil.h:194
int resolution
000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
Definition: alsdec.c:159
int time_diff_sign
Definition: alsdec.c:187
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)