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: pvmp4setaudioconfigg
21
22------------------------------------------------------------------------------
23 REVISION HISTORY
24
25 Who:                                         Date:
26 Description:
27
28------------------------------------------------------------------------------
29 INPUT AND OUTPUT DEFINITIONS
30
31 Inputs:
32    pExt = pointer to the external interface structure. See the file
33           PVMP4AudioDecoder_API.h for a description of each field.
34           Data type of pointer to a tPVMP4AudioDecoderExternal
35           structure.
36
37           pExt->pInputBuffer: pointer to input buffer containing input
38                               bitstream
39
40           pExt->inputBufferCurrentLength: number of bytes in the input buffer
41
42           pExt->inputBufferUsedLength: number of bytes already consumed in
43                                        input buffer
44
45           pExt->remainderBits: number of bits consumed in addition to
46                                pExt->inputBufferUsedLength
47
48    pMem = void pointer to hide the internal implementation of the library
49           It is cast back to a tDec_Int_File structure. This structure
50           contains information that needs to persist between calls to
51           this function, or is too big to be placed on the stack, even
52           though the data is only needed during execution of this function
53           Data type void pointer, internally pointer to a tDec_Int_File
54           structure.
55
56 Local Stores/Buffers/Pointers Needed: None
57           (The memory set aside in pMem performs this task)
58
59 Global Stores/Buffers/Pointers Needed: None
60
61 Outputs:
62     status = 0                       if no error occurred
63              MP4AUDEC_NONRECOVERABLE if a non-recoverable error occurred
64              MP4AUDEC_RECOVERABLE    if a recoverable error occurred.
65              Presently a recoverable error does not exist, but this
66              was a requirement.
67
68
69 Pointers and Buffers Modified:
70    pMem contents are modified.
71    pExt: (more detail in the file PVMP4AudioDecoder_API.h)
72    inputBufferUsedLength - number of array elements used up by the stream.
73    remainderBits - remaining bits in the next UInt32 buffer
74    samplingRate - sampling rate in samples per sec
75    encodedChannels - channels found on the file (informative)
76    frameLength - length of the frame
77
78 Local Stores Modified: None.
79
80 Global Stores Modified: None.
81
82------------------------------------------------------------------------------
83 FUNCTION DESCRIPTION
84
85
86------------------------------------------------------------------------------
87 REQUIREMENTS
88
89 PacketVideo Document # CCC-AUD-AAC-ERS-0003
90
91------------------------------------------------------------------------------
92 REFERENCES
93
94
95------------------------------------------------------------------------------
96 RESOURCES USED
97   When the code is written for a specific target processor the
98     the resources used should be documented below.
99
100 STACK USAGE: [stack count for this module] + [variable to represent
101          stack usage for each subroutine called]
102
103     where: [stack usage variable] = stack usage for [subroutine
104         name] (see [filename].ext)
105
106 DATA MEMORY USED: x words
107
108 PROGRAM MEMORY USED: x words
109
110 CLOCK CYCLES: [cycle count equation for this module] + [variable
111           used to represent cycle count for each subroutine
112           called]
113
114     where: [cycle count variable] = cycle count for [subroutine
115        name] (see [filename].ext)
116
117------------------------------------------------------------------------------
118*/
119
120
121/*----------------------------------------------------------------------------
122; INCLUDES
123----------------------------------------------------------------------------*/
124
125#include "pv_audio_type_defs.h"
126#include "s_tdec_int_file.h"
127#include "ibstream.h"           /* where #define INBUF_ARRAY_INDEX_SHIFT */
128#include "sfb.h"                   /* Where samp_rate_info[] is declared */
129
130#include "get_audio_specific_config.h"
131#include "pvmp4audiodecoder_api.h"   /* Where this function is declared */
132#include "set_mc_info.h"
133
134/*----------------------------------------------------------------------------
135; MACROS
136; Define module specific macros here
137----------------------------------------------------------------------------*/
138
139/*----------------------------------------------------------------------------
140; DEFINES
141; Include all pre-processor statements here. Include conditional
142; compile variables also.
143----------------------------------------------------------------------------*/
144
145
146/*----------------------------------------------------------------------------
147; LOCAL FUNCTION DEFINITIONS
148; Function Prototype declaration
149----------------------------------------------------------------------------*/
150
151/*----------------------------------------------------------------------------
152; LOCAL STORE/BUFFER/POINTER DEFINITIONS
153; Variable declaration - defined here and used outside this module
154----------------------------------------------------------------------------*/
155
156/*----------------------------------------------------------------------------
157; EXTERNAL FUNCTION REFERENCES
158; Declare functions defined elsewhere and referenced in this module
159----------------------------------------------------------------------------*/
160
161/*----------------------------------------------------------------------------
162; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
163; Declare variables used in this module but defined elsewhere
164----------------------------------------------------------------------------*/
165
166/*----------------------------------------------------------------------------
167; FUNCTION CODE
168----------------------------------------------------------------------------*/
169
170Int PVMP4SetAudioConfig(
171    tPVMP4AudioDecoderExternal  *pExt,
172    void                        *pMem,
173    Int                         upsamplingFactor,
174    Int                         samp_rate,
175    Int                         num_ch,
176    tMP4AudioObjectType         audioObjectType)
177
178{
179
180    tDec_Int_File *pVars;           /* Helper pointer */
181
182    Int            status = MP4AUDEC_INCOMPLETE_FRAME;
183
184    /*
185     * Initialize "helper" pointers to existing memory.
186     */
187    pVars = (tDec_Int_File *)pMem;
188    /*
189     * Translate input buffer variables.
190     */
191    pVars->inputStream.pBuffer = pExt->pInputBuffer;
192
193    pVars->inputStream.availableBits = 0;
194
195    pVars->inputStream.usedBits = 0;
196
197
198
199    /*
200     *  get sampling rate index
201     */
202
203    switch (samp_rate)
204    {
205        case 96000:
206            pVars->prog_config.sampling_rate_idx = 0;
207            break;
208        case 88200:
209            pVars->prog_config.sampling_rate_idx = 1;
210            break;
211        case 64000:
212            pVars->prog_config.sampling_rate_idx = 2;
213            break;
214        case 48000:
215            pVars->prog_config.sampling_rate_idx = 3;
216            break;
217        case 44100:
218            pVars->prog_config.sampling_rate_idx = 4;
219            break;
220        case 32000:
221            pVars->prog_config.sampling_rate_idx = 5;
222            break;
223        case 24000:
224            pVars->prog_config.sampling_rate_idx = 6;
225            break;
226        case 22050:
227            pVars->prog_config.sampling_rate_idx = 7;
228            break;
229        case 16000:
230            pVars->prog_config.sampling_rate_idx = 8;
231            break;
232        case 12000:
233            pVars->prog_config.sampling_rate_idx = 9;
234            break;
235        case 11025:
236            pVars->prog_config.sampling_rate_idx = 10;
237            break;
238        case 8000:
239            pVars->prog_config.sampling_rate_idx = 11;
240            break;
241        case 7350:
242            pVars->prog_config.sampling_rate_idx = 12;
243            break;
244        default:
245            status = -1;
246
247            break;
248    }
249
250    pVars->mc_info.sbrPresentFlag = 0;
251    pVars->mc_info.psPresentFlag = 0;
252#ifdef AAC_PLUS
253    pVars->mc_info.bDownSampledSbr = 0;
254#endif
255    pVars->mc_info.implicit_channeling = 0;
256    pVars->mc_info.nch = num_ch;
257    pVars->mc_info.upsamplingFactor = upsamplingFactor;
258
259
260    /*
261     *  Set number of channels
262     */
263
264    if (num_ch == 2)
265    {
266        pVars->prog_config.front.ele_is_cpe[0] = 1;
267    }
268    else if (num_ch == 1)
269    {
270        pVars->prog_config.front.ele_is_cpe[0] = 0;
271    }
272    else
273    {
274        status = -1; /* do not support more than two channels */
275        pVars->status = status;
276        return (status);
277    }
278
279
280    /*
281     *  Set AAC bitstream
282     */
283
284    if ((audioObjectType == MP4AUDIO_AAC_LC)        ||
285            (audioObjectType == MP4AUDIO_LTP))
286    {
287        pVars->aacPlusEnabled = false;
288
289        status = set_mc_info(&(pVars->mc_info),
290                             audioObjectType, /* previously profile */
291                             pVars->prog_config.sampling_rate_idx,
292                             pVars->prog_config.front.ele_tag[0],
293                             pVars->prog_config.front.ele_is_cpe[0],
294                             pVars->winmap, /*pVars->pWinSeqInfo,*/
295                             pVars->SFBWidth128);
296    }
297    else if ((audioObjectType == MP4AUDIO_SBR)        ||
298             (audioObjectType == MP4AUDIO_PS))
299    {
300        pVars->aacPlusEnabled = true;
301
302
303        status = set_mc_info(&(pVars->mc_info),
304                             MP4AUDIO_AAC_LC,
305                             pVars->prog_config.sampling_rate_idx,
306                             pVars->prog_config.front.ele_tag[0],
307                             pVars->prog_config.front.ele_is_cpe[0],
308                             pVars->winmap, /*pVars->pWinSeqInfo,*/
309                             pVars->SFBWidth128);
310
311        pVars->mc_info.sbrPresentFlag = 1;
312        if (audioObjectType == MP4AUDIO_PS)
313        {
314            pVars->mc_info.psPresentFlag = 1;
315        }
316
317        if (upsamplingFactor == 1)
318        {
319#ifdef AAC_PLUS
320            pVars->mc_info.bDownSampledSbr = 1;
321#endif
322
323            /*
324             *  Disable SBR decoding for any sbr-downsampled file whose SF is >= 24 KHz
325             */
326            if (pVars->prog_config.sampling_rate_idx < 6)
327            {
328                pVars->aacPlusEnabled = false;
329            }
330        }
331
332    }
333    else
334    {
335        status = -1;
336    }
337
338
339    /*
340     * Translate from units of bits back into units of words.
341     */
342    pExt->inputBufferUsedLength = 0;
343
344    pExt->remainderBits = 0;
345
346    pVars->bno++;
347
348    pExt->samplingRate = samp_rate * upsamplingFactor;
349
350    pExt->aacPlusEnabled = pVars->aacPlusEnabled;
351
352    /*
353     *  we default to 2 channel, even for mono files, (where channels have same content)
354     *  this is done to ensure support for enhanced aac+ with implicit signalling
355     */
356
357    pExt->encodedChannels = 2;
358
359    pExt->frameLength = 1024;
360#ifdef AAC_PLUS
361    pExt->aacPlusUpsamplingFactor = upsamplingFactor;
362#endif
363
364    pVars->status = status;
365
366    return (status);
367
368} /* PVMP4AudioDecoderDecodeFrame */
369