]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/eac3dec.c
Make av_log_missing_feature an internal function, and change its name
[frescor/ffmpeg.git] / libavcodec / eac3dec.c
1 /*
2  * E-AC-3 decoder
3  * Copyright (c) 2007 Bartlomiej Wolowiec <bartek.wolowiec@gmail.com>
4  * Copyright (c) 2008 Justin Ruggles
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 #include "avcodec.h"
24 #include "internal.h"
25 #include "ac3.h"
26 #include "ac3_parser.h"
27 #include "ac3dec.h"
28 #include "ac3dec_data.h"
29
30 /** gain adaptive quantization mode */
31 typedef enum {
32     EAC3_GAQ_NO =0,
33     EAC3_GAQ_12,
34     EAC3_GAQ_14,
35     EAC3_GAQ_124
36 } EAC3GaqMode;
37
38 #define EAC3_SR_CODE_REDUCED  3
39
40 /** lrint(M_SQRT2*cos(2*M_PI/12)*(1<<23)) */
41 #define COEFF_0 10273905LL
42
43 /** lrint(M_SQRT2*cos(0*M_PI/12)*(1<<23)) = lrint(M_SQRT2*(1<<23)) */
44 #define COEFF_1 11863283LL
45
46 /** lrint(M_SQRT2*cos(5*M_PI/12)*(1<<23)) */
47 #define COEFF_2  3070444LL
48
49 /**
50  * Calculate 6-point IDCT of the pre-mantissas.
51  * All calculations are 24-bit fixed-point.
52  */
53 static void idct6(int pre_mant[6])
54 {
55     int tmp;
56     int even0, even1, even2, odd0, odd1, odd2;
57
58     odd1 = pre_mant[1] - pre_mant[3] - pre_mant[5];
59
60     even2 = ( pre_mant[2]                * COEFF_0) >> 23;
61     tmp   = ( pre_mant[4]                * COEFF_1) >> 23;
62     odd0  = ((pre_mant[1] + pre_mant[5]) * COEFF_2) >> 23;
63
64     even0 = pre_mant[0] + (tmp >> 1);
65     even1 = pre_mant[0] - tmp;
66
67     tmp = even0;
68     even0 = tmp + even2;
69     even2 = tmp - even2;
70
71     tmp = odd0;
72     odd0 = tmp + pre_mant[1] + pre_mant[3];
73     odd2 = tmp + pre_mant[5] - pre_mant[3];
74
75     pre_mant[0] = even0 + odd0;
76     pre_mant[1] = even1 + odd1;
77     pre_mant[2] = even2 + odd2;
78     pre_mant[3] = even2 - odd2;
79     pre_mant[4] = even1 - odd1;
80     pre_mant[5] = even0 - odd0;
81 }
82
83 void ff_eac3_decode_transform_coeffs_aht_ch(AC3DecodeContext *s, int ch)
84 {
85     int bin, blk, gs;
86     int end_bap, gaq_mode;
87     GetBitContext *gbc = &s->gbc;
88     int gaq_gain[AC3_MAX_COEFS];
89
90     gaq_mode = get_bits(gbc, 2);
91     end_bap = (gaq_mode < 2) ? 12 : 17;
92
93     /* if GAQ gain is used, decode gain codes for bins with hebap between
94        8 and end_bap */
95     gs = 0;
96     if (gaq_mode == EAC3_GAQ_12 || gaq_mode == EAC3_GAQ_14) {
97         /* read 1-bit GAQ gain codes */
98         for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
99             if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < end_bap)
100                 gaq_gain[gs++] = get_bits1(gbc) << (gaq_mode-1);
101         }
102     } else if (gaq_mode == EAC3_GAQ_124) {
103         /* read 1.67-bit GAQ gain codes (3 codes in 5 bits) */
104         int gc = 2;
105         for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
106             if (s->bap[ch][bin] > 7 && s->bap[ch][bin] < 17) {
107                 if (gc++ == 2) {
108                     int group_code = get_bits(gbc, 5);
109                     if (group_code > 26) {
110                         av_log(s->avctx, AV_LOG_WARNING, "GAQ gain group code out-of-range\n");
111                         group_code = 26;
112                     }
113                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][0];
114                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][1];
115                     gaq_gain[gs++] = ff_ac3_ungroup_3_in_5_bits_tab[group_code][2];
116                     gc = 0;
117                 }
118             }
119         }
120     }
121
122     gs=0;
123     for (bin = s->start_freq[ch]; bin < s->end_freq[ch]; bin++) {
124         int hebap = s->bap[ch][bin];
125         int bits = ff_eac3_bits_vs_hebap[hebap];
126         if (!hebap) {
127             /* zero-mantissa dithering */
128             for (blk = 0; blk < 6; blk++) {
129                 s->pre_mantissa[ch][bin][blk] = (av_lfg_get(&s->dith_state) & 0x7FFFFF) - 0x400000;
130             }
131         } else if (hebap < 8) {
132             /* Vector Quantization */
133             int v = get_bits(gbc, bits);
134             for (blk = 0; blk < 6; blk++) {
135                 s->pre_mantissa[ch][bin][blk] = ff_eac3_mantissa_vq[hebap][v][blk] << 8;
136             }
137         } else {
138             /* Gain Adaptive Quantization */
139             int gbits, log_gain;
140             if (gaq_mode != EAC3_GAQ_NO && hebap < end_bap) {
141                 log_gain = gaq_gain[gs++];
142             } else {
143                 log_gain = 0;
144             }
145             gbits = bits - log_gain;
146
147             for (blk = 0; blk < 6; blk++) {
148                 int mant = get_sbits(gbc, gbits);
149                 if (mant == -(1 << (gbits-1))) {
150                     /* large mantissa */
151                     int b;
152                     mant = get_sbits(gbc, bits-2+log_gain) << (26-log_gain-bits);
153                     /* remap mantissa value to correct for asymmetric quantization */
154                     if (mant >= 0)
155                         b = 32768 >> (log_gain+8);
156                     else
157                         b = ff_eac3_gaq_remap_2_4_b[hebap-8][log_gain-1];
158                     mant += (ff_eac3_gaq_remap_2_4_a[hebap-8][log_gain-1] * (mant>>8) + b) >> 7;
159                 } else {
160                     /* small mantissa, no GAQ, or Gk=1 */
161                     mant <<= 24 - bits;
162                     if (!log_gain) {
163                         /* remap mantissa value for no GAQ or Gk=1 */
164                         mant += (ff_eac3_gaq_remap_1[hebap-8] * (mant>>8)) >> 7;
165                     }
166                 }
167                 s->pre_mantissa[ch][bin][blk] = mant;
168             }
169         }
170         idct6(s->pre_mantissa[ch][bin]);
171     }
172 }
173
174 int ff_eac3_parse_header(AC3DecodeContext *s)
175 {
176     int i, blk, ch;
177     int ac3_exponent_strategy, parse_aht_info, parse_spx_atten_data;
178     int parse_transient_proc_info;
179     int num_cpl_blocks;
180     GetBitContext *gbc = &s->gbc;
181
182     /* An E-AC-3 stream can have multiple independent streams which the
183        application can select from. each independent stream can also contain
184        dependent streams which are used to add or replace channels. */
185     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
186         ff_log_missing_feature(s->avctx, "Dependent substream decoding", 1);
187         return AC3_PARSE_ERROR_FRAME_TYPE;
188     } else if (s->frame_type == EAC3_FRAME_TYPE_RESERVED) {
189         av_log(s->avctx, AV_LOG_ERROR, "Reserved frame type\n");
190         return AC3_PARSE_ERROR_FRAME_TYPE;
191     }
192
193     /* The substream id indicates which substream this frame belongs to. each
194        independent stream has its own substream id, and the dependent streams
195        associated to an independent stream have matching substream id's. */
196     if (s->substreamid) {
197         /* only decode substream with id=0. skip any additional substreams. */
198         ff_log_missing_feature(s->avctx, "Additional substreams", 1);
199         return AC3_PARSE_ERROR_FRAME_TYPE;
200     }
201
202     if (s->bit_alloc_params.sr_code == EAC3_SR_CODE_REDUCED) {
203         /* The E-AC-3 specification does not tell how to handle reduced sample
204            rates in bit allocation.  The best assumption would be that it is
205            handled like AC-3 DolbyNet, but we cannot be sure until we have a
206            sample which utilizes this feature. */
207         ff_log_missing_feature(s->avctx, "Reduced sampling rates", 1);
208         return -1;
209     }
210     skip_bits(gbc, 5); // skip bitstream id
211
212     /* volume control params */
213     for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
214         skip_bits(gbc, 5); // skip dialog normalization
215         if (get_bits1(gbc)) {
216             skip_bits(gbc, 8); // skip compression gain word
217         }
218     }
219
220     /* dependent stream channel map */
221     if (s->frame_type == EAC3_FRAME_TYPE_DEPENDENT) {
222         if (get_bits1(gbc)) {
223             skip_bits(gbc, 16); // skip custom channel map
224         }
225     }
226
227     /* mixing metadata */
228     if (get_bits1(gbc)) {
229         /* center and surround mix levels */
230         if (s->channel_mode > AC3_CHMODE_STEREO) {
231             skip_bits(gbc, 2);  // skip preferred stereo downmix mode
232             if (s->channel_mode & 1) {
233                 /* if three front channels exist */
234                 skip_bits(gbc, 3); //skip Lt/Rt center mix level
235                 s->center_mix_level = get_bits(gbc, 3);
236             }
237             if (s->channel_mode & 4) {
238                 /* if a surround channel exists */
239                 skip_bits(gbc, 3); //skip Lt/Rt surround mix level
240                 s->surround_mix_level = get_bits(gbc, 3);
241             }
242         }
243
244         /* lfe mix level */
245         if (s->lfe_on && get_bits1(gbc)) {
246             // TODO: use LFE mix level
247             skip_bits(gbc, 5); // skip LFE mix level code
248         }
249
250         /* info for mixing with other streams and substreams */
251         if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT) {
252             for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
253                 // TODO: apply program scale factor
254                 if (get_bits1(gbc)) {
255                     skip_bits(gbc, 6);  // skip program scale factor
256                 }
257             }
258             if (get_bits1(gbc)) {
259                 skip_bits(gbc, 6);  // skip external program scale factor
260             }
261             /* skip mixing parameter data */
262             switch(get_bits(gbc, 2)) {
263                 case 1: skip_bits(gbc, 5);  break;
264                 case 2: skip_bits(gbc, 12); break;
265                 case 3: {
266                     int mix_data_size = (get_bits(gbc, 5) + 2) << 3;
267                     skip_bits_long(gbc, mix_data_size);
268                     break;
269                 }
270             }
271             /* skip pan information for mono or dual mono source */
272             if (s->channel_mode < AC3_CHMODE_STEREO) {
273                 for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
274                     if (get_bits1(gbc)) {
275                         /* note: this is not in the ATSC A/52B specification
276                            reference: ETSI TS 102 366 V1.1.1
277                                       section: E.1.3.1.25 */
278                         skip_bits(gbc, 8);  // skip pan mean direction index
279                         skip_bits(gbc, 6);  // skip reserved paninfo bits
280                     }
281                 }
282             }
283             /* skip mixing configuration information */
284             if (get_bits1(gbc)) {
285                 for (blk = 0; blk < s->num_blocks; blk++) {
286                     if (s->num_blocks == 1 || get_bits1(gbc)) {
287                         skip_bits(gbc, 5);
288                     }
289                 }
290             }
291         }
292     }
293
294     /* informational metadata */
295     if (get_bits1(gbc)) {
296         skip_bits(gbc, 3); // skip bit stream mode
297         skip_bits(gbc, 2); // skip copyright bit and original bitstream bit
298         if (s->channel_mode == AC3_CHMODE_STEREO) {
299             skip_bits(gbc, 4); // skip Dolby surround and headphone mode
300         }
301         if (s->channel_mode >= AC3_CHMODE_2F2R) {
302             skip_bits(gbc, 2); // skip Dolby surround EX mode
303         }
304         for (i = 0; i < (s->channel_mode ? 1 : 2); i++) {
305             if (get_bits1(gbc)) {
306                 skip_bits(gbc, 8); // skip mix level, room type, and A/D converter type
307             }
308         }
309         if (s->bit_alloc_params.sr_code != EAC3_SR_CODE_REDUCED) {
310             skip_bits1(gbc); // skip source sample rate code
311         }
312     }
313
314     /* converter synchronization flag
315        If frames are less than six blocks, this bit should be turned on
316        once every 6 blocks to indicate the start of a frame set.
317        reference: RFC 4598, Section 2.1.3  Frame Sets */
318     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT && s->num_blocks != 6) {
319         skip_bits1(gbc); // skip converter synchronization flag
320     }
321
322     /* original frame size code if this stream was converted from AC-3 */
323     if (s->frame_type == EAC3_FRAME_TYPE_AC3_CONVERT &&
324             (s->num_blocks == 6 || get_bits1(gbc))) {
325         skip_bits(gbc, 6); // skip frame size code
326     }
327
328     /* additional bitstream info */
329     if (get_bits1(gbc)) {
330         int addbsil = get_bits(gbc, 6);
331         for (i = 0; i < addbsil + 1; i++) {
332             skip_bits(gbc, 8); // skip additional bit stream info
333         }
334     }
335
336     /* audio frame syntax flags, strategy data, and per-frame data */
337
338     if (s->num_blocks == 6) {
339         ac3_exponent_strategy = get_bits1(gbc);
340         parse_aht_info        = get_bits1(gbc);
341     } else {
342         /* less than 6 blocks, so use AC-3-style exponent strategy syntax, and
343            do not use AHT */
344         ac3_exponent_strategy = 1;
345         parse_aht_info = 0;
346     }
347
348     s->snr_offset_strategy    = get_bits(gbc, 2);
349     parse_transient_proc_info = get_bits1(gbc);
350
351     s->block_switch_syntax = get_bits1(gbc);
352     if (!s->block_switch_syntax)
353         memset(s->block_switch, 0, sizeof(s->block_switch));
354
355     s->dither_flag_syntax = get_bits1(gbc);
356     if (!s->dither_flag_syntax) {
357         for (ch = 1; ch <= s->fbw_channels; ch++)
358             s->dither_flag[ch] = 1;
359     }
360     s->dither_flag[CPL_CH] = s->dither_flag[s->lfe_ch] = 0;
361
362     s->bit_allocation_syntax = get_bits1(gbc);
363     if (!s->bit_allocation_syntax) {
364         /* set default bit allocation parameters */
365         s->bit_alloc_params.slow_decay = ff_ac3_slow_decay_tab[2];
366         s->bit_alloc_params.fast_decay = ff_ac3_fast_decay_tab[1];
367         s->bit_alloc_params.slow_gain  = ff_ac3_slow_gain_tab [1];
368         s->bit_alloc_params.db_per_bit = ff_ac3_db_per_bit_tab[2];
369         s->bit_alloc_params.floor      = ff_ac3_floor_tab     [7];
370     }
371
372     s->fast_gain_syntax  = get_bits1(gbc);
373     s->dba_syntax        = get_bits1(gbc);
374     s->skip_syntax       = get_bits1(gbc);
375     parse_spx_atten_data = get_bits1(gbc);
376
377     /* coupling strategy occurance and coupling use per block */
378     num_cpl_blocks = 0;
379     if (s->channel_mode > 1) {
380         for (blk = 0; blk < s->num_blocks; blk++) {
381             s->cpl_strategy_exists[blk] = (!blk || get_bits1(gbc));
382             if (s->cpl_strategy_exists[blk]) {
383                 s->cpl_in_use[blk] = get_bits1(gbc);
384             } else {
385                 s->cpl_in_use[blk] = s->cpl_in_use[blk-1];
386             }
387             num_cpl_blocks += s->cpl_in_use[blk];
388         }
389     } else {
390         memset(s->cpl_in_use, 0, sizeof(s->cpl_in_use));
391     }
392
393     /* exponent strategy data */
394     if (ac3_exponent_strategy) {
395         /* AC-3-style exponent strategy syntax */
396         for (blk = 0; blk < s->num_blocks; blk++) {
397             for (ch = !s->cpl_in_use[blk]; ch <= s->fbw_channels; ch++) {
398                 s->exp_strategy[blk][ch] = get_bits(gbc, 2);
399             }
400         }
401     } else {
402         /* LUT-based exponent strategy syntax */
403         for (ch = !((s->channel_mode > 1) && num_cpl_blocks); ch <= s->fbw_channels; ch++) {
404             int frmchexpstr = get_bits(gbc, 5);
405             for (blk = 0; blk < 6; blk++) {
406                 s->exp_strategy[blk][ch] = ff_eac3_frm_expstr[frmchexpstr][blk];
407             }
408         }
409     }
410     /* LFE exponent strategy */
411     if (s->lfe_on) {
412         for (blk = 0; blk < s->num_blocks; blk++) {
413             s->exp_strategy[blk][s->lfe_ch] = get_bits1(gbc);
414         }
415     }
416     /* original exponent strategies if this stream was converted from AC-3 */
417     if (s->frame_type == EAC3_FRAME_TYPE_INDEPENDENT &&
418             (s->num_blocks == 6 || get_bits1(gbc))) {
419         skip_bits(gbc, 5 * s->fbw_channels); // skip converter channel exponent strategy
420     }
421
422     /* determine which channels use AHT */
423     if (parse_aht_info) {
424         /* For AHT to be used, all non-zero blocks must reuse exponents from
425            the first block.  Furthermore, for AHT to be used in the coupling
426            channel, all blocks must use coupling and use the same coupling
427            strategy. */
428         s->channel_uses_aht[CPL_CH]=0;
429         for (ch = (num_cpl_blocks != 6); ch <= s->channels; ch++) {
430             int use_aht = 1;
431             for (blk = 1; blk < 6; blk++) {
432                 if ((s->exp_strategy[blk][ch] != EXP_REUSE) ||
433                         (!ch && s->cpl_strategy_exists[blk])) {
434                     use_aht = 0;
435                     break;
436                 }
437             }
438             s->channel_uses_aht[ch] = use_aht && get_bits1(gbc);
439         }
440     } else {
441         memset(s->channel_uses_aht, 0, sizeof(s->channel_uses_aht));
442     }
443
444     /* per-frame SNR offset */
445     if (!s->snr_offset_strategy) {
446         int csnroffst = (get_bits(gbc, 6) - 15) << 4;
447         int snroffst = (csnroffst + get_bits(gbc, 4)) << 2;
448         for (ch = 0; ch <= s->channels; ch++)
449             s->snr_offset[ch] = snroffst;
450     }
451
452     /* transient pre-noise processing data */
453     if (parse_transient_proc_info) {
454         for (ch = 1; ch <= s->fbw_channels; ch++) {
455             if (get_bits1(gbc)) { // channel in transient processing
456                 skip_bits(gbc, 10); // skip transient processing location
457                 skip_bits(gbc, 8);  // skip transient processing length
458             }
459         }
460     }
461
462     /* spectral extension attenuation data */
463     if (parse_spx_atten_data) {
464         ff_log_missing_feature(s->avctx, "Spectral extension attenuation", 1);
465         for (ch = 1; ch <= s->fbw_channels; ch++) {
466             if (get_bits1(gbc)) { // channel has spx attenuation
467                 skip_bits(gbc, 5); // skip spx attenuation code
468             }
469         }
470     }
471
472     /* block start information */
473     if (s->num_blocks > 1 && get_bits1(gbc)) {
474         /* reference: Section E2.3.2.27
475            nblkstrtbits = (numblks - 1) * (4 + ceiling(log2(words_per_frame)))
476            The spec does not say what this data is or what it's used for.
477            It is likely the offset of each block within the frame. */
478         int block_start_bits = (s->num_blocks-1) * (4 + av_log2(s->frame_size-2));
479         skip_bits_long(gbc, block_start_bits);
480         ff_log_missing_feature(s->avctx, "Block start info", 1);
481     }
482
483     /* syntax state initialization */
484     for (ch = 1; ch <= s->fbw_channels; ch++) {
485         s->first_cpl_coords[ch] = 1;
486     }
487     s->first_cpl_leak = 1;
488
489     return 0;
490 }