msvc: Add a missing file.
[L-SMASH.git] / codecs / dts.c
blob0a1282d0d87541ef9026ee7ef1352105f1217ee4
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 LSMASH_ERR_FUNCTION_PARAM;
171 param->box = lsmash_malloc( sizeof(lsmash_dts_reserved_box_t) );
172 if( !param->box )
173 return LSMASH_ERR_MEMORY_ALLOC;
174 param->box->data = lsmash_memdup( box_data, box_size );
175 if( !param->box->data )
177 lsmash_freep( &param->box );
178 return LSMASH_ERR_MEMORY_ALLOC;
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 int reserved_box_present = (param->box && param->box->data && param->box->size);
256 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
257 if( !bits )
258 return NULL;
259 /* Create a DTSSpecificBox. */
260 lsmash_bits_put( bits, 32, 0 ); /* box size */
261 lsmash_bits_put( bits, 32, ISOM_BOX_TYPE_DDTS.fourcc ); /* box type: 'ddts' */
262 lsmash_bits_put( bits, 32, param->DTSSamplingFrequency );
263 lsmash_bits_put( bits, 32, param->maxBitrate ); /* maxBitrate; setup by isom_update_bitrate_description */
264 lsmash_bits_put( bits, 32, param->avgBitrate ); /* avgBitrate; setup by isom_update_bitrate_description */
265 lsmash_bits_put( bits, 8, param->pcmSampleDepth );
266 lsmash_bits_put( bits, 2, param->FrameDuration );
267 lsmash_bits_put( bits, 5, param->StreamConstruction );
268 lsmash_bits_put( bits, 1, param->CoreLFEPresent );
269 lsmash_bits_put( bits, 6, param->CoreLayout );
270 lsmash_bits_put( bits, 14, param->CoreSize );
271 lsmash_bits_put( bits, 1, param->StereoDownmix );
272 lsmash_bits_put( bits, 3, param->RepresentationType );
273 lsmash_bits_put( bits, 16, param->ChannelLayout );
274 lsmash_bits_put( bits, 1, param->MultiAssetFlag );
275 lsmash_bits_put( bits, 1, param->LBRDurationMod );
276 lsmash_bits_put( bits, 1, reserved_box_present );
277 lsmash_bits_put( bits, 5, 0 ); /* Reserved */
278 /* ReservedBox */
279 if( reserved_box_present )
280 for( uint32_t i = 0; i < param->box->size; i++ )
281 lsmash_bits_put( bits, 8, param->box->data[i] );
282 /* */
283 uint8_t *data = lsmash_bits_export_data( bits, data_length );
284 lsmash_bits_adhoc_cleanup( bits );
285 /* Update box size. */
286 LSMASH_SET_BE32( data, *data_length );
287 return data;
290 int lsmash_setup_dts_specific_parameters_from_frame( lsmash_dts_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
292 lsmash_bits_t bits = { 0 };
293 lsmash_bs_t bs = { 0 };
294 uint8_t buffer[DTS_MAX_EXSS_SIZE] = { 0 };
295 bs.buffer.data = buffer;
296 bs.buffer.store = data_length;
297 bs.buffer.alloc = DTS_MAX_EXSS_SIZE;
298 dts_info_t *info = &(dts_info_t){ .bits = &bits };
299 info->bits = &bits;
300 lsmash_bits_init( &bits, &bs );
301 memcpy( buffer, data, LSMASH_MIN( data_length, DTS_MAX_EXSS_SIZE ) );
302 dts_setup_parser( info );
303 uint64_t next_frame_pos = 0;
304 while( 1 )
306 int err;
307 /* Seek to the head of the next syncframe. */
308 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
309 /* Check the remainder length of the buffer.
310 * If there is enough length, then continue to parse the frame in it.
311 * The length 10 is the required byte length to get frame size. */
312 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
313 if( bs.eob || (bs.eof && remain_size < 10) )
314 goto setup_param; /* No more valid data. */
315 /* Parse substream frame. */
316 dts_substream_type prev_substream_type = info->substream_type;
317 info->substream_type = dts_get_substream_type( info );
318 int (*dts_parse_frame)( dts_info_t * ) = NULL;
319 switch( info->substream_type )
321 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
322 case DTS_SUBSTREAM_TYPE_CORE :
323 if( prev_substream_type != DTS_SUBSTREAM_TYPE_NONE )
324 goto setup_param;
325 dts_parse_frame = dts_parse_core_substream;
326 break;
327 case DTS_SUBSTREAM_TYPE_EXTENSION :
329 uint8_t prev_exss_index = info->exss_index;
330 if( (err = dts_get_exss_index( info, &info->exss_index )) < 0 )
331 return err;
332 if( prev_substream_type == DTS_SUBSTREAM_TYPE_EXTENSION && info->exss_index <= prev_exss_index )
333 goto setup_param;
334 dts_parse_frame = dts_parse_extension_substream;
335 break;
337 default :
338 /* An unknown stream type is detected. */
339 return LSMASH_ERR_NAMELESS;
341 info->frame_size = 0;
342 if( (err = dts_parse_frame( info )) < 0 )
343 return err; /* Failed to parse. */
344 next_frame_pos += info->frame_size;
346 setup_param:
347 dts_update_specific_param( info );
348 *param = info->ddts_param;
349 return 0;
352 static uint64_t dts_bits_get( lsmash_bits_t *bits, uint32_t width, uint64_t *bits_pos )
354 *bits_pos += width;
355 return lsmash_bits_get( bits, width );
358 static int dts_get_channel_count_from_channel_layout( uint16_t channel_layout )
360 #define DTS_CHANNEL_PAIR_MASK \
361 (DTS_CHANNEL_LAYOUT_L_R \
362 | DTS_CHANNEL_LAYOUT_LS_RS \
363 | DTS_CHANNEL_LAYOUT_LH_RH \
364 | DTS_CHANNEL_LAYOUT_LSR_RSR \
365 | DTS_CHANNEL_LAYOUT_LC_RC \
366 | DTS_CHANNEL_LAYOUT_LW_RW \
367 | DTS_CHANNEL_LAYOUT_LSS_RSS \
368 | DTS_CHANNEL_LAYOUT_LHS_RHS \
369 | DTS_CHANNEL_LAYOUT_LHR_RHR)
370 return lsmash_count_bits( channel_layout )
371 + lsmash_count_bits( channel_layout & DTS_CHANNEL_PAIR_MASK );
372 #undef DTS_CHANNEL_PAIR_MASK
375 static uint32_t dts_get_channel_layout_from_ls_mask32( uint32_t mask )
377 uint32_t layout = 0;
378 if( mask & DTS_LOUDSPEAKER_MASK32_C )
379 layout |= DTS_CHANNEL_LAYOUT_C;
380 if( mask & (DTS_LOUDSPEAKER_MASK32_L | DTS_LOUDSPEAKER_MASK32_R) )
381 layout |= DTS_CHANNEL_LAYOUT_L_R;
382 if( mask & (DTS_LOUDSPEAKER_MASK32_LS | DTS_LOUDSPEAKER_MASK32_RS) )
383 layout |= DTS_CHANNEL_LAYOUT_LS_RS;
384 if( mask & DTS_LOUDSPEAKER_MASK32_LFE1 )
385 layout |= DTS_CHANNEL_LAYOUT_LFE1;
386 if( mask & DTS_LOUDSPEAKER_MASK32_CS )
387 layout |= DTS_CHANNEL_LAYOUT_CS;
388 if( mask & (DTS_LOUDSPEAKER_MASK32_LH | DTS_LOUDSPEAKER_MASK32_RH) )
389 layout |= DTS_CHANNEL_LAYOUT_LH_RH;
390 if( mask & (DTS_LOUDSPEAKER_MASK32_LSR | DTS_LOUDSPEAKER_MASK32_RSR) )
391 layout |= DTS_CHANNEL_LAYOUT_LSR_RSR;
392 if( mask & DTS_LOUDSPEAKER_MASK32_CH )
393 layout |= DTS_CHANNEL_LAYOUT_CH;
394 if( mask & DTS_LOUDSPEAKER_MASK32_OH )
395 layout |= DTS_CHANNEL_LAYOUT_OH;
396 if( mask & (DTS_LOUDSPEAKER_MASK32_LC | DTS_LOUDSPEAKER_MASK32_RC) )
397 layout |= DTS_CHANNEL_LAYOUT_LC_RC;
398 if( mask & (DTS_LOUDSPEAKER_MASK32_LW | DTS_LOUDSPEAKER_MASK32_RW) )
399 layout |= DTS_CHANNEL_LAYOUT_LW_RW;
400 if( mask & (DTS_LOUDSPEAKER_MASK32_LSS | DTS_LOUDSPEAKER_MASK32_RSS) )
401 layout |= DTS_CHANNEL_LAYOUT_LSS_RSS;
402 if( mask & DTS_LOUDSPEAKER_MASK32_LFE2 )
403 layout |= DTS_CHANNEL_LAYOUT_LFE2;
404 if( mask & (DTS_LOUDSPEAKER_MASK32_LHS | DTS_LOUDSPEAKER_MASK32_RHS) )
405 layout |= DTS_CHANNEL_LAYOUT_LHS_RHS;
406 if( mask & DTS_LOUDSPEAKER_MASK32_CHR )
407 layout |= DTS_CHANNEL_LAYOUT_CHR;
408 if( mask & (DTS_LOUDSPEAKER_MASK32_LHR | DTS_LOUDSPEAKER_MASK32_RHR) )
409 layout |= DTS_CHANNEL_LAYOUT_LHR_RHR;
410 return layout;
413 /* for channels which cannot be expressed by ChannelLayout; CL, LL and RL */
414 static inline uint8_t dts_get_lower_channels_from_ls_mask32( uint32_t mask )
416 return (mask >> 25) & 0x7;
419 static void dts_parse_xll_navigation( lsmash_bits_t *bits, dts_xll_info_t *xll, int nuBits4ExSSFsize, uint64_t *bits_pos )
421 xll->size = dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ) + 1; /* nuExSSXLLFsize (nuBits4ExSSFsize) */
422 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSXLLSyncPresent (1) */
424 dts_bits_get( bits, 4, bits_pos ); /* nuPeakBRCntrlBuffSzkB (4) */
425 int nuBitsInitDecDly = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitsInitDecDly (5) */
426 dts_bits_get( bits, nuBitsInitDecDly, bits_pos ); /* nuInitLLDecDlyFrames (nuBitsInitDecDly) */
427 dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ); /* nuExSSXLLSyncOffset (nuBits4ExSSFsize) */
431 static void dts_parse_lbr_navigation( lsmash_bits_t *bits, dts_lbr_info_t *lbr, uint64_t *bits_pos )
433 lbr->size = dts_bits_get( bits, 14, bits_pos ); /* nuExSSLBRFsize (14) */
434 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSLBRSyncPresent (1) */
435 dts_bits_get( bits, 2, bits_pos ); /* nuExSSLBRSyncDistInFrames (2) */
438 static int dts_parse_asset_descriptor( dts_info_t *info, uint64_t *bits_pos )
440 lsmash_bits_t *bits = info->bits;
441 dts_extension_info_t *exss = &info->exss[ info->exss_index ];
442 /* Audio asset descriptor */
443 uint64_t asset_descriptor_pos = *bits_pos;
444 int nuAssetDescriptFsize = dts_bits_get( bits, 9, bits_pos ) + 1; /* nuAssetDescriptFsize (9) */
445 dts_audio_asset_t *asset = &exss->asset[ dts_bits_get( bits, 3, bits_pos ) ]; /* nuAssetIndex (3) */
446 /* Static metadata */
447 int bEmbeddedStereoFlag = 0;
448 int bEmbeddedSixChFlag = 0;
449 int nuTotalNumChs = 0;
450 if( exss->bStaticFieldsPresent )
452 if( dts_bits_get( bits, 1, bits_pos ) ) /* bAssetTypeDescrPresent (1)*/
453 dts_bits_get( bits, 4, bits_pos ); /* nuAssetTypeDescriptor (4) */
454 if( dts_bits_get( bits, 1, bits_pos ) ) /* bLanguageDescrPresent (1) */
455 dts_bits_get( bits, 24, bits_pos ); /* LanguageDescriptor (24) */
456 if( dts_bits_get( bits, 1, bits_pos ) )
458 int nuInfoTextByteSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nuInfoTextByteSize (10) */
459 dts_bits_get( bits, nuInfoTextByteSize * 8, bits_pos ); /* InfoTextString (nuInfoTextByteSize) */
461 int nuBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitResolution (5) */
462 exss->bit_resolution = LSMASH_MAX( exss->bit_resolution, nuBitResolution );
463 int nuMaxSampleRate = dts_bits_get( bits, 4, bits_pos ); /* nuMaxSampleRate (4) */
464 static const uint32_t source_sample_rate_table[16] =
466 8000, 16000, 32000, 64000, 128000,
467 22050, 44100, 88200, 176400, 352800,
468 12000, 24000, 48000, 96000, 192000, 384000
470 exss->sampling_frequency = LSMASH_MAX( exss->sampling_frequency, source_sample_rate_table[nuMaxSampleRate] );
471 nuTotalNumChs = dts_bits_get( bits, 8, bits_pos ) + 1; /* nuTotalNumChs (8) */
472 asset->bOne2OneMapChannels2Speakers = dts_bits_get( bits, 1, bits_pos ); /* bOne2OneMapChannels2Speakers (1) */
473 if( asset->bOne2OneMapChannels2Speakers )
475 if( nuTotalNumChs > 2 )
477 bEmbeddedStereoFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedStereoFlag (1) */
478 exss->stereo_downmix |= bEmbeddedStereoFlag;
480 if( nuTotalNumChs > 6 )
481 bEmbeddedSixChFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedSixChFlag (1) */
482 int nuNumBits4SAMask;
483 if( dts_bits_get( bits, 1, bits_pos ) ) /* bSpkrMaskEnabled (1) */
485 nuNumBits4SAMask = (dts_bits_get( bits, 2, bits_pos ) + 1) << 2; /* nuNumBits4SAMask (2) */
486 asset->channel_layout |= dts_bits_get( bits, nuNumBits4SAMask, bits_pos ); /* nuSpkrActivityMask (nuNumBits4SAMask) */
488 else
489 /* The specification doesn't mention the value of nuNumBits4SAMask if bSpkrMaskEnabled is set to 0. */
490 nuNumBits4SAMask = 16;
491 int nuNumSpkrRemapSets = dts_bits_get( bits, 3, bits_pos );
492 int nuStndrSpkrLayoutMask[8] = { 0 };
493 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
494 nuStndrSpkrLayoutMask[ns] = dts_bits_get( bits, nuNumBits4SAMask, bits_pos );
495 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
497 int nuNumSpeakers = dts_get_channel_count_from_channel_layout( nuStndrSpkrLayoutMask[ns] );
498 int nuNumDecCh4Remap = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuNumDecCh4Remap[ns] (5) */
499 for( int nCh = 0; nCh < nuNumSpeakers; nCh++ )
501 uint32_t nuRemapDecChMask = dts_bits_get( bits, nuNumDecCh4Remap, bits_pos );
502 int nCoef = lsmash_count_bits( nuRemapDecChMask );
503 for( int nc = 0; nc < nCoef; nc++ )
504 dts_bits_get( bits, 5, bits_pos ); /* nuSpkrRemapCodes[ns][nCh][nc] (5) */
508 else
510 asset->nuRepresentationType = dts_bits_get( bits, 3, bits_pos ); /* nuRepresentationType (3) */
511 if( asset->nuRepresentationType == 2
512 || asset->nuRepresentationType == 3 )
513 nuTotalNumChs = 2;
516 /* Dynamic metadata */
517 int bDRCCoefPresent = dts_bits_get( bits, 1, bits_pos ); /* bDRCCoefPresent (1) */
518 if( bDRCCoefPresent )
519 dts_bits_get( bits, 8, bits_pos ); /* nuDRCCode (8) */
520 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDialNormPresent (1) */
521 dts_bits_get( bits, 5, bits_pos ); /* nuDialNormCode (5) */
522 if( bDRCCoefPresent && bEmbeddedStereoFlag )
523 dts_bits_get( bits, 8, bits_pos ); /* nuDRC2ChDmixCode (8) */
524 int bMixMetadataPresent;
525 if( exss->bMixMetadataEnbl )
526 bMixMetadataPresent = dts_bits_get( bits, 1, bits_pos ); /* bMixMetadataPresent (1) */
527 else
528 bMixMetadataPresent = 0;
529 if( bMixMetadataPresent )
531 dts_bits_get( bits, 7, bits_pos ); /* bExternalMixFlag (1)
532 * nuPostMixGainAdjCode (7) */
533 if( dts_bits_get( bits, 2, bits_pos ) < 3 ) /* nuControlMixerDRC (2) */
534 dts_bits_get( bits, 3, bits_pos ); /* nuLimit4EmbeddedDRC (3) */
535 else
536 dts_bits_get( bits, 8, bits_pos ); /* nuCustomDRCCode (8) */
537 int bEnblPerChMainAudioScale = dts_bits_get( bits, 1, bits_pos ); /* bEnblPerChMainAudioScale (1) */
538 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
539 if( bEnblPerChMainAudioScale )
540 for( uint8_t nCh = 0; nCh < exss->nNumMixOutCh[ns]; nCh++ )
541 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][nCh] (6) */
542 else
543 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][0] (6) */
544 int nEmDM = 1;
545 int nDecCh[3] = { nuTotalNumChs, 0, 0 };
546 if( bEmbeddedSixChFlag )
548 nDecCh[nEmDM] = 6;
549 ++nEmDM;
551 if( bEmbeddedStereoFlag )
553 nDecCh[nEmDM] = 2;
554 ++nEmDM;
556 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
557 for( int nE = 0; nE < nEmDM; nE++ )
558 for( int nCh = 0; nCh < nDecCh[nE]; nCh++ )
560 int nuMixMapMask = dts_bits_get( bits, exss->nNumMixOutCh[ns], bits_pos ); /* nuMixMapMask (nNumMixOutCh[ns]) */
561 int nuNumMixCoefs = lsmash_count_bits( nuMixMapMask );
562 for( int nC = 0; nC < nuNumMixCoefs; nC++ )
563 dts_bits_get( bits, 6, bits_pos ); /* nuMixCoeffs[ns][nE][nCh][nC] (6) */
566 /* Decoder navigation data */
567 asset->nuCodingMode = dts_bits_get( bits, 2, bits_pos ); /* nuCodingMode (2) */
568 switch( asset->nuCodingMode )
570 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
572 int nuCoreExtensionMask = dts_bits_get( bits, 12, bits_pos ); /* nuCoreExtensionMask (12) */
573 asset->nuCoreExtensionMask = nuCoreExtensionMask;
574 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
576 asset->core.frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSCoreFsize (14) */
577 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSCoreSyncPresent (1) */
578 dts_bits_get( bits, 2, bits_pos ); /* nuExSSCoreSyncDistInFrames (2) */
580 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
581 asset->xbr_size = dts_bits_get( bits, 14, bits_pos ) + 1;
582 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
583 asset->core.xxch.size = dts_bits_get( bits, 14, bits_pos ) + 1;
584 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
585 asset->x96_size = dts_bits_get( bits, 12, bits_pos ) + 1;
586 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
587 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
588 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
589 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
590 break;
592 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
593 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
594 break;
595 case 2 : /* DTS-HD Low bit-rate mode */
596 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
597 break;
598 case 3 : /* Auxiliary coding mode */
599 asset->aux_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSAuxFsize (14) */
600 break;
601 default :
602 assert( 0 );
603 break;
605 dts_bits_get( bits, nuAssetDescriptFsize * 8 - (*bits_pos - asset_descriptor_pos), bits_pos ); /* Skip remaining part of Audio asset descriptor. */
606 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
609 static int dts_parse_xxch( dts_info_t *info, uint64_t *bits_pos, dts_xxch_info_t *xxch )
611 lsmash_bits_t *bits = info->bits;
612 /* XXCH Frame Header */
613 uint64_t xxch_pos = *bits_pos - 32; /* SYNCXXCh (32) */
614 uint64_t nuHeaderSizeXXCh = dts_bits_get( bits, 6, bits_pos ) + 1; /* nuHeaderSizeXXCh (6) */
615 dts_bits_get( bits, 1, bits_pos ); /* bCRCPresent4ChSetHeaderXXCh (1) */
616 int nuBits4SpkrMaskXXCh = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBits4SpkrMaskXXCh (5) */
617 int nuNumChSetsInXXCh = dts_bits_get( bits, 2, bits_pos ) + 1; /* nuNumChSetsInXXCh (2) */
618 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
619 dts_bits_get( bits, 14, bits_pos ); /* pnuChSetFsizeXXCh[nChSet] - 1 (14) */
620 /* A 5.1 decoder uses this AMODE to configure its decoded outputs to C, L, R, Ls and Rs layout.
621 * On the other hand a 7.1 decoder ignores the AMODE information from the core stream and uses
622 * instead the nuCoreSpkrActivityMask (C, L, R, LFE1, Lss and Rss) and the nuXXChSpkrLayoutMask
623 * (Lsr and Rsr) from the XXCh stream to get the original 7.1 speaker layout (C, L, R, LFE1, Lss,
624 * Rsr, Lsr and Rsr) and configures its outputs accordingly. */
625 uint32_t xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos ); /* nuCoreSpkrActivityMask (nuBits4SpkrMaskXXCh) */
626 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
627 xxch->lower_planes = dts_get_lower_channels_from_ls_mask32( xxch_mask );
628 dts_bits_get( bits, nuHeaderSizeXXCh * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Frame Header. */
629 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
631 /* XXCH Channel Set Header */
632 xxch_pos = *bits_pos;
633 uint64_t nuXXChChSetHeaderSize = dts_bits_get( bits, 7, bits_pos ) + 1; /* nuXXChChSetHeaderSize (7)*/
634 dts_bits_get( bits, 3, bits_pos ); /* nuChInChSetXXCh (3) */
635 if( nuBits4SpkrMaskXXCh > 6 )
637 xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh - 6, bits_pos ) << 6; /* nuXXChSpkrLayoutMask (nuBits4SpkrMaskXXCh - 6) */
638 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
639 xxch->lower_planes |= dts_get_lower_channels_from_ls_mask32( xxch_mask );
641 #if 0 /* FIXME: Can we detect stereo downmixing from only XXCH data within the core substream? */
642 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDownMixCoeffCodeEmbedded (1) */
644 int bDownMixEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownMixEmbedded (1) */
645 dts_bits_get( bits, 6, bits_pos ); /* nDmixScaleFactor (6) */
646 uint32_t DownMixChMapMask[8];
647 for( int nCh = 0; nCh < nuChInChSetXXCh; nCh++ )
648 DownMixChMapMask[nCh] = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos );
650 #endif
651 dts_bits_get( bits, nuXXChChSetHeaderSize * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Channel Set Header. */
653 return 0;
656 static int dts_parse_core_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
658 if( core->extension_audio_descriptor == 0
659 || core->extension_audio_descriptor == 3 )
660 return LSMASH_ERR_INVALID_DATA;
661 int err = dts_parse_xxch( info, bits_pos, &core->xxch );
662 if( err < 0 )
663 return err;
664 info->flags |= DTS_CORE_SUBSTREAM_XXCH_FLAG;
665 return info->bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
668 static int dts_parse_exss_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
670 lsmash_bits_t *bits = info->bits;
671 if( DTS_SYNCWORD_XXCH != dts_bits_get( bits, 32, bits_pos ) )
672 return LSMASH_ERR_INVALID_DATA;
673 int err = dts_parse_xxch( info, bits_pos, &core->xxch );
674 if( err < 0 )
675 return err;
676 info->flags |= DTS_EXT_SUBSTREAM_XXCH_FLAG;
677 return bits->bs->error ? LSMASH_ERR_NAMELESS : 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 ? LSMASH_ERR_NAMELESS : 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 ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA;
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 ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA;
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 ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA;
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 LSMASH_ERR_NAMELESS; /* unknown */
803 info->flags |= DTS_EXT_SUBSTREAM_LBR_FLAG;
804 return bits->bs->error ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA;
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 ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA; /* 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 LSMASH_ERR_INVALID_DATA;
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 LSMASH_ERR_INVALID_DATA; /* 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 LSMASH_ERR_INVALID_DATA;
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 LSMASH_ERR_INVALID_DATA; /* 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 LSMASH_ERR_INVALID_DATA; /* 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 LSMASH_ERR_INVALID_DATA; /* 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 int err;
1051 syncword = ((syncword << 8) & 0xffffff00) | dts_bits_get( bits, 8, bits_pos );
1052 switch( syncword )
1054 case DTS_SYNCWORD_XXCH :
1055 if( (err = dts_parse_core_xxch( info, bits_pos, core )) < 0 )
1056 return err;
1057 syncword = dts_bits_get( bits, 24, bits_pos );
1058 break;
1059 case DTS_SYNCWORD_X96K :
1060 if( (err = dts_parse_core_x96( info, bits_pos, core )) < 0 )
1061 return err;
1062 syncword = dts_bits_get( bits, 24, bits_pos );
1063 break;
1064 case DTS_SYNCWORD_XCH :
1065 if( (err = dts_parse_core_xch( info, bits_pos, core )) < 0 )
1066 return err;
1067 break;
1068 default :
1069 continue;
1073 return bits->bs->error ? LSMASH_ERR_NAMELESS : 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 LSMASH_ERR_INVALID_DATA;
1081 int err = dts_parse_core( info, bits_pos, &asset->core );
1082 if( err < 0 )
1083 return err;
1084 info->flags |= DTS_EXT_SUBSTREAM_CORE_FLAG;
1085 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1088 int dts_parse_core_substream( dts_info_t *info )
1090 lsmash_bits_t *bits = info->bits;
1091 uint64_t bits_pos = 0;
1092 int err;
1093 if( DTS_SYNCWORD_CORE != dts_bits_get( bits, 32, &bits_pos ) )
1095 err = LSMASH_ERR_INVALID_DATA;
1096 goto parse_fail;
1098 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0. */
1099 dts_extension_info_t *exss = &info->exss[0];
1100 if( (err = dts_parse_core( info, &bits_pos, &exss->asset[0].core )) < 0 )
1101 goto parse_fail;
1102 exss->bBcCorePresent [0] = 1;
1103 exss->nuBcCoreExtSSIndex[0] = 0;
1104 exss->nuBcCoreAssetIndex[0] = 0;
1105 info->flags |= DTS_CORE_SUBSTREAM_CORE_FLAG;
1106 info->exss_count = 0;
1107 info->core = exss->asset[0].core;
1108 info->frame_size = exss->asset[0].core.frame_size;
1109 lsmash_bits_get_align( bits );
1110 return 0;
1111 parse_fail:
1112 lsmash_bits_get_align( bits );
1113 return err;
1116 int dts_parse_extension_substream( dts_info_t *info )
1118 lsmash_bits_t *bits = info->bits;
1119 uint64_t bits_pos = 0;
1120 dts_bits_get( bits, 40, &bits_pos ); /* SYNCEXTSSH (32)
1121 * UserDefinedBits (8) */
1122 int nExtSSIndex = dts_bits_get( bits, 2, &bits_pos ); /* nExtSSIndex (2) */
1123 info->exss_index = nExtSSIndex;
1124 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1125 memset( exss, 0, sizeof(dts_extension_info_t) );
1126 int bHeaderSizeType = dts_bits_get( bits, 1, &bits_pos ); /* bHeaderSizeType (1) */
1127 int nuBits4Header = 8 + bHeaderSizeType * 4;
1128 int nuBits4ExSSFsize = 16 + bHeaderSizeType * 4;
1129 exss->nuBits4ExSSFsize = nuBits4ExSSFsize;
1130 uint32_t nuExtSSHeaderSize = dts_bits_get( bits, nuBits4Header, &bits_pos ) + 1; /* nuExtSSHeaderSize (8 or 12) */
1131 info->frame_size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuExtSSFsize (16 or 20) */
1132 if( info->frame_size < 10 )
1133 return LSMASH_ERR_INVALID_DATA;
1134 exss->bStaticFieldsPresent = dts_bits_get( bits, 1, &bits_pos ); /* bStaticFieldsPresent (1) */
1135 if( exss->bStaticFieldsPresent )
1137 dts_bits_get( bits, 2, &bits_pos ); /* nuRefClockCode (2) */
1138 exss->frame_duration = 512 * (dts_bits_get( bits, 3, &bits_pos ) + 1); /* nuExSSFrameDurationCode (3) */
1139 if( dts_bits_get( bits, 1, &bits_pos ) ) /* bTimeStampFlag (1) */
1140 dts_bits_get( bits, 36, &bits_pos ); /* nuTimeStamp (32)
1141 * nLSB (4) */
1142 exss->nuNumAudioPresnt = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAudioPresnt (3) */
1143 exss->nuNumAssets = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAssets (3) */
1144 /* The extension substreams with indexes lower than or equal to the index of the current extension substream can
1145 * be activated in the audio presentations indicated within the current extension substream. */
1146 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1147 exss->nuActiveExSSMask[nAuPr]
1148 = dts_bits_get( bits, nExtSSIndex + 1, &bits_pos ); /* nuActiveExSSMask[nAuPr] (nExtSSIndex + 1) */
1149 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1150 for( uint8_t nSS = 0; nSS <= nExtSSIndex; nSS++ )
1151 exss->nuActiveAssetMask[nAuPr][nSS]
1152 = ((exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1)
1153 ? dts_bits_get( bits, 8, &bits_pos ) /* nuActiveAssetMask[nAuPr][nSS] (8) */
1154 : 0;
1155 exss->bMixMetadataEnbl = dts_bits_get( bits, 1, &bits_pos ); /* bMixMetadataEnbl (1) */
1156 if( exss->bMixMetadataEnbl )
1158 dts_bits_get( bits, 2, &bits_pos ); /* nuMixMetadataAdjLevel (2) */
1159 int nuBits4MixOutMask = (dts_bits_get( bits, 2, &bits_pos ) + 1) << 2; /* nuBits4MixOutMask (2) */
1160 exss->nuNumMixOutConfigs = dts_bits_get( bits, 2, &bits_pos ) + 1; /* nuNumMixOutConfigs (2) */
1161 for( int ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
1163 int nuMixOutChMask = dts_bits_get( bits, nuBits4MixOutMask, &bits_pos ); /* nuMixOutChMask[ns] (nuBits4MixOutMask) */
1164 exss->nNumMixOutCh[ns] = dts_get_channel_count_from_channel_layout( nuMixOutChMask );
1168 else
1170 exss->nuNumAudioPresnt = 1;
1171 exss->nuNumAssets = 1;
1172 exss->bMixMetadataEnbl = 0;
1173 exss->nuNumMixOutConfigs = 0;
1175 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1176 exss->asset[nAst].size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuAssetFsize[nAst] - 1 (nuBits4ExSSFsize) */
1177 int err;
1178 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1179 if( (err = dts_parse_asset_descriptor( info, &bits_pos )) < 0 )
1180 goto parse_fail;
1181 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1182 exss->bBcCorePresent[nAuPr] = dts_bits_get( bits, 1, &bits_pos );
1183 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1184 if( exss->bBcCorePresent[nAuPr] )
1186 exss->nuBcCoreExtSSIndex[nAuPr] = dts_bits_get( bits, 2, &bits_pos );
1187 exss->nuBcCoreAssetIndex[nAuPr] = dts_bits_get( bits, 3, &bits_pos );
1189 dts_bits_get( bits, nuExtSSHeaderSize * 8 - bits_pos, &bits_pos );
1190 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1192 /* Asset Data */
1193 dts_audio_asset_t *asset = &exss->asset[nAst];
1194 uint32_t asset_pos = bits_pos;
1195 switch( asset->nuCodingMode )
1197 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
1199 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
1201 /* Core component */
1202 uint64_t core_pos = bits_pos;
1203 if( (err = dts_parse_exss_core( info, &bits_pos, asset )) < 0 )
1204 goto parse_fail;
1205 dts_bits_get( bits, asset->core.frame_size * 8 - (bits_pos - core_pos), &bits_pos );
1207 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
1209 /* XBR extension */
1210 uint64_t xbr_pos = bits_pos;
1211 if( (err = dts_parse_exss_xbr( info, &bits_pos )) < 0 )
1212 goto parse_fail;
1213 dts_bits_get( bits, asset->xbr_size * 8 - (bits_pos - xbr_pos), &bits_pos );
1215 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1217 /* XXCH extension */
1218 uint64_t xxch_pos = bits_pos;
1219 if( (err = dts_parse_exss_xxch( info, &bits_pos, &asset->core )) < 0 )
1220 goto parse_fail;
1221 dts_bits_get( bits, asset->core.xxch.size * 8 - (bits_pos - xxch_pos), &bits_pos );
1223 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
1225 /* X96 extension */
1226 uint64_t x96_pos = bits_pos;
1227 if( (err = dts_parse_exss_x96( info, &bits_pos, &asset->core )) < 0 )
1228 goto parse_fail;
1229 dts_bits_get( bits, asset->x96_size * 8 - (bits_pos - x96_pos), &bits_pos );
1231 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
1233 /* LBR component */
1234 uint64_t lbr_pos = bits_pos;
1235 if( (err = dts_parse_exss_lbr( info, &bits_pos, asset )) < 0 )
1236 goto parse_fail;
1237 dts_bits_get( bits, asset->lbr.size * 8 - (bits_pos - lbr_pos), &bits_pos );
1239 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
1241 /* Lossless extension */
1242 uint64_t xll_pos = bits_pos;
1243 if( (err = dts_parse_exss_xll( info, &bits_pos, asset )) < 0 )
1244 goto parse_fail;
1245 dts_bits_get( bits, asset->xll.size * 8 - (bits_pos - xll_pos), &bits_pos );
1247 break;
1249 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
1250 if( (err = dts_parse_exss_xll( info, &bits_pos, asset )) < 0 )
1251 goto parse_fail;
1252 break;
1253 case 2 : /* DTS-HD Low bit-rate mode */
1254 if( (err = dts_parse_exss_lbr( info, &bits_pos, asset )) < 0 )
1255 goto parse_fail;
1256 break;
1257 case 3 : /* Auxiliary coding mode */
1258 dts_bits_get( bits, asset->aux_size * 8, &bits_pos );
1259 break;
1261 dts_bits_get( bits, asset->size * 8 - (bits_pos - asset_pos), &bits_pos );
1263 dts_bits_get( bits, info->frame_size * 8 - bits_pos, &bits_pos );
1264 lsmash_bits_get_align( bits );
1265 if( info->exss_count < DTS_MAX_NUM_EXSS )
1266 info->exss_count += 1;
1267 return 0;
1268 parse_fail:
1269 lsmash_bits_get_align( bits );
1270 return err;
1273 dts_substream_type dts_get_substream_type( dts_info_t *info )
1275 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 4 )
1276 return DTS_SUBSTREAM_TYPE_NONE;
1277 uint8_t *buffer = lsmash_bs_get_buffer_data( info->bits->bs );
1278 uint32_t syncword = LSMASH_4CC( buffer[0], buffer[1], buffer[2], buffer[3] );
1279 switch( syncword )
1281 case DTS_SYNCWORD_CORE :
1282 return DTS_SUBSTREAM_TYPE_CORE;
1283 case DTS_SYNCWORD_SUBSTREAM :
1284 return DTS_SUBSTREAM_TYPE_EXTENSION;
1285 default :
1286 return DTS_SUBSTREAM_TYPE_NONE;
1290 int dts_get_exss_index( dts_info_t *info, uint8_t *exss_index )
1292 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 6 )
1293 return LSMASH_ERR_INVALID_DATA;
1294 *exss_index = lsmash_bs_show_byte( info->bits->bs, 5 ) >> 6;
1295 return 0;
1298 int dts_get_max_channel_count( dts_info_t *info )
1300 int max_channel_count = 0;
1301 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1303 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1304 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1306 /* Get the channel layout of an audio presentation from a core component. */
1307 uint16_t channel_layout = 0;
1308 int channel_count = 0;
1309 if( exss->bBcCorePresent [nAuPr]
1310 && exss->nuBcCoreAssetIndex[nAuPr] < exss->nuNumAssets )
1312 dts_core_info_t *core = &info->exss[ exss->nuBcCoreExtSSIndex[nAuPr] ].asset[ exss->nuBcCoreAssetIndex[nAuPr] ].core;
1313 if( core->xxch.channel_layout | core->xxch.lower_planes )
1315 channel_layout = core->xxch.channel_layout;
1316 channel_count = lsmash_count_bits( core->xxch.lower_planes ); /* FIXME: Should we count these channels? */
1318 else
1319 channel_layout = core->channel_layout;
1321 channel_count += dts_get_channel_count_from_channel_layout( channel_layout );
1322 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1323 /* Get the channel layouts of an audio presentation from extension substreams. */
1324 uint16_t ext_channel_layout = 0;
1325 uint16_t lbr_channel_layout = 0;
1326 uint16_t xll_channel_layout = 0;
1327 uint8_t xll_lower_channels = 0;
1328 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1329 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1330 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1331 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1333 dts_audio_asset_t *asset = &exss->asset[nAst];
1334 ext_channel_layout |= asset->channel_layout;
1335 lbr_channel_layout |= asset->lbr.channel_layout;
1336 xll_channel_layout |= asset->xll.channel_layout;
1337 xll_lower_channels |= asset->xll.lower_planes;
1339 /* Audio asset descriptors */
1340 channel_count = dts_get_channel_count_from_channel_layout( ext_channel_layout );
1341 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1342 /* LBR components */
1343 channel_count = dts_get_channel_count_from_channel_layout( lbr_channel_layout );
1344 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1345 /* Lossless extensions */
1346 channel_count = dts_get_channel_count_from_channel_layout( xll_channel_layout )
1347 + lsmash_count_bits( xll_lower_channels );
1348 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1351 return max_channel_count;
1354 void dts_update_specific_param( dts_info_t *info )
1356 lsmash_dts_specific_parameters_t *param = &info->ddts_param;
1357 int exss_index_start = 0;
1358 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1360 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1361 if( exss->nuNumAudioPresnt && exss->nuNumAssets )
1363 exss_index_start = nExtSSIndex;
1364 break;
1367 /* DTSSamplingFrequency and FrameDuration */
1368 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1370 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1371 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1372 continue;
1373 if( param->DTSSamplingFrequency <= exss->sampling_frequency )
1375 param->DTSSamplingFrequency = exss->sampling_frequency;
1376 info->frame_duration = exss->frame_duration;
1378 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1380 dts_audio_asset_t *asset = &exss->asset[nAst];
1381 if( param->DTSSamplingFrequency <= asset->core.sampling_frequency )
1383 param->DTSSamplingFrequency = asset->core.sampling_frequency;
1384 info->frame_duration = asset->core.frame_duration;
1386 if( param->DTSSamplingFrequency <= asset->lbr.sampling_frequency )
1388 param->DTSSamplingFrequency = asset->lbr.sampling_frequency;
1389 info->frame_duration = asset->lbr.frame_duration;
1391 if( param->DTSSamplingFrequency <= asset->xll.sampling_frequency )
1393 param->DTSSamplingFrequency = asset->xll.sampling_frequency;
1394 info->frame_duration = asset->xll.frame_duration;
1398 param->FrameDuration = 0;
1399 for( uint32_t frame_duration = info->frame_duration >> 10; frame_duration; frame_duration >>= 1 )
1400 ++ param->FrameDuration;
1401 /* pcmSampleDepth */
1402 param->pcmSampleDepth = 0;
1403 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1405 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1406 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1407 continue;
1408 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, exss->bit_resolution );
1409 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1411 dts_audio_asset_t *asset = &exss->asset[nAst];
1412 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->core.pcm_resolution );
1413 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->lbr.sample_size );
1414 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->xll.pcm_resolution );
1417 param->pcmSampleDepth = param->pcmSampleDepth > 16 ? 24 : 16;
1418 /* StreamConstruction */
1419 param->StreamConstruction = lsmash_dts_get_stream_construction( info->flags );
1420 /* CoreLFEPresent */
1421 param->CoreLFEPresent = !!(info->core.channel_layout & DTS_CHANNEL_LAYOUT_LFE1);
1422 /* CoreLayout */
1423 if( param->StreamConstruction == 0 /* Unknown */
1424 || param->StreamConstruction >= 17 /* No core substream */ )
1425 /* Use ChannelLayout. */
1426 param->CoreLayout = 31;
1427 else
1429 if( info->core.channel_arrangement != 1
1430 && info->core.channel_arrangement != 3
1431 && info->core.channel_arrangement <= 9 )
1432 param->CoreLayout = info->core.channel_arrangement;
1433 else
1434 /* Use ChannelLayout. */
1435 param->CoreLayout = 31;
1437 /* CoreSize
1438 * The specification says this field is the size of a core substream AU in bytes.
1439 * If we don't assume CoreSize is the copy of FSIZE, when FSIZE equals 0x3FFF, this field overflows and becomes 0. */
1440 param->CoreSize = info->core.frame_size ? LSMASH_MIN( info->core.frame_size - 1, 0x3FFF ) : 0;
1441 /* StereoDownmix */
1442 param->StereoDownmix = 0;
1443 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1445 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1446 param->StereoDownmix |= exss->stereo_downmix;
1447 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1449 param->StereoDownmix |= exss->asset[nAst].lbr.stereo_downmix;
1450 param->StereoDownmix |= exss->asset[nAst].xll.stereo_downmix;
1453 /* RepresentationType
1454 * Available only when core substream is absent and ChannelLayout is set to 0. */
1455 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1457 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1458 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1459 continue;
1460 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1462 int asset_count = 0;
1463 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1464 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1465 asset_count += lsmash_count_bits( exss->nuActiveAssetMask[nAuPr][nSS] );
1466 if( asset_count > 1 )
1468 /* An audio presentation has mulple audio assets.
1469 * Audio asset designated for mixing with another audio asset. */
1470 param->RepresentationType = 0;
1471 nExtSSIndex = DTS_MAX_NUM_EXSS;
1472 break;
1474 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1475 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1476 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1477 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1479 dts_audio_asset_t *asset = &exss->asset[nAst];
1480 if( asset->nuRepresentationType == info->exss[exss_index_start].asset[0].nuRepresentationType )
1481 param->RepresentationType = asset->nuRepresentationType;
1482 else
1484 /* Detected different representation types. Use ChannelLayout. */
1485 param->RepresentationType = 0;
1486 nAuPr = exss->nuNumAudioPresnt;
1487 nExtSSIndex = DTS_MAX_NUM_EXSS;
1488 break;
1493 /* ChannelLayout
1494 * complete information on channels coded in the audio stream including core and extensions */
1495 param->ChannelLayout = 0;
1496 if( param->RepresentationType == 0 )
1497 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1499 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1500 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1501 continue;
1502 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1504 dts_audio_asset_t *asset = &exss->asset[nAst];
1505 param->ChannelLayout |= asset->channel_layout;
1506 param->ChannelLayout |= asset->core.channel_layout;
1507 param->ChannelLayout |= asset->core.xxch.channel_layout;
1508 param->ChannelLayout |= asset->lbr.channel_layout;
1509 param->ChannelLayout |= asset->xll.channel_layout;
1512 /* MultiAssetFlag
1513 * When multiple assets exist, the remaining parameters in the DTSSpecificBox only reflect the coding parameters of the first asset. */
1514 param->MultiAssetFlag = ((info->exss[0].nuNumAssets
1515 + info->exss[1].nuNumAssets
1516 + info->exss[2].nuNumAssets
1517 + info->exss[3].nuNumAssets) > 1);
1518 /* LBRDurationMod */
1519 param->LBRDurationMod = info->exss[exss_index_start].asset[0].lbr.duration_modifier;
1520 info->ddts_param_initialized = 1;
1523 int dts_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1525 assert( dst && dst->data.structured && src && src->data.unstructured );
1526 if( src->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1527 return LSMASH_ERR_INVALID_DATA;
1528 lsmash_dts_specific_parameters_t *param = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1529 uint8_t *data = src->data.unstructured;
1530 uint64_t size = LSMASH_GET_BE32( data );
1531 int dts_specific_box_min_length = DTS_SPECIFIC_BOX_MIN_LENGTH;
1532 data += ISOM_BASEBOX_COMMON_SIZE;
1533 if( size == 1 )
1535 size = LSMASH_GET_BE64( data );
1536 dts_specific_box_min_length += 8;
1537 data += 8;
1539 if( size != src->size )
1540 return LSMASH_ERR_INVALID_DATA;
1541 param->DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1542 param->maxBitrate = LSMASH_GET_BE32( &data[4] );
1543 param->avgBitrate = LSMASH_GET_BE32( &data[8] );
1544 param->pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1545 param->FrameDuration = (data[13] >> 6) & 0x03;
1546 param->StreamConstruction = (data[13] >> 1) & 0x1F;
1547 param->CoreLFEPresent = data[13] & 0x01;
1548 param->CoreLayout = (data[14] >> 2) & 0x3F;
1549 param->CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1550 param->StereoDownmix = (data[16] >> 3) & 0x01;
1551 param->RepresentationType = data[16] & 0x07;
1552 param->ChannelLayout = (data[17] << 8) | data[18];
1553 param->MultiAssetFlag = (data[19] >> 7) & 0x01;
1554 param->LBRDurationMod = (data[19] >> 6) & 0x01;
1555 int reserved_box_present = ((data[19] >> 5) & 0x01) && (size > DTS_SPECIFIC_BOX_MIN_LENGTH);
1556 if( reserved_box_present )
1557 lsmash_append_dts_reserved_box( param, data + 20, size - DTS_SPECIFIC_BOX_MIN_LENGTH );
1558 return 0;
1561 int dts_copy_codec_specific( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1563 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
1564 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
1565 lsmash_dts_specific_parameters_t *src_data = (lsmash_dts_specific_parameters_t *)src->data.structured;
1566 lsmash_dts_specific_parameters_t *dst_data = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1567 lsmash_remove_dts_reserved_box( dst_data );
1568 *dst_data = *src_data;
1569 if( !src_data->box || !src_data->box->data || src_data->box->size == 0 )
1571 lsmash_remove_dts_reserved_box( dst_data );
1572 return 0;
1574 return lsmash_append_dts_reserved_box( dst_data, src_data->box->data, src_data->box->size );
1577 int dts_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
1579 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
1580 int indent = level;
1581 lsmash_ifprintf( fp, indent++, "[%s: DTS Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
1582 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
1583 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
1584 if( box->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1585 return LSMASH_ERR_INVALID_DATA;
1586 uint8_t *data = box->binary;
1587 isom_skip_box_common( &data );
1588 uint32_t DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1589 uint32_t maxBitrate = LSMASH_GET_BE32( &data[4] );
1590 uint32_t avgBitrate = LSMASH_GET_BE32( &data[8] );
1591 uint8_t pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1592 uint8_t FrameDuration = (data[13] >> 6) & 0x03;
1593 uint8_t StreamConstruction = (data[13] >> 1) & 0x1F;
1594 uint8_t CoreLFEPresent = data[13] & 0x01;
1595 uint8_t CoreLayout = (data[14] >> 2) & 0x3F;
1596 uint16_t CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1597 uint8_t StereoDownmix = (data[16] >> 3) & 0x01;
1598 uint8_t RepresentationType = data[16] & 0x07;
1599 uint16_t ChannelLayout = (data[17] << 8) | data[18];
1600 uint8_t MultiAssetFlag = (data[19] >> 7) & 0x01;
1601 uint8_t LBRDurationMod = (data[19] >> 6) & 0x01;
1602 uint8_t ReservedBoxPresent = (data[19] >> 5) & 0x01;
1603 uint8_t Reserved = data[19] & 0x1F;
1604 uint32_t frame_duration = 512 << FrameDuration;
1605 int construction_flags = StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? construction_info[StreamConstruction] : 0;
1606 static const char *core_layout_description[64] =
1608 "Mono (1/0)",
1609 "Undefined",
1610 "Stereo (2/0)",
1611 "Undefined",
1612 "LT,RT (2/0)",
1613 "L, C, R (3/0)",
1614 "L, R, S (2/1)",
1615 "L, C, R, S (3/1)",
1616 "L, R, LS, RS (2/2)",
1617 "L, C, R, LS, RS (3/2)",
1618 [31] = "use ChannelLayout"
1620 static const char *representation_type_description[8] =
1622 "Audio asset designated for mixing with another audio asset",
1623 "Reserved",
1624 "Lt/Rt Encoded for matrix surround decoding",
1625 "Audio processed for headphone playback",
1626 "Reserved",
1627 "Reserved",
1628 "Reserved",
1629 "Reserved"
1631 static const char *channel_layout_description[16] =
1633 "Center in front of listener",
1634 "Left/Right in front",
1635 "Left/Right surround on side in rear",
1636 "Low frequency effects subwoofer",
1637 "Center surround in rear",
1638 "Left/Right height in front",
1639 "Left/Right surround in rear",
1640 "Center Height in front",
1641 "Over the listener's head",
1642 "Between left/right and center in front",
1643 "Left/Right on side in front",
1644 "Left/Right surround on side",
1645 "Second low frequency effects subwoofer",
1646 "Left/Right height on side",
1647 "Center height in rear",
1648 "Left/Right height in rear"
1650 lsmash_ifprintf( fp, indent, "DTSSamplingFrequency = %"PRIu32" Hz\n", DTSSamplingFrequency );
1651 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32" bit/s\n", maxBitrate );
1652 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32" bit/s\n", avgBitrate );
1653 lsmash_ifprintf( fp, indent, "pcmSampleDepth = %"PRIu8" bits\n", pcmSampleDepth );
1654 lsmash_ifprintf( fp, indent, "FrameDuration = %"PRIu8" (%"PRIu32" samples)\n", FrameDuration, frame_duration );
1655 lsmash_ifprintf( fp, indent, "StreamConstruction = 0x%02"PRIx8"\n", StreamConstruction );
1656 if( construction_flags & (DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG) )
1658 lsmash_ifprintf( fp, indent + 1, "Core substream\n" );
1659 if( construction_flags & DTS_CORE_SUBSTREAM_CORE_FLAG )
1660 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1661 if( construction_flags & DTS_CORE_SUBSTREAM_XCH_FLAG )
1662 lsmash_ifprintf( fp, indent + 2, "XCH\n" );
1663 if( construction_flags & DTS_CORE_SUBSTREAM_X96_FLAG )
1664 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1665 if( construction_flags & DTS_CORE_SUBSTREAM_XXCH_FLAG )
1666 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1668 if( construction_flags & (DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG
1669 | DTS_EXT_SUBSTREAM_XBR_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG | DTS_EXT_SUBSTREAM_LBR_FLAG) )
1671 lsmash_ifprintf( fp, indent + 1, "Extension substream\n" );
1672 if( construction_flags & DTS_EXT_SUBSTREAM_CORE_FLAG )
1673 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1674 if( construction_flags & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1675 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1676 if( construction_flags & DTS_EXT_SUBSTREAM_X96_FLAG )
1677 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1678 if( construction_flags & DTS_EXT_SUBSTREAM_XBR_FLAG )
1679 lsmash_ifprintf( fp, indent + 2, "XBR\n" );
1680 if( construction_flags & DTS_EXT_SUBSTREAM_XLL_FLAG )
1681 lsmash_ifprintf( fp, indent + 2, "XLL\n" );
1682 if( construction_flags & DTS_EXT_SUBSTREAM_LBR_FLAG )
1683 lsmash_ifprintf( fp, indent + 2, "LBR\n" );
1685 lsmash_ifprintf( fp, indent, "CoreLFEPresent = %s\n", CoreLFEPresent ? "1 (LFE exists)" : "0 (no LFE)" );
1686 if( core_layout_description[CoreLayout] )
1687 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (%s)\n", CoreLayout, core_layout_description[CoreLayout] );
1688 else
1689 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (Undefined)\n", CoreLayout );
1690 if( CoreSize )
1691 lsmash_ifprintf( fp, indent, "CoreSize = %"PRIu16"\n", CoreSize );
1692 else
1693 lsmash_ifprintf( fp, indent, "CoreSize = 0 (no core substream exists)\n" );
1694 lsmash_ifprintf( fp, indent, "StereoDownmix = %s\n", StereoDownmix ? "1 (embedded downmix present)" : "0 (no embedded downmix)" );
1695 lsmash_ifprintf( fp, indent, "RepresentationType = %"PRIu8" (%s)\n", RepresentationType, representation_type_description[RepresentationType] );
1696 lsmash_ifprintf( fp, indent, "ChannelLayout = 0x%04"PRIx16"\n", ChannelLayout );
1697 if( ChannelLayout )
1698 for( int i = 0; i < 16; i++ )
1699 if( (ChannelLayout >> i) & 0x01 )
1700 lsmash_ifprintf( fp, indent + 1, "%s\n", channel_layout_description[i] );
1701 lsmash_ifprintf( fp, indent, "MultiAssetFlag = %s\n", MultiAssetFlag ? "1 (multiple asset)" : "0 (single asset)" );
1702 if( LBRDurationMod )
1703 lsmash_ifprintf( fp, indent, "LBRDurationMod = 1 (%"PRIu32" -> %"PRIu32" samples)\n", frame_duration, (frame_duration * 3) / 2 );
1704 else
1705 lsmash_ifprintf( fp, indent, "LBRDurationMod = 0 (no LBR duration modifier)\n" );
1706 lsmash_ifprintf( fp, indent, "ReservedBoxPresent = %s\n", ReservedBoxPresent ? "1 (ReservedBox present)" : "0 (no ReservedBox)" );
1707 lsmash_ifprintf( fp, indent, "Reserved = 0x%02"PRIx8"\n", Reserved );
1708 return 0;