OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / media / libstagefright / codecs / aacdec / get_adts_header.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /*
19
20  Pathname: get_adts_header.c
21
22 ------------------------------------------------------------------------------
23  REVISION HISTORY
24
25  Description: Remove default_config variable
26
27  Description: change enter_mc_info to set_mc_info
28
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
33
34  Who:                                          Date:
35  Description:
36
37 ------------------------------------------------------------------------------
38  INPUT AND OUTPUT DEFINITIONS
39
40  Inputs:
41     pVars           =   Pointer to structure that holds file-scope variables.
42                         [ tDec_Int_File * ]
43
44     pSyncword       =   Pointer to variable that holds the 28-bit fixed
45                         header upon the exit of this function. [ UInt32 * ]
46
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 * ]
50
51  Local Stores/Buffers/Pointers Needed:
52     None
53
54  Global Stores/Buffers/Pointers Needed:
55     None
56
57  Outputs:
58     Status = SUCCESS or ERROR CODE
59
60  Pointers and Buffers Modified:
61     pVars->prog_config   Updated with program information data as read from
62                          the ADTS header.
63
64     pSyncword            Value pointed to is updated with the contents of
65                          the 28-bit fixed header.
66
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.
70
71  Local Stores Modified:
72     None
73
74  Global Stores Modified:
75     None
76
77 ------------------------------------------------------------------------------
78  FUNCTION DESCRIPTION
79
80  Acronym Definitions
81  ADTS  Audio Data Transport Stream
82  CRC   Cyclic Redundancy Code
83
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
88  syncword.
89
90  Configuration information is then extracted from the bitstream.
91
92  The bitstream information is packed as follows.
93  Comments about the correct interpretation of these bits are contained within
94  the code.
95
96                                       CRC_absent    sampling_rate_idx
97                                            \               / \
98                                             \             /   \
99                                              \  Profile  /     \  UNUSED
100                                               \   / \   /       \   /
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            \-------------/           |                                  \  /
105                                      |                                   \/
106                                      ID == '1' for MPEG-2 AAC    channel_config
107        copyright_id_bit                 == '0' for MPEG-4 AAC
108           /
109     home /
110      /  /
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|----/    \ /          |_____________________________|
121           \-------------/           |
122                               headerless_frames
123
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 ------------------------------------------------------------------------------
128  REQUIREMENTS
129
130  After the ADTS syncword is detected, this function shall parse the
131  information residing behind the syncword in the bitstream.
132 ------------------------------------------------------------------------------
133  REFERENCES
134  (1) ISO/IEC 13818-7:1997(E)
135      Part 7
136         Subpart 6.2 (Audio_Data_Transport_Stream frame, ADTS)
137
138  (2) ISO/IEC 11172-3:1993(E)
139      Part 3
140         Subpart 2.4.3 The audio decoding process
141
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
162    works."
163    Copyright(c)1996.
164
165 ------------------------------------------------------------------------------
166  PSEUDO-CODE
167
168     IF (*(pInvoke) > 3)
169
170          CALL find_adts_syncword(
171                     pSyncword,
172                    &(pVars->inputStream),
173                     LENGTH_FIXED_HEADER,
174                     MASK_28BITS);
175            RETURNING  status
176     ELSE
177
178         *(pSyncword) = SYNCWORD_15BITS;
179
180         CALL find_adts_syncword(
181                    pSyncword,
182                   &(pVars->inputStream),
183                    LENGTH_SYNCWORD,
184                    ID_BIT_FILTER);
185
186           MODIFYING  *(pSyncword) = 28-bit fixed header (long syncword)
187           RETURNING  status
188
189         CALL getbits(
190                 (LENGTH_FIXED_HEADER - LENGTH_SYNCWORD),
191                &(pVars->inputStream));
192
193           MODIFYING pVars->inputStream
194           RETURNING adts_header = remaining bits in the fixed header
195
196         *(pSyncword) <<= 13;
197         *(pSyncword) = *(pSyncword) OR adts_header;
198
199         pVars->prog_config.CRC_absent  = ((UInt)(adts_header >> 12)) AND 0x0001;
200
201         lower_16 = (UInt)adts_header;
202
203         pVars->prog_config.profile = (lower_16 >> 10) AND 0x3;
204
205         pVars->prog_config.sampling_rate_idx = (lower_16 >> 6) AND 0xF;
206
207         channel_configuration = (lower_16 >> 2) AND 0x7;
208
209         channel_configuration = channel_configuration - 1;
210         pVars->prog_config.front.ele_is_cpe[0] = channel_configuration;
211
212         pVars->prog_config.front.num_ele    = 1;
213
214         pVars->prog_config.front.ele_tag[0] = 0;
215
216         pVars->prog_config.mono_mix.present = 0;
217         pVars->prog_config.stereo_mix.present = 0;
218         pVars->prog_config.matrix_mix.present = 0;
219
220         CALL set_mc_info(
221                 &(pVars->mc_info),
222                 &(pVars->savedMCInfo),
223                 &(pVars->prog_config),
224                   pVars->pWinSeqInfo,
225                   pVars->SFBWidth128);
226           MODIFYING pVars->mc_info = multi-channel configuration information
227           RETURNING status         = SUCCESS/FAILURE
228
229         IF ( (*pInvoke) != 0)
230             CALL check_mc_info(
231                     &(pVars->mc_info),
232                     &(pVars->savedMCInfo),
233                      FALSE);
234               RETURNING status = SUCCESS/FAILURE
235         ELSE
236             CALL check_mc_info(
237                     &(pVars->mc_info),
238                     &(pVars->savedMCInfo),
239                      TRUE);
240               MODIFYING pVars->savedMCInfo = pVars->mc_info
241               RETURNING status = SUCCESS/FAILURE
242         ENDIF
243
244         IF (status == SUCCESS)
245             (*pInvoke) = (*pInvoke) + 1;
246         ELSE
247             (*pInvoke) = 0;
248         ENDIF
249
250     ENDIF
251
252     CALL getbits(
253             LENGTH_VARIABLE_HEADER,
254            &(pVars->inputStream));
255       RETURNING adts_header = 28-bits (the contents of the variable header.)
256
257     pVars->prog_config.frame_length  = ((UInt)(adts_header >> 13)) AND 0x1FFF;
258
259     lower_16 = (UInt)adts_header;
260
261     pVars->prog_config.buffer_fullness = (lower_16 >> 2) AND 0x7FF;
262
263     pVars->prog_config.headerless_frames = (lower_16 AND 0x0003);
264
265     IF (pVars->prog_config.CRC_absent == 0)
266
267         CALL getbits(
268                 LENGTH_CRC,
269                &(pVars->inputStream) );
270           RETURNING pVars->prog_config.CRC_check = 16-bit CRC
271
272     ENDIF
273
274     pVars->default_config = 0;
275
276     IF (byte_align_offset > 7)
277         status = 1;
278     ENDIF
279
280     return (status);
281
282 ------------------------------------------------------------------------------
283  RESOURCES USED
284    When the code is written for a specific target processor the
285      the resources used should be documented below.
286
287  STACK USAGE: [stack count for this module] + [variable to represent
288           stack usage for each subroutine called]
289
290      where: [stack usage variable] = stack usage for [subroutine
291          name] (see [filename].ext)
292
293  DATA MEMORY USED: x words
294
295  PROGRAM MEMORY USED: x words
296
297  CLOCK CYCLES: [cycle count equation for this module] + [variable
298            used to represent cycle count for each subroutine
299            called]
300
301      where: [cycle count variable] = cycle count for [subroutine
302         name] (see [filename].ext)
303
304 ------------------------------------------------------------------------------
305 */
306
307
308 /*----------------------------------------------------------------------------
309 ; INCLUDES
310 ----------------------------------------------------------------------------*/
311 #include "pv_audio_type_defs.h"
312 #include "s_bits.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"
318
319 /*----------------------------------------------------------------------------
320 ; MACROS
321 ; Define module specific macros here
322 ----------------------------------------------------------------------------*/
323
324 /*----------------------------------------------------------------------------
325 ; DEFINES
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
333
334 #define ID_BIT_FILTER           0x7FFB
335 #define SYNCWORD_15BITS         0x7FF8
336 #define MASK_28BITS             0x0FFFFFFFL
337
338 /*----------------------------------------------------------------------------
339 ; LOCAL FUNCTION DEFINITIONS
340 ; Function Prototype declaration
341 ----------------------------------------------------------------------------*/
342
343 /*----------------------------------------------------------------------------
344 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
345 ; Variable declaration - defined here and used outside this module
346 ----------------------------------------------------------------------------*/
347
348 /*----------------------------------------------------------------------------
349 ; EXTERNAL FUNCTION REFERENCES
350 ; Declare functions defined elsewhere and referenced in this module
351 ----------------------------------------------------------------------------*/
352
353 /*----------------------------------------------------------------------------
354 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
355 ; Declare variables used in this module but defined elsewhere
356 ----------------------------------------------------------------------------*/
357
358 /*----------------------------------------------------------------------------
359 ; FUNCTION CODE
360 ----------------------------------------------------------------------------*/
361 Int get_adts_header(
362     tDec_Int_File *pVars,
363     UInt32        *pSyncword,
364     Int           *pInvoke,
365     Int            CorrectlyReadFramesCount)
366 {
367     UInt32 adts_header;
368     UInt   lower_16;
369     Int    status = SUCCESS;
370     UInt   channel_configuration;
371
372     /*
373      * Search for the LONG ADTS syncword (comprised of the entire fixed header)
374      * if the number of CorrectlyReadFrames is > CorrectlyReadFramesCount
375      *
376      * Otherwise, search for just the short syncword.
377      */
378     if (*(pInvoke) > CorrectlyReadFramesCount)
379     {
380         /*
381          * Find the long ADTS syncword
382          * (comprised of the entire ADTS fixed header)
383          */
384
385         status = find_adts_syncword(pSyncword,
386                                     &(pVars->inputStream),
387                                     LENGTH_FIXED_HEADER,
388                                     MASK_28BITS);
389     }
390     else
391     {
392
393         *(pSyncword) = SYNCWORD_15BITS;
394
395         status = find_adts_syncword(pSyncword,
396                                     &(pVars->inputStream),
397                                     LENGTH_SYNCWORD,
398                                     ID_BIT_FILTER);
399
400         /*
401          *  Extract the data from the header following the syncword
402          */
403         adts_header = getbits((LENGTH_FIXED_HEADER - LENGTH_SYNCWORD),
404                               &(pVars->inputStream));
405
406         *(pSyncword) <<= (LENGTH_FIXED_HEADER - LENGTH_SYNCWORD);
407         *(pSyncword)  |= adts_header;
408
409         /* Denotes whether a CRC check should be performed */
410         pVars->prog_config.CRC_absent  = ((UInt)(adts_header >> 12)) & 0x0001;
411
412         /*
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.
416          */
417         lower_16 = (UInt)adts_header;
418
419         /*
420          * Profile consists of 2 bits, which indicate
421          * the profile used.
422          *
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
427          */
428         pVars->prog_config.profile = (lower_16 >> 10) & 0x3;
429
430         if (pVars->prog_config.profile == MP4AUDIO_AAC_SSR)
431         {
432             status = 1;     /* Not supported */
433         }
434
435         /*
436          * Sampling_rate_idx consists of 4 bits
437          * see Ref #1 for their interpretation.
438          */
439         pVars->prog_config.sampling_rate_idx = (lower_16 >> 6) & 0xF;
440
441         /*
442          * private_bit is a bit for private use.  ISO/IEC will not make
443          * use of this bit in the future.
444          *
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...
448          *
449          * private_bit = (lower_16 & 0x0400) >> 10;
450          */
451
452         /*
453          * These 3 bits indicate the channel configuration used.
454          *
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.
458          *
459          * If '1' then the channel configuration is MONO.
460          * If '2' then the channel configuration is STEREO
461          *
462          * 3-7 represent channel configurations which this library
463          * will not support in the forseeable future.
464          */
465         channel_configuration = (lower_16 >> 2) & 0x7;
466         /* do not support more than 2 channels */
467         if (channel_configuration > 2)
468         {
469             status = 1;
470         }
471
472         /*
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.
476          *
477          * home is '0' for a copy, '1' for an original.
478          *
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...
482          *
483          * original_copy = (lower_16 >> 1) & 0x1;
484          *
485          * home = (lower_16 & 0x1);
486          *
487          */
488
489         /* Set up based on information extracted from the ADTS FIXED header */
490
491         /* This equals 1 for STEREO, 0 for MONO */
492         if (channel_configuration)
493         {
494             channel_configuration--;
495         }
496         pVars->prog_config.front.ele_is_cpe[0] = channel_configuration;
497
498         /* This value is constant for both MONO and STEREO */
499         pVars->prog_config.front.num_ele    = 1;
500
501         /* ADTS does not specify this tag value - do we even use it? */
502         pVars->prog_config.front.ele_tag[0] = 0;
503
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;
508
509         /* enter configuration into MC_Info structure */
510         if (status == SUCCESS)
511         {
512             /* profile + 1 == audioObjectType */
513             status =
514                 set_mc_info(
515                     &(pVars->mc_info),
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, */
521                     pVars->SFBWidth128);
522
523         } /* if (status == SUCCESS) */
524
525
526 #ifdef AAC_PLUS
527
528         /*
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
532          */
533         if ((pVars->prog_config.sampling_rate_idx >= 6) && (pVars->aacPlusEnabled == TRUE))
534         {
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;
540         }
541 #endif
542
543
544         /*
545          * The tag and is_cpe will be checked in huffdecode,
546          * remove this check routine.
547          */
548         /*if (status == SUCCESS)
549          *{
550          *   if ( (*pInvoke) != 0)
551          *   {
552          *       status =
553          *           check_mc_info(
554          *               &(pVars->mc_info),
555          *               &(pVars->savedMCInfo),
556          *               FALSE);
557          *   }
558          *   else
559          *   {
560          *       status =
561          *           check_mc_info(
562          *               &(pVars->mc_info),
563          *               &(pVars->savedMCInfo),
564          *               TRUE);
565          *   }
566          *
567          *}*/ /* if (status == SUCCESS) */
568
569         /*
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
574          */
575
576         if (status == SUCCESS)
577         {
578             (*pInvoke)++;
579         }
580         else
581         {
582             (*pInvoke) = 0;
583         }
584
585     } /* END if (*(pInvoke) > 3) */
586
587     /* Grab the bits in the ADTS variable header */
588     adts_header = getbits(
589                       LENGTH_VARIABLE_HEADER,
590                       &(pVars->inputStream));
591     /*
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.
596      *
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.
600      *
601      *
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...
605      *
606      * copyright_id_bit = ((UInt)(adts_header >> 27)) & 0x1;
607      *
608      * copyright_id_start = ((UInt)(adts_header >> 26)) & 0x1;
609      */
610
611     /*
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.
615      */
616     pVars->prog_config.frame_length  = ((UInt)(adts_header >> 13)) & 0x1FFF;
617
618     /*
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.
622      */
623     lower_16 = (UInt)adts_header;
624
625     /*
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.
631      */
632     pVars->prog_config.buffer_fullness = (lower_16 >> 2) & 0x7FF;
633
634     /*
635      * headerless_frames indicates the number of
636      * frames with no headers to be processed before the reading
637      * in of the next header.
638      *
639      * In ADTS, up to 4 "no header frames" can exist between
640      * syncwords.
641      *
642      * EXAMPLES:
643      *
644      * Legend: (Sync words denoted by X, frames
645      * deonted by FRAME_#)
646      *
647      * Example(1): The ADTS sequence below packs 5
648      * frames per header.
649      * Here, headerless_frames would always be read in as "4"
650      *
651      * |X||FRAME_0||FRAME_1||FRAME_2||FRAME_3||FRAME_4||X||FRAME_0|
652      *
653      * Example(2): The ADTS sequence below packs 1 frame per header.
654      * Here, headerless_frames would always be read in as "0"
655      *
656      * |X||FRAME_0||X||FRAME_1||X||FRAME_2|
657      *
658      */
659     pVars->prog_config.headerless_frames = (lower_16 & 0x0003);
660
661     if (pVars->prog_config.CRC_absent == 0)
662     {
663         pVars->prog_config.CRC_check = (UInt)getbits(
664                                            LENGTH_CRC,
665                                            &(pVars->inputStream));
666     }
667
668     /* pVars->current_program = 0; */ /* shall be set after PCE is read */
669
670     return (status);
671
672 } /* END get_adts_header */