isom: Evaluate return value properly.
[L-SMASH.git] / codecs / a52.c
blobe46fa38485547a928086659417db9ab927a3a538
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 -1;
126 /* Check a syncword. */
127 if( data[0] != 0x0b
128 || data[1] != 0x77 )
129 return -1;
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 if( ac3_parse_syncframe_header( &info ) < 0 )
140 return -1;
141 *param = info.dac3_param;
142 return 0;
145 static int ac3_check_syncframe_header( lsmash_ac3_specific_parameters_t *param )
147 if( param->fscod == 0x3 )
148 return -1; /* unknown Sample Rate Code */
149 if( param->frmsizecod > 0x25 )
150 return -1; /* unknown Frame Size Code */
151 if( param->bsid >= 10 )
152 return -1; /* might be EAC-3 */
153 return 0;
156 int ac3_parse_syncframe_header( ac3_info_t *info )
158 lsmash_bits_t *bits = info->bits;
159 lsmash_ac3_specific_parameters_t *param = &info->dac3_param;
160 lsmash_bits_get( bits, 32 ); /* syncword + crc1 */
161 param->fscod = lsmash_bits_get( bits, 2 );
162 param->frmsizecod = lsmash_bits_get( bits, 6 );
163 param->bsid = lsmash_bits_get( bits, 5 );
164 param->bsmod = lsmash_bits_get( bits, 3 );
165 param->acmod = lsmash_bits_get( bits, 3 );
166 if( (param->acmod & 0x01) && (param->acmod != 0x01) )
167 lsmash_bits_get( bits, 2 ); /* cmixlev */
168 if( param->acmod & 0x04 )
169 lsmash_bits_get( bits, 2 ); /* surmixlev */
170 if( param->acmod == 0x02 )
171 lsmash_bits_get( bits, 2 ); /* dsurmod */
172 param->lfeon = lsmash_bits_get( bits, 1 );
173 lsmash_bits_get_align( bits );
174 return ac3_check_syncframe_header( param );
177 int ac3_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
179 assert( dst && dst->data.structured && src && src->data.unstructured );
180 if( src->size < AC3_SPECIFIC_BOX_LENGTH )
181 return -1;
182 lsmash_ac3_specific_parameters_t *param = (lsmash_ac3_specific_parameters_t *)dst->data.structured;
183 uint8_t *data = src->data.unstructured;
184 uint64_t size = LSMASH_GET_BE32( data );
185 data += ISOM_BASEBOX_COMMON_SIZE;
186 if( size == 1 )
188 size = LSMASH_GET_BE64( data );
189 data += 8;
191 if( size != src->size )
192 return -1;
193 param->fscod = (data[0] >> 6) & 0x03; /* XXxx xxxx xxxx xxxx xxxx xxxx */
194 param->bsid = (data[0] >> 1) & 0x1F; /* xxXX XXXx xxxx xxxx xxxx xxxx */
195 param->bsmod = ((data[0] & 0x01) << 2) | ((data[2] >> 6) & 0x03); /* xxxx xxxX XXxx xxxx xxxx xxxx */
196 param->acmod = (data[1] >> 3) & 0x07; /* xxxx xxxx xxXX Xxxx xxxx xxxx */
197 param->lfeon = (data[1] >> 2) & 0x01; /* xxxx xxxx xxxx xXxx xxxx xxxx */
198 param->frmsizecod = ((data[1] & 0x03) << 3) | ((data[3] >> 5) & 0x07); /* xxxx xxxx xxxx xxXX XXXx xxxx */
199 param->frmsizecod <<= 1;
200 return 0;
203 int ac3_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
205 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
206 int indent = level;
207 lsmash_ifprintf( fp, indent++, "[%s: AC3 Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
208 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
209 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
210 if( box->size < AC3_SPECIFIC_BOX_LENGTH )
211 return -1;
212 uint8_t *data = box->binary;
213 isom_skip_box_common( &data );
214 uint8_t fscod = (data[0] >> 6) & 0x03;
215 uint8_t bsid = (data[0] >> 1) & 0x1F;
216 uint8_t bsmod = ((data[0] & 0x01) << 2) | ((data[1] >> 6) & 0x03);
217 uint8_t acmod = (data[1] >> 3) & 0x07;
218 uint8_t lfeon = (data[1] >> 2) & 0x01;
219 uint8_t bit_rate_code = ((data[1] & 0x03) << 3) | ((data[2] >> 5) & 0x07);
220 if( fscod != 0x03 )
221 lsmash_ifprintf( fp, indent, "fscod = %"PRIu8" (%"PRIu32" Hz)\n", fscod, ac3_sample_rate_table[fscod] );
222 else
223 lsmash_ifprintf( fp, indent, "fscod = 0x03 (reserved)\n" );
224 lsmash_ifprintf( fp, indent, "bsid = %"PRIu8"\n", bsid );
225 lsmash_ifprintf( fp, indent, "bsmod = %"PRIu8" (%s)\n", bsmod, bit_stream_mode[bsmod + (acmod == 0x01 ? 1 : acmod > 0x01 ? 2 : 0)] );
226 lsmash_ifprintf( fp, indent, "acmod = %"PRIu8" (%s)\n", acmod, audio_coding_mode[acmod + (bsmod == 0x07 ? 8 : 0)] );
227 lsmash_ifprintf( fp, indent, "lfeon = %s\n", lfeon ? "1 (LFE)" : "0" );
228 static const uint32_t bit_rate[] =
230 32, 40, 48, 56, 64, 80, 96, 112, 128,
231 160, 192, 224, 256, 320, 384, 448, 512, 576, 640,
232 0 /* undefined */
234 lsmash_ifprintf( fp, indent, "bit_rate_code = 0x%02"PRIx8" (%"PRIu32" kbit/s)\n", bit_rate_code, bit_rate[bit_rate_code] );
235 lsmash_ifprintf( fp, indent, "reserved = 0x%02"PRIx8"\n", data[2] & 0x1F );
236 return 0;
239 #undef AC3_SPECIFIC_BOX_LENGTH
241 /***************************************************************************
242 Enhanced AC-3 tools
243 ETSI TS 102 366 V1.2.1 (2008-08)
244 ***************************************************************************/
246 uint8_t *lsmash_create_eac3_specific_info( lsmash_eac3_specific_parameters_t *param, uint32_t *data_length )
248 #define EAC3_SPECIFIC_BOX_MAX_LENGTH 42
249 if( param->num_ind_sub > 7 )
250 return NULL;
251 lsmash_bits_t bits = { 0 };
252 lsmash_bs_t bs = { 0 };
253 lsmash_bits_init( &bits, &bs );
254 uint8_t buffer[EAC3_SPECIFIC_BOX_MAX_LENGTH] = { 0 };
255 bs.buffer.data = buffer;
256 bs.buffer.alloc = EAC3_SPECIFIC_BOX_MAX_LENGTH;
257 lsmash_bits_put( &bits, 32, 0 ); /* box size */
258 lsmash_bits_put( &bits, 32, ISOM_BOX_TYPE_DEC3.fourcc ); /* box type: 'dec3' */
259 lsmash_bits_put( &bits, 13, param->data_rate ); /* data_rate; setup by isom_update_bitrate_description */
260 lsmash_bits_put( &bits, 3, param->num_ind_sub );
261 /* Apparently, the condition of this loop defined in ETSI TS 102 366 V1.2.1 (2008-08) is wrong. */
262 for( int i = 0; i <= param->num_ind_sub; i++ )
264 lsmash_eac3_substream_info_t *independent_info = &param->independent_info[i];
265 lsmash_bits_put( &bits, 2, independent_info->fscod );
266 lsmash_bits_put( &bits, 5, independent_info->bsid );
267 lsmash_bits_put( &bits, 5, independent_info->bsmod );
268 lsmash_bits_put( &bits, 3, independent_info->acmod );
269 lsmash_bits_put( &bits, 1, independent_info->lfeon );
270 lsmash_bits_put( &bits, 3, 0 ); /* reserved */
271 lsmash_bits_put( &bits, 4, independent_info->num_dep_sub );
272 if( independent_info->num_dep_sub > 0 )
273 lsmash_bits_put( &bits, 9, independent_info->chan_loc );
274 else
275 lsmash_bits_put( &bits, 1, 0 ); /* reserved */
277 uint8_t *data = lsmash_bits_export_data( &bits, data_length );
278 lsmash_bits_empty( &bits );
279 /* Update box size. */
280 LSMASH_SET_BE32( data, *data_length );
281 return data;
282 #undef EAC3_SPECIFIC_BOX_MAX_LENGTH
285 /* Return -1 if incomplete Enhanced AC-3 sample is given. */
286 int lsmash_setup_eac3_specific_parameters_from_frame( lsmash_eac3_specific_parameters_t *param, uint8_t *data, uint32_t data_length )
288 if( !data || data_length < 5 )
289 return -1;
290 lsmash_bits_t bits = { 0 };
291 lsmash_bs_t bs = { 0 };
292 uint8_t buffer[EAC3_MAX_SYNCFRAME_LENGTH] = { 0 };
293 bs.buffer.data = buffer;
294 bs.buffer.store = data_length;
295 bs.buffer.alloc = EAC3_MAX_SYNCFRAME_LENGTH;
296 eac3_info_t *info = &(eac3_info_t){ .bits = &bits };
297 lsmash_bits_init( &bits, &bs );
298 memcpy( buffer, data, LSMASH_MIN( data_length, EAC3_MAX_SYNCFRAME_LENGTH ) );
299 uint64_t next_frame_pos = 0;
300 while( 1 )
302 /* Seek to the head of the next syncframe. */
303 bs.buffer.pos = LSMASH_MIN( data_length, next_frame_pos );
304 /* Check the remainder length of the input data.
305 * If there is enough length, then parse the syncframe in it.
306 * The length 5 is the required byte length to get frame size. */
307 uint64_t remain_size = lsmash_bs_get_remaining_buffer_size( &bs );
308 if( bs.eob || (bs.eof && remain_size < 5) )
309 goto setup_param; /* No more valid data. */
310 /* Check the syncword. */
311 if( lsmash_bs_show_byte( &bs, 0 ) != 0x0b
312 || lsmash_bs_show_byte( &bs, 1 ) != 0x77 )
313 goto setup_param;
314 /* Parse syncframe. */
315 info->frame_size = 0;
316 if( eac3_parse_syncframe( info ) < 0 )
317 goto setup_param;
318 if( remain_size < info->frame_size )
319 goto setup_param;
320 int independent = info->strmtyp != 0x1;
321 if( independent && info->substreamid == 0x0 )
323 if( info->number_of_audio_blocks == 6 )
325 /* Encountered the first syncframe of the next access unit. */
326 info->number_of_audio_blocks = 0;
327 goto setup_param;
329 else if( info->number_of_audio_blocks > 6 )
330 goto setup_param;
331 info->number_of_audio_blocks += eac3_audio_block_table[ info->numblkscod ];
332 info->number_of_independent_substreams = 0;
334 else if( info->syncframe_count == 0 )
335 /* The first syncframe in an AU must be independent and assigned substream ID 0. */
336 return -2;
337 if( independent )
338 info->independent_info[info->number_of_independent_substreams ++].num_dep_sub = 0;
339 else
340 ++ info->independent_info[info->number_of_independent_substreams - 1].num_dep_sub;
341 next_frame_pos += info->frame_size;
342 ++ info->syncframe_count;
344 setup_param:
345 if( info->number_of_independent_substreams == 0 || info->number_of_independent_substreams > 8 )
346 return -1;
347 if( !info->dec3_param_initialized )
348 eac3_update_specific_param( info );
349 *param = info->dec3_param;
350 return info->number_of_audio_blocks == 6 ? 0 : -1;
353 uint16_t lsmash_eac3_get_chan_loc_from_chanmap( uint16_t chanmap )
355 return ((chanmap & 0x7f8) >> 2) | ((chanmap & 0x2) >> 1);
358 static int eac3_check_syncframe_header( eac3_info_t *info )
360 if( info->strmtyp == 0x3 )
361 return -1; /* unknown Stream type */
362 lsmash_eac3_substream_info_t *substream_info;
363 if( info->strmtyp != 0x1 )
364 substream_info = &info->independent_info[ info->current_independent_substream_id ];
365 else
366 substream_info = &info->dependent_info;
367 if( substream_info->fscod == 0x3 && info->fscod2 == 0x3 )
368 return -1; /* unknown Sample Rate Code */
369 if( substream_info->bsid < 10 || substream_info->bsid > 16 )
370 return -1; /* not EAC-3 */
371 return 0;
374 int eac3_parse_syncframe( eac3_info_t *info )
376 lsmash_bits_t *bits = info->bits;
377 lsmash_bits_get( bits, 16 ); /* syncword (16) */
378 info->strmtyp = lsmash_bits_get( bits, 2 ); /* strmtyp (2) */
379 info->substreamid = lsmash_bits_get( bits, 3 ); /* substreamid (3) */
380 lsmash_eac3_substream_info_t *substream_info;
381 if( info->strmtyp != 0x1 )
383 if( info->substreamid == 0x0 && info->number_of_independent_substreams )
384 eac3_update_specific_param( info );
385 info->current_independent_substream_id = info->substreamid;
386 substream_info = &info->independent_info[ info->current_independent_substream_id ];
387 substream_info->chan_loc = 0;
389 else
390 substream_info = &info->dependent_info;
391 info->frame_size = 2 * (lsmash_bits_get( bits, 11 ) + 1); /* frmsiz (11) */
392 substream_info->fscod = lsmash_bits_get( bits, 2 ); /* fscod (2) */
393 if( substream_info->fscod == 0x3 )
395 info->fscod2 = lsmash_bits_get( bits, 2 ); /* fscod2 (2) */
396 info->numblkscod = 0x3;
398 else
399 info->numblkscod = lsmash_bits_get( bits, 2 ); /* numblkscod (2) */
400 substream_info->acmod = lsmash_bits_get( bits, 3 ); /* acmod (3) */
401 substream_info->lfeon = lsmash_bits_get( bits, 1 ); /* lfeon (1) */
402 substream_info->bsid = lsmash_bits_get( bits, 5 ); /* bsid (5) */
403 lsmash_bits_get( bits, 5 ); /* dialnorm (5) */
404 if( lsmash_bits_get( bits, 1 ) ) /* compre (1) */
405 lsmash_bits_get( bits, 8 ); /* compr (8) */
406 if( substream_info->acmod == 0x0 )
408 lsmash_bits_get( bits, 5 ); /* dialnorm2 (5) */
409 if( lsmash_bits_get( bits, 1 ) ) /* compre2 (1) */
410 lsmash_bits_get( bits, 8 ); /* compr2 (8) */
412 if( info->strmtyp == 0x1 && lsmash_bits_get( bits, 1 ) ) /* chanmape (1) */
414 uint16_t chanmap = lsmash_bits_get( bits, 16 ); /* chanmap (16) */
415 info->independent_info[ info->current_independent_substream_id ].chan_loc |= lsmash_eac3_get_chan_loc_from_chanmap( chanmap );
417 if( lsmash_bits_get( bits, 1 ) ) /* mixmdate (1) */
419 if( substream_info->acmod > 0x2 )
420 lsmash_bits_get( bits, 2 ); /* dmixmod (2) */
421 if( ((substream_info->acmod & 0x1) && (substream_info->acmod > 0x2)) || (substream_info->acmod & 0x4) )
422 lsmash_bits_get( bits, 6 ); /* ltrt[c/sur]mixlev (3)
423 * loro[c/sur]mixlev (3) */
424 if( substream_info->lfeon && lsmash_bits_get( bits, 1 ) ) /* lfemixlevcode (1) */
425 lsmash_bits_get( bits, 5 ); /* lfemixlevcod (5) */
426 if( info->strmtyp == 0x0 )
428 if( lsmash_bits_get( bits, 1 ) ) /* pgmscle (1) */
429 lsmash_bits_get( bits, 6 ); /* pgmscl (6) */
430 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* pgmscle2 (1) */
431 lsmash_bits_get( bits, 6 ); /* pgmscl2 (6) */
432 if( lsmash_bits_get( bits, 1 ) ) /* extpgmscle (1) */
433 lsmash_bits_get( bits, 6 ); /* extpgmscl (6) */
434 uint8_t mixdef = lsmash_bits_get( bits, 2 ); /* mixdef (2) */
435 if( mixdef == 0x1 )
436 lsmash_bits_get( bits, 5 ); /* premixcmpsel (1)
437 * drcsrc (1)
438 * premixcmpscl (3) */
439 else if( mixdef == 0x2 )
440 lsmash_bits_get( bits, 12 ); /* mixdata (12) */
441 else if( mixdef == 0x3 )
443 uint8_t mixdeflen = lsmash_bits_get( bits, 5 ); /* mixdeflen (5) */
444 lsmash_bits_get( bits, 8 * (mixdeflen + 2) ); /* mixdata (8 * (mixdeflen + 2))
445 * mixdatafill (0-7) */
447 if( substream_info->acmod < 0x2 )
449 if( lsmash_bits_get( bits, 1 ) ) /* paninfoe (1) */
450 lsmash_bits_get( bits, 14 ); /* panmean (8)
451 * paninfo (6) */
452 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* paninfo2e (1) */
453 lsmash_bits_get( bits, 14 ); /* panmean2 (8)
454 * paninfo2 (6) */
456 if( lsmash_bits_get( bits, 1 ) ) /* frmmixcfginfoe (1) */
458 if( info->numblkscod == 0x0 )
459 lsmash_bits_get( bits, 5 ); /* blkmixcfginfo[0] (5) */
460 else
462 int number_of_blocks_per_syncframe = ((int []){ 1, 2, 3, 6 })[ info->numblkscod ];
463 for( int blk = 0; blk < number_of_blocks_per_syncframe; blk++ )
464 if( lsmash_bits_get( bits, 1 ) ) /* blkmixcfginfoe (1)*/
465 lsmash_bits_get( bits, 5 ); /* blkmixcfginfo[blk] (5) */
470 if( lsmash_bits_get( bits, 1 ) ) /* infomdate (1) */
472 substream_info->bsmod = lsmash_bits_get( bits, 3 ); /* bsmod (3) */
473 lsmash_bits_get( bits, 1 ); /* copyrightb (1) */
474 lsmash_bits_get( bits, 1 ); /* origbs (1) */
475 if( substream_info->acmod == 0x2 )
476 lsmash_bits_get( bits, 4 ); /* dsurmod (2)
477 * dheadphonmod (2) */
478 else if( substream_info->acmod >= 0x6 )
479 lsmash_bits_get( bits, 2 ); /* dsurexmod (2) */
480 if( lsmash_bits_get( bits, 1 ) ) /* audprodie (1) */
481 lsmash_bits_get( bits, 8 ); /* mixlevel (5)
482 * roomtyp (2)
483 * adconvtyp (1) */
484 if( substream_info->acmod == 0x0 && lsmash_bits_get( bits, 1 ) ) /* audprodie2 (1) */
485 lsmash_bits_get( bits, 8 ); /* mixlevel2 (5)
486 * roomtyp2 (2)
487 * adconvtyp2 (1) */
488 if( substream_info->fscod < 0x3 )
489 lsmash_bits_get( bits, 1 ); /* sourcefscod (1) */
491 else
492 substream_info->bsmod = 0;
493 if( info->strmtyp == 0x0 && info->numblkscod != 0x3 )
494 lsmash_bits_get( bits, 1 ); /* convsync (1) */
495 if( info->strmtyp == 0x2 )
497 int blkid;
498 if( info->numblkscod == 0x3 )
499 blkid = 1;
500 else
501 blkid = lsmash_bits_get( bits, 1 ); /* blkid (1) */
502 if( blkid )
503 lsmash_bits_get( bits, 6 ); /* frmsizecod (6) */
505 if( lsmash_bits_get( bits, 1 ) ) /* addbsie (1) */
507 uint8_t addbsil = lsmash_bits_get( bits, 6 ); /* addbsil (6) */
508 lsmash_bits_get( bits, (addbsil + 1) * 8 ); /* addbsi ((addbsil + 1) * 8) */
510 lsmash_bits_get_align( bits );
511 return eac3_check_syncframe_header( info );
514 void eac3_update_specific_param( eac3_info_t *info )
516 lsmash_eac3_specific_parameters_t *param = &info->dec3_param;
517 param->data_rate = 0;
518 param->num_ind_sub = info->number_of_independent_substreams - 1;
519 for( uint8_t i = 0; i <= param->num_ind_sub; i++ )
520 param->independent_info[i] = info->independent_info[i];
521 info->dec3_param_initialized = 1;
524 #define EAC3_SPECIFIC_BOX_MIN_LENGTH 13
526 int eac3_construct_specific_parameters( lsmash_codec_specific_t *dst, lsmash_codec_specific_t *src )
528 assert( dst && dst->data.structured && src && src->data.unstructured );
529 if( src->size < EAC3_SPECIFIC_BOX_MIN_LENGTH )
530 return -1;
531 lsmash_eac3_specific_parameters_t *param = (lsmash_eac3_specific_parameters_t *)dst->data.structured;
532 uint8_t *data = src->data.unstructured;
533 uint64_t size = LSMASH_GET_BE32( data );
534 data += ISOM_BASEBOX_COMMON_SIZE;
535 if( size == 1 )
537 size = LSMASH_GET_BE64( data );
538 data += 8;
540 if( size != src->size )
541 return -1;
542 param->data_rate = (data[0] << 5) | ((data[1] >> 3) & 0x1F); /* XXXX XXXX XXXX Xxxx */
543 param->num_ind_sub = data[1] & 0x07; /* xxxx xxxx xxxx xXXX */
544 data += 2;
545 size -= 2;
546 for( int i = 0; i <= param->num_ind_sub; i++ )
548 if( size < 3 )
549 return -1;
550 lsmash_eac3_substream_info_t *independent_info = &param->independent_info[i];
551 independent_info->fscod = (data[0] >> 6) & 0x03; /* XXxx xxxx xxxx xxxx xxxx xxxx */
552 independent_info->bsid = (data[0] >> 1) & 0x1F; /* xxXX XXXx xxxx xxxx xxxx xxxx */
553 independent_info->bsmod = ((data[0] & 0x01) << 4) | ((data[1] >> 4) & 0x0F); /* xxxx xxxX XXXX xxxx xxxx xxxx */
554 independent_info->acmod = (data[1] >> 1) & 0x07; /* xxxx xxxx xxxx XXXx xxxx xxxx */
555 independent_info->lfeon = data[1] & 0x01; /* xxxx xxxx xxxx xxxX xxxx xxxx */
556 independent_info->num_dep_sub = (data[2] >> 1) & 0x0F; /* xxxx xxxx xxxx xxxx xxxX XXXx */
557 data += 3;
558 size -= 3;
559 if( independent_info->num_dep_sub > 0 )
561 if( size < 1 )
562 return -1;
563 independent_info->chan_loc = ((data[-1] & 0x01) << 8) | data[0]; /* xxxx xxxX XXXX XXXX */
564 data += 1;
565 size -= 1;
568 return 0;
571 void eac3_update_sample_rate
573 uint32_t *frequency,
574 lsmash_eac3_specific_parameters_t *dec3_param,
575 uint8_t *fscod2
578 /* Additional independent substreams 1 to 7 must be encoded at the same sample rate as independent substream 0. */
579 uint32_t samplerate = ac3_sample_rate_table[ dec3_param->independent_info[0].fscod ];
580 if( samplerate == 0 && fscod2 )
581 /* The value 3 (or 0b11) of fscod2 is reserved. */
582 samplerate = ac3_sample_rate_table[ *fscod2 ] / 2;
583 if( samplerate != 0 )
584 *frequency = samplerate;
585 else
586 lsmash_log( NULL, LSMASH_LOG_WARNING, "Unknown sampling rate is detected.\n" );
589 #if 0
590 /* 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. */
591 static void eac3_update_channel_layout
593 lsmash_channel_layout_tag *layout_tag,
594 lsmash_eac3_substream_info_t *independent_info
597 if( independent_info->chan_loc == 0 )
599 *layout_tag = ac3_channel_layout_table[ independent_info->acmod ][ independent_info->lfeon ];
600 return;
602 else if( independent_info->acmod != 0x7 )
604 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
605 return;
607 /* OK. All L, C, R, Ls and Rs exsist. */
608 if( !independent_info->lfeon )
610 if( independent_info->chan_loc == 0x80 )
611 *layout_tag = QT_CHANNEL_LAYOUT_EAC_7_0_A;
612 else if( independent_info->chan_loc == 0x40 )
613 *layout_tag = QT_CHANNEL_LAYOUT_EAC_6_0_A;
614 else
615 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
616 return;
618 /* Also LFE exsists. */
619 static const struct
621 uint16_t chan_loc;
622 lsmash_channel_layout_tag tag;
623 } eac3_channel_layout_table[]
625 { 0x100, QT_CHANNEL_LAYOUT_EAC3_7_1_B },
626 { 0x80, QT_CHANNEL_LAYOUT_EAC3_7_1_A },
627 { 0x40, QT_CHANNEL_LAYOUT_EAC3_6_1_A },
628 { 0x20, QT_CHANNEL_LAYOUT_EAC3_6_1_B },
629 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_C },
630 { 0x10, QT_CHANNEL_LAYOUT_EAC3_7_1_D },
631 { 0x4, QT_CHANNEL_LAYOUT_EAC3_7_1_E },
632 { 0x2, QT_CHANNEL_LAYOUT_EAC3_6_1_C },
633 { 0x60, QT_CHANNEL_LAYOUT_EAC3_7_1_F },
634 { 0x42, QT_CHANNEL_LAYOUT_EAC3_7_1_G },
635 { 0x22, QT_CHANNEL_LAYOUT_EAC3_7_1_H },
636 { 0 }
638 for( int i = 0; eac3_channel_layout_table[i].chan_loc; i++ )
639 if( independent_info->chan_loc == eac3_channel_layout_table[i].chan_loc )
641 *layout_tag = eac3_channel_layout_table[i].tag;
642 return;
644 *layout_tag = QT_CHANNEL_LAYOUT_UNKNOWN;
646 #endif
648 void eac3_update_channel_count
650 uint32_t *channels,
651 lsmash_eac3_specific_parameters_t *dec3_param
654 /* The default programme selection should always be Programme 1.
655 * Thus, pick the number of channels of Programme 1. */
656 lsmash_eac3_substream_info_t *independent_info = &dec3_param->independent_info[0];
657 *channels = ac3_channel_count_table[ independent_info->acmod ] /* L/C/R/Ls/Rs combination */
658 + 2 * !!(independent_info->chan_loc & 0x100) /* Lc/Rc pair */
659 + 2 * !!(independent_info->chan_loc & 0x80) /* Lrs/Rrs pair */
660 + !!(independent_info->chan_loc & 0x40) /* Cs */
661 + !!(independent_info->chan_loc & 0x20) /* Ts */
662 + 2 * !!(independent_info->chan_loc & 0x10) /* Lsd/Rsd pair */
663 + 2 * !!(independent_info->chan_loc & 0x8) /* Lw/Rw pair */
664 + 2 * !!(independent_info->chan_loc & 0x4) /* Lvh/Rvh pair */
665 + !!(independent_info->chan_loc & 0x2) /* Cvh */
666 + !!(independent_info->chan_loc & 0x1) /* LFE2 */
667 + independent_info->lfeon; /* LFE */
670 int eac3_print_codec_specific( FILE *fp, lsmash_file_t *file, isom_box_t *box, int level )
672 assert( fp && file && box && (box->manager & LSMASH_BINARY_CODED_BOX) );
673 int indent = level;
674 lsmash_ifprintf( fp, indent++, "[%s: EC3 Specific Box]\n", isom_4cc2str( box->type.fourcc ) );
675 lsmash_ifprintf( fp, indent, "position = %"PRIu64"\n", box->pos );
676 lsmash_ifprintf( fp, indent, "size = %"PRIu64"\n", box->size );
677 if( box->size < EAC3_SPECIFIC_BOX_MIN_LENGTH )
678 return -1;
679 uint8_t *data = box->binary;
680 isom_skip_box_common( &data );
681 lsmash_ifprintf( fp, indent, "data_rate = %"PRIu16" kbit/s\n", (data[0] << 5) | ((data[1] >> 3) & 0x1F) );
682 uint8_t num_ind_sub = data[1] & 0x07;
683 lsmash_ifprintf( fp, indent, "num_ind_sub = %"PRIu8"\n", num_ind_sub );
684 data += 2;
685 for( int i = 0; i <= num_ind_sub; i++ )
687 lsmash_ifprintf( fp, indent, "independent_substream[%d]\n", i );
688 int sub_indent = indent + 1;
689 uint8_t fscod = (data[0] >> 6) & 0x03;
690 uint8_t bsid = (data[0] >> 1) & 0x1F;
691 uint8_t bsmod = ((data[0] & 0x01) << 4) | ((data[1] >> 4) & 0x0F);
692 uint8_t acmod = (data[1] >> 1) & 0x07;
693 uint8_t lfeon = data[1] & 0x01;
694 uint8_t num_dep_sub = (data[2] >> 1) & 0x0F;
695 if( fscod != 0x03 )
696 lsmash_ifprintf( fp, sub_indent, "fscod = %"PRIu8" (%"PRIu32" Hz)\n", fscod, ac3_sample_rate_table[fscod] );
697 else
698 lsmash_ifprintf( fp, sub_indent, "fscod = 0x03 (reduced sample rate)\n" );
699 lsmash_ifprintf( fp, sub_indent, "bsid = %"PRIu8"\n", bsid );
700 if( bsmod < 0x08 )
701 lsmash_ifprintf( fp, sub_indent, "bsmod = %"PRIu8" (%s)\n", bsmod, bit_stream_mode[bsmod + (acmod == 0x01 ? 1 : acmod > 0x01 ? 2 : 0)] );
702 else
703 lsmash_ifprintf( fp, sub_indent, "bsmod = %"PRIu8" (Undefined service)\n" );
704 lsmash_ifprintf( fp, sub_indent, "acmod = %"PRIu8" (%s)\n", acmod, audio_coding_mode[acmod + (bsmod == 0x07 ? 8 : 0)] );
705 lsmash_ifprintf( fp, sub_indent, "lfeon = %s\n", lfeon ? "1 (LFE)" : "0" );
706 lsmash_ifprintf( fp, sub_indent, "num_dep_sub = %"PRIu8"\n", num_dep_sub );
707 data += 3;
708 if( num_dep_sub > 0 )
710 static const char *channel_location[] =
712 "LFE2",
713 "Cvh",
714 "Lvh/Rvh pair",
715 "Lw/Rw pair",
716 "Lsd/Rsd pair",
717 "Ts",
718 "Cs",
719 "Lrs/Rrs pair",
720 "Lc/Rc pair"
722 uint16_t chan_loc = ((data[-1] & 0x01) << 8) | data[0];
723 lsmash_ifprintf( fp, sub_indent, "chan_loc = 0x%04"PRIx16"\n", chan_loc );
724 for( int j = 0; j < 9; j++ )
725 if( (chan_loc >> j & 0x01) )
726 lsmash_ifprintf( fp, sub_indent + 1, "%s\n", channel_location[j] );
727 data += 1;
729 else
730 lsmash_ifprintf( fp, sub_indent, "reserved = %"PRIu8"\n", data[2] & 0x01 );
732 return 0;
735 #undef EAC3_SPECIFIC_BOX_MIN_LENGTH