]> git.sesse.net Git - ffmpeg/blob - libavcodec/alsdec.c
Add MPEG-4 Audio Lossless Coding (ALS) decoder.
[ffmpeg] / libavcodec / alsdec.c
1 /*
2  * MPEG-4 ALS decoder
3  * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file libavcodec/alsdec.c
24  * MPEG-4 ALS decoder
25  * @author Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
26  */
27
28
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
38 #include "als_data.h"
39
40 enum RA_Flag {
41     RA_FLAG_NONE,
42     RA_FLAG_FRAMES,
43     RA_FLAG_HEADER
44 };
45
46
47 typedef struct {
48     uint32_t samples;         ///< number of samples, 0xFFFFFFFF if unknown
49     int resolution;           ///< 000 = 8-bit; 001 = 16-bit; 010 = 24-bit; 011 = 32-bit
50     int floating;             ///< 1 = IEEE 32-bit floating-point, 0 = integer
51     int frame_length;         ///< frame length for each frame (last frame may differ)
52     int ra_distance;          ///< distance between RA frames (in frames, 0...255)
53     enum RA_Flag ra_flag;     ///< indicates where the size of ra units is stored
54     int adapt_order;          ///< adaptive order: 1 = on, 0 = off
55     int coef_table;           ///< table index of Rice code parameters
56     int long_term_prediction; ///< long term prediction (LTP): 1 = on, 0 = off
57     int max_order;            ///< maximum prediction order (0..1023)
58     int block_switching;      ///< number of block switching levels
59     int bgmc;                 ///< "Block Gilbert-Moore Code": 1 = on, 0 = off (Rice coding only)
60     int sb_part;              ///< sub-block partition
61     int joint_stereo;         ///< joint stereo: 1 = on, 0 = off
62     int mc_coding;            ///< extended inter-channel coding (multi channel coding): 1 = on, 0 = off
63     int chan_config;          ///< indicates that a chan_config_info field is present
64     int chan_sort;            ///< channel rearrangement: 1 = on, 0 = off
65     int rlslms;               ///< use "Recursive Least Square-Least Mean Square" predictor: 1 = on, 0 = off
66     int chan_config_info;     ///< mapping of channels to loudspeaker locations. Unused until setting channel configuration is implemented.
67     int *chan_pos;            ///< original channel positions
68     uint32_t header_size;     ///< header size of original audio file in bytes, provided for debugging
69     uint32_t trailer_size;    ///< trailer size of original audio file in bytes, provided for debugging
70 } ALSSpecificConfig;
71
72
73 typedef struct {
74     AVCodecContext *avctx;
75     ALSSpecificConfig sconf;
76     GetBitContext gb;
77     unsigned int cur_frame_length;  ///< length of the current frame to decode
78     unsigned int frame_id;          ///< the frame ID / number of the current frame
79     unsigned int js_switch;         ///< if true, joint-stereo decoding is enforced
80     unsigned int num_blocks;        ///< number of blocks used in the current frame
81     int32_t *quant_cof;             ///< quantized parcor coefficients
82     int32_t *lpc_cof;               ///< coefficients of the direct form prediction filter
83     int32_t *prev_raw_samples;      ///< contains unshifted raw samples from the previous block
84     int32_t **raw_samples;          ///< decoded raw samples for each channel
85     int32_t *raw_buffer;            ///< contains all decoded raw samples including carryover samples
86 } ALSDecContext;
87
88
89 static av_cold void dprint_specific_config(ALSDecContext *ctx)
90 {
91 #ifdef DEBUG
92     AVCodecContext *avctx    = ctx->avctx;
93     ALSSpecificConfig *sconf = &ctx->sconf;
94
95     dprintf(avctx, "resolution = %i\n",           sconf->resolution);
96     dprintf(avctx, "floating = %i\n",             sconf->floating);
97     dprintf(avctx, "frame_length = %i\n",         sconf->frame_length);
98     dprintf(avctx, "ra_distance = %i\n",          sconf->ra_distance);
99     dprintf(avctx, "ra_flag = %i\n",              sconf->ra_flag);
100     dprintf(avctx, "adapt_order = %i\n",          sconf->adapt_order);
101     dprintf(avctx, "coef_table = %i\n",           sconf->coef_table);
102     dprintf(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
103     dprintf(avctx, "max_order = %i\n",            sconf->max_order);
104     dprintf(avctx, "block_switching = %i\n",      sconf->block_switching);
105     dprintf(avctx, "bgmc = %i\n",                 sconf->bgmc);
106     dprintf(avctx, "sb_part = %i\n",              sconf->sb_part);
107     dprintf(avctx, "joint_stereo = %i\n",         sconf->joint_stereo);
108     dprintf(avctx, "mc_coding = %i\n",            sconf->mc_coding);
109     dprintf(avctx, "chan_config = %i\n",          sconf->chan_config);
110     dprintf(avctx, "chan_sort = %i\n",            sconf->chan_sort);
111     dprintf(avctx, "RLSLMS = %i\n",               sconf->rlslms);
112     dprintf(avctx, "chan_config_info = %i\n",     sconf->chan_config_info);
113     dprintf(avctx, "header_size = %i\n",          sconf->header_size);
114     dprintf(avctx, "trailer_size = %i\n",         sconf->trailer_size);
115 #endif
116 }
117
118
119 /** Reads an ALSSpecificConfig from a buffer into the output struct.
120  */
121 static av_cold int read_specific_config(ALSDecContext *ctx)
122 {
123     GetBitContext gb;
124     uint64_t ht_size;
125     int i, config_offset, crc_enabled;
126     MPEG4AudioConfig m4ac;
127     ALSSpecificConfig *sconf = &ctx->sconf;
128     AVCodecContext *avctx    = ctx->avctx;
129     uint32_t als_id;
130
131     init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
132
133     config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
134                                              avctx->extradata_size);
135
136     if (config_offset < 0)
137         return -1;
138
139     skip_bits_long(&gb, config_offset);
140
141     if (get_bits_left(&gb) < (30 << 3))
142         return -1;
143
144     // read the fixed items
145     als_id                      = get_bits_long(&gb, 32);
146     avctx->sample_rate          = m4ac.sample_rate;
147     skip_bits_long(&gb, 32); // sample rate already known
148     sconf->samples              = get_bits_long(&gb, 32);
149     avctx->channels             = m4ac.channels;
150     skip_bits(&gb, 16);      // number of channels already knwon
151     skip_bits(&gb, 3);       // skip file_type
152     sconf->resolution           = get_bits(&gb, 3);
153     sconf->floating             = get_bits1(&gb);
154     skip_bits1(&gb);         // skip msb_first
155     sconf->frame_length         = get_bits(&gb, 16) + 1;
156     sconf->ra_distance          = get_bits(&gb, 8);
157     sconf->ra_flag              = get_bits(&gb, 2);
158     sconf->adapt_order          = get_bits1(&gb);
159     sconf->coef_table           = get_bits(&gb, 2);
160     sconf->long_term_prediction = get_bits1(&gb);
161     sconf->max_order            = get_bits(&gb, 10);
162     sconf->block_switching      = get_bits(&gb, 2);
163     sconf->bgmc                 = get_bits1(&gb);
164     sconf->sb_part              = get_bits1(&gb);
165     sconf->joint_stereo         = get_bits1(&gb);
166     sconf->mc_coding            = get_bits1(&gb);
167     sconf->chan_config          = get_bits1(&gb);
168     sconf->chan_sort            = get_bits1(&gb);
169     crc_enabled                 = get_bits1(&gb);
170     sconf->rlslms               = get_bits1(&gb);
171     skip_bits(&gb, 5);       // skip 5 reserved bits
172     skip_bits1(&gb);         // skip aux_data_enabled
173
174
175     // check for ALSSpecificConfig struct
176     if (als_id != MKBETAG('A','L','S','\0'))
177         return -1;
178
179     ctx->cur_frame_length = sconf->frame_length;
180
181     // allocate quantized parcor coefficient buffer
182     if (!(ctx->quant_cof = av_malloc(sizeof(*ctx->quant_cof) * sconf->max_order)) ||
183         !(ctx->lpc_cof   = av_malloc(sizeof(*ctx->lpc_cof)   * sconf->max_order))) {
184         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
185         return AVERROR(ENOMEM);
186     }
187
188     // read channel config
189     if (sconf->chan_config)
190         sconf->chan_config_info = get_bits(&gb, 16);
191     // TODO: use this to set avctx->channel_layout
192
193
194     // read channel sorting
195     if (sconf->chan_sort && avctx->channels > 1) {
196         int chan_pos_bits = av_ceil_log2(avctx->channels);
197         int bits_needed  = avctx->channels * chan_pos_bits + 7;
198         if (get_bits_left(&gb) < bits_needed)
199             return -1;
200
201         if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
202             return AVERROR(ENOMEM);
203
204         for (i = 0; i < avctx->channels; i++)
205             sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
206
207         align_get_bits(&gb);
208         // TODO: use this to actually do channel sorting
209     } else {
210         sconf->chan_sort = 0;
211     }
212
213
214     // read fixed header and trailer sizes,
215     // if size = 0xFFFFFFFF then there is no data field!
216     if (get_bits_left(&gb) < 64)
217         return -1;
218
219     sconf->header_size  = get_bits_long(&gb, 32);
220     sconf->trailer_size = get_bits_long(&gb, 32);
221     if (sconf->header_size  == 0xFFFFFFFF)
222         sconf->header_size  = 0;
223     if (sconf->trailer_size == 0xFFFFFFFF)
224         sconf->trailer_size = 0;
225
226     ht_size = ((int64_t)(sconf->header_size) + (int64_t)(sconf->trailer_size)) << 3;
227
228
229     // skip the header and trailer data
230     if (get_bits_left(&gb) < ht_size)
231         return -1;
232
233     if (ht_size > INT32_MAX)
234         return -1;
235
236     skip_bits_long(&gb, ht_size);
237
238
239     // skip the crc data
240     if (crc_enabled) {
241         if (get_bits_left(&gb) < 32)
242             return -1;
243
244         skip_bits_long(&gb, 32);
245     }
246
247
248     // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
249
250     dprint_specific_config(ctx);
251
252     return 0;
253 }
254
255
256 /** Checks the ALSSpecificConfig for unsupported features.
257  */
258 static int check_specific_config(ALSDecContext *ctx)
259 {
260     ALSSpecificConfig *sconf = &ctx->sconf;
261     int error = 0;
262
263     // report unsupported feature and set error value
264     #define MISSING_ERR(cond, str, errval)              \
265     {                                                   \
266         if (cond) {                                     \
267             av_log_missing_feature(ctx->avctx, str, 0); \
268             error = errval;                             \
269         }                                               \
270     }
271
272     MISSING_ERR(sconf->floating,             "Floating point decoding",     -1);
273     MISSING_ERR(sconf->long_term_prediction, "Long-term prediction",        -1);
274     MISSING_ERR(sconf->bgmc,                 "BGMC entropy decoding",       -1);
275     MISSING_ERR(sconf->mc_coding,            "Multi-channel correlation",   -1);
276     MISSING_ERR(sconf->rlslms,               "Adaptive RLS-LMS prediction", -1);
277     MISSING_ERR(sconf->chan_sort,            "Channel sorting",              0);
278
279     return error;
280 }
281
282
283 /** Parses the bs_info field to extract the block partitioning used in
284  *  block switching mode, refer to ISO/IEC 14496-3, section 11.6.2.
285  */
286 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
287                           unsigned int div, unsigned int **div_blocks,
288                           unsigned int *num_blocks)
289 {
290     if (n < 31 && ((bs_info << n) & 0x40000000)) {
291         // if the level is valid and the investigated bit n is set
292         // then recursively check both children at bits (2n+1) and (2n+2)
293         n   *= 2;
294         div += 1;
295         parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
296         parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
297     } else {
298         // else the bit is not set or the last level has been reached
299         // (bit implicitly not set)
300         **div_blocks = div;
301         (*div_blocks)++;
302         (*num_blocks)++;
303     }
304 }
305
306
307 /** Reads and decodes a Rice codeword.
308  */
309 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
310 {
311     int max = gb->size_in_bits - get_bits_count(gb) - k;
312     int q   = get_unary(gb, 0, max);
313     int r   = k ? get_bits1(gb) : !(q & 1);
314
315     if (k > 1) {
316         q <<= (k - 1);
317         q  += get_bits_long(gb, k - 1);
318     } else if (!k) {
319         q >>= 1;
320     }
321     return r ? q : ~q;
322 }
323
324
325 /** Converts PARCOR coefficient k to direct filter coefficient.
326  */
327 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
328 {
329     int i, j;
330
331     for (i = 0, j = k - 1; i < j; i++, j--) {
332         int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
333         cof[j]  += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
334         cof[i]  += tmp1;
335     }
336     if (i == j)
337         cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
338
339     cof[k] = par[k];
340 }
341
342
343 /** Reads block switching field if necessary and sets actual block sizes.
344  *  Also assures that the block sizes of the last frame correspond to the
345  *  actual number of samples.
346  */
347 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
348                             uint32_t *bs_info)
349 {
350     ALSSpecificConfig *sconf     = &ctx->sconf;
351     GetBitContext *gb            = &ctx->gb;
352     unsigned int *ptr_div_blocks = div_blocks;
353     unsigned int b;
354
355     if (sconf->block_switching) {
356         unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
357         *bs_info = get_bits_long(gb, bs_info_len);
358         *bs_info <<= (32 - bs_info_len);
359     }
360
361     ctx->num_blocks = 0;
362     parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
363
364     // The last frame may have an overdetermined block structure given in
365     // the bitstream. In that case the defined block structure would need
366     // more samples than available to be consistent.
367     // The block structure is actually used but the block sizes are adapted
368     // to fit the actual number of available samples.
369     // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
370     // This results in the actual block sizes:    2 2 1 0.
371     // This is not specified in 14496-3 but actually done by the reference
372     // codec RM22 revision 2.
373     // This appears to happen in case of an odd number of samples in the last
374     // frame which is actually not allowed by the block length switching part
375     // of 14496-3.
376     // The ALS conformance files feature an odd number of samples in the last
377     // frame.
378
379     for (b = 0; b < ctx->num_blocks; b++)
380         div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
381
382     if (ctx->cur_frame_length != ctx->sconf.frame_length) {
383         unsigned int remaining = ctx->cur_frame_length;
384
385         for (b = 0; b < ctx->num_blocks; b++) {
386             if (remaining < div_blocks[b]) {
387                 div_blocks[b] = remaining;
388                 ctx->num_blocks = b + 1;
389                 break;
390             }
391
392             remaining -= div_blocks[b];
393         }
394     }
395 }
396
397
398 /** Reads the block data for a constant block
399  */
400 static void read_const_block(ALSDecContext *ctx, int32_t *raw_samples,
401                              unsigned int block_length, unsigned int *js_blocks)
402 {
403     ALSSpecificConfig *sconf = &ctx->sconf;
404     AVCodecContext *avctx    = ctx->avctx;
405     GetBitContext *gb        = &ctx->gb;
406     int32_t const_val        = 0;
407     unsigned int const_block, k;
408
409     const_block  = get_bits1(gb);    // 1 = constant value, 0 = zero block (silence)
410     *js_blocks   = get_bits1(gb);
411
412     // skip 5 reserved bits
413     skip_bits(gb, 5);
414
415     if (const_block) {
416         unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
417         const_val = get_sbits_long(gb, const_val_bits);
418     }
419
420     // write raw samples into buffer
421     for (k = 0; k < block_length; k++)
422         raw_samples[k] = const_val;
423 }
424
425
426 /** Reads the block data for a non-constant block
427  */
428 static int read_var_block(ALSDecContext *ctx, unsigned int ra_block,
429                           int32_t *raw_samples, unsigned int block_length,
430                           unsigned int *js_blocks, int32_t *raw_other,
431                           unsigned int *shift_lsbs)
432 {
433     ALSSpecificConfig *sconf = &ctx->sconf;
434     AVCodecContext *avctx    = ctx->avctx;
435     GetBitContext *gb        = &ctx->gb;
436     unsigned int k;
437     unsigned int s[8];
438     unsigned int sub_blocks, log2_sub_blocks, sb_length;
439     unsigned int opt_order  = 1;
440     int32_t      *quant_cof = ctx->quant_cof;
441     int32_t      *lpc_cof   = ctx->lpc_cof;
442     unsigned int start      = 0;
443     int          smp        = 0;
444     int          sb, store_prev_samples;
445     int64_t      y;
446
447     *js_blocks  = get_bits1(gb);
448
449     // determine the number of subblocks for entropy decoding
450     if (!sconf->bgmc && !sconf->sb_part) {
451         log2_sub_blocks = 0;
452     } else {
453         if (sconf->bgmc && sconf->sb_part)
454             log2_sub_blocks = get_bits(gb, 2);
455         else
456             log2_sub_blocks = 2 * get_bits1(gb);
457     }
458
459     sub_blocks = 1 << log2_sub_blocks;
460
461     // do not continue in case of a damaged stream since
462     // block_length must be evenly divisible by sub_blocks
463     if (block_length & (sub_blocks - 1)) {
464         av_log(avctx, AV_LOG_WARNING,
465                "Block length is not evenly divisible by the number of subblocks.\n");
466         return -1;
467     }
468
469     sb_length = block_length >> log2_sub_blocks;
470
471
472     if (sconf->bgmc) {
473         // TODO: BGMC mode
474     } else {
475         s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
476         for (k = 1; k < sub_blocks; k++)
477             s[k] = s[k - 1] + decode_rice(gb, 0);
478     }
479
480     if (get_bits1(gb))
481         *shift_lsbs = get_bits(gb, 4) + 1;
482
483     store_prev_samples = (*js_blocks && raw_other) || *shift_lsbs;
484
485
486     if (!sconf->rlslms) {
487         if (sconf->adapt_order) {
488             int opt_order_length = av_ceil_log2(av_clip((block_length >> 3) - 1,
489                                                 2, sconf->max_order + 1));
490             opt_order            = get_bits(gb, opt_order_length);
491         } else {
492             opt_order = sconf->max_order;
493         }
494
495         if (opt_order) {
496             int add_base;
497
498             if (sconf->coef_table == 3) {
499                 add_base = 0x7F;
500
501                 // read coefficient 0
502                 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
503
504                 // read coefficient 1
505                 if (opt_order > 1)
506                     quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
507
508                 // read coefficients 2 to opt_order
509                 for (k = 2; k < opt_order; k++)
510                     quant_cof[k] = get_bits(gb, 7);
511             } else {
512                 int k_max;
513                 add_base = 1;
514
515                 // read coefficient 0 to 19
516                 k_max = FFMIN(opt_order, 20);
517                 for (k = 0; k < k_max; k++) {
518                     int rice_param = parcor_rice_table[sconf->coef_table][k][1];
519                     int offset     = parcor_rice_table[sconf->coef_table][k][0];
520                     quant_cof[k] = decode_rice(gb, rice_param) + offset;
521                 }
522
523                 // read coefficients 20 to 126
524                 k_max = FFMIN(opt_order, 127);
525                 for (; k < k_max; k++)
526                     quant_cof[k] = decode_rice(gb, 2) + (k & 1);
527
528                 // read coefficients 127 to opt_order
529                 for (; k < opt_order; k++)
530                     quant_cof[k] = decode_rice(gb, 1);
531
532                 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
533
534                 if (opt_order > 1)
535                     quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
536             }
537
538             for (k = 2; k < opt_order; k++)
539                 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
540         }
541     }
542
543     // TODO: LTP mode
544
545     // read first value and residuals in case of a random access block
546     if (ra_block) {
547         if (opt_order)
548             raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
549         if (opt_order > 1)
550             raw_samples[1] = decode_rice(gb, s[0] + 3);
551         if (opt_order > 2)
552             raw_samples[2] = decode_rice(gb, s[0] + 1);
553
554         start = FFMIN(opt_order, 3);
555     }
556
557     // read all residuals
558     if (sconf->bgmc) {
559         // TODO: BGMC mode
560     } else {
561         int32_t *current_res = raw_samples + start;
562
563         for (sb = 0; sb < sub_blocks; sb++, start = 0)
564             for (; start < sb_length; start++)
565                 *current_res++ = decode_rice(gb, s[sb]);
566      }
567
568     // reconstruct all samples from residuals
569     if (ra_block) {
570         for (smp = 0; smp < opt_order; smp++) {
571             y = 1 << 19;
572
573             for (sb = 0; sb < smp; sb++)
574                 y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
575
576             raw_samples[smp] -= y >> 20;
577             parcor_to_lpc(smp, quant_cof, lpc_cof);
578         }
579     } else {
580         for (k = 0; k < opt_order; k++)
581             parcor_to_lpc(k, quant_cof, lpc_cof);
582
583         // store previous samples in case that they have to be altered
584         if (store_prev_samples)
585             memcpy(ctx->prev_raw_samples, raw_samples - sconf->max_order,
586                    sizeof(*ctx->prev_raw_samples) * sconf->max_order);
587
588         // reconstruct difference signal for prediction (joint-stereo)
589         if (*js_blocks && raw_other) {
590             int32_t *left, *right;
591
592             if (raw_other > raw_samples) {          // D = R - L
593                 left  = raw_samples;
594                 right = raw_other;
595             } else {                                // D = R - L
596                 left  = raw_other;
597                 right = raw_samples;
598             }
599
600             for (sb = -1; sb >= -sconf->max_order; sb--)
601                 raw_samples[sb] = right[sb] - left[sb];
602         }
603
604         // reconstruct shifted signal
605         if (*shift_lsbs)
606             for (sb = -1; sb >= -sconf->max_order; sb--)
607                 raw_samples[sb] >>= *shift_lsbs;
608     }
609
610     // reconstruct raw samples
611     for (; smp < block_length; smp++) {
612         y = 1 << 19;
613
614         for (sb = 0; sb < opt_order; sb++)
615             y += MUL64(lpc_cof[sb],raw_samples[smp - (sb + 1)]);
616
617         raw_samples[smp] -= y >> 20;
618     }
619
620     // restore previous samples in case that they have been altered
621     if (store_prev_samples)
622         memcpy(raw_samples - sconf->max_order, ctx->prev_raw_samples,
623                sizeof(*raw_samples) * sconf->max_order);
624
625     return 0;
626 }
627
628
629 /** Reads the block data.
630  */
631 static int read_block_data(ALSDecContext *ctx, unsigned int ra_block,
632                            int32_t *raw_samples, unsigned int block_length,
633                            unsigned int *js_blocks, int32_t *raw_other)
634 {
635     ALSSpecificConfig *sconf = &ctx->sconf;
636     GetBitContext *gb        = &ctx->gb;
637     unsigned int shift_lsbs  = 0;
638     unsigned int k;
639
640     // read block type flag and read the samples accordingly
641     if (get_bits1(gb)) {
642         if (read_var_block(ctx, ra_block, raw_samples, block_length, js_blocks,
643                            raw_other, &shift_lsbs))
644             return -1;
645     } else {
646         read_const_block(ctx, raw_samples, block_length, js_blocks);
647     }
648
649     // TODO: read RLSLMS extension data
650
651     if (!sconf->mc_coding || ctx->js_switch)
652         align_get_bits(gb);
653
654     if (shift_lsbs)
655         for (k = 0; k < block_length; k++)
656             raw_samples[k] <<= shift_lsbs;
657
658     return 0;
659 }
660
661
662 /** Computes the number of samples left to decode for the current frame and
663  *  sets these samples to zero.
664  */
665 static void zero_remaining(unsigned int b, unsigned int b_max,
666                            const unsigned int *div_blocks, int32_t *buf)
667 {
668     unsigned int count = 0;
669
670     while (b < b_max)
671         count += div_blocks[b];
672
673     memset(buf, 0, sizeof(*buf) * count);
674 }
675
676
677 /** Decodes blocks independently.
678  */
679 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
680                              unsigned int c, const unsigned int *div_blocks,
681                              unsigned int *js_blocks)
682 {
683     int32_t *raw_sample;
684     unsigned int b;
685     raw_sample = ctx->raw_samples[c];
686
687     for (b = 0; b < ctx->num_blocks; b++) {
688         if (read_block_data(ctx, ra_frame, raw_sample,
689                             div_blocks[b], &js_blocks[0], NULL)) {
690             // damaged block, write zero for the rest of the frame
691             zero_remaining(b, ctx->num_blocks, div_blocks, raw_sample);
692             return -1;
693         }
694         raw_sample += div_blocks[b];
695         ra_frame    = 0;
696     }
697
698     return 0;
699 }
700
701
702 /** Decodes blocks dependently.
703  */
704 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
705                          unsigned int c, const unsigned int *div_blocks,
706                          unsigned int *js_blocks)
707 {
708     ALSSpecificConfig *sconf = &ctx->sconf;
709     unsigned int offset = 0;
710     int32_t *raw_samples_R;
711     int32_t *raw_samples_L;
712     unsigned int b;
713
714     // decode all blocks
715     for (b = 0; b < ctx->num_blocks; b++) {
716         unsigned int s;
717         raw_samples_L = ctx->raw_samples[c    ] + offset;
718         raw_samples_R = ctx->raw_samples[c + 1] + offset;
719         if (read_block_data(ctx, ra_frame, raw_samples_L, div_blocks[b],
720                             &js_blocks[0], raw_samples_R) ||
721             read_block_data(ctx, ra_frame, raw_samples_R, div_blocks[b],
722                             &js_blocks[1], raw_samples_L)) {
723             // damaged block, write zero for the rest of the frame
724             zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_L);
725             zero_remaining(b, ctx->num_blocks, div_blocks, raw_samples_R);
726             return -1;
727         }
728
729         // reconstruct joint-stereo blocks
730         if (js_blocks[0]) {
731             if (js_blocks[1])
732                 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
733
734             for (s = 0; s < div_blocks[b]; s++)
735                 raw_samples_L[s] = raw_samples_R[s] - raw_samples_L[s];
736         } else if (js_blocks[1]) {
737             for (s = 0; s < div_blocks[b]; s++)
738                 raw_samples_R[s] = raw_samples_R[s] + raw_samples_L[s];
739         }
740
741         offset  += div_blocks[b];
742         ra_frame = 0;
743     }
744
745     // store carryover raw samples,
746     // the others channel raw samples are stored by the calling function.
747     memmove(ctx->raw_samples[c] - sconf->max_order,
748             ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
749             sizeof(*ctx->raw_samples[c]) * sconf->max_order);
750
751     return 0;
752 }
753
754
755 /** Reads the frame data.
756  */
757 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
758 {
759     ALSSpecificConfig *sconf = &ctx->sconf;
760     AVCodecContext *avctx    = ctx->avctx;
761     GetBitContext *gb = &ctx->gb;
762     unsigned int div_blocks[32];                ///< block sizes.
763     unsigned int c;
764     unsigned int js_blocks[2];
765
766     uint32_t bs_info = 0;
767
768     // skip the size of the ra unit if present in the frame
769     if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
770         skip_bits_long(gb, 32);
771
772     if (sconf->mc_coding && sconf->joint_stereo) {
773         ctx->js_switch = get_bits1(gb);
774         align_get_bits(gb);
775     }
776
777     if (!sconf->mc_coding || ctx->js_switch) {
778         int independent_bs = !sconf->joint_stereo;
779
780         for (c = 0; c < avctx->channels; c++) {
781             js_blocks[0] = 0;
782             js_blocks[1] = 0;
783
784             get_block_sizes(ctx, div_blocks, &bs_info);
785
786             // if joint_stereo and block_switching is set, independent decoding
787             // is signaled via the first bit of bs_info
788             if (sconf->joint_stereo && sconf->block_switching)
789                 if (bs_info >> 31)
790                     independent_bs = 2;
791
792             // if this is the last channel, it has to be decoded independently
793             if (c == avctx->channels - 1)
794                 independent_bs = 1;
795
796             if (independent_bs) {
797                 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
798                     return -1;
799
800                 independent_bs--;
801             } else {
802                 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
803                     return -1;
804
805                 c++;
806             }
807
808             // store carryover raw samples
809             memmove(ctx->raw_samples[c] - sconf->max_order,
810                     ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
811                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
812         }
813     } else { // multi-channel coding
814         get_block_sizes(ctx, div_blocks, &bs_info);
815
816         // TODO: multi channel coding might use a temporary buffer instead as
817         //       the actual channel is not known when read_block-data is called
818         if (decode_blocks_ind(ctx, ra_frame, 0, div_blocks, js_blocks))
819             return -1;
820         // TODO: read_channel_data
821     }
822
823     // TODO: read_diff_float_data
824
825     return 0;
826 }
827
828
829 /** Decodes an ALS frame.
830  */
831 static int decode_frame(AVCodecContext *avctx,
832                         void *data, int *data_size,
833                         AVPacket *avpkt)
834 {
835     ALSDecContext *ctx       = avctx->priv_data;
836     ALSSpecificConfig *sconf = &ctx->sconf;
837     const uint8_t *buffer    = avpkt->data;
838     int buffer_size          = avpkt->size;
839     int invalid_frame, size;
840     unsigned int c, sample, ra_frame, bytes_read, shift;
841
842     init_get_bits(&ctx->gb, buffer, buffer_size * 8);
843
844     // In the case that the distance between random access frames is set to zero
845     // (sconf->ra_distance == 0) no frame is treated as a random access frame.
846     // For the first frame, if prediction is used, all samples used from the
847     // previous frame are assumed to be zero.
848     ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
849
850     // the last frame to decode might have a different length
851     if (sconf->samples != 0xFFFFFFFF)
852         ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
853                                       sconf->frame_length);
854     else
855         ctx->cur_frame_length = sconf->frame_length;
856
857     // decode the frame data
858     if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
859         av_log(ctx->avctx, AV_LOG_WARNING,
860                "Reading frame data failed. Skipping RA unit.\n");
861
862     ctx->frame_id++;
863
864     // check for size of decoded data
865     size = ctx->cur_frame_length * avctx->channels *
866            (av_get_bits_per_sample_format(avctx->sample_fmt) >> 3);
867
868     if (size > *data_size) {
869         av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
870         return -1;
871     }
872
873     *data_size = size;
874
875     // transform decoded frame into output format
876     #define INTERLEAVE_OUTPUT(bps)                                 \
877     {                                                              \
878         int##bps##_t *dest = (int##bps##_t*) data;                 \
879         shift = bps - ctx->avctx->bits_per_raw_sample;             \
880         for (sample = 0; sample < ctx->cur_frame_length; sample++) \
881             for (c = 0; c < avctx->channels; c++)                  \
882                 *dest++ = ctx->raw_samples[c][sample] << shift;    \
883     }
884
885     if (ctx->avctx->bits_per_raw_sample <= 16) {
886         INTERLEAVE_OUTPUT(16)
887     } else {
888         INTERLEAVE_OUTPUT(32)
889     }
890
891     bytes_read = invalid_frame ? buffer_size :
892                                  (get_bits_count(&ctx->gb) + 7) >> 3;
893
894     return bytes_read;
895 }
896
897
898 /** Uninitializes the ALS decoder.
899  */
900 static av_cold int decode_end(AVCodecContext *avctx)
901 {
902     ALSDecContext *ctx = avctx->priv_data;
903
904     av_freep(&ctx->sconf.chan_pos);
905
906     av_freep(&ctx->quant_cof);
907     av_freep(&ctx->lpc_cof);
908     av_freep(&ctx->prev_raw_samples);
909     av_freep(&ctx->raw_samples);
910     av_freep(&ctx->raw_buffer);
911
912     return 0;
913 }
914
915
916 /** Initializes the ALS decoder.
917  */
918 static av_cold int decode_init(AVCodecContext *avctx)
919 {
920     unsigned int c;
921     unsigned int channel_size;
922     ALSDecContext *ctx = avctx->priv_data;
923     ALSSpecificConfig *sconf = &ctx->sconf;
924     ctx->avctx = avctx;
925
926     if (!avctx->extradata) {
927         av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
928         return -1;
929     }
930
931     if (read_specific_config(ctx)) {
932         av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
933         decode_end(avctx);
934         return -1;
935     }
936
937     if (check_specific_config(ctx)) {
938         decode_end(avctx);
939         return -1;
940     }
941
942     if (sconf->floating) {
943         avctx->sample_fmt          = SAMPLE_FMT_FLT;
944         avctx->bits_per_raw_sample = 32;
945     } else {
946         avctx->sample_fmt          = sconf->resolution > 1
947                                      ? SAMPLE_FMT_S32 : SAMPLE_FMT_S16;
948         avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
949     }
950
951     avctx->frame_size = sconf->frame_length;
952     channel_size      = sconf->frame_length + sconf->max_order;
953
954     ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
955     ctx->raw_buffer       = av_mallocz(sizeof(*ctx->     raw_buffer)  * avctx->channels * channel_size);
956     ctx->raw_samples      = av_malloc (sizeof(*ctx->     raw_samples) * avctx->channels);
957
958     // allocate previous raw sample buffer
959     if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
960         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
961         decode_end(avctx);
962         return AVERROR(ENOMEM);
963     }
964
965     // assign raw samples buffers
966     ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
967     for (c = 1; c < avctx->channels; c++)
968         ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
969
970     return 0;
971 }
972
973
974 /** Flushes (resets) the frame ID after seeking.
975  */
976 static av_cold void flush(AVCodecContext *avctx)
977 {
978     ALSDecContext *ctx = avctx->priv_data;
979
980     ctx->frame_id = 0;
981 }
982
983
984 AVCodec als_decoder = {
985     "als",
986     CODEC_TYPE_AUDIO,
987     CODEC_ID_MP4ALS,
988     sizeof(ALSDecContext),
989     decode_init,
990     NULL,
991     decode_end,
992     decode_frame,
993     .flush = flush,
994     .capabilities = CODEC_CAP_SUBFRAMES,
995     .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
996 };
997