list: Decide the entry eliminator of list at its initialization.
[L-SMASH.git] / codecs / dts.c
blobf0f8e0269492042ffe7b91bdfdb80b86c74fcba5
1 /*****************************************************************************
2 * dts.c
3 *****************************************************************************
4 * Copyright (C) 2012-2017 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
41 Common File Format & Media Formats Specification Version 2.2 31 July 2015
42 *****************************************************************************/
43 #include "dts.h"
45 #define DTS_MIN_CORE_SIZE 96
46 #define DTS_MAX_STREAM_CONSTRUCTION 21
47 #define DTS_SPECIFIC_BOX_MIN_LENGTH 28
49 typedef enum
51 DTS_SYNCWORD_CORE = 0x7FFE8001,
52 DTS_SYNCWORD_XCH = 0x5A5A5A5A,
53 DTS_SYNCWORD_XXCH = 0x47004A03,
54 DTS_SYNCWORD_X96K = 0x1D95F262,
55 DTS_SYNCWORD_XBR = 0x655E315E,
56 DTS_SYNCWORD_LBR = 0x0A801921,
57 DTS_SYNCWORD_XLL = 0x41A29547,
58 DTS_SYNCWORD_SUBSTREAM = 0x64582025,
59 DTS_SYNCWORD_SUBSTREAM_CORE = 0x02b09261,
60 DTS_SYNCWORD_X = 0x02000850,
61 } dts_syncword;
63 /* Loudspeaker Masks (up to 32-bit) for
64 * - nuCoreSpkrActivityMask
65 * - nuXXChSpkrLayoutMask
66 * - DownMixChMapMask
67 * - nChMask
68 * - nSpkrMask */
69 typedef enum
71 DTS_LOUDSPEAKER_MASK32_C = 0x00000001, /* Centre in front of listener */
72 DTS_LOUDSPEAKER_MASK32_L = 0x00000002, /* Left in front */
73 DTS_LOUDSPEAKER_MASK32_R = 0x00000004, /* Right in front */
74 DTS_LOUDSPEAKER_MASK32_LS = 0x00000008, /* Left surround on side in rear */
75 DTS_LOUDSPEAKER_MASK32_RS = 0x00000010, /* Right surround on side in rear */
76 DTS_LOUDSPEAKER_MASK32_LFE1 = 0x00000020, /* Low frequency effects subwoofer */
77 DTS_LOUDSPEAKER_MASK32_CS = 0x00000040, /* Centre surround in rear */
78 DTS_LOUDSPEAKER_MASK32_LSR = 0x00000080, /* Left surround in rear */
79 DTS_LOUDSPEAKER_MASK32_RSR = 0x00000100, /* Right surround in rear */
80 DTS_LOUDSPEAKER_MASK32_LSS = 0x00000200, /* Left surround on side */
81 DTS_LOUDSPEAKER_MASK32_RSS = 0x00000400, /* Right surround on side */
82 DTS_LOUDSPEAKER_MASK32_LC = 0x00000800, /* Between left and centre in front */
83 DTS_LOUDSPEAKER_MASK32_RC = 0x00001000, /* Between right and centre in front */
84 DTS_LOUDSPEAKER_MASK32_LH = 0x00002000, /* Left height in front */
85 DTS_LOUDSPEAKER_MASK32_CH = 0x00004000, /* Centre Height in front */
86 DTS_LOUDSPEAKER_MASK32_RH = 0x00008000, /* Right Height in front */
87 DTS_LOUDSPEAKER_MASK32_LFE2 = 0x00010000, /* Second low frequency effects subwoofer */
88 DTS_LOUDSPEAKER_MASK32_LW = 0x00020000, /* Left on side in front */
89 DTS_LOUDSPEAKER_MASK32_RW = 0x00040000, /* Right on side in front */
90 DTS_LOUDSPEAKER_MASK32_OH = 0x00080000, /* Over the listener's head */
91 DTS_LOUDSPEAKER_MASK32_LHS = 0x00100000, /* Left height on side */
92 DTS_LOUDSPEAKER_MASK32_RHS = 0x00200000, /* Right height on side */
93 DTS_LOUDSPEAKER_MASK32_CHR = 0x00400000, /* Centre height in rear */
94 DTS_LOUDSPEAKER_MASK32_LHR = 0x00800000, /* Left height in rear */
95 DTS_LOUDSPEAKER_MASK32_RHR = 0x01000000, /* Right height in rear */
96 DTS_LOUDSPEAKER_MASK32_CL = 0x02000000, /* Centre in the plane lower than listener's ears */
97 DTS_LOUDSPEAKER_MASK32_LL = 0x04000000, /* Left in the plane lower than listener's ears */
98 DTS_LOUDSPEAKER_MASK32_RL = 0x08000000, /* Right in the plane lower than listener's ears */
99 } dts_loudspeaker_mask;
101 /* Loudspeaker Masks (up to 16-bit) for
102 * - nuSpkrActivityMask
103 * - nuStndrSpkrLayoutMask
104 * - nuMixOutChMask
105 * - ChannelLayout of DTSSpecificBox */
106 typedef enum
108 DTS_CHANNEL_LAYOUT_C = 0x0001, /* Centre in front of listener */
109 DTS_CHANNEL_LAYOUT_L_R = 0x0002, /* Left/Right in front */
110 DTS_CHANNEL_LAYOUT_LS_RS = 0x0004, /* Left/Right surround on side in rear */
111 DTS_CHANNEL_LAYOUT_LFE1 = 0x0008, /* Low frequency effects subwoofer */
112 DTS_CHANNEL_LAYOUT_CS = 0x0010, /* Centre surround in rear */
113 DTS_CHANNEL_LAYOUT_LH_RH = 0x0020, /* Left/Right height in front */
114 DTS_CHANNEL_LAYOUT_LSR_RSR = 0x0040, /* Left/Right surround in rear */
115 DTS_CHANNEL_LAYOUT_CH = 0x0080, /* Centre height in front */
116 DTS_CHANNEL_LAYOUT_OH = 0x0100, /* Over the listener's head */
117 DTS_CHANNEL_LAYOUT_LC_RC = 0x0200, /* Between left/right and centre in front */
118 DTS_CHANNEL_LAYOUT_LW_RW = 0x0400, /* Left/Right on side in front */
119 DTS_CHANNEL_LAYOUT_LSS_RSS = 0x0800, /* Left/Right surround on side */
120 DTS_CHANNEL_LAYOUT_LFE2 = 0x1000, /* Second low frequency effects subwoofer */
121 DTS_CHANNEL_LAYOUT_LHS_RHS = 0x2000, /* Left/Right height on side */
122 DTS_CHANNEL_LAYOUT_CHR = 0x4000, /* Centre height in rear */
123 DTS_CHANNEL_LAYOUT_LHR_RHR = 0x8000, /* Left/Right height in rear */
124 } dts_channel_layout;
126 static const lsmash_dts_construction_flag construction_info[DTS_MAX_STREAM_CONSTRUCTION + 1] =
129 DTS_CORE_SUBSTREAM_CORE_FLAG,
130 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG,
131 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG,
132 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG,
133 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG,
134 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
135 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
136 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
137 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_XBR_FLAG,
138 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
139 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
140 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
141 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG,
142 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
143 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
144 DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG,
145 DTS_EXT_SUBSTREAM_XLL_FLAG,
146 DTS_EXT_SUBSTREAM_LBR_FLAG,
147 DTS_EXT_SUBSTREAM_CORE_FLAG,
148 DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG,
149 DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG ,
152 void dts_setup_parser( dts_info_t *info )
154 dts_extension_info_t *exss = &info->exss[0];
155 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0.
156 * Therefore, we can treat as if one extension substream is there even if no extension substreams. */
157 exss->nuNumAudioPresnt = 1;
158 exss->nuNumAssets = 1;
159 exss->bBcCorePresent [0] = 0;
160 exss->nuBcCoreExtSSIndex[0] = 0;
161 exss->nuBcCoreAssetIndex[0] = 0;
164 struct lsmash_dts_reserved_box_tag
166 uint32_t size;
167 uint8_t *data;
170 int lsmash_append_dts_reserved_box( lsmash_dts_specific_parameters_t *param, const uint8_t *box_data, uint32_t box_size )
172 if( !param || !box_data || box_size == 0 )
173 return LSMASH_ERR_FUNCTION_PARAM;
174 if( !param->box )
176 param->box = lsmash_malloc_zero( sizeof(lsmash_dts_reserved_box_t) );
177 if( !param->box )
178 return LSMASH_ERR_MEMORY_ALLOC;
180 /* New a DTSExpansionBox. */
181 uint32_t size = param->box->size + box_size;
182 uint8_t *data = lsmash_realloc( param->box->data, size );
183 if( !data )
184 return LSMASH_ERR_MEMORY_ALLOC;
185 memcpy( data + param->box->size, box_data, box_size );
186 param->box->data = data;
187 param->box->size = size;
188 return 0;
191 void lsmash_remove_dts_reserved_box( lsmash_dts_specific_parameters_t *param )
193 if( !param->box )
194 return;
195 lsmash_free( param->box->data );
196 lsmash_freep( &param->box );
199 void dts_destruct_specific_data( void *data )
201 if( !data )
202 return;
203 lsmash_remove_dts_reserved_box( data );
204 lsmash_free( data );
207 uint8_t lsmash_dts_get_stream_construction( lsmash_dts_construction_flag flags )
209 uint8_t StreamConstruction;
210 for( StreamConstruction = 1; StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION; StreamConstruction++ )
211 if( flags == construction_info[StreamConstruction] )
212 break;
213 /* For any stream type not listed in the above table,
214 * StreamConstruction shall be set to 0 and the codingname shall default to 'dtsh'. */
215 return StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? StreamConstruction : 0;
218 lsmash_dts_construction_flag lsmash_dts_get_construction_flags( uint8_t stream_construction )
220 if( stream_construction <= DTS_MAX_STREAM_CONSTRUCTION )
221 return construction_info[stream_construction];
222 return 0;
225 lsmash_codec_type_t lsmash_dts_get_codingname( lsmash_dts_specific_parameters_t *param )
227 assert( param->StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION );
228 if( param->MultiAssetFlag )
229 return ISOM_CODEC_TYPE_DTSH_AUDIO; /* Multiple asset streams shall use the 'dtsh' coding_name. */
230 static lsmash_codec_type_t codingname_table[DTS_MAX_STREAM_CONSTRUCTION + 1] = { LSMASH_CODEC_TYPE_INITIALIZER };
231 if( lsmash_check_codec_type_identical( codingname_table[0], LSMASH_CODEC_TYPE_UNSPECIFIED ) )
233 int i = 0;
234 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO; /* Undefined stream types shall be set to 0 and the codingname shall default to 'dtsh'. */
235 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
236 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
237 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
238 codingname_table[i++] = ISOM_CODEC_TYPE_DTSC_AUDIO;
239 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
240 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
241 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
242 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
243 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
244 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
245 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_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_DTSH_AUDIO;
249 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
250 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
251 codingname_table[i++] = ISOM_CODEC_TYPE_DTSL_AUDIO;
252 codingname_table[i++] = ISOM_CODEC_TYPE_DTSE_AUDIO;
253 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
254 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
255 codingname_table[i++] = ISOM_CODEC_TYPE_DTSH_AUDIO;
257 lsmash_codec_type_t codingname = codingname_table[ param->StreamConstruction ];
258 /* Check the presence of DTSXParameters Box. */
259 if( !lsmash_check_codec_type_identical( codingname, ISOM_CODEC_TYPE_DTSC_AUDIO )
260 && !lsmash_check_codec_type_identical( codingname, ISOM_CODEC_TYPE_DTSE_AUDIO )
261 && param->box
262 && param->box->data
263 && param->box->size >= ISOM_FULLBOX_COMMON_SIZE )
265 uint8_t *data = param->box->data;
266 uint32_t pos = 0;
267 while( pos + ISOM_FULLBOX_COMMON_SIZE <= param->box->size )
269 uint32_t size = LSMASH_GET_BE32( &data[0] );
270 uint32_t type = LSMASH_GET_BE32( &data[4] );
271 if( type == LSMASH_4CC( 'd', 'x', 'p', 'b' ) )
272 return ISOM_CODEC_TYPE_DTSX_AUDIO;
273 pos += size;
276 return codingname;
279 uint8_t *lsmash_create_dts_specific_info( lsmash_dts_specific_parameters_t *param, uint32_t *data_length )
281 int reserved_box_present = (param->box && param->box->data && param->box->size);
282 lsmash_bits_t *bits = lsmash_bits_adhoc_create();
283 if( !bits )
284 return NULL;
285 /* Create a DTSSpecificBox. */
286 lsmash_bits_put( bits, 32, 0 ); /* box size */
287 lsmash_bits_put( bits, 32, ISOM_BOX_TYPE_DDTS.fourcc ); /* box type: 'ddts' */
288 lsmash_bits_put( bits, 32, param->DTSSamplingFrequency );
289 lsmash_bits_put( bits, 32, param->maxBitrate ); /* maxBitrate; setup by isom_update_bitrate_description */
290 lsmash_bits_put( bits, 32, param->avgBitrate ); /* avgBitrate; setup by isom_update_bitrate_description */
291 lsmash_bits_put( bits, 8, param->pcmSampleDepth );
292 lsmash_bits_put( bits, 2, param->FrameDuration );
293 lsmash_bits_put( bits, 5, param->StreamConstruction );
294 lsmash_bits_put( bits, 1, param->CoreLFEPresent );
295 lsmash_bits_put( bits, 6, param->CoreLayout );
296 lsmash_bits_put( bits, 14, param->CoreSize );
297 lsmash_bits_put( bits, 1, param->StereoDownmix );
298 lsmash_bits_put( bits, 3, param->RepresentationType );
299 lsmash_bits_put( bits, 16, param->ChannelLayout );
300 lsmash_bits_put( bits, 1, param->MultiAssetFlag );
301 lsmash_bits_put( bits, 1, param->LBRDurationMod );
302 lsmash_bits_put( bits, 1, reserved_box_present );
303 lsmash_bits_put( bits, 5, 0 ); /* Reserved */
304 /* ReservedBox */
305 if( reserved_box_present )
306 for( uint32_t i = 0; i < param->box->size; i++ )
307 lsmash_bits_put( bits, 8, param->box->data[i] );
308 /* */
309 uint8_t *data = lsmash_bits_export_data( bits, data_length );
310 lsmash_bits_adhoc_cleanup( bits );
311 /* Update box size. */
312 LSMASH_SET_BE32( data, *data_length );
313 return data;
316 int lsmash_setup_dts_specific_parameters_from_frame( lsmash_dts_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
318 lsmash_bits_t bits = { 0 };
319 lsmash_bs_t bs = { 0 };
320 uint8_t buffer[DTS_MAX_EXSS_SIZE] = { 0 };
321 bs.buffer.data = buffer;
322 bs.buffer.store = data_length;
323 bs.buffer.alloc = DTS_MAX_EXSS_SIZE;
324 dts_info_t *info = &(dts_info_t){ .bits = &bits };
325 info->bits = &bits;
326 lsmash_bits_init( &bits, &bs );
327 memcpy( buffer, data, LSMASH_MIN( data_length, DTS_MAX_EXSS_SIZE ) );
328 dts_setup_parser( info );
329 uint64_t next_frame_pos = 0;
330 while( 1 )
332 int err;
333 /* Seek to the head of the next syncframe. */
334 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
335 /* Check the remainder length of the buffer.
336 * If there is enough length, then continue to parse the frame in it.
337 * The length 10 is the required byte length to get frame size. */
338 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
339 if( bs.eob || (bs.eof && remain_size < 10) )
340 goto setup_param; /* No more valid data. */
341 /* Parse substream frame. */
342 dts_substream_type prev_substream_type = info->substream_type;
343 info->substream_type = dts_get_substream_type( info );
344 int (*dts_parse_frame)( dts_info_t * ) = NULL;
345 switch( info->substream_type )
347 /* Decide substream frame parser and check if this frame and the previous frame belong to the same AU. */
348 case DTS_SUBSTREAM_TYPE_CORE :
349 if( prev_substream_type != DTS_SUBSTREAM_TYPE_NONE )
350 goto setup_param;
351 dts_parse_frame = dts_parse_core_substream;
352 break;
353 case DTS_SUBSTREAM_TYPE_EXTENSION :
355 uint8_t prev_exss_index = info->exss_index;
356 if( (err = dts_get_exss_index( info, &info->exss_index )) < 0 )
357 return err;
358 if( prev_substream_type == DTS_SUBSTREAM_TYPE_EXTENSION && info->exss_index <= prev_exss_index )
359 goto setup_param;
360 dts_parse_frame = dts_parse_extension_substream;
361 break;
363 default :
364 /* An unknown stream type is detected. */
365 return LSMASH_ERR_NAMELESS;
367 info->frame_size = 0;
368 if( (err = dts_parse_frame( info )) < 0 )
369 return err; /* Failed to parse. */
370 next_frame_pos += info->frame_size;
372 setup_param:
373 dts_update_specific_param( info );
374 *param = info->ddts_param;
375 return 0;
378 static inline uint64_t dts_bits_get( lsmash_bits_t *bits, uint32_t width, uint64_t *bits_pos )
380 *bits_pos += width;
381 return lsmash_bits_get( bits, width );
384 static inline void dts_bits_align( lsmash_bits_t *bits, uint64_t *bits_pos )
386 uint8_t remainder = 8 - (*bits_pos & 0x7);
387 (void)dts_bits_get( bits, remainder, bits_pos );
390 static inline void dts_bits_align4( lsmash_bits_t *bits, uint64_t *bits_pos )
392 uint8_t remainder = 32 - (*bits_pos & 0x1f);
393 (void)dts_bits_get( bits, remainder, bits_pos );
396 static int dts_get_channel_count_from_channel_layout( uint16_t channel_layout )
398 #define DTS_CHANNEL_PAIR_MASK \
399 (DTS_CHANNEL_LAYOUT_L_R \
400 | DTS_CHANNEL_LAYOUT_LS_RS \
401 | DTS_CHANNEL_LAYOUT_LH_RH \
402 | DTS_CHANNEL_LAYOUT_LSR_RSR \
403 | DTS_CHANNEL_LAYOUT_LC_RC \
404 | DTS_CHANNEL_LAYOUT_LW_RW \
405 | DTS_CHANNEL_LAYOUT_LSS_RSS \
406 | DTS_CHANNEL_LAYOUT_LHS_RHS \
407 | DTS_CHANNEL_LAYOUT_LHR_RHR)
408 return lsmash_count_bits( channel_layout )
409 + lsmash_count_bits( channel_layout & DTS_CHANNEL_PAIR_MASK );
410 #undef DTS_CHANNEL_PAIR_MASK
413 static uint32_t dts_get_channel_layout_from_ls_mask32( uint32_t mask )
415 uint32_t layout = 0;
416 if( mask & DTS_LOUDSPEAKER_MASK32_C )
417 layout |= DTS_CHANNEL_LAYOUT_C;
418 if( mask & (DTS_LOUDSPEAKER_MASK32_L | DTS_LOUDSPEAKER_MASK32_R) )
419 layout |= DTS_CHANNEL_LAYOUT_L_R;
420 if( mask & (DTS_LOUDSPEAKER_MASK32_LS | DTS_LOUDSPEAKER_MASK32_RS) )
421 layout |= DTS_CHANNEL_LAYOUT_LS_RS;
422 if( mask & DTS_LOUDSPEAKER_MASK32_LFE1 )
423 layout |= DTS_CHANNEL_LAYOUT_LFE1;
424 if( mask & DTS_LOUDSPEAKER_MASK32_CS )
425 layout |= DTS_CHANNEL_LAYOUT_CS;
426 if( mask & (DTS_LOUDSPEAKER_MASK32_LH | DTS_LOUDSPEAKER_MASK32_RH) )
427 layout |= DTS_CHANNEL_LAYOUT_LH_RH;
428 if( mask & (DTS_LOUDSPEAKER_MASK32_LSR | DTS_LOUDSPEAKER_MASK32_RSR) )
429 layout |= DTS_CHANNEL_LAYOUT_LSR_RSR;
430 if( mask & DTS_LOUDSPEAKER_MASK32_CH )
431 layout |= DTS_CHANNEL_LAYOUT_CH;
432 if( mask & DTS_LOUDSPEAKER_MASK32_OH )
433 layout |= DTS_CHANNEL_LAYOUT_OH;
434 if( mask & (DTS_LOUDSPEAKER_MASK32_LC | DTS_LOUDSPEAKER_MASK32_RC) )
435 layout |= DTS_CHANNEL_LAYOUT_LC_RC;
436 if( mask & (DTS_LOUDSPEAKER_MASK32_LW | DTS_LOUDSPEAKER_MASK32_RW) )
437 layout |= DTS_CHANNEL_LAYOUT_LW_RW;
438 if( mask & (DTS_LOUDSPEAKER_MASK32_LSS | DTS_LOUDSPEAKER_MASK32_RSS) )
439 layout |= DTS_CHANNEL_LAYOUT_LSS_RSS;
440 if( mask & DTS_LOUDSPEAKER_MASK32_LFE2 )
441 layout |= DTS_CHANNEL_LAYOUT_LFE2;
442 if( mask & (DTS_LOUDSPEAKER_MASK32_LHS | DTS_LOUDSPEAKER_MASK32_RHS) )
443 layout |= DTS_CHANNEL_LAYOUT_LHS_RHS;
444 if( mask & DTS_LOUDSPEAKER_MASK32_CHR )
445 layout |= DTS_CHANNEL_LAYOUT_CHR;
446 if( mask & (DTS_LOUDSPEAKER_MASK32_LHR | DTS_LOUDSPEAKER_MASK32_RHR) )
447 layout |= DTS_CHANNEL_LAYOUT_LHR_RHR;
448 return layout;
451 /* for channels which cannot be expressed by ChannelLayout; CL, LL and RL */
452 static inline uint8_t dts_get_lower_channels_from_ls_mask32( uint32_t mask )
454 return (mask >> 25) & 0x7;
457 static void dts_parse_xll_navigation( lsmash_bits_t *bits, dts_xll_info_t *xll, int nuBits4ExSSFsize, uint64_t *bits_pos )
459 xll->size = dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ) + 1; /* nuExSSXLLFsize (nuBits4ExSSFsize) */
460 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSXLLSyncPresent (1) */
462 dts_bits_get( bits, 4, bits_pos ); /* nuPeakBRCntrlBuffSzkB (4) */
463 int nuBitsInitDecDly = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitsInitDecDly (5) */
464 dts_bits_get( bits, nuBitsInitDecDly, bits_pos ); /* nuInitLLDecDlyFrames (nuBitsInitDecDly) */
465 dts_bits_get( bits, nuBits4ExSSFsize, bits_pos ); /* nuExSSXLLSyncOffset (nuBits4ExSSFsize) */
469 static void dts_parse_lbr_navigation( lsmash_bits_t *bits, dts_lbr_info_t *lbr, uint64_t *bits_pos )
471 lbr->size = dts_bits_get( bits, 14, bits_pos ); /* nuExSSLBRFsize (14) */
472 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSLBRSyncPresent (1) */
473 dts_bits_get( bits, 2, bits_pos ); /* nuExSSLBRSyncDistInFrames (2) */
476 static int dts_parse_asset_descriptor( dts_info_t *info, uint64_t *bits_pos )
478 lsmash_bits_t *bits = info->bits;
479 dts_extension_info_t *exss = &info->exss[ info->exss_index ];
480 /* Audio asset descriptor */
481 uint64_t asset_descriptor_pos = *bits_pos;
482 int nuAssetDescriptFsize = dts_bits_get( bits, 9, bits_pos ) + 1; /* nuAssetDescriptFsize (9) */
483 dts_audio_asset_t *asset = &exss->asset[ dts_bits_get( bits, 3, bits_pos ) ]; /* nuAssetIndex (3) */
484 /* Static metadata */
485 int bEmbeddedStereoFlag = 0;
486 int bEmbeddedSixChFlag = 0;
487 int nuTotalNumChs = 0;
488 if( exss->bStaticFieldsPresent )
490 if( dts_bits_get( bits, 1, bits_pos ) ) /* bAssetTypeDescrPresent (1)*/
491 dts_bits_get( bits, 4, bits_pos ); /* nuAssetTypeDescriptor (4) */
492 if( dts_bits_get( bits, 1, bits_pos ) ) /* bLanguageDescrPresent (1) */
493 dts_bits_get( bits, 24, bits_pos ); /* LanguageDescriptor (24) */
494 if( dts_bits_get( bits, 1, bits_pos ) )
496 int nuInfoTextByteSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nuInfoTextByteSize (10) */
497 dts_bits_get( bits, nuInfoTextByteSize * 8, bits_pos ); /* InfoTextString (nuInfoTextByteSize) */
499 int nuBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBitResolution (5) */
500 exss->bit_resolution = LSMASH_MAX( exss->bit_resolution, nuBitResolution );
501 int nuMaxSampleRate = dts_bits_get( bits, 4, bits_pos ); /* nuMaxSampleRate (4) */
502 static const uint32_t source_sample_rate_table[16] =
504 8000, 16000, 32000, 64000, 128000,
505 22050, 44100, 88200, 176400, 352800,
506 12000, 24000, 48000, 96000, 192000, 384000
508 exss->sampling_frequency = LSMASH_MAX( exss->sampling_frequency, source_sample_rate_table[nuMaxSampleRate] );
509 nuTotalNumChs = dts_bits_get( bits, 8, bits_pos ) + 1; /* nuTotalNumChs (8) */
510 asset->bOne2OneMapChannels2Speakers = dts_bits_get( bits, 1, bits_pos ); /* bOne2OneMapChannels2Speakers (1) */
511 if( asset->bOne2OneMapChannels2Speakers )
513 if( nuTotalNumChs > 2 )
515 bEmbeddedStereoFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedStereoFlag (1) */
516 exss->stereo_downmix |= bEmbeddedStereoFlag;
518 if( nuTotalNumChs > 6 )
519 bEmbeddedSixChFlag = dts_bits_get( bits, 1, bits_pos ); /* bEmbeddedSixChFlag (1) */
520 int nuNumBits4SAMask;
521 if( dts_bits_get( bits, 1, bits_pos ) ) /* bSpkrMaskEnabled (1) */
523 nuNumBits4SAMask = (dts_bits_get( bits, 2, bits_pos ) + 1) << 2; /* nuNumBits4SAMask (2) */
524 asset->channel_layout |= dts_bits_get( bits, nuNumBits4SAMask, bits_pos ); /* nuSpkrActivityMask (nuNumBits4SAMask) */
526 else
527 /* The specification doesn't mention the value of nuNumBits4SAMask if bSpkrMaskEnabled is set to 0. */
528 nuNumBits4SAMask = 16;
529 int nuNumSpkrRemapSets = dts_bits_get( bits, 3, bits_pos );
530 int nuStndrSpkrLayoutMask[8] = { 0 };
531 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
532 nuStndrSpkrLayoutMask[ns] = dts_bits_get( bits, nuNumBits4SAMask, bits_pos );
533 for( int ns = 0; ns < nuNumSpkrRemapSets; ns++ )
535 int nuNumSpeakers = dts_get_channel_count_from_channel_layout( nuStndrSpkrLayoutMask[ns] );
536 int nuNumDecCh4Remap = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuNumDecCh4Remap[ns] (5) */
537 for( int nCh = 0; nCh < nuNumSpeakers; nCh++ )
539 uint32_t nuRemapDecChMask = dts_bits_get( bits, nuNumDecCh4Remap, bits_pos );
540 int nCoef = lsmash_count_bits( nuRemapDecChMask );
541 for( int nc = 0; nc < nCoef; nc++ )
542 dts_bits_get( bits, 5, bits_pos ); /* nuSpkrRemapCodes[ns][nCh][nc] (5) */
546 else
548 asset->nuRepresentationType = dts_bits_get( bits, 3, bits_pos ); /* nuRepresentationType (3) */
549 if( asset->nuRepresentationType == 2
550 || asset->nuRepresentationType == 3 )
551 nuTotalNumChs = 2;
554 /* Dynamic metadata */
555 int bDRCCoefPresent = dts_bits_get( bits, 1, bits_pos ); /* bDRCCoefPresent (1) */
556 if( bDRCCoefPresent )
557 dts_bits_get( bits, 8, bits_pos ); /* nuDRCCode (8) */
558 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDialNormPresent (1) */
559 dts_bits_get( bits, 5, bits_pos ); /* nuDialNormCode (5) */
560 if( bDRCCoefPresent && bEmbeddedStereoFlag )
561 dts_bits_get( bits, 8, bits_pos ); /* nuDRC2ChDmixCode (8) */
562 int bMixMetadataPresent;
563 if( exss->bMixMetadataEnbl )
564 bMixMetadataPresent = dts_bits_get( bits, 1, bits_pos ); /* bMixMetadataPresent (1) */
565 else
566 bMixMetadataPresent = 0;
567 if( bMixMetadataPresent )
569 dts_bits_get( bits, 7, bits_pos ); /* bExternalMixFlag (1)
570 * nuPostMixGainAdjCode (7) */
571 if( dts_bits_get( bits, 2, bits_pos ) < 3 ) /* nuControlMixerDRC (2) */
572 dts_bits_get( bits, 3, bits_pos ); /* nuLimit4EmbeddedDRC (3) */
573 else
574 dts_bits_get( bits, 8, bits_pos ); /* nuCustomDRCCode (8) */
575 int bEnblPerChMainAudioScale = dts_bits_get( bits, 1, bits_pos ); /* bEnblPerChMainAudioScale (1) */
576 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
577 if( bEnblPerChMainAudioScale )
578 for( uint8_t nCh = 0; nCh < exss->nNumMixOutCh[ns]; nCh++ )
579 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][nCh] (6) */
580 else
581 dts_bits_get( bits, 6, bits_pos ); /* nuMainAudioScaleCode[ns][0] (6) */
582 int nEmDM = 1;
583 int nDecCh[3] = { nuTotalNumChs, 0, 0 };
584 if( bEmbeddedSixChFlag )
586 nDecCh[nEmDM] = 6;
587 ++nEmDM;
589 if( bEmbeddedStereoFlag )
591 nDecCh[nEmDM] = 2;
592 ++nEmDM;
594 for( uint8_t ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
595 for( int nE = 0; nE < nEmDM; nE++ )
596 for( int nCh = 0; nCh < nDecCh[nE]; nCh++ )
598 int nuMixMapMask = dts_bits_get( bits, exss->nNumMixOutCh[ns], bits_pos ); /* nuMixMapMask (nNumMixOutCh[ns]) */
599 int nuNumMixCoefs = lsmash_count_bits( nuMixMapMask );
600 for( int nC = 0; nC < nuNumMixCoefs; nC++ )
601 dts_bits_get( bits, 6, bits_pos ); /* nuMixCoeffs[ns][nE][nCh][nC] (6) */
604 /* Decoder navigation data */
605 asset->nuCodingMode = dts_bits_get( bits, 2, bits_pos ); /* nuCodingMode (2) */
606 switch( asset->nuCodingMode )
608 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
610 int nuCoreExtensionMask = dts_bits_get( bits, 12, bits_pos ); /* nuCoreExtensionMask (12) */
611 asset->nuCoreExtensionMask = nuCoreExtensionMask;
612 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
614 asset->core.frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSCoreFsize (14) */
615 if( dts_bits_get( bits, 1, bits_pos ) ) /* bExSSCoreSyncPresent (1) */
616 dts_bits_get( bits, 2, bits_pos ); /* nuExSSCoreSyncDistInFrames (2) */
618 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
619 asset->xbr_size = dts_bits_get( bits, 14, bits_pos ) + 1;
620 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
621 asset->core.xxch.size = dts_bits_get( bits, 14, bits_pos ) + 1;
622 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
623 asset->x96_size = dts_bits_get( bits, 12, bits_pos ) + 1;
624 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
625 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
626 if( nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
627 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
628 break;
630 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
631 dts_parse_xll_navigation( bits, &asset->xll, exss->nuBits4ExSSFsize, bits_pos );
632 break;
633 case 2 : /* DTS-HD Low bit-rate mode */
634 dts_parse_lbr_navigation( bits, &asset->lbr, bits_pos );
635 break;
636 case 3 : /* Auxiliary coding mode */
637 asset->aux_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* nuExSSAuxFsize (14) */
638 break;
639 default :
640 assert( 0 );
641 break;
643 dts_bits_get( bits, nuAssetDescriptFsize * 8 - (*bits_pos - asset_descriptor_pos), bits_pos ); /* Skip remaining part of Audio asset descriptor. */
644 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
647 static int dts_parse_xxch( dts_info_t *info, uint64_t *bits_pos, dts_xxch_info_t *xxch )
649 lsmash_bits_t *bits = info->bits;
650 /* XXCH Frame Header */
651 uint64_t xxch_pos = *bits_pos - 32; /* SYNCXXCh (32) */
652 uint64_t nuHeaderSizeXXCh = dts_bits_get( bits, 6, bits_pos ) + 1; /* nuHeaderSizeXXCh (6) */
653 dts_bits_get( bits, 1, bits_pos ); /* bCRCPresent4ChSetHeaderXXCh (1) */
654 int nuBits4SpkrMaskXXCh = dts_bits_get( bits, 5, bits_pos ) + 1; /* nuBits4SpkrMaskXXCh (5) */
655 int nuNumChSetsInXXCh = dts_bits_get( bits, 2, bits_pos ) + 1; /* nuNumChSetsInXXCh (2) */
656 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
657 dts_bits_get( bits, 14, bits_pos ); /* pnuChSetFsizeXXCh[nChSet] - 1 (14) */
658 /* A 5.1 decoder uses this AMODE to configure its decoded outputs to C, L, R, Ls and Rs layout.
659 * On the other hand a 7.1 decoder ignores the AMODE information from the core stream and uses
660 * instead the nuCoreSpkrActivityMask (C, L, R, LFE1, Lss and Rss) and the nuXXChSpkrLayoutMask
661 * (Lsr and Rsr) from the XXCh stream to get the original 7.1 speaker layout (C, L, R, LFE1, Lss,
662 * Rsr, Lsr and Rsr) and configures its outputs accordingly. */
663 uint32_t xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos ); /* nuCoreSpkrActivityMask (nuBits4SpkrMaskXXCh) */
664 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
665 xxch->lower_planes = dts_get_lower_channels_from_ls_mask32( xxch_mask );
666 dts_bits_get( bits, nuHeaderSizeXXCh * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Frame Header. */
667 for( int nChSet = 0; nChSet < nuNumChSetsInXXCh; nChSet++ )
669 /* XXCH Channel Set Header */
670 xxch_pos = *bits_pos;
671 uint64_t nuXXChChSetHeaderSize = dts_bits_get( bits, 7, bits_pos ) + 1; /* nuXXChChSetHeaderSize (7)*/
672 dts_bits_get( bits, 3, bits_pos ); /* nuChInChSetXXCh (3) */
673 if( nuBits4SpkrMaskXXCh > 6 )
675 xxch_mask = dts_bits_get( bits, nuBits4SpkrMaskXXCh - 6, bits_pos ) << 6; /* nuXXChSpkrLayoutMask (nuBits4SpkrMaskXXCh - 6) */
676 xxch->channel_layout |= dts_get_channel_layout_from_ls_mask32( xxch_mask );
677 xxch->lower_planes |= dts_get_lower_channels_from_ls_mask32( xxch_mask );
679 #if 0 /* FIXME: Can we detect stereo downmixing from only XXCH data within the core substream? */
680 if( dts_bits_get( bits, 1, bits_pos ) ) /* bDownMixCoeffCodeEmbedded (1) */
682 int bDownMixEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownMixEmbedded (1) */
683 dts_bits_get( bits, 6, bits_pos ); /* nDmixScaleFactor (6) */
684 uint32_t DownMixChMapMask[8];
685 for( int nCh = 0; nCh < nuChInChSetXXCh; nCh++ )
686 DownMixChMapMask[nCh] = dts_bits_get( bits, nuBits4SpkrMaskXXCh, bits_pos );
688 #endif
689 dts_bits_get( bits, nuXXChChSetHeaderSize * 8 - (*bits_pos - xxch_pos), bits_pos ); /* Skip remaining part of XXCH Channel Set Header. */
691 return 0;
694 static int dts_parse_core_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
696 if( core->extension_audio_descriptor == 0
697 || core->extension_audio_descriptor == 3 )
698 return LSMASH_ERR_INVALID_DATA;
699 int err = dts_parse_xxch( info, bits_pos, &core->xxch );
700 if( err < 0 )
701 return err;
702 info->flags |= DTS_CORE_SUBSTREAM_XXCH_FLAG;
703 return info->bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
706 static int dts_parse_exss_xxch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
708 lsmash_bits_t *bits = info->bits;
709 if( DTS_SYNCWORD_XXCH != dts_bits_get( bits, 32, bits_pos ) )
710 return LSMASH_ERR_INVALID_DATA;
711 int err = dts_parse_xxch( info, bits_pos, &core->xxch );
712 if( err < 0 )
713 return err;
714 info->flags |= DTS_EXT_SUBSTREAM_XXCH_FLAG;
715 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
718 static int dts_parse_core_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
720 if( core->extension_audio_descriptor != 2
721 && core->extension_audio_descriptor != 3 )
722 return 0; /* Probably this is not an X96 extension. We skip this anyway. */
723 lsmash_bits_t *bits = info->bits;
724 /* DTS_BCCORE_X96 Frame Header */
725 /* SYNCX96 (32) */
726 /* To reduce the probability of false synchronization caused by the presence of pseudo sync words, it is
727 * imperative to check the distance between the detected sync word and the end of current frame. This
728 * distance in bytes shall match the value of FSIZE96. */
729 uint64_t FSIZE96 = ((lsmash_bs_show_byte( bits->bs, 0 ) << 4)
730 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 4) & 0x0F)) + 1;
731 if( core->frame_size * 8 != (*bits_pos - 32 + FSIZE96 * 8) )
732 return 0; /* Encountered four emulation bytes (pseudo sync word). */
733 dts_bits_get( bits, 16, bits_pos ); /* FSIZE96 (12)
734 * REVNO (4) */
735 core->sampling_frequency *= 2;
736 core->frame_duration *= 2;
737 info->flags |= DTS_CORE_SUBSTREAM_X96_FLAG;
738 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
741 static int dts_parse_core_xch( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
743 if( core->extension_audio_descriptor != 0
744 && core->extension_audio_descriptor != 3 )
745 return 0; /* Probably this is not an XCh extension. We skip this anyway. */
746 lsmash_bits_t *bits = info->bits;
747 /* XCH Frame Header */
748 /* XChSYNC (32) */
749 /* For compatibility reasons with legacy bitstreams the estimated distance in bytes is checked against
750 * the XChFSIZE+1 as well as the XChFSIZE. The XCh synchronization is pronounced if the distance matches
751 * either of these two values. */
752 uint64_t XChFSIZE = (lsmash_bs_show_byte( bits->bs, 0 ) << 2)
753 | ((lsmash_bs_show_byte( bits->bs, 1 ) >> 6) & 0x03);
754 if( core->frame_size * 8 != (*bits_pos - 32 + (XChFSIZE + 1) * 8)
755 && core->frame_size * 8 != (*bits_pos - 32 + XChFSIZE * 8) )
756 return 0; /* Encountered four emulation bytes (pseudo sync word). */
757 if( ((lsmash_bs_show_byte( bits->bs, 1 ) >> 2) & 0xF) != 1 )
758 return 0; /* A known value of AMODE is only 1. Otherwise just skip. */
759 dts_bits_get( bits, 16, bits_pos ); /* XChFSIZE (10)
760 * AMODE (4)
761 * byte align (2) */
762 core->channel_layout |= DTS_CHANNEL_LAYOUT_CS;
763 info->flags |= DTS_CORE_SUBSTREAM_XCH_FLAG;
764 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
767 static int dts_parse_exss_xbr( dts_info_t *info, uint64_t *bits_pos )
769 lsmash_bits_t *bits = info->bits;
770 /* XBR Frame Header */
771 uint64_t xbr_pos = *bits_pos;
772 if( DTS_SYNCWORD_XBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXBR (32) */
773 return LSMASH_ERR_INVALID_DATA;
774 uint64_t nHeaderSizeXBR = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
775 dts_bits_get( bits, nHeaderSizeXBR * 8 - (*bits_pos - xbr_pos), bits_pos ); /* Skip the remaining bits in XBR Frame Header. */
776 info->flags |= DTS_EXT_SUBSTREAM_XBR_FLAG;
777 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
780 static int dts_parse_exss_x96( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
782 lsmash_bits_t *bits = info->bits;
783 /* DTS_EXSUB_STREAM_X96 Frame Header */
784 uint64_t x96_pos = *bits_pos;
785 if( DTS_SYNCWORD_X96K != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCX96 (32) */
786 return LSMASH_ERR_INVALID_DATA;
787 uint64_t nHeaderSizeX96 = dts_bits_get( bits, 6, bits_pos ) + 1; /* nHeaderSizeXBR (6) */
788 dts_bits_get( bits, nHeaderSizeX96 * 8 - (*bits_pos - x96_pos), bits_pos ); /* Skip the remaining bits in DTS_EXSUB_STREAM_X96 Frame Header. */
789 /* What the fuck! The specification drops 'if' sentence.
790 * We assume the same behaviour for core substream. */
791 core->sampling_frequency *= 2;
792 core->frame_duration *= 2;
793 info->flags |= DTS_EXT_SUBSTREAM_X96_FLAG;
794 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
797 static int dts_parse_exss_lbr( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
799 lsmash_bits_t *bits = info->bits;
800 dts_lbr_info_t *lbr = &asset->lbr;
801 if( DTS_SYNCWORD_LBR != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCEXTLBR (32) */
802 return LSMASH_ERR_INVALID_DATA;
803 int ucFmtInfoCode = dts_bits_get( bits, 8, bits_pos );
804 if( ucFmtInfoCode == 2 )
806 /* LBR decoder initialization data */
807 int nLBRSampleRateCode = dts_bits_get( bits, 8, bits_pos ); /* nLBRSampleRateCode (8) */
808 int usLBRSpkrMask = dts_bits_get( bits, 16, bits_pos ); /* usLBRSpkrMask (16) */
809 dts_bits_get( bits, 16, bits_pos ); /* nLBRversion (16) */
810 int nLBRCompressedFlags = dts_bits_get( bits, 8, bits_pos ); /* nLBRCompressedFlags (8) */
811 dts_bits_get( bits, 40, bits_pos ); /* nLBRBitRateMSnybbles (8)
812 * nLBROriginalBitRate_LSW (16)
813 * nLBRScaledBitRate_LSW (16) */
814 static const uint32_t source_sample_rate_table[16] =
816 8000, 16000, 32000, 0, 0,
817 11025, 22050, 44100, 0, 0,
818 12000, 24000, 48000, 0, 0, 0
820 enum LBRFlags
822 LBR_FLAG_24_BIT_SAMPLES = 0x01, /* 0b00000001 */
823 LBR_FLAG_USE_LFE = 0x02, /* 0b00000010 */
824 LBR_FLAG_BANDLMT_MASK = 0x1C, /* 0b00011100 */
825 LBR_FLAG_STEREO_DOWNMIX = 0x20, /* 0b00100000 */
826 LBR_FLAG_MULTICHANNEL_DOWNMIX = 0x40, /* 0b01000000 */
828 lbr->sampling_frequency = source_sample_rate_table[nLBRSampleRateCode];
829 lbr->frame_duration = lbr->sampling_frequency < 16000 ? 1024
830 : lbr->sampling_frequency < 32000 ? 2048
831 : 4096;
832 lbr->channel_layout = ((usLBRSpkrMask >> 8) & 0xff) | ((usLBRSpkrMask << 8) & 0xff00); /* usLBRSpkrMask is little-endian. */
833 lbr->stereo_downmix |= !!(nLBRCompressedFlags & LBR_FLAG_STEREO_DOWNMIX);
834 lbr->lfe_present |= !!(nLBRCompressedFlags & LBR_FLAG_USE_LFE);
835 lbr->duration_modifier |= ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x04)
836 || ((nLBRCompressedFlags & LBR_FLAG_BANDLMT_MASK) == 0x0C);
837 lbr->sample_size = (nLBRCompressedFlags & LBR_FLAG_24_BIT_SAMPLES) ? 24 : 16;
839 else if( ucFmtInfoCode != 1 )
840 return LSMASH_ERR_NAMELESS; /* unknown */
841 info->flags |= DTS_EXT_SUBSTREAM_LBR_FLAG;
842 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
845 static int dts_parse_exss_xll( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
847 lsmash_bits_t *bits = info->bits;
848 dts_xll_info_t *xll = &asset->xll;
849 /* Common Header */
850 uint64_t xll_pos = *bits_pos;
851 if( DTS_SYNCWORD_XLL != dts_bits_get( bits, 32, bits_pos ) ) /* SYNCXLL (32) */
852 return LSMASH_ERR_INVALID_DATA;
853 dts_bits_get( bits, 4, bits_pos ); /* nVersion (4) */
854 uint64_t nHeaderSize = dts_bits_get( bits, 8, bits_pos ) + 1; /* nHeaderSize (8) */
855 int nBits4FrameFsize = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4FrameFsize (5) */
856 dts_bits_get( bits, nBits4FrameFsize, bits_pos ); /* nLLFrameSize (nBits4FrameFsize) */
857 int nNumChSetsInFrame = dts_bits_get( bits, 4, bits_pos ) + 1; /* nNumChSetsInFrame (4) */
858 uint16_t nSegmentsInFrame = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSegmentsInFrame (4) */
859 uint16_t nSmplInSeg = 1 << dts_bits_get( bits, 4, bits_pos ); /* nSmplInSeg (4) */
860 int nBits4SSize = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4SSize (5) */
861 dts_bits_get( bits, 3, bits_pos ); /* nBandDataCRCEn (2)
862 * bScalableLSBs (1) */
863 int nBits4ChMask = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBits4ChMask (5) */
864 dts_bits_get( bits, nHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Common Header. */
865 int sum_nChSetLLChannel = 0;
866 uint32_t nFs1 = 0;
867 int number_of_frequency_bands = 0; /* the number of frequency bands is determined simply by the underlying maximum sampling
868 * frequency among all of the channel sets.
869 * For sampling frequency Fs,
870 * Number of frequency bands is 1 for Fs <= Base_Fs
871 * Number of frequency bands is 2 for Base_Fs < Fs <= 2 * Base_Fs
872 * Number of frequency bands is 2 for 2 * Base_Fs < Fs <= 4 * Base_Fs
873 * where Base_Fs denotes the base sampling frequency i.e. 64 kHz, 88.2 kHz, or 96 kHz. */
874 int nNumFreqBands1 = 0;
875 int nNumFreqBands[16] = { 0 };
876 xll->channel_layout = 0;
877 for( int nChSet = 0; nChSet < nNumChSetsInFrame; nChSet++ )
879 /* Channel Set Sub-Header */
880 xll_pos = *bits_pos;
881 uint64_t nChSetHeaderSize = dts_bits_get( bits, 10, bits_pos ) + 1; /* nChSetHeaderSize (10) */
882 int nChSetLLChannel = dts_bits_get( bits, 4, bits_pos ) + 1; /* nChSetLLChannel (4) */
883 dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* nResidualChEncode (nChSetLLChannel) */
884 uint8_t nBitResolution = dts_bits_get( bits, 5, bits_pos ) + 1; /* nBitResolution (5) */
885 dts_bits_get( bits, 5, bits_pos ); /* nBitWidth (5) */
886 xll->pcm_resolution = LSMASH_MAX( xll->pcm_resolution, nBitResolution );
887 static const uint32_t source_sample_rate_table[16] =
889 8000, 16000, 32000, 64000, 128000,
890 22050, 44100, 88200, 176400, 352800,
891 12000, 24000, 48000, 96000, 192000, 384000
893 int sFreqIndex = dts_bits_get( bits, 4, bits_pos ); /* sFreqIndex (4) */
894 uint32_t nFs = source_sample_rate_table[sFreqIndex];
895 dts_bits_get( bits, 2, bits_pos ); /* nFsInterpolate (2) */
896 int nReplacementSet = dts_bits_get( bits, 2, bits_pos ); /* nReplacementSet (2) */
897 if( nReplacementSet > 0 )
898 dts_bits_get( bits, 1, bits_pos ); /* bActiveReplaceSet (1) */
899 if( asset->bOne2OneMapChannels2Speakers )
901 /* Downmix is allowed only when the encoded channel represents a signal feed to a corresponding loudspeaker. */
902 int bPrimaryChSet = dts_bits_get( bits, 1, bits_pos ); /* bPrimaryChSet (1) */
903 int bDownmixCoeffCodeEmbedded = dts_bits_get( bits, 1, bits_pos ); /* bDownmixCoeffCodeEmbedded (1) */
904 int nLLDownmixType = 0x7; /* 0b111: Unused */
905 if( bDownmixCoeffCodeEmbedded )
907 dts_bits_get( bits, 1, bits_pos ); /* bDownmixEmbedded (1) */
908 if( bPrimaryChSet )
909 nLLDownmixType = dts_bits_get( bits, 3, bits_pos ); /* nLLDownmixType (3) */
911 int bHierChSet = dts_bits_get( bits, 1, bits_pos ); /* bHierChSet (1) */
912 if( bDownmixCoeffCodeEmbedded )
914 /* N: the number of channels in the current channel set
915 * for non-primary channel set, adding +1 for the down scaling coefficients that prevent overflow
916 * M: the number of channels that the current channel set is mixed into
917 * Downmix coefficients are transmitted using 9-bit codes. */
918 static const int downmix_channel_count_table[8] = { 1, 2, 2, 3, 3, 4, 4, 0 };
919 int N = nChSetLLChannel + (bPrimaryChSet ? 0 : 1);
920 int M = bPrimaryChSet ? downmix_channel_count_table[nLLDownmixType] : sum_nChSetLLChannel;
921 int nDownmixCoeffs = N * M;
922 dts_bits_get( bits, nDownmixCoeffs * 9, bits_pos ); /* DownmixCoeffs (nDownmixCoeffs * 9) */
923 if( bPrimaryChSet && downmix_channel_count_table[nLLDownmixType] == 2 )
924 xll->stereo_downmix |= 1;
926 if( bHierChSet )
927 sum_nChSetLLChannel += nChSetLLChannel;
928 if( dts_bits_get( bits, 1, bits_pos ) ) /* bChMaskEnabled (1) */
930 uint32_t nChMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nChMask (nBits4ChMask) */
931 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nChMask );
932 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nChMask );
934 else
935 dts_bits_get( bits, 25 * nChSetLLChannel, bits_pos ); /* RadiusDelta[ch] (9)
936 * Theta[ch] (9)
937 * Phi[ch] (7)
938 * per channel */
940 else
942 /* No downmixing is allowed and each channel set is the primary channel set. */
943 if( dts_bits_get( bits, 1, bits_pos ) ) /* bMappingCoeffsPresent (1) */
945 int nBitsCh2SpkrCoef = 6 + 2 * dts_bits_get( bits, 3, bits_pos ); /* nBitsCh2SpkrCoef (3) */
946 int nNumSpeakerConfigs = dts_bits_get( bits, 2, bits_pos ) + 1; /* nNumSpeakerConfigs (2) */
947 for( int nSpkrConf = 0; nSpkrConf < nNumSpeakerConfigs; nSpkrConf++ )
949 int pnActiveChannelMask = dts_bits_get( bits, nChSetLLChannel, bits_pos ); /* pnActiveChannelMask[nSpkrConf] (nChSetLLChannel) */
950 int pnNumSpeakers = dts_bits_get( bits, 6, bits_pos ) + 1; /* pnNumSpeakers[nSpkrConf] (6) */
951 int bSpkrMaskEnabled = dts_bits_get( bits, 1, bits_pos ); /* bSpkrMaskEnabled (1) */
952 if( bSpkrMaskEnabled )
954 uint32_t nSpkrMask = dts_bits_get( bits, nBits4ChMask, bits_pos ); /* nSpkrMask[nSpkrConf] (nBits4ChMask) */
955 xll->channel_layout |= dts_get_channel_layout_from_ls_mask32( nSpkrMask );
956 xll->lower_planes |= dts_get_lower_channels_from_ls_mask32( nSpkrMask );
958 for( int nSpkr = 0; nSpkr < pnNumSpeakers; nSpkr++ )
960 if( !bSpkrMaskEnabled )
961 dts_bits_get( bits, 25, bits_pos ); /* ChSetSpeakerConfiguration (25) */
962 for( int nCh = 0; nCh < nChSetLLChannel; nCh++ )
963 if( pnActiveChannelMask & (1 << nCh) )
964 dts_bits_get( bits, nBitsCh2SpkrCoef, bits_pos ); /* pnCh2SpkrMapCoeff (nBitsCh2SpkrCoef) */
969 int full_bandwidth;
970 if( nFs > 96000 )
972 /* When bXtraFreqBands is equal to 0, only one-half of the original bandwidth is preserved and, thus, the number
973 * of frequency bands is also one-half of the number in the case where full bandwidth is preserved. Apparently,
974 * nSmplInSeg is the number of samples in a segment per one frequency band when full bandwidth is preserved.
975 * Because of this, to get the correct number of samples per frame, multiply the result by 2 when bXtraFreqBands
976 * is equal to 0. */
977 full_bandwidth = dts_bits_get( bits, 1, bits_pos ); /* bXtraFreqBands (1) */
978 nNumFreqBands[nChSet] = (1 + full_bandwidth) << (nFs > 192000);
980 else
982 full_bandwidth = 1;
983 nNumFreqBands[nChSet] = 1;
985 uint32_t nSmplInSeg_nChSet;
986 if( nChSet == 0 )
988 nFs1 = nFs;
989 nNumFreqBands1 = nNumFreqBands[nChSet];
990 nSmplInSeg_nChSet = nSmplInSeg;
992 else
993 nSmplInSeg_nChSet = (nSmplInSeg * (nFs * nNumFreqBands1)) / (nFs1 * nNumFreqBands[nChSet]);
994 if( xll->sampling_frequency < nFs )
996 xll->sampling_frequency = nFs;
997 uint32_t samples_per_band_in_frame = nSegmentsInFrame * nSmplInSeg_nChSet;
998 xll->frame_duration = samples_per_band_in_frame * nNumFreqBands[nChSet] * (2 - full_bandwidth);
1000 if( number_of_frequency_bands < nNumFreqBands[nChSet] )
1001 number_of_frequency_bands = nNumFreqBands[nChSet];
1002 dts_bits_get( bits, nChSetHeaderSize * 8 - (*bits_pos - xll_pos), bits_pos ); /* Skip the remaining bits in Channel Set Sub-Header. */
1004 /* NAVI */
1005 uint64_t FreqBandDataSize = 0;
1006 for( int Band = 0; Band < number_of_frequency_bands; Band++ )
1007 for( int Seg = 0; Seg < nSegmentsInFrame; Seg++ )
1009 /* The spec pseudocode extracts bits and initialize SegmentSize[Band][Seg] here. This may be one of lies in the spec.
1010 * According to 8.3.2 Stream Navigation in ETSI TS 102 114 V1.4.1, sum of all band data for all channel set in a segments is
1011 * the size of that segment. In addition there are no headers associated with segment and channel set of abstraction layer.
1012 * Obviously, the extraction is meaningless and the navigation should works without it. */
1013 // SegmentSize[Band][Seg] = dts_bits_get( bits, nBits4SSize, bits_pos );
1014 for( int nChSet = 0; nChSet < nNumChSetsInFrame; nChSet++ )
1015 if( nNumFreqBands[nChSet] > Band )
1016 FreqBandDataSize += dts_bits_get( bits, nBits4SSize, bits_pos ) + 1; /* BandChSetSize[Band][Seg][nChSet] (nBits4SSize) */
1018 dts_bits_align( bits, bits_pos );
1019 dts_bits_get( bits, 16, bits_pos ); /* Checksum (16) */
1020 /* Skip band data. */
1021 dts_bits_get( bits, FreqBandDataSize * 8, bits_pos );
1022 dts_bits_align4( bits, bits_pos );
1023 if( lsmash_bs_show_be32( bits->bs, 0 ) == DTS_SYNCWORD_X )
1024 xll->dtsx_extension_present = 1;
1025 info->flags |= DTS_EXT_SUBSTREAM_XLL_FLAG;
1026 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1029 static uint16_t dts_generate_channel_layout_from_core( int channel_arrangement )
1031 static const uint16_t channel_layout_map_table[] =
1033 DTS_CHANNEL_LAYOUT_C,
1034 DTS_CHANNEL_LAYOUT_L_R, /* dual mono */
1035 DTS_CHANNEL_LAYOUT_L_R, /* stereo */
1036 DTS_CHANNEL_LAYOUT_L_R, /* sum-difference */
1037 DTS_CHANNEL_LAYOUT_L_R, /* Lt/Rt */
1038 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R,
1039 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
1040 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_CS,
1041 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
1042 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
1043 DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LS_RS,
1044 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR | DTS_CHANNEL_LAYOUT_OH,
1045 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LSR_RSR,
1046 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS,
1047 DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS | DTS_CHANNEL_LAYOUT_LSR_RSR,
1048 DTS_CHANNEL_LAYOUT_C | DTS_CHANNEL_LAYOUT_CS | DTS_CHANNEL_LAYOUT_L_R | DTS_CHANNEL_LAYOUT_LC_RC | DTS_CHANNEL_LAYOUT_LS_RS
1050 return channel_arrangement < 16 ? channel_layout_map_table[channel_arrangement] : 0;
1053 static int dts_parse_core( dts_info_t *info, uint64_t *bits_pos, dts_core_info_t *core )
1055 lsmash_bits_t *bits = info->bits;
1056 memset( core, 0, sizeof(dts_core_info_t) );
1057 /* SYNC (32) */
1058 int frame_type = dts_bits_get( bits, 1, bits_pos ); /* FTYPE (1) */
1059 int deficit_sample_count = dts_bits_get( bits, 5, bits_pos ); /* SHORT (5) */
1060 if( frame_type == 1 && deficit_sample_count != 31 )
1061 return LSMASH_ERR_INVALID_DATA; /* Any normal frame (FTYPE == 1) must have SHORT == 31. */
1062 int crc_present_flag = dts_bits_get( bits, 1, bits_pos ); /* CPF (1) */
1063 int num_of_pcm_sample_blocks = dts_bits_get( bits, 7, bits_pos ) + 1; /* NBLKS (7) */
1064 if( num_of_pcm_sample_blocks <= 5 )
1065 return LSMASH_ERR_INVALID_DATA;
1066 core->frame_duration = 32 * num_of_pcm_sample_blocks;
1067 if( frame_type == 1
1068 && core->frame_duration != 256
1069 && core->frame_duration != 512 && core->frame_duration != 1024
1070 && core->frame_duration != 2048 && core->frame_duration != 4096 )
1071 return LSMASH_ERR_INVALID_DATA; /* For any normal frame, the actual number of PCM core samples per channel must be
1072 * either 4096, 2048, 1024, 512, or 256 samples per channel. */
1073 core->frame_size = dts_bits_get( bits, 14, bits_pos ) + 1; /* FSIZE (14) */
1074 if( core->frame_size < DTS_MIN_CORE_SIZE )
1075 return LSMASH_ERR_INVALID_DATA;
1076 core->channel_arrangement = dts_bits_get( bits, 6, bits_pos ); /* AMODE (6) */
1077 core->channel_layout = dts_generate_channel_layout_from_core( core->channel_arrangement );
1078 int core_audio_sampling_frequency = dts_bits_get( bits, 4, bits_pos ); /* SFREQ (4) */
1079 static const uint32_t sampling_frequency_table[16] =
1082 8000, 16000, 32000, 0, 0,
1083 11025, 22050, 44100, 0, 0,
1084 12000, 24000, 48000, 0, 0
1086 core->sampling_frequency = sampling_frequency_table[core_audio_sampling_frequency];
1087 if( core->sampling_frequency == 0 )
1088 return LSMASH_ERR_INVALID_DATA; /* invalid */
1089 dts_bits_get( bits, 10, bits_pos ); /* Skip remainder 10 bits.
1090 * RATE (5)
1091 * MIX (1)
1092 * DYNF (1)
1093 * TIMEF (1)
1094 * AUXF (1)
1095 * HDCD (1) */
1096 core->extension_audio_descriptor = dts_bits_get( bits, 3, bits_pos ); /* EXT_AUDIO_ID (3)
1097 * Note: EXT_AUDIO_ID == 3 is defined in V1.2.1.
1098 * However, its definition disappears and is reserved in V1.3.1. */
1099 int extended_coding_flag = dts_bits_get( bits, 1, bits_pos ); /* EXT_AUDIO (1) */
1100 dts_bits_get( bits, 1, bits_pos ); /* ASPF (1) */
1101 int low_frequency_effects_flag = dts_bits_get( bits, 2, bits_pos ); /* LFF (2) */
1102 if( low_frequency_effects_flag == 0x3 )
1103 return LSMASH_ERR_INVALID_DATA; /* invalid */
1104 if( low_frequency_effects_flag )
1105 core->channel_layout |= DTS_CHANNEL_LAYOUT_LFE1;
1106 dts_bits_get( bits, 8 + crc_present_flag * 16, bits_pos ); /* HFLAG (1)
1107 * HCRC (16)
1108 * FILTS (1)
1109 * VERNUM (4)
1110 * CHIST (2) */
1111 int PCMR = dts_bits_get( bits, 3, bits_pos ); /* PCMR (3) */
1112 static const uint8_t source_resolution_table[8] = { 16, 16, 20, 20, 0, 24, 24, 0 };
1113 core->pcm_resolution = source_resolution_table[PCMR];
1114 if( core->pcm_resolution == 0 )
1115 return LSMASH_ERR_INVALID_DATA; /* invalid */
1116 dts_bits_get( bits, 6, bits_pos ); /* SUMF (1)
1117 * SUMS (1)
1118 * DIALNORM/UNSPEC (4) */
1119 if( extended_coding_flag )
1121 uint32_t syncword = dts_bits_get( bits, 24, bits_pos );
1122 uint64_t frame_size_bits = core->frame_size * 8;
1123 while( (*bits_pos + 24) < frame_size_bits )
1125 int err;
1126 syncword = ((syncword << 8) & 0xffffff00) | dts_bits_get( bits, 8, bits_pos );
1127 switch( syncword )
1129 case DTS_SYNCWORD_XXCH :
1130 if( (err = dts_parse_core_xxch( info, bits_pos, core )) < 0 )
1131 return err;
1132 syncword = dts_bits_get( bits, 24, bits_pos );
1133 break;
1134 case DTS_SYNCWORD_X96K :
1135 if( (err = dts_parse_core_x96( info, bits_pos, core )) < 0 )
1136 return err;
1137 syncword = dts_bits_get( bits, 24, bits_pos );
1138 break;
1139 case DTS_SYNCWORD_XCH :
1140 if( (err = dts_parse_core_xch( info, bits_pos, core )) < 0 )
1141 return err;
1142 break;
1143 default :
1144 continue;
1148 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1151 static int dts_parse_exss_core( dts_info_t *info, uint64_t *bits_pos, dts_audio_asset_t *asset )
1153 lsmash_bits_t *bits = info->bits;
1154 if( DTS_SYNCWORD_SUBSTREAM_CORE != dts_bits_get( bits, 32, bits_pos ) )
1155 return LSMASH_ERR_INVALID_DATA;
1156 int err = dts_parse_core( info, bits_pos, &asset->core );
1157 if( err < 0 )
1158 return err;
1159 info->flags |= DTS_EXT_SUBSTREAM_CORE_FLAG;
1160 return bits->bs->error ? LSMASH_ERR_NAMELESS : 0;
1163 int dts_parse_core_substream( dts_info_t *info )
1165 lsmash_bits_t *bits = info->bits;
1166 uint64_t bits_pos = 0;
1167 int err;
1168 if( DTS_SYNCWORD_CORE != dts_bits_get( bits, 32, &bits_pos ) )
1170 err = LSMASH_ERR_INVALID_DATA;
1171 goto parse_fail;
1173 /* By default the core substream data, if present, has the nuBcCoreExtSSIndex = 0 and the nuBcCoreAssetIndex = 0. */
1174 dts_extension_info_t *exss = &info->exss[0];
1175 if( (err = dts_parse_core( info, &bits_pos, &exss->asset[0].core )) < 0 )
1176 goto parse_fail;
1177 exss->bBcCorePresent [0] = 1;
1178 exss->nuBcCoreExtSSIndex[0] = 0;
1179 exss->nuBcCoreAssetIndex[0] = 0;
1180 info->flags |= DTS_CORE_SUBSTREAM_CORE_FLAG;
1181 info->exss_count = 0;
1182 info->core = exss->asset[0].core;
1183 info->frame_size = exss->asset[0].core.frame_size;
1184 lsmash_bits_get_align( bits );
1185 return 0;
1186 parse_fail:
1187 lsmash_bits_get_align( bits );
1188 return err;
1191 int dts_parse_extension_substream( dts_info_t *info )
1193 lsmash_bits_t *bits = info->bits;
1194 uint64_t bits_pos = 0;
1195 dts_bits_get( bits, 40, &bits_pos ); /* SYNCEXTSSH (32)
1196 * UserDefinedBits (8) */
1197 int nExtSSIndex = dts_bits_get( bits, 2, &bits_pos ); /* nExtSSIndex (2) */
1198 info->exss_index = nExtSSIndex;
1199 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1200 memset( exss, 0, sizeof(dts_extension_info_t) );
1201 int bHeaderSizeType = dts_bits_get( bits, 1, &bits_pos ); /* bHeaderSizeType (1) */
1202 int nuBits4Header = 8 + bHeaderSizeType * 4;
1203 int nuBits4ExSSFsize = 16 + bHeaderSizeType * 4;
1204 exss->nuBits4ExSSFsize = nuBits4ExSSFsize;
1205 uint32_t nuExtSSHeaderSize = dts_bits_get( bits, nuBits4Header, &bits_pos ) + 1; /* nuExtSSHeaderSize (8 or 12) */
1206 info->frame_size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuExtSSFsize (16 or 20) */
1207 if( info->frame_size < 10 )
1208 return LSMASH_ERR_INVALID_DATA;
1209 exss->bStaticFieldsPresent = dts_bits_get( bits, 1, &bits_pos ); /* bStaticFieldsPresent (1) */
1210 if( exss->bStaticFieldsPresent )
1212 dts_bits_get( bits, 2, &bits_pos ); /* nuRefClockCode (2) */
1213 exss->frame_duration = 512 * (dts_bits_get( bits, 3, &bits_pos ) + 1); /* nuExSSFrameDurationCode (3) */
1214 if( dts_bits_get( bits, 1, &bits_pos ) ) /* bTimeStampFlag (1) */
1215 dts_bits_get( bits, 36, &bits_pos ); /* nuTimeStamp (32)
1216 * nLSB (4) */
1217 exss->nuNumAudioPresnt = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAudioPresnt (3) */
1218 exss->nuNumAssets = dts_bits_get( bits, 3, &bits_pos ) + 1; /* nuNumAssets (3) */
1219 /* The extension substreams with indexes lower than or equal to the index of the current extension substream can
1220 * be activated in the audio presentations indicated within the current extension substream. */
1221 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1222 exss->nuActiveExSSMask[nAuPr]
1223 = dts_bits_get( bits, nExtSSIndex + 1, &bits_pos ); /* nuActiveExSSMask[nAuPr] (nExtSSIndex + 1) */
1224 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1225 for( uint8_t nSS = 0; nSS <= nExtSSIndex; nSS++ )
1226 exss->nuActiveAssetMask[nAuPr][nSS]
1227 = ((exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1)
1228 ? dts_bits_get( bits, 8, &bits_pos ) /* nuActiveAssetMask[nAuPr][nSS] (8) */
1229 : 0;
1230 exss->bMixMetadataEnbl = dts_bits_get( bits, 1, &bits_pos ); /* bMixMetadataEnbl (1) */
1231 if( exss->bMixMetadataEnbl )
1233 dts_bits_get( bits, 2, &bits_pos ); /* nuMixMetadataAdjLevel (2) */
1234 int nuBits4MixOutMask = (dts_bits_get( bits, 2, &bits_pos ) + 1) << 2; /* nuBits4MixOutMask (2) */
1235 exss->nuNumMixOutConfigs = dts_bits_get( bits, 2, &bits_pos ) + 1; /* nuNumMixOutConfigs (2) */
1236 for( int ns = 0; ns < exss->nuNumMixOutConfigs; ns++ )
1238 int nuMixOutChMask = dts_bits_get( bits, nuBits4MixOutMask, &bits_pos ); /* nuMixOutChMask[ns] (nuBits4MixOutMask) */
1239 exss->nNumMixOutCh[ns] = dts_get_channel_count_from_channel_layout( nuMixOutChMask );
1243 else
1245 exss->nuNumAudioPresnt = 1;
1246 exss->nuNumAssets = 1;
1247 exss->bMixMetadataEnbl = 0;
1248 exss->nuNumMixOutConfigs = 0;
1250 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1251 exss->asset[nAst].size = dts_bits_get( bits, nuBits4ExSSFsize, &bits_pos ) + 1; /* nuAssetFsize[nAst] - 1 (nuBits4ExSSFsize) */
1252 int err;
1253 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1254 if( (err = dts_parse_asset_descriptor( info, &bits_pos )) < 0 )
1255 goto parse_fail;
1256 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1257 exss->bBcCorePresent[nAuPr] = dts_bits_get( bits, 1, &bits_pos );
1258 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1259 if( exss->bBcCorePresent[nAuPr] )
1261 exss->nuBcCoreExtSSIndex[nAuPr] = dts_bits_get( bits, 2, &bits_pos );
1262 exss->nuBcCoreAssetIndex[nAuPr] = dts_bits_get( bits, 3, &bits_pos );
1264 dts_bits_get( bits, nuExtSSHeaderSize * 8 - bits_pos, &bits_pos );
1265 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1267 /* Asset Data */
1268 dts_audio_asset_t *asset = &exss->asset[nAst];
1269 uint32_t asset_pos = bits_pos;
1270 switch( asset->nuCodingMode )
1272 case 0 : /* DTS-HD Coding Mode that may contain multiple coding components */
1274 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_CORE_FLAG )
1276 /* Core component */
1277 uint64_t core_pos = bits_pos;
1278 if( (err = dts_parse_exss_core( info, &bits_pos, asset )) < 0 )
1279 goto parse_fail;
1280 dts_bits_get( bits, asset->core.frame_size * 8 - (bits_pos - core_pos), &bits_pos );
1282 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XBR_FLAG )
1284 /* XBR extension */
1285 uint64_t xbr_pos = bits_pos;
1286 if( (err = dts_parse_exss_xbr( info, &bits_pos )) < 0 )
1287 goto parse_fail;
1288 dts_bits_get( bits, asset->xbr_size * 8 - (bits_pos - xbr_pos), &bits_pos );
1290 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1292 /* XXCH extension */
1293 uint64_t xxch_pos = bits_pos;
1294 if( (err = dts_parse_exss_xxch( info, &bits_pos, &asset->core )) < 0 )
1295 goto parse_fail;
1296 dts_bits_get( bits, asset->core.xxch.size * 8 - (bits_pos - xxch_pos), &bits_pos );
1298 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_X96_FLAG )
1300 /* X96 extension */
1301 uint64_t x96_pos = bits_pos;
1302 if( (err = dts_parse_exss_x96( info, &bits_pos, &asset->core )) < 0 )
1303 goto parse_fail;
1304 dts_bits_get( bits, asset->x96_size * 8 - (bits_pos - x96_pos), &bits_pos );
1306 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_LBR_FLAG )
1308 /* LBR component */
1309 uint64_t lbr_pos = bits_pos;
1310 if( (err = dts_parse_exss_lbr( info, &bits_pos, asset )) < 0 )
1311 goto parse_fail;
1312 dts_bits_get( bits, asset->lbr.size * 8 - (bits_pos - lbr_pos), &bits_pos );
1314 if( asset->nuCoreExtensionMask & DTS_EXT_SUBSTREAM_XLL_FLAG )
1316 /* Lossless extension */
1317 uint64_t xll_pos = bits_pos;
1318 if( (err = dts_parse_exss_xll( info, &bits_pos, asset )) < 0 )
1319 goto parse_fail;
1320 dts_bits_get( bits, asset->xll.size * 8 - (bits_pos - xll_pos), &bits_pos );
1322 break;
1324 case 1 : /* DTS-HD Loss-less coding mode without CBR component */
1325 if( (err = dts_parse_exss_xll( info, &bits_pos, asset )) < 0 )
1326 goto parse_fail;
1327 break;
1328 case 2 : /* DTS-HD Low bit-rate mode */
1329 if( (err = dts_parse_exss_lbr( info, &bits_pos, asset )) < 0 )
1330 goto parse_fail;
1331 break;
1332 case 3 : /* Auxiliary coding mode */
1333 dts_bits_get( bits, asset->aux_size * 8, &bits_pos );
1334 break;
1336 dts_bits_get( bits, asset->size * 8 - (bits_pos - asset_pos), &bits_pos );
1338 dts_bits_get( bits, info->frame_size * 8 - bits_pos, &bits_pos );
1339 lsmash_bits_get_align( bits );
1340 if( info->exss_count < DTS_MAX_NUM_EXSS )
1341 info->exss_count += 1;
1342 return 0;
1343 parse_fail:
1344 lsmash_bits_get_align( bits );
1345 return err;
1348 dts_substream_type dts_get_substream_type( dts_info_t *info )
1350 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 4 )
1351 return DTS_SUBSTREAM_TYPE_NONE;
1352 uint8_t *buffer = lsmash_bs_get_buffer_data( info->bits->bs );
1353 uint32_t syncword = LSMASH_4CC( buffer[0], buffer[1], buffer[2], buffer[3] );
1354 switch( syncword )
1356 case DTS_SYNCWORD_CORE :
1357 return DTS_SUBSTREAM_TYPE_CORE;
1358 case DTS_SYNCWORD_SUBSTREAM :
1359 return DTS_SUBSTREAM_TYPE_EXTENSION;
1360 default :
1361 return DTS_SUBSTREAM_TYPE_NONE;
1365 int dts_get_exss_index( dts_info_t *info, uint8_t *exss_index )
1367 if( lsmash_bs_get_remaining_buffer_size( info->bits->bs ) < 6 )
1368 return LSMASH_ERR_INVALID_DATA;
1369 *exss_index = lsmash_bs_show_byte( info->bits->bs, 5 ) >> 6;
1370 return 0;
1373 int dts_get_max_channel_count( dts_info_t *info )
1375 int max_channel_count = 0;
1376 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1378 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1379 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1381 /* Get the channel layout of an audio presentation from a core component. */
1382 uint16_t channel_layout = 0;
1383 int channel_count = 0;
1384 if( exss->bBcCorePresent [nAuPr]
1385 && exss->nuBcCoreAssetIndex[nAuPr] < exss->nuNumAssets )
1387 dts_core_info_t *core = &info->exss[ exss->nuBcCoreExtSSIndex[nAuPr] ].asset[ exss->nuBcCoreAssetIndex[nAuPr] ].core;
1388 if( core->xxch.channel_layout | core->xxch.lower_planes )
1390 channel_layout = core->xxch.channel_layout;
1391 channel_count = lsmash_count_bits( core->xxch.lower_planes ); /* FIXME: Should we count these channels? */
1393 else
1394 channel_layout = core->channel_layout;
1396 channel_count += dts_get_channel_count_from_channel_layout( channel_layout );
1397 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1398 /* Get the channel layouts of an audio presentation from extension substreams. */
1399 uint16_t ext_channel_layout = 0;
1400 uint16_t lbr_channel_layout = 0;
1401 uint16_t xll_channel_layout = 0;
1402 uint8_t xll_lower_channels = 0;
1403 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1404 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1405 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1406 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1408 dts_audio_asset_t *asset = &exss->asset[nAst];
1409 ext_channel_layout |= asset->channel_layout;
1410 lbr_channel_layout |= asset->lbr.channel_layout;
1411 xll_channel_layout |= asset->xll.channel_layout;
1412 xll_lower_channels |= asset->xll.lower_planes;
1414 /* Audio asset descriptors */
1415 channel_count = dts_get_channel_count_from_channel_layout( ext_channel_layout );
1416 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1417 /* LBR components */
1418 channel_count = dts_get_channel_count_from_channel_layout( lbr_channel_layout );
1419 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1420 /* Lossless extensions */
1421 channel_count = dts_get_channel_count_from_channel_layout( xll_channel_layout )
1422 + lsmash_count_bits( xll_lower_channels );
1423 max_channel_count = LSMASH_MAX( max_channel_count, channel_count );
1426 return max_channel_count;
1429 void dts_update_specific_param( dts_info_t *info )
1431 lsmash_dts_specific_parameters_t *param = &info->ddts_param;
1432 /* Find the first valid substream.
1433 * Both nuNumAudioPresnt and nuNumAssets of any substream must not be 0. Therefore, at least one of these are 0,
1434 * then the substream is invalid or absent. */
1435 int exss_index_start = 0;
1436 for( int nExtSSIndex = 0; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1438 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1439 if( exss->nuNumAudioPresnt && exss->nuNumAssets )
1441 exss_index_start = nExtSSIndex;
1442 break;
1445 /* DTSSamplingFrequency and FrameDuration */
1446 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1448 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1449 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1450 continue;
1451 if( param->DTSSamplingFrequency <= exss->sampling_frequency )
1453 param->DTSSamplingFrequency = exss->sampling_frequency;
1454 info->frame_duration = exss->frame_duration;
1456 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1458 dts_audio_asset_t *asset = &exss->asset[nAst];
1459 if( param->DTSSamplingFrequency <= asset->core.sampling_frequency )
1461 param->DTSSamplingFrequency = asset->core.sampling_frequency;
1462 info->frame_duration = asset->core.frame_duration;
1464 if( param->DTSSamplingFrequency <= asset->lbr.sampling_frequency )
1466 param->DTSSamplingFrequency = asset->lbr.sampling_frequency;
1467 info->frame_duration = asset->lbr.frame_duration;
1469 if( param->DTSSamplingFrequency <= asset->xll.sampling_frequency )
1471 param->DTSSamplingFrequency = asset->xll.sampling_frequency;
1472 info->frame_duration = asset->xll.frame_duration;
1476 param->FrameDuration = 0;
1477 for( uint32_t frame_duration = info->frame_duration >> 10; frame_duration; frame_duration >>= 1 )
1478 ++ param->FrameDuration;
1479 /* pcmSampleDepth */
1480 param->pcmSampleDepth = 0;
1481 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1483 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1484 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1485 continue;
1486 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, exss->bit_resolution );
1487 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1489 dts_audio_asset_t *asset = &exss->asset[nAst];
1490 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->core.pcm_resolution );
1491 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->lbr.sample_size );
1492 param->pcmSampleDepth = LSMASH_MAX( param->pcmSampleDepth, asset->xll.pcm_resolution );
1495 param->pcmSampleDepth = param->pcmSampleDepth > 16 ? 24 : 16;
1496 /* StreamConstruction */
1497 param->StreamConstruction = lsmash_dts_get_stream_construction( info->flags );
1498 /* CoreLFEPresent */
1499 param->CoreLFEPresent = !!(info->core.channel_layout & DTS_CHANNEL_LAYOUT_LFE1);
1500 /* CoreLayout */
1501 if( param->StreamConstruction == 0 /* Unknown */
1502 || param->StreamConstruction >= 17 /* No core substream */ )
1503 /* Use ChannelLayout. */
1504 param->CoreLayout = 31;
1505 else
1507 if( info->core.channel_arrangement != 1
1508 && info->core.channel_arrangement != 3
1509 && info->core.channel_arrangement <= 9 )
1510 param->CoreLayout = info->core.channel_arrangement;
1511 else
1512 /* Use ChannelLayout. */
1513 param->CoreLayout = 31;
1515 /* CoreSize
1516 * The specification says this field is the size of a core substream AU in bytes.
1517 * If we don't assume CoreSize is the copy of FSIZE, when FSIZE equals 0x3FFF, this field overflows and becomes 0. */
1518 param->CoreSize = info->core.frame_size ? LSMASH_MIN( info->core.frame_size - 1, 0x3FFF ) : 0;
1519 /* StereoDownmix */
1520 param->StereoDownmix = 0;
1521 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1523 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1524 param->StereoDownmix |= exss->stereo_downmix;
1525 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1527 param->StereoDownmix |= exss->asset[nAst].lbr.stereo_downmix;
1528 param->StereoDownmix |= exss->asset[nAst].xll.stereo_downmix;
1531 /* RepresentationType
1532 * Available only when core substream is absent and ChannelLayout is set to 0. */
1533 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1535 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1536 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1537 continue;
1538 for( uint8_t nAuPr = 0; nAuPr < exss->nuNumAudioPresnt; nAuPr++ )
1540 int asset_count = 0;
1541 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1542 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1543 asset_count += lsmash_count_bits( exss->nuActiveAssetMask[nAuPr][nSS] );
1544 if( asset_count > 1 )
1546 /* An audio presentation has mulple audio assets.
1547 * Audio asset designated for mixing with another audio asset. */
1548 param->RepresentationType = 0;
1549 nExtSSIndex = DTS_MAX_NUM_EXSS;
1550 break;
1552 for( int nSS = 0; nSS <= nExtSSIndex; nSS++ )
1553 if( (exss->nuActiveExSSMask[nAuPr] >> nSS) & 0x1 )
1554 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1555 if( (exss->nuActiveAssetMask[nAuPr][nSS] >> nAst) & 0x1 )
1557 dts_audio_asset_t *asset = &exss->asset[nAst];
1558 if( asset->nuRepresentationType == info->exss[exss_index_start].asset[0].nuRepresentationType )
1559 param->RepresentationType = asset->nuRepresentationType;
1560 else
1562 /* Detected different representation types. Use ChannelLayout. */
1563 param->RepresentationType = 0;
1564 nAuPr = exss->nuNumAudioPresnt;
1565 nExtSSIndex = DTS_MAX_NUM_EXSS;
1566 break;
1571 /* ChannelLayout
1572 * complete information on channels coded in the audio stream including core and extensions */
1573 param->ChannelLayout = 0;
1574 if( param->RepresentationType == 0 )
1575 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1577 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1578 if( exss->nuNumAudioPresnt == 0 || exss->nuNumAssets == 0 )
1579 continue;
1580 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1582 dts_audio_asset_t *asset = &exss->asset[nAst];
1583 param->ChannelLayout |= asset->channel_layout;
1584 param->ChannelLayout |= asset->core.channel_layout;
1585 param->ChannelLayout |= asset->core.xxch.channel_layout;
1586 param->ChannelLayout |= asset->lbr.channel_layout;
1587 param->ChannelLayout |= asset->xll.channel_layout;
1590 /* MultiAssetFlag
1591 * When multiple assets exist, the remaining parameters in the DTSSpecificBox only reflect the coding parameters of the first asset. */
1592 param->MultiAssetFlag = ((info->exss[0].nuNumAssets
1593 + info->exss[1].nuNumAssets
1594 + info->exss[2].nuNumAssets
1595 + info->exss[3].nuNumAssets) > 1);
1596 /* LBRDurationMod */
1597 param->LBRDurationMod = info->exss[exss_index_start].asset[0].lbr.duration_modifier;
1598 /* DTSExpansionBox[] */
1599 for( int nExtSSIndex = exss_index_start; nExtSSIndex < DTS_MAX_NUM_EXSS; nExtSSIndex++ )
1601 dts_extension_info_t *exss = &info->exss[nExtSSIndex];
1602 for( uint8_t nAst = 0; nAst < exss->nuNumAssets; nAst++ )
1604 dts_audio_asset_t *asset = &exss->asset[nAst];
1605 if( asset->xll.dtsx_extension_present )
1607 /* Add DTSXParameters Box so that its presence indicates DTS:X extensions are present in the bitstream.
1608 * Here, treat as unknown whether dialog level control for dialog objects in the bitstream is present or not. */
1609 static const uint8_t dxpb[] =
1611 0x00, 0x00, 0x00, 0x0c, /* size = 12 */
1612 0x64, 0x78, 0x70, 0x62, /* type = 'dxpb' */
1613 0x00, 0x00, 0x00, 0x00 /* version = 0, flags = 0x000000 (no dialog_control_info_present flag) */
1615 lsmash_remove_dts_reserved_box( param );
1616 lsmash_append_dts_reserved_box( param, dxpb, sizeof(dxpb) );
1617 /* No error checks and just return. */
1618 goto param_initialized;
1622 param_initialized:
1623 info->ddts_param_initialized = 1;
1626 int dts_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1628 assert( dst && dst->data.structured && src && src->data.unstructured );
1629 if( src->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1630 return LSMASH_ERR_INVALID_DATA;
1631 lsmash_dts_specific_parameters_t *param = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1632 uint8_t *data = src->data.unstructured;
1633 uint64_t size = LSMASH_GET_BE32( data );
1634 int dts_specific_box_min_length = DTS_SPECIFIC_BOX_MIN_LENGTH;
1635 data += ISOM_BASEBOX_COMMON_SIZE;
1636 if( size == 1 )
1638 size = LSMASH_GET_BE64( data );
1639 dts_specific_box_min_length += 8;
1640 data += 8;
1642 if( size != src->size )
1643 return LSMASH_ERR_INVALID_DATA;
1644 param->DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1645 param->maxBitrate = LSMASH_GET_BE32( &data[4] );
1646 param->avgBitrate = LSMASH_GET_BE32( &data[8] );
1647 param->pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1648 param->FrameDuration = (data[13] >> 6) & 0x03;
1649 param->StreamConstruction = (data[13] >> 1) & 0x1F;
1650 param->CoreLFEPresent = data[13] & 0x01;
1651 param->CoreLayout = (data[14] >> 2) & 0x3F;
1652 param->CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1653 param->StereoDownmix = (data[16] >> 3) & 0x01;
1654 param->RepresentationType = data[16] & 0x07;
1655 param->ChannelLayout = (data[17] << 8) | data[18];
1656 param->MultiAssetFlag = (data[19] >> 7) & 0x01;
1657 param->LBRDurationMod = (data[19] >> 6) & 0x01;
1658 int reserved_box_present = ((data[19] >> 5) & 0x01) && (size > dts_specific_box_min_length);
1659 if( reserved_box_present )
1660 lsmash_append_dts_reserved_box( param, data + 20, size - dts_specific_box_min_length );
1661 return 0;
1664 int dts_copy_codec_specific( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
1666 assert( src && src->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && src->data.structured );
1667 assert( dst && dst->format == LSMASH_CODEC_SPECIFIC_FORMAT_STRUCTURED && dst->data.structured );
1668 lsmash_dts_specific_parameters_t *src_data = (lsmash_dts_specific_parameters_t *)src->data.structured;
1669 lsmash_dts_specific_parameters_t *dst_data = (lsmash_dts_specific_parameters_t *)dst->data.structured;
1670 lsmash_remove_dts_reserved_box( dst_data );
1671 *dst_data = *src_data;
1672 if( !src_data->box || !src_data->box->data || src_data->box->size == 0 )
1674 lsmash_remove_dts_reserved_box( dst_data );
1675 return 0;
1677 return lsmash_append_dts_reserved_box( dst_data, src_data->box->data, src_data->box->size );
1680 int dts_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
1682 assert( box->manager & LSMASH_BINARY_CODED_BOX );
1683 int indent = level;
1684 lsmash_ifprintf( fp, indent++, "[%s: DTS Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
1685 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
1686 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
1687 if( box->size < DTS_SPECIFIC_BOX_MIN_LENGTH )
1688 return LSMASH_ERR_INVALID_DATA;
1689 uint8_t *data = box->binary;
1690 isom_skip_box_common( &data );
1691 uint32_t DTSSamplingFrequency = LSMASH_GET_BE32( &data[0] );
1692 uint32_t maxBitrate = LSMASH_GET_BE32( &data[4] );
1693 uint32_t avgBitrate = LSMASH_GET_BE32( &data[8] );
1694 uint8_t pcmSampleDepth = LSMASH_GET_BYTE( &data[12] );
1695 uint8_t FrameDuration = (data[13] >> 6) & 0x03;
1696 uint8_t StreamConstruction = (data[13] >> 1) & 0x1F;
1697 uint8_t CoreLFEPresent = data[13] & 0x01;
1698 uint8_t CoreLayout = (data[14] >> 2) & 0x3F;
1699 uint16_t CoreSize = ((data[14] & 0x03) << 12) | (data[15] << 4) | ((data[16] >> 4) & 0x0F);
1700 uint8_t StereoDownmix = (data[16] >> 3) & 0x01;
1701 uint8_t RepresentationType = data[16] & 0x07;
1702 uint16_t ChannelLayout = (data[17] << 8) | data[18];
1703 uint8_t MultiAssetFlag = (data[19] >> 7) & 0x01;
1704 uint8_t LBRDurationMod = (data[19] >> 6) & 0x01;
1705 uint8_t ReservedBoxPresent = (data[19] >> 5) & 0x01;
1706 uint8_t Reserved = data[19] & 0x1F;
1707 uint32_t frame_duration = 512 << FrameDuration;
1708 int construction_flags = StreamConstruction <= DTS_MAX_STREAM_CONSTRUCTION ? construction_info[StreamConstruction] : 0;
1709 static const char *core_layout_description[64] =
1711 "Mono (1/0)",
1712 "Undefined",
1713 "Stereo (2/0)",
1714 "Undefined",
1715 "LT,RT (2/0)",
1716 "L, C, R (3/0)",
1717 "L, R, S (2/1)",
1718 "L, C, R, S (3/1)",
1719 "L, R, LS, RS (2/2)",
1720 "L, C, R, LS, RS (3/2)",
1721 [31] = "use ChannelLayout"
1723 static const char *representation_type_description[8] =
1725 "Audio asset designated for mixing with another audio asset",
1726 "Reserved",
1727 "Lt/Rt Encoded for matrix surround decoding",
1728 "Audio processed for headphone playback",
1729 "Reserved",
1730 "Reserved",
1731 "Reserved",
1732 "Reserved"
1734 static const char *channel_layout_description[16] =
1736 "Center in front of listener",
1737 "Left/Right in front",
1738 "Left/Right surround on side in rear",
1739 "Low frequency effects subwoofer",
1740 "Center surround in rear",
1741 "Left/Right height in front",
1742 "Left/Right surround in rear",
1743 "Center Height in front",
1744 "Over the listener's head",
1745 "Between left/right and center in front",
1746 "Left/Right on side in front",
1747 "Left/Right surround on side",
1748 "Second low frequency effects subwoofer",
1749 "Left/Right height on side",
1750 "Center height in rear",
1751 "Left/Right height in rear"
1753 lsmash_ifprintf( fp, indent, "DTSSamplingFrequency = %"PRIu32" Hz\n", DTSSamplingFrequency );
1754 lsmash_ifprintf( fp, indent, "maxBitrate = %"PRIu32" bit/s\n", maxBitrate );
1755 lsmash_ifprintf( fp, indent, "avgBitrate = %"PRIu32" bit/s\n", avgBitrate );
1756 lsmash_ifprintf( fp, indent, "pcmSampleDepth = %"PRIu8" bits\n", pcmSampleDepth );
1757 lsmash_ifprintf( fp, indent, "FrameDuration = %"PRIu8" (%"PRIu32" samples)\n", FrameDuration, frame_duration );
1758 lsmash_ifprintf( fp, indent, "StreamConstruction = 0x%02"PRIx8"\n", StreamConstruction );
1759 if( construction_flags & (DTS_CORE_SUBSTREAM_CORE_FLAG | DTS_CORE_SUBSTREAM_XCH_FLAG | DTS_CORE_SUBSTREAM_X96_FLAG | DTS_CORE_SUBSTREAM_XXCH_FLAG) )
1761 lsmash_ifprintf( fp, indent + 1, "Core substream\n" );
1762 if( construction_flags & DTS_CORE_SUBSTREAM_CORE_FLAG )
1763 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1764 if( construction_flags & DTS_CORE_SUBSTREAM_XCH_FLAG )
1765 lsmash_ifprintf( fp, indent + 2, "XCH\n" );
1766 if( construction_flags & DTS_CORE_SUBSTREAM_X96_FLAG )
1767 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1768 if( construction_flags & DTS_CORE_SUBSTREAM_XXCH_FLAG )
1769 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1771 if( construction_flags & (DTS_EXT_SUBSTREAM_CORE_FLAG | DTS_EXT_SUBSTREAM_XXCH_FLAG | DTS_EXT_SUBSTREAM_X96_FLAG
1772 | DTS_EXT_SUBSTREAM_XBR_FLAG | DTS_EXT_SUBSTREAM_XLL_FLAG | DTS_EXT_SUBSTREAM_LBR_FLAG) )
1774 lsmash_ifprintf( fp, indent + 1, "Extension substream\n" );
1775 if( construction_flags & DTS_EXT_SUBSTREAM_CORE_FLAG )
1776 lsmash_ifprintf( fp, indent + 2, "Core\n" );
1777 if( construction_flags & DTS_EXT_SUBSTREAM_XXCH_FLAG )
1778 lsmash_ifprintf( fp, indent + 2, "XXCH\n" );
1779 if( construction_flags & DTS_EXT_SUBSTREAM_X96_FLAG )
1780 lsmash_ifprintf( fp, indent + 2, "X96\n" );
1781 if( construction_flags & DTS_EXT_SUBSTREAM_XBR_FLAG )
1782 lsmash_ifprintf( fp, indent + 2, "XBR\n" );
1783 if( construction_flags & DTS_EXT_SUBSTREAM_XLL_FLAG )
1784 lsmash_ifprintf( fp, indent + 2, "XLL\n" );
1785 if( construction_flags & DTS_EXT_SUBSTREAM_LBR_FLAG )
1786 lsmash_ifprintf( fp, indent + 2, "LBR\n" );
1788 lsmash_ifprintf( fp, indent, "CoreLFEPresent = %s\n", CoreLFEPresent ? "1 (LFE exists)" : "0 (no LFE)" );
1789 if( core_layout_description[CoreLayout] )
1790 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (%s)\n", CoreLayout, core_layout_description[CoreLayout] );
1791 else
1792 lsmash_ifprintf( fp, indent, "CoreLayout = %"PRIu8" (Undefined)\n", CoreLayout );
1793 if( CoreSize )
1794 lsmash_ifprintf( fp, indent, "CoreSize = %"PRIu16"\n", CoreSize );
1795 else
1796 lsmash_ifprintf( fp, indent, "CoreSize = 0 (no core substream exists)\n" );
1797 lsmash_ifprintf( fp, indent, "StereoDownmix = %s\n", StereoDownmix ? "1 (embedded downmix present)" : "0 (no embedded downmix)" );
1798 lsmash_ifprintf( fp, indent, "RepresentationType = %"PRIu8" (%s)\n", RepresentationType, representation_type_description[RepresentationType] );
1799 lsmash_ifprintf( fp, indent, "ChannelLayout = 0x%04"PRIx16"\n", ChannelLayout );
1800 if( ChannelLayout )
1801 for( int i = 0; i < 16; i++ )
1802 if( (ChannelLayout >> i) & 0x01 )
1803 lsmash_ifprintf( fp, indent + 1, "%s\n", channel_layout_description[i] );
1804 lsmash_ifprintf( fp, indent, "MultiAssetFlag = %s\n", MultiAssetFlag ? "1 (multiple asset)" : "0 (single asset)" );
1805 if( LBRDurationMod )
1806 lsmash_ifprintf( fp, indent, "LBRDurationMod = 1 (%"PRIu32" -> %"PRIu32" samples)\n", frame_duration, (frame_duration * 3) / 2 );
1807 else
1808 lsmash_ifprintf( fp, indent, "LBRDurationMod = 0 (no LBR duration modifier)\n" );
1809 lsmash_ifprintf( fp, indent, "ReservedBoxPresent = %s\n", ReservedBoxPresent ? "1 (ReservedBox present)" : "0 (no ReservedBox)" );
1810 lsmash_ifprintf( fp, indent, "Reserved = 0x%02"PRIx8"\n", Reserved );
1811 return 0;
1814 int dts_update_bitrate( isom_stbl_t *stbl, isom_mdhd_t *mdhd, uint32_t sample_description_index )
1816 isom_audio_entry_t *dts_audio = (isom_audio_entry_t *)lsmash_list_get_entry_data( &stbl->stsd->list, sample_description_index );
1817 if( LSMASH_IS_NON_EXISTING_BOX( dts_audio ) )
1818 return LSMASH_ERR_INVALID_DATA;
1819 isom_box_t *ext = isom_get_extension_box( &dts_audio->extensions, ISOM_BOX_TYPE_DDTS );
1820 if( !((ext->manager & LSMASH_BINARY_CODED_BOX) && ext->binary && ext->size >= 28) )
1821 return LSMASH_ERR_INVALID_DATA;
1822 uint32_t bufferSizeDB;
1823 uint32_t maxBitrate;
1824 uint32_t avgBitrate;
1825 int err = isom_calculate_bitrate_description( stbl, mdhd, &bufferSizeDB, &maxBitrate, &avgBitrate, sample_description_index );
1826 if( err < 0 )
1827 return err;
1828 if( !isom_is_variable_size( stbl ) )
1829 maxBitrate = avgBitrate;
1830 uint8_t *exdata = ext->binary + 12;
1831 LSMASH_SET_BE32( &exdata[0], maxBitrate );
1832 LSMASH_SET_BE32( &exdata[4], avgBitrate );
1833 return 0;