Use git_bits_left() instead of size_in_bits - get_bits_count().
[ffmpeg-lucabe.git] / libavcodec / alsdec.c
blobac4a1f86a3bd1752c6e674d56f88ffac0a974f19
1 /*
2 * MPEG-4 ALS decoder
3 * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file libavcodec/alsdec.c
24 * MPEG-4 ALS decoder
25 * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
29 //#define DEBUG
32 #include "avcodec.h"
33 #include "get_bits.h"
34 #include "unary.h"
35 #include "mpeg4audio.h"
36 #include "bytestream.h"
38 #include <stdint.h>
40 /** Rice parameters and corresponding index offsets for decoding the
41 * indices of scaled PARCOR values. The table choosen is set globally
42 * by the encoder and stored in ALSSpecificConfig.
44 static const int8_t parcor_rice_table[3][20][2] = {
45 { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},
46 { 12, 3}, { -7, 3}, { 9, 3}, { -5, 3}, { 6, 3},
47 { -4, 3}, { 3, 3}, { -3, 2}, { 3, 2}, { -2, 2},
48 { 3, 2}, { -1, 2}, { 2, 2}, { -1, 2}, { 2, 2} },
49 { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},
50 { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},
51 {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},
52 { 7, 3}, { -4, 4}, { 3, 3}, { -1, 3}, { 1, 3} },
53 { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},
54 { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},
55 {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},
56 { 3, 3}, { 0, 3}, { -1, 3}, { 2, 3}, { -1, 2} }
60 /** Scaled PARCOR values used for the first two PARCOR coefficients.
61 * To be indexed by the Rice coded indices.
62 * Generated by: parcor_scaled_values[i] = 32 + ((i * (i+1)) << 7) - (1 << 20)
63 * Actual values are divided by 32 in order to be stored in 16 bits.
65 static const int16_t parcor_scaled_values[] = {
66 -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,
67 -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,
68 -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,
69 -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,
70 -1013728 / 32, -1009376 / 32, -1004768 / 32, -999904 / 32,
71 -994784 / 32, -989408 / 32, -983776 / 32, -977888 / 32,
72 -971744 / 32, -965344 / 32, -958688 / 32, -951776 / 32,
73 -944608 / 32, -937184 / 32, -929504 / 32, -921568 / 32,
74 -913376 / 32, -904928 / 32, -896224 / 32, -887264 / 32,
75 -878048 / 32, -868576 / 32, -858848 / 32, -848864 / 32,
76 -838624 / 32, -828128 / 32, -817376 / 32, -806368 / 32,
77 -795104 / 32, -783584 / 32, -771808 / 32, -759776 / 32,
78 -747488 / 32, -734944 / 32, -722144 / 32, -709088 / 32,
79 -695776 / 32, -682208 / 32, -668384 / 32, -654304 / 32,
80 -639968 / 32, -625376 / 32, -610528 / 32, -595424 / 32,
81 -580064 / 32, -564448 / 32, -548576 / 32, -532448 / 32,
82 -516064 / 32, -499424 / 32, -482528 / 32, -465376 / 32,
83 -447968 / 32, -430304 / 32, -412384 / 32, -394208 / 32,
84 -375776 / 32, -357088 / 32, -338144 / 32, -318944 / 32,
85 -299488 / 32, -279776 / 32, -259808 / 32, -239584 / 32,
86 -219104 / 32, -198368 / 32, -177376 / 32, -156128 / 32,
87 -134624 / 32, -112864 / 32, -90848 / 32, -68576 / 32,
88 -46048 / 32, -23264 / 32, -224 / 32, 23072 / 32,
89 46624 / 32, 70432 / 32, 94496 / 32, 118816 / 32,
90 143392 / 32, 168224 / 32, 193312 / 32, 218656 / 32,
91 244256 / 32, 270112 / 32, 296224 / 32, 322592 / 32,
92 349216 / 32, 376096 / 32, 403232 / 32, 430624 / 32,
93 458272 / 32, 486176 / 32, 514336 / 32, 542752 / 32,
94 571424 / 32, 600352 / 32, 629536 / 32, 658976 / 32,
95 688672 / 32, 718624 / 32, 748832 / 32, 779296 / 32,
96 810016 / 32, 840992 / 32, 872224 / 32, 903712 / 32,
97 935456 / 32, 967456 / 32, 999712 / 32, 1032224 / 32
101 /** Gain values of p(0) for long-term prediction.
102 * To be indexed by the Rice coded indices.
104 static const uint8_t ltp_gain_values [4][4] = {
105 { 0, 8, 16, 24},
106 {32, 40, 48, 56},
107 {64, 70, 76, 82},
108 {88, 92, 96, 100}
112 enum RA_Flag {
113 RA_FLAG_NONE,
114 RA_FLAG_FRAMES,
115 RA_FLAG_HEADER
119 typedef struct {
120 uint32_t samples; ///< number of samples, 0xFFFFFFFF if unknown
121 int resolution; ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
122 int floating; ///< 1 = IEEE 32-bit floating-point, 0 = integer
123 int frame_length; ///< frame length for each frame (last frame may differ)
124 int ra_distance; ///< distance between RA frames (in frames, 0...255)
125 enum RA_Flag ra_flag; ///< indicates where the size of ra units is stored
126 int adapt_order; ///< adaptive order: 1 = on, 0 = off
127 int coef_table; ///< table index of Rice code parameters
128 int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off
129 int max_order; ///< maximum prediction order (0..1023)
130 int block_switching; ///< number of block switching levels
131 int bgmc; ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)
132 int sb_part; ///< sub-block partition
133 int joint_stereo; ///< joint stereo: 1 = on, 0 = off
134 int mc_coding; ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off
135 int chan_config; ///< indicates that a chan_config_info field is present
136 int chan_sort; ///< channel rearrangement: 1 = on, 0 = off
137 int rlslms; ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
138 int chan_config_info; ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.
139 int *chan_pos; ///< original channel positions
140 uint32_t header_size; ///< header size of original audio file in bytes, provided for debugging
141 uint32_t trailer_size; ///< trailer size of original audio file in bytes, provided for debugging
142 } ALSSpecificConfig;
145 typedef struct {
146 AVCodecContext *avctx;
147 ALSSpecificConfig sconf;
148 GetBitContext gb;
149 unsigned int cur_frame_length; ///< length of the current frame to decode
150 unsigned int frame_id; ///< the frame ID / number of the current frame
151 unsigned int js_switch; ///< if true, joint-stereo decoding is enforced
152 unsigned int num_blocks; ///< number of blocks used in the current frame
153 int ltp_lag_length; ///< number of bits used for ltp lag value
154 int32_t *quant_cof; ///< quantized parcor coefficients
155 int32_t *lpc_cof; ///< coefficients of the direct form prediction filter
156 int32_t *prev_raw_samples; ///< contains unshifted raw samples from the previous block
157 int32_t **raw_samples; ///< decoded raw samples for each channel
158 int32_t *raw_buffer; ///< contains all decoded raw samples including carryover samples
159 } ALSDecContext;
162 static av_cold void dprint_specific_config(ALSDecContext *ctx)
164 #ifdef DEBUG
165 AVCodecContext *avctx = ctx->avctx;
166 ALSSpecificConfig *sconf = &ctx->sconf;
168 dprintf(avctx, "resolution = %i\n", sconf->resolution);
169 dprintf(avctx, "floating = %i\n", sconf->floating);
170 dprintf(avctx, "frame_length = %i\n", sconf->frame_length);
171 dprintf(avctx, "ra_distance = %i\n", sconf->ra_distance);
172 dprintf(avctx, "ra_flag = %i\n", sconf->ra_flag);
173 dprintf(avctx, "adapt_order = %i\n", sconf->adapt_order);
174 dprintf(avctx, "coef_table = %i\n", sconf->coef_table);
175 dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
176 dprintf(avctx, "max_order = %i\n", sconf->max_order);
177 dprintf(avctx, "block_switching = %i\n", sconf->block_switching);
178 dprintf(avctx, "bgmc = %i\n", sconf->bgmc);
179 dprintf(avctx, "sb_part = %i\n", sconf->sb_part);
180 dprintf(avctx, "joint_stereo = %i\n", sconf->joint_stereo);
181 dprintf(avctx, "mc_coding = %i\n", sconf->mc_coding);
182 dprintf(avctx, "chan_config = %i\n", sconf->chan_config);
183 dprintf(avctx, "chan_sort = %i\n", sconf->chan_sort);
184 dprintf(avctx, "RLSLMS = %i\n", sconf->rlslms);
185 dprintf(avctx, "chan_config_info = %i\n", sconf->chan_config_info);
186 dprintf(avctx, "header_size = %i\n", sconf->header_size);
187 dprintf(avctx, "trailer_size = %i\n", sconf->trailer_size);
188 #endif
192 /** Reads an ALSSpecificConfig from a buffer into the output struct.
194 static av_cold int read_specific_config(ALSDecContext *ctx)
196 GetBitContext gb;
197 uint64_t ht_size;
198 int i, config_offset, crc_enabled;
199 MPEG4AudioConfig m4ac;
200 ALSSpecificConfig *sconf = &ctx->sconf;
201 AVCodecContext *avctx = ctx->avctx;
202 uint32_t als_id;
204 init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
206 config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
207 avctx->extradata_size);
209 if (config_offset < 0)
210 return -1;
212 skip_bits_long(&gb, config_offset);
214 if (get_bits_left(&gb) < (30 << 3))
215 return -1;
217 // read the fixed items
218 als_id = get_bits_long(&gb, 32);
219 avctx->sample_rate = m4ac.sample_rate;
220 skip_bits_long(&gb, 32); // sample rate already known
221 sconf->samples = get_bits_long(&gb, 32);
222 avctx->channels = m4ac.channels;
223 skip_bits(&gb, 16); // number of channels already knwon
224 skip_bits(&gb, 3); // skip file_type
225 sconf->resolution = get_bits(&gb, 3);
226 sconf->floating = get_bits1(&gb);
227 skip_bits1(&gb); // skip msb_first
228 sconf->frame_length = get_bits(&gb, 16) + 1;
229 sconf->ra_distance = get_bits(&gb, 8);
230 sconf->ra_flag = get_bits(&gb, 2);
231 sconf->adapt_order = get_bits1(&gb);
232 sconf->coef_table = get_bits(&gb, 2);
233 sconf->long_term_prediction = get_bits1(&gb);
234 sconf->max_order = get_bits(&gb, 10);
235 sconf->block_switching = get_bits(&gb, 2);
236 sconf->bgmc = get_bits1(&gb);
237 sconf->sb_part = get_bits1(&gb);
238 sconf->joint_stereo = get_bits1(&gb);
239 sconf->mc_coding = get_bits1(&gb);
240 sconf->chan_config = get_bits1(&gb);
241 sconf->chan_sort = get_bits1(&gb);
242 crc_enabled = get_bits1(&gb);
243 sconf->rlslms = get_bits1(&gb);
244 skip_bits(&gb, 5); // skip 5 reserved bits
245 skip_bits1(&gb); // skip aux_data_enabled
248 // check for ALSSpecificConfig struct
249 if (als_id != MKBETAG('A','L','S','\0'))
250 return -1;
252 ctx->cur_frame_length = sconf->frame_length;
254 // allocate quantized parcor coefficient buffer
255 if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) ||
256 !(ctx->lpc_cof = av_malloc(sizeof(*ctx->lpc_cof) * sconf->max_order))) {
257 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
258 return AVERROR(ENOMEM);
261 // read channel config
262 if (sconf->chan_config)
263 sconf->chan_config_info = get_bits(&gb, 16);
264 // TODO: use this to set avctx->channel_layout
267 // read channel sorting
268 if (sconf->chan_sort && avctx->channels > 1) {
269 int chan_pos_bits = av_ceil_log2(avctx->channels);
270 int bits_needed = avctx->channels * chan_pos_bits + 7;
271 if (get_bits_left(&gb) < bits_needed)
272 return -1;
274 if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
275 return AVERROR(ENOMEM);
277 for (i = 0; i < avctx->channels; i++)
278 sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
280 align_get_bits(&gb);
281 // TODO: use this to actually do channel sorting
282 } else {
283 sconf->chan_sort = 0;
287 // read fixed header and trailer sizes,
288 // if size = 0xFFFFFFFF then there is no data field!
289 if (get_bits_left(&gb) < 64)
290 return -1;
292 sconf->header_size = get_bits_long(&gb, 32);
293 sconf->trailer_size = get_bits_long(&gb, 32);
294 if (sconf->header_size == 0xFFFFFFFF)
295 sconf->header_size = 0;
296 if (sconf->trailer_size == 0xFFFFFFFF)
297 sconf->trailer_size = 0;
299 ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3;
302 // skip the header and trailer data
303 if (get_bits_left(&gb) < ht_size)
304 return -1;
306 if (ht_size > INT32_MAX)
307 return -1;
309 skip_bits_long(&gb, ht_size);
312 // skip the crc data
313 if (crc_enabled) {
314 if (get_bits_left(&gb) < 32)
315 return -1;
317 skip_bits_long(&gb, 32);
321 // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
323 dprint_specific_config(ctx);
325 return 0;
329 /** Checks the ALSSpecificConfig for unsupported features.
331 static int check_specific_config(ALSDecContext *ctx)
333 ALSSpecificConfig *sconf = &ctx->sconf;
334 int error = 0;
336 // report unsupported feature and set error value
337 #define MISSING_ERR(cond, str, errval) \
339 if (cond) { \
340 av_log_missing_feature(ctx->avctx, str, 0); \
341 error = errval; \
345 MISSING_ERR(sconf->floating, "Floating point decoding", -1);
346 MISSING_ERR(sconf->bgmc, "BGMC entropy decoding", -1);
347 MISSING_ERR(sconf->mc_coding, "Multi-channel correlation", -1);
348 MISSING_ERR(sconf->rlslms, "Adaptive RLS-LMS prediction", -1);
349 MISSING_ERR(sconf->chan_sort, "Channel sorting", 0);
351 return error;
355 /** Parses the bs_info field to extract the block partitioning used in
356 * block switching mode, refer to ISO/IEC 14496-3, section 11.6.2.
358 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
359 unsigned int div, unsigned int **div_blocks,
360 unsigned int *num_blocks)
362 if (n < 31 && ((bs_info << n) & 0x40000000)) {
363 // if the level is valid and the investigated bit n is set
364 // then recursively check both children at bits (2n+1) and (2n+2)
365 n *= 2;
366 div += 1;
367 parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
368 parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
369 } else {
370 // else the bit is not set or the last level has been reached
371 // (bit implicitly not set)
372 **div_blocks = div;
373 (*div_blocks)++;
374 (*num_blocks)++;
379 /** Reads and decodes a Rice codeword.
381 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
383 int max = get_bits_left(gb) - k;
384 int q = get_unary(gb, 0, max);
385 int r = k ? get_bits1(gb) : !(q & 1);
387 if (k > 1) {
388 q <<= (k - 1);
389 q += get_bits_long(gb, k - 1);
390 } else if (!k) {
391 q >>= 1;
393 return r ? q : ~q;
397 /** Converts PARCOR coefficient k to direct filter coefficient.
399 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
401 int i, j;
403 for (i = 0, j = k - 1; i < j; i++, j--) {
404 int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
405 cof[j] += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
406 cof[i] += tmp1;
408 if (i == j)
409 cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
411 cof[k] = par[k];
415 /** Reads block switching field if necessary and sets actual block sizes.
416 * Also assures that the block sizes of the last frame correspond to the
417 * actual number of samples.
419 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
420 uint32_t *bs_info)
422 ALSSpecificConfig *sconf = &ctx->sconf;
423 GetBitContext *gb = &ctx->gb;
424 unsigned int *ptr_div_blocks = div_blocks;
425 unsigned int b;
427 if (sconf->block_switching) {
428 unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
429 *bs_info = get_bits_long(gb, bs_info_len);
430 *bs_info <<= (32 - bs_info_len);
433 ctx->num_blocks = 0;
434 parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
436 // The last frame may have an overdetermined block structure given in
437 // the bitstream. In that case the defined block structure would need
438 // more samples than available to be consistent.
439 // The block structure is actually used but the block sizes are adapted
440 // to fit the actual number of available samples.
441 // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
442 // This results in the actual block sizes: 2 2 1 0.
443 // This is not specified in 14496-3 but actually done by the reference
444 // codec RM22 revision 2.
445 // This appears to happen in case of an odd number of samples in the last
446 // frame which is actually not allowed by the block length switching part
447 // of 14496-3.
448 // The ALS conformance files feature an odd number of samples in the last
449 // frame.
451 for (b = 0; b < ctx->num_blocks; b++)
452 div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
454 if (ctx->cur_frame_length != ctx->sconf.frame_length) {
455 unsigned int remaining = ctx->cur_frame_length;
457 for (b = 0; b < ctx->num_blocks; b++) {
458 if (remaining < div_blocks[b]) {
459 div_blocks[b] = remaining;
460 ctx->num_blocks = b + 1;
461 break;
464 remaining -= div_blocks[b];
470 /** Reads the block data for a constant block
472 static void read_const_block(ALSDecContext *ctx, int32_t *raw_samples,
473 unsigned int block_length, unsigned int *js_blocks)
475 ALSSpecificConfig *sconf = &ctx->sconf;
476 AVCodecContext *avctx = ctx->avctx;
477 GetBitContext *gb = &ctx->gb;
478 int32_t const_val = 0;
479 unsigned int const_block, k;
481 const_block = get_bits1(gb); // 1 = constant value, 0 = zero block (silence)
482 *js_blocks = get_bits1(gb);
484 // skip 5 reserved bits
485 skip_bits(gb, 5);
487 if (const_block) {
488 unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
489 const_val = get_sbits_long(gb, const_val_bits);
492 // write raw samples into buffer
493 for (k = 0; k < block_length; k++)
494 raw_samples[k] = const_val;
498 /** Reads the block data for a non-constant block
500 static int read_var_block(ALSDecContext *ctx, unsigned int ra_block,
501 int32_t *raw_samples, unsigned int block_length,
502 unsigned int *js_blocks, int32_t *raw_other,
503 unsigned int *shift_lsbs)
505 ALSSpecificConfig *sconf = &ctx->sconf;
506 AVCodecContext *avctx = ctx->avctx;
507 GetBitContext *gb = &ctx->gb;
508 unsigned int k;
509 unsigned int s[8];
510 unsigned int sub_blocks, log2_sub_blocks, sb_length;
511 unsigned int opt_order = 1;
512 int32_t *quant_cof = ctx->quant_cof;
513 int32_t *lpc_cof = ctx->lpc_cof;
514 unsigned int start = 0;
515 int smp = 0;
516 int sb, store_prev_samples;
517 int64_t y;
518 int use_ltp = 0;
519 int ltp_lag = 0;
520 int ltp_gain[5];
522 *js_blocks = get_bits1(gb);
524 // determine the number of subblocks for entropy decoding
525 if (!sconf->bgmc && !sconf->sb_part) {
526 log2_sub_blocks = 0;
527 } else {
528 if (sconf->bgmc && sconf->sb_part)
529 log2_sub_blocks = get_bits(gb, 2);
530 else
531 log2_sub_blocks = 2 * get_bits1(gb);
534 sub_blocks = 1 << log2_sub_blocks;
536 // do not continue in case of a damaged stream since
537 // block_length must be evenly divisible by sub_blocks
538 if (block_length & (sub_blocks - 1)) {
539 av_log(avctx, AV_LOG_WARNING,
540 "Block length is not evenly divisible by the number of subblocks.\n");
541 return -1;
544 sb_length = block_length >> log2_sub_blocks;
547 if (sconf->bgmc) {
548 // TODO: BGMC mode
549 } else {
550 s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
551 for (k = 1; k < sub_blocks; k++)
552 s[k] = s[k - 1] + decode_rice(gb, 0);
555 if (get_bits1(gb))
556 *shift_lsbs = get_bits(gb, 4) + 1;
558 store_prev_samples = (*js_blocks && raw_other) || *shift_lsbs;
561 if (!sconf->rlslms) {
562 if (sconf->adapt_order) {
563 int opt_order_length = av_ceil_log2(av_clip((block_length >> 3) - 1,
564 2, sconf->max_order + 1));
565 opt_order = get_bits(gb, opt_order_length);
566 } else {
567 opt_order = sconf->max_order;
570 if (opt_order) {
571 int add_base;
573 if (sconf->coef_table == 3) {
574 add_base = 0x7F;
576 // read coefficient 0
577 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
579 // read coefficient 1
580 if (opt_order > 1)
581 quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
583 // read coefficients 2 to opt_order
584 for (k = 2; k < opt_order; k++)
585 quant_cof[k] = get_bits(gb, 7);
586 } else {
587 int k_max;
588 add_base = 1;
590 // read coefficient 0 to 19
591 k_max = FFMIN(opt_order, 20);
592 for (k = 0; k < k_max; k++) {
593 int rice_param = parcor_rice_table[sconf->coef_table][k][1];
594 int offset = parcor_rice_table[sconf->coef_table][k][0];
595 quant_cof[k] = decode_rice(gb, rice_param) + offset;
598 // read coefficients 20 to 126
599 k_max = FFMIN(opt_order, 127);
600 for (; k < k_max; k++)
601 quant_cof[k] = decode_rice(gb, 2) + (k & 1);
603 // read coefficients 127 to opt_order
604 for (; k < opt_order; k++)
605 quant_cof[k] = decode_rice(gb, 1);
607 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
609 if (opt_order > 1)
610 quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
613 for (k = 2; k < opt_order; k++)
614 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
618 // read LTP gain and lag values
619 if (sconf->long_term_prediction) {
620 use_ltp = get_bits1(gb);
622 if (use_ltp) {
623 ltp_gain[0] = decode_rice(gb, 1) << 3;
624 ltp_gain[1] = decode_rice(gb, 2) << 3;
626 ltp_gain[2] = ltp_gain_values[get_unary(gb, 0, 4)][get_bits(gb, 2)];
628 ltp_gain[3] = decode_rice(gb, 2) << 3;
629 ltp_gain[4] = decode_rice(gb, 1) << 3;
631 ltp_lag = get_bits(gb, ctx->ltp_lag_length);
632 ltp_lag += FFMAX(4, opt_order + 1);
636 // read first value and residuals in case of a random access block
637 if (ra_block) {
638 if (opt_order)
639 raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
640 if (opt_order > 1)
641 raw_samples[1] = decode_rice(gb, s[0] + 3);
642 if (opt_order > 2)
643 raw_samples[2] = decode_rice(gb, s[0] + 1);
645 start = FFMIN(opt_order, 3);
648 // read all residuals
649 if (sconf->bgmc) {
650 // TODO: BGMC mode
651 } else {
652 int32_t *current_res = raw_samples + start;
654 for (sb = 0; sb < sub_blocks; sb++, start = 0)
655 for (; start < sb_length; start++)
656 *current_res++ = decode_rice(gb, s[sb]);
659 // reverse long-term prediction
660 if (use_ltp) {
661 int ltp_smp;
663 for (ltp_smp = FFMAX(ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
664 int center = ltp_smp - ltp_lag;
665 int begin = FFMAX(0, center - 2);
666 int end = center + 3;
667 int tab = 5 - (end - begin);
668 int base;
670 y = 1 << 6;
672 for (base = begin; base < end; base++, tab++)
673 y += MUL64(ltp_gain[tab], raw_samples[base]);
675 raw_samples[ltp_smp] += y >> 7;
679 // reconstruct all samples from residuals
680 if (ra_block) {
681 for (smp = 0; smp < opt_order; smp++) {
682 y = 1 << 19;
684 for (sb = 0; sb < smp; sb++)
685 y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
687 raw_samples[smp] -= y >> 20;
688 parcor_to_lpc(smp, quant_cof, lpc_cof);
690 } else {
691 for (k = 0; k < opt_order; k++)
692 parcor_to_lpc(k, quant_cof, lpc_cof);
694 // store previous samples in case that they have to be altered
695 if (store_prev_samples)
696 memcpy(ctx->prev_raw_samples, raw_samples - sconf->max_order,
697 sizeof(*ctx->prev_raw_samples) * sconf->max_order);
699 // reconstruct difference signal for prediction (joint-stereo)
700 if (*js_blocks && raw_other) {
701 int32_t *left, *right;
703 if (raw_other > raw_samples) { // D = R - L
704 left = raw_samples;
705 right = raw_other;
706 } else { // D = R - L
707 left = raw_other;
708 right = raw_samples;
711 for (sb = -1; sb >= -sconf->max_order; sb--)
712 raw_samples[sb] = right[sb] - left[sb];
715 // reconstruct shifted signal
716 if (*shift_lsbs)
717 for (sb = -1; sb >= -sconf->max_order; sb--)
718 raw_samples[sb] >>= *shift_lsbs;
721 // reconstruct raw samples
722 for (; smp < block_length; smp++) {
723 y = 1 << 19;
725 for (sb = 0; sb < opt_order; sb++)
726 y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
728 raw_samples[smp] -= y >> 20;
731 // restore previous samples in case that they have been altered
732 if (store_prev_samples)
733 memcpy(raw_samples - sconf->max_order, ctx->prev_raw_samples,
734 sizeof(*raw_samples) * sconf->max_order);
736 return 0;
740 /** Reads the block data.
742 static int read_block_data(ALSDecContext *ctx, unsigned int ra_block,
743 int32_t *raw_samples, unsigned int block_length,
744 unsigned int *js_blocks, int32_t *raw_other)
746 ALSSpecificConfig *sconf = &ctx->sconf;
747 GetBitContext *gb = &ctx->gb;
748 unsigned int shift_lsbs = 0;
749 unsigned int k;
751 // read block type flag and read the samples accordingly
752 if (get_bits1(gb)) {
753 if (read_var_block(ctx, ra_block, raw_samples, block_length, js_blocks,
754 raw_other, &shift_lsbs))
755 return -1;
756 } else {
757 read_const_block(ctx, raw_samples, block_length, js_blocks);
760 // TODO: read RLSLMS extension data
762 if (!sconf->mc_coding || ctx->js_switch)
763 align_get_bits(gb);
765 if (shift_lsbs)
766 for (k = 0; k < block_length; k++)
767 raw_samples[k] <<= shift_lsbs;
769 return 0;
773 /** Computes the number of samples left to decode for the current frame and
774 * sets these samples to zero.
776 static void zero_remaining(unsigned int b, unsigned int b_max,
777 const unsigned int *div_blocks, int32_t *buf)
779 unsigned int count = 0;
781 while (b < b_max)
782 count += div_blocks[b];
784 if (count)
785 memset(buf, 0, sizeof(*buf) * count);
789 /** Decodes blocks independently.
791 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
792 unsigned int c, const unsigned int *div_blocks,
793 unsigned int *js_blocks)
795 int32_t *raw_sample;
796 unsigned int b;
797 raw_sample = ctx->raw_samples[c];
799 for (b = 0; b < ctx->num_blocks; b++) {
800 if (read_block_data(ctx, ra_frame, raw_sample,
801 div_blocks[b], &js_blocks[0], NULL)) {
802 // damaged block, write zero for the rest of the frame
803 zero_remaining(b, ctx->num_blocks, div_blocks, raw_sample);
804 return -1;
806 raw_sample += div_blocks[b];
807 ra_frame = 0;
810 return 0;
814 /** Decodes blocks dependently.
816 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
817 unsigned int c, const unsigned int *div_blocks,
818 unsigned int *js_blocks)
820 ALSSpecificConfig *sconf = &ctx->sconf;
821 unsigned int offset = 0;
822 int32_t *raw_samples_R;
823 int32_t *raw_samples_L;
824 unsigned int b;
826 // decode all blocks
827 for (b = 0; b < ctx->num_blocks; b++) {
828 unsigned int s;
829 raw_samples_L = ctx->raw_samples[c ] + offset;
830 raw_samples_R = ctx->raw_samples[c + 1] + offset;
831 if (read_block_data(ctx, ra_frame, raw_samples_L, div_blocks[b],
832 &js_blocks[0], raw_samples_R) ||
833 read_block_data(ctx, ra_frame, raw_samples_R, div_blocks[b],
834 &js_blocks[1], raw_samples_L)) {
835 // damaged block, write zero for the rest of the frame
836 zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_L);
837 zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_R);
838 return -1;
841 // reconstruct joint-stereo blocks
842 if (js_blocks[0]) {
843 if (js_blocks[1])
844 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
846 for (s = 0; s < div_blocks[b]; s++)
847 raw_samples_L[s] = raw_samples_R[s] - raw_samples_L[s];
848 } else if (js_blocks[1]) {
849 for (s = 0; s < div_blocks[b]; s++)
850 raw_samples_R[s] = raw_samples_R[s] + raw_samples_L[s];
853 offset += div_blocks[b];
854 ra_frame = 0;
857 // store carryover raw samples,
858 // the others channel raw samples are stored by the calling function.
859 memmove(ctx->raw_samples[c] - sconf->max_order,
860 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
861 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
863 return 0;
867 /** Reads the frame data.
869 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
871 ALSSpecificConfig *sconf = &ctx->sconf;
872 AVCodecContext *avctx = ctx->avctx;
873 GetBitContext *gb = &ctx->gb;
874 unsigned int div_blocks[32]; ///< block sizes.
875 unsigned int c;
876 unsigned int js_blocks[2];
878 uint32_t bs_info = 0;
880 // skip the size of the ra unit if present in the frame
881 if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
882 skip_bits_long(gb, 32);
884 if (sconf->mc_coding && sconf->joint_stereo) {
885 ctx->js_switch = get_bits1(gb);
886 align_get_bits(gb);
889 if (!sconf->mc_coding || ctx->js_switch) {
890 int independent_bs = !sconf->joint_stereo;
892 for (c = 0; c < avctx->channels; c++) {
893 js_blocks[0] = 0;
894 js_blocks[1] = 0;
896 get_block_sizes(ctx, div_blocks, &bs_info);
898 // if joint_stereo and block_switching is set, independent decoding
899 // is signaled via the first bit of bs_info
900 if (sconf->joint_stereo && sconf->block_switching)
901 if (bs_info >> 31)
902 independent_bs = 2;
904 // if this is the last channel, it has to be decoded independently
905 if (c == avctx->channels - 1)
906 independent_bs = 1;
908 if (independent_bs) {
909 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
910 return -1;
912 independent_bs--;
913 } else {
914 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
915 return -1;
917 c++;
920 // store carryover raw samples
921 memmove(ctx->raw_samples[c] - sconf->max_order,
922 ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
923 sizeof(*ctx->raw_samples[c]) * sconf->max_order);
925 } else { // multi-channel coding
926 get_block_sizes(ctx, div_blocks, &bs_info);
928 // TODO: multi channel coding might use a temporary buffer instead as
929 // the actual channel is not known when read_block-data is called
930 if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks))
931 return -1;
932 // TODO: read_channel_data
935 // TODO: read_diff_float_data
937 return 0;
941 /** Decodes an ALS frame.
943 static int decode_frame(AVCodecContext *avctx,
944 void *data, int *data_size,
945 AVPacket *avpkt)
947 ALSDecContext *ctx = avctx->priv_data;
948 ALSSpecificConfig *sconf = &ctx->sconf;
949 const uint8_t *buffer = avpkt->data;
950 int buffer_size = avpkt->size;
951 int invalid_frame, size;
952 unsigned int c, sample, ra_frame, bytes_read, shift;
954 init_get_bits(&ctx->gb, buffer, buffer_size * 8);
956 // In the case that the distance between random access frames is set to zero
957 // (sconf->ra_distance == 0) no frame is treated as a random access frame.
958 // For the first frame, if prediction is used, all samples used from the
959 // previous frame are assumed to be zero.
960 ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
962 // the last frame to decode might have a different length
963 if (sconf->samples != 0xFFFFFFFF)
964 ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
965 sconf->frame_length);
966 else
967 ctx->cur_frame_length = sconf->frame_length;
969 // decode the frame data
970 if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
971 av_log(ctx->avctx, AV_LOG_WARNING,
972 "Reading frame data failed. Skipping RA unit.\n");
974 ctx->frame_id++;
976 // check for size of decoded data
977 size = ctx->cur_frame_length * avctx->channels *
978 (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3);
980 if (size > *data_size) {
981 av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
982 return -1;
985 *data_size = size;
987 // transform decoded frame into output format
988 #define INTERLEAVE_OUTPUT(bps) \
990 int##bps##_t *dest = (int##bps##_t*) data; \
991 shift = bps - ctx->avctx->bits_per_raw_sample; \
992 for (sample = 0; sample < ctx->cur_frame_length; sample++) \
993 for (c = 0; c < avctx->channels; c++) \
994 *dest++ = ctx->raw_samples[c][sample] << shift; \
997 if (ctx->avctx->bits_per_raw_sample <= 16) {
998 INTERLEAVE_OUTPUT(16)
999 } else {
1000 INTERLEAVE_OUTPUT(32)
1003 bytes_read = invalid_frame ? buffer_size :
1004 (get_bits_count(&ctx->gb) + 7) >> 3;
1006 return bytes_read;
1010 /** Uninitializes the ALS decoder.
1012 static av_cold int decode_end(AVCodecContext *avctx)
1014 ALSDecContext *ctx = avctx->priv_data;
1016 av_freep(&ctx->sconf.chan_pos);
1018 av_freep(&ctx->quant_cof);
1019 av_freep(&ctx->lpc_cof);
1020 av_freep(&ctx->prev_raw_samples);
1021 av_freep(&ctx->raw_samples);
1022 av_freep(&ctx->raw_buffer);
1024 return 0;
1028 /** Initializes the ALS decoder.
1030 static av_cold int decode_init(AVCodecContext *avctx)
1032 unsigned int c;
1033 unsigned int channel_size;
1034 ALSDecContext *ctx = avctx->priv_data;
1035 ALSSpecificConfig *sconf = &ctx->sconf;
1036 ctx->avctx = avctx;
1038 if (!avctx->extradata) {
1039 av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
1040 return -1;
1043 if (read_specific_config(ctx)) {
1044 av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
1045 decode_end(avctx);
1046 return -1;
1049 if (check_specific_config(ctx)) {
1050 decode_end(avctx);
1051 return -1;
1054 if (sconf->floating) {
1055 avctx->sample_fmt = SAMPLE_FMT_FLT;
1056 avctx->bits_per_raw_sample = 32;
1057 } else {
1058 avctx->sample_fmt = sconf->resolution > 1
1059 ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
1060 avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
1063 // set lag value for long-term prediction
1064 ctx->ltp_lag_length = 8 + (avctx->sample_rate >= 96000) +
1065 (avctx->sample_rate >= 192000);
1067 avctx->frame_size = sconf->frame_length;
1068 channel_size = sconf->frame_length + sconf->max_order;
1070 ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
1071 ctx->raw_buffer = av_mallocz(sizeof(*ctx-> raw_buffer) * avctx->channels * channel_size);
1072 ctx->raw_samples = av_malloc (sizeof(*ctx-> raw_samples) * avctx->channels);
1074 // allocate previous raw sample buffer
1075 if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
1076 av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
1077 decode_end(avctx);
1078 return AVERROR(ENOMEM);
1081 // assign raw samples buffers
1082 ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
1083 for (c = 1; c < avctx->channels; c++)
1084 ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
1086 return 0;
1090 /** Flushes (resets) the frame ID after seeking.
1092 static av_cold void flush(AVCodecContext *avctx)
1094 ALSDecContext *ctx = avctx->priv_data;
1096 ctx->frame_id = 0;
1100 AVCodec als_decoder = {
1101 "als",
1102 CODEC_TYPE_AUDIO,
1103 CODEC_ID_MP4ALS,
1104 sizeof(ALSDecContext),
1105 decode_init,
1106 NULL,
1107 decode_end,
1108 decode_frame,
1109 .flush = flush,
1110 .capabilities = CODEC_CAP_SUBFRAMES,
1111 .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),