msvc: Add a missing file.
[L-SMASH.git] / codecs / a52.c
blob0c34209817bd7bc0b5f99c65b0a183528624dc75
1 /*****************************************************************************
2 * a52.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 static const char *bit_stream_mode[] =
33 "Main audio service: complete main (CM)",
34 "Main audio service: music and effects (ME)",
35 "Associated service: visually impaired (VI)",
36 "Associated service: hearing impaired (HI)",
37 "Associated service: dialogue (D)",
38 "Associated service: commentary (C)",
39 "Associated service: emergency (E)",
40 "Undefined service",
41 "Associated service: voice over (VO)", /* only if acmod == 0b001 */
42 "Main audio service: karaoke"
45 /* For karaoke mode, C->M, S->V1, SL->V1 and SR->V2. */
46 static const char *audio_coding_mode[] =
48 "1 + 1: Dual mono",
49 "1/0: C",
50 "2/0: L, R",
51 "3/0: L, C, R",
52 "2/1: L, R, S",
53 "3/1: L, C, R, S",
54 "2/2: L, R, SL, SR",
55 "3/2: L, C, R, SL, SR",
56 "Undefined audio coding mode",
57 "Undefined audio coding mode",
58 "2/0: L, R",
59 "3/0: L, M, R",
60 "2/1: L, R, V1",
61 "3/1: L, M, R, V1",
62 "2/2: L, R, V1, V2",
63 "3/2: L, M, R, V1, V2"
66 /***************************************************************************
67 AC-3 tools
68 ETSI TS 102 366 V1.2.1 (2008-08)
69 ***************************************************************************/
70 #include "a52.h"
72 #define AC3_SPECIFIC_BOX_LENGTH 11
74 uint32_t ac3_get_sample_rate( lsmash_ac3_specific_parameters_t *dac3_param )
76 /* The value 3 (or 0b11) of fscod is reserved. */
77 uint32_t samplerate = ac3_sample_rate_table[ dac3_param->fscod ];
78 if( samplerate == 0 )
79 lsmash_log( NULL, LSMASH_LOG_WARNING, "Unknown sampling rate is detected.\n" );
80 return samplerate;
83 #if 0
84 /* FIXME: though this table is for AC-3 in QTFF, we don't support it yet since the structure of CODEC specific info is unknown.
85 * ChannelLayout is given by ac3_channel_layout_table[ acmod ][ lfeon ]. */
86 static const lsmash_channel_layout_tag ac3_channel_layout_table[8][2] =
88 /* LFE: off LFE: on */
89 { QT_CHANNEL_LAYOUT_UNKNOWN, QT_CHANNEL_LAYOUT_UNKNOWN }, /* FIXME: dual mono */
90 { QT_CHANNEL_LAYOUT_MONO, QT_CHANNEL_LAYOUT_AC3_1_0_1 },
91 { QT_CHANNEL_LAYOUT_STEREO, QT_CHANNEL_LAYOUT_DVD_4 },
92 { QT_CHANNEL_LAYOUT_AC3_3_0, QT_CHANNEL_LAYOUT_AC3_3_0_1 },
93 { QT_CHANNEL_LAYOUT_DVD_2, QT_CHANNEL_LAYOUT_AC3_2_1_1 },
94 { QT_CHANNEL_LAYOUT_AC3_3_1, QT_CHANNEL_LAYOUT_AC3_3_1_1 },
95 { QT_CHANNEL_LAYOUT_DVD_3, QT_CHANNEL_LAYOUT_DVD_18 },
96 { QT_CHANNEL_LAYOUT_MPEG_5_0_C, QT_CHANNEL_LAYOUT_MPEG_5_1_C }
98 #endif
100 uint8_t *lsmash_create_ac3_specific_info( lsmash_ac3_specific_parameters_t *param, uint32_t *data_length )
102 lsmash_bits_t bits = { 0 };
103 lsmash_bs_t bs = { 0 };
104 lsmash_bits_init( &bits, &bs );
105 uint8_t buffer[AC3_SPECIFIC_BOX_LENGTH] = { 0 };
106 bs.buffer.data = buffer;
107 bs.buffer.alloc = AC3_SPECIFIC_BOX_LENGTH;
108 lsmash_bits_put( &bits, 32, AC3_SPECIFIC_BOX_LENGTH ); /* box size */
109 lsmash_bits_put( &bits, 32, ISOM_BOX_TYPE_DAC3.fourcc ); /* box type: 'dac3' */
110 lsmash_bits_put( &bits, 2, param->fscod );
111 lsmash_bits_put( &bits, 5, param->bsid );
112 lsmash_bits_put( &bits, 3, param->bsmod );
113 lsmash_bits_put( &bits, 3, param->acmod );
114 lsmash_bits_put( &bits, 1, param->lfeon );
115 lsmash_bits_put( &bits, 5, param->frmsizecod >> 1 );
116 lsmash_bits_put( &bits, 5, 0 );
117 uint8_t *data = lsmash_bits_export_data( &bits, data_length );
118 lsmash_bits_empty( &bits );
119 return data;
122 int lsmash_setup_ac3_specific_parameters_from_syncframe( lsmash_ac3_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
124 if( !data || data_length < AC3_MIN_SYNCFRAME_LENGTH )
125 return LSMASH_ERR_FUNCTION_PARAM;
126 /* Check a syncword. */
127 if( data[0] != 0x0b
128 || data[1] != 0x77 )
129 return LSMASH_ERR_INVALID_DATA;
130 lsmash_bits_t bits = { 0 };
131 lsmash_bs_t bs = { 0 };
132 uint8_t buffer[AC3_MAX_SYNCFRAME_LENGTH] = { 0 };
133 bs.buffer.data = buffer;
134 bs.buffer.store = data_length;
135 bs.buffer.alloc = AC3_MAX_SYNCFRAME_LENGTH;
136 ac3_info_t info = { .bits = &bits };
137 lsmash_bits_init( &bits, &bs );
138 memcpy( buffer, data, LSMASH_MIN( data_length, AC3_MAX_SYNCFRAME_LENGTH ) );
139 int err = ac3_parse_syncframe_header( &info );
140 if( err < 0 )
141 return err;
142 *param = info.dac3_param;
143 return 0;
146 static int ac3_check_syncframe_header( lsmash_ac3_specific_parameters_t *param )
148 if( param->fscod == 0x3 )
149 return LSMASH_ERR_INVALID_DATA; /* unknown Sample Rate Code */
150 if( param->frmsizecod > 0x25 )
151 return LSMASH_ERR_INVALID_DATA; /* unknown Frame Size Code */
152 if( param->bsid >= 10 )
153 return LSMASH_ERR_INVALID_DATA; /* might be EAC-3 */
154 return 0;
157 int ac3_parse_syncframe_header( ac3_info_t *info )
159 lsmash_bits_t *bits = info->bits;
160 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
161 lsmash_bits_get( bits, 32 ); /* syncword + crc1 */
162 param->fscod = lsmash_bits_get( bits, 2 );
163 param->frmsizecod = lsmash_bits_get( bits, 6 );
164 param->bsid = lsmash_bits_get( bits, 5 );
165 param->bsmod = lsmash_bits_get( bits, 3 );
166 param->acmod = lsmash_bits_get( bits, 3 );
167 if( (param->acmod & 0x01) && (param->acmod != 0x01) )
168 lsmash_bits_get( bits, 2 ); /* cmixlev */
169 if( param->acmod & 0x04 )
170 lsmash_bits_get( bits, 2 ); /* surmixlev */
171 if( param->acmod == 0x02 )
172 lsmash_bits_get( bits, 2 ); /* dsurmod */
173 param->lfeon = lsmash_bits_get( bits, 1 );
174 lsmash_bits_get_align( bits );
175 return ac3_check_syncframe_header( param );
178 int ac3_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
180 assert( dst && dst->data.structured && src && src->data.unstructured );
181 if( src->size < AC3_SPECIFIC_BOX_LENGTH )
182 return LSMASH_ERR_INVALID_DATA;
183 lsmash_ac3_specific_parameters_t *param = (lsmash_ac3_specific_parameters_t *)dst->data.structured;
184 uint8_t *data = src->data.unstructured;
185 uint64_t size = LSMASH_GET_BE32( data );
186 data += ISOM_BASEBOX_COMMON_SIZE;
187 if( size == 1 )
189 size = LSMASH_GET_BE64( data );
190 data += 8;
192 if( size != src->size )
193 return LSMASH_ERR_INVALID_DATA;
194 param->fscod = (data[0] >> 6) & 0x03; /* XXxx xxxx xxxx xxxx xxxx xxxx */
195 param->bsid = (data[0] >> 1) & 0x1F; /* xxXX XXXx xxxx xxxx xxxx xxxx */
196 param->bsmod = ((data[0] & 0x01) << 2) | ((data[2] >> 6) & 0x03); /* xxxx xxxX XXxx xxxx xxxx xxxx */
197 param->acmod = (data[1] >> 3) & 0x07; /* xxxx xxxx xxXX Xxxx xxxx xxxx */
198 param->lfeon = (data[1] >> 2) & 0x01; /* xxxx xxxx xxxx xXxx xxxx xxxx */
199 param->frmsizecod = ((data[1] & 0x03) << 3) | ((data[3] >> 5) & 0x07); /* xxxx xxxx xxxx xxXX XXXx xxxx */
200 param->frmsizecod <<= 1;
201 return 0;
204 int ac3_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
206 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
207 int indent = level;
208 lsmash_ifprintf( fp, indent++, "[%s: AC3 Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
209 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
210 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
211 if( box->size < AC3_SPECIFIC_BOX_LENGTH )
212 return LSMASH_ERR_INVALID_DATA;
213 uint8_t *data = box->binary;
214 isom_skip_box_common( &data );
215 uint8_t fscod = (data[0] >> 6) & 0x03;
216 uint8_t bsid = (data[0] >> 1) & 0x1F;
217 uint8_t bsmod = ((data[0] & 0x01) << 2) | ((data[1] >> 6) & 0x03);
218 uint8_t acmod = (data[1] >> 3) & 0x07;
219 uint8_t lfeon = (data[1] >> 2) & 0x01;
220 uint8_t bit_rate_code = ((data[1] & 0x03) << 3) | ((data[2] >> 5) & 0x07);
221 if( fscod != 0x03 )
222 lsmash_ifprintf( fp, indent, "fscod = %"PRIu8" (%"PRIu32" Hz)\n", fscod, ac3_sample_rate_table[fscod] );
223 else
224 lsmash_ifprintf( fp, indent, "fscod = 0x03 (reserved)\n" );
225 lsmash_ifprintf( fp, indent, "bsid = %"PRIu8"\n", bsid );
226 lsmash_ifprintf( fp, indent, "bsmod = %"PRIu8" (%s)\n", bsmod, bit_stream_mode[bsmod + (acmod == 0x01 ? 1 : acmod > 0x01 ? 2 : 0)] );
227 lsmash_ifprintf( fp, indent, "acmod = %"PRIu8" (%s)\n", acmod, audio_coding_mode[acmod + (bsmod == 0x07 ? 8 : 0)] );
228 lsmash_ifprintf( fp, indent, "lfeon = %s\n", lfeon ? "1 (LFE)" : "0" );
229 static const uint32_t bit_rate[] =
231 32, 40, 48, 56, 64, 80, 96, 112, 128,
232 160, 192, 224, 256, 320, 384, 448, 512, 576, 640,
233 0 /* undefined */
235 lsmash_ifprintf( fp, indent, "bit_rate_code = 0x%02"PRIx8" (%"PRIu32" kbit/s)\n", bit_rate_code, bit_rate[bit_rate_code] );
236 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", data[2] & 0x1F );
237 return 0;
240 #undef AC3_SPECIFIC_BOX_LENGTH
242 /***************************************************************************
243 Enhanced AC-3 tools
244 ETSI TS 102 366 V1.2.1 (2008-08)
245 ***************************************************************************/
247 uint8_t *lsmash_create_eac3_specific_info( lsmash_eac3_specific_parameters_t *param, uint32_t *data_length )
249 #define EAC3_SPECIFIC_BOX_MAX_LENGTH 42
250 if( param->num_ind_sub > 7 )
251 return NULL;
252 lsmash_bits_t bits = { 0 };
253 lsmash_bs_t bs = { 0 };
254 lsmash_bits_init( &bits, &bs );
255 uint8_t buffer[EAC3_SPECIFIC_BOX_MAX_LENGTH] = { 0 };
256 bs.buffer.data = buffer;
257 bs.buffer.alloc = EAC3_SPECIFIC_BOX_MAX_LENGTH;
258 lsmash_bits_put( &bits, 32, 0 ); /* box size */
259 lsmash_bits_put( &bits, 32, ISOM_BOX_TYPE_DEC3.fourcc ); /* box type: 'dec3' */
260 lsmash_bits_put( &bits, 13, param->data_rate ); /* data_rate; setup by isom_update_bitrate_description */
261 lsmash_bits_put( &bits, 3, param->num_ind_sub );
262 /* Apparently, the condition of this loop defined in ETSI TS 102 366 V1.2.1 (2008-08) is wrong. */
263 for( int i = 0; i <= param->num_ind_sub; i++ )
265 lsmash_eac3_substream_info_t *independent_info = &param->independent_info[i];
266 lsmash_bits_put( &bits, 2, independent_info->fscod );
267 lsmash_bits_put( &bits, 5, independent_info->bsid );
268 lsmash_bits_put( &bits, 5, independent_info->bsmod );
269 lsmash_bits_put( &bits, 3, independent_info->acmod );
270 lsmash_bits_put( &bits, 1, independent_info->lfeon );
271 lsmash_bits_put( &bits, 3, 0 ); /* reserved */
272 lsmash_bits_put( &bits, 4, independent_info->num_dep_sub );
273 if( independent_info->num_dep_sub > 0 )
274 lsmash_bits_put( &bits, 9, independent_info->chan_loc );
275 else
276 lsmash_bits_put( &bits, 1, 0 ); /* reserved */
278 uint8_t *data = lsmash_bits_export_data( &bits, data_length );
279 lsmash_bits_empty( &bits );
280 /* Update box size. */
281 LSMASH_SET_BE32( data, *data_length );
282 return data;
283 #undef EAC3_SPECIFIC_BOX_MAX_LENGTH
286 /* Return -1 if incomplete Enhanced AC-3 sample is given. */
287 int lsmash_setup_eac3_specific_parameters_from_frame( lsmash_eac3_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
289 if( !data || data_length < 5 )
290 return LSMASH_ERR_FUNCTION_PARAM;
291 lsmash_bits_t bits = { 0 };
292 lsmash_bs_t bs = { 0 };
293 uint8_t buffer[EAC3_MAX_SYNCFRAME_LENGTH] = { 0 };
294 bs.buffer.data = buffer;
295 bs.buffer.store = data_length;
296 bs.buffer.alloc = EAC3_MAX_SYNCFRAME_LENGTH;
297 eac3_info_t *info = &(eac3_info_t){ .bits = &bits };
298 lsmash_bits_init( &bits, &bs );
299 memcpy( buffer, data, LSMASH_MIN( data_length, EAC3_MAX_SYNCFRAME_LENGTH ) );
300 uint64_t next_frame_pos = 0;
301 while( 1 )
303 /* Seek to the head of the next syncframe. */
304 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
305 /* Check the remainder length of the input data.
306 * If there is enough length, then parse the syncframe in it.
307 * The length 5 is the required byte length to get frame size. */
308 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
309 if( bs.eob || (bs.eof && remain_size < 5) )
310 goto setup_param; /* No more valid data. */
311 /* Check the syncword. */
312 if( lsmash_bs_show_byte( &bs, 0 ) != 0x0b
313 || lsmash_bs_show_byte( &bs, 1 ) != 0x77 )
314 goto setup_param;
315 /* Parse syncframe. */
316 info->frame_size = 0;
317 if( eac3_parse_syncframe( info ) < 0 )
318 goto setup_param;
319 if( remain_size < info->frame_size )
320 goto setup_param;
321 int independent = info->strmtyp != 0x1;
322 if( independent && info->substreamid == 0x0 )
324 if( info->number_of_audio_blocks == 6 )
326 /* Encountered the first syncframe of the next access unit. */
327 info->number_of_audio_blocks = 0;
328 goto setup_param;
330 else if( info->number_of_audio_blocks > 6 )
331 goto setup_param;
332 info->number_of_audio_blocks += eac3_audio_block_table[ info->numblkscod ];
333 info->number_of_independent_substreams = 0;
335 else if( info->syncframe_count == 0 )
336 /* The first syncframe in an AU must be independent and assigned substream ID 0. */
337 return LSMASH_ERR_INVALID_DATA;
338 if( independent )
339 info->independent_info[info->number_of_independent_substreams ++].num_dep_sub = 0;
340 else
341 ++ info->independent_info[info->number_of_independent_substreams - 1].num_dep_sub;
342 next_frame_pos += info->frame_size;
343 ++ info->syncframe_count;
345 setup_param:
346 if( info->number_of_independent_substreams == 0 || info->number_of_independent_substreams > 8 )
347 return LSMASH_ERR_INVALID_DATA;
348 if( !info->dec3_param_initialized )
349 eac3_update_specific_param( info );
350 *param = info->dec3_param;
351 return info->number_of_audio_blocks == 6 ? 0 : LSMASH_ERR_INVALID_DATA;
354 uint16_t lsmash_eac3_get_chan_loc_from_chanmap( uint16_t chanmap )
356 return ((chanmap & 0x7f8) >> 2) | ((chanmap & 0x2) >> 1);
359 static int eac3_check_syncframe_header( eac3_info_t *info )
361 if( info->strmtyp == 0x3 )
362 return LSMASH_ERR_INVALID_DATA; /* unknown Stream type */
363 lsmash_eac3_substream_info_t *substream_info;
364 if( info->strmtyp != 0x1 )
365 substream_info = &info->independent_info[ info->current_independent_substream_id ];
366 else
367 substream_info = &info->dependent_info;
368 if( substream_info->fscod == 0x3 && info->fscod2 == 0x3 )
369 return LSMASH_ERR_INVALID_DATA; /* unknown Sample Rate Code */
370 if( substream_info->bsid < 10 || substream_info->bsid > 16 )
371 return LSMASH_ERR_INVALID_DATA; /* not EAC-3 */
372 return 0;
375 int eac3_parse_syncframe( eac3_info_t *info )
377 lsmash_bits_t *bits = info->bits;
378 lsmash_bits_get( bits, 16 ); /* syncword (16) */
379 info->strmtyp = lsmash_bits_get( bits, 2 ); /* strmtyp (2) */
380 info->substreamid = lsmash_bits_get( bits, 3 ); /* substreamid (3) */
381 lsmash_eac3_substream_info_t *substream_info;
382 if( info->strmtyp != 0x1 )
384 if( info->substreamid == 0x0 && info->number_of_independent_substreams )
385 eac3_update_specific_param( info );
386 info->current_independent_substream_id = info->substreamid;
387 substream_info = &info->independent_info[ info->current_independent_substream_id ];
388 substream_info->chan_loc = 0;
390 else
391 substream_info = &info->dependent_info;
392 info->frame_size = 2 * (lsmash_bits_get( bits, 11 ) + 1); /* frmsiz (11) */
393 substream_info->fscod = lsmash_bits_get( bits, 2 ); /* fscod (2) */
394 if( substream_info->fscod == 0x3 )
396 info->fscod2 = lsmash_bits_get( bits, 2 ); /* fscod2 (2) */
397 info->numblkscod = 0x3;
399 else
400 info->numblkscod = lsmash_bits_get( bits, 2 ); /* numblkscod (2) */
401 substream_info->acmod = lsmash_bits_get( bits, 3 ); /* acmod (3) */
402 substream_info->lfeon = lsmash_bits_get( bits, 1 ); /* lfeon (1) */
403 substream_info->bsid = lsmash_bits_get( bits, 5 ); /* bsid (5) */
404 lsmash_bits_get( bits, 5 ); /* dialnorm (5) */
405 if( lsmash_bits_get( bits, 1 ) ) /* compre (1) */
406 lsmash_bits_get( bits, 8 ); /* compr (8) */
407 if( substream_info->acmod == 0x0 )
409 lsmash_bits_get( bits, 5 ); /* dialnorm2 (5) */
410 if( lsmash_bits_get( bits, 1 ) ) /* compre2 (1) */
411 lsmash_bits_get( bits, 8 ); /* compr2 (8) */
413 if( info->strmtyp == 0x1 && lsmash_bits_get( bits, 1 ) ) /* chanmape (1) */
415 uint16_t chanmap = lsmash_bits_get( bits, 16 ); /* chanmap (16) */
416 info->independent_info[ info->current_independent_substream_id ].chan_loc |= lsmash_eac3_get_chan_loc_from_chanmap( chanmap );
418 if( lsmash_bits_get( bits, 1 ) ) /* mixmdate (1) */
420 if( substream_info->acmod > 0x2 )
421 lsmash_bits_get( bits, 2 ); /* dmixmod (2) */
422 if( ((substream_info->acmod & 0x1) && (substream_info->acmod > 0x2)) || (substream_info->acmod & 0x4) )
423 lsmash_bits_get( bits, 6 ); /* ltrt[c/sur]mixlev (3)
424 * loro[c/sur]mixlev (3) */
425 if( substream_info->lfeon && lsmash_bits_get( bits, 1 ) ) /* lfemixlevcode (1) */
426 lsmash_bits_get( bits, 5 ); /* lfemixlevcod (5) */
427 if( info->strmtyp == 0x0 )
429 if( lsmash_bits_get( bits, 1 ) ) /* pgmscle (1) */
430 lsmash_bits_get( bits, 6 ); /* pgmscl (6) */
431 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* pgmscle2 (1) */
432 lsmash_bits_get( bits, 6 ); /* pgmscl2 (6) */
433 if( lsmash_bits_get( bits, 1 ) ) /* extpgmscle (1) */
434 lsmash_bits_get( bits, 6 ); /* extpgmscl (6) */
435 uint8_t mixdef = lsmash_bits_get( bits, 2 ); /* mixdef (2) */
436 if( mixdef == 0x1 )
437 lsmash_bits_get( bits, 5 ); /* premixcmpsel (1)
438 * drcsrc (1)
439 * premixcmpscl (3) */
440 else if( mixdef == 0x2 )
441 lsmash_bits_get( bits, 12 ); /* mixdata (12) */
442 else if( mixdef == 0x3 )
444 uint8_t mixdeflen = lsmash_bits_get( bits, 5 ); /* mixdeflen (5) */
445 lsmash_bits_get( bits, 8 * (mixdeflen + 2) ); /* mixdata (8 * (mixdeflen + 2))
446 * mixdatafill (0-7) */
448 if( substream_info->acmod < 0x2 )
450 if( lsmash_bits_get( bits, 1 ) ) /* paninfoe (1) */
451 lsmash_bits_get( bits, 14 ); /* panmean (8)
452 * paninfo (6) */
453 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* paninfo2e (1) */
454 lsmash_bits_get( bits, 14 ); /* panmean2 (8)
455 * paninfo2 (6) */
457 if( lsmash_bits_get( bits, 1 ) ) /* frmmixcfginfoe (1) */
459 if( info->numblkscod == 0x0 )
460 lsmash_bits_get( bits, 5 ); /* blkmixcfginfo[0] (5) */
461 else
463 int number_of_blocks_per_syncframe = ((int []){ 1, 2, 3, 6 })[ info->numblkscod ];
464 for( int blk = 0; blk < number_of_blocks_per_syncframe; blk++ )
465 if( lsmash_bits_get( bits, 1 ) ) /* blkmixcfginfoe (1)*/
466 lsmash_bits_get( bits, 5 ); /* blkmixcfginfo[blk] (5) */
471 if( lsmash_bits_get( bits, 1 ) ) /* infomdate (1) */
473 substream_info->bsmod = lsmash_bits_get( bits, 3 ); /* bsmod (3) */
474 lsmash_bits_get( bits, 1 ); /* copyrightb (1) */
475 lsmash_bits_get( bits, 1 ); /* origbs (1) */
476 if( substream_info->acmod == 0x2 )
477 lsmash_bits_get( bits, 4 ); /* dsurmod (2)
478 * dheadphonmod (2) */
479 else if( substream_info->acmod >= 0x6 )
480 lsmash_bits_get( bits, 2 ); /* dsurexmod (2) */
481 if( lsmash_bits_get( bits, 1 ) ) /* audprodie (1) */
482 lsmash_bits_get( bits, 8 ); /* mixlevel (5)
483 * roomtyp (2)
484 * adconvtyp (1) */
485 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* audprodie2 (1) */
486 lsmash_bits_get( bits, 8 ); /* mixlevel2 (5)
487 * roomtyp2 (2)
488 * adconvtyp2 (1) */
489 if( substream_info->fscod < 0x3 )
490 lsmash_bits_get( bits, 1 ); /* sourcefscod (1) */
492 else
493 substream_info->bsmod = 0;
494 if( info->strmtyp == 0x0 && info->numblkscod != 0x3 )
495 lsmash_bits_get( bits, 1 ); /* convsync (1) */
496 if( info->strmtyp == 0x2 )
498 int blkid;
499 if( info->numblkscod == 0x3 )
500 blkid = 1;
501 else
502 blkid = lsmash_bits_get( bits, 1 ); /* blkid (1) */
503 if( blkid )
504 lsmash_bits_get( bits, 6 ); /* frmsizecod (6) */
506 if( lsmash_bits_get( bits, 1 ) ) /* addbsie (1) */
508 uint8_t addbsil = lsmash_bits_get( bits, 6 ); /* addbsil (6) */
509 lsmash_bits_get( bits, (addbsil + 1) * 8 ); /* addbsi ((addbsil + 1) * 8) */
511 lsmash_bits_get_align( bits );
512 return eac3_check_syncframe_header( info );
515 void eac3_update_specific_param( eac3_info_t *info )
517 lsmash_eac3_specific_parameters_t *param = &info->dec3_param;
518 param->data_rate = 0;
519 param->num_ind_sub = info->number_of_independent_substreams - 1;
520 for( uint8_t i = 0; i <= param->num_ind_sub; i++ )
521 param->independent_info[i] = info->independent_info[i];
522 info->dec3_param_initialized = 1;
525 #define EAC3_SPECIFIC_BOX_MIN_LENGTH 13
527 int eac3_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
529 assert( dst && dst->data.structured && src && src->data.unstructured );
530 if( src->size < EAC3_SPECIFIC_BOX_MIN_LENGTH )
531 return LSMASH_ERR_INVALID_DATA;
532 lsmash_eac3_specific_parameters_t *param = (lsmash_eac3_specific_parameters_t *)dst->data.structured;
533 uint8_t *data = src->data.unstructured;
534 uint64_t size = LSMASH_GET_BE32( data );
535 data += ISOM_BASEBOX_COMMON_SIZE;
536 if( size == 1 )
538 size = LSMASH_GET_BE64( data );
539 data += 8;
541 if( size != src->size )
542 return LSMASH_ERR_INVALID_DATA;
543 param->data_rate = (data[0] << 5) | ((data[1] >> 3) & 0x1F); /* XXXX XXXX XXXX Xxxx */
544 param->num_ind_sub = data[1] & 0x07; /* xxxx xxxx xxxx xXXX */
545 data += 2;
546 size -= 2;
547 for( int i = 0; i <= param->num_ind_sub; i++ )
549 if( size < 3 )
550 return LSMASH_ERR_INVALID_DATA;
551 lsmash_eac3_substream_info_t *independent_info = &param->independent_info[i];
552 independent_info->fscod = (data[0] >> 6) & 0x03; /* XXxx xxxx xxxx xxxx xxxx xxxx */
553 independent_info->bsid = (data[0] >> 1) & 0x1F; /* xxXX XXXx xxxx xxxx xxxx xxxx */
554 independent_info->bsmod = ((data[0] & 0x01) << 4) | ((data[1] >> 4) & 0x0F); /* xxxx xxxX XXXX xxxx xxxx xxxx */
555 independent_info->acmod = (data[1] >> 1) & 0x07; /* xxxx xxxx xxxx XXXx xxxx xxxx */
556 independent_info->lfeon = data[1] & 0x01; /* xxxx xxxx xxxx xxxX xxxx xxxx */
557 independent_info->num_dep_sub = (data[2] >> 1) & 0x0F; /* xxxx xxxx xxxx xxxx xxxX XXXx */
558 data += 3;
559 size -= 3;
560 if( independent_info->num_dep_sub > 0 )
562 if( size < 1 )
563 return LSMASH_ERR_INVALID_DATA;
564 independent_info->chan_loc = ((data[-1] & 0x01) << 8) | data[0]; /* xxxx xxxX XXXX XXXX */
565 data += 1;
566 size -= 1;
569 return 0;
572 void eac3_update_sample_rate
574 uint32_t *frequency,
575 lsmash_eac3_specific_parameters_t *dec3_param,
576 uint8_t *fscod2
579 /* Additional independent substreams 1 to 7 must be encoded at the same sample rate as independent substream 0. */
580 uint32_t samplerate = ac3_sample_rate_table[ dec3_param->independent_info[0].fscod ];
581 if( samplerate == 0 && fscod2 )
582 /* The value 3 (or 0b11) of fscod2 is reserved. */
583 samplerate = ac3_sample_rate_table[ *fscod2 ] / 2;
584 if( samplerate != 0 )
585 *frequency = samplerate;
586 else
587 lsmash_log( NULL, LSMASH_LOG_WARNING, "Unknown sampling rate is detected.\n" );
590 #if 0
591 /* FIXME: though this table is for EAC-3 in QTFF, we don't support it yet since the structure of CODEC specific info is unknown. */
592 static void eac3_update_channel_layout
594 lsmash_channel_layout_tag *layout_tag,
595 lsmash_eac3_substream_info_t *independent_info
598 if( independent_info->chan_loc == 0 )
600 *layout_tag = ac3_channel_layout_table[ independent_info->acmod ][ independent_info->lfeon ];
601 return;
603 else if( independent_info->acmod != 0x7 )
605 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
606 return;
608 /* OK. All L, C, R, Ls and Rs exsist. */
609 if( !independent_info->lfeon )
611 if( independent_info->chan_loc == 0x80 )
612 *layout_tag = QT_CHANNEL_LAYOUT_EAC_7_0_A;
613 else if( independent_info->chan_loc == 0x40 )
614 *layout_tag = QT_CHANNEL_LAYOUT_EAC_6_0_A;
615 else
616 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
617 return;
619 /* Also LFE exsists. */
620 static const struct
622 uint16_t chan_loc;
623 lsmash_channel_layout_tag tag;
624 } eac3_channel_layout_table[]
626 { 0x100, QT_CHANNEL_LAYOUT_EAC3_7_1_B },
627 { 0x80, QT_CHANNEL_LAYOUT_EAC3_7_1_A },
628 { 0x40, QT_CHANNEL_LAYOUT_EAC3_6_1_A },
629 { 0x20, QT_CHANNEL_LAYOUT_EAC3_6_1_B },
630 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_C },
631 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_D },
632 { 0x4, QT_CHANNEL_LAYOUT_EAC3_7_1_E },
633 { 0x2, QT_CHANNEL_LAYOUT_EAC3_6_1_C },
634 { 0x60, QT_CHANNEL_LAYOUT_EAC3_7_1_F },
635 { 0x42, QT_CHANNEL_LAYOUT_EAC3_7_1_G },
636 { 0x22, QT_CHANNEL_LAYOUT_EAC3_7_1_H },
637 { 0 }
639 for( int i = 0; eac3_channel_layout_table[i].chan_loc; i++ )
640 if( independent_info->chan_loc == eac3_channel_layout_table[i].chan_loc )
642 *layout_tag = eac3_channel_layout_table[i].tag;
643 return;
645 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
647 #endif
649 void eac3_update_channel_count
651 uint32_t *channels,
652 lsmash_eac3_specific_parameters_t *dec3_param
655 /* The default programme selection should always be Programme 1.
656 * Thus, pick the number of channels of Programme 1. */
657 lsmash_eac3_substream_info_t *independent_info = &dec3_param->independent_info[0];
658 *channels = ac3_channel_count_table[ independent_info->acmod ] /* L/C/R/Ls/Rs combination */
659 + 2 * !!(independent_info->chan_loc & 0x100) /* Lc/Rc pair */
660 + 2 * !!(independent_info->chan_loc & 0x80) /* Lrs/Rrs pair */
661 + !!(independent_info->chan_loc & 0x40) /* Cs */
662 + !!(independent_info->chan_loc & 0x20) /* Ts */
663 + 2 * !!(independent_info->chan_loc & 0x10) /* Lsd/Rsd pair */
664 + 2 * !!(independent_info->chan_loc & 0x8) /* Lw/Rw pair */
665 + 2 * !!(independent_info->chan_loc & 0x4) /* Lvh/Rvh pair */
666 + !!(independent_info->chan_loc & 0x2) /* Cvh */
667 + !!(independent_info->chan_loc & 0x1) /* LFE2 */
668 + independent_info->lfeon; /* LFE */
671 int eac3_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
673 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
674 int indent = level;
675 lsmash_ifprintf( fp, indent++, "[%s: EC3 Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
676 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
677 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
678 if( box->size < EAC3_SPECIFIC_BOX_MIN_LENGTH )
679 return LSMASH_ERR_INVALID_DATA;
680 uint8_t *data = box->binary;
681 isom_skip_box_common( &data );
682 lsmash_ifprintf( fp, indent, "data_rate = %"PRIu16" kbit/s\n", (data[0] << 5) | ((data[1] >> 3) & 0x1F) );
683 uint8_t num_ind_sub = data[1] & 0x07;
684 lsmash_ifprintf( fp, indent, "num_ind_sub = %"PRIu8"\n", num_ind_sub );
685 data += 2;
686 for( int i = 0; i <= num_ind_sub; i++ )
688 lsmash_ifprintf( fp, indent, "independent_substream[%d]\n", i );
689 int sub_indent = indent + 1;
690 uint8_t fscod = (data[0] >> 6) & 0x03;
691 uint8_t bsid = (data[0] >> 1) & 0x1F;
692 uint8_t bsmod = ((data[0] & 0x01) << 4) | ((data[1] >> 4) & 0x0F);
693 uint8_t acmod = (data[1] >> 1) & 0x07;
694 uint8_t lfeon = data[1] & 0x01;
695 uint8_t num_dep_sub = (data[2] >> 1) & 0x0F;
696 if( fscod != 0x03 )
697 lsmash_ifprintf( fp, sub_indent, "fscod = %"PRIu8" (%"PRIu32" Hz)\n", fscod, ac3_sample_rate_table[fscod] );
698 else
699 lsmash_ifprintf( fp, sub_indent, "fscod = 0x03 (reduced sample rate)\n" );
700 lsmash_ifprintf( fp, sub_indent, "bsid = %"PRIu8"\n", bsid );
701 if( bsmod < 0x08 )
702 lsmash_ifprintf( fp, sub_indent, "bsmod = %"PRIu8" (%s)\n", bsmod, bit_stream_mode[bsmod + (acmod == 0x01 ? 1 : acmod > 0x01 ? 2 : 0)] );
703 else
704 lsmash_ifprintf( fp, sub_indent, "bsmod = %"PRIu8" (Undefined service)\n" );
705 lsmash_ifprintf( fp, sub_indent, "acmod = %"PRIu8" (%s)\n", acmod, audio_coding_mode[acmod + (bsmod == 0x07 ? 8 : 0)] );
706 lsmash_ifprintf( fp, sub_indent, "lfeon = %s\n", lfeon ? "1 (LFE)" : "0" );
707 lsmash_ifprintf( fp, sub_indent, "num_dep_sub = %"PRIu8"\n", num_dep_sub );
708 data += 3;
709 if( num_dep_sub > 0 )
711 static const char *channel_location[] =
713 "LFE2",
714 "Cvh",
715 "Lvh/Rvh pair",
716 "Lw/Rw pair",
717 "Lsd/Rsd pair",
718 "Ts",
719 "Cs",
720 "Lrs/Rrs pair",
721 "Lc/Rc pair"
723 uint16_t chan_loc = ((data[-1] & 0x01) << 8) | data[0];
724 lsmash_ifprintf( fp, sub_indent, "chan_loc = 0x%04"PRIx16"\n", chan_loc );
725 for( int j = 0; j < 9; j++ )
726 if( (chan_loc >> j & 0x01) )
727 lsmash_ifprintf( fp, sub_indent + 1, "%s\n", channel_location[j] );
728 data += 1;
730 else
731 lsmash_ifprintf( fp, sub_indent, "reserved = %"PRIu8"\n", data[2] & 0x01 );
733 return 0;
736 #undef EAC3_SPECIFIC_BOX_MIN_LENGTH