isom: Evaluate return value properly.
[L-SMASH.git] / codecs / dts.c
blob5577ab4c35fcc23aa039377966f4f9f79c20516a
1 /*****************************************************************************
2 * dts.c:
3 *****************************************************************************
4 * Copyright (C) 2012-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "common/internal.h" /* must be placed first */
25 #include <stdlib.h>
26 #include <string.h>
27 #include <inttypes.h>
29 #include "core/box.h"
31 /***************************************************************************
32 ETSI TS 102 114 V1.2.1 (2002-12)
33 ETSI TS 102 114 V1.3.1 (2011-08)
34 ETSI TS 102 114 V1.4.1 (2012-09)
36 IMPLEMENTATION OF DTS AUDIO IN MEDIA FILES BASED ON ISO/IEC 14496
37 Document No.: 9302J81100
38 Revision: F
39 Version: 1.3
40 ***************************************************************************/
41 #include "dts.h"
43 #define DTS_MIN_CORE_SIZE 96
44 #define DTS_MAX_STREAM_CONSTRUCTION 21
45 #define DTS_SPECIFIC_BOX_MIN_LENGTH 28
47 typedef enum
49 DTS_SYNCWORD_CORE = 0x7FFE8001,
50 DTS_SYNCWORD_XCH = 0x5A5A5A5A,
51 DTS_SYNCWORD_XXCH = 0x47004A03,
52 DTS_SYNCWORD_X96K = 0x1D95F262,
53 DTS_SYNCWORD_XBR = 0x655E315E,
54 DTS_SYNCWORD_LBR = 0x0A801921,
55 DTS_SYNCWORD_XLL = 0x41A29547,
56 DTS_SYNCWORD_SUBSTREAM = 0x64582025,
57 DTS_SYNCWORD_SUBSTREAM_CORE = 0x02b09261,
58 } dts_syncword;
60 /* Loudspeaker Masks (up to 32-bit) for
61 * - nuCoreSpkrActivityMask
62 * - nuXXChSpkrLayoutMask
63 * - DownMixChMapMask
64 * - nChMask
65 * - nSpkrMask */
66 typedef enum
68 DTS_LOUDSPEAKER_MASK32_C = 0x00000001, /* Centre in front of listener */
69 DTS_LOUDSPEAKER_MASK32_L = 0x00000002, /* Left in front */
70 DTS_LOUDSPEAKER_MASK32_R = 0x00000004, /* Right in front */
71 DTS_LOUDSPEAKER_MASK32_LS = 0x00000008, /* Left surround on side in rear */
72 DTS_LOUDSPEAKER_MASK32_RS = 0x00000010, /* Right surround on side in rear */
73 DTS_LOUDSPEAKER_MASK32_LFE1 = 0x00000020, /* Low frequency effects subwoofer */
74 DTS_LOUDSPEAKER_MASK32_CS = 0x00000040, /* Centre surround in rear */
75 DTS_LOUDSPEAKER_MASK32_LSR = 0x00000080, /* Left surround in rear */
76 DTS_LOUDSPEAKER_MASK32_RSR = 0x00000100, /* Right surround in rear */
77 DTS_LOUDSPEAKER_MASK32_LSS = 0x00000200, /* Left surround on side */
78 DTS_LOUDSPEAKER_MASK32_RSS = 0x00000400, /* Right surround on side */
79 DTS_LOUDSPEAKER_MASK32_LC = 0x00000800, /* Between left and centre in front */
80 DTS_LOUDSPEAKER_MASK32_RC = 0x00001000, /* Between right and centre in front */
81 DTS_LOUDSPEAKER_MASK32_LH = 0x00002000, /* Left height in front */
82 DTS_LOUDSPEAKER_MASK32_CH = 0x00004000, /* Centre Height in front */
83 DTS_LOUDSPEAKER_MASK32_RH = 0x00008000, /* Right Height in front */
84 DTS_LOUDSPEAKER_MASK32_LFE2 = 0x00010000, /* Second low frequency effects subwoofer */
85 DTS_LOUDSPEAKER_MASK32_LW = 0x00020000, /* Left on side in front */
86 DTS_LOUDSPEAKER_MASK32_RW = 0x00040000, /* Right on side in front */
87 DTS_LOUDSPEAKER_MASK32_OH = 0x00080000, /* Over the listener's head */
88 DTS_LOUDSPEAKER_MASK32_LHS = 0x00100000, /* Left height on side */
89 DTS_LOUDSPEAKER_MASK32_RHS = 0x00200000, /* Right height on side */
90 DTS_LOUDSPEAKER_MASK32_CHR = 0x00400000, /* Centre height in rear */
91 DTS_LOUDSPEAKER_MASK32_LHR = 0x00800000, /* Left height in rear */
92 DTS_LOUDSPEAKER_MASK32_RHR = 0x01000000, /* Right height in rear */
93 DTS_LOUDSPEAKER_MASK32_CL = 0x02000000, /* Centre in the plane lower than listener's ears */
94 DTS_LOUDSPEAKER_MASK32_LL = 0x04000000, /* Left in the plane lower than listener's ears */
95 DTS_LOUDSPEAKER_MASK32_RL = 0x08000000, /* Right in the plane lower than listener's ears */
96 } dts_loudspeaker_mask;
98 /* Loudspeaker Masks (up to 16-bit) for
99 * - nuSpkrActivityMask
100 * - nuStndrSpkrLayoutMask
101 * - nuMixOutChMask
102 * - ChannelLayout of DTSSpecificBox */
103 typedef enum
105 DTS_CHANNEL_LAYOUT_C = 0x0001, /* Centre in front of listener */
106 DTS_CHANNEL_LAYOUT_L_R = 0x0002, /* Left/Right in front */
107 DTS_CHANNEL_LAYOUT_LS_RS = 0x0004, /* Left/Right surround on side in rear */
108 DTS_CHANNEL_LAYOUT_LFE1 = 0x0008, /* Low frequency effects subwoofer */
109 DTS_CHANNEL_LAYOUT_CS = 0x0010, /* Centre surround in rear */
110 DTS_CHANNEL_LAYOUT_LH_RH = 0x0020, /* Left/Right height in front */
111 DTS_CHANNEL_LAYOUT_LSR_RSR = 0x0040, /* Left/Right surround in rear */
112 DTS_CHANNEL_LAYOUT_CH = 0x0080, /* Centre height in front */
113 DTS_CHANNEL_LAYOUT_OH = 0x0100, /* Over the listener's head */
114 DTS_CHANNEL_LAYOUT_LC_RC = 0x0200, /* Between left/right and centre in front */
115 DTS_CHANNEL_LAYOUT_LW_RW = 0x0400, /* Left/Right on side in front */
116 DTS_CHANNEL_LAYOUT_LSS_RSS = 0x0800, /* Left/Right surround on side */
117 DTS_CHANNEL_LAYOUT_LFE2 = 0x1000, /* Second low frequency effects subwoofer */
118 DTS_CHANNEL_LAYOUT_LHS_RHS = 0x2000, /* Left/Right height on side */
119 DTS_CHANNEL_LAYOUT_CHR = 0x4000, /* Centre height in rear */
120 DTS_CHANNEL_LAYOUT_LHR_RHR = 0x8000, /* Left/Right height in rear */
121 } dts_channel_layout;
123 static const lsmash_dts_construction_flag construction_info[DTS_MAX_STREAM_CONSTRUCTION + 1] =
126 DTS_CORE_SUBSTREAM_CORE_FLAG,
127 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG,
128 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG,
129 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG,
130 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG,
131 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
132 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
133 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
134 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
135 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
136 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
137 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
138 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
139 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
140 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
141 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
142 DTS_EXT_SUBSTREAM_XLL_FLAG,
143 DTS_EXT_SUBSTREAM_LBR_FLAG,
144 DTS_EXT_SUBSTREAM_CORE_FLAG,
145 DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG,
146 DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG ,
149 void dts_setup_parser( dts_info_t *info )
151 dts_extension_info_t *exss = &info->exss[0];
152 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0.
153 * Therefore, we can treat as if one extension substream is there even if no extension substreams. */
154 exss->nuNumAudioPresnt = 1;
155 exss->nuNumAssets = 1;
156 exss->bBcCorePresent [0] = 0;
157 exss->nuBcCoreExtSSIndex[0] = 0;
158 exss->nuBcCoreAssetIndex[0] = 0;
161 struct lsmash_dts_reserved_box_tag
163 uint32_t size;
164 uint8_t *data;
167 int lsmash_append_dts_reserved_box( lsmash_dts_specific_parameters_t *param, uint8_t *box_data, uint32_t box_size )
169 if( !param || !box_data || box_size == 0 )
170 return -1;
171 param->box = lsmash_malloc( sizeof(lsmash_dts_reserved_box_t) );
172 if( !param->box )
173 return -1;
174 param->box->data = lsmash_memdup( box_data, box_size );
175 if( !param->box->data )
177 lsmash_freep( &param->box );
178 return -1;
180 param->box->size = box_size;
181 return 0;
184 void lsmash_remove_dts_reserved_box( lsmash_dts_specific_parameters_t *param )
186 if( !param->box )
187 return;
188 if( param->box->data )
189 lsmash_free( param->box->data );
190 lsmash_freep( &param->box );
193 void dts_destruct_specific_data( void *data )
195 if( !data )
196 return;
197 lsmash_remove_dts_reserved_box( data );
198 lsmash_free( data );
201 uint8_t lsmash_dts_get_stream_construction( lsmash_dts_construction_flag flags )
203 uint8_t StreamConstruction;
204 for( StreamConstruction = 1; StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION; StreamConstruction++ )
205 if( flags == construction_info[StreamConstruction] )
206 break;
207 /* For any stream type not listed in the above table,
208 * StreamConstruction shall be set to 0 and the codingname shall default to 'dtsh'. */
209 return StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? StreamConstruction : 0;
212 lsmash_dts_construction_flag lsmash_dts_get_construction_flags( uint8_t stream_construction )
214 if( stream_construction <= DTS_MAX_STREAM_CONSTRUCTION )
215 return construction_info[stream_construction];
216 return 0;
219 lsmash_codec_type_t lsmash_dts_get_codingname( lsmash_dts_specific_parameters_t *param )
221 assert( param->StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION );
222 if( param->MultiAssetFlag )
223 return ISOM_CODEC_TYPE_DTSH_AUDIO; /* Multiple asset streams shall use the 'dtsh' coding_name. */
224 static lsmash_codec_type_t codingname_table[DTS_MAX_STREAM_CONSTRUCTION + 1] = { LSMASH_CODEC_TYPE_INITIALIZER };
225 if( lsmash_check_codec_type_identical( codingname_table[0], LSMASH_CODEC_TYPE_UNSPECIFIED ) )
227 int i = 0;
228 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO; /* Undefined stream types shall be set to 0 and the codingname shall default to 'dtsh'. */
229 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
230 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
231 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
232 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
233 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
234 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
235 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
236 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
237 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
238 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
239 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
240 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
241 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
242 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
243 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
244 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
245 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
246 codingname_table[i++] = ISOM_CODEC_TYPE_DTSE_AUDIO;
247 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
248 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
249 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
251 return codingname_table[ param->StreamConstruction ];
254 uint8_t *lsmash_create_dts_specific_info( lsmash_dts_specific_parameters_t *param, uint32_t *data_length )
256 lsmash_bits_t bits = { 0 };
257 lsmash_bs_t bs = { 0 };
258 lsmash_bits_init( &bits, &bs );
259 int reserved_box_present = (param->box && param->box->data && param->box->size);
260 uint32_t buffer_length = DTS_SPECIFIC_BOX_MIN_LENGTH + (reserved_box_present ? param->box->size : 0);
261 uint8_t buffer[buffer_length];
262 memset( buffer, 0, buffer_length );
263 bs.buffer.data = buffer;
264 bs.buffer.alloc = buffer_length;
265 /* Create a DTSSpecificBox. */
266 lsmash_bits_put( &bits, 32, 0 ); /* box size */
267 lsmash_bits_put( &bits, 32, ISOM_BOX_TYPE_DDTS.fourcc ); /* box type: 'ddts' */
268 lsmash_bits_put( &bits, 32, param->DTSSamplingFrequency );
269 lsmash_bits_put( &bits, 32, param->maxBitrate ); /* maxBitrate; setup by isom_update_bitrate_description */
270 lsmash_bits_put( &bits, 32, param->avgBitrate ); /* avgBitrate; setup by isom_update_bitrate_description */
271 lsmash_bits_put( &bits, 8, param->pcmSampleDepth );
272 lsmash_bits_put( &bits, 2, param->FrameDuration );
273 lsmash_bits_put( &bits, 5, param->StreamConstruction );
274 lsmash_bits_put( &bits, 1, param->CoreLFEPresent );
275 lsmash_bits_put( &bits, 6, param->CoreLayout );
276 lsmash_bits_put( &bits, 14, param->CoreSize );
277 lsmash_bits_put( &bits, 1, param->StereoDownmix );
278 lsmash_bits_put( &bits, 3, param->RepresentationType );
279 lsmash_bits_put( &bits, 16, param->ChannelLayout );
280 lsmash_bits_put( &bits, 1, param->MultiAssetFlag );
281 lsmash_bits_put( &bits, 1, param->LBRDurationMod );
282 lsmash_bits_put( &bits, 1, reserved_box_present );
283 lsmash_bits_put( &bits, 5, 0 ); /* Reserved */
284 /* ReservedBox */
285 if( reserved_box_present )
286 for( uint32_t i = 0; i < param->box->size; i++ )
287 lsmash_bits_put( &bits, 8, param->box->data[i] );
288 /* */
289 uint8_t *data = lsmash_bits_export_data( &bits, data_length );
290 /* Update box size. */
291 LSMASH_SET_BE32( data, *data_length );
292 return data;
295 int lsmash_setup_dts_specific_parameters_from_frame( lsmash_dts_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
297 lsmash_bits_t bits = { 0 };
298 lsmash_bs_t bs = { 0 };
299 uint8_t buffer[DTS_MAX_EXSS_SIZE] = { 0 };
300 bs.buffer.data = buffer;
301 bs.buffer.store = data_length;
302 bs.buffer.alloc = DTS_MAX_EXSS_SIZE;
303 dts_info_t *info = &(dts_info_t){ .bits = &bits };
304 info->bits = &bits;
305 lsmash_bits_init( &bits, &bs );
306 memcpy( buffer, data, LSMASH_MIN( data_length, DTS_MAX_EXSS_SIZE ) );
307 dts_setup_parser( info );
308 uint64_t next_frame_pos = 0;
309 while( 1 )
311 /* Seek to the head of the next syncframe. */
312 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
313 /* Check the remainder length of the buffer.
314 * If there is enough length, then continue to parse the frame in it.
315 * The length 10 is the required byte length to get frame size. */
316 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
317 if( bs.eob || (bs.eof && remain_size < 10) )
318 goto setup_param; /* No more valid data. */
319 /* Parse substream frame. */
320 dts_substream_type prev_substream_type = info->substream_type;
321 info->substream_type = dts_get_substream_type( info );
322 int (*dts_parse_frame)( dts_info_t * ) = NULL;
323 switch( info->substream_type )
325 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
326 case DTS_SUBSTREAM_TYPE_CORE :
327 if( prev_substream_type != DTS_SUBSTREAM_TYPE_NONE )
328 goto setup_param;
329 dts_parse_frame = dts_parse_core_substream;
330 break;
331 case DTS_SUBSTREAM_TYPE_EXTENSION :
333 uint8_t prev_exss_index = info->exss_index;
334 if( dts_get_exss_index( info, &info->exss_index ) < 0 )
335 return -1;
336 if( prev_substream_type == DTS_SUBSTREAM_TYPE_EXTENSION && info->exss_index <= prev_exss_index )
337 goto setup_param;
338 dts_parse_frame = dts_parse_extension_substream;
339 break;
341 default :
342 return -1;
344 info->frame_size = 0;
345 if( dts_parse_frame( info ) < 0 )
346 return -1; /* Failed to parse. */
347 next_frame_pos += info->frame_size;
349 setup_param:
350 dts_update_specific_param( info );
351 *param = info->ddts_param;
352 return 0;
355 static uint64_t dts_bits_get( lsmash_bits_t *bits, uint32_t width, uint64_t *bits_pos )
357 *bits_pos += width;
358 return lsmash_bits_get( bits, width );
361 static int dts_get_channel_count_from_channel_layout( uint16_t channel_layout )
363 #define DTS_CHANNEL_PAIR_MASK \
364 (DTS_CHANNEL_LAYOUT_L_R \
365 | DTS_CHANNEL_LAYOUT_LS_RS \
366 | DTS_CHANNEL_LAYOUT_LH_RH \
367 | DTS_CHANNEL_LAYOUT_LSR_RSR \
368 | DTS_CHANNEL_LAYOUT_LC_RC \
369 | DTS_CHANNEL_LAYOUT_LW_RW \
370 | DTS_CHANNEL_LAYOUT_LSS_RSS \
371 | DTS_CHANNEL_LAYOUT_LHS_RHS \
372 | DTS_CHANNEL_LAYOUT_LHR_RHR)
373 return lsmash_count_bits( channel_layout )
374 + lsmash_count_bits( channel_layout & DTS_CHANNEL_PAIR_MASK );
375 #undef DTS_CHANNEL_PAIR_MASK
378 static uint32_t dts_get_channel_layout_from_ls_mask32( uint32_t mask )
380 uint32_t layout = 0;
381 if( mask & DTS_LOUDSPEAKER_MASK32_C )
382 layout |= DTS_CHANNEL_LAYOUT_C;
383 if( mask & (DTS_LOUDSPEAKER_MASK32_L | DTS_LOUDSPEAKER_MASK32_R) )
384 layout |= DTS_CHANNEL_LAYOUT_L_R;
385 if( mask & (DTS_LOUDSPEAKER_MASK32_LS | DTS_LOUDSPEAKER_MASK32_RS) )
386 layout |= DTS_CHANNEL_LAYOUT_LS_RS;
387 if( mask & DTS_LOUDSPEAKER_MASK32_LFE1 )
388 layout |= DTS_CHANNEL_LAYOUT_LFE1;
389 if( mask & DTS_LOUDSPEAKER_MASK32_CS )
390 layout |= DTS_CHANNEL_LAYOUT_CS;
391 if( mask & (DTS_LOUDSPEAKER_MASK32_LH | DTS_LOUDSPEAKER_MASK32_RH) )
392 layout |= DTS_CHANNEL_LAYOUT_LH_RH;
393 if( mask & (DTS_LOUDSPEAKER_MASK32_LSR | DTS_LOUDSPEAKER_MASK32_RSR) )
394 layout |= DTS_CHANNEL_LAYOUT_LSR_RSR;
395 if( mask & DTS_LOUDSPEAKER_MASK32_CH )
396 layout |= DTS_CHANNEL_LAYOUT_CH;
397 if( mask & DTS_LOUDSPEAKER_MASK32_OH )
398 layout |= DTS_CHANNEL_LAYOUT_OH;
399 if( mask & (DTS_LOUDSPEAKER_MASK32_LC | DTS_LOUDSPEAKER_MASK32_RC) )
400 layout |= DTS_CHANNEL_LAYOUT_LC_RC;
401 if( mask & (DTS_LOUDSPEAKER_MASK32_LW | DTS_LOUDSPEAKER_MASK32_RW) )
402 layout |= DTS_CHANNEL_LAYOUT_LW_RW;
403 if( mask & (DTS_LOUDSPEAKER_MASK32_LSS | DTS_LOUDSPEAKER_MASK32_RSS) )
404 layout |= DTS_CHANNEL_LAYOUT_LSS_RSS;
405 if( mask & DTS_LOUDSPEAKER_MASK32_LFE2 )
406 layout |= DTS_CHANNEL_LAYOUT_LFE2;
407 if( mask & (DTS_LOUDSPEAKER_MASK32_LHS | DTS_LOUDSPEAKER_MASK32_RHS) )
408 layout |= DTS_CHANNEL_LAYOUT_LHS_RHS;
409 if( mask & DTS_LOUDSPEAKER_MASK32_CHR )
410 layout |= DTS_CHANNEL_LAYOUT_CHR;
411 if( mask & (DTS_LOUDSPEAKER_MASK32_LHR | DTS_LOUDSPEAKER_MASK32_RHR) )
412 layout |= DTS_CHANNEL_LAYOUT_LHR_RHR;
413 return layout;
416 /* for channels which cannot be expressed by ChannelLayout; CL, LL and RL */
417 static inline uint8_t dts_get_lower_channels_from_ls_mask32( uint32_t mask )
419 return (mask >> 25) & 0x7;
422 static void dts_parse_xll_navigation( lsmash_bits_t *bits, dts_xll_info_t *xll, int nuBits4ExSSFsize, uint64_t *bits_pos )
424 xll->size = dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ) + 1; /* nuExSSXLLFsize (nuBits4ExSSFsize) */
425 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSXLLSyncPresent (1) */
427 dts_bits_get( bits, 4, bits_pos ); /* nuPeakBRCntrlBuffSzkB (4) */
428 int nuBitsInitDecDly = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitsInitDecDly (5) */
429 dts_bits_get( bits, nuBitsInitDecDly, bits_pos ); /* nuInitLLDecDlyFrames (nuBitsInitDecDly) */
430 dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ); /* nuExSSXLLSyncOffset (nuBits4ExSSFsize) */
434 static void dts_parse_lbr_navigation( lsmash_bits_t *bits, dts_lbr_info_t *lbr, uint64_t *bits_pos )
436 lbr->size = dts_bits_get( bits, 14, bits_pos ); /* nuExSSLBRFsize (14) */
437 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSLBRSyncPresent (1) */
438 dts_bits_get( bits, 2, bits_pos ); /* nuExSSLBRSyncDistInFrames (2) */
441 static int dts_parse_asset_descriptor( dts_info_t *info, uint64_t *bits_pos )
443 lsmash_bits_t *bits = info->bits;
444 dts_extension_info_t *exss = &info->exss[ info->exss_index ];
445 /* Audio asset descriptor */
446 uint64_t asset_descriptor_pos = *bits_pos;
447 int nuAssetDescriptFsize = dts_bits_get( bits, 9, bits_pos ) + 1; /* nuAssetDescriptFsize (9) */
448 dts_audio_asset_t *asset = &exss->asset[ dts_bits_get( bits, 3, bits_pos ) ]; /* nuAssetIndex (3) */
449 /* Static metadata */
450 int bEmbeddedStereoFlag = 0;
451 int bEmbeddedSixChFlag = 0;
452 int nuTotalNumChs = 0;
453 if( exss->bStaticFieldsPresent )
455 if( dts_bits_get( bits, 1, bits_pos ) ) /* bAssetTypeDescrPresent (1)*/
456 dts_bits_get( bits, 4, bits_pos ); /* nuAssetTypeDescriptor (4) */
457 if( dts_bits_get( bits, 1, bits_pos ) ) /* bLanguageDescrPresent (1) */
458 dts_bits_get( bits, 24, bits_pos ); /* LanguageDescriptor (24) */
459 if( dts_bits_get( bits, 1, bits_pos ) )
461 int nuInfoTextByteSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nuInfoTextByteSize (10) */
462 dts_bits_get( bits, nuInfoTextByteSize * 8, bits_pos ); /* InfoTextString (nuInfoTextByteSize) */
464 int nuBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitResolution (5) */
465 exss->bit_resolution = LSMASH_MAX( exss->bit_resolution, nuBitResolution );
466 int nuMaxSampleRate = dts_bits_get( bits, 4, bits_pos ); /* nuMaxSampleRate (4) */
467 static const uint32_t source_sample_rate_table[16] =
469 8000, 16000, 32000, 64000, 128000,
470 22050, 44100, 88200, 176400, 352800,
471 12000, 24000, 48000, 96000, 192000, 384000
473 exss->sampling_frequency = LSMASH_MAX( exss->sampling_frequency, source_sample_rate_table[nuMaxSampleRate] );
474 nuTotalNumChs = dts_bits_get( bits, 8, bits_pos ) + 1; /* nuTotalNumChs (8) */
475 asset->bOne2OneMapChannels2Speakers = dts_bits_get( bits, 1, bits_pos ); /* bOne2OneMapChannels2Speakers (1) */
476 if( asset->bOne2OneMapChannels2Speakers )
478 if( nuTotalNumChs > 2 )
480 bEmbeddedStereoFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedStereoFlag (1) */
481 exss->stereo_downmix |= bEmbeddedStereoFlag;
483 if( nuTotalNumChs > 6 )
484 bEmbeddedSixChFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedSixChFlag (1) */
485 int nuNumBits4SAMask;
486 if( dts_bits_get( bits, 1, bits_pos ) ) /* bSpkrMaskEnabled (1) */
488 nuNumBits4SAMask = (dts_bits_get( bits, 2, bits_pos ) + 1) << 2; /* nuNumBits4SAMask (2) */
489 asset->channel_layout |= dts_bits_get( bits, nuNumBits4SAMask, bits_pos ); /* nuSpkrActivityMask (nuNumBits4SAMask) */
491 else
492 /* The specification doesn't mention the value of nuNumBits4SAMask if bSpkrMaskEnabled is set to 0. */
493 nuNumBits4SAMask = 16;
494 int nuNumSpkrRemapSets = dts_bits_get( bits, 3, bits_pos );
495 int nuStndrSpkrLayoutMask[8] = { 0 };
496 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
497 nuStndrSpkrLayoutMask[ns] = dts_bits_get( bits, nuNumBits4SAMask, bits_pos );
498 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
500 int nuNumSpeakers = dts_get_channel_count_from_channel_layout( nuStndrSpkrLayoutMask[ns] );
501 int nuNumDecCh4Remap = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuNumDecCh4Remap[ns] (5) */
502 for( int nCh = 0; nCh < nuNumSpeakers; nCh++ )
504 uint32_t nuRemapDecChMask = dts_bits_get( bits, nuNumDecCh4Remap, bits_pos );
505 int nCoef = lsmash_count_bits( nuRemapDecChMask );
506 for( int nc = 0; nc < nCoef; nc++ )
507 dts_bits_get( bits, 5, bits_pos ); /* nuSpkrRemapCodes[ns][nCh][nc] (5) */
511 else
513 asset->nuRepresentationType = dts_bits_get( bits, 3, bits_pos ); /* nuRepresentationType (3) */
514 if( asset->nuRepresentationType == 2
515 || asset->nuRepresentationType == 3 )
516 nuTotalNumChs = 2;
519 /* Dynamic metadata */
520 int bDRCCoefPresent = dts_bits_get( bits, 1, bits_pos ); /* bDRCCoefPresent (1) */
521 if( bDRCCoefPresent )
522 dts_bits_get( bits, 8, bits_pos ); /* nuDRCCode (8) */
523 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDialNormPresent (1) */
524 dts_bits_get( bits, 5, bits_pos ); /* nuDialNormCode (5) */
525 if( bDRCCoefPresent && bEmbeddedStereoFlag )
526 dts_bits_get( bits, 8, bits_pos ); /* nuDRC2ChDmixCode (8) */
527 int bMixMetadataPresent;
528 if( exss->bMixMetadataEnbl )
529 bMixMetadataPresent = dts_bits_get( bits, 1, bits_pos ); /* bMixMetadataPresent (1) */
530 else
531 bMixMetadataPresent = 0;
532 if( bMixMetadataPresent )
534 dts_bits_get( bits, 7, bits_pos ); /* bExternalMixFlag (1)
535 * nuPostMixGainAdjCode (7) */
536 if( dts_bits_get( bits, 2, bits_pos ) < 3 ) /* nuControlMixerDRC (2) */
537 dts_bits_get( bits, 3, bits_pos ); /* nuLimit4EmbeddedDRC (3) */
538 else
539 dts_bits_get( bits, 8, bits_pos ); /* nuCustomDRCCode (8) */
540 int bEnblPerChMainAudioScale = dts_bits_get( bits, 1, bits_pos ); /* bEnblPerChMainAudioScale (1) */
541 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
542 if( bEnblPerChMainAudioScale )
543 for( uint8_t nCh = 0; nCh < exss->nNumMixOutCh[ns]; nCh++ )
544 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][nCh] (6) */
545 else
546 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][0] (6) */
547 int nEmDM = 1;
548 int nDecCh[3] = { nuTotalNumChs, 0, 0 };
549 if( bEmbeddedSixChFlag )
551 nDecCh[nEmDM] = 6;
552 ++nEmDM;
554 if( bEmbeddedStereoFlag )
556 nDecCh[nEmDM] = 2;
557 ++nEmDM;
559 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
560 for( int nE = 0; nE < nEmDM; nE++ )
561 for( int nCh = 0; nCh < nDecCh[nE]; nCh++ )
563 int nuMixMapMask = dts_bits_get( bits, exss->nNumMixOutCh[ns], bits_pos ); /* nuMixMapMask (nNumMixOutCh[ns]) */
564 int nuNumMixCoefs = lsmash_count_bits( nuMixMapMask );
565 for( int nC = 0; nC < nuNumMixCoefs; nC++ )
566 dts_bits_get( bits, 6, bits_pos ); /* nuMixCoeffs[ns][nE][nCh][nC] (6) */
569 /* Decoder navigation data */
570 asset->nuCodingMode = dts_bits_get( bits, 2, bits_pos ); /* nuCodingMode (2) */
571 switch( asset->nuCodingMode )
573 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
575 int nuCoreExtensionMask = dts_bits_get( bits, 12, bits_pos ); /* nuCoreExtensionMask (12) */
576 asset->nuCoreExtensionMask = nuCoreExtensionMask;
577 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
579 asset->core.frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSCoreFsize (14) */
580 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSCoreSyncPresent (1) */
581 dts_bits_get( bits, 2, bits_pos ); /* nuExSSCoreSyncDistInFrames (2) */
583 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
584 asset->xbr_size = dts_bits_get( bits, 14, bits_pos ) + 1;
585 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
586 asset->core.xxch.size = dts_bits_get( bits, 14, bits_pos ) + 1;
587 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
588 asset->x96_size = dts_bits_get( bits, 12, bits_pos ) + 1;
589 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
590 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
591 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
592 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
593 break;
595 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
596 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
597 break;
598 case 2 : /* DTS-HD Low bit-rate mode */
599 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
600 break;
601 case 3 : /* Auxiliary coding mode */
602 asset->aux_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSAuxFsize (14) */
603 break;
604 default :
605 assert( 0 );
606 break;
608 dts_bits_get( bits, nuAssetDescriptFsize * 8 - (*bits_pos - asset_descriptor_pos), bits_pos ); /* Skip remaining part of Audio asset descriptor. */
609 return bits->bs->error ? -1 : 0;
612 static int dts_parse_xxch( dts_info_t *info, uint64_t *bits_pos, dts_xxch_info_t *xxch )
614 lsmash_bits_t *bits = info->bits;
615 /* XXCH Frame Header */
616 uint64_t xxch_pos = *bits_pos - 32; /* SYNCXXCh (32) */
617 uint64_t nuHeaderSizeXXCh = dts_bits_get( bits, 6, bits_pos ) + 1; /* nuHeaderSizeXXCh (6) */
618 dts_bits_get( bits, 1, bits_pos ); /* bCRCPresent4ChSetHeaderXXCh (1) */
619 int nuBits4SpkrMaskXXCh = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBits4SpkrMaskXXCh (5) */
620 int nuNumChSetsInXXCh = dts_bits_get( bits, 2, bits_pos ) + 1; /* nuNumChSetsInXXCh (2) */
621 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
622 dts_bits_get( bits, 14, bits_pos ); /* pnuChSetFsizeXXCh[nChSet] - 1 (14) */
623 /* A 5.1 decoder uses this AMODE to configure its decoded outputs to C, L, R, Ls and Rs layout.
624 * On the other hand a 7.1 decoder ignores the AMODE information from the core stream and uses
625 * instead the nuCoreSpkrActivityMask (C, L, R, LFE1, Lss and Rss) and the nuXXChSpkrLayoutMask
626 * (Lsr and Rsr) from the XXCh stream to get the original 7.1 speaker layout (C, L, R, LFE1, Lss,
627 * Rsr, Lsr and Rsr) and configures its outputs accordingly. */
628 uint32_t xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos ); /* nuCoreSpkrActivityMask (nuBits4SpkrMaskXXCh) */
629 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
630 xxch->lower_planes = dts_get_lower_channels_from_ls_mask32( xxch_mask );
631 dts_bits_get( bits, nuHeaderSizeXXCh * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Frame Header. */
632 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
634 /* XXCH Channel Set Header */
635 xxch_pos = *bits_pos;
636 uint64_t nuXXChChSetHeaderSize = dts_bits_get( bits, 7, bits_pos ) + 1; /* nuXXChChSetHeaderSize (7)*/
637 dts_bits_get( bits, 3, bits_pos ); /* nuChInChSetXXCh (3) */
638 if( nuBits4SpkrMaskXXCh > 6 )
640 xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh - 6, bits_pos ) << 6; /* nuXXChSpkrLayoutMask (nuBits4SpkrMaskXXCh - 6) */
641 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
642 xxch->lower_planes |= dts_get_lower_channels_from_ls_mask32( xxch_mask );
644 #if 0 /* FIXME: Can we detect stereo downmixing from only XXCH data within the core substream? */
645 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDownMixCoeffCodeEmbedded (1) */
647 int bDownMixEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownMixEmbedded (1) */
648 dts_bits_get( bits, 6, bits_pos ); /* nDmixScaleFactor (6) */
649 uint32_t DownMixChMapMask[8];
650 for( int nCh = 0; nCh < nuChInChSetXXCh; nCh++ )
651 DownMixChMapMask[nCh] = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos );
653 #endif
654 dts_bits_get( bits, nuXXChChSetHeaderSize * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Channel Set Header. */
656 return 0;
659 static int dts_parse_core_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
661 if( core->extension_audio_descriptor == 0
662 || core->extension_audio_descriptor == 3 )
663 return -1;
664 if( dts_parse_xxch( info, bits_pos, &core->xxch ) < 0 )
665 return -1;
666 info->flags |= DTS_CORE_SUBSTREAM_XXCH_FLAG;
667 return info->bits->bs->error ? -1 : 0;
670 static int dts_parse_exss_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
672 lsmash_bits_t *bits = info->bits;
673 if( DTS_SYNCWORD_XXCH != dts_bits_get( bits, 32, bits_pos ) )
674 return -1;
675 if( dts_parse_xxch( info, bits_pos, &core->xxch ) < 0 )
676 return -1;
677 info->flags |= DTS_EXT_SUBSTREAM_XXCH_FLAG;
678 return bits->bs->error ? -1 : 0;
681 static int dts_parse_core_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
683 if( core->extension_audio_descriptor != 2
684 && core->extension_audio_descriptor != 3 )
685 return 0; /* Probably this is not an X96 extension. We skip this anyway. */
686 lsmash_bits_t *bits = info->bits;
687 /* DTS_BCCORE_X96 Frame Header */
688 /* SYNCX96 (32) */
689 /* To reduce the probability of false synchronization caused by the presence of pseudo sync words, it is
690 * imperative to check the distance between the detected sync word and the end of current frame. This
691 * distance in bytes shall match the value of FSIZE96. */
692 uint64_t FSIZE96 = ((lsmash_bs_show_byte( bits->bs, 0 ) << 4)
693 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 4) & 0x0F)) + 1;
694 if( core->frame_size * 8 != (*bits_pos - 32 + FSIZE96 * 8) )
695 return 0; /* Encountered four emulation bytes (pseudo sync word). */
696 dts_bits_get( bits, 16, bits_pos ); /* FSIZE96 (12)
697 * REVNO (4) */
698 core->sampling_frequency *= 2;
699 core->frame_duration *= 2;
700 info->flags |= DTS_CORE_SUBSTREAM_X96_FLAG;
701 return bits->bs->error ? -1 : 0;
704 static int dts_parse_core_xch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
706 if( core->extension_audio_descriptor != 0
707 && core->extension_audio_descriptor != 3 )
708 return 0; /* Probably this is not an XCh extension. We skip this anyway. */
709 lsmash_bits_t *bits = info->bits;
710 /* XCH Frame Header */
711 /* XChSYNC (32) */
712 /* For compatibility reasons with legacy bitstreams the estimated distance in bytes is checked against
713 * the XChFSIZE+1 as well as the XChFSIZE. The XCh synchronization is pronounced if the distance matches
714 * either of these two values. */
715 uint64_t XChFSIZE = (lsmash_bs_show_byte( bits->bs, 0 ) << 2)
716 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 6) & 0x03);
717 if( core->frame_size * 8 != (*bits_pos - 32 + (XChFSIZE + 1) * 8)
718 && core->frame_size * 8 != (*bits_pos - 32 + XChFSIZE * 8) )
719 return 0; /* Encountered four emulation bytes (pseudo sync word). */
720 if( ((lsmash_bs_show_byte( bits->bs, 1 ) >> 2) & 0xF) != 1 )
721 return 0; /* A known value of AMODE is only 1. Otherwise just skip. */
722 dts_bits_get( bits, 16, bits_pos ); /* XChFSIZE (10)
723 * AMODE (4)
724 * byte align (2) */
725 core->channel_layout |= DTS_CHANNEL_LAYOUT_CS;
726 info->flags |= DTS_CORE_SUBSTREAM_XCH_FLAG;
727 return bits->bs->error ? -1 : 0;
730 static int dts_parse_exss_xbr( dts_info_t *info, uint64_t *bits_pos )
732 lsmash_bits_t *bits = info->bits;
733 /* XBR Frame Header */
734 uint64_t xbr_pos = *bits_pos;
735 if( DTS_SYNCWORD_XBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXBR (32) */
736 return -1;
737 uint64_t nHeaderSizeXBR = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
738 dts_bits_get( bits, nHeaderSizeXBR * 8 - (*bits_pos - xbr_pos), bits_pos ); /* Skip the remaining bits in XBR Frame Header. */
739 info->flags |= DTS_EXT_SUBSTREAM_XBR_FLAG;
740 return bits->bs->error ? -1 : 0;
743 static int dts_parse_exss_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
745 lsmash_bits_t *bits = info->bits;
746 /* DTS_EXSUB_STREAM_X96 Frame Header */
747 uint64_t x96_pos = *bits_pos;
748 if( DTS_SYNCWORD_X96K != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCX96 (32) */
749 return -1;
750 uint64_t nHeaderSizeX96 = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
751 dts_bits_get( bits, nHeaderSizeX96 * 8 - (*bits_pos - x96_pos), bits_pos ); /* Skip the remaining bits in DTS_EXSUB_STREAM_X96 Frame Header. */
752 /* What the fuck! The specification drops 'if' sentence.
753 * We assume the same behaviour for core substream. */
754 core->sampling_frequency *= 2;
755 core->frame_duration *= 2;
756 info->flags |= DTS_EXT_SUBSTREAM_X96_FLAG;
757 return bits->bs->error ? -1 : 0;
760 static int dts_parse_exss_lbr( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
762 lsmash_bits_t *bits = info->bits;
763 dts_lbr_info_t *lbr = &asset->lbr;
764 if( DTS_SYNCWORD_LBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCEXTLBR (32) */
765 return -1;
766 int ucFmtInfoCode = dts_bits_get( bits, 8, bits_pos );
767 if( ucFmtInfoCode == 2 )
769 /* LBR decoder initialization data */
770 int nLBRSampleRateCode = dts_bits_get( bits, 8, bits_pos ); /* nLBRSampleRateCode (8) */
771 int usLBRSpkrMask = dts_bits_get( bits, 16, bits_pos ); /* usLBRSpkrMask (16) */
772 dts_bits_get( bits, 16, bits_pos ); /* nLBRversion (16) */
773 int nLBRCompressedFlags = dts_bits_get( bits, 8, bits_pos ); /* nLBRCompressedFlags (8) */
774 dts_bits_get( bits, 40, bits_pos ); /* nLBRBitRateMSnybbles (8)
775 * nLBROriginalBitRate_LSW (16)
776 * nLBRScaledBitRate_LSW (16) */
777 static const uint32_t source_sample_rate_table[16] =
779 8000, 16000, 32000, 0, 0,
780 11025, 22050, 44100, 0, 0,
781 12000, 24000, 48000, 0, 0, 0
783 enum LBRFlags
785 LBR_FLAG_24_BIT_SAMPLES = 0x01, /* 0b00000001 */
786 LBR_FLAG_USE_LFE = 0x02, /* 0b00000010 */
787 LBR_FLAG_BANDLMT_MASK = 0x1C, /* 0b00011100 */
788 LBR_FLAG_STEREO_DOWNMIX = 0x20, /* 0b00100000 */
789 LBR_FLAG_MULTICHANNEL_DOWNMIX = 0x40, /* 0b01000000 */
791 lbr->sampling_frequency = source_sample_rate_table[nLBRSampleRateCode];
792 lbr->frame_duration = lbr->sampling_frequency < 16000 ? 1024
793 : lbr->sampling_frequency < 32000 ? 2048
794 : 4096;
795 lbr->channel_layout = ((usLBRSpkrMask >> 8) & 0xff) | ((usLBRSpkrMask << 8) & 0xff00); /* usLBRSpkrMask is little-endian. */
796 lbr->stereo_downmix |= !!(nLBRCompressedFlags & LBR_FLAG_STEREO_DOWNMIX);
797 lbr->lfe_present |= !!(nLBRCompressedFlags & LBR_FLAG_USE_LFE);
798 lbr->duration_modifier |= ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x04)
799 || ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x0C);
800 lbr->sample_size = (nLBRCompressedFlags & LBR_FLAG_24_BIT_SAMPLES) ? 24 : 16;
802 else if( ucFmtInfoCode != 1 )
803 return -1; /* unknown */
804 info->flags |= DTS_EXT_SUBSTREAM_LBR_FLAG;
805 return bits->bs->error ? -1 : 0;
808 static int dts_parse_exss_xll( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
810 lsmash_bits_t *bits = info->bits;
811 dts_xll_info_t *xll = &asset->xll;
812 /* Common Header */
813 uint64_t xll_pos = *bits_pos;
814 if( DTS_SYNCWORD_XLL != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXLL (32) */
815 return -1;
816 dts_bits_get( bits, 4, bits_pos ); /* nVersion (4) */
817 uint64_t nHeaderSize = dts_bits_get( bits, 8, bits_pos ) + 1; /* nHeaderSize (8) */
818 int nBits4FrameFsize = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4FrameFsize (5) */
819 dts_bits_get( bits, nBits4FrameFsize, bits_pos ); /* nLLFrameSize (nBits4FrameFsize) */
820 int nNumChSetsInFrame = dts_bits_get( bits, 4, bits_pos ) + 1; /* nNumChSetsInFrame (4) */
821 uint16_t nSegmentsInFrame = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSegmentsInFrame (4) */
822 uint16_t nSmplInSeg = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSmplInSeg (4) */
823 dts_bits_get( bits, 5, bits_pos ); /* nBits4SSize (5) */
824 dts_bits_get( bits, 3, bits_pos ); /* nBandDataCRCEn (2)
825 * bScalableLSBs (1) */
826 int nBits4ChMask = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4ChMask (5) */
827 dts_bits_get( bits, nHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Common Header. */
828 int sum_nChSetLLChannel = 0;
829 uint32_t nFs1 = 0;
830 int nNumFreqBands1 = 0;
831 xll->channel_layout = 0;
832 for( int nChSet = 0; nChSet < nNumChSetsInFrame; nChSet++ )
834 /* Channel Set Sub-Header */
835 xll_pos = *bits_pos;
836 uint64_t nChSetHeaderSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nChSetHeaderSize (10) */
837 int nChSetLLChannel = dts_bits_get( bits, 4, bits_pos ) + 1; /* nChSetLLChannel (4) */
838 dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* nResidualChEncode (nChSetLLChannel) */
839 uint8_t nBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBitResolution (5) */
840 dts_bits_get( bits, 5, bits_pos ); /* nBitWidth (5) */
841 xll->pcm_resolution = LSMASH_MAX( xll->pcm_resolution, nBitResolution );
842 static const uint32_t source_sample_rate_table[16] =
844 8000, 16000, 32000, 64000, 128000,
845 22050, 44100, 88200, 176400, 352800,
846 12000, 24000, 48000, 96000, 192000, 384000
848 int sFreqIndex = dts_bits_get( bits, 4, bits_pos ); /* sFreqIndex (4) */
849 uint32_t nFs = source_sample_rate_table[sFreqIndex];
850 dts_bits_get( bits, 2, bits_pos ); /* nFsInterpolate (2) */
851 int nReplacementSet = dts_bits_get( bits, 2, bits_pos ); /* nReplacementSet (2) */
852 if( nReplacementSet > 0 )
853 dts_bits_get( bits, 1, bits_pos ); /* bActiveReplaceSet (1) */
854 if( asset->bOne2OneMapChannels2Speakers )
856 /* Downmix is allowed only when the encoded channel represents a signal feed to a corresponding loudspeaker. */
857 int bPrimaryChSet = dts_bits_get( bits, 1, bits_pos ); /* bPrimaryChSet (1) */
858 int bDownmixCoeffCodeEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownmixCoeffCodeEmbedded (1) */
859 int nLLDownmixType = 0x7; /* 0b111: Unused */
860 if( bDownmixCoeffCodeEmbedded )
862 dts_bits_get( bits, 1, bits_pos ); /* bDownmixEmbedded (1) */
863 if( bPrimaryChSet )
864 nLLDownmixType = dts_bits_get( bits, 3, bits_pos ); /* nLLDownmixType (3) */
866 int bHierChSet = dts_bits_get( bits, 1, bits_pos ); /* bHierChSet (1) */
867 if( bDownmixCoeffCodeEmbedded )
869 /* N: the number of channels in the current channel set
870 * for non-primary channel set, adding +1 for the down scaling coefficients that prevent overflow
871 * M: the number of channels that the current channel set is mixed into
872 * Downmix coefficients are transmitted using 9-bit codes. */
873 static const int downmix_channel_count_table[8] = { 1, 2, 2, 3, 3, 4, 4, 0 };
874 int N = nChSetLLChannel + (bPrimaryChSet ? 0 : 1);
875 int M = bPrimaryChSet ? downmix_channel_count_table[nLLDownmixType] : sum_nChSetLLChannel;
876 int nDownmixCoeffs = N * M;
877 dts_bits_get( bits, nDownmixCoeffs * 9, bits_pos ); /* DownmixCoeffs (nDownmixCoeffs * 9) */
878 if( bPrimaryChSet && downmix_channel_count_table[nLLDownmixType] == 2 )
879 xll->stereo_downmix |= 1;
881 if( bHierChSet )
882 sum_nChSetLLChannel += nChSetLLChannel;
883 if( dts_bits_get( bits, 1, bits_pos ) ) /* bChMaskEnabled (1) */
885 uint32_t nChMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nChMask (nBits4ChMask) */
886 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nChMask );
887 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nChMask );
889 else
890 dts_bits_get( bits, 25 * nChSetLLChannel, bits_pos ); /* RadiusDelta[ch] (9)
891 * Theta[ch] (9)
892 * Phi[ch] (7)
893 * per channel */
895 else
897 /* No downmixing is allowed and each channel set is the primary channel set. */
898 if( dts_bits_get( bits, 1, bits_pos ) ) /* bMappingCoeffsPresent (1) */
900 int nBitsCh2SpkrCoef = 6 + 2 * dts_bits_get( bits, 3, bits_pos ); /* nBitsCh2SpkrCoef (3) */
901 int nNumSpeakerConfigs = dts_bits_get( bits, 2, bits_pos ) + 1; /* nNumSpeakerConfigs (2) */
902 for( int nSpkrConf = 0; nSpkrConf < nNumSpeakerConfigs; nSpkrConf++ )
904 int pnActiveChannelMask = dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* pnActiveChannelMask[nSpkrConf] (nChSetLLChannel) */
905 int pnNumSpeakers = dts_bits_get( bits, 6, bits_pos ) + 1; /* pnNumSpeakers[nSpkrConf] (6) */
906 int bSpkrMaskEnabled = dts_bits_get( bits, 1, bits_pos ); /* bSpkrMaskEnabled (1) */
907 if( bSpkrMaskEnabled )
909 uint32_t nSpkrMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nSpkrMask[nSpkrConf] (nBits4ChMask) */
910 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nSpkrMask );
911 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nSpkrMask );
913 for( int nSpkr = 0; nSpkr < pnNumSpeakers; nSpkr++ )
915 if( !bSpkrMaskEnabled )
916 dts_bits_get( bits, 25, bits_pos ); /* ChSetSpeakerConfiguration (25) */
917 for( int nCh = 0; nCh < nChSetLLChannel; nCh++ )
918 if( pnActiveChannelMask & (1 << nCh) )
919 dts_bits_get( bits, nBitsCh2SpkrCoef, bits_pos ); /* pnCh2SpkrMapCoeff (nBitsCh2SpkrCoef) */
924 int nNumFreqBands;
925 if( nFs > 96000 )
927 if( dts_bits_get( bits, 1, bits_pos ) ) /* bXtraFreqBands (1) */
928 nNumFreqBands = nFs > 192000 ? 4 : 2;
929 else
930 nNumFreqBands = nFs > 192000 ? 2 : 1;
932 else
933 nNumFreqBands = 1;
934 uint32_t nSmplInSeg_nChSet;
935 if( nChSet == 0 )
937 nFs1 = nFs;
938 nNumFreqBands1 = nNumFreqBands;
939 nSmplInSeg_nChSet = nSmplInSeg;
941 else
942 nSmplInSeg_nChSet = (nSmplInSeg * (nFs * nNumFreqBands1)) / (nFs1 * nNumFreqBands);
943 if( xll->sampling_frequency < nFs )
945 xll->sampling_frequency = nFs;
946 uint32_t samples_per_band_in_frame = nSegmentsInFrame * nSmplInSeg_nChSet;
947 xll->frame_duration = samples_per_band_in_frame * nNumFreqBands;
949 dts_bits_get( bits, nChSetHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Channel Set Sub-Header. */
951 info->flags |= DTS_EXT_SUBSTREAM_XLL_FLAG;
952 return bits->bs->error ? -1 : 0;
955 static uint16_t dts_generate_channel_layout_from_core( int channel_arrangement )
957 static const uint16_t channel_layout_map_table[] =
959 DTS_CHANNEL_LAYOUT_C,
960 DTS_CHANNEL_LAYOUT_L_R, /* dual mono */
961 DTS_CHANNEL_LAYOUT_L_R, /* stereo */
962 DTS_CHANNEL_LAYOUT_L_R, /* sum-difference */
963 DTS_CHANNEL_LAYOUT_L_R, /* Lt/Rt */
964 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R,
965 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
966 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
967 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
968 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
969 DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
970 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR | DTS_CHANNEL_LAYOUT_OH,
971 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR,
972 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS,
973 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS | DTS_CHANNEL_LAYOUT_LSR_RSR,
974 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS
976 return channel_arrangement < 16 ? channel_layout_map_table[channel_arrangement] : 0;
979 static int dts_parse_core( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
981 lsmash_bits_t *bits = info->bits;
982 memset( core, 0, sizeof(dts_core_info_t) );
983 /* SYNC (32) */
984 int frame_type = dts_bits_get( bits, 1, bits_pos ); /* FTYPE (1) */
985 int deficit_sample_count = dts_bits_get( bits, 5, bits_pos ); /* SHORT (5) */
986 if( frame_type == 1 && deficit_sample_count != 31 )
987 return -1; /* Any normal frame (FTYPE == 1) must have SHORT == 31. */
988 int crc_present_flag = dts_bits_get( bits, 1, bits_pos ); /* CPF (1) */
989 int num_of_pcm_sample_blocks = dts_bits_get( bits, 7, bits_pos ) + 1; /* NBLKS (7) */
990 if( num_of_pcm_sample_blocks <= 5 )
991 return -1;
992 core->frame_duration = 32 * num_of_pcm_sample_blocks;
993 if( frame_type == 1
994 && core->frame_duration != 256
995 && core->frame_duration != 512 && core->frame_duration != 1024
996 && core->frame_duration != 2048 && core->frame_duration != 4096 )
997 return -1; /* For any normal frame, the actual number of PCM core samples per channel must be
998 * either 4096, 2048, 1024, 512, or 256 samples per channel. */
999 core->frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* FSIZE (14) */
1000 if( core->frame_size < DTS_MIN_CORE_SIZE )
1001 return -1;
1002 core->channel_arrangement = dts_bits_get( bits, 6, bits_pos ); /* AMODE (6) */
1003 core->channel_layout = dts_generate_channel_layout_from_core( core->channel_arrangement );
1004 int core_audio_sampling_frequency = dts_bits_get( bits, 4, bits_pos ); /* SFREQ (4) */
1005 static const uint32_t sampling_frequency_table[16] =
1008 8000, 16000, 32000, 0, 0,
1009 11025, 22050, 44100, 0, 0,
1010 12000, 24000, 48000, 0, 0
1012 core->sampling_frequency = sampling_frequency_table[core_audio_sampling_frequency];
1013 if( core->sampling_frequency == 0 )
1014 return -1; /* invalid */
1015 dts_bits_get( bits, 10, bits_pos ); /* Skip remainder 10 bits.
1016 * RATE (5)
1017 * MIX (1)
1018 * DYNF (1)
1019 * TIMEF (1)
1020 * AUXF (1)
1021 * HDCD (1) */
1022 core->extension_audio_descriptor = dts_bits_get( bits, 3, bits_pos ); /* EXT_AUDIO_ID (3)
1023 * Note: EXT_AUDIO_ID == 3 is defined in V1.2.1.
1024 * However, its definition disappears and is reserved in V1.3.1. */
1025 int extended_coding_flag = dts_bits_get( bits, 1, bits_pos ); /* EXT_AUDIO (1) */
1026 dts_bits_get( bits, 1, bits_pos ); /* ASPF (1) */
1027 int low_frequency_effects_flag = dts_bits_get( bits, 2, bits_pos ); /* LFF (2) */
1028 if( low_frequency_effects_flag == 0x3 )
1029 return -1; /* invalid */
1030 if( low_frequency_effects_flag )
1031 core->channel_layout |= DTS_CHANNEL_LAYOUT_LFE1;
1032 dts_bits_get( bits, 8 + crc_present_flag * 16, bits_pos ); /* HFLAG (1)
1033 * HCRC (16)
1034 * FILTS (1)
1035 * VERNUM (4)
1036 * CHIST (2) */
1037 int PCMR = dts_bits_get( bits, 3, bits_pos ); /* PCMR (3) */
1038 static const uint8_t source_resolution_table[8] = { 16, 16, 20, 20, 0, 24, 24, 0 };
1039 core->pcm_resolution = source_resolution_table[PCMR];
1040 if( core->pcm_resolution == 0 )
1041 return -1; /* invalid */
1042 dts_bits_get( bits, 6, bits_pos ); /* SUMF (1)
1043 * SUMS (1)
1044 * DIALNORM/UNSPEC (4) */
1045 if( extended_coding_flag )
1047 uint32_t syncword = dts_bits_get( bits, 24, bits_pos );
1048 uint64_t frame_size_bits = core->frame_size * 8;
1049 while( (*bits_pos + 24) < frame_size_bits )
1051 syncword = ((syncword << 8) & 0xffffff00) | dts_bits_get( bits, 8, bits_pos );
1052 switch( syncword )
1054 case DTS_SYNCWORD_XXCH :
1055 if( dts_parse_core_xxch( info, bits_pos, core ) )
1056 return -1;
1057 syncword = dts_bits_get( bits, 24, bits_pos );
1058 break;
1059 case DTS_SYNCWORD_X96K :
1060 if( dts_parse_core_x96( info, bits_pos, core ) )
1061 return -1;
1062 syncword = dts_bits_get( bits, 24, bits_pos );
1063 break;
1064 case DTS_SYNCWORD_XCH :
1065 if( dts_parse_core_xch( info, bits_pos, core ) )
1066 return -1;
1067 break;
1068 default :
1069 continue;
1073 return bits->bs->error ? -1 : 0;
1076 static int dts_parse_exss_core( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
1078 lsmash_bits_t *bits = info->bits;
1079 if( DTS_SYNCWORD_SUBSTREAM_CORE != dts_bits_get( bits, 32, bits_pos ) )
1080 return -1;
1081 if( dts_parse_core( info, bits_pos, &asset->core ) < 0 )
1082 return -1;
1083 info->flags |= DTS_EXT_SUBSTREAM_CORE_FLAG;
1084 return bits->bs->error ? -1 : 0;
1087 int dts_parse_core_substream( dts_info_t *info )
1089 lsmash_bits_t *bits = info->bits;
1090 uint64_t bits_pos = 0;
1091 if( DTS_SYNCWORD_CORE != dts_bits_get( bits, 32, &bits_pos ) )
1092 goto parse_fail;
1093 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0. */
1094 dts_extension_info_t *exss = &info->exss[0];
1095 if( dts_parse_core( info, &bits_pos, &exss->asset[0].core ) < 0 )
1096 goto parse_fail;
1097 exss->bBcCorePresent [0] = 1;
1098 exss->nuBcCoreExtSSIndex[0] = 0;
1099 exss->nuBcCoreAssetIndex[0] = 0;
1100 info->flags |= DTS_CORE_SUBSTREAM_CORE_FLAG;
1101 info->exss_count = 0;
1102 info->core = exss->asset[0].core;
1103 info->frame_size = exss->asset[0].core.frame_size;
1104 lsmash_bits_get_align( bits );
1105 return 0;
1106 parse_fail:
1107 lsmash_bits_get_align( bits );
1108 return -1;
1111 int dts_parse_extension_substream( dts_info_t *info )
1113 lsmash_bits_t *bits = info->bits;
1114 uint64_t bits_pos = 0;
1115 dts_bits_get( bits, 40, &bits_pos ); /* SYNCEXTSSH (32)
1116 * UserDefinedBits (8) */
1117 int nExtSSIndex = dts_bits_get( bits, 2, &bits_pos ); /* nExtSSIndex (2) */
1118 info->exss_index = nExtSSIndex;
1119 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1120 memset( exss, 0, sizeof(dts_extension_info_t) );
1121 int bHeaderSizeType = dts_bits_get( bits, 1, &bits_pos ); /* bHeaderSizeType (1) */
1122 int nuBits4Header = 8 + bHeaderSizeType * 4;
1123 int nuBits4ExSSFsize = 16 + bHeaderSizeType * 4;
1124 exss->nuBits4ExSSFsize = nuBits4ExSSFsize;
1125 uint32_t nuExtSSHeaderSize = dts_bits_get( bits, nuBits4Header, &bits_pos ) + 1; /* nuExtSSHeaderSize (8 or 12) */
1126 info->frame_size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuExtSSFsize (16 or 20) */
1127 if( info->frame_size < 10 )
1128 return -1;
1129 exss->bStaticFieldsPresent = dts_bits_get( bits, 1, &bits_pos ); /* bStaticFieldsPresent (1) */
1130 if( exss->bStaticFieldsPresent )
1132 dts_bits_get( bits, 2, &bits_pos ); /* nuRefClockCode (2) */
1133 exss->frame_duration = 512 * (dts_bits_get( bits, 3, &bits_pos ) + 1); /* nuExSSFrameDurationCode (3) */
1134 if( dts_bits_get( bits, 1, &bits_pos ) ) /* bTimeStampFlag (1) */
1135 dts_bits_get( bits, 36, &bits_pos ); /* nuTimeStamp (32)
1136 * nLSB (4) */
1137 exss->nuNumAudioPresnt = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAudioPresnt (3) */
1138 exss->nuNumAssets = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAssets (3) */
1139 /* The extension substreams with indexes lower than or equal to the index of the current extension substream can
1140 * be activated in the audio presentations indicated within the current extension substream. */
1141 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1142 exss->nuActiveExSSMask[nAuPr]
1143 = dts_bits_get( bits, nExtSSIndex + 1, &bits_pos ); /* nuActiveExSSMask[nAuPr] (nExtSSIndex + 1) */
1144 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1145 for( uint8_t nSS = 0; nSS <= nExtSSIndex; nSS++ )
1146 exss->nuActiveAssetMask[nAuPr][nSS]
1147 = ((exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1)
1148 ? dts_bits_get( bits, 8, &bits_pos ) /* nuActiveAssetMask[nAuPr][nSS] (8) */
1149 : 0;
1150 exss->bMixMetadataEnbl = dts_bits_get( bits, 1, &bits_pos ); /* bMixMetadataEnbl (1) */
1151 if( exss->bMixMetadataEnbl )
1153 dts_bits_get( bits, 2, &bits_pos ); /* nuMixMetadataAdjLevel (2) */
1154 int nuBits4MixOutMask = (dts_bits_get( bits, 2, &bits_pos ) + 1) << 2; /* nuBits4MixOutMask (2) */
1155 exss->nuNumMixOutConfigs = dts_bits_get( bits, 2, &bits_pos ) + 1; /* nuNumMixOutConfigs (2) */
1156 for( int ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
1158 int nuMixOutChMask = dts_bits_get( bits, nuBits4MixOutMask, &bits_pos ); /* nuMixOutChMask[ns] (nuBits4MixOutMask) */
1159 exss->nNumMixOutCh[ns] = dts_get_channel_count_from_channel_layout( nuMixOutChMask );
1163 else
1165 exss->nuNumAudioPresnt = 1;
1166 exss->nuNumAssets = 1;
1167 exss->bMixMetadataEnbl = 0;
1168 exss->nuNumMixOutConfigs = 0;
1170 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1171 exss->asset[nAst].size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuAssetFsize[nAst] - 1 (nuBits4ExSSFsize) */
1172 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1173 if( dts_parse_asset_descriptor( info, &bits_pos ) )
1174 goto parse_fail;
1175 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1176 exss->bBcCorePresent[nAuPr] = dts_bits_get( bits, 1, &bits_pos );
1177 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1178 if( exss->bBcCorePresent[nAuPr] )
1180 exss->nuBcCoreExtSSIndex[nAuPr] = dts_bits_get( bits, 2, &bits_pos );
1181 exss->nuBcCoreAssetIndex[nAuPr] = dts_bits_get( bits, 3, &bits_pos );
1183 dts_bits_get( bits, nuExtSSHeaderSize * 8 - bits_pos, &bits_pos );
1184 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1186 /* Asset Data */
1187 dts_audio_asset_t *asset = &exss->asset[nAst];
1188 uint32_t asset_pos = bits_pos;
1189 switch( asset->nuCodingMode )
1191 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
1193 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
1195 /* Core component */
1196 uint64_t core_pos = bits_pos;
1197 if( dts_parse_exss_core( info, &bits_pos, asset ) < 0 )
1198 goto parse_fail;
1199 dts_bits_get( bits, asset->core.frame_size * 8 - (bits_pos - core_pos), &bits_pos );
1201 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
1203 /* XBR extension */
1204 uint64_t xbr_pos = bits_pos;
1205 if( dts_parse_exss_xbr( info, &bits_pos ) < 0 )
1206 goto parse_fail;
1207 dts_bits_get( bits, asset->xbr_size * 8 - (bits_pos - xbr_pos), &bits_pos );
1209 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1211 /* XXCH extension */
1212 uint64_t xxch_pos = bits_pos;
1213 if( dts_parse_exss_xxch( info, &bits_pos, &asset->core ) < 0 )
1214 goto parse_fail;
1215 dts_bits_get( bits, asset->core.xxch.size * 8 - (bits_pos - xxch_pos), &bits_pos );
1217 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
1219 /* X96 extension */
1220 uint64_t x96_pos = bits_pos;
1221 if( dts_parse_exss_x96( info, &bits_pos, &asset->core ) < 0 )
1222 goto parse_fail;
1223 dts_bits_get( bits, asset->x96_size * 8 - (bits_pos - x96_pos), &bits_pos );
1225 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
1227 /* LBR component */
1228 uint64_t lbr_pos = bits_pos;
1229 if( dts_parse_exss_lbr( info, &bits_pos, asset ) < 0 )
1230 goto parse_fail;
1231 dts_bits_get( bits, asset->lbr.size * 8 - (bits_pos - lbr_pos), &bits_pos );
1233 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
1235 /* Lossless extension */
1236 uint64_t xll_pos = bits_pos;
1237 if( dts_parse_exss_xll( info, &bits_pos, asset ) < 0 )
1238 goto parse_fail;
1239 dts_bits_get( bits, asset->xll.size * 8 - (bits_pos - xll_pos), &bits_pos );
1241 break;
1243 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
1244 if( dts_parse_exss_xll( info, &bits_pos, asset ) < 0 )
1245 goto parse_fail;
1246 break;
1247 case 2 : /* DTS-HD Low bit-rate mode */
1248 if( dts_parse_exss_lbr( info, &bits_pos, asset ) < 0 )
1249 goto parse_fail;
1250 break;
1251 case 3 : /* Auxiliary coding mode */
1252 dts_bits_get( bits, asset->aux_size * 8, &bits_pos );
1253 break;
1255 dts_bits_get( bits, asset->size * 8 - (bits_pos - asset_pos), &bits_pos );
1257 dts_bits_get( bits, info->frame_size * 8 - bits_pos, &bits_pos );
1258 lsmash_bits_get_align( bits );
1259 if( info->exss_count < DTS_MAX_NUM_EXSS )
1260 info->exss_count += 1;
1261 return 0;
1262 parse_fail:
1263 lsmash_bits_get_align( bits );
1264 return -1;
1267 dts_substream_type dts_get_substream_type( dts_info_t *info )
1269 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 4 )
1270 return DTS_SUBSTREAM_TYPE_NONE;
1271 uint8_t *buffer = lsmash_bs_get_buffer_data( info->bits->bs );
1272 uint32_t syncword = LSMASH_4CC( buffer[0], buffer[1], buffer[2], buffer[3] );
1273 switch( syncword )
1275 case DTS_SYNCWORD_CORE :
1276 return DTS_SUBSTREAM_TYPE_CORE;
1277 case DTS_SYNCWORD_SUBSTREAM :
1278 return DTS_SUBSTREAM_TYPE_EXTENSION;
1279 default :
1280 return DTS_SUBSTREAM_TYPE_NONE;
1284 int dts_get_exss_index( dts_info_t *info, uint8_t *exss_index )
1286 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 6 )
1287 return -1;
1288 *exss_index = lsmash_bs_show_byte( info->bits->bs, 5 ) >> 6;
1289 return 0;
1292 int dts_get_max_channel_count( dts_info_t *info )
1294 int max_channel_count = 0;
1295 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1297 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1298 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1300 /* Get the channel layout of an audio presentation from a core component. */
1301 uint16_t channel_layout = 0;
1302 int channel_count = 0;
1303 if( exss->bBcCorePresent [nAuPr]
1304 && exss->nuBcCoreAssetIndex[nAuPr] < exss->nuNumAssets )
1306 dts_core_info_t *core = &info->exss[ exss->nuBcCoreExtSSIndex[nAuPr] ].asset[ exss->nuBcCoreAssetIndex[nAuPr] ].core;
1307 if( core->xxch.channel_layout | core->xxch.lower_planes )
1309 channel_layout = core->xxch.channel_layout;
1310 channel_count = lsmash_count_bits( core->xxch.lower_planes ); /* FIXME: Should we count these channels? */
1312 else
1313 channel_layout = core->channel_layout;
1315 channel_count += dts_get_channel_count_from_channel_layout( channel_layout );
1316 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1317 /* Get the channel layouts of an audio presentation from extension substreams. */
1318 uint16_t ext_channel_layout = 0;
1319 uint16_t lbr_channel_layout = 0;
1320 uint16_t xll_channel_layout = 0;
1321 uint8_t xll_lower_channels = 0;
1322 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1323 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1324 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1325 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1327 dts_audio_asset_t *asset = &exss->asset[nAst];
1328 ext_channel_layout |= asset->channel_layout;
1329 lbr_channel_layout |= asset->lbr.channel_layout;
1330 xll_channel_layout |= asset->xll.channel_layout;
1331 xll_lower_channels |= asset->xll.lower_planes;
1333 /* Audio asset descriptors */
1334 channel_count = dts_get_channel_count_from_channel_layout( ext_channel_layout );
1335 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1336 /* LBR components */
1337 channel_count = dts_get_channel_count_from_channel_layout( lbr_channel_layout );
1338 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1339 /* Lossless extensions */
1340 channel_count = dts_get_channel_count_from_channel_layout( xll_channel_layout )
1341 + lsmash_count_bits( xll_lower_channels );
1342 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1345 return max_channel_count;
1348 void dts_update_specific_param( dts_info_t *info )
1350 lsmash_dts_specific_parameters_t *param = &info->ddts_param;
1351 int exss_index_start = 0;
1352 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1354 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1355 if( exss->nuNumAudioPresnt && exss->nuNumAssets )
1357 exss_index_start = nExtSSIndex;
1358 break;
1361 /* DTSSamplingFrequency and FrameDuration */
1362 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1364 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1365 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1366 continue;
1367 if( param->DTSSamplingFrequency <= exss->sampling_frequency )
1369 param->DTSSamplingFrequency = exss->sampling_frequency;
1370 info->frame_duration = exss->frame_duration;
1372 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1374 dts_audio_asset_t *asset = &exss->asset[nAst];
1375 if( param->DTSSamplingFrequency <= asset->core.sampling_frequency )
1377 param->DTSSamplingFrequency = asset->core.sampling_frequency;
1378 info->frame_duration = asset->core.frame_duration;
1380 if( param->DTSSamplingFrequency <= asset->lbr.sampling_frequency )
1382 param->DTSSamplingFrequency = asset->lbr.sampling_frequency;
1383 info->frame_duration = asset->lbr.frame_duration;
1385 if( param->DTSSamplingFrequency <= asset->xll.sampling_frequency )
1387 param->DTSSamplingFrequency = asset->xll.sampling_frequency;
1388 info->frame_duration = asset->xll.frame_duration;
1392 param->FrameDuration = 0;
1393 for( uint32_t frame_duration = info->frame_duration >> 10; frame_duration; frame_duration >>= 1 )
1394 ++ param->FrameDuration;
1395 /* pcmSampleDepth */
1396 param->pcmSampleDepth = 0;
1397 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1399 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1400 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1401 continue;
1402 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, exss->bit_resolution );
1403 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1405 dts_audio_asset_t *asset = &exss->asset[nAst];
1406 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->core.pcm_resolution );
1407 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->lbr.sample_size );
1408 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->xll.pcm_resolution );
1411 param->pcmSampleDepth = param->pcmSampleDepth > 16 ? 24 : 16;
1412 /* StreamConstruction */
1413 param->StreamConstruction = lsmash_dts_get_stream_construction( info->flags );
1414 /* CoreLFEPresent */
1415 param->CoreLFEPresent = !!(info->core.channel_layout & DTS_CHANNEL_LAYOUT_LFE1);
1416 /* CoreLayout */
1417 if( param->StreamConstruction == 0 /* Unknown */
1418 || param->StreamConstruction >= 17 /* No core substream */ )
1419 /* Use ChannelLayout. */
1420 param->CoreLayout = 31;
1421 else
1423 if( info->core.channel_arrangement != 1
1424 && info->core.channel_arrangement != 3
1425 && info->core.channel_arrangement <= 9 )
1426 param->CoreLayout = info->core.channel_arrangement;
1427 else
1428 /* Use ChannelLayout. */
1429 param->CoreLayout = 31;
1431 /* CoreSize
1432 * The specification says this field is the size of a core substream AU in bytes.
1433 * If we don't assume CoreSize is the copy of FSIZE, when FSIZE equals 0x3FFF, this field overflows and becomes 0. */
1434 param->CoreSize = info->core.frame_size ? LSMASH_MIN( info->core.frame_size - 1, 0x3FFF ) : 0;
1435 /* StereoDownmix */
1436 param->StereoDownmix = 0;
1437 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1439 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1440 param->StereoDownmix |= exss->stereo_downmix;
1441 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1443 param->StereoDownmix |= exss->asset[nAst].lbr.stereo_downmix;
1444 param->StereoDownmix |= exss->asset[nAst].xll.stereo_downmix;
1447 /* RepresentationType
1448 * Available only when core substream is absent and ChannelLayout is set to 0. */
1449 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1451 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1452 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1453 continue;
1454 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1456 int asset_count = 0;
1457 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1458 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1459 asset_count += lsmash_count_bits( exss->nuActiveAssetMask[nAuPr][nSS] );
1460 if( asset_count > 1 )
1462 /* An audio presentation has mulple audio assets.
1463 * Audio asset designated for mixing with another audio asset. */
1464 param->RepresentationType = 0;
1465 nExtSSIndex = DTS_MAX_NUM_EXSS;
1466 break;
1468 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1469 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1470 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1471 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1473 dts_audio_asset_t *asset = &exss->asset[nAst];
1474 if( asset->nuRepresentationType == info->exss[exss_index_start].asset[0].nuRepresentationType )
1475 param->RepresentationType = asset->nuRepresentationType;
1476 else
1478 /* Detected different representation types. Use ChannelLayout. */
1479 param->RepresentationType = 0;
1480 nAuPr = exss->nuNumAudioPresnt;
1481 nExtSSIndex = DTS_MAX_NUM_EXSS;
1482 break;
1487 /* ChannelLayout
1488 * complete information on channels coded in the audio stream including core and extensions */
1489 param->ChannelLayout = 0;
1490 if( param->RepresentationType == 0 )
1491 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1493 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1494 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1495 continue;
1496 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1498 dts_audio_asset_t *asset = &exss->asset[nAst];
1499 param->ChannelLayout |= asset->channel_layout;
1500 param->ChannelLayout |= asset->core.channel_layout;
1501 param->ChannelLayout |= asset->core.xxch.channel_layout;
1502 param->ChannelLayout |= asset->lbr.channel_layout;
1503 param->ChannelLayout |= asset->xll.channel_layout;
1506 /* MultiAssetFlag
1507 * When multiple assets exist, the remaining parameters in the DTSSpecificBox only reflect the coding parameters of the first asset. */
1508 param->MultiAssetFlag = ((info->exss[0].nuNumAssets
1509 + info->exss[1].nuNumAssets
1510 + info->exss[2].nuNumAssets
1511 + info->exss[3].nuNumAssets) > 1);
1512 /* LBRDurationMod */
1513 param->LBRDurationMod = info->exss[exss_index_start].asset[0].lbr.duration_modifier;
1514 info->ddts_param_initialized = 1;
1517 int dts_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1519 assert( dst && dst->data.structured && src && src->data.unstructured );
1520 if( src->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1521 return -1;
1522 lsmash_dts_specific_parameters_t *param = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1523 uint8_t *data = src->data.unstructured;
1524 uint64_t size = LSMASH_GET_BE32( data );
1525 int dts_specific_box_min_length = DTS_SPECIFIC_BOX_MIN_LENGTH;
1526 data += ISOM_BASEBOX_COMMON_SIZE;
1527 if( size == 1 )
1529 size = LSMASH_GET_BE64( data );
1530 dts_specific_box_min_length += 8;
1531 data += 8;
1533 if( size != src->size )
1534 return -1;
1535 param->DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1536 param->maxBitrate = LSMASH_GET_BE32( &data[4] );
1537 param->avgBitrate = LSMASH_GET_BE32( &data[8] );
1538 param->pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1539 param->FrameDuration = (data[13] >> 6) & 0x03;
1540 param->StreamConstruction = (data[13] >> 1) & 0x1F;
1541 param->CoreLFEPresent = data[13] & 0x01;
1542 param->CoreLayout = (data[14] >> 2) & 0x3F;
1543 param->CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1544 param->StereoDownmix = (data[16] >> 3) & 0x01;
1545 param->RepresentationType = data[16] & 0x07;
1546 param->ChannelLayout = (data[17] << 8) | data[18];
1547 param->MultiAssetFlag = (data[19] >> 7) & 0x01;
1548 param->LBRDurationMod = (data[19] >> 6) & 0x01;
1549 int reserved_box_present = ((data[19] >> 5) & 0x01) && (size > DTS_SPECIFIC_BOX_MIN_LENGTH);
1550 if( reserved_box_present )
1551 lsmash_append_dts_reserved_box( param, data + 20, size - DTS_SPECIFIC_BOX_MIN_LENGTH );
1552 return 0;
1555 int dts_copy_codec_specific( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1557 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
1558 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
1559 lsmash_dts_specific_parameters_t *src_data = (lsmash_dts_specific_parameters_t *)src->data.structured;
1560 lsmash_dts_specific_parameters_t *dst_data = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1561 lsmash_remove_dts_reserved_box( dst_data );
1562 *dst_data = *src_data;
1563 if( !src_data->box && src_data->box->data && src_data->box->size )
1564 return 0;
1565 return lsmash_append_dts_reserved_box( dst_data, src_data->box->data, src_data->box->size );
1568 int dts_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
1570 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
1571 int indent = level;
1572 lsmash_ifprintf( fp, indent++, "[%s: DTS Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
1573 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
1574 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
1575 if( box->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1576 return -1;
1577 uint8_t *data = box->binary;
1578 isom_skip_box_common( &data );
1579 uint32_t DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1580 uint32_t maxBitrate = LSMASH_GET_BE32( &data[4] );
1581 uint32_t avgBitrate = LSMASH_GET_BE32( &data[8] );
1582 uint8_t pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1583 uint8_t FrameDuration = (data[13] >> 6) & 0x03;
1584 uint8_t StreamConstruction = (data[13] >> 1) & 0x1F;
1585 uint8_t CoreLFEPresent = data[13] & 0x01;
1586 uint8_t CoreLayout = (data[14] >> 2) & 0x3F;
1587 uint16_t CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1588 uint8_t StereoDownmix = (data[16] >> 3) & 0x01;
1589 uint8_t RepresentationType = data[16] & 0x07;
1590 uint16_t ChannelLayout = (data[17] << 8) | data[18];
1591 uint8_t MultiAssetFlag = (data[19] >> 7) & 0x01;
1592 uint8_t LBRDurationMod = (data[19] >> 6) & 0x01;
1593 uint8_t ReservedBoxPresent = (data[19] >> 5) & 0x01;
1594 uint8_t Reserved = data[19] & 0x1F;
1595 uint32_t frame_duration = 512 << FrameDuration;
1596 int construction_flags = StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? construction_info[StreamConstruction] : 0;
1597 static const char *core_layout_description[64] =
1599 "Mono (1/0)",
1600 "Undefined",
1601 "Stereo (2/0)",
1602 "Undefined",
1603 "LT,RT (2/0)",
1604 "L, C, R (3/0)",
1605 "L, R, S (2/1)",
1606 "L, C, R, S (3/1)",
1607 "L, R, LS, RS (2/2)",
1608 "L, C, R, LS, RS (3/2)",
1609 [31] = "use ChannelLayout"
1611 static const char *representation_type_description[8] =
1613 "Audio asset designated for mixing with another audio asset",
1614 "Reserved",
1615 "Lt/Rt Encoded for matrix surround decoding",
1616 "Audio processed for headphone playback",
1617 "Reserved",
1618 "Reserved",
1619 "Reserved",
1620 "Reserved"
1622 static const char *channel_layout_description[16] =
1624 "Center in front of listener",
1625 "Left/Right in front",
1626 "Left/Right surround on side in rear",
1627 "Low frequency effects subwoofer",
1628 "Center surround in rear",
1629 "Left/Right height in front",
1630 "Left/Right surround in rear",
1631 "Center Height in front",
1632 "Over the listener's head",
1633 "Between left/right and center in front",
1634 "Left/Right on side in front",
1635 "Left/Right surround on side",
1636 "Second low frequency effects subwoofer",
1637 "Left/Right height on side",
1638 "Center height in rear",
1639 "Left/Right height in rear"
1641 lsmash_ifprintf( fp, indent, "DTSSamplingFrequency = %"PRIu32" Hz\n", DTSSamplingFrequency );
1642 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32" bit/s\n", maxBitrate );
1643 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32" bit/s\n", avgBitrate );
1644 lsmash_ifprintf( fp, indent, "pcmSampleDepth = %"PRIu8" bits\n", pcmSampleDepth );
1645 lsmash_ifprintf( fp, indent, "FrameDuration = %"PRIu8" (%"PRIu32" samples)\n", FrameDuration, frame_duration );
1646 lsmash_ifprintf( fp, indent, "StreamConstruction = 0x%02"PRIx8"\n", StreamConstruction );
1647 if( construction_flags & (DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG) )
1649 lsmash_ifprintf( fp, indent + 1, "Core substream\n" );
1650 if( construction_flags & DTS_CORE_SUBSTREAM_CORE_FLAG )
1651 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1652 if( construction_flags & DTS_CORE_SUBSTREAM_XCH_FLAG )
1653 lsmash_ifprintf( fp, indent + 2, "XCH\n" );
1654 if( construction_flags & DTS_CORE_SUBSTREAM_X96_FLAG )
1655 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1656 if( construction_flags & DTS_CORE_SUBSTREAM_XXCH_FLAG )
1657 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1659 if( construction_flags & (DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG
1660 | DTS_EXT_SUBSTREAM_XBR_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG | DTS_EXT_SUBSTREAM_LBR_FLAG) )
1662 lsmash_ifprintf( fp, indent + 1, "Extension substream\n" );
1663 if( construction_flags & DTS_EXT_SUBSTREAM_CORE_FLAG )
1664 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1665 if( construction_flags & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1666 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1667 if( construction_flags & DTS_EXT_SUBSTREAM_X96_FLAG )
1668 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1669 if( construction_flags & DTS_EXT_SUBSTREAM_XBR_FLAG )
1670 lsmash_ifprintf( fp, indent + 2, "XBR\n" );
1671 if( construction_flags & DTS_EXT_SUBSTREAM_XLL_FLAG )
1672 lsmash_ifprintf( fp, indent + 2, "XLL\n" );
1673 if( construction_flags & DTS_EXT_SUBSTREAM_LBR_FLAG )
1674 lsmash_ifprintf( fp, indent + 2, "LBR\n" );
1676 lsmash_ifprintf( fp, indent, "CoreLFEPresent = %s\n", CoreLFEPresent ? "1 (LFE exists)" : "0 (no LFE)" );
1677 if( core_layout_description[CoreLayout] )
1678 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (%s)\n", CoreLayout, core_layout_description[CoreLayout] );
1679 else
1680 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (Undefined)\n", CoreLayout );
1681 if( CoreSize )
1682 lsmash_ifprintf( fp, indent, "CoreSize = %"PRIu16"\n", CoreSize );
1683 else
1684 lsmash_ifprintf( fp, indent, "CoreSize = 0 (no core substream exists)\n" );
1685 lsmash_ifprintf( fp, indent, "StereoDownmix = %s\n", StereoDownmix ? "1 (embedded downmix present)" : "0 (no embedded downmix)" );
1686 lsmash_ifprintf( fp, indent, "RepresentationType = %"PRIu8" (%s)\n", RepresentationType, representation_type_description[RepresentationType] );
1687 lsmash_ifprintf( fp, indent, "ChannelLayout = 0x%04"PRIx16"\n", ChannelLayout );
1688 if( ChannelLayout )
1689 for( int i = 0; i < 16; i++ )
1690 if( (ChannelLayout >> i) & 0x01 )
1691 lsmash_ifprintf( fp, indent + 1, "%s\n", channel_layout_description[i] );
1692 lsmash_ifprintf( fp, indent, "MultiAssetFlag = %s\n", MultiAssetFlag ? "1 (multiple asset)" : "0 (single asset)" );
1693 if( LBRDurationMod )
1694 lsmash_ifprintf( fp, indent, "LBRDurationMod = 1 (%"PRIu32" -> %"PRIu32" samples)\n", frame_duration, (frame_duration * 3) / 2 );
1695 else
1696 lsmash_ifprintf( fp, indent, "LBRDurationMod = 0 (no LBR duration modifier)\n" );
1697 lsmash_ifprintf( fp, indent, "ReservedBoxPresent = %s\n", ReservedBoxPresent ? "1 (ReservedBox present)" : "0 (no ReservedBox)" );
1698 lsmash_ifprintf( fp, indent, "Reserved = 0x%02"PRIx8"\n", Reserved );
1699 return 0;