1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
20 Pathname: get_adts_header.c
22 ------------------------------------------------------------------------------
25 Description: Remove default_config variable
27 Description: change enter_mc_info to set_mc_info
29 Description: (1) add error checking for channel_config > 2
30 (2) eliminated call to check_mc_info
31 (3) use (profile + 1) when calling set_mc_info
32 (4) use winmap when calling set_mc_info
37 ------------------------------------------------------------------------------
38 INPUT AND OUTPUT DEFINITIONS
41 pVars = Pointer to structure that holds file-scope variables.
44 pSyncword = Pointer to variable that holds the 28-bit fixed
45 header upon the exit of this function. [ UInt32 * ]
47 pInvoke = Pointer to variable that keeps track of how many
48 "short" (14 bit) headers have been successfully
49 parsed from the bitstream. [ Int * ]
51 Local Stores/Buffers/Pointers Needed:
54 Global Stores/Buffers/Pointers Needed:
58 Status = SUCCESS or ERROR CODE
60 Pointers and Buffers Modified:
61 pVars->prog_config Updated with program information data as read from
64 pSyncword Value pointed to is updated with the contents of
65 the 28-bit fixed header.
67 pInvoke Value pointed to is updated to reflect the number
68 of successful "short" (14 bit) headers that have
69 been successfully parsed from the bitstream.
71 Local Stores Modified:
74 Global Stores Modified:
77 ------------------------------------------------------------------------------
81 ADTS Audio Data Transport Stream
82 CRC Cyclic Redundancy Code
84 This function calls find_adts_syncword to find the next ADTS header. Until
85 three consistent headers have been read, the syncword used for detection
86 consists of the 12-bit syncword and the 2-bit Layer. After three consistent
87 headers are read, the entire fixed header is used for a robust 28-bit
90 Configuration information is then extracted from the bitstream.
92 The bitstream information is packed as follows.
93 Comments about the correct interpretation of these bits are contained within
96 CRC_absent sampling_rate_idx
101 |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|
102 \ _______________ / | \ / \ /
103 \-------|0xFFF syncword |-------/ | Layer == '00' for AAC \ /
104 \-------------/ | \ /
106 ID == '1' for MPEG-2 AAC channel_config
107 copyright_id_bit == '0' for MPEG-4 AAC
111 |26|27|28|29|30|31|32|33|34|35|36|37|38|39|40|41|42|
112 | \ \ _____________ /
113 | \ \--------|frame length |---------/
114 orig_copy \ \-----------/
115 \ ______________________________
116 copyright_id_start | TOTAL HEADER LENGTH: 56 bits|
117 |-----------------------------|
118 |43|44|45|46|47|48|49|50|51|52|53|54|55| | FIXED HEADER BITS 00-27 |
119 \ _______________ / | | | VARIABLE HEADER BITS 28-55 |
120 \-----|buffer_fullness|----/ \ / |_____________________________|
124 In addition to the bits displayed above, if the value CRC_absent is '0' an
125 additional 16 bits corresponding to a CRC word are read from the bitstream,
126 following the header.
127 ------------------------------------------------------------------------------
130 After the ADTS syncword is detected, this function shall parse the
131 information residing behind the syncword in the bitstream.
132 ------------------------------------------------------------------------------
134 (1) ISO/IEC 13818-7:1997(E)
136 Subpart 6.2 (Audio_Data_Transport_Stream frame, ADTS)
138 (2) ISO/IEC 11172-3:1993(E)
140 Subpart 2.4.3 The audio decoding process
142 (3) MPEG-2 NBC Audio Decoder
143 "This software module was originally developed by AT&T, Dolby
144 Laboratories, Fraunhofer Gesellschaft IIS in the course of development
145 of the MPEG-2 NBC/MPEG-4 Audio standard ISO/IEC 13818-7, 14496-1,2 and
146 3. This software module is an implementation of a part of one or more
147 MPEG-2 NBC/MPEG-4 Audio tools as specified by the MPEG-2 NBC/MPEG-4
148 Audio standard. ISO/IEC gives users of the MPEG-2 NBC/MPEG-4 Audio
149 standards free license to this software module or modifications thereof
150 for use in hardware or software products claiming conformance to the
151 MPEG-2 NBC/MPEG-4 Audio standards. Those UIntending to use this software
152 module in hardware or software products are advised that this use may
153 infringe existing patents. The original developer of this software
154 module and his/her company, the subsequent editors and their companies,
155 and ISO/IEC have no liability for use of this software module or
156 modifications thereof in an implementation. Copyright is not released
157 for non MPEG-2 NBC/MPEG-4 Audio conforming products.The original
158 developer retains full right to use the code for his/her own purpose,
159 assign or donate the code to a third party and to inhibit third party
160 from using the code for non MPEG-2 NBC/MPEG-4 Audio conforming products.
161 This copyright notice must be included in all copies or derivative
165 ------------------------------------------------------------------------------
170 CALL find_adts_syncword(
172 &(pVars->inputStream),
178 *(pSyncword) = SYNCWORD_15BITS;
180 CALL find_adts_syncword(
182 &(pVars->inputStream),
186 MODIFYING *(pSyncword) = 28-bit fixed header (long syncword)
190 (LENGTH_FIXED_HEADER - LENGTH_SYNCWORD),
191 &(pVars->inputStream));
193 MODIFYING pVars->inputStream
194 RETURNING adts_header = remaining bits in the fixed header
197 *(pSyncword) = *(pSyncword) OR adts_header;
199 pVars->prog_config.CRC_absent = ((UInt)(adts_header >> 12)) AND 0x0001;
201 lower_16 = (UInt)adts_header;
203 pVars->prog_config.profile = (lower_16 >> 10) AND 0x3;
205 pVars->prog_config.sampling_rate_idx = (lower_16 >> 6) AND 0xF;
207 channel_configuration = (lower_16 >> 2) AND 0x7;
209 channel_configuration = channel_configuration - 1;
210 pVars->prog_config.front.ele_is_cpe[0] = channel_configuration;
212 pVars->prog_config.front.num_ele = 1;
214 pVars->prog_config.front.ele_tag[0] = 0;
216 pVars->prog_config.mono_mix.present = 0;
217 pVars->prog_config.stereo_mix.present = 0;
218 pVars->prog_config.matrix_mix.present = 0;
222 &(pVars->savedMCInfo),
223 &(pVars->prog_config),
226 MODIFYING pVars->mc_info = multi-channel configuration information
227 RETURNING status = SUCCESS/FAILURE
229 IF ( (*pInvoke) != 0)
232 &(pVars->savedMCInfo),
234 RETURNING status = SUCCESS/FAILURE
238 &(pVars->savedMCInfo),
240 MODIFYING pVars->savedMCInfo = pVars->mc_info
241 RETURNING status = SUCCESS/FAILURE
244 IF (status == SUCCESS)
245 (*pInvoke) = (*pInvoke) + 1;
253 LENGTH_VARIABLE_HEADER,
254 &(pVars->inputStream));
255 RETURNING adts_header = 28-bits (the contents of the variable header.)
257 pVars->prog_config.frame_length = ((UInt)(adts_header >> 13)) AND 0x1FFF;
259 lower_16 = (UInt)adts_header;
261 pVars->prog_config.buffer_fullness = (lower_16 >> 2) AND 0x7FF;
263 pVars->prog_config.headerless_frames = (lower_16 AND 0x0003);
265 IF (pVars->prog_config.CRC_absent == 0)
269 &(pVars->inputStream) );
270 RETURNING pVars->prog_config.CRC_check = 16-bit CRC
274 pVars->default_config = 0;
276 IF (byte_align_offset > 7)
282 ------------------------------------------------------------------------------
284 When the code is written for a specific target processor the
285 the resources used should be documented below.
287 STACK USAGE: [stack count for this module] + [variable to represent
288 stack usage for each subroutine called]
290 where: [stack usage variable] = stack usage for [subroutine
291 name] (see [filename].ext)
293 DATA MEMORY USED: x words
295 PROGRAM MEMORY USED: x words
297 CLOCK CYCLES: [cycle count equation for this module] + [variable
298 used to represent cycle count for each subroutine
301 where: [cycle count variable] = cycle count for [subroutine
302 name] (see [filename].ext)
304 ------------------------------------------------------------------------------
308 /*----------------------------------------------------------------------------
310 ----------------------------------------------------------------------------*/
311 #include "pv_audio_type_defs.h"
313 #include "s_tdec_int_file.h"
314 #include "ibstream.h"
315 #include "set_mc_info.h"
316 #include "find_adts_syncword.h"
317 #include "get_adts_header.h"
319 /*----------------------------------------------------------------------------
321 ; Define module specific macros here
322 ----------------------------------------------------------------------------*/
324 /*----------------------------------------------------------------------------
326 ; Include all pre-processor statements here. Include conditional
327 ; compile variables also.
328 ----------------------------------------------------------------------------*/
329 #define LENGTH_VARIABLE_HEADER 28
330 #define LENGTH_FIXED_HEADER 28
331 #define LENGTH_SYNCWORD 15
332 #define LENGTH_CRC 16
334 #define ID_BIT_FILTER 0x7FFB
335 #define SYNCWORD_15BITS 0x7FF8
336 #define MASK_28BITS 0x0FFFFFFFL
338 /*----------------------------------------------------------------------------
339 ; LOCAL FUNCTION DEFINITIONS
340 ; Function Prototype declaration
341 ----------------------------------------------------------------------------*/
343 /*----------------------------------------------------------------------------
344 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
345 ; Variable declaration - defined here and used outside this module
346 ----------------------------------------------------------------------------*/
348 /*----------------------------------------------------------------------------
349 ; EXTERNAL FUNCTION REFERENCES
350 ; Declare functions defined elsewhere and referenced in this module
351 ----------------------------------------------------------------------------*/
353 /*----------------------------------------------------------------------------
354 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
355 ; Declare variables used in this module but defined elsewhere
356 ----------------------------------------------------------------------------*/
358 /*----------------------------------------------------------------------------
360 ----------------------------------------------------------------------------*/
362 tDec_Int_File *pVars,
365 Int CorrectlyReadFramesCount)
369 Int status = SUCCESS;
370 UInt channel_configuration;
373 * Search for the LONG ADTS syncword (comprised of the entire fixed header)
374 * if the number of CorrectlyReadFrames is > CorrectlyReadFramesCount
376 * Otherwise, search for just the short syncword.
378 if (*(pInvoke) > CorrectlyReadFramesCount)
381 * Find the long ADTS syncword
382 * (comprised of the entire ADTS fixed header)
385 status = find_adts_syncword(pSyncword,
386 &(pVars->inputStream),
393 *(pSyncword) = SYNCWORD_15BITS;
395 status = find_adts_syncword(pSyncword,
396 &(pVars->inputStream),
401 * Extract the data from the header following the syncword
403 adts_header = getbits((LENGTH_FIXED_HEADER - LENGTH_SYNCWORD),
404 &(pVars->inputStream));
406 *(pSyncword) <<= (LENGTH_FIXED_HEADER - LENGTH_SYNCWORD);
407 *(pSyncword) |= adts_header;
409 /* Denotes whether a CRC check should be performed */
410 pVars->prog_config.CRC_absent = ((UInt)(adts_header >> 12)) & 0x0001;
413 * All the unread bits in adts_header reside in the lower
414 * 16-bits at this point. Perform a typecast for faster
415 * execution on 16-bit processors.
417 lower_16 = (UInt)adts_header;
420 * Profile consists of 2 bits, which indicate
423 * '00' AAC_MAIN profile
424 * '01' AAC_LC (Low Complexity) profile
425 * '10' AAC_SSR (Scaleable Sampling Rate) profile
426 * '11' AAC_LTP (Long Term Prediction) profile
428 pVars->prog_config.profile = (lower_16 >> 10) & 0x3;
430 if (pVars->prog_config.profile == MP4AUDIO_AAC_SSR)
432 status = 1; /* Not supported */
436 * Sampling_rate_idx consists of 4 bits
437 * see Ref #1 for their interpretation.
439 pVars->prog_config.sampling_rate_idx = (lower_16 >> 6) & 0xF;
442 * private_bit is a bit for private use. ISO/IEC will not make
443 * use of this bit in the future.
445 * We currently make no use of it, but parsing the information
446 * from the bitstream could be easily implemented with the
447 * following instruction...
449 * private_bit = (lower_16 & 0x0400) >> 10;
453 * These 3 bits indicate the channel configuration used.
455 * If '0' then the channel configuration is unspecified here,
456 * and must be given by a program configuration element in
457 * the raw data block.
459 * If '1' then the channel configuration is MONO.
460 * If '2' then the channel configuration is STEREO
462 * 3-7 represent channel configurations which this library
463 * will not support in the forseeable future.
465 channel_configuration = (lower_16 >> 2) & 0x7;
466 /* do not support more than 2 channels */
467 if (channel_configuration > 2)
473 * The following 2 bits encode copyright information.
474 * original_copy is '0' if there is no copyright in the bitstream.
475 * '1' if the bitstream is copyright protected.
477 * home is '0' for a copy, '1' for an original.
479 * PacketVideo currently does nothing with this information,
480 * however, parsing the data from the bitstream could be easily
481 * implemented with the following instructions...
483 * original_copy = (lower_16 >> 1) & 0x1;
485 * home = (lower_16 & 0x1);
489 /* Set up based on information extracted from the ADTS FIXED header */
491 /* This equals 1 for STEREO, 0 for MONO */
492 if (channel_configuration)
494 channel_configuration--;
496 pVars->prog_config.front.ele_is_cpe[0] = channel_configuration;
498 /* This value is constant for both MONO and STEREO */
499 pVars->prog_config.front.num_ele = 1;
501 /* ADTS does not specify this tag value - do we even use it? */
502 pVars->prog_config.front.ele_tag[0] = 0;
504 /* Disable all mix related variables */
505 pVars->prog_config.mono_mix.present = 0;
506 pVars->prog_config.stereo_mix.present = 0;
507 pVars->prog_config.matrix_mix.present = 0;
509 /* enter configuration into MC_Info structure */
510 if (status == SUCCESS)
512 /* profile + 1 == audioObjectType */
516 (tMP4AudioObjectType)(pVars->prog_config.profile + 1),
517 pVars->prog_config.sampling_rate_idx,
518 pVars->prog_config.front.ele_tag[0],
519 pVars->prog_config.front.ele_is_cpe[0],
520 pVars->winmap, /* changed from pVars->pWinSeqInfo, */
523 } /* if (status == SUCCESS) */
529 * For implicit signalling, no hint that sbr or ps is used, so we need to
530 * check the sampling frequency of the aac content, if lesser or equal to
531 * 24 KHz, by defualt upsample, otherwise, do nothing
533 if ((pVars->prog_config.sampling_rate_idx >= 6) && (pVars->aacPlusEnabled == TRUE))
535 pVars->mc_info.upsamplingFactor = 2;
536 pVars->prog_config.sampling_rate_idx -= 3;
537 pVars->mc_info.sbrPresentFlag = 1;
538 pVars->sbrDecoderData.SbrChannel[0].syncState = SBR_ACTIVE;
539 pVars->sbrDecoderData.SbrChannel[1].syncState = SBR_ACTIVE;
545 * The tag and is_cpe will be checked in huffdecode,
546 * remove this check routine.
548 /*if (status == SUCCESS)
550 * if ( (*pInvoke) != 0)
555 * &(pVars->savedMCInfo),
563 * &(pVars->savedMCInfo),
567 *}*/ /* if (status == SUCCESS) */
570 * This keeps track of how many headers have been read in the file.
571 * After the three successful headers with the same configuration
572 * are read in, the entire ADTS fixed header is used as the syncword
573 * for a more robust 28-bit long syncword
576 if (status == SUCCESS)
585 } /* END if (*(pInvoke) > 3) */
587 /* Grab the bits in the ADTS variable header */
588 adts_header = getbits(
589 LENGTH_VARIABLE_HEADER,
590 &(pVars->inputStream));
592 * copyright_identification bit is a single bit of the 72-bit
593 * copyright_id field. This consists of a 8-bit copyright identifier
594 * and a 64-bit copyright_number. 72 headers must be decoded
595 * to reconstruct the entire copyright_id field.
597 * copyright_identification_start is a single bit flagging
598 * the beginning bit of the copyright_id field. '1' for start of
599 * copyright_id, '0' otherwise.
602 * PacketVideo currently does nothing with this information,
603 * however, parsing the data from the bitstream could be easily
604 * implemented with the following instructions...
606 * copyright_id_bit = ((UInt)(adts_header >> 27)) & 0x1;
608 * copyright_id_start = ((UInt)(adts_header >> 26)) & 0x1;
612 * frame_length is a 13-bit field which indicates the length,
613 * in bytes, of the frame including error_check and headers.
614 * This information can theoretically be used to help verify syncwords.
616 pVars->prog_config.frame_length = ((UInt)(adts_header >> 13)) & 0x1FFF;
619 * All the unread bits in adts_header reside in the lower
620 * 16-bits at this point. Perform a typecast for faster
621 * execution on 16-bit processors.
623 lower_16 = (UInt)adts_header;
626 * Indicates the number of 32-bit words remaining in the
627 * encoder buffer after the encoding of the first raw
628 * data block. This value is 0x7ff for variable bit
629 * rate encoders, since buffer fullness does not apply
630 * to Variable Bit Rate (VBR) encoders.
632 pVars->prog_config.buffer_fullness = (lower_16 >> 2) & 0x7FF;
635 * headerless_frames indicates the number of
636 * frames with no headers to be processed before the reading
637 * in of the next header.
639 * In ADTS, up to 4 "no header frames" can exist between
644 * Legend: (Sync words denoted by X, frames
645 * deonted by FRAME_#)
647 * Example(1): The ADTS sequence below packs 5
649 * Here, headerless_frames would always be read in as "4"
651 * |X||FRAME_0||FRAME_1||FRAME_2||FRAME_3||FRAME_4||X||FRAME_0|
653 * Example(2): The ADTS sequence below packs 1 frame per header.
654 * Here, headerless_frames would always be read in as "0"
656 * |X||FRAME_0||X||FRAME_1||X||FRAME_2|
659 pVars->prog_config.headerless_frames = (lower_16 & 0x0003);
661 if (pVars->prog_config.CRC_absent == 0)
663 pVars->prog_config.CRC_check = (UInt)getbits(
665 &(pVars->inputStream));
668 /* pVars->current_program = 0; */ /* shall be set after PCE is read */
672 } /* END get_adts_header */