]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca_exss.c
Merge commit '785bfb1d7bb8de567c3aac1d9cc369b55ac9fb7b'
[ffmpeg] / libavcodec / dca_exss.c
1 /*
2  * Copyright (C) 2016 foo86
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "dcadec.h"
22 #include "dcadata.h"
23
24 static void parse_xll_parameters(DCAExssParser *s, DCAExssAsset *asset)
25 {
26     // Size of XLL data in extension substream
27     asset->xll_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
28
29     // XLL sync word present flag
30     if (asset->xll_sync_present = get_bits1(&s->gb)) {
31         int xll_delay_nbits;
32
33         // Peak bit rate smoothing buffer size
34         skip_bits(&s->gb, 4);
35
36         // Number of bits for XLL decoding delay
37         xll_delay_nbits = get_bits(&s->gb, 5) + 1;
38
39         // Initial XLL decoding delay in frames
40         asset->xll_delay_nframes = get_bits_long(&s->gb, xll_delay_nbits);
41
42         // Number of bytes offset to XLL sync
43         asset->xll_sync_offset = get_bits(&s->gb, s->exss_size_nbits);
44     } else {
45         asset->xll_delay_nframes = 0;
46         asset->xll_sync_offset = 0;
47     }
48 }
49
50 static void parse_lbr_parameters(DCAExssParser *s, DCAExssAsset *asset)
51 {
52     // Size of LBR component in extension substream
53     asset->lbr_size = get_bits(&s->gb, 14) + 1;
54
55     // LBR sync word present flag
56     if (get_bits1(&s->gb))
57         // LBR sync distance
58         skip_bits(&s->gb, 2);
59 }
60
61 static int parse_descriptor(DCAExssParser *s, DCAExssAsset *asset)
62 {
63     int i, j, drc_present, descr_size, descr_pos = get_bits_count(&s->gb);
64
65     // Size of audio asset descriptor in bytes
66     descr_size = get_bits(&s->gb, 9) + 1;
67
68     // Audio asset identifier
69     asset->asset_index = get_bits(&s->gb, 3);
70
71     //
72     // Per stream static metadata
73     //
74
75     if (s->static_fields_present) {
76         // Asset type descriptor presence
77         if (get_bits1(&s->gb))
78             // Asset type descriptor
79             skip_bits(&s->gb, 4);
80
81         // Language descriptor presence
82         if (get_bits1(&s->gb))
83             // Language descriptor
84             skip_bits(&s->gb, 24);
85
86         // Additional textual information presence
87         if (get_bits1(&s->gb)) {
88             // Byte size of additional text info
89             int text_size = get_bits(&s->gb, 10) + 1;
90
91             // Sanity check available size
92             if (get_bits_left(&s->gb) < text_size * 8)
93                 return AVERROR_INVALIDDATA;
94
95             // Additional textual information string
96             skip_bits_long(&s->gb, text_size * 8);
97         }
98
99         // PCM bit resolution
100         asset->pcm_bit_res = get_bits(&s->gb, 5) + 1;
101
102         // Maximum sample rate
103         asset->max_sample_rate = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
104
105         // Total number of channels
106         asset->nchannels_total = get_bits(&s->gb, 8) + 1;
107
108         // One to one map channel to speakers
109         if (asset->one_to_one_map_ch_to_spkr = get_bits1(&s->gb)) {
110             int spkr_mask_nbits = 0;
111             int spkr_remap_nsets;
112             int nspeakers[8];
113
114             // Embedded stereo flag
115             if (asset->nchannels_total > 2)
116                 asset->embedded_stereo = get_bits1(&s->gb);
117
118             // Embedded 6 channels flag
119             if (asset->nchannels_total > 6)
120                 asset->embedded_6ch = get_bits1(&s->gb);
121
122             // Speaker mask enabled flag
123             if (asset->spkr_mask_enabled = get_bits1(&s->gb)) {
124                 // Number of bits for speaker activity mask
125                 spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
126
127                 // Loudspeaker activity mask
128                 asset->spkr_mask = get_bits(&s->gb, spkr_mask_nbits);
129             }
130
131             // Number of speaker remapping sets
132             if ((spkr_remap_nsets = get_bits(&s->gb, 3)) && !spkr_mask_nbits) {
133                 av_log(s->avctx, AV_LOG_ERROR, "Speaker mask disabled yet there are remapping sets\n");
134                 return AVERROR_INVALIDDATA;
135             }
136
137             // Standard loudspeaker layout mask
138             for (i = 0; i < spkr_remap_nsets; i++)
139                 nspeakers[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
140
141             for (i = 0; i < spkr_remap_nsets; i++) {
142                 // Number of channels to be decoded for speaker remapping
143                 int nch_for_remaps = get_bits(&s->gb, 5) + 1;
144
145                 for (j = 0; j < nspeakers[i]; j++) {
146                     // Decoded channels to output speaker mapping mask
147                     int remap_ch_mask = get_bits_long(&s->gb, nch_for_remaps);
148
149                     // Loudspeaker remapping codes
150                     skip_bits_long(&s->gb, av_popcount(remap_ch_mask) * 5);
151                 }
152             }
153         } else {
154             asset->embedded_stereo = 0;
155             asset->embedded_6ch = 0;
156             asset->spkr_mask_enabled = 0;
157             asset->spkr_mask = 0;
158
159             // Representation type
160             asset->representation_type = get_bits(&s->gb, 3);
161         }
162     }
163
164     //
165     // DRC, DNC and mixing metadata
166     //
167
168     // Dynamic range coefficient presence flag
169     drc_present = get_bits1(&s->gb);
170
171     // Code for dynamic range coefficient
172     if (drc_present)
173         skip_bits(&s->gb, 8);
174
175     // Dialog normalization presence flag
176     if (get_bits1(&s->gb))
177         // Dialog normalization code
178         skip_bits(&s->gb, 5);
179
180     // DRC for stereo downmix
181     if (drc_present && asset->embedded_stereo)
182         skip_bits(&s->gb, 8);
183
184     // Mixing metadata presence flag
185     if (s->mix_metadata_enabled && get_bits1(&s->gb)) {
186         int nchannels_dmix;
187
188         // External mixing flag
189         skip_bits1(&s->gb);
190
191         // Post mixing / replacement gain adjustment
192         skip_bits(&s->gb, 6);
193
194         // DRC prior to mixing
195         if (get_bits(&s->gb, 2) == 3)
196             // Custom code for mixing DRC
197             skip_bits(&s->gb, 8);
198         else
199             // Limit for mixing DRC
200             skip_bits(&s->gb, 3);
201
202         // Scaling type for channels of main audio
203         // Scaling parameters of main audio
204         if (get_bits1(&s->gb))
205             for (i = 0; i < s->nmixoutconfigs; i++)
206                 skip_bits_long(&s->gb, 6 * s->nmixoutchs[i]);
207         else
208             skip_bits_long(&s->gb, 6 * s->nmixoutconfigs);
209
210         nchannels_dmix = asset->nchannels_total;
211         if (asset->embedded_6ch)
212             nchannels_dmix += 6;
213         if (asset->embedded_stereo)
214             nchannels_dmix += 2;
215
216         for (i = 0; i < s->nmixoutconfigs; i++) {
217             if (!s->nmixoutchs[i]) {
218                 av_log(s->avctx, AV_LOG_ERROR, "Invalid speaker layout mask for mixing configuration\n");
219                 return AVERROR_INVALIDDATA;
220             }
221             for (j = 0; j < nchannels_dmix; j++) {
222                 // Mix output mask
223                 int mix_map_mask = get_bits(&s->gb, s->nmixoutchs[i]);
224
225                 // Mixing coefficients
226                 skip_bits_long(&s->gb, av_popcount(mix_map_mask) * 6);
227             }
228         }
229     }
230
231     //
232     // Decoder navigation data
233     //
234
235     // Coding mode for the asset
236     asset->coding_mode = get_bits(&s->gb, 2);
237
238     // Coding components used in asset
239     switch (asset->coding_mode) {
240     case 0: // Coding mode that may contain multiple coding components
241         asset->extension_mask = get_bits(&s->gb, 12);
242
243         if (asset->extension_mask & DCA_EXSS_CORE) {
244             // Size of core component in extension substream
245             asset->core_size = get_bits(&s->gb, 14) + 1;
246             // Core sync word present flag
247             if (get_bits1(&s->gb))
248                 // Core sync distance
249                 skip_bits(&s->gb, 2);
250         }
251
252         if (asset->extension_mask & DCA_EXSS_XBR)
253             // Size of XBR extension in extension substream
254             asset->xbr_size = get_bits(&s->gb, 14) + 1;
255
256         if (asset->extension_mask & DCA_EXSS_XXCH)
257             // Size of XXCH extension in extension substream
258             asset->xxch_size = get_bits(&s->gb, 14) + 1;
259
260         if (asset->extension_mask & DCA_EXSS_X96)
261             // Size of X96 extension in extension substream
262             asset->x96_size = get_bits(&s->gb, 12) + 1;
263
264         if (asset->extension_mask & DCA_EXSS_LBR)
265             parse_lbr_parameters(s, asset);
266
267         if (asset->extension_mask & DCA_EXSS_XLL)
268             parse_xll_parameters(s, asset);
269
270         if (asset->extension_mask & DCA_EXSS_RSV1)
271             skip_bits(&s->gb, 16);
272
273         if (asset->extension_mask & DCA_EXSS_RSV2)
274             skip_bits(&s->gb, 16);
275         break;
276
277     case 1: // Loss-less coding mode without CBR component
278         asset->extension_mask = DCA_EXSS_XLL;
279         parse_xll_parameters(s, asset);
280         break;
281
282     case 2: // Low bit rate mode
283         asset->extension_mask = DCA_EXSS_LBR;
284         parse_lbr_parameters(s, asset);
285         break;
286
287     case 3: // Auxiliary coding mode
288         asset->extension_mask = 0;
289
290         // Size of auxiliary coded data
291         skip_bits(&s->gb, 14);
292
293         // Auxiliary codec identification
294         skip_bits(&s->gb, 8);
295
296         // Aux sync word present flag
297         if (get_bits1(&s->gb))
298             // Aux sync distance
299             skip_bits(&s->gb, 3);
300         break;
301     }
302
303     if (asset->extension_mask & DCA_EXSS_XLL)
304         // DTS-HD stream ID
305         asset->hd_stream_id = get_bits(&s->gb, 3);
306
307     // One to one mixing flag
308     // Per channel main audio scaling flag
309     // Main audio scaling codes
310     // Decode asset in secondary decoder flag
311     // Revision 2 DRC metadata
312     // Reserved
313     // Zero pad
314     if (ff_dca_seek_bits(&s->gb, descr_pos + descr_size * 8)) {
315         av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS asset descriptor\n");
316         return AVERROR_INVALIDDATA;
317     }
318
319     return 0;
320 }
321
322 static int set_exss_offsets(DCAExssAsset *asset)
323 {
324     int offs = asset->asset_offset;
325     int size = asset->asset_size;
326
327     if (asset->extension_mask & DCA_EXSS_CORE) {
328         asset->core_offset = offs;
329         if (asset->core_size > size)
330             return AVERROR_INVALIDDATA;
331         offs += asset->core_size;
332         size -= asset->core_size;
333     }
334
335     if (asset->extension_mask & DCA_EXSS_XBR) {
336         asset->xbr_offset = offs;
337         if (asset->xbr_size > size)
338             return AVERROR_INVALIDDATA;
339         offs += asset->xbr_size;
340         size -= asset->xbr_size;
341     }
342
343     if (asset->extension_mask & DCA_EXSS_XXCH) {
344         asset->xxch_offset = offs;
345         if (asset->xxch_size > size)
346             return AVERROR_INVALIDDATA;
347         offs += asset->xxch_size;
348         size -= asset->xxch_size;
349     }
350
351     if (asset->extension_mask & DCA_EXSS_X96) {
352         asset->x96_offset = offs;
353         if (asset->x96_size > size)
354             return AVERROR_INVALIDDATA;
355         offs += asset->x96_size;
356         size -= asset->x96_size;
357     }
358
359     if (asset->extension_mask & DCA_EXSS_LBR) {
360         asset->lbr_offset = offs;
361         if (asset->lbr_size > size)
362             return AVERROR_INVALIDDATA;
363         offs += asset->lbr_size;
364         size -= asset->lbr_size;
365     }
366
367     if (asset->extension_mask & DCA_EXSS_XLL) {
368         asset->xll_offset = offs;
369         if (asset->xll_size > size)
370             return AVERROR_INVALIDDATA;
371         offs += asset->xll_size;
372         size -= asset->xll_size;
373     }
374
375     return 0;
376 }
377
378 int ff_dca_exss_parse(DCAExssParser *s, uint8_t *data, int size)
379 {
380     int i, ret, offset, wide_hdr, header_size;
381
382     if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
383         return ret;
384
385     // Extension substream sync word
386     skip_bits_long(&s->gb, 32);
387
388     // User defined bits
389     skip_bits(&s->gb, 8);
390
391     // Extension substream index
392     s->exss_index = get_bits(&s->gb, 2);
393
394     // Flag indicating short or long header size
395     wide_hdr = get_bits1(&s->gb);
396
397     // Extension substream header length
398     header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1;
399
400     // Check CRC
401     if (ff_dca_check_crc(s->avctx, &s->gb, 32 + 8, header_size * 8)) {
402         av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n");
403         return AVERROR_INVALIDDATA;
404     }
405
406     s->exss_size_nbits = 16 + 4 * wide_hdr;
407
408     // Number of bytes of extension substream
409     s->exss_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
410     if (s->exss_size > size) {
411         av_log(s->avctx, AV_LOG_ERROR, "Packet too short for EXSS frame\n");
412         return AVERROR_INVALIDDATA;
413     }
414
415     // Per stream static fields presence flag
416     if (s->static_fields_present = get_bits1(&s->gb)) {
417         int active_exss_mask[8];
418
419         // Reference clock code
420         skip_bits(&s->gb, 2);
421
422         // Extension substream frame duration
423         skip_bits(&s->gb, 3);
424
425         // Timecode presence flag
426         if (get_bits1(&s->gb))
427             // Timecode data
428             skip_bits_long(&s->gb, 36);
429
430         // Number of defined audio presentations
431         s->npresents = get_bits(&s->gb, 3) + 1;
432         if (s->npresents > 1) {
433             avpriv_request_sample(s->avctx, "%d audio presentations", s->npresents);
434             return AVERROR_PATCHWELCOME;
435         }
436
437         // Number of audio assets in extension substream
438         s->nassets = get_bits(&s->gb, 3) + 1;
439         if (s->nassets > 1) {
440             avpriv_request_sample(s->avctx, "%d audio assets", s->nassets);
441             return AVERROR_PATCHWELCOME;
442         }
443
444         // Active extension substream mask for audio presentation
445         for (i = 0; i < s->npresents; i++)
446             active_exss_mask[i] = get_bits(&s->gb, s->exss_index + 1);
447
448         // Active audio asset mask
449         for (i = 0; i < s->npresents; i++)
450             skip_bits_long(&s->gb, av_popcount(active_exss_mask[i]) * 8);
451
452         // Mixing metadata enable flag
453         if (s->mix_metadata_enabled = get_bits1(&s->gb)) {
454             int spkr_mask_nbits;
455
456             // Mixing metadata adjustment level
457             skip_bits(&s->gb, 2);
458
459             // Number of bits for mixer output speaker activity mask
460             spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
461
462             // Number of mixing configurations
463             s->nmixoutconfigs = get_bits(&s->gb, 2) + 1;
464
465             // Speaker layout mask for mixer output channels
466             for (i = 0; i < s->nmixoutconfigs; i++)
467                 s->nmixoutchs[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
468         }
469     } else {
470         s->npresents = 1;
471         s->nassets = 1;
472     }
473
474     // Size of encoded asset data in bytes
475     offset = header_size;
476     for (i = 0; i < s->nassets; i++) {
477         s->assets[i].asset_offset = offset;
478         s->assets[i].asset_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
479         offset += s->assets[i].asset_size;
480         if (offset > s->exss_size) {
481             av_log(s->avctx, AV_LOG_ERROR, "EXSS asset out of bounds\n");
482             return AVERROR_INVALIDDATA;
483         }
484     }
485
486     // Audio asset descriptor
487     for (i = 0; i < s->nassets; i++) {
488         if ((ret = parse_descriptor(s, &s->assets[i])) < 0)
489             return ret;
490         if ((ret = set_exss_offsets(&s->assets[i])) < 0) {
491             av_log(s->avctx, AV_LOG_ERROR, "Invalid extension size in EXSS asset descriptor\n");
492             return ret;
493         }
494     }
495
496     // Backward compatible core present
497     // Backward compatible core substream index
498     // Backward compatible core asset index
499     // Reserved
500     // Byte align
501     // CRC16 of extension substream header
502     if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
503         av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS header\n");
504         return AVERROR_INVALIDDATA;
505     }
506
507     return 0;
508 }