dts: Evaluate return value properly.
[L-SMASH.git] / codecs / dts.c
blobd124d56b9aaf0cf0b15b40634cf4d1ceacc6016b
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 lsmash_free( param->box->data );
189 lsmash_freep( &param->box );
192 void dts_destruct_specific_data( void *data )
194 if( !data )
195 return;
196 lsmash_remove_dts_reserved_box( data );
197 lsmash_free( data );
200 uint8_t lsmash_dts_get_stream_construction( lsmash_dts_construction_flag flags )
202 uint8_t StreamConstruction;
203 for( StreamConstruction = 1; StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION; StreamConstruction++ )
204 if( flags == construction_info[StreamConstruction] )
205 break;
206 /* For any stream type not listed in the above table,
207 * StreamConstruction shall be set to 0 and the codingname shall default to 'dtsh'. */
208 return StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? StreamConstruction : 0;
211 lsmash_dts_construction_flag lsmash_dts_get_construction_flags( uint8_t stream_construction )
213 if( stream_construction <= DTS_MAX_STREAM_CONSTRUCTION )
214 return construction_info[stream_construction];
215 return 0;
218 lsmash_codec_type_t lsmash_dts_get_codingname( lsmash_dts_specific_parameters_t *param )
220 assert( param->StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION );
221 if( param->MultiAssetFlag )
222 return ISOM_CODEC_TYPE_DTSH_AUDIO; /* Multiple asset streams shall use the 'dtsh' coding_name. */
223 static lsmash_codec_type_t codingname_table[DTS_MAX_STREAM_CONSTRUCTION + 1] = { LSMASH_CODEC_TYPE_INITIALIZER };
224 if( lsmash_check_codec_type_identical( codingname_table[0], LSMASH_CODEC_TYPE_UNSPECIFIED ) )
226 int i = 0;
227 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO; /* Undefined stream types shall be set to 0 and the codingname shall default to 'dtsh'. */
228 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
229 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
230 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
231 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
232 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_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_DTSL_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_DTSE_AUDIO;
246 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
247 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
248 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
250 return codingname_table[ param->StreamConstruction ];
253 uint8_t *lsmash_create_dts_specific_info( lsmash_dts_specific_parameters_t *param, uint32_t *data_length )
255 lsmash_bits_t bits = { 0 };
256 lsmash_bs_t bs = { 0 };
257 lsmash_bits_init( &bits, &bs );
258 int reserved_box_present = (param->box && param->box->data && param->box->size);
259 uint32_t buffer_length = DTS_SPECIFIC_BOX_MIN_LENGTH + (reserved_box_present ? param->box->size : 0);
260 uint8_t buffer[buffer_length];
261 memset( buffer, 0, buffer_length );
262 bs.buffer.data = buffer;
263 bs.buffer.alloc = buffer_length;
264 /* Create a DTSSpecificBox. */
265 lsmash_bits_put( &bits, 32, 0 ); /* box size */
266 lsmash_bits_put( &bits, 32, ISOM_BOX_TYPE_DDTS.fourcc ); /* box type: 'ddts' */
267 lsmash_bits_put( &bits, 32, param->DTSSamplingFrequency );
268 lsmash_bits_put( &bits, 32, param->maxBitrate ); /* maxBitrate; setup by isom_update_bitrate_description */
269 lsmash_bits_put( &bits, 32, param->avgBitrate ); /* avgBitrate; setup by isom_update_bitrate_description */
270 lsmash_bits_put( &bits, 8, param->pcmSampleDepth );
271 lsmash_bits_put( &bits, 2, param->FrameDuration );
272 lsmash_bits_put( &bits, 5, param->StreamConstruction );
273 lsmash_bits_put( &bits, 1, param->CoreLFEPresent );
274 lsmash_bits_put( &bits, 6, param->CoreLayout );
275 lsmash_bits_put( &bits, 14, param->CoreSize );
276 lsmash_bits_put( &bits, 1, param->StereoDownmix );
277 lsmash_bits_put( &bits, 3, param->RepresentationType );
278 lsmash_bits_put( &bits, 16, param->ChannelLayout );
279 lsmash_bits_put( &bits, 1, param->MultiAssetFlag );
280 lsmash_bits_put( &bits, 1, param->LBRDurationMod );
281 lsmash_bits_put( &bits, 1, reserved_box_present );
282 lsmash_bits_put( &bits, 5, 0 ); /* Reserved */
283 /* ReservedBox */
284 if( reserved_box_present )
285 for( uint32_t i = 0; i < param->box->size; i++ )
286 lsmash_bits_put( &bits, 8, param->box->data[i] );
287 /* */
288 uint8_t *data = lsmash_bits_export_data( &bits, data_length );
289 /* Update box size. */
290 LSMASH_SET_BE32( data, *data_length );
291 return data;
294 int lsmash_setup_dts_specific_parameters_from_frame( lsmash_dts_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
296 lsmash_bits_t bits = { 0 };
297 lsmash_bs_t bs = { 0 };
298 uint8_t buffer[DTS_MAX_EXSS_SIZE] = { 0 };
299 bs.buffer.data = buffer;
300 bs.buffer.store = data_length;
301 bs.buffer.alloc = DTS_MAX_EXSS_SIZE;
302 dts_info_t *info = &(dts_info_t){ .bits = &bits };
303 info->bits = &bits;
304 lsmash_bits_init( &bits, &bs );
305 memcpy( buffer, data, LSMASH_MIN( data_length, DTS_MAX_EXSS_SIZE ) );
306 dts_setup_parser( info );
307 uint64_t next_frame_pos = 0;
308 while( 1 )
310 /* Seek to the head of the next syncframe. */
311 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
312 /* Check the remainder length of the buffer.
313 * If there is enough length, then continue to parse the frame in it.
314 * The length 10 is the required byte length to get frame size. */
315 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
316 if( bs.eob || (bs.eof && remain_size < 10) )
317 goto setup_param; /* No more valid data. */
318 /* Parse substream frame. */
319 dts_substream_type prev_substream_type = info->substream_type;
320 info->substream_type = dts_get_substream_type( info );
321 int (*dts_parse_frame)( dts_info_t * ) = NULL;
322 switch( info->substream_type )
324 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
325 case DTS_SUBSTREAM_TYPE_CORE :
326 if( prev_substream_type != DTS_SUBSTREAM_TYPE_NONE )
327 goto setup_param;
328 dts_parse_frame = dts_parse_core_substream;
329 break;
330 case DTS_SUBSTREAM_TYPE_EXTENSION :
332 uint8_t prev_exss_index = info->exss_index;
333 if( dts_get_exss_index( info, &info->exss_index ) < 0 )
334 return -1;
335 if( prev_substream_type == DTS_SUBSTREAM_TYPE_EXTENSION && info->exss_index <= prev_exss_index )
336 goto setup_param;
337 dts_parse_frame = dts_parse_extension_substream;
338 break;
340 default :
341 return -1;
343 info->frame_size = 0;
344 if( dts_parse_frame( info ) < 0 )
345 return -1; /* Failed to parse. */
346 next_frame_pos += info->frame_size;
348 setup_param:
349 dts_update_specific_param( info );
350 *param = info->ddts_param;
351 return 0;
354 static uint64_t dts_bits_get( lsmash_bits_t *bits, uint32_t width, uint64_t *bits_pos )
356 *bits_pos += width;
357 return lsmash_bits_get( bits, width );
360 static int dts_get_channel_count_from_channel_layout( uint16_t channel_layout )
362 #define DTS_CHANNEL_PAIR_MASK \
363 (DTS_CHANNEL_LAYOUT_L_R \
364 | DTS_CHANNEL_LAYOUT_LS_RS \
365 | DTS_CHANNEL_LAYOUT_LH_RH \
366 | DTS_CHANNEL_LAYOUT_LSR_RSR \
367 | DTS_CHANNEL_LAYOUT_LC_RC \
368 | DTS_CHANNEL_LAYOUT_LW_RW \
369 | DTS_CHANNEL_LAYOUT_LSS_RSS \
370 | DTS_CHANNEL_LAYOUT_LHS_RHS \
371 | DTS_CHANNEL_LAYOUT_LHR_RHR)
372 return lsmash_count_bits( channel_layout )
373 + lsmash_count_bits( channel_layout & DTS_CHANNEL_PAIR_MASK );
374 #undef DTS_CHANNEL_PAIR_MASK
377 static uint32_t dts_get_channel_layout_from_ls_mask32( uint32_t mask )
379 uint32_t layout = 0;
380 if( mask & DTS_LOUDSPEAKER_MASK32_C )
381 layout |= DTS_CHANNEL_LAYOUT_C;
382 if( mask & (DTS_LOUDSPEAKER_MASK32_L | DTS_LOUDSPEAKER_MASK32_R) )
383 layout |= DTS_CHANNEL_LAYOUT_L_R;
384 if( mask & (DTS_LOUDSPEAKER_MASK32_LS | DTS_LOUDSPEAKER_MASK32_RS) )
385 layout |= DTS_CHANNEL_LAYOUT_LS_RS;
386 if( mask & DTS_LOUDSPEAKER_MASK32_LFE1 )
387 layout |= DTS_CHANNEL_LAYOUT_LFE1;
388 if( mask & DTS_LOUDSPEAKER_MASK32_CS )
389 layout |= DTS_CHANNEL_LAYOUT_CS;
390 if( mask & (DTS_LOUDSPEAKER_MASK32_LH | DTS_LOUDSPEAKER_MASK32_RH) )
391 layout |= DTS_CHANNEL_LAYOUT_LH_RH;
392 if( mask & (DTS_LOUDSPEAKER_MASK32_LSR | DTS_LOUDSPEAKER_MASK32_RSR) )
393 layout |= DTS_CHANNEL_LAYOUT_LSR_RSR;
394 if( mask & DTS_LOUDSPEAKER_MASK32_CH )
395 layout |= DTS_CHANNEL_LAYOUT_CH;
396 if( mask & DTS_LOUDSPEAKER_MASK32_OH )
397 layout |= DTS_CHANNEL_LAYOUT_OH;
398 if( mask & (DTS_LOUDSPEAKER_MASK32_LC | DTS_LOUDSPEAKER_MASK32_RC) )
399 layout |= DTS_CHANNEL_LAYOUT_LC_RC;
400 if( mask & (DTS_LOUDSPEAKER_MASK32_LW | DTS_LOUDSPEAKER_MASK32_RW) )
401 layout |= DTS_CHANNEL_LAYOUT_LW_RW;
402 if( mask & (DTS_LOUDSPEAKER_MASK32_LSS | DTS_LOUDSPEAKER_MASK32_RSS) )
403 layout |= DTS_CHANNEL_LAYOUT_LSS_RSS;
404 if( mask & DTS_LOUDSPEAKER_MASK32_LFE2 )
405 layout |= DTS_CHANNEL_LAYOUT_LFE2;
406 if( mask & (DTS_LOUDSPEAKER_MASK32_LHS | DTS_LOUDSPEAKER_MASK32_RHS) )
407 layout |= DTS_CHANNEL_LAYOUT_LHS_RHS;
408 if( mask & DTS_LOUDSPEAKER_MASK32_CHR )
409 layout |= DTS_CHANNEL_LAYOUT_CHR;
410 if( mask & (DTS_LOUDSPEAKER_MASK32_LHR | DTS_LOUDSPEAKER_MASK32_RHR) )
411 layout |= DTS_CHANNEL_LAYOUT_LHR_RHR;
412 return layout;
415 /* for channels which cannot be expressed by ChannelLayout; CL, LL and RL */
416 static inline uint8_t dts_get_lower_channels_from_ls_mask32( uint32_t mask )
418 return (mask >> 25) & 0x7;
421 static void dts_parse_xll_navigation( lsmash_bits_t *bits, dts_xll_info_t *xll, int nuBits4ExSSFsize, uint64_t *bits_pos )
423 xll->size = dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ) + 1; /* nuExSSXLLFsize (nuBits4ExSSFsize) */
424 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSXLLSyncPresent (1) */
426 dts_bits_get( bits, 4, bits_pos ); /* nuPeakBRCntrlBuffSzkB (4) */
427 int nuBitsInitDecDly = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitsInitDecDly (5) */
428 dts_bits_get( bits, nuBitsInitDecDly, bits_pos ); /* nuInitLLDecDlyFrames (nuBitsInitDecDly) */
429 dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ); /* nuExSSXLLSyncOffset (nuBits4ExSSFsize) */
433 static void dts_parse_lbr_navigation( lsmash_bits_t *bits, dts_lbr_info_t *lbr, uint64_t *bits_pos )
435 lbr->size = dts_bits_get( bits, 14, bits_pos ); /* nuExSSLBRFsize (14) */
436 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSLBRSyncPresent (1) */
437 dts_bits_get( bits, 2, bits_pos ); /* nuExSSLBRSyncDistInFrames (2) */
440 static int dts_parse_asset_descriptor( dts_info_t *info, uint64_t *bits_pos )
442 lsmash_bits_t *bits = info->bits;
443 dts_extension_info_t *exss = &info->exss[ info->exss_index ];
444 /* Audio asset descriptor */
445 uint64_t asset_descriptor_pos = *bits_pos;
446 int nuAssetDescriptFsize = dts_bits_get( bits, 9, bits_pos ) + 1; /* nuAssetDescriptFsize (9) */
447 dts_audio_asset_t *asset = &exss->asset[ dts_bits_get( bits, 3, bits_pos ) ]; /* nuAssetIndex (3) */
448 /* Static metadata */
449 int bEmbeddedStereoFlag = 0;
450 int bEmbeddedSixChFlag = 0;
451 int nuTotalNumChs = 0;
452 if( exss->bStaticFieldsPresent )
454 if( dts_bits_get( bits, 1, bits_pos ) ) /* bAssetTypeDescrPresent (1)*/
455 dts_bits_get( bits, 4, bits_pos ); /* nuAssetTypeDescriptor (4) */
456 if( dts_bits_get( bits, 1, bits_pos ) ) /* bLanguageDescrPresent (1) */
457 dts_bits_get( bits, 24, bits_pos ); /* LanguageDescriptor (24) */
458 if( dts_bits_get( bits, 1, bits_pos ) )
460 int nuInfoTextByteSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nuInfoTextByteSize (10) */
461 dts_bits_get( bits, nuInfoTextByteSize * 8, bits_pos ); /* InfoTextString (nuInfoTextByteSize) */
463 int nuBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitResolution (5) */
464 exss->bit_resolution = LSMASH_MAX( exss->bit_resolution, nuBitResolution );
465 int nuMaxSampleRate = dts_bits_get( bits, 4, bits_pos ); /* nuMaxSampleRate (4) */
466 static const uint32_t source_sample_rate_table[16] =
468 8000, 16000, 32000, 64000, 128000,
469 22050, 44100, 88200, 176400, 352800,
470 12000, 24000, 48000, 96000, 192000, 384000
472 exss->sampling_frequency = LSMASH_MAX( exss->sampling_frequency, source_sample_rate_table[nuMaxSampleRate] );
473 nuTotalNumChs = dts_bits_get( bits, 8, bits_pos ) + 1; /* nuTotalNumChs (8) */
474 asset->bOne2OneMapChannels2Speakers = dts_bits_get( bits, 1, bits_pos ); /* bOne2OneMapChannels2Speakers (1) */
475 if( asset->bOne2OneMapChannels2Speakers )
477 if( nuTotalNumChs > 2 )
479 bEmbeddedStereoFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedStereoFlag (1) */
480 exss->stereo_downmix |= bEmbeddedStereoFlag;
482 if( nuTotalNumChs > 6 )
483 bEmbeddedSixChFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedSixChFlag (1) */
484 int nuNumBits4SAMask;
485 if( dts_bits_get( bits, 1, bits_pos ) ) /* bSpkrMaskEnabled (1) */
487 nuNumBits4SAMask = (dts_bits_get( bits, 2, bits_pos ) + 1) << 2; /* nuNumBits4SAMask (2) */
488 asset->channel_layout |= dts_bits_get( bits, nuNumBits4SAMask, bits_pos ); /* nuSpkrActivityMask (nuNumBits4SAMask) */
490 else
491 /* The specification doesn't mention the value of nuNumBits4SAMask if bSpkrMaskEnabled is set to 0. */
492 nuNumBits4SAMask = 16;
493 int nuNumSpkrRemapSets = dts_bits_get( bits, 3, bits_pos );
494 int nuStndrSpkrLayoutMask[8] = { 0 };
495 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
496 nuStndrSpkrLayoutMask[ns] = dts_bits_get( bits, nuNumBits4SAMask, bits_pos );
497 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
499 int nuNumSpeakers = dts_get_channel_count_from_channel_layout( nuStndrSpkrLayoutMask[ns] );
500 int nuNumDecCh4Remap = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuNumDecCh4Remap[ns] (5) */
501 for( int nCh = 0; nCh < nuNumSpeakers; nCh++ )
503 uint32_t nuRemapDecChMask = dts_bits_get( bits, nuNumDecCh4Remap, bits_pos );
504 int nCoef = lsmash_count_bits( nuRemapDecChMask );
505 for( int nc = 0; nc < nCoef; nc++ )
506 dts_bits_get( bits, 5, bits_pos ); /* nuSpkrRemapCodes[ns][nCh][nc] (5) */
510 else
512 asset->nuRepresentationType = dts_bits_get( bits, 3, bits_pos ); /* nuRepresentationType (3) */
513 if( asset->nuRepresentationType == 2
514 || asset->nuRepresentationType == 3 )
515 nuTotalNumChs = 2;
518 /* Dynamic metadata */
519 int bDRCCoefPresent = dts_bits_get( bits, 1, bits_pos ); /* bDRCCoefPresent (1) */
520 if( bDRCCoefPresent )
521 dts_bits_get( bits, 8, bits_pos ); /* nuDRCCode (8) */
522 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDialNormPresent (1) */
523 dts_bits_get( bits, 5, bits_pos ); /* nuDialNormCode (5) */
524 if( bDRCCoefPresent && bEmbeddedStereoFlag )
525 dts_bits_get( bits, 8, bits_pos ); /* nuDRC2ChDmixCode (8) */
526 int bMixMetadataPresent;
527 if( exss->bMixMetadataEnbl )
528 bMixMetadataPresent = dts_bits_get( bits, 1, bits_pos ); /* bMixMetadataPresent (1) */
529 else
530 bMixMetadataPresent = 0;
531 if( bMixMetadataPresent )
533 dts_bits_get( bits, 7, bits_pos ); /* bExternalMixFlag (1)
534 * nuPostMixGainAdjCode (7) */
535 if( dts_bits_get( bits, 2, bits_pos ) < 3 ) /* nuControlMixerDRC (2) */
536 dts_bits_get( bits, 3, bits_pos ); /* nuLimit4EmbeddedDRC (3) */
537 else
538 dts_bits_get( bits, 8, bits_pos ); /* nuCustomDRCCode (8) */
539 int bEnblPerChMainAudioScale = dts_bits_get( bits, 1, bits_pos ); /* bEnblPerChMainAudioScale (1) */
540 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
541 if( bEnblPerChMainAudioScale )
542 for( uint8_t nCh = 0; nCh < exss->nNumMixOutCh[ns]; nCh++ )
543 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][nCh] (6) */
544 else
545 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][0] (6) */
546 int nEmDM = 1;
547 int nDecCh[3] = { nuTotalNumChs, 0, 0 };
548 if( bEmbeddedSixChFlag )
550 nDecCh[nEmDM] = 6;
551 ++nEmDM;
553 if( bEmbeddedStereoFlag )
555 nDecCh[nEmDM] = 2;
556 ++nEmDM;
558 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
559 for( int nE = 0; nE < nEmDM; nE++ )
560 for( int nCh = 0; nCh < nDecCh[nE]; nCh++ )
562 int nuMixMapMask = dts_bits_get( bits, exss->nNumMixOutCh[ns], bits_pos ); /* nuMixMapMask (nNumMixOutCh[ns]) */
563 int nuNumMixCoefs = lsmash_count_bits( nuMixMapMask );
564 for( int nC = 0; nC < nuNumMixCoefs; nC++ )
565 dts_bits_get( bits, 6, bits_pos ); /* nuMixCoeffs[ns][nE][nCh][nC] (6) */
568 /* Decoder navigation data */
569 asset->nuCodingMode = dts_bits_get( bits, 2, bits_pos ); /* nuCodingMode (2) */
570 switch( asset->nuCodingMode )
572 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
574 int nuCoreExtensionMask = dts_bits_get( bits, 12, bits_pos ); /* nuCoreExtensionMask (12) */
575 asset->nuCoreExtensionMask = nuCoreExtensionMask;
576 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
578 asset->core.frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSCoreFsize (14) */
579 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSCoreSyncPresent (1) */
580 dts_bits_get( bits, 2, bits_pos ); /* nuExSSCoreSyncDistInFrames (2) */
582 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
583 asset->xbr_size = dts_bits_get( bits, 14, bits_pos ) + 1;
584 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
585 asset->core.xxch.size = dts_bits_get( bits, 14, bits_pos ) + 1;
586 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
587 asset->x96_size = dts_bits_get( bits, 12, bits_pos ) + 1;
588 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
589 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
590 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
591 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
592 break;
594 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
595 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
596 break;
597 case 2 : /* DTS-HD Low bit-rate mode */
598 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
599 break;
600 case 3 : /* Auxiliary coding mode */
601 asset->aux_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSAuxFsize (14) */
602 break;
603 default :
604 assert( 0 );
605 break;
607 dts_bits_get( bits, nuAssetDescriptFsize * 8 - (*bits_pos - asset_descriptor_pos), bits_pos ); /* Skip remaining part of Audio asset descriptor. */
608 return bits->bs->error ? -1 : 0;
611 static int dts_parse_xxch( dts_info_t *info, uint64_t *bits_pos, dts_xxch_info_t *xxch )
613 lsmash_bits_t *bits = info->bits;
614 /* XXCH Frame Header */
615 uint64_t xxch_pos = *bits_pos - 32; /* SYNCXXCh (32) */
616 uint64_t nuHeaderSizeXXCh = dts_bits_get( bits, 6, bits_pos ) + 1; /* nuHeaderSizeXXCh (6) */
617 dts_bits_get( bits, 1, bits_pos ); /* bCRCPresent4ChSetHeaderXXCh (1) */
618 int nuBits4SpkrMaskXXCh = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBits4SpkrMaskXXCh (5) */
619 int nuNumChSetsInXXCh = dts_bits_get( bits, 2, bits_pos ) + 1; /* nuNumChSetsInXXCh (2) */
620 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
621 dts_bits_get( bits, 14, bits_pos ); /* pnuChSetFsizeXXCh[nChSet] - 1 (14) */
622 /* A 5.1 decoder uses this AMODE to configure its decoded outputs to C, L, R, Ls and Rs layout.
623 * On the other hand a 7.1 decoder ignores the AMODE information from the core stream and uses
624 * instead the nuCoreSpkrActivityMask (C, L, R, LFE1, Lss and Rss) and the nuXXChSpkrLayoutMask
625 * (Lsr and Rsr) from the XXCh stream to get the original 7.1 speaker layout (C, L, R, LFE1, Lss,
626 * Rsr, Lsr and Rsr) and configures its outputs accordingly. */
627 uint32_t xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos ); /* nuCoreSpkrActivityMask (nuBits4SpkrMaskXXCh) */
628 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
629 xxch->lower_planes = dts_get_lower_channels_from_ls_mask32( xxch_mask );
630 dts_bits_get( bits, nuHeaderSizeXXCh * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Frame Header. */
631 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
633 /* XXCH Channel Set Header */
634 xxch_pos = *bits_pos;
635 uint64_t nuXXChChSetHeaderSize = dts_bits_get( bits, 7, bits_pos ) + 1; /* nuXXChChSetHeaderSize (7)*/
636 dts_bits_get( bits, 3, bits_pos ); /* nuChInChSetXXCh (3) */
637 if( nuBits4SpkrMaskXXCh > 6 )
639 xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh - 6, bits_pos ) << 6; /* nuXXChSpkrLayoutMask (nuBits4SpkrMaskXXCh - 6) */
640 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
641 xxch->lower_planes |= dts_get_lower_channels_from_ls_mask32( xxch_mask );
643 #if 0 /* FIXME: Can we detect stereo downmixing from only XXCH data within the core substream? */
644 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDownMixCoeffCodeEmbedded (1) */
646 int bDownMixEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownMixEmbedded (1) */
647 dts_bits_get( bits, 6, bits_pos ); /* nDmixScaleFactor (6) */
648 uint32_t DownMixChMapMask[8];
649 for( int nCh = 0; nCh < nuChInChSetXXCh; nCh++ )
650 DownMixChMapMask[nCh] = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos );
652 #endif
653 dts_bits_get( bits, nuXXChChSetHeaderSize * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Channel Set Header. */
655 return 0;
658 static int dts_parse_core_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
660 if( core->extension_audio_descriptor == 0
661 || core->extension_audio_descriptor == 3 )
662 return -1;
663 if( dts_parse_xxch( info, bits_pos, &core->xxch ) < 0 )
664 return -1;
665 info->flags |= DTS_CORE_SUBSTREAM_XXCH_FLAG;
666 return info->bits->bs->error ? -1 : 0;
669 static int dts_parse_exss_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
671 lsmash_bits_t *bits = info->bits;
672 if( DTS_SYNCWORD_XXCH != dts_bits_get( bits, 32, bits_pos ) )
673 return -1;
674 if( dts_parse_xxch( info, bits_pos, &core->xxch ) < 0 )
675 return -1;
676 info->flags |= DTS_EXT_SUBSTREAM_XXCH_FLAG;
677 return bits->bs->error ? -1 : 0;
680 static int dts_parse_core_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
682 if( core->extension_audio_descriptor != 2
683 && core->extension_audio_descriptor != 3 )
684 return 0; /* Probably this is not an X96 extension. We skip this anyway. */
685 lsmash_bits_t *bits = info->bits;
686 /* DTS_BCCORE_X96 Frame Header */
687 /* SYNCX96 (32) */
688 /* To reduce the probability of false synchronization caused by the presence of pseudo sync words, it is
689 * imperative to check the distance between the detected sync word and the end of current frame. This
690 * distance in bytes shall match the value of FSIZE96. */
691 uint64_t FSIZE96 = ((lsmash_bs_show_byte( bits->bs, 0 ) << 4)
692 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 4) & 0x0F)) + 1;
693 if( core->frame_size * 8 != (*bits_pos - 32 + FSIZE96 * 8) )
694 return 0; /* Encountered four emulation bytes (pseudo sync word). */
695 dts_bits_get( bits, 16, bits_pos ); /* FSIZE96 (12)
696 * REVNO (4) */
697 core->sampling_frequency *= 2;
698 core->frame_duration *= 2;
699 info->flags |= DTS_CORE_SUBSTREAM_X96_FLAG;
700 return bits->bs->error ? -1 : 0;
703 static int dts_parse_core_xch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
705 if( core->extension_audio_descriptor != 0
706 && core->extension_audio_descriptor != 3 )
707 return 0; /* Probably this is not an XCh extension. We skip this anyway. */
708 lsmash_bits_t *bits = info->bits;
709 /* XCH Frame Header */
710 /* XChSYNC (32) */
711 /* For compatibility reasons with legacy bitstreams the estimated distance in bytes is checked against
712 * the XChFSIZE+1 as well as the XChFSIZE. The XCh synchronization is pronounced if the distance matches
713 * either of these two values. */
714 uint64_t XChFSIZE = (lsmash_bs_show_byte( bits->bs, 0 ) << 2)
715 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 6) & 0x03);
716 if( core->frame_size * 8 != (*bits_pos - 32 + (XChFSIZE + 1) * 8)
717 && core->frame_size * 8 != (*bits_pos - 32 + XChFSIZE * 8) )
718 return 0; /* Encountered four emulation bytes (pseudo sync word). */
719 if( ((lsmash_bs_show_byte( bits->bs, 1 ) >> 2) & 0xF) != 1 )
720 return 0; /* A known value of AMODE is only 1. Otherwise just skip. */
721 dts_bits_get( bits, 16, bits_pos ); /* XChFSIZE (10)
722 * AMODE (4)
723 * byte align (2) */
724 core->channel_layout |= DTS_CHANNEL_LAYOUT_CS;
725 info->flags |= DTS_CORE_SUBSTREAM_XCH_FLAG;
726 return bits->bs->error ? -1 : 0;
729 static int dts_parse_exss_xbr( dts_info_t *info, uint64_t *bits_pos )
731 lsmash_bits_t *bits = info->bits;
732 /* XBR Frame Header */
733 uint64_t xbr_pos = *bits_pos;
734 if( DTS_SYNCWORD_XBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXBR (32) */
735 return -1;
736 uint64_t nHeaderSizeXBR = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
737 dts_bits_get( bits, nHeaderSizeXBR * 8 - (*bits_pos - xbr_pos), bits_pos ); /* Skip the remaining bits in XBR Frame Header. */
738 info->flags |= DTS_EXT_SUBSTREAM_XBR_FLAG;
739 return bits->bs->error ? -1 : 0;
742 static int dts_parse_exss_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
744 lsmash_bits_t *bits = info->bits;
745 /* DTS_EXSUB_STREAM_X96 Frame Header */
746 uint64_t x96_pos = *bits_pos;
747 if( DTS_SYNCWORD_X96K != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCX96 (32) */
748 return -1;
749 uint64_t nHeaderSizeX96 = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
750 dts_bits_get( bits, nHeaderSizeX96 * 8 - (*bits_pos - x96_pos), bits_pos ); /* Skip the remaining bits in DTS_EXSUB_STREAM_X96 Frame Header. */
751 /* What the fuck! The specification drops 'if' sentence.
752 * We assume the same behaviour for core substream. */
753 core->sampling_frequency *= 2;
754 core->frame_duration *= 2;
755 info->flags |= DTS_EXT_SUBSTREAM_X96_FLAG;
756 return bits->bs->error ? -1 : 0;
759 static int dts_parse_exss_lbr( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
761 lsmash_bits_t *bits = info->bits;
762 dts_lbr_info_t *lbr = &asset->lbr;
763 if( DTS_SYNCWORD_LBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCEXTLBR (32) */
764 return -1;
765 int ucFmtInfoCode = dts_bits_get( bits, 8, bits_pos );
766 if( ucFmtInfoCode == 2 )
768 /* LBR decoder initialization data */
769 int nLBRSampleRateCode = dts_bits_get( bits, 8, bits_pos ); /* nLBRSampleRateCode (8) */
770 int usLBRSpkrMask = dts_bits_get( bits, 16, bits_pos ); /* usLBRSpkrMask (16) */
771 dts_bits_get( bits, 16, bits_pos ); /* nLBRversion (16) */
772 int nLBRCompressedFlags = dts_bits_get( bits, 8, bits_pos ); /* nLBRCompressedFlags (8) */
773 dts_bits_get( bits, 40, bits_pos ); /* nLBRBitRateMSnybbles (8)
774 * nLBROriginalBitRate_LSW (16)
775 * nLBRScaledBitRate_LSW (16) */
776 static const uint32_t source_sample_rate_table[16] =
778 8000, 16000, 32000, 0, 0,
779 11025, 22050, 44100, 0, 0,
780 12000, 24000, 48000, 0, 0, 0
782 enum LBRFlags
784 LBR_FLAG_24_BIT_SAMPLES = 0x01, /* 0b00000001 */
785 LBR_FLAG_USE_LFE = 0x02, /* 0b00000010 */
786 LBR_FLAG_BANDLMT_MASK = 0x1C, /* 0b00011100 */
787 LBR_FLAG_STEREO_DOWNMIX = 0x20, /* 0b00100000 */
788 LBR_FLAG_MULTICHANNEL_DOWNMIX = 0x40, /* 0b01000000 */
790 lbr->sampling_frequency = source_sample_rate_table[nLBRSampleRateCode];
791 lbr->frame_duration = lbr->sampling_frequency < 16000 ? 1024
792 : lbr->sampling_frequency < 32000 ? 2048
793 : 4096;
794 lbr->channel_layout = ((usLBRSpkrMask >> 8) & 0xff) | ((usLBRSpkrMask << 8) & 0xff00); /* usLBRSpkrMask is little-endian. */
795 lbr->stereo_downmix |= !!(nLBRCompressedFlags & LBR_FLAG_STEREO_DOWNMIX);
796 lbr->lfe_present |= !!(nLBRCompressedFlags & LBR_FLAG_USE_LFE);
797 lbr->duration_modifier |= ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x04)
798 || ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x0C);
799 lbr->sample_size = (nLBRCompressedFlags & LBR_FLAG_24_BIT_SAMPLES) ? 24 : 16;
801 else if( ucFmtInfoCode != 1 )
802 return -1; /* unknown */
803 info->flags |= DTS_EXT_SUBSTREAM_LBR_FLAG;
804 return bits->bs->error ? -1 : 0;
807 static int dts_parse_exss_xll( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
809 lsmash_bits_t *bits = info->bits;
810 dts_xll_info_t *xll = &asset->xll;
811 /* Common Header */
812 uint64_t xll_pos = *bits_pos;
813 if( DTS_SYNCWORD_XLL != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXLL (32) */
814 return -1;
815 dts_bits_get( bits, 4, bits_pos ); /* nVersion (4) */
816 uint64_t nHeaderSize = dts_bits_get( bits, 8, bits_pos ) + 1; /* nHeaderSize (8) */
817 int nBits4FrameFsize = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4FrameFsize (5) */
818 dts_bits_get( bits, nBits4FrameFsize, bits_pos ); /* nLLFrameSize (nBits4FrameFsize) */
819 int nNumChSetsInFrame = dts_bits_get( bits, 4, bits_pos ) + 1; /* nNumChSetsInFrame (4) */
820 uint16_t nSegmentsInFrame = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSegmentsInFrame (4) */
821 uint16_t nSmplInSeg = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSmplInSeg (4) */
822 dts_bits_get( bits, 5, bits_pos ); /* nBits4SSize (5) */
823 dts_bits_get( bits, 3, bits_pos ); /* nBandDataCRCEn (2)
824 * bScalableLSBs (1) */
825 int nBits4ChMask = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4ChMask (5) */
826 dts_bits_get( bits, nHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Common Header. */
827 int sum_nChSetLLChannel = 0;
828 uint32_t nFs1 = 0;
829 int nNumFreqBands1 = 0;
830 xll->channel_layout = 0;
831 for( int nChSet = 0; nChSet < nNumChSetsInFrame; nChSet++ )
833 /* Channel Set Sub-Header */
834 xll_pos = *bits_pos;
835 uint64_t nChSetHeaderSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nChSetHeaderSize (10) */
836 int nChSetLLChannel = dts_bits_get( bits, 4, bits_pos ) + 1; /* nChSetLLChannel (4) */
837 dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* nResidualChEncode (nChSetLLChannel) */
838 uint8_t nBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBitResolution (5) */
839 dts_bits_get( bits, 5, bits_pos ); /* nBitWidth (5) */
840 xll->pcm_resolution = LSMASH_MAX( xll->pcm_resolution, nBitResolution );
841 static const uint32_t source_sample_rate_table[16] =
843 8000, 16000, 32000, 64000, 128000,
844 22050, 44100, 88200, 176400, 352800,
845 12000, 24000, 48000, 96000, 192000, 384000
847 int sFreqIndex = dts_bits_get( bits, 4, bits_pos ); /* sFreqIndex (4) */
848 uint32_t nFs = source_sample_rate_table[sFreqIndex];
849 dts_bits_get( bits, 2, bits_pos ); /* nFsInterpolate (2) */
850 int nReplacementSet = dts_bits_get( bits, 2, bits_pos ); /* nReplacementSet (2) */
851 if( nReplacementSet > 0 )
852 dts_bits_get( bits, 1, bits_pos ); /* bActiveReplaceSet (1) */
853 if( asset->bOne2OneMapChannels2Speakers )
855 /* Downmix is allowed only when the encoded channel represents a signal feed to a corresponding loudspeaker. */
856 int bPrimaryChSet = dts_bits_get( bits, 1, bits_pos ); /* bPrimaryChSet (1) */
857 int bDownmixCoeffCodeEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownmixCoeffCodeEmbedded (1) */
858 int nLLDownmixType = 0x7; /* 0b111: Unused */
859 if( bDownmixCoeffCodeEmbedded )
861 dts_bits_get( bits, 1, bits_pos ); /* bDownmixEmbedded (1) */
862 if( bPrimaryChSet )
863 nLLDownmixType = dts_bits_get( bits, 3, bits_pos ); /* nLLDownmixType (3) */
865 int bHierChSet = dts_bits_get( bits, 1, bits_pos ); /* bHierChSet (1) */
866 if( bDownmixCoeffCodeEmbedded )
868 /* N: the number of channels in the current channel set
869 * for non-primary channel set, adding +1 for the down scaling coefficients that prevent overflow
870 * M: the number of channels that the current channel set is mixed into
871 * Downmix coefficients are transmitted using 9-bit codes. */
872 static const int downmix_channel_count_table[8] = { 1, 2, 2, 3, 3, 4, 4, 0 };
873 int N = nChSetLLChannel + (bPrimaryChSet ? 0 : 1);
874 int M = bPrimaryChSet ? downmix_channel_count_table[nLLDownmixType] : sum_nChSetLLChannel;
875 int nDownmixCoeffs = N * M;
876 dts_bits_get( bits, nDownmixCoeffs * 9, bits_pos ); /* DownmixCoeffs (nDownmixCoeffs * 9) */
877 if( bPrimaryChSet && downmix_channel_count_table[nLLDownmixType] == 2 )
878 xll->stereo_downmix |= 1;
880 if( bHierChSet )
881 sum_nChSetLLChannel += nChSetLLChannel;
882 if( dts_bits_get( bits, 1, bits_pos ) ) /* bChMaskEnabled (1) */
884 uint32_t nChMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nChMask (nBits4ChMask) */
885 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nChMask );
886 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nChMask );
888 else
889 dts_bits_get( bits, 25 * nChSetLLChannel, bits_pos ); /* RadiusDelta[ch] (9)
890 * Theta[ch] (9)
891 * Phi[ch] (7)
892 * per channel */
894 else
896 /* No downmixing is allowed and each channel set is the primary channel set. */
897 if( dts_bits_get( bits, 1, bits_pos ) ) /* bMappingCoeffsPresent (1) */
899 int nBitsCh2SpkrCoef = 6 + 2 * dts_bits_get( bits, 3, bits_pos ); /* nBitsCh2SpkrCoef (3) */
900 int nNumSpeakerConfigs = dts_bits_get( bits, 2, bits_pos ) + 1; /* nNumSpeakerConfigs (2) */
901 for( int nSpkrConf = 0; nSpkrConf < nNumSpeakerConfigs; nSpkrConf++ )
903 int pnActiveChannelMask = dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* pnActiveChannelMask[nSpkrConf] (nChSetLLChannel) */
904 int pnNumSpeakers = dts_bits_get( bits, 6, bits_pos ) + 1; /* pnNumSpeakers[nSpkrConf] (6) */
905 int bSpkrMaskEnabled = dts_bits_get( bits, 1, bits_pos ); /* bSpkrMaskEnabled (1) */
906 if( bSpkrMaskEnabled )
908 uint32_t nSpkrMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nSpkrMask[nSpkrConf] (nBits4ChMask) */
909 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nSpkrMask );
910 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nSpkrMask );
912 for( int nSpkr = 0; nSpkr < pnNumSpeakers; nSpkr++ )
914 if( !bSpkrMaskEnabled )
915 dts_bits_get( bits, 25, bits_pos ); /* ChSetSpeakerConfiguration (25) */
916 for( int nCh = 0; nCh < nChSetLLChannel; nCh++ )
917 if( pnActiveChannelMask & (1 << nCh) )
918 dts_bits_get( bits, nBitsCh2SpkrCoef, bits_pos ); /* pnCh2SpkrMapCoeff (nBitsCh2SpkrCoef) */
923 int nNumFreqBands;
924 if( nFs > 96000 )
926 if( dts_bits_get( bits, 1, bits_pos ) ) /* bXtraFreqBands (1) */
927 nNumFreqBands = nFs > 192000 ? 4 : 2;
928 else
929 nNumFreqBands = nFs > 192000 ? 2 : 1;
931 else
932 nNumFreqBands = 1;
933 uint32_t nSmplInSeg_nChSet;
934 if( nChSet == 0 )
936 nFs1 = nFs;
937 nNumFreqBands1 = nNumFreqBands;
938 nSmplInSeg_nChSet = nSmplInSeg;
940 else
941 nSmplInSeg_nChSet = (nSmplInSeg * (nFs * nNumFreqBands1)) / (nFs1 * nNumFreqBands);
942 if( xll->sampling_frequency < nFs )
944 xll->sampling_frequency = nFs;
945 uint32_t samples_per_band_in_frame = nSegmentsInFrame * nSmplInSeg_nChSet;
946 xll->frame_duration = samples_per_band_in_frame * nNumFreqBands;
948 dts_bits_get( bits, nChSetHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Channel Set Sub-Header. */
950 info->flags |= DTS_EXT_SUBSTREAM_XLL_FLAG;
951 return bits->bs->error ? -1 : 0;
954 static uint16_t dts_generate_channel_layout_from_core( int channel_arrangement )
956 static const uint16_t channel_layout_map_table[] =
958 DTS_CHANNEL_LAYOUT_C,
959 DTS_CHANNEL_LAYOUT_L_R, /* dual mono */
960 DTS_CHANNEL_LAYOUT_L_R, /* stereo */
961 DTS_CHANNEL_LAYOUT_L_R, /* sum-difference */
962 DTS_CHANNEL_LAYOUT_L_R, /* Lt/Rt */
963 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R,
964 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
965 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
966 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
967 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
968 DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
969 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR | DTS_CHANNEL_LAYOUT_OH,
970 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR,
971 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS,
972 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS | DTS_CHANNEL_LAYOUT_LSR_RSR,
973 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS
975 return channel_arrangement < 16 ? channel_layout_map_table[channel_arrangement] : 0;
978 static int dts_parse_core( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
980 lsmash_bits_t *bits = info->bits;
981 memset( core, 0, sizeof(dts_core_info_t) );
982 /* SYNC (32) */
983 int frame_type = dts_bits_get( bits, 1, bits_pos ); /* FTYPE (1) */
984 int deficit_sample_count = dts_bits_get( bits, 5, bits_pos ); /* SHORT (5) */
985 if( frame_type == 1 && deficit_sample_count != 31 )
986 return -1; /* Any normal frame (FTYPE == 1) must have SHORT == 31. */
987 int crc_present_flag = dts_bits_get( bits, 1, bits_pos ); /* CPF (1) */
988 int num_of_pcm_sample_blocks = dts_bits_get( bits, 7, bits_pos ) + 1; /* NBLKS (7) */
989 if( num_of_pcm_sample_blocks <= 5 )
990 return -1;
991 core->frame_duration = 32 * num_of_pcm_sample_blocks;
992 if( frame_type == 1
993 && core->frame_duration != 256
994 && core->frame_duration != 512 && core->frame_duration != 1024
995 && core->frame_duration != 2048 && core->frame_duration != 4096 )
996 return -1; /* For any normal frame, the actual number of PCM core samples per channel must be
997 * either 4096, 2048, 1024, 512, or 256 samples per channel. */
998 core->frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* FSIZE (14) */
999 if( core->frame_size < DTS_MIN_CORE_SIZE )
1000 return -1;
1001 core->channel_arrangement = dts_bits_get( bits, 6, bits_pos ); /* AMODE (6) */
1002 core->channel_layout = dts_generate_channel_layout_from_core( core->channel_arrangement );
1003 int core_audio_sampling_frequency = dts_bits_get( bits, 4, bits_pos ); /* SFREQ (4) */
1004 static const uint32_t sampling_frequency_table[16] =
1007 8000, 16000, 32000, 0, 0,
1008 11025, 22050, 44100, 0, 0,
1009 12000, 24000, 48000, 0, 0
1011 core->sampling_frequency = sampling_frequency_table[core_audio_sampling_frequency];
1012 if( core->sampling_frequency == 0 )
1013 return -1; /* invalid */
1014 dts_bits_get( bits, 10, bits_pos ); /* Skip remainder 10 bits.
1015 * RATE (5)
1016 * MIX (1)
1017 * DYNF (1)
1018 * TIMEF (1)
1019 * AUXF (1)
1020 * HDCD (1) */
1021 core->extension_audio_descriptor = dts_bits_get( bits, 3, bits_pos ); /* EXT_AUDIO_ID (3)
1022 * Note: EXT_AUDIO_ID == 3 is defined in V1.2.1.
1023 * However, its definition disappears and is reserved in V1.3.1. */
1024 int extended_coding_flag = dts_bits_get( bits, 1, bits_pos ); /* EXT_AUDIO (1) */
1025 dts_bits_get( bits, 1, bits_pos ); /* ASPF (1) */
1026 int low_frequency_effects_flag = dts_bits_get( bits, 2, bits_pos ); /* LFF (2) */
1027 if( low_frequency_effects_flag == 0x3 )
1028 return -1; /* invalid */
1029 if( low_frequency_effects_flag )
1030 core->channel_layout |= DTS_CHANNEL_LAYOUT_LFE1;
1031 dts_bits_get( bits, 8 + crc_present_flag * 16, bits_pos ); /* HFLAG (1)
1032 * HCRC (16)
1033 * FILTS (1)
1034 * VERNUM (4)
1035 * CHIST (2) */
1036 int PCMR = dts_bits_get( bits, 3, bits_pos ); /* PCMR (3) */
1037 static const uint8_t source_resolution_table[8] = { 16, 16, 20, 20, 0, 24, 24, 0 };
1038 core->pcm_resolution = source_resolution_table[PCMR];
1039 if( core->pcm_resolution == 0 )
1040 return -1; /* invalid */
1041 dts_bits_get( bits, 6, bits_pos ); /* SUMF (1)
1042 * SUMS (1)
1043 * DIALNORM/UNSPEC (4) */
1044 if( extended_coding_flag )
1046 uint32_t syncword = dts_bits_get( bits, 24, bits_pos );
1047 uint64_t frame_size_bits = core->frame_size * 8;
1048 while( (*bits_pos + 24) < frame_size_bits )
1050 syncword = ((syncword << 8) & 0xffffff00) | dts_bits_get( bits, 8, bits_pos );
1051 switch( syncword )
1053 case DTS_SYNCWORD_XXCH :
1054 if( dts_parse_core_xxch( info, bits_pos, core ) )
1055 return -1;
1056 syncword = dts_bits_get( bits, 24, bits_pos );
1057 break;
1058 case DTS_SYNCWORD_X96K :
1059 if( dts_parse_core_x96( info, bits_pos, core ) )
1060 return -1;
1061 syncword = dts_bits_get( bits, 24, bits_pos );
1062 break;
1063 case DTS_SYNCWORD_XCH :
1064 if( dts_parse_core_xch( info, bits_pos, core ) )
1065 return -1;
1066 break;
1067 default :
1068 continue;
1072 return bits->bs->error ? -1 : 0;
1075 static int dts_parse_exss_core( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
1077 lsmash_bits_t *bits = info->bits;
1078 if( DTS_SYNCWORD_SUBSTREAM_CORE != dts_bits_get( bits, 32, bits_pos ) )
1079 return -1;
1080 if( dts_parse_core( info, bits_pos, &asset->core ) < 0 )
1081 return -1;
1082 info->flags |= DTS_EXT_SUBSTREAM_CORE_FLAG;
1083 return bits->bs->error ? -1 : 0;
1086 int dts_parse_core_substream( dts_info_t *info )
1088 lsmash_bits_t *bits = info->bits;
1089 uint64_t bits_pos = 0;
1090 if( DTS_SYNCWORD_CORE != dts_bits_get( bits, 32, &bits_pos ) )
1091 goto parse_fail;
1092 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0. */
1093 dts_extension_info_t *exss = &info->exss[0];
1094 if( dts_parse_core( info, &bits_pos, &exss->asset[0].core ) < 0 )
1095 goto parse_fail;
1096 exss->bBcCorePresent [0] = 1;
1097 exss->nuBcCoreExtSSIndex[0] = 0;
1098 exss->nuBcCoreAssetIndex[0] = 0;
1099 info->flags |= DTS_CORE_SUBSTREAM_CORE_FLAG;
1100 info->exss_count = 0;
1101 info->core = exss->asset[0].core;
1102 info->frame_size = exss->asset[0].core.frame_size;
1103 lsmash_bits_get_align( bits );
1104 return 0;
1105 parse_fail:
1106 lsmash_bits_get_align( bits );
1107 return -1;
1110 int dts_parse_extension_substream( dts_info_t *info )
1112 lsmash_bits_t *bits = info->bits;
1113 uint64_t bits_pos = 0;
1114 dts_bits_get( bits, 40, &bits_pos ); /* SYNCEXTSSH (32)
1115 * UserDefinedBits (8) */
1116 int nExtSSIndex = dts_bits_get( bits, 2, &bits_pos ); /* nExtSSIndex (2) */
1117 info->exss_index = nExtSSIndex;
1118 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1119 memset( exss, 0, sizeof(dts_extension_info_t) );
1120 int bHeaderSizeType = dts_bits_get( bits, 1, &bits_pos ); /* bHeaderSizeType (1) */
1121 int nuBits4Header = 8 + bHeaderSizeType * 4;
1122 int nuBits4ExSSFsize = 16 + bHeaderSizeType * 4;
1123 exss->nuBits4ExSSFsize = nuBits4ExSSFsize;
1124 uint32_t nuExtSSHeaderSize = dts_bits_get( bits, nuBits4Header, &bits_pos ) + 1; /* nuExtSSHeaderSize (8 or 12) */
1125 info->frame_size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuExtSSFsize (16 or 20) */
1126 if( info->frame_size < 10 )
1127 return -1;
1128 exss->bStaticFieldsPresent = dts_bits_get( bits, 1, &bits_pos ); /* bStaticFieldsPresent (1) */
1129 if( exss->bStaticFieldsPresent )
1131 dts_bits_get( bits, 2, &bits_pos ); /* nuRefClockCode (2) */
1132 exss->frame_duration = 512 * (dts_bits_get( bits, 3, &bits_pos ) + 1); /* nuExSSFrameDurationCode (3) */
1133 if( dts_bits_get( bits, 1, &bits_pos ) ) /* bTimeStampFlag (1) */
1134 dts_bits_get( bits, 36, &bits_pos ); /* nuTimeStamp (32)
1135 * nLSB (4) */
1136 exss->nuNumAudioPresnt = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAudioPresnt (3) */
1137 exss->nuNumAssets = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAssets (3) */
1138 /* The extension substreams with indexes lower than or equal to the index of the current extension substream can
1139 * be activated in the audio presentations indicated within the current extension substream. */
1140 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1141 exss->nuActiveExSSMask[nAuPr]
1142 = dts_bits_get( bits, nExtSSIndex + 1, &bits_pos ); /* nuActiveExSSMask[nAuPr] (nExtSSIndex + 1) */
1143 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1144 for( uint8_t nSS = 0; nSS <= nExtSSIndex; nSS++ )
1145 exss->nuActiveAssetMask[nAuPr][nSS]
1146 = ((exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1)
1147 ? dts_bits_get( bits, 8, &bits_pos ) /* nuActiveAssetMask[nAuPr][nSS] (8) */
1148 : 0;
1149 exss->bMixMetadataEnbl = dts_bits_get( bits, 1, &bits_pos ); /* bMixMetadataEnbl (1) */
1150 if( exss->bMixMetadataEnbl )
1152 dts_bits_get( bits, 2, &bits_pos ); /* nuMixMetadataAdjLevel (2) */
1153 int nuBits4MixOutMask = (dts_bits_get( bits, 2, &bits_pos ) + 1) << 2; /* nuBits4MixOutMask (2) */
1154 exss->nuNumMixOutConfigs = dts_bits_get( bits, 2, &bits_pos ) + 1; /* nuNumMixOutConfigs (2) */
1155 for( int ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
1157 int nuMixOutChMask = dts_bits_get( bits, nuBits4MixOutMask, &bits_pos ); /* nuMixOutChMask[ns] (nuBits4MixOutMask) */
1158 exss->nNumMixOutCh[ns] = dts_get_channel_count_from_channel_layout( nuMixOutChMask );
1162 else
1164 exss->nuNumAudioPresnt = 1;
1165 exss->nuNumAssets = 1;
1166 exss->bMixMetadataEnbl = 0;
1167 exss->nuNumMixOutConfigs = 0;
1169 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1170 exss->asset[nAst].size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuAssetFsize[nAst] - 1 (nuBits4ExSSFsize) */
1171 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1172 if( dts_parse_asset_descriptor( info, &bits_pos ) < 0 )
1173 goto parse_fail;
1174 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1175 exss->bBcCorePresent[nAuPr] = dts_bits_get( bits, 1, &bits_pos );
1176 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1177 if( exss->bBcCorePresent[nAuPr] )
1179 exss->nuBcCoreExtSSIndex[nAuPr] = dts_bits_get( bits, 2, &bits_pos );
1180 exss->nuBcCoreAssetIndex[nAuPr] = dts_bits_get( bits, 3, &bits_pos );
1182 dts_bits_get( bits, nuExtSSHeaderSize * 8 - bits_pos, &bits_pos );
1183 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1185 /* Asset Data */
1186 dts_audio_asset_t *asset = &exss->asset[nAst];
1187 uint32_t asset_pos = bits_pos;
1188 switch( asset->nuCodingMode )
1190 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
1192 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
1194 /* Core component */
1195 uint64_t core_pos = bits_pos;
1196 if( dts_parse_exss_core( info, &bits_pos, asset ) < 0 )
1197 goto parse_fail;
1198 dts_bits_get( bits, asset->core.frame_size * 8 - (bits_pos - core_pos), &bits_pos );
1200 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
1202 /* XBR extension */
1203 uint64_t xbr_pos = bits_pos;
1204 if( dts_parse_exss_xbr( info, &bits_pos ) < 0 )
1205 goto parse_fail;
1206 dts_bits_get( bits, asset->xbr_size * 8 - (bits_pos - xbr_pos), &bits_pos );
1208 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1210 /* XXCH extension */
1211 uint64_t xxch_pos = bits_pos;
1212 if( dts_parse_exss_xxch( info, &bits_pos, &asset->core ) < 0 )
1213 goto parse_fail;
1214 dts_bits_get( bits, asset->core.xxch.size * 8 - (bits_pos - xxch_pos), &bits_pos );
1216 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
1218 /* X96 extension */
1219 uint64_t x96_pos = bits_pos;
1220 if( dts_parse_exss_x96( info, &bits_pos, &asset->core ) < 0 )
1221 goto parse_fail;
1222 dts_bits_get( bits, asset->x96_size * 8 - (bits_pos - x96_pos), &bits_pos );
1224 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
1226 /* LBR component */
1227 uint64_t lbr_pos = bits_pos;
1228 if( dts_parse_exss_lbr( info, &bits_pos, asset ) < 0 )
1229 goto parse_fail;
1230 dts_bits_get( bits, asset->lbr.size * 8 - (bits_pos - lbr_pos), &bits_pos );
1232 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
1234 /* Lossless extension */
1235 uint64_t xll_pos = bits_pos;
1236 if( dts_parse_exss_xll( info, &bits_pos, asset ) < 0 )
1237 goto parse_fail;
1238 dts_bits_get( bits, asset->xll.size * 8 - (bits_pos - xll_pos), &bits_pos );
1240 break;
1242 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
1243 if( dts_parse_exss_xll( info, &bits_pos, asset ) < 0 )
1244 goto parse_fail;
1245 break;
1246 case 2 : /* DTS-HD Low bit-rate mode */
1247 if( dts_parse_exss_lbr( info, &bits_pos, asset ) < 0 )
1248 goto parse_fail;
1249 break;
1250 case 3 : /* Auxiliary coding mode */
1251 dts_bits_get( bits, asset->aux_size * 8, &bits_pos );
1252 break;
1254 dts_bits_get( bits, asset->size * 8 - (bits_pos - asset_pos), &bits_pos );
1256 dts_bits_get( bits, info->frame_size * 8 - bits_pos, &bits_pos );
1257 lsmash_bits_get_align( bits );
1258 if( info->exss_count < DTS_MAX_NUM_EXSS )
1259 info->exss_count += 1;
1260 return 0;
1261 parse_fail:
1262 lsmash_bits_get_align( bits );
1263 return -1;
1266 dts_substream_type dts_get_substream_type( dts_info_t *info )
1268 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 4 )
1269 return DTS_SUBSTREAM_TYPE_NONE;
1270 uint8_t *buffer = lsmash_bs_get_buffer_data( info->bits->bs );
1271 uint32_t syncword = LSMASH_4CC( buffer[0], buffer[1], buffer[2], buffer[3] );
1272 switch( syncword )
1274 case DTS_SYNCWORD_CORE :
1275 return DTS_SUBSTREAM_TYPE_CORE;
1276 case DTS_SYNCWORD_SUBSTREAM :
1277 return DTS_SUBSTREAM_TYPE_EXTENSION;
1278 default :
1279 return DTS_SUBSTREAM_TYPE_NONE;
1283 int dts_get_exss_index( dts_info_t *info, uint8_t *exss_index )
1285 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 6 )
1286 return -1;
1287 *exss_index = lsmash_bs_show_byte( info->bits->bs, 5 ) >> 6;
1288 return 0;
1291 int dts_get_max_channel_count( dts_info_t *info )
1293 int max_channel_count = 0;
1294 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1296 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1297 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1299 /* Get the channel layout of an audio presentation from a core component. */
1300 uint16_t channel_layout = 0;
1301 int channel_count = 0;
1302 if( exss->bBcCorePresent [nAuPr]
1303 && exss->nuBcCoreAssetIndex[nAuPr] < exss->nuNumAssets )
1305 dts_core_info_t *core = &info->exss[ exss->nuBcCoreExtSSIndex[nAuPr] ].asset[ exss->nuBcCoreAssetIndex[nAuPr] ].core;
1306 if( core->xxch.channel_layout | core->xxch.lower_planes )
1308 channel_layout = core->xxch.channel_layout;
1309 channel_count = lsmash_count_bits( core->xxch.lower_planes ); /* FIXME: Should we count these channels? */
1311 else
1312 channel_layout = core->channel_layout;
1314 channel_count += dts_get_channel_count_from_channel_layout( channel_layout );
1315 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1316 /* Get the channel layouts of an audio presentation from extension substreams. */
1317 uint16_t ext_channel_layout = 0;
1318 uint16_t lbr_channel_layout = 0;
1319 uint16_t xll_channel_layout = 0;
1320 uint8_t xll_lower_channels = 0;
1321 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1322 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1323 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1324 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1326 dts_audio_asset_t *asset = &exss->asset[nAst];
1327 ext_channel_layout |= asset->channel_layout;
1328 lbr_channel_layout |= asset->lbr.channel_layout;
1329 xll_channel_layout |= asset->xll.channel_layout;
1330 xll_lower_channels |= asset->xll.lower_planes;
1332 /* Audio asset descriptors */
1333 channel_count = dts_get_channel_count_from_channel_layout( ext_channel_layout );
1334 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1335 /* LBR components */
1336 channel_count = dts_get_channel_count_from_channel_layout( lbr_channel_layout );
1337 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1338 /* Lossless extensions */
1339 channel_count = dts_get_channel_count_from_channel_layout( xll_channel_layout )
1340 + lsmash_count_bits( xll_lower_channels );
1341 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1344 return max_channel_count;
1347 void dts_update_specific_param( dts_info_t *info )
1349 lsmash_dts_specific_parameters_t *param = &info->ddts_param;
1350 int exss_index_start = 0;
1351 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1353 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1354 if( exss->nuNumAudioPresnt && exss->nuNumAssets )
1356 exss_index_start = nExtSSIndex;
1357 break;
1360 /* DTSSamplingFrequency and FrameDuration */
1361 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1363 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1364 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1365 continue;
1366 if( param->DTSSamplingFrequency <= exss->sampling_frequency )
1368 param->DTSSamplingFrequency = exss->sampling_frequency;
1369 info->frame_duration = exss->frame_duration;
1371 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1373 dts_audio_asset_t *asset = &exss->asset[nAst];
1374 if( param->DTSSamplingFrequency <= asset->core.sampling_frequency )
1376 param->DTSSamplingFrequency = asset->core.sampling_frequency;
1377 info->frame_duration = asset->core.frame_duration;
1379 if( param->DTSSamplingFrequency <= asset->lbr.sampling_frequency )
1381 param->DTSSamplingFrequency = asset->lbr.sampling_frequency;
1382 info->frame_duration = asset->lbr.frame_duration;
1384 if( param->DTSSamplingFrequency <= asset->xll.sampling_frequency )
1386 param->DTSSamplingFrequency = asset->xll.sampling_frequency;
1387 info->frame_duration = asset->xll.frame_duration;
1391 param->FrameDuration = 0;
1392 for( uint32_t frame_duration = info->frame_duration >> 10; frame_duration; frame_duration >>= 1 )
1393 ++ param->FrameDuration;
1394 /* pcmSampleDepth */
1395 param->pcmSampleDepth = 0;
1396 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1398 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1399 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1400 continue;
1401 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, exss->bit_resolution );
1402 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1404 dts_audio_asset_t *asset = &exss->asset[nAst];
1405 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->core.pcm_resolution );
1406 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->lbr.sample_size );
1407 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->xll.pcm_resolution );
1410 param->pcmSampleDepth = param->pcmSampleDepth > 16 ? 24 : 16;
1411 /* StreamConstruction */
1412 param->StreamConstruction = lsmash_dts_get_stream_construction( info->flags );
1413 /* CoreLFEPresent */
1414 param->CoreLFEPresent = !!(info->core.channel_layout & DTS_CHANNEL_LAYOUT_LFE1);
1415 /* CoreLayout */
1416 if( param->StreamConstruction == 0 /* Unknown */
1417 || param->StreamConstruction >= 17 /* No core substream */ )
1418 /* Use ChannelLayout. */
1419 param->CoreLayout = 31;
1420 else
1422 if( info->core.channel_arrangement != 1
1423 && info->core.channel_arrangement != 3
1424 && info->core.channel_arrangement <= 9 )
1425 param->CoreLayout = info->core.channel_arrangement;
1426 else
1427 /* Use ChannelLayout. */
1428 param->CoreLayout = 31;
1430 /* CoreSize
1431 * The specification says this field is the size of a core substream AU in bytes.
1432 * If we don't assume CoreSize is the copy of FSIZE, when FSIZE equals 0x3FFF, this field overflows and becomes 0. */
1433 param->CoreSize = info->core.frame_size ? LSMASH_MIN( info->core.frame_size - 1, 0x3FFF ) : 0;
1434 /* StereoDownmix */
1435 param->StereoDownmix = 0;
1436 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1438 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1439 param->StereoDownmix |= exss->stereo_downmix;
1440 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1442 param->StereoDownmix |= exss->asset[nAst].lbr.stereo_downmix;
1443 param->StereoDownmix |= exss->asset[nAst].xll.stereo_downmix;
1446 /* RepresentationType
1447 * Available only when core substream is absent and ChannelLayout is set to 0. */
1448 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1450 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1451 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1452 continue;
1453 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1455 int asset_count = 0;
1456 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1457 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1458 asset_count += lsmash_count_bits( exss->nuActiveAssetMask[nAuPr][nSS] );
1459 if( asset_count > 1 )
1461 /* An audio presentation has mulple audio assets.
1462 * Audio asset designated for mixing with another audio asset. */
1463 param->RepresentationType = 0;
1464 nExtSSIndex = DTS_MAX_NUM_EXSS;
1465 break;
1467 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1468 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1469 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1470 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1472 dts_audio_asset_t *asset = &exss->asset[nAst];
1473 if( asset->nuRepresentationType == info->exss[exss_index_start].asset[0].nuRepresentationType )
1474 param->RepresentationType = asset->nuRepresentationType;
1475 else
1477 /* Detected different representation types. Use ChannelLayout. */
1478 param->RepresentationType = 0;
1479 nAuPr = exss->nuNumAudioPresnt;
1480 nExtSSIndex = DTS_MAX_NUM_EXSS;
1481 break;
1486 /* ChannelLayout
1487 * complete information on channels coded in the audio stream including core and extensions */
1488 param->ChannelLayout = 0;
1489 if( param->RepresentationType == 0 )
1490 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1492 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1493 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1494 continue;
1495 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1497 dts_audio_asset_t *asset = &exss->asset[nAst];
1498 param->ChannelLayout |= asset->channel_layout;
1499 param->ChannelLayout |= asset->core.channel_layout;
1500 param->ChannelLayout |= asset->core.xxch.channel_layout;
1501 param->ChannelLayout |= asset->lbr.channel_layout;
1502 param->ChannelLayout |= asset->xll.channel_layout;
1505 /* MultiAssetFlag
1506 * When multiple assets exist, the remaining parameters in the DTSSpecificBox only reflect the coding parameters of the first asset. */
1507 param->MultiAssetFlag = ((info->exss[0].nuNumAssets
1508 + info->exss[1].nuNumAssets
1509 + info->exss[2].nuNumAssets
1510 + info->exss[3].nuNumAssets) > 1);
1511 /* LBRDurationMod */
1512 param->LBRDurationMod = info->exss[exss_index_start].asset[0].lbr.duration_modifier;
1513 info->ddts_param_initialized = 1;
1516 int dts_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1518 assert( dst && dst->data.structured && src && src->data.unstructured );
1519 if( src->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1520 return -1;
1521 lsmash_dts_specific_parameters_t *param = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1522 uint8_t *data = src->data.unstructured;
1523 uint64_t size = LSMASH_GET_BE32( data );
1524 int dts_specific_box_min_length = DTS_SPECIFIC_BOX_MIN_LENGTH;
1525 data += ISOM_BASEBOX_COMMON_SIZE;
1526 if( size == 1 )
1528 size = LSMASH_GET_BE64( data );
1529 dts_specific_box_min_length += 8;
1530 data += 8;
1532 if( size != src->size )
1533 return -1;
1534 param->DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1535 param->maxBitrate = LSMASH_GET_BE32( &data[4] );
1536 param->avgBitrate = LSMASH_GET_BE32( &data[8] );
1537 param->pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1538 param->FrameDuration = (data[13] >> 6) & 0x03;
1539 param->StreamConstruction = (data[13] >> 1) & 0x1F;
1540 param->CoreLFEPresent = data[13] & 0x01;
1541 param->CoreLayout = (data[14] >> 2) & 0x3F;
1542 param->CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1543 param->StereoDownmix = (data[16] >> 3) & 0x01;
1544 param->RepresentationType = data[16] & 0x07;
1545 param->ChannelLayout = (data[17] << 8) | data[18];
1546 param->MultiAssetFlag = (data[19] >> 7) & 0x01;
1547 param->LBRDurationMod = (data[19] >> 6) & 0x01;
1548 int reserved_box_present = ((data[19] >> 5) & 0x01) && (size > DTS_SPECIFIC_BOX_MIN_LENGTH);
1549 if( reserved_box_present )
1550 lsmash_append_dts_reserved_box( param, data + 20, size - DTS_SPECIFIC_BOX_MIN_LENGTH );
1551 return 0;
1554 int dts_copy_codec_specific( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1556 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
1557 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
1558 lsmash_dts_specific_parameters_t *src_data = (lsmash_dts_specific_parameters_t *)src->data.structured;
1559 lsmash_dts_specific_parameters_t *dst_data = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1560 lsmash_remove_dts_reserved_box( dst_data );
1561 *dst_data = *src_data;
1562 if( !src_data->box && src_data->box->data && src_data->box->size )
1563 return 0;
1564 return lsmash_append_dts_reserved_box( dst_data, src_data->box->data, src_data->box->size );
1567 int dts_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
1569 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
1570 int indent = level;
1571 lsmash_ifprintf( fp, indent++, "[%s: DTS Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
1572 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
1573 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
1574 if( box->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1575 return -1;
1576 uint8_t *data = box->binary;
1577 isom_skip_box_common( &data );
1578 uint32_t DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1579 uint32_t maxBitrate = LSMASH_GET_BE32( &data[4] );
1580 uint32_t avgBitrate = LSMASH_GET_BE32( &data[8] );
1581 uint8_t pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1582 uint8_t FrameDuration = (data[13] >> 6) & 0x03;
1583 uint8_t StreamConstruction = (data[13] >> 1) & 0x1F;
1584 uint8_t CoreLFEPresent = data[13] & 0x01;
1585 uint8_t CoreLayout = (data[14] >> 2) & 0x3F;
1586 uint16_t CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1587 uint8_t StereoDownmix = (data[16] >> 3) & 0x01;
1588 uint8_t RepresentationType = data[16] & 0x07;
1589 uint16_t ChannelLayout = (data[17] << 8) | data[18];
1590 uint8_t MultiAssetFlag = (data[19] >> 7) & 0x01;
1591 uint8_t LBRDurationMod = (data[19] >> 6) & 0x01;
1592 uint8_t ReservedBoxPresent = (data[19] >> 5) & 0x01;
1593 uint8_t Reserved = data[19] & 0x1F;
1594 uint32_t frame_duration = 512 << FrameDuration;
1595 int construction_flags = StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? construction_info[StreamConstruction] : 0;
1596 static const char *core_layout_description[64] =
1598 "Mono (1/0)",
1599 "Undefined",
1600 "Stereo (2/0)",
1601 "Undefined",
1602 "LT,RT (2/0)",
1603 "L, C, R (3/0)",
1604 "L, R, S (2/1)",
1605 "L, C, R, S (3/1)",
1606 "L, R, LS, RS (2/2)",
1607 "L, C, R, LS, RS (3/2)",
1608 [31] = "use ChannelLayout"
1610 static const char *representation_type_description[8] =
1612 "Audio asset designated for mixing with another audio asset",
1613 "Reserved",
1614 "Lt/Rt Encoded for matrix surround decoding",
1615 "Audio processed for headphone playback",
1616 "Reserved",
1617 "Reserved",
1618 "Reserved",
1619 "Reserved"
1621 static const char *channel_layout_description[16] =
1623 "Center in front of listener",
1624 "Left/Right in front",
1625 "Left/Right surround on side in rear",
1626 "Low frequency effects subwoofer",
1627 "Center surround in rear",
1628 "Left/Right height in front",
1629 "Left/Right surround in rear",
1630 "Center Height in front",
1631 "Over the listener's head",
1632 "Between left/right and center in front",
1633 "Left/Right on side in front",
1634 "Left/Right surround on side",
1635 "Second low frequency effects subwoofer",
1636 "Left/Right height on side",
1637 "Center height in rear",
1638 "Left/Right height in rear"
1640 lsmash_ifprintf( fp, indent, "DTSSamplingFrequency = %"PRIu32" Hz\n", DTSSamplingFrequency );
1641 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32" bit/s\n", maxBitrate );
1642 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32" bit/s\n", avgBitrate );
1643 lsmash_ifprintf( fp, indent, "pcmSampleDepth = %"PRIu8" bits\n", pcmSampleDepth );
1644 lsmash_ifprintf( fp, indent, "FrameDuration = %"PRIu8" (%"PRIu32" samples)\n", FrameDuration, frame_duration );
1645 lsmash_ifprintf( fp, indent, "StreamConstruction = 0x%02"PRIx8"\n", StreamConstruction );
1646 if( construction_flags & (DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG) )
1648 lsmash_ifprintf( fp, indent + 1, "Core substream\n" );
1649 if( construction_flags & DTS_CORE_SUBSTREAM_CORE_FLAG )
1650 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1651 if( construction_flags & DTS_CORE_SUBSTREAM_XCH_FLAG )
1652 lsmash_ifprintf( fp, indent + 2, "XCH\n" );
1653 if( construction_flags & DTS_CORE_SUBSTREAM_X96_FLAG )
1654 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1655 if( construction_flags & DTS_CORE_SUBSTREAM_XXCH_FLAG )
1656 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1658 if( construction_flags & (DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG
1659 | DTS_EXT_SUBSTREAM_XBR_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG | DTS_EXT_SUBSTREAM_LBR_FLAG) )
1661 lsmash_ifprintf( fp, indent + 1, "Extension substream\n" );
1662 if( construction_flags & DTS_EXT_SUBSTREAM_CORE_FLAG )
1663 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1664 if( construction_flags & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1665 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1666 if( construction_flags & DTS_EXT_SUBSTREAM_X96_FLAG )
1667 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1668 if( construction_flags & DTS_EXT_SUBSTREAM_XBR_FLAG )
1669 lsmash_ifprintf( fp, indent + 2, "XBR\n" );
1670 if( construction_flags & DTS_EXT_SUBSTREAM_XLL_FLAG )
1671 lsmash_ifprintf( fp, indent + 2, "XLL\n" );
1672 if( construction_flags & DTS_EXT_SUBSTREAM_LBR_FLAG )
1673 lsmash_ifprintf( fp, indent + 2, "LBR\n" );
1675 lsmash_ifprintf( fp, indent, "CoreLFEPresent = %s\n", CoreLFEPresent ? "1 (LFE exists)" : "0 (no LFE)" );
1676 if( core_layout_description[CoreLayout] )
1677 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (%s)\n", CoreLayout, core_layout_description[CoreLayout] );
1678 else
1679 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (Undefined)\n", CoreLayout );
1680 if( CoreSize )
1681 lsmash_ifprintf( fp, indent, "CoreSize = %"PRIu16"\n", CoreSize );
1682 else
1683 lsmash_ifprintf( fp, indent, "CoreSize = 0 (no core substream exists)\n" );
1684 lsmash_ifprintf( fp, indent, "StereoDownmix = %s\n", StereoDownmix ? "1 (embedded downmix present)" : "0 (no embedded downmix)" );
1685 lsmash_ifprintf( fp, indent, "RepresentationType = %"PRIu8" (%s)\n", RepresentationType, representation_type_description[RepresentationType] );
1686 lsmash_ifprintf( fp, indent, "ChannelLayout = 0x%04"PRIx16"\n", ChannelLayout );
1687 if( ChannelLayout )
1688 for( int i = 0; i < 16; i++ )
1689 if( (ChannelLayout >> i) & 0x01 )
1690 lsmash_ifprintf( fp, indent + 1, "%s\n", channel_layout_description[i] );
1691 lsmash_ifprintf( fp, indent, "MultiAssetFlag = %s\n", MultiAssetFlag ? "1 (multiple asset)" : "0 (single asset)" );
1692 if( LBRDurationMod )
1693 lsmash_ifprintf( fp, indent, "LBRDurationMod = 1 (%"PRIu32" -> %"PRIu32" samples)\n", frame_duration, (frame_duration * 3) / 2 );
1694 else
1695 lsmash_ifprintf( fp, indent, "LBRDurationMod = 0 (no LBR duration modifier)\n" );
1696 lsmash_ifprintf( fp, indent, "ReservedBoxPresent = %s\n", ReservedBoxPresent ? "1 (ReservedBox present)" : "0 (no ReservedBox)" );
1697 lsmash_ifprintf( fp, indent, "Reserved = 0x%02"PRIx8"\n", Reserved );
1698 return 0;