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