Coverage Report

Created: 2025-07-18 06:36

/proc/self/cwd/libfaad/decoder.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: decoder.c,v 1.119 2015/01/24 14:21:05 knik Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
#include <stdio.h>
36
#include <string.h>
37
38
#include "mp4.h"
39
#include "syntax.h"
40
#include "error.h"
41
#include "output.h"
42
#include "filtbank.h"
43
#include "drc.h"
44
#ifdef SBR_DEC
45
#include "sbr_dec.h"
46
#include "sbr_syntax.h"
47
#endif
48
#ifdef SSR_DEC
49
#include "ssr.h"
50
#endif
51
52
#ifdef ANALYSIS
53
uint16_t dbg_count;
54
#endif
55
56
#if !defined(PACKAGE_VERSION)
57
#error "PACKAGE_VERSION must be defined"
58
#endif
59
60
/* static function declarations */
61
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
62
                              NeAACDecFrameInfo *hInfo,
63
                              unsigned char *buffer,
64
                              unsigned long buffer_size,
65
                              void **sample_buffer2,
66
                              unsigned long sample_buffer_size);
67
static void create_channel_config(NeAACDecStruct *hDecoder,
68
                                  NeAACDecFrameInfo *hInfo);
69
70
71
int NeAACDecGetVersion(char **faad_id_string,
72
                                   char **faad_copyright_string)
73
325
{
74
325
    static char *libfaadName = PACKAGE_VERSION;
75
325
    static char *libCopyright =
76
325
        " Copyright 2002-2004: Ahead Software AG\n"
77
325
  " http://www.audiocoding.com\n"
78
325
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
325
    if (faad_id_string)
81
325
        *faad_id_string = libfaadName;
82
83
325
    if (faad_copyright_string)
84
325
        *faad_copyright_string = libCopyright;
85
86
325
    return 0;
87
325
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
325
{
91
325
    if (errcode >= NUM_ERROR_MESSAGES)
92
218
        return NULL;
93
107
    return err_msg[errcode];
94
325
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.30k
{
98
1.30k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.30k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
325
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
650
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
650
    cap += LD_DEC_CAP;
111
#endif
112
1.30k
#ifdef ERROR_RESILIENCE
113
1.30k
    cap += ERROR_RESILIENCE_CAP;
114
1.30k
#endif
115
#ifdef FIXED_POINT
116
650
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.30k
    return cap;
120
1.30k
}
NeAACDecGetCapabilities
Line
Count
Source
97
325
{
98
325
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
325
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
    cap += LD_DEC_CAP;
111
#endif
112
325
#ifdef ERROR_RESILIENCE
113
325
    cap += ERROR_RESILIENCE_CAP;
114
325
#endif
115
325
#ifdef FIXED_POINT
116
325
    cap += FIXED_POINT_CAP;
117
325
#endif
118
119
325
    return cap;
120
325
}
NeAACDecGetCapabilities
Line
Count
Source
97
325
{
98
325
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
325
    cap += LC_DEC_CAP;
102
103
325
#ifdef MAIN_DEC
104
325
    cap += MAIN_DEC_CAP;
105
325
#endif
106
325
#ifdef LTP_DEC
107
325
    cap += LTP_DEC_CAP;
108
325
#endif
109
325
#ifdef LD_DEC
110
325
    cap += LD_DEC_CAP;
111
325
#endif
112
325
#ifdef ERROR_RESILIENCE
113
325
    cap += ERROR_RESILIENCE_CAP;
114
325
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
325
    return cap;
120
325
}
NeAACDecGetCapabilities
Line
Count
Source
97
325
{
98
325
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
325
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
325
#ifdef LTP_DEC
107
325
    cap += LTP_DEC_CAP;
108
325
#endif
109
325
#ifdef LD_DEC
110
325
    cap += LD_DEC_CAP;
111
325
#endif
112
325
#ifdef ERROR_RESILIENCE
113
325
    cap += ERROR_RESILIENCE_CAP;
114
325
#endif
115
325
#ifdef FIXED_POINT
116
325
    cap += FIXED_POINT_CAP;
117
325
#endif
118
119
325
    return cap;
120
325
}
NeAACDecGetCapabilities
Line
Count
Source
97
325
{
98
325
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
325
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
    cap += LD_DEC_CAP;
111
#endif
112
325
#ifdef ERROR_RESILIENCE
113
325
    cap += ERROR_RESILIENCE_CAP;
114
325
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
325
    return cap;
120
325
}
121
122
const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 };
123
NeAACDecHandle NeAACDecOpen(void)
124
57.5k
{
125
57.5k
    uint8_t i;
126
57.5k
    NeAACDecStruct *hDecoder = NULL;
127
128
57.5k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
57.5k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
57.5k
    hDecoder->cmes = mes;
134
57.5k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
57.5k
    hDecoder->config.defObjectType = MAIN;
136
57.5k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
57.5k
    hDecoder->config.downMatrix = 0;
138
57.5k
    hDecoder->adts_header_present = 0;
139
57.5k
    hDecoder->adif_header_present = 0;
140
57.5k
    hDecoder->latm_header_present = 0;
141
57.5k
#ifdef ERROR_RESILIENCE
142
57.5k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
57.5k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
57.5k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
57.5k
#endif
146
57.5k
    hDecoder->frameLength = 1024;
147
148
57.5k
    hDecoder->frame = 0;
149
57.5k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
57.5k
    hDecoder->__r1 = 0x2bb431ea;
153
57.5k
    hDecoder->__r2 = 0x206155b7;
154
155
3.73M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.68M
    {
157
3.68M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.68M
        hDecoder->window_shape_prev[i] = 0;
159
3.68M
        hDecoder->time_out[i] = NULL;
160
3.68M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
3.68M
    }
173
174
57.5k
#ifdef SBR_DEC
175
2.81M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.76M
    {
177
2.76M
        hDecoder->sbr[i] = NULL;
178
2.76M
    }
179
57.5k
#endif
180
181
57.5k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
57.5k
    return hDecoder;
184
57.5k
}
NeAACDecOpen
Line
Count
Source
124
10.8k
{
125
10.8k
    uint8_t i;
126
10.8k
    NeAACDecStruct *hDecoder = NULL;
127
128
10.8k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
10.8k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
10.8k
    hDecoder->cmes = mes;
134
10.8k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
10.8k
    hDecoder->config.defObjectType = MAIN;
136
10.8k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
10.8k
    hDecoder->config.downMatrix = 0;
138
10.8k
    hDecoder->adts_header_present = 0;
139
10.8k
    hDecoder->adif_header_present = 0;
140
10.8k
    hDecoder->latm_header_present = 0;
141
10.8k
#ifdef ERROR_RESILIENCE
142
10.8k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
10.8k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
10.8k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
10.8k
#endif
146
10.8k
    hDecoder->frameLength = 1024;
147
148
10.8k
    hDecoder->frame = 0;
149
10.8k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
10.8k
    hDecoder->__r1 = 0x2bb431ea;
153
10.8k
    hDecoder->__r2 = 0x206155b7;
154
155
708k
    for (i = 0; i < MAX_CHANNELS; i++)
156
697k
    {
157
697k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
697k
        hDecoder->window_shape_prev[i] = 0;
159
697k
        hDecoder->time_out[i] = NULL;
160
697k
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
697k
    }
173
174
10.8k
#ifdef SBR_DEC
175
534k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
523k
    {
177
523k
        hDecoder->sbr[i] = NULL;
178
523k
    }
179
10.8k
#endif
180
181
10.8k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
10.8k
    return hDecoder;
184
10.8k
}
NeAACDecOpen
Line
Count
Source
124
17.8k
{
125
17.8k
    uint8_t i;
126
17.8k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.8k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.8k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.8k
    hDecoder->cmes = mes;
134
17.8k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.8k
    hDecoder->config.defObjectType = MAIN;
136
17.8k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.8k
    hDecoder->config.downMatrix = 0;
138
17.8k
    hDecoder->adts_header_present = 0;
139
17.8k
    hDecoder->adif_header_present = 0;
140
17.8k
    hDecoder->latm_header_present = 0;
141
17.8k
#ifdef ERROR_RESILIENCE
142
17.8k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.8k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.8k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.8k
#endif
146
17.8k
    hDecoder->frameLength = 1024;
147
148
17.8k
    hDecoder->frame = 0;
149
17.8k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.8k
    hDecoder->__r1 = 0x2bb431ea;
153
17.8k
    hDecoder->__r2 = 0x206155b7;
154
155
1.16M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.14M
    {
157
1.14M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.14M
        hDecoder->window_shape_prev[i] = 0;
159
1.14M
        hDecoder->time_out[i] = NULL;
160
1.14M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
1.14M
#ifdef MAIN_DEC
166
1.14M
        hDecoder->pred_stat[i] = NULL;
167
1.14M
#endif
168
1.14M
#ifdef LTP_DEC
169
1.14M
        hDecoder->ltp_lag[i] = 0;
170
1.14M
        hDecoder->lt_pred_stat[i] = NULL;
171
1.14M
#endif
172
1.14M
    }
173
174
17.8k
#ifdef SBR_DEC
175
875k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
857k
    {
177
857k
        hDecoder->sbr[i] = NULL;
178
857k
    }
179
17.8k
#endif
180
181
17.8k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.8k
    return hDecoder;
184
17.8k
}
NeAACDecOpen
Line
Count
Source
124
10.8k
{
125
10.8k
    uint8_t i;
126
10.8k
    NeAACDecStruct *hDecoder = NULL;
127
128
10.8k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
10.8k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
10.8k
    hDecoder->cmes = mes;
134
10.8k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
10.8k
    hDecoder->config.defObjectType = MAIN;
136
10.8k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
10.8k
    hDecoder->config.downMatrix = 0;
138
10.8k
    hDecoder->adts_header_present = 0;
139
10.8k
    hDecoder->adif_header_present = 0;
140
10.8k
    hDecoder->latm_header_present = 0;
141
10.8k
#ifdef ERROR_RESILIENCE
142
10.8k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
10.8k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
10.8k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
10.8k
#endif
146
10.8k
    hDecoder->frameLength = 1024;
147
148
10.8k
    hDecoder->frame = 0;
149
10.8k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
10.8k
    hDecoder->__r1 = 0x2bb431ea;
153
10.8k
    hDecoder->__r2 = 0x206155b7;
154
155
708k
    for (i = 0; i < MAX_CHANNELS; i++)
156
697k
    {
157
697k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
697k
        hDecoder->window_shape_prev[i] = 0;
159
697k
        hDecoder->time_out[i] = NULL;
160
697k
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
697k
#ifdef LTP_DEC
169
697k
        hDecoder->ltp_lag[i] = 0;
170
697k
        hDecoder->lt_pred_stat[i] = NULL;
171
697k
#endif
172
697k
    }
173
174
10.8k
#ifdef SBR_DEC
175
534k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
523k
    {
177
523k
        hDecoder->sbr[i] = NULL;
178
523k
    }
179
10.8k
#endif
180
181
10.8k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
10.8k
    return hDecoder;
184
10.8k
}
NeAACDecOpen
Line
Count
Source
124
17.8k
{
125
17.8k
    uint8_t i;
126
17.8k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.8k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.8k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.8k
    hDecoder->cmes = mes;
134
17.8k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.8k
    hDecoder->config.defObjectType = MAIN;
136
17.8k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.8k
    hDecoder->config.downMatrix = 0;
138
17.8k
    hDecoder->adts_header_present = 0;
139
17.8k
    hDecoder->adif_header_present = 0;
140
17.8k
    hDecoder->latm_header_present = 0;
141
17.8k
#ifdef ERROR_RESILIENCE
142
17.8k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.8k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.8k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.8k
#endif
146
17.8k
    hDecoder->frameLength = 1024;
147
148
17.8k
    hDecoder->frame = 0;
149
17.8k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.8k
    hDecoder->__r1 = 0x2bb431ea;
153
17.8k
    hDecoder->__r2 = 0x206155b7;
154
155
1.16M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.14M
    {
157
1.14M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.14M
        hDecoder->window_shape_prev[i] = 0;
159
1.14M
        hDecoder->time_out[i] = NULL;
160
1.14M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
1.14M
    }
173
174
17.8k
#ifdef SBR_DEC
175
875k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
857k
    {
177
857k
        hDecoder->sbr[i] = NULL;
178
857k
    }
179
17.8k
#endif
180
181
17.8k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.8k
    return hDecoder;
184
17.8k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
27.7k
{
188
27.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
27.7k
    if (hDecoder)
190
27.7k
    {
191
27.7k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
27.7k
        return config;
194
27.7k
    }
195
196
0
    return NULL;
197
27.7k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
27.9k
{
202
27.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
27.9k
    if (hDecoder && config)
204
27.9k
    {
205
        /* check if we can decode this object type */
206
27.9k
        if (can_decode_ot(config->defObjectType) < 0)
207
25
            return 0;
208
27.9k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
27.9k
        if (config->defSampleRate == 0)
212
4
            return 0;
213
27.9k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
27.9k
#ifdef FIXED_POINT
217
27.9k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
212
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
27.7k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
27.7k
        if (config->downMatrix > 1)
226
30
            return 0;
227
27.7k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
27.7k
        return 1;
231
27.7k
    }
232
233
0
    return 0;
234
27.9k
}
235
236
237
#if 0
238
static int latmCheck(latm_header *latm, bitfile *ld)
239
{
240
    uint32_t good=0, bad=0, bits, m;
241
242
    while (ld->bytes_left)
243
    {
244
        bits = faad_latm_frame(latm, ld);
245
        if(bits==0xFFFFFFFF)
246
            bad++;
247
        else
248
        {
249
            good++;
250
            while(bits>0)
251
            {
252
                m = min(bits, 8);
253
                faad_getbits(ld, m);
254
                bits -= m;
255
            }
256
        }
257
    }
258
259
    return (good>0);
260
}
261
#endif
262
263
long NeAACDecInit(NeAACDecHandle hpDecoder,
264
                              unsigned char *buffer,
265
                              unsigned long buffer_size,
266
                              unsigned long *samplerate,
267
                              unsigned char *channels)
268
19.4k
{
269
19.4k
    uint32_t bits = 0;
270
19.4k
    bitfile ld;
271
19.4k
    adif_header adif;
272
19.4k
    adts_header adts;
273
19.4k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
19.4k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
46
        return -1;
278
279
19.3k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
19.3k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
19.3k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
19.3k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
19.3k
    *channels = 1;
284
285
19.3k
    if (buffer != NULL)
286
19.3k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
19.3k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
19.3k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
19.3k
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
407
        {
314
407
            hDecoder->adif_header_present = 1;
315
316
407
            get_adif_header(&adif, &ld);
317
407
            faad_byte_align(&ld);
318
319
407
            hDecoder->sf_index = adif.pce[0].sf_index;
320
407
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
407
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
407
            *channels = adif.pce[0].channels;
324
325
407
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
407
            hDecoder->pce_set = 1;
327
328
407
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
18.9k
        } else if (adts_frame(&adts, &ld) == 0) {
332
569
            hDecoder->adts_header_present = 1;
333
334
569
            hDecoder->sf_index = adts.sf_index;
335
569
            hDecoder->object_type = adts.profile + 1;
336
337
569
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
569
            *channels = (adts.channel_configuration > 6) ?
339
504
                2 : adts.channel_configuration;
340
569
        }
341
342
19.3k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
19.3k
        faad_endbits(&ld);
348
19.3k
    }
349
350
19.3k
    if (!*samplerate)
351
175
        return -1;
352
353
19.2k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
19.2k
    if (*channels == 1)
356
18.4k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
18.4k
        *channels = 2;
359
18.4k
    }
360
19.2k
#endif
361
362
19.2k
    hDecoder->channelConfiguration = *channels;
363
364
19.2k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
19.2k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
9.02k
    {
368
9.02k
        *samplerate *= 2;
369
9.02k
        hDecoder->forceUpSampling = 1;
370
10.1k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
10.1k
        hDecoder->downSampledSBR = 1;
372
10.1k
    }
373
19.2k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
19.2k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
12.4k
    if (hDecoder->object_type == LD)
385
519
        hDecoder->frameLength >>= 1;
386
#endif
387
388
19.2k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
209
        return -1;
390
391
18.9k
    return bits;
392
19.2k
}
NeAACDecInit
Line
Count
Source
268
6.84k
{
269
6.84k
    uint32_t bits = 0;
270
6.84k
    bitfile ld;
271
6.84k
    adif_header adif;
272
6.84k
    adts_header adts;
273
6.84k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
6.84k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
24
        return -1;
278
279
6.82k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
6.82k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
6.82k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
6.82k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
6.82k
    *channels = 1;
284
285
6.82k
    if (buffer != NULL)
286
6.82k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
6.82k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
6.82k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
6.82k
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
167
        {
314
167
            hDecoder->adif_header_present = 1;
315
316
167
            get_adif_header(&adif, &ld);
317
167
            faad_byte_align(&ld);
318
319
167
            hDecoder->sf_index = adif.pce[0].sf_index;
320
167
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
167
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
167
            *channels = adif.pce[0].channels;
324
325
167
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
167
            hDecoder->pce_set = 1;
327
328
167
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
6.65k
        } else if (adts_frame(&adts, &ld) == 0) {
332
103
            hDecoder->adts_header_present = 1;
333
334
103
            hDecoder->sf_index = adts.sf_index;
335
103
            hDecoder->object_type = adts.profile + 1;
336
337
103
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
103
            *channels = (adts.channel_configuration > 6) ?
339
89
                2 : adts.channel_configuration;
340
103
        }
341
342
6.82k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
6.82k
        faad_endbits(&ld);
348
6.82k
    }
349
350
6.82k
    if (!*samplerate)
351
72
        return -1;
352
353
6.74k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
6.74k
    if (*channels == 1)
356
6.55k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
6.55k
        *channels = 2;
359
6.55k
    }
360
6.74k
#endif
361
362
6.74k
    hDecoder->channelConfiguration = *channels;
363
364
6.74k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
6.74k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
3.23k
    {
368
3.23k
        *samplerate *= 2;
369
3.23k
        hDecoder->forceUpSampling = 1;
370
3.51k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
3.51k
        hDecoder->downSampledSBR = 1;
372
3.51k
    }
373
6.74k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
6.74k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
    if (hDecoder->object_type == LD)
385
        hDecoder->frameLength >>= 1;
386
#endif
387
388
6.74k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
106
        return -1;
390
391
6.64k
    return bits;
392
6.74k
}
NeAACDecInit
Line
Count
Source
268
12.5k
{
269
12.5k
    uint32_t bits = 0;
270
12.5k
    bitfile ld;
271
12.5k
    adif_header adif;
272
12.5k
    adts_header adts;
273
12.5k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
12.5k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
22
        return -1;
278
279
12.5k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
12.5k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
12.5k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
12.5k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
12.5k
    *channels = 1;
284
285
12.5k
    if (buffer != NULL)
286
12.5k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
12.5k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
12.5k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
12.5k
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
240
        {
314
240
            hDecoder->adif_header_present = 1;
315
316
240
            get_adif_header(&adif, &ld);
317
240
            faad_byte_align(&ld);
318
319
240
            hDecoder->sf_index = adif.pce[0].sf_index;
320
240
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
240
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
240
            *channels = adif.pce[0].channels;
324
325
240
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
240
            hDecoder->pce_set = 1;
327
328
240
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
12.3k
        } else if (adts_frame(&adts, &ld) == 0) {
332
466
            hDecoder->adts_header_present = 1;
333
334
466
            hDecoder->sf_index = adts.sf_index;
335
466
            hDecoder->object_type = adts.profile + 1;
336
337
466
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
466
            *channels = (adts.channel_configuration > 6) ?
339
415
                2 : adts.channel_configuration;
340
466
        }
341
342
12.5k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
12.5k
        faad_endbits(&ld);
348
12.5k
    }
349
350
12.5k
    if (!*samplerate)
351
103
        return -1;
352
353
12.4k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
12.4k
    if (*channels == 1)
356
11.8k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
11.8k
        *channels = 2;
359
11.8k
    }
360
12.4k
#endif
361
362
12.4k
    hDecoder->channelConfiguration = *channels;
363
364
12.4k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
12.4k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
5.79k
    {
368
5.79k
        *samplerate *= 2;
369
5.79k
        hDecoder->forceUpSampling = 1;
370
6.66k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
6.66k
        hDecoder->downSampledSBR = 1;
372
6.66k
    }
373
12.4k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
12.4k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
12.4k
#ifdef LD_DEC
384
12.4k
    if (hDecoder->object_type == LD)
385
519
        hDecoder->frameLength >>= 1;
386
12.4k
#endif
387
388
12.4k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
103
        return -1;
390
391
12.3k
    return bits;
392
12.4k
}
393
394
/* Init the library using a DecoderSpecificInfo */
395
char NeAACDecInit2(NeAACDecHandle hpDecoder,
396
                               unsigned char *pBuffer,
397
                               unsigned long SizeOfDecoderSpecificInfo,
398
                               unsigned long *samplerate,
399
                               unsigned char *channels)
400
8.28k
{
401
8.28k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
8.28k
    int8_t rc;
403
8.28k
    mp4AudioSpecificConfig mp4ASC;
404
405
8.28k
    if((hDecoder == NULL)
406
8.28k
        || (pBuffer == NULL)
407
8.28k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.28k
        || (samplerate == NULL)
409
8.28k
        || (channels == NULL))
410
14
    {
411
14
        return -1;
412
14
    }
413
414
8.27k
    hDecoder->adif_header_present = 0;
415
8.27k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.27k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.27k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.27k
    *samplerate = mp4ASC.samplingFrequency;
423
8.27k
    if (mp4ASC.channelsConfiguration)
424
6.93k
    {
425
6.93k
        *channels = mp4ASC.channelsConfiguration;
426
6.93k
    } else {
427
1.33k
        *channels = hDecoder->pce.channels;
428
1.33k
        hDecoder->pce_set = 1;
429
1.33k
    }
430
8.27k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.27k
    if (*channels == 1)
433
64
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
64
        *channels = 2;
436
64
    }
437
8.27k
#endif
438
8.27k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.27k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.27k
#ifdef ERROR_RESILIENCE
441
8.27k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.27k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.27k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.27k
#endif
445
8.27k
#ifdef SBR_DEC
446
8.27k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.27k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.27k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.27k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
8.27k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
5.36k
    {
456
5.36k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
5.36k
    }
458
8.27k
#endif
459
460
8.27k
    if (rc != 0)
461
96
    {
462
96
        return rc;
463
96
    }
464
8.17k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.17k
    if (mp4ASC.frameLengthFlag)
466
5.11k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
5.11k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
8.17k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
5.43k
    if (hDecoder->object_type == LD)
482
633
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.17k
    return 0;
486
8.27k
}
NeAACDecInit2
Line
Count
Source
400
2.78k
{
401
2.78k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
2.78k
    int8_t rc;
403
2.78k
    mp4AudioSpecificConfig mp4ASC;
404
405
2.78k
    if((hDecoder == NULL)
406
2.78k
        || (pBuffer == NULL)
407
2.78k
        || (SizeOfDecoderSpecificInfo < 2)
408
2.78k
        || (samplerate == NULL)
409
2.78k
        || (channels == NULL))
410
7
    {
411
7
        return -1;
412
7
    }
413
414
2.77k
    hDecoder->adif_header_present = 0;
415
2.77k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
2.77k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
2.77k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
2.77k
    *samplerate = mp4ASC.samplingFrequency;
423
2.77k
    if (mp4ASC.channelsConfiguration)
424
2.21k
    {
425
2.21k
        *channels = mp4ASC.channelsConfiguration;
426
2.21k
    } else {
427
566
        *channels = hDecoder->pce.channels;
428
566
        hDecoder->pce_set = 1;
429
566
    }
430
2.77k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
2.77k
    if (*channels == 1)
433
46
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
46
        *channels = 2;
436
46
    }
437
2.77k
#endif
438
2.77k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
2.77k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
2.77k
#ifdef ERROR_RESILIENCE
441
2.77k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
2.77k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
2.77k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
2.77k
#endif
445
2.77k
#ifdef SBR_DEC
446
2.77k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
2.77k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
2.77k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
2.77k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
2.77k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
1.66k
    {
456
1.66k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
1.66k
    }
458
2.77k
#endif
459
460
2.77k
    if (rc != 0)
461
40
    {
462
40
        return rc;
463
40
    }
464
2.73k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
2.73k
    if (mp4ASC.frameLengthFlag)
466
1.81k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
1.81k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
2.73k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
    if (hDecoder->object_type == LD)
482
        hDecoder->frameLength >>= 1;
483
#endif
484
485
2.73k
    return 0;
486
2.77k
}
NeAACDecInit2
Line
Count
Source
400
5.50k
{
401
5.50k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
5.50k
    int8_t rc;
403
5.50k
    mp4AudioSpecificConfig mp4ASC;
404
405
5.50k
    if((hDecoder == NULL)
406
5.50k
        || (pBuffer == NULL)
407
5.50k
        || (SizeOfDecoderSpecificInfo < 2)
408
5.50k
        || (samplerate == NULL)
409
5.50k
        || (channels == NULL))
410
7
    {
411
7
        return -1;
412
7
    }
413
414
5.49k
    hDecoder->adif_header_present = 0;
415
5.49k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
5.49k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
5.49k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
5.49k
    *samplerate = mp4ASC.samplingFrequency;
423
5.49k
    if (mp4ASC.channelsConfiguration)
424
4.72k
    {
425
4.72k
        *channels = mp4ASC.channelsConfiguration;
426
4.72k
    } else {
427
771
        *channels = hDecoder->pce.channels;
428
771
        hDecoder->pce_set = 1;
429
771
    }
430
5.49k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
5.49k
    if (*channels == 1)
433
18
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
18
        *channels = 2;
436
18
    }
437
5.49k
#endif
438
5.49k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
5.49k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
5.49k
#ifdef ERROR_RESILIENCE
441
5.49k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
5.49k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
5.49k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
5.49k
#endif
445
5.49k
#ifdef SBR_DEC
446
5.49k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
5.49k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
5.49k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
5.49k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
5.49k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
3.69k
    {
456
3.69k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
3.69k
    }
458
5.49k
#endif
459
460
5.49k
    if (rc != 0)
461
56
    {
462
56
        return rc;
463
56
    }
464
5.43k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
5.43k
    if (mp4ASC.frameLengthFlag)
466
3.30k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.30k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
5.43k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
5.43k
#ifdef LD_DEC
481
5.43k
    if (hDecoder->object_type == LD)
482
633
        hDecoder->frameLength >>= 1;
483
5.43k
#endif
484
485
5.43k
    return 0;
486
5.49k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
781
{
493
781
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
781
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
781
    NeAACDecClose(*hDecoder);
498
499
781
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
781
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
781
    (*hDecoder)->config.defSampleRate = samplerate;
505
781
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
781
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
781
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
781
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
781
#endif
510
781
    (*hDecoder)->frameLength = 960;
511
781
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
781
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
781
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
128
        (*hDecoder)->channelConfiguration = 2;
516
653
    else
517
653
        (*hDecoder)->channelConfiguration = 1;
518
519
781
#ifdef SBR_DEC
520
781
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
104
        (*hDecoder)->sbr_present_flag = 0;
522
677
    else
523
677
        (*hDecoder)->sbr_present_flag = 1;
524
781
#endif
525
526
781
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
781
    return 0;
529
781
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
10.4k
{
534
10.4k
    uint8_t i;
535
10.4k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
10.4k
    if (hDecoder == NULL)
538
0
        return;
539
540
#ifdef PROFILE
541
    printf("AAC decoder total:  %I64d cycles\n", hDecoder->cycles);
542
    printf("requant:            %I64d cycles\n", hDecoder->requant_cycles);
543
    printf("spectral_data:      %I64d cycles\n", hDecoder->spectral_cycles);
544
    printf("scalefactors:       %I64d cycles\n", hDecoder->scalefac_cycles);
545
    printf("output:             %I64d cycles\n", hDecoder->output_cycles);
546
#endif
547
548
682k
    for (i = 0; i < MAX_CHANNELS; i++)
549
671k
    {
550
671k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
671k
        if (hDecoder->fb_intermed[i]) faad_free(hDecoder->fb_intermed[i]);
552
#ifdef SSR_DEC
553
        if (hDecoder->ssr_overlap[i]) faad_free(hDecoder->ssr_overlap[i]);
554
        if (hDecoder->prev_fmd[i]) faad_free(hDecoder->prev_fmd[i]);
555
#endif
556
#ifdef MAIN_DEC
557
        if (hDecoder->pred_stat[i]) faad_free(hDecoder->pred_stat[i]);
558
#endif
559
#ifdef LTP_DEC
560
        if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);
561
#endif
562
671k
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
10.4k
        filter_bank_end(hDecoder->fb);
570
571
10.4k
    drc_end(hDecoder->drc);
572
573
10.4k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
10.4k
#ifdef SBR_DEC
576
514k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
503k
    {
578
503k
        if (hDecoder->sbr[i])
579
123k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
503k
    }
581
10.4k
#endif
582
583
10.4k
    if (hDecoder) faad_free(hDecoder);
584
10.4k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
19.9k
{
588
19.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
19.9k
    if (hDecoder)
590
19.9k
    {
591
19.9k
        hDecoder->postSeekResetFlag = 1;
592
593
19.9k
        if (frame != -1)
594
19.9k
            hDecoder->frame = frame;
595
19.9k
    }
596
19.9k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
41.0k
{
600
41.0k
    hInfo->num_front_channels = 0;
601
41.0k
    hInfo->num_side_channels = 0;
602
41.0k
    hInfo->num_back_channels = 0;
603
41.0k
    hInfo->num_lfe_channels = 0;
604
41.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
41.0k
    if (hDecoder->downMatrix)
607
4.37k
    {
608
4.37k
        hInfo->num_front_channels = 2;
609
4.37k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.37k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.37k
        return;
612
4.37k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
36.6k
    if (hDecoder->pce_set)
617
1.41k
    {
618
1.41k
        uint8_t i, chpos = 0;
619
1.41k
        uint8_t chdir, back_center = 0;
620
621
1.41k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.41k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.41k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.41k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.41k
        chdir = hInfo->num_front_channels;
626
1.41k
        if (chdir & 1)
627
624
        {
628
624
#if (defined(PS_DEC) || defined(DRM_PS))
629
624
            if (hInfo->num_front_channels == 1 &&
630
624
                hInfo->num_side_channels == 0 &&
631
624
                hInfo->num_back_channels == 0 &&
632
624
                hInfo->num_lfe_channels == 0)
633
152
            {
634
                /* When PS is enabled output is always stereo */
635
152
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
152
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
152
            } else
638
472
#endif
639
472
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
624
            chdir--;
641
624
        }
642
7.16k
        for (i = 0; i < chdir; i++)
643
5.74k
        {
644
5.74k
            hInfo->channel_position[chpos++] =
645
5.74k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
5.74k
        }
647
648
6.46k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
5.04k
        {
650
5.04k
            hInfo->channel_position[chpos++] =
651
5.04k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
5.04k
        }
653
654
1.41k
        chdir = hInfo->num_back_channels;
655
1.41k
        if (chdir & 1)
656
268
        {
657
268
            back_center = 1;
658
268
            chdir--;
659
268
        }
660
5.44k
        for (i = 0; i < chdir; i++)
661
4.02k
        {
662
4.02k
            hInfo->channel_position[chpos++] =
663
4.02k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
4.02k
        }
665
1.41k
        if (back_center)
666
268
        {
667
268
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
268
        }
669
670
2.34k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
928
        {
672
928
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
928
        }
674
675
35.2k
    } else {
676
35.2k
        switch (hDecoder->channelConfiguration)
677
35.2k
        {
678
1.70k
        case 1:
679
1.70k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
1.70k
            hInfo->num_front_channels = 2;
682
1.70k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
1.70k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
1.70k
            break;
689
16.4k
        case 2:
690
16.4k
            hInfo->num_front_channels = 2;
691
16.4k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
16.4k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
16.4k
            break;
694
2.58k
        case 3:
695
2.58k
            hInfo->num_front_channels = 3;
696
2.58k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
2.58k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
2.58k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
2.58k
            break;
700
4.02k
        case 4:
701
4.02k
            hInfo->num_front_channels = 3;
702
4.02k
            hInfo->num_back_channels = 1;
703
4.02k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.02k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.02k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.02k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.02k
            break;
708
2.91k
        case 5:
709
2.91k
            hInfo->num_front_channels = 3;
710
2.91k
            hInfo->num_back_channels = 2;
711
2.91k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.91k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.91k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.91k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.91k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.91k
            break;
717
1.50k
        case 6:
718
1.50k
            hInfo->num_front_channels = 3;
719
1.50k
            hInfo->num_back_channels = 2;
720
1.50k
            hInfo->num_lfe_channels = 1;
721
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.50k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.50k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.50k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.50k
            break;
728
5.20k
        case 7:
729
5.20k
            hInfo->num_front_channels = 3;
730
5.20k
            hInfo->num_side_channels = 2;
731
5.20k
            hInfo->num_back_channels = 2;
732
5.20k
            hInfo->num_lfe_channels = 1;
733
5.20k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
5.20k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
5.20k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
5.20k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
5.20k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
5.20k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
5.20k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
5.20k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
5.20k
            break;
742
840
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
840
            {
744
840
                uint8_t i;
745
840
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
840
                if (ch & 1) /* there's either a center front or a center back channel */
747
308
                {
748
308
                    uint8_t ch1 = (ch-1)/2;
749
308
                    if (hDecoder->first_syn_ele == ID_SCE)
750
208
                    {
751
208
                        hInfo->num_front_channels = ch1 + 1;
752
208
                        hInfo->num_back_channels = ch1;
753
208
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
856
                        for (i = 1; i <= ch1; i+=2)
755
648
                        {
756
648
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
648
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
648
                        }
759
856
                        for (i = ch1+1; i < ch; i+=2)
760
648
                        {
761
648
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
648
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
648
                        }
764
208
                    } else {
765
100
                        hInfo->num_front_channels = ch1;
766
100
                        hInfo->num_back_channels = ch1 + 1;
767
556
                        for (i = 0; i < ch1; i+=2)
768
456
                        {
769
456
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
456
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
456
                        }
772
556
                        for (i = ch1; i < ch-1; i+=2)
773
456
                        {
774
456
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
456
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
456
                        }
777
100
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
100
                    }
779
532
                } else {
780
532
                    uint8_t ch1 = (ch)/2;
781
532
                    hInfo->num_front_channels = ch1;
782
532
                    hInfo->num_back_channels = ch1;
783
532
                    if (ch1 & 1)
784
236
                    {
785
236
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.64k
                        for (i = 1; i <= ch1; i+=2)
787
1.40k
                        {
788
1.40k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.40k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.40k
                        }
791
1.40k
                        for (i = ch1+1; i < ch-1; i+=2)
792
1.16k
                        {
793
1.16k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
1.16k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
1.16k
                        }
796
236
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
296
                    } else {
798
1.36k
                        for (i = 0; i < ch1; i+=2)
799
1.06k
                        {
800
1.06k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
1.06k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
1.06k
                        }
803
1.36k
                        for (i = ch1; i < ch; i+=2)
804
1.06k
                        {
805
1.06k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
1.06k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
1.06k
                        }
808
296
                    }
809
532
                }
810
840
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
1.93k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
1.09k
                {
813
1.09k
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
1.09k
                }
815
840
            }
816
840
            break;
817
35.2k
        }
818
35.2k
    }
819
36.6k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.17k
    if (hDecoder->pce_set)
617
354
    {
618
354
        uint8_t i, chpos = 0;
619
354
        uint8_t chdir, back_center = 0;
620
621
354
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
354
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
354
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
354
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
354
        chdir = hInfo->num_front_channels;
626
354
        if (chdir & 1)
627
156
        {
628
156
#if (defined(PS_DEC) || defined(DRM_PS))
629
156
            if (hInfo->num_front_channels == 1 &&
630
156
                hInfo->num_side_channels == 0 &&
631
156
                hInfo->num_back_channels == 0 &&
632
156
                hInfo->num_lfe_channels == 0)
633
38
            {
634
                /* When PS is enabled output is always stereo */
635
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
38
            } else
638
118
#endif
639
118
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
156
            chdir--;
641
156
        }
642
1.79k
        for (i = 0; i < chdir; i++)
643
1.43k
        {
644
1.43k
            hInfo->channel_position[chpos++] =
645
1.43k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.43k
        }
647
648
1.61k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.26k
        {
650
1.26k
            hInfo->channel_position[chpos++] =
651
1.26k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.26k
        }
653
654
354
        chdir = hInfo->num_back_channels;
655
354
        if (chdir & 1)
656
67
        {
657
67
            back_center = 1;
658
67
            chdir--;
659
67
        }
660
1.36k
        for (i = 0; i < chdir; i++)
661
1.00k
        {
662
1.00k
            hInfo->channel_position[chpos++] =
663
1.00k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
1.00k
        }
665
354
        if (back_center)
666
67
        {
667
67
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
67
        }
669
670
586
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
232
        {
672
232
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
232
        }
674
675
8.81k
    } else {
676
8.81k
        switch (hDecoder->channelConfiguration)
677
8.81k
        {
678
427
        case 1:
679
427
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
427
            hInfo->num_front_channels = 2;
682
427
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
427
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
427
            break;
689
4.12k
        case 2:
690
4.12k
            hInfo->num_front_channels = 2;
691
4.12k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.12k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.12k
            break;
694
645
        case 3:
695
645
            hInfo->num_front_channels = 3;
696
645
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
645
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
645
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
645
            break;
700
1.00k
        case 4:
701
1.00k
            hInfo->num_front_channels = 3;
702
1.00k
            hInfo->num_back_channels = 1;
703
1.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.00k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.00k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.00k
            break;
708
728
        case 5:
709
728
            hInfo->num_front_channels = 3;
710
728
            hInfo->num_back_channels = 2;
711
728
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
728
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
728
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
728
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
728
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
728
            break;
717
375
        case 6:
718
375
            hInfo->num_front_channels = 3;
719
375
            hInfo->num_back_channels = 2;
720
375
            hInfo->num_lfe_channels = 1;
721
375
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
375
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
375
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
375
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
375
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
375
            hInfo->channel_position[5] = LFE_CHANNEL;
727
375
            break;
728
1.30k
        case 7:
729
1.30k
            hInfo->num_front_channels = 3;
730
1.30k
            hInfo->num_side_channels = 2;
731
1.30k
            hInfo->num_back_channels = 2;
732
1.30k
            hInfo->num_lfe_channels = 1;
733
1.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.30k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.30k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.30k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.30k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.30k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.30k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.30k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.30k
            break;
742
210
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
210
            {
744
210
                uint8_t i;
745
210
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
210
                if (ch & 1) /* there's either a center front or a center back channel */
747
77
                {
748
77
                    uint8_t ch1 = (ch-1)/2;
749
77
                    if (hDecoder->first_syn_ele == ID_SCE)
750
52
                    {
751
52
                        hInfo->num_front_channels = ch1 + 1;
752
52
                        hInfo->num_back_channels = ch1;
753
52
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
214
                        for (i = 1; i <= ch1; i+=2)
755
162
                        {
756
162
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
162
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
162
                        }
759
214
                        for (i = ch1+1; i < ch; i+=2)
760
162
                        {
761
162
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
162
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
162
                        }
764
52
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
139
                        for (i = 0; i < ch1; i+=2)
768
114
                        {
769
114
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
114
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
114
                        }
772
139
                        for (i = ch1; i < ch-1; i+=2)
773
114
                        {
774
114
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
114
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
114
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
133
                } else {
780
133
                    uint8_t ch1 = (ch)/2;
781
133
                    hInfo->num_front_channels = ch1;
782
133
                    hInfo->num_back_channels = ch1;
783
133
                    if (ch1 & 1)
784
59
                    {
785
59
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
410
                        for (i = 1; i <= ch1; i+=2)
787
351
                        {
788
351
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
351
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
351
                        }
791
351
                        for (i = ch1+1; i < ch-1; i+=2)
792
292
                        {
793
292
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
292
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
292
                        }
796
59
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
340
                        for (i = 0; i < ch1; i+=2)
799
266
                        {
800
266
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
266
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
266
                        }
803
340
                        for (i = ch1; i < ch; i+=2)
804
266
                        {
805
266
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
266
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
266
                        }
808
74
                    }
809
133
                }
810
210
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
484
                for (i = ch; i < hDecoder->fr_channels; i++)
812
274
                {
813
274
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
274
                }
815
210
            }
816
210
            break;
817
8.81k
        }
818
8.81k
    }
819
9.17k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.17k
    if (hDecoder->pce_set)
617
354
    {
618
354
        uint8_t i, chpos = 0;
619
354
        uint8_t chdir, back_center = 0;
620
621
354
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
354
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
354
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
354
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
354
        chdir = hInfo->num_front_channels;
626
354
        if (chdir & 1)
627
156
        {
628
156
#if (defined(PS_DEC) || defined(DRM_PS))
629
156
            if (hInfo->num_front_channels == 1 &&
630
156
                hInfo->num_side_channels == 0 &&
631
156
                hInfo->num_back_channels == 0 &&
632
156
                hInfo->num_lfe_channels == 0)
633
38
            {
634
                /* When PS is enabled output is always stereo */
635
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
38
            } else
638
118
#endif
639
118
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
156
            chdir--;
641
156
        }
642
1.79k
        for (i = 0; i < chdir; i++)
643
1.43k
        {
644
1.43k
            hInfo->channel_position[chpos++] =
645
1.43k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.43k
        }
647
648
1.61k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.26k
        {
650
1.26k
            hInfo->channel_position[chpos++] =
651
1.26k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.26k
        }
653
654
354
        chdir = hInfo->num_back_channels;
655
354
        if (chdir & 1)
656
67
        {
657
67
            back_center = 1;
658
67
            chdir--;
659
67
        }
660
1.36k
        for (i = 0; i < chdir; i++)
661
1.00k
        {
662
1.00k
            hInfo->channel_position[chpos++] =
663
1.00k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
1.00k
        }
665
354
        if (back_center)
666
67
        {
667
67
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
67
        }
669
670
586
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
232
        {
672
232
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
232
        }
674
675
8.81k
    } else {
676
8.81k
        switch (hDecoder->channelConfiguration)
677
8.81k
        {
678
427
        case 1:
679
427
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
427
            hInfo->num_front_channels = 2;
682
427
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
427
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
427
            break;
689
4.12k
        case 2:
690
4.12k
            hInfo->num_front_channels = 2;
691
4.12k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.12k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.12k
            break;
694
645
        case 3:
695
645
            hInfo->num_front_channels = 3;
696
645
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
645
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
645
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
645
            break;
700
1.00k
        case 4:
701
1.00k
            hInfo->num_front_channels = 3;
702
1.00k
            hInfo->num_back_channels = 1;
703
1.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.00k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.00k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.00k
            break;
708
728
        case 5:
709
728
            hInfo->num_front_channels = 3;
710
728
            hInfo->num_back_channels = 2;
711
728
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
728
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
728
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
728
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
728
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
728
            break;
717
375
        case 6:
718
375
            hInfo->num_front_channels = 3;
719
375
            hInfo->num_back_channels = 2;
720
375
            hInfo->num_lfe_channels = 1;
721
375
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
375
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
375
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
375
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
375
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
375
            hInfo->channel_position[5] = LFE_CHANNEL;
727
375
            break;
728
1.30k
        case 7:
729
1.30k
            hInfo->num_front_channels = 3;
730
1.30k
            hInfo->num_side_channels = 2;
731
1.30k
            hInfo->num_back_channels = 2;
732
1.30k
            hInfo->num_lfe_channels = 1;
733
1.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.30k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.30k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.30k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.30k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.30k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.30k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.30k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.30k
            break;
742
210
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
210
            {
744
210
                uint8_t i;
745
210
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
210
                if (ch & 1) /* there's either a center front or a center back channel */
747
77
                {
748
77
                    uint8_t ch1 = (ch-1)/2;
749
77
                    if (hDecoder->first_syn_ele == ID_SCE)
750
52
                    {
751
52
                        hInfo->num_front_channels = ch1 + 1;
752
52
                        hInfo->num_back_channels = ch1;
753
52
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
214
                        for (i = 1; i <= ch1; i+=2)
755
162
                        {
756
162
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
162
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
162
                        }
759
214
                        for (i = ch1+1; i < ch; i+=2)
760
162
                        {
761
162
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
162
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
162
                        }
764
52
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
139
                        for (i = 0; i < ch1; i+=2)
768
114
                        {
769
114
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
114
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
114
                        }
772
139
                        for (i = ch1; i < ch-1; i+=2)
773
114
                        {
774
114
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
114
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
114
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
133
                } else {
780
133
                    uint8_t ch1 = (ch)/2;
781
133
                    hInfo->num_front_channels = ch1;
782
133
                    hInfo->num_back_channels = ch1;
783
133
                    if (ch1 & 1)
784
59
                    {
785
59
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
410
                        for (i = 1; i <= ch1; i+=2)
787
351
                        {
788
351
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
351
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
351
                        }
791
351
                        for (i = ch1+1; i < ch-1; i+=2)
792
292
                        {
793
292
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
292
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
292
                        }
796
59
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
340
                        for (i = 0; i < ch1; i+=2)
799
266
                        {
800
266
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
266
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
266
                        }
803
340
                        for (i = ch1; i < ch; i+=2)
804
266
                        {
805
266
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
266
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
266
                        }
808
74
                    }
809
133
                }
810
210
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
484
                for (i = ch; i < hDecoder->fr_channels; i++)
812
274
                {
813
274
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
274
                }
815
210
            }
816
210
            break;
817
8.81k
        }
818
8.81k
    }
819
9.17k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.17k
    if (hDecoder->pce_set)
617
354
    {
618
354
        uint8_t i, chpos = 0;
619
354
        uint8_t chdir, back_center = 0;
620
621
354
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
354
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
354
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
354
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
354
        chdir = hInfo->num_front_channels;
626
354
        if (chdir & 1)
627
156
        {
628
156
#if (defined(PS_DEC) || defined(DRM_PS))
629
156
            if (hInfo->num_front_channels == 1 &&
630
156
                hInfo->num_side_channels == 0 &&
631
156
                hInfo->num_back_channels == 0 &&
632
156
                hInfo->num_lfe_channels == 0)
633
38
            {
634
                /* When PS is enabled output is always stereo */
635
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
38
            } else
638
118
#endif
639
118
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
156
            chdir--;
641
156
        }
642
1.79k
        for (i = 0; i < chdir; i++)
643
1.43k
        {
644
1.43k
            hInfo->channel_position[chpos++] =
645
1.43k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.43k
        }
647
648
1.61k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.26k
        {
650
1.26k
            hInfo->channel_position[chpos++] =
651
1.26k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.26k
        }
653
654
354
        chdir = hInfo->num_back_channels;
655
354
        if (chdir & 1)
656
67
        {
657
67
            back_center = 1;
658
67
            chdir--;
659
67
        }
660
1.36k
        for (i = 0; i < chdir; i++)
661
1.00k
        {
662
1.00k
            hInfo->channel_position[chpos++] =
663
1.00k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
1.00k
        }
665
354
        if (back_center)
666
67
        {
667
67
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
67
        }
669
670
586
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
232
        {
672
232
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
232
        }
674
675
8.81k
    } else {
676
8.81k
        switch (hDecoder->channelConfiguration)
677
8.81k
        {
678
427
        case 1:
679
427
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
427
            hInfo->num_front_channels = 2;
682
427
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
427
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
427
            break;
689
4.12k
        case 2:
690
4.12k
            hInfo->num_front_channels = 2;
691
4.12k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.12k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.12k
            break;
694
645
        case 3:
695
645
            hInfo->num_front_channels = 3;
696
645
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
645
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
645
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
645
            break;
700
1.00k
        case 4:
701
1.00k
            hInfo->num_front_channels = 3;
702
1.00k
            hInfo->num_back_channels = 1;
703
1.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.00k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.00k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.00k
            break;
708
728
        case 5:
709
728
            hInfo->num_front_channels = 3;
710
728
            hInfo->num_back_channels = 2;
711
728
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
728
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
728
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
728
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
728
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
728
            break;
717
375
        case 6:
718
375
            hInfo->num_front_channels = 3;
719
375
            hInfo->num_back_channels = 2;
720
375
            hInfo->num_lfe_channels = 1;
721
375
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
375
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
375
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
375
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
375
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
375
            hInfo->channel_position[5] = LFE_CHANNEL;
727
375
            break;
728
1.30k
        case 7:
729
1.30k
            hInfo->num_front_channels = 3;
730
1.30k
            hInfo->num_side_channels = 2;
731
1.30k
            hInfo->num_back_channels = 2;
732
1.30k
            hInfo->num_lfe_channels = 1;
733
1.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.30k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.30k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.30k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.30k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.30k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.30k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.30k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.30k
            break;
742
210
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
210
            {
744
210
                uint8_t i;
745
210
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
210
                if (ch & 1) /* there's either a center front or a center back channel */
747
77
                {
748
77
                    uint8_t ch1 = (ch-1)/2;
749
77
                    if (hDecoder->first_syn_ele == ID_SCE)
750
52
                    {
751
52
                        hInfo->num_front_channels = ch1 + 1;
752
52
                        hInfo->num_back_channels = ch1;
753
52
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
214
                        for (i = 1; i <= ch1; i+=2)
755
162
                        {
756
162
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
162
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
162
                        }
759
214
                        for (i = ch1+1; i < ch; i+=2)
760
162
                        {
761
162
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
162
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
162
                        }
764
52
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
139
                        for (i = 0; i < ch1; i+=2)
768
114
                        {
769
114
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
114
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
114
                        }
772
139
                        for (i = ch1; i < ch-1; i+=2)
773
114
                        {
774
114
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
114
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
114
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
133
                } else {
780
133
                    uint8_t ch1 = (ch)/2;
781
133
                    hInfo->num_front_channels = ch1;
782
133
                    hInfo->num_back_channels = ch1;
783
133
                    if (ch1 & 1)
784
59
                    {
785
59
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
410
                        for (i = 1; i <= ch1; i+=2)
787
351
                        {
788
351
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
351
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
351
                        }
791
351
                        for (i = ch1+1; i < ch-1; i+=2)
792
292
                        {
793
292
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
292
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
292
                        }
796
59
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
340
                        for (i = 0; i < ch1; i+=2)
799
266
                        {
800
266
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
266
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
266
                        }
803
340
                        for (i = ch1; i < ch; i+=2)
804
266
                        {
805
266
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
266
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
266
                        }
808
74
                    }
809
133
                }
810
210
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
484
                for (i = ch; i < hDecoder->fr_channels; i++)
812
274
                {
813
274
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
274
                }
815
210
            }
816
210
            break;
817
8.81k
        }
818
8.81k
    }
819
9.17k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.17k
    if (hDecoder->pce_set)
617
354
    {
618
354
        uint8_t i, chpos = 0;
619
354
        uint8_t chdir, back_center = 0;
620
621
354
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
354
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
354
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
354
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
354
        chdir = hInfo->num_front_channels;
626
354
        if (chdir & 1)
627
156
        {
628
156
#if (defined(PS_DEC) || defined(DRM_PS))
629
156
            if (hInfo->num_front_channels == 1 &&
630
156
                hInfo->num_side_channels == 0 &&
631
156
                hInfo->num_back_channels == 0 &&
632
156
                hInfo->num_lfe_channels == 0)
633
38
            {
634
                /* When PS is enabled output is always stereo */
635
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
38
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
38
            } else
638
118
#endif
639
118
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
156
            chdir--;
641
156
        }
642
1.79k
        for (i = 0; i < chdir; i++)
643
1.43k
        {
644
1.43k
            hInfo->channel_position[chpos++] =
645
1.43k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.43k
        }
647
648
1.61k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.26k
        {
650
1.26k
            hInfo->channel_position[chpos++] =
651
1.26k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.26k
        }
653
654
354
        chdir = hInfo->num_back_channels;
655
354
        if (chdir & 1)
656
67
        {
657
67
            back_center = 1;
658
67
            chdir--;
659
67
        }
660
1.36k
        for (i = 0; i < chdir; i++)
661
1.00k
        {
662
1.00k
            hInfo->channel_position[chpos++] =
663
1.00k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
1.00k
        }
665
354
        if (back_center)
666
67
        {
667
67
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
67
        }
669
670
586
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
232
        {
672
232
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
232
        }
674
675
8.81k
    } else {
676
8.81k
        switch (hDecoder->channelConfiguration)
677
8.81k
        {
678
427
        case 1:
679
427
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
427
            hInfo->num_front_channels = 2;
682
427
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
427
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
427
            break;
689
4.12k
        case 2:
690
4.12k
            hInfo->num_front_channels = 2;
691
4.12k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.12k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.12k
            break;
694
645
        case 3:
695
645
            hInfo->num_front_channels = 3;
696
645
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
645
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
645
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
645
            break;
700
1.00k
        case 4:
701
1.00k
            hInfo->num_front_channels = 3;
702
1.00k
            hInfo->num_back_channels = 1;
703
1.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.00k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.00k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.00k
            break;
708
728
        case 5:
709
728
            hInfo->num_front_channels = 3;
710
728
            hInfo->num_back_channels = 2;
711
728
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
728
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
728
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
728
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
728
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
728
            break;
717
375
        case 6:
718
375
            hInfo->num_front_channels = 3;
719
375
            hInfo->num_back_channels = 2;
720
375
            hInfo->num_lfe_channels = 1;
721
375
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
375
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
375
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
375
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
375
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
375
            hInfo->channel_position[5] = LFE_CHANNEL;
727
375
            break;
728
1.30k
        case 7:
729
1.30k
            hInfo->num_front_channels = 3;
730
1.30k
            hInfo->num_side_channels = 2;
731
1.30k
            hInfo->num_back_channels = 2;
732
1.30k
            hInfo->num_lfe_channels = 1;
733
1.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.30k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.30k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.30k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.30k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.30k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.30k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.30k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.30k
            break;
742
210
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
210
            {
744
210
                uint8_t i;
745
210
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
210
                if (ch & 1) /* there's either a center front or a center back channel */
747
77
                {
748
77
                    uint8_t ch1 = (ch-1)/2;
749
77
                    if (hDecoder->first_syn_ele == ID_SCE)
750
52
                    {
751
52
                        hInfo->num_front_channels = ch1 + 1;
752
52
                        hInfo->num_back_channels = ch1;
753
52
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
214
                        for (i = 1; i <= ch1; i+=2)
755
162
                        {
756
162
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
162
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
162
                        }
759
214
                        for (i = ch1+1; i < ch; i+=2)
760
162
                        {
761
162
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
162
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
162
                        }
764
52
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
139
                        for (i = 0; i < ch1; i+=2)
768
114
                        {
769
114
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
114
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
114
                        }
772
139
                        for (i = ch1; i < ch-1; i+=2)
773
114
                        {
774
114
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
114
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
114
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
133
                } else {
780
133
                    uint8_t ch1 = (ch)/2;
781
133
                    hInfo->num_front_channels = ch1;
782
133
                    hInfo->num_back_channels = ch1;
783
133
                    if (ch1 & 1)
784
59
                    {
785
59
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
410
                        for (i = 1; i <= ch1; i+=2)
787
351
                        {
788
351
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
351
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
351
                        }
791
351
                        for (i = ch1+1; i < ch-1; i+=2)
792
292
                        {
793
292
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
292
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
292
                        }
796
59
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
340
                        for (i = 0; i < ch1; i+=2)
799
266
                        {
800
266
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
266
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
266
                        }
803
340
                        for (i = ch1; i < ch; i+=2)
804
266
                        {
805
266
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
266
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
266
                        }
808
74
                    }
809
133
                }
810
210
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
484
                for (i = ch; i < hDecoder->fr_channels; i++)
812
274
                {
813
274
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
274
                }
815
210
            }
816
210
            break;
817
8.81k
        }
818
8.81k
    }
819
9.17k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
41.7k
{
826
41.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
41.7k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
41.7k
}
829
830
void* NeAACDecDecode2(NeAACDecHandle hpDecoder,
831
                                  NeAACDecFrameInfo *hInfo,
832
                                  unsigned char *buffer,
833
                                  unsigned long buffer_size,
834
                                  void **sample_buffer,
835
                                  unsigned long sample_buffer_size)
836
12.3k
{
837
12.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
12.3k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
92
    {
840
92
        hInfo->error = 27;
841
92
        return NULL;
842
92
    }
843
844
12.2k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
12.2k
        sample_buffer, sample_buffer_size);
846
12.3k
}
847
848
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
849
                              NeAACDecFrameInfo *hInfo,
850
                              unsigned char *buffer,
851
                              unsigned long buffer_size,
852
                              void **sample_buffer2,
853
                              unsigned long sample_buffer_size)
854
54.0k
{
855
54.0k
    uint16_t i;
856
54.0k
    uint8_t channels = 0;
857
54.0k
    uint8_t output_channels = 0;
858
54.0k
    bitfile ld;
859
54.0k
    uint32_t bitsconsumed;
860
54.0k
    uint16_t frame_len;
861
54.0k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
54.0k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
54.0k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
54.0k
    frame_len = hDecoder->frameLength;
882
883
884
54.0k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
54.0k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
54.0k
    if (buffer_size >= 128)
903
6.11k
    {
904
6.11k
        if (memcmp(buffer, "TAG", 3) == 0)
905
12
        {
906
            /* found it */
907
12
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
12
            return NULL;
910
12
        }
911
6.11k
    }
912
913
914
    /* initialize the bitstream */
915
53.9k
    faad_initbits(&ld, buffer, buffer_size);
916
53.9k
    if (ld.error != 0)
917
21.7k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
11.2k
    if (hDecoder->object_type == DRM_ER_LC)
952
998
    {
953
        /* We do not support stereo right now */
954
998
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
998
        faad_getbits(&ld, 8
961
998
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
998
    }
963
11.2k
#endif
964
965
32.2k
    if (hDecoder->adts_header_present)
966
775
    {
967
775
        adts_header adts;
968
969
775
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
775
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
109
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
775
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
11.1k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
998
    {
987
998
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.1k
    } else {
989
10.1k
#endif
990
20.9k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
20.9k
    channels = hDecoder->fr_channels;
1009
1010
32.1k
    if (hInfo->error > 0)
1011
21.8k
        goto error;
1012
1013
    /* safety check */
1014
10.3k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
98
    {
1016
        /* invalid number of channels */
1017
98
        hInfo->error = 12;
1018
98
        goto error;
1019
98
    }
1020
1021
    /* no more bit reading after this */
1022
10.2k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
10.2k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
10.2k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
10.2k
    faad_endbits(&ld);
1030
1031
1032
10.2k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
10.2k
        )
1037
10.0k
    {
1038
10.0k
        if (hDecoder->channelConfiguration == 0)
1039
318
            hDecoder->channelConfiguration = channels;
1040
1041
10.0k
        if (channels == 8) /* 7.1 */
1042
936
            hDecoder->channelConfiguration = 7;
1043
10.0k
        if (channels == 7) /* not a standard channelConfiguration */
1044
54
            hDecoder->channelConfiguration = 0;
1045
10.0k
    }
1046
1047
10.2k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.08k
    {
1049
1.08k
        hDecoder->downMatrix = 1;
1050
1.08k
        output_channels = 2;
1051
9.19k
    } else {
1052
9.19k
        output_channels = channels;
1053
9.19k
    }
1054
1055
10.2k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
10.2k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
10.2k
    if (output_channels == 1)
1059
297
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
297
        hDecoder->upMatrix = 1;
1062
297
        output_channels = 2;
1063
297
    }
1064
10.2k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
10.2k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
363
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
363
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
9
        {
1072
9
            hInfo->error = 22;
1073
9
            return NULL;
1074
9
        }
1075
363
    }
1076
10.2k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
10.2k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
10.2k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
10.2k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
10.2k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
10.2k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
10.2k
    hInfo->header_type = RAW;
1090
10.2k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
10.2k
    if (hDecoder->adts_header_present)
1093
208
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
10.2k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
10.2k
    hInfo->ps = hDecoder->ps_used_global;
1100
10.2k
#endif
1101
1102
    /* check if frame has channel elements */
1103
10.2k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
10.2k
    {
1110
10.2k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
10.2k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
10.2k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
10.2k
        };
1114
10.2k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
10.2k
#ifdef SBR_DEC
1116
10.2k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
5.90k
        {
1118
5.90k
            stride = 2 * stride;
1119
5.90k
        }
1120
10.2k
#endif
1121
10.2k
        required_buffer_size = frame_len*output_channels*stride;
1122
10.2k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
10.2k
    if (sample_buffer_size == 0)
1126
7.03k
    {
1127
        /* allocate the buffer for the final samples */
1128
7.03k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
6.11k
        {
1130
6.11k
            if (hDecoder->sample_buffer)
1131
99
                faad_free(hDecoder->sample_buffer);
1132
6.11k
            hDecoder->sample_buffer = NULL;
1133
6.11k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
6.11k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
6.11k
        }
1136
7.03k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.33k
        hInfo->error = 27;
1139
1.33k
        return NULL;
1140
1.33k
    }
1141
1142
8.93k
    if (sample_buffer_size == 0)
1143
7.03k
    {
1144
7.03k
        sample_buffer = hDecoder->sample_buffer;
1145
7.03k
    } else {
1146
1.90k
        sample_buffer = *sample_buffer2;
1147
1.90k
    }
1148
1149
8.93k
#ifdef SBR_DEC
1150
8.93k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
6.25k
    {
1152
6.25k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
6.25k
        if (!hDecoder->downSampledSBR)
1156
4.95k
        {
1157
4.95k
            frame_len *= 2;
1158
4.95k
            hInfo->samples *= 2;
1159
4.95k
            hInfo->samplerate *= 2;
1160
4.95k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
31.5k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
25.3k
        {
1165
25.3k
            if (hDecoder->sbr[ele] == NULL)
1166
10
            {
1167
10
                hInfo->error = 25;
1168
10
                goto error;
1169
10
            }
1170
25.3k
        }
1171
1172
        /* sbr */
1173
6.24k
        if (hDecoder->sbr_present_flag == 1)
1174
3.97k
        {
1175
3.97k
            hInfo->object_type = HE_AAC;
1176
3.97k
            hInfo->sbr = SBR_UPSAMPLED;
1177
3.97k
        } else {
1178
2.27k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.27k
        }
1180
6.24k
        if (hDecoder->downSampledSBR)
1181
1.28k
        {
1182
1.28k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.28k
        }
1184
6.24k
    }
1185
8.92k
#endif
1186
1187
1188
8.92k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
8.92k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
8.92k
    hDecoder->postSeekResetFlag = 0;
1198
1199
8.92k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
5.92k
    if (hDecoder->object_type != LD)
1202
5.20k
    {
1203
5.20k
#endif
1204
8.20k
        if (hDecoder->frame <= 1)
1205
3.07k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
720
    } else {
1208
        /* LD encoders will give lower delay */
1209
720
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
720
    }
1212
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
8.92k
    return sample_buffer;
1225
1226
22.0k
error:
1227
1228
    /* reset filterbank state */
1229
1.43M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.40M
    {
1231
1.40M
        if (hDecoder->fb_intermed[i] != NULL)
1232
700k
        {
1233
700k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
700k
        }
1235
1.40M
    }
1236
22.0k
#ifdef SBR_DEC
1237
1.07M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
1.05M
    {
1239
1.05M
        if (hDecoder->sbr[i] != NULL)
1240
504k
        {
1241
504k
            sbrReset(hDecoder->sbr[i]);
1242
504k
        }
1243
1.05M
    }
1244
22.0k
#endif
1245
1246
1247
22.0k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
22.0k
    return NULL;
1255
8.93k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
18.6k
{
855
18.6k
    uint16_t i;
856
18.6k
    uint8_t channels = 0;
857
18.6k
    uint8_t output_channels = 0;
858
18.6k
    bitfile ld;
859
18.6k
    uint32_t bitsconsumed;
860
18.6k
    uint16_t frame_len;
861
18.6k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
18.6k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
18.6k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
18.6k
    frame_len = hDecoder->frameLength;
882
883
884
18.6k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
18.6k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
18.6k
    if (buffer_size >= 128)
903
2.79k
    {
904
2.79k
        if (memcmp(buffer, "TAG", 3) == 0)
905
6
        {
906
            /* found it */
907
6
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
6
            return NULL;
910
6
        }
911
2.79k
    }
912
913
914
    /* initialize the bitstream */
915
18.6k
    faad_initbits(&ld, buffer, buffer_size);
916
18.6k
    if (ld.error != 0)
917
7.45k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
11.2k
#ifdef DRM
951
11.2k
    if (hDecoder->object_type == DRM_ER_LC)
952
998
    {
953
        /* We do not support stereo right now */
954
998
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
998
        faad_getbits(&ld, 8
961
998
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
998
    }
963
11.2k
#endif
964
965
11.2k
    if (hDecoder->adts_header_present)
966
90
    {
967
90
        adts_header adts;
968
969
90
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
90
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
6
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
90
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
11.1k
#ifdef DRM
985
11.1k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
998
    {
987
998
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.1k
    } else {
989
10.1k
#endif
990
10.1k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
10.1k
#ifdef DRM
992
10.1k
    }
993
11.1k
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
11.1k
    channels = hDecoder->fr_channels;
1009
1010
11.1k
    if (hInfo->error > 0)
1011
7.50k
        goto error;
1012
1013
    /* safety check */
1014
3.69k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
34
    {
1016
        /* invalid number of channels */
1017
34
        hInfo->error = 12;
1018
34
        goto error;
1019
34
    }
1020
1021
    /* no more bit reading after this */
1022
3.65k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
3.65k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
3.65k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
3.65k
    faad_endbits(&ld);
1030
1031
1032
3.65k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
3.65k
        )
1037
3.61k
    {
1038
3.61k
        if (hDecoder->channelConfiguration == 0)
1039
156
            hDecoder->channelConfiguration = channels;
1040
1041
3.61k
        if (channels == 8) /* 7.1 */
1042
182
            hDecoder->channelConfiguration = 7;
1043
3.61k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
3.61k
    }
1046
1047
3.65k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
500
    {
1049
500
        hDecoder->downMatrix = 1;
1050
500
        output_channels = 2;
1051
3.15k
    } else {
1052
3.15k
        output_channels = channels;
1053
3.15k
    }
1054
1055
3.65k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
3.65k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
3.65k
    if (output_channels == 1)
1059
0
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
0
        hDecoder->upMatrix = 1;
1062
0
        output_channels = 2;
1063
0
    }
1064
3.65k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
3.65k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
176
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
176
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
3
        {
1072
3
            hInfo->error = 22;
1073
3
            return NULL;
1074
3
        }
1075
176
    }
1076
3.65k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
3.65k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
3.65k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
3.65k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
3.65k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
3.65k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
3.65k
    hInfo->header_type = RAW;
1090
3.65k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
3.65k
    if (hDecoder->adts_header_present)
1093
40
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
3.65k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
3.65k
    hInfo->ps = hDecoder->ps_used_global;
1100
3.65k
#endif
1101
1102
    /* check if frame has channel elements */
1103
3.65k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
3.65k
    {
1110
3.65k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
3.65k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
3.65k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
3.65k
        };
1114
3.65k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
3.65k
#ifdef SBR_DEC
1116
3.65k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
2.03k
        {
1118
2.03k
            stride = 2 * stride;
1119
2.03k
        }
1120
3.65k
#endif
1121
3.65k
        required_buffer_size = frame_len*output_channels*stride;
1122
3.65k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
3.65k
    if (sample_buffer_size == 0)
1126
1.98k
    {
1127
        /* allocate the buffer for the final samples */
1128
1.98k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
1.74k
        {
1130
1.74k
            if (hDecoder->sample_buffer)
1131
51
                faad_free(hDecoder->sample_buffer);
1132
1.74k
            hDecoder->sample_buffer = NULL;
1133
1.74k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
1.74k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
1.74k
        }
1136
1.98k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
654
        hInfo->error = 27;
1139
654
        return NULL;
1140
654
    }
1141
1142
3.00k
    if (sample_buffer_size == 0)
1143
1.98k
    {
1144
1.98k
        sample_buffer = hDecoder->sample_buffer;
1145
1.98k
    } else {
1146
1.01k
        sample_buffer = *sample_buffer2;
1147
1.01k
    }
1148
1149
3.00k
#ifdef SBR_DEC
1150
3.00k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
2.19k
    {
1152
2.19k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
2.19k
        if (!hDecoder->downSampledSBR)
1156
1.55k
        {
1157
1.55k
            frame_len *= 2;
1158
1.55k
            hInfo->samples *= 2;
1159
1.55k
            hInfo->samplerate *= 2;
1160
1.55k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
11.5k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
9.30k
        {
1165
9.30k
            if (hDecoder->sbr[ele] == NULL)
1166
1
            {
1167
1
                hInfo->error = 25;
1168
1
                goto error;
1169
1
            }
1170
9.30k
        }
1171
1172
        /* sbr */
1173
2.19k
        if (hDecoder->sbr_present_flag == 1)
1174
1.72k
        {
1175
1.72k
            hInfo->object_type = HE_AAC;
1176
1.72k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.72k
        } else {
1178
475
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
475
        }
1180
2.19k
        if (hDecoder->downSampledSBR)
1181
641
        {
1182
641
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
641
        }
1184
2.19k
    }
1185
2.99k
#endif
1186
1187
1188
2.99k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
2.99k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
2.99k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
2.99k
#endif
1195
1196
1197
2.99k
    hDecoder->postSeekResetFlag = 0;
1198
1199
2.99k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
2.99k
        if (hDecoder->frame <= 1)
1205
1.16k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
    } else {
1208
        /* LD encoders will give lower delay */
1209
        if (hDecoder->frame <= 0)
1210
            hInfo->samples = 0;
1211
    }
1212
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
2.99k
    return sample_buffer;
1225
1226
7.54k
error:
1227
1228
    /* reset filterbank state */
1229
490k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
482k
    {
1231
482k
        if (hDecoder->fb_intermed[i] != NULL)
1232
183k
        {
1233
183k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
183k
        }
1235
482k
    }
1236
7.54k
#ifdef SBR_DEC
1237
369k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
362k
    {
1239
362k
        if (hDecoder->sbr[i] != NULL)
1240
138k
        {
1241
138k
            sbrReset(hDecoder->sbr[i]);
1242
138k
        }
1243
362k
    }
1244
7.54k
#endif
1245
1246
1247
7.54k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
7.54k
    return NULL;
1255
3.00k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
35.3k
{
855
35.3k
    uint16_t i;
856
35.3k
    uint8_t channels = 0;
857
35.3k
    uint8_t output_channels = 0;
858
35.3k
    bitfile ld;
859
35.3k
    uint32_t bitsconsumed;
860
35.3k
    uint16_t frame_len;
861
35.3k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
35.3k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
35.3k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
35.3k
    frame_len = hDecoder->frameLength;
882
883
884
35.3k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
35.3k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
35.3k
    if (buffer_size >= 128)
903
3.31k
    {
904
3.31k
        if (memcmp(buffer, "TAG", 3) == 0)
905
6
        {
906
            /* found it */
907
6
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
6
            return NULL;
910
6
        }
911
3.31k
    }
912
913
914
    /* initialize the bitstream */
915
35.3k
    faad_initbits(&ld, buffer, buffer_size);
916
35.3k
    if (ld.error != 0)
917
14.2k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
    if (hDecoder->object_type == DRM_ER_LC)
952
    {
953
        /* We do not support stereo right now */
954
        if (0) //(hDecoder->channelConfiguration == 2)
955
        {
956
            hInfo->error = 28; // Throw CRC error
957
            goto error;
958
        }
959
960
        faad_getbits(&ld, 8
961
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
    }
963
#endif
964
965
21.0k
    if (hDecoder->adts_header_present)
966
685
    {
967
685
        adts_header adts;
968
969
685
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
685
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
103
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
685
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
    {
987
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
    } else {
989
#endif
990
20.9k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
20.9k
    channels = hDecoder->fr_channels;
1009
1010
20.9k
    if (hInfo->error > 0)
1011
14.3k
        goto error;
1012
1013
    /* safety check */
1014
6.67k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
64
    {
1016
        /* invalid number of channels */
1017
64
        hInfo->error = 12;
1018
64
        goto error;
1019
64
    }
1020
1021
    /* no more bit reading after this */
1022
6.61k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
6.61k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
6.61k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
6.61k
    faad_endbits(&ld);
1030
1031
1032
6.61k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
6.61k
        )
1037
6.44k
    {
1038
6.44k
        if (hDecoder->channelConfiguration == 0)
1039
162
            hDecoder->channelConfiguration = channels;
1040
1041
6.44k
        if (channels == 8) /* 7.1 */
1042
754
            hDecoder->channelConfiguration = 7;
1043
6.44k
        if (channels == 7) /* not a standard channelConfiguration */
1044
54
            hDecoder->channelConfiguration = 0;
1045
6.44k
    }
1046
1047
6.61k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
582
    {
1049
582
        hDecoder->downMatrix = 1;
1050
582
        output_channels = 2;
1051
6.03k
    } else {
1052
6.03k
        output_channels = channels;
1053
6.03k
    }
1054
1055
6.61k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
6.61k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
6.61k
    if (output_channels == 1)
1059
297
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
297
        hDecoder->upMatrix = 1;
1062
297
        output_channels = 2;
1063
297
    }
1064
6.61k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
6.61k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
187
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
187
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
187
    }
1076
6.60k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
6.60k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
6.60k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
6.60k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
6.60k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
6.60k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
6.60k
    hInfo->header_type = RAW;
1090
6.60k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
6.60k
    if (hDecoder->adts_header_present)
1093
168
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
6.60k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
6.60k
    hInfo->ps = hDecoder->ps_used_global;
1100
6.60k
#endif
1101
1102
    /* check if frame has channel elements */
1103
6.60k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
6.60k
    {
1110
6.60k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
6.60k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
6.60k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
6.60k
        };
1114
6.60k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
6.60k
#ifdef SBR_DEC
1116
6.60k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.86k
        {
1118
3.86k
            stride = 2 * stride;
1119
3.86k
        }
1120
6.60k
#endif
1121
6.60k
        required_buffer_size = frame_len*output_channels*stride;
1122
6.60k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
6.60k
    if (sample_buffer_size == 0)
1126
5.04k
    {
1127
        /* allocate the buffer for the final samples */
1128
5.04k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
4.36k
        {
1130
4.36k
            if (hDecoder->sample_buffer)
1131
48
                faad_free(hDecoder->sample_buffer);
1132
4.36k
            hDecoder->sample_buffer = NULL;
1133
4.36k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
4.36k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
4.36k
        }
1136
5.04k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
676
        hInfo->error = 27;
1139
676
        return NULL;
1140
676
    }
1141
1142
5.93k
    if (sample_buffer_size == 0)
1143
5.04k
    {
1144
5.04k
        sample_buffer = hDecoder->sample_buffer;
1145
5.04k
    } else {
1146
889
        sample_buffer = *sample_buffer2;
1147
889
    }
1148
1149
5.93k
#ifdef SBR_DEC
1150
5.93k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
4.05k
    {
1152
4.05k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
4.05k
        if (!hDecoder->downSampledSBR)
1156
3.40k
        {
1157
3.40k
            frame_len *= 2;
1158
3.40k
            hInfo->samples *= 2;
1159
3.40k
            hInfo->samplerate *= 2;
1160
3.40k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
20.0k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
16.0k
        {
1165
16.0k
            if (hDecoder->sbr[ele] == NULL)
1166
9
            {
1167
9
                hInfo->error = 25;
1168
9
                goto error;
1169
9
            }
1170
16.0k
        }
1171
1172
        /* sbr */
1173
4.04k
        if (hDecoder->sbr_present_flag == 1)
1174
2.24k
        {
1175
2.24k
            hInfo->object_type = HE_AAC;
1176
2.24k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.24k
        } else {
1178
1.79k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.79k
        }
1180
4.04k
        if (hDecoder->downSampledSBR)
1181
647
        {
1182
647
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
647
        }
1184
4.04k
    }
1185
5.92k
#endif
1186
1187
1188
5.92k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
5.92k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
5.92k
    hDecoder->postSeekResetFlag = 0;
1198
1199
5.92k
    hDecoder->frame++;
1200
5.92k
#ifdef LD_DEC
1201
5.92k
    if (hDecoder->object_type != LD)
1202
5.20k
    {
1203
5.20k
#endif
1204
5.20k
        if (hDecoder->frame <= 1)
1205
1.90k
            hInfo->samples = 0;
1206
5.20k
#ifdef LD_DEC
1207
5.20k
    } else {
1208
        /* LD encoders will give lower delay */
1209
720
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
720
    }
1212
5.92k
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
5.92k
    return sample_buffer;
1225
1226
14.4k
error:
1227
1228
    /* reset filterbank state */
1229
941k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
926k
    {
1231
926k
        if (hDecoder->fb_intermed[i] != NULL)
1232
516k
        {
1233
516k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
516k
        }
1235
926k
    }
1236
14.4k
#ifdef SBR_DEC
1237
709k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
694k
    {
1239
694k
        if (hDecoder->sbr[i] != NULL)
1240
366k
        {
1241
366k
            sbrReset(hDecoder->sbr[i]);
1242
366k
        }
1243
694k
    }
1244
14.4k
#endif
1245
1246
1247
14.4k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
14.4k
    return NULL;
1255
5.93k
}