Coverage Report

Created: 2026-02-14 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/decoder.c
Line
Count
Source
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
339
{
74
339
    static char *libfaadName = PACKAGE_VERSION;
75
339
    static char *libCopyright =
76
339
        " Copyright 2002-2004: Ahead Software AG\n"
77
339
  " http://www.audiocoding.com\n"
78
339
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
339
    if (faad_id_string)
81
339
        *faad_id_string = libfaadName;
82
83
339
    if (faad_copyright_string)
84
339
        *faad_copyright_string = libCopyright;
85
86
339
    return 0;
87
339
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
339
{
91
339
    if (errcode >= NUM_ERROR_MESSAGES)
92
177
        return NULL;
93
162
    return err_msg[errcode];
94
339
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.35k
{
98
1.35k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.35k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
339
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
678
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
678
    cap += LD_DEC_CAP;
111
#endif
112
1.35k
#ifdef ERROR_RESILIENCE
113
1.35k
    cap += ERROR_RESILIENCE_CAP;
114
1.35k
#endif
115
#ifdef FIXED_POINT
116
678
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.35k
    return cap;
120
1.35k
}
NeAACDecGetCapabilities
Line
Count
Source
97
339
{
98
339
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
339
    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
339
#ifdef ERROR_RESILIENCE
113
339
    cap += ERROR_RESILIENCE_CAP;
114
339
#endif
115
339
#ifdef FIXED_POINT
116
339
    cap += FIXED_POINT_CAP;
117
339
#endif
118
119
339
    return cap;
120
339
}
NeAACDecGetCapabilities
Line
Count
Source
97
339
{
98
339
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
339
    cap += LC_DEC_CAP;
102
103
339
#ifdef MAIN_DEC
104
339
    cap += MAIN_DEC_CAP;
105
339
#endif
106
339
#ifdef LTP_DEC
107
339
    cap += LTP_DEC_CAP;
108
339
#endif
109
339
#ifdef LD_DEC
110
339
    cap += LD_DEC_CAP;
111
339
#endif
112
339
#ifdef ERROR_RESILIENCE
113
339
    cap += ERROR_RESILIENCE_CAP;
114
339
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
339
    return cap;
120
339
}
NeAACDecGetCapabilities
Line
Count
Source
97
339
{
98
339
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
339
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
339
#ifdef LTP_DEC
107
339
    cap += LTP_DEC_CAP;
108
339
#endif
109
339
#ifdef LD_DEC
110
339
    cap += LD_DEC_CAP;
111
339
#endif
112
339
#ifdef ERROR_RESILIENCE
113
339
    cap += ERROR_RESILIENCE_CAP;
114
339
#endif
115
339
#ifdef FIXED_POINT
116
339
    cap += FIXED_POINT_CAP;
117
339
#endif
118
119
339
    return cap;
120
339
}
NeAACDecGetCapabilities
Line
Count
Source
97
339
{
98
339
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
339
    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
339
#ifdef ERROR_RESILIENCE
113
339
    cap += ERROR_RESILIENCE_CAP;
114
339
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
339
    return cap;
120
339
}
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
40.3k
{
125
40.3k
    uint8_t i;
126
40.3k
    NeAACDecStruct *hDecoder = NULL;
127
128
40.3k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
40.3k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
40.3k
    hDecoder->cmes = mes;
134
40.3k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
40.3k
    hDecoder->config.defObjectType = MAIN;
136
40.3k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
40.3k
    hDecoder->config.downMatrix = 0;
138
40.3k
    hDecoder->adts_header_present = 0;
139
40.3k
    hDecoder->adif_header_present = 0;
140
40.3k
    hDecoder->latm_header_present = 0;
141
40.3k
#ifdef ERROR_RESILIENCE
142
40.3k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
40.3k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
40.3k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
40.3k
#endif
146
40.3k
    hDecoder->frameLength = 1024;
147
148
40.3k
    hDecoder->frame = 0;
149
40.3k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
40.3k
    hDecoder->__r1 = 0x2bb431ea;
153
40.3k
    hDecoder->__r2 = 0x206155b7;
154
155
2.62M
    for (i = 0; i < MAX_CHANNELS; i++)
156
2.58M
    {
157
2.58M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
2.58M
        hDecoder->window_shape_prev[i] = 0;
159
2.58M
        hDecoder->time_out[i] = NULL;
160
2.58M
        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
2.58M
    }
173
174
40.3k
#ifdef SBR_DEC
175
1.97M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
1.93M
    {
177
1.93M
        hDecoder->sbr[i] = NULL;
178
1.93M
    }
179
40.3k
#endif
180
181
40.3k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
40.3k
    return hDecoder;
184
40.3k
}
NeAACDecOpen
Line
Count
Source
124
6.89k
{
125
6.89k
    uint8_t i;
126
6.89k
    NeAACDecStruct *hDecoder = NULL;
127
128
6.89k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
6.89k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
6.89k
    hDecoder->cmes = mes;
134
6.89k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
6.89k
    hDecoder->config.defObjectType = MAIN;
136
6.89k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
6.89k
    hDecoder->config.downMatrix = 0;
138
6.89k
    hDecoder->adts_header_present = 0;
139
6.89k
    hDecoder->adif_header_present = 0;
140
6.89k
    hDecoder->latm_header_present = 0;
141
6.89k
#ifdef ERROR_RESILIENCE
142
6.89k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
6.89k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
6.89k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
6.89k
#endif
146
6.89k
    hDecoder->frameLength = 1024;
147
148
6.89k
    hDecoder->frame = 0;
149
6.89k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
6.89k
    hDecoder->__r1 = 0x2bb431ea;
153
6.89k
    hDecoder->__r2 = 0x206155b7;
154
155
448k
    for (i = 0; i < MAX_CHANNELS; i++)
156
441k
    {
157
441k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
441k
        hDecoder->window_shape_prev[i] = 0;
159
441k
        hDecoder->time_out[i] = NULL;
160
441k
        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
441k
    }
173
174
6.89k
#ifdef SBR_DEC
175
337k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
330k
    {
177
330k
        hDecoder->sbr[i] = NULL;
178
330k
    }
179
6.89k
#endif
180
181
6.89k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
6.89k
    return hDecoder;
184
6.89k
}
NeAACDecOpen
Line
Count
Source
124
13.2k
{
125
13.2k
    uint8_t i;
126
13.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
13.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
13.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
13.2k
    hDecoder->cmes = mes;
134
13.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
13.2k
    hDecoder->config.defObjectType = MAIN;
136
13.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
13.2k
    hDecoder->config.downMatrix = 0;
138
13.2k
    hDecoder->adts_header_present = 0;
139
13.2k
    hDecoder->adif_header_present = 0;
140
13.2k
    hDecoder->latm_header_present = 0;
141
13.2k
#ifdef ERROR_RESILIENCE
142
13.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
13.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
13.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
13.2k
#endif
146
13.2k
    hDecoder->frameLength = 1024;
147
148
13.2k
    hDecoder->frame = 0;
149
13.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
13.2k
    hDecoder->__r1 = 0x2bb431ea;
153
13.2k
    hDecoder->__r2 = 0x206155b7;
154
155
862k
    for (i = 0; i < MAX_CHANNELS; i++)
156
849k
    {
157
849k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
849k
        hDecoder->window_shape_prev[i] = 0;
159
849k
        hDecoder->time_out[i] = NULL;
160
849k
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
849k
#ifdef MAIN_DEC
166
849k
        hDecoder->pred_stat[i] = NULL;
167
849k
#endif
168
849k
#ifdef LTP_DEC
169
849k
        hDecoder->ltp_lag[i] = 0;
170
849k
        hDecoder->lt_pred_stat[i] = NULL;
171
849k
#endif
172
849k
    }
173
174
13.2k
#ifdef SBR_DEC
175
650k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
637k
    {
177
637k
        hDecoder->sbr[i] = NULL;
178
637k
    }
179
13.2k
#endif
180
181
13.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
13.2k
    return hDecoder;
184
13.2k
}
NeAACDecOpen
Line
Count
Source
124
6.89k
{
125
6.89k
    uint8_t i;
126
6.89k
    NeAACDecStruct *hDecoder = NULL;
127
128
6.89k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
6.89k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
6.89k
    hDecoder->cmes = mes;
134
6.89k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
6.89k
    hDecoder->config.defObjectType = MAIN;
136
6.89k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
6.89k
    hDecoder->config.downMatrix = 0;
138
6.89k
    hDecoder->adts_header_present = 0;
139
6.89k
    hDecoder->adif_header_present = 0;
140
6.89k
    hDecoder->latm_header_present = 0;
141
6.89k
#ifdef ERROR_RESILIENCE
142
6.89k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
6.89k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
6.89k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
6.89k
#endif
146
6.89k
    hDecoder->frameLength = 1024;
147
148
6.89k
    hDecoder->frame = 0;
149
6.89k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
6.89k
    hDecoder->__r1 = 0x2bb431ea;
153
6.89k
    hDecoder->__r2 = 0x206155b7;
154
155
448k
    for (i = 0; i < MAX_CHANNELS; i++)
156
441k
    {
157
441k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
441k
        hDecoder->window_shape_prev[i] = 0;
159
441k
        hDecoder->time_out[i] = NULL;
160
441k
        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
441k
#ifdef LTP_DEC
169
441k
        hDecoder->ltp_lag[i] = 0;
170
441k
        hDecoder->lt_pred_stat[i] = NULL;
171
441k
#endif
172
441k
    }
173
174
6.89k
#ifdef SBR_DEC
175
337k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
330k
    {
177
330k
        hDecoder->sbr[i] = NULL;
178
330k
    }
179
6.89k
#endif
180
181
6.89k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
6.89k
    return hDecoder;
184
6.89k
}
NeAACDecOpen
Line
Count
Source
124
13.2k
{
125
13.2k
    uint8_t i;
126
13.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
13.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
13.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
13.2k
    hDecoder->cmes = mes;
134
13.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
13.2k
    hDecoder->config.defObjectType = MAIN;
136
13.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
13.2k
    hDecoder->config.downMatrix = 0;
138
13.2k
    hDecoder->adts_header_present = 0;
139
13.2k
    hDecoder->adif_header_present = 0;
140
13.2k
    hDecoder->latm_header_present = 0;
141
13.2k
#ifdef ERROR_RESILIENCE
142
13.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
13.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
13.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
13.2k
#endif
146
13.2k
    hDecoder->frameLength = 1024;
147
148
13.2k
    hDecoder->frame = 0;
149
13.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
13.2k
    hDecoder->__r1 = 0x2bb431ea;
153
13.2k
    hDecoder->__r2 = 0x206155b7;
154
155
862k
    for (i = 0; i < MAX_CHANNELS; i++)
156
849k
    {
157
849k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
849k
        hDecoder->window_shape_prev[i] = 0;
159
849k
        hDecoder->time_out[i] = NULL;
160
849k
        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
849k
    }
173
174
13.2k
#ifdef SBR_DEC
175
650k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
637k
    {
177
637k
        hDecoder->sbr[i] = NULL;
178
637k
    }
179
13.2k
#endif
180
181
13.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
13.2k
    return hDecoder;
184
13.2k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
19.3k
{
188
19.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
19.3k
    if (hDecoder)
190
19.3k
    {
191
19.3k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
19.3k
        return config;
194
19.3k
    }
195
196
0
    return NULL;
197
19.3k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
19.5k
{
202
19.5k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
19.5k
    if (hDecoder && config)
204
19.5k
    {
205
        /* check if we can decode this object type */
206
19.5k
        if (can_decode_ot(config->defObjectType) < 0)
207
15
            return 0;
208
19.5k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
19.5k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
19.5k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
19.5k
#ifdef FIXED_POINT
217
19.5k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
120
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
19.3k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
19.3k
        if (config->downMatrix > 1)
226
19
            return 0;
227
19.3k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
19.3k
        return 1;
231
19.3k
    }
232
233
0
    return 0;
234
19.5k
}
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
13.0k
{
269
13.0k
    uint32_t bits = 0;
270
13.0k
    bitfile ld;
271
13.0k
    adif_header adif;
272
13.0k
    adts_header adts;
273
13.0k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
13.0k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
21
        return -1;
278
279
12.9k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
12.9k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
12.9k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
12.9k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
12.9k
    *channels = 1;
284
285
12.9k
    if (buffer != NULL)
286
12.9k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
12.9k
        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.9k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
359
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
328
        {
314
328
            hDecoder->adif_header_present = 1;
315
316
328
            get_adif_header(&adif, &ld);
317
328
            faad_byte_align(&ld);
318
319
328
            hDecoder->sf_index = adif.pce[0].sf_index;
320
328
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
328
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
328
            *channels = adif.pce[0].channels;
324
325
328
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
328
            hDecoder->pce_set = 1;
327
328
328
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
12.6k
        } else if (adts_frame(&adts, &ld) == 0) {
332
469
            hDecoder->adts_header_present = 1;
333
334
469
            hDecoder->sf_index = adts.sf_index;
335
469
            hDecoder->object_type = adts.profile + 1;
336
337
469
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
469
            *channels = (adts.channel_configuration > 6) ?
339
423
                2 : adts.channel_configuration;
340
469
        }
341
342
12.9k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
12.9k
        faad_endbits(&ld);
348
12.9k
    }
349
350
12.9k
    if (!*samplerate)
351
115
        return -1;
352
353
12.8k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
12.8k
    if (*channels == 1)
356
12.2k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
12.2k
        *channels = 2;
359
12.2k
    }
360
12.8k
#endif
361
362
12.8k
    hDecoder->channelConfiguration = *channels;
363
364
12.8k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
12.8k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
6.05k
    {
368
6.05k
        *samplerate *= 2;
369
6.05k
        hDecoder->forceUpSampling = 1;
370
6.81k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
6.81k
        hDecoder->downSampledSBR = 1;
372
6.81k
    }
373
12.8k
#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.8k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
7.97k
    if (hDecoder->object_type == LD)
385
311
        hDecoder->frameLength >>= 1;
386
#endif
387
388
12.8k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
164
        return -1;
390
391
12.7k
    return bits;
392
12.8k
}
NeAACDecInit
Line
Count
Source
268
4.95k
{
269
4.95k
    uint32_t bits = 0;
270
4.95k
    bitfile ld;
271
4.95k
    adif_header adif;
272
4.95k
    adts_header adts;
273
4.95k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
4.95k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
11
        return -1;
278
279
4.94k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
4.94k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
4.94k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
4.94k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
4.94k
    *channels = 1;
284
285
4.94k
    if (buffer != NULL)
286
4.94k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
4.94k
        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
4.94k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
145
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
130
        {
314
130
            hDecoder->adif_header_present = 1;
315
316
130
            get_adif_header(&adif, &ld);
317
130
            faad_byte_align(&ld);
318
319
130
            hDecoder->sf_index = adif.pce[0].sf_index;
320
130
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
130
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
130
            *channels = adif.pce[0].channels;
324
325
130
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
130
            hDecoder->pce_set = 1;
327
328
130
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
4.81k
        } else if (adts_frame(&adts, &ld) == 0) {
332
83
            hDecoder->adts_header_present = 1;
333
334
83
            hDecoder->sf_index = adts.sf_index;
335
83
            hDecoder->object_type = adts.profile + 1;
336
337
83
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
83
            *channels = (adts.channel_configuration > 6) ?
339
75
                2 : adts.channel_configuration;
340
83
        }
341
342
4.94k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
4.94k
        faad_endbits(&ld);
348
4.94k
    }
349
350
4.94k
    if (!*samplerate)
351
48
        return -1;
352
353
4.89k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
4.89k
    if (*channels == 1)
356
4.73k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
4.73k
        *channels = 2;
359
4.73k
    }
360
4.89k
#endif
361
362
4.89k
    hDecoder->channelConfiguration = *channels;
363
364
4.89k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
4.89k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
2.31k
    {
368
2.31k
        *samplerate *= 2;
369
2.31k
        hDecoder->forceUpSampling = 1;
370
2.58k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
2.58k
        hDecoder->downSampledSBR = 1;
372
2.58k
    }
373
4.89k
#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
4.89k
        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
4.89k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
88
        return -1;
390
391
4.81k
    return bits;
392
4.89k
}
NeAACDecInit
Line
Count
Source
268
8.05k
{
269
8.05k
    uint32_t bits = 0;
270
8.05k
    bitfile ld;
271
8.05k
    adif_header adif;
272
8.05k
    adts_header adts;
273
8.05k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
8.05k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
10
        return -1;
278
279
8.04k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
8.04k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
8.04k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
8.04k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
8.04k
    *channels = 1;
284
285
8.04k
    if (buffer != NULL)
286
8.04k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
8.04k
        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
8.04k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
214
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
198
        {
314
198
            hDecoder->adif_header_present = 1;
315
316
198
            get_adif_header(&adif, &ld);
317
198
            faad_byte_align(&ld);
318
319
198
            hDecoder->sf_index = adif.pce[0].sf_index;
320
198
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
198
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
198
            *channels = adif.pce[0].channels;
324
325
198
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
198
            hDecoder->pce_set = 1;
327
328
198
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
7.84k
        } else if (adts_frame(&adts, &ld) == 0) {
332
386
            hDecoder->adts_header_present = 1;
333
334
386
            hDecoder->sf_index = adts.sf_index;
335
386
            hDecoder->object_type = adts.profile + 1;
336
337
386
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
386
            *channels = (adts.channel_configuration > 6) ?
339
348
                2 : adts.channel_configuration;
340
386
        }
341
342
8.04k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
8.04k
        faad_endbits(&ld);
348
8.04k
    }
349
350
8.04k
    if (!*samplerate)
351
67
        return -1;
352
353
7.97k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
7.97k
    if (*channels == 1)
356
7.46k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
7.46k
        *channels = 2;
359
7.46k
    }
360
7.97k
#endif
361
362
7.97k
    hDecoder->channelConfiguration = *channels;
363
364
7.97k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
7.97k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
3.74k
    {
368
3.74k
        *samplerate *= 2;
369
3.74k
        hDecoder->forceUpSampling = 1;
370
4.23k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
4.23k
        hDecoder->downSampledSBR = 1;
372
4.23k
    }
373
7.97k
#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
7.97k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
7.97k
#ifdef LD_DEC
384
7.97k
    if (hDecoder->object_type == LD)
385
311
        hDecoder->frameLength >>= 1;
386
7.97k
#endif
387
388
7.97k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
76
        return -1;
390
391
7.89k
    return bits;
392
7.97k
}
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
6.35k
{
401
6.35k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
6.35k
    int8_t rc;
403
6.35k
    mp4AudioSpecificConfig mp4ASC;
404
405
6.35k
    if((hDecoder == NULL)
406
6.35k
        || (pBuffer == NULL)
407
6.35k
        || (SizeOfDecoderSpecificInfo < 2)
408
6.34k
        || (samplerate == NULL)
409
6.34k
        || (channels == NULL))
410
19
    {
411
19
        return -1;
412
19
    }
413
414
6.34k
    hDecoder->adif_header_present = 0;
415
6.34k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
6.34k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
6.34k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
6.34k
    *samplerate = mp4ASC.samplingFrequency;
423
6.34k
    if (mp4ASC.channelsConfiguration)
424
5.39k
    {
425
5.39k
        *channels = mp4ASC.channelsConfiguration;
426
5.39k
    } else {
427
946
        *channels = hDecoder->pce.channels;
428
946
        hDecoder->pce_set = 1;
429
946
    }
430
6.34k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
6.34k
    if (*channels == 1)
433
29
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
29
        *channels = 2;
436
29
    }
437
6.34k
#endif
438
6.34k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
6.34k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
6.34k
#ifdef ERROR_RESILIENCE
441
6.34k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
6.34k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
6.34k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
6.34k
#endif
445
6.34k
#ifdef SBR_DEC
446
6.34k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
6.34k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
6.34k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
6.34k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
6.34k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
3.97k
    {
456
3.97k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
3.97k
    }
458
6.34k
#endif
459
460
6.34k
    if (rc != 0)
461
73
    {
462
73
        return rc;
463
73
    }
464
6.26k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
6.26k
    if (mp4ASC.frameLengthFlag)
466
3.85k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.85k
        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
6.26k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
4.02k
    if (hDecoder->object_type == LD)
482
468
        hDecoder->frameLength >>= 1;
483
#endif
484
485
6.26k
    return 0;
486
6.34k
}
NeAACDecInit2
Line
Count
Source
400
2.28k
{
401
2.28k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
2.28k
    int8_t rc;
403
2.28k
    mp4AudioSpecificConfig mp4ASC;
404
405
2.28k
    if((hDecoder == NULL)
406
2.28k
        || (pBuffer == NULL)
407
2.28k
        || (SizeOfDecoderSpecificInfo < 2)
408
2.27k
        || (samplerate == NULL)
409
2.27k
        || (channels == NULL))
410
9
    {
411
9
        return -1;
412
9
    }
413
414
2.27k
    hDecoder->adif_header_present = 0;
415
2.27k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
2.27k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
2.27k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
2.27k
    *samplerate = mp4ASC.samplingFrequency;
423
2.27k
    if (mp4ASC.channelsConfiguration)
424
1.85k
    {
425
1.85k
        *channels = mp4ASC.channelsConfiguration;
426
1.85k
    } else {
427
412
        *channels = hDecoder->pce.channels;
428
412
        hDecoder->pce_set = 1;
429
412
    }
430
2.27k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
2.27k
    if (*channels == 1)
433
11
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
11
        *channels = 2;
436
11
    }
437
2.27k
#endif
438
2.27k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
2.27k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
2.27k
#ifdef ERROR_RESILIENCE
441
2.27k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
2.27k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
2.27k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
2.27k
#endif
445
2.27k
#ifdef SBR_DEC
446
2.27k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
2.27k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
2.27k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
2.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
2.27k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
1.35k
    {
456
1.35k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
1.35k
    }
458
2.27k
#endif
459
460
2.27k
    if (rc != 0)
461
28
    {
462
28
        return rc;
463
28
    }
464
2.24k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
2.24k
    if (mp4ASC.frameLengthFlag)
466
1.49k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
1.49k
        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.24k
        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.24k
    return 0;
486
2.27k
}
NeAACDecInit2
Line
Count
Source
400
4.07k
{
401
4.07k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
4.07k
    int8_t rc;
403
4.07k
    mp4AudioSpecificConfig mp4ASC;
404
405
4.07k
    if((hDecoder == NULL)
406
4.07k
        || (pBuffer == NULL)
407
4.07k
        || (SizeOfDecoderSpecificInfo < 2)
408
4.06k
        || (samplerate == NULL)
409
4.06k
        || (channels == NULL))
410
10
    {
411
10
        return -1;
412
10
    }
413
414
4.06k
    hDecoder->adif_header_present = 0;
415
4.06k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
4.06k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
4.06k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
4.06k
    *samplerate = mp4ASC.samplingFrequency;
423
4.06k
    if (mp4ASC.channelsConfiguration)
424
3.53k
    {
425
3.53k
        *channels = mp4ASC.channelsConfiguration;
426
3.53k
    } else {
427
534
        *channels = hDecoder->pce.channels;
428
534
        hDecoder->pce_set = 1;
429
534
    }
430
4.06k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
4.06k
    if (*channels == 1)
433
18
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
18
        *channels = 2;
436
18
    }
437
4.06k
#endif
438
4.06k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
4.06k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
4.06k
#ifdef ERROR_RESILIENCE
441
4.06k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
4.06k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
4.06k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
4.06k
#endif
445
4.06k
#ifdef SBR_DEC
446
4.06k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
4.06k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
4.06k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
4.06k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
4.06k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
2.61k
    {
456
2.61k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
2.61k
    }
458
4.06k
#endif
459
460
4.06k
    if (rc != 0)
461
45
    {
462
45
        return rc;
463
45
    }
464
4.02k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
4.02k
    if (mp4ASC.frameLengthFlag)
466
2.36k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
2.36k
        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
4.02k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
4.02k
#ifdef LD_DEC
481
4.02k
    if (hDecoder->object_type == LD)
482
468
        hDecoder->frameLength >>= 1;
483
4.02k
#endif
484
485
4.02k
    return 0;
486
4.06k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
640
{
493
640
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
640
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
640
    NeAACDecClose(*hDecoder);
498
499
640
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
640
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
640
    (*hDecoder)->config.defSampleRate = samplerate;
505
640
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
640
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
640
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
640
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
640
#endif
510
640
    (*hDecoder)->frameLength = 960;
511
640
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
640
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
640
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
104
        (*hDecoder)->channelConfiguration = 2;
516
536
    else
517
536
        (*hDecoder)->channelConfiguration = 1;
518
519
640
#ifdef SBR_DEC
520
640
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
69
        (*hDecoder)->sbr_present_flag = 0;
522
571
    else
523
571
        (*hDecoder)->sbr_present_flag = 1;
524
640
#endif
525
526
640
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
640
    return 0;
529
640
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
7.93k
{
534
7.93k
    uint8_t i;
535
7.93k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
7.93k
    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
515k
    for (i = 0; i < MAX_CHANNELS; i++)
549
507k
    {
550
507k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
507k
        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
507k
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
7.93k
        filter_bank_end(hDecoder->fb);
570
571
7.93k
    drc_end(hDecoder->drc);
572
573
7.93k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
7.93k
#ifdef SBR_DEC
576
388k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
380k
    {
578
380k
        if (hDecoder->sbr[i])
579
92.6k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
380k
    }
581
7.93k
#endif
582
583
7.93k
    if (hDecoder) faad_free(hDecoder);
584
7.93k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
14.8k
{
588
14.8k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
14.8k
    if (hDecoder)
590
14.8k
    {
591
14.8k
        hDecoder->postSeekResetFlag = 1;
592
593
14.8k
        if (frame != -1)
594
14.8k
            hDecoder->frame = frame;
595
14.8k
    }
596
14.8k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
29.5k
{
600
29.5k
    hInfo->num_front_channels = 0;
601
29.5k
    hInfo->num_side_channels = 0;
602
29.5k
    hInfo->num_back_channels = 0;
603
29.5k
    hInfo->num_lfe_channels = 0;
604
29.5k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
29.5k
    if (hDecoder->downMatrix)
607
3.32k
    {
608
3.32k
        hInfo->num_front_channels = 2;
609
3.32k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
3.32k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
3.32k
        return;
612
3.32k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
26.1k
    if (hDecoder->pce_set)
617
1.13k
    {
618
1.13k
        uint8_t i, chpos = 0;
619
1.13k
        uint8_t chdir, back_center = 0;
620
621
1.13k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.13k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.13k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.13k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.13k
        chdir = hInfo->num_front_channels;
626
1.13k
        if (chdir & 1)
627
532
        {
628
532
#if (defined(PS_DEC) || defined(DRM_PS))
629
532
            if (hInfo->num_front_channels == 1 &&
630
248
                hInfo->num_side_channels == 0 &&
631
156
                hInfo->num_back_channels == 0 &&
632
104
                hInfo->num_lfe_channels == 0)
633
68
            {
634
                /* When PS is enabled output is always stereo */
635
68
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
68
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
68
            } else
638
464
#endif
639
464
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
532
            chdir--;
641
532
        }
642
5.86k
        for (i = 0; i < chdir; i++)
643
4.72k
        {
644
4.72k
            hInfo->channel_position[chpos++] =
645
4.72k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
4.72k
        }
647
648
5.62k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
4.49k
        {
650
4.49k
            hInfo->channel_position[chpos++] =
651
4.49k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
4.49k
        }
653
654
1.13k
        chdir = hInfo->num_back_channels;
655
1.13k
        if (chdir & 1)
656
200
        {
657
200
            back_center = 1;
658
200
            chdir--;
659
200
        }
660
4.61k
        for (i = 0; i < chdir; i++)
661
3.48k
        {
662
3.48k
            hInfo->channel_position[chpos++] =
663
3.48k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.48k
        }
665
1.13k
        if (back_center)
666
200
        {
667
200
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
200
        }
669
670
1.99k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
860
        {
672
860
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
860
        }
674
675
25.0k
    } else {
676
25.0k
        switch (hDecoder->channelConfiguration)
677
25.0k
        {
678
1.30k
        case 1:
679
1.30k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
1.30k
            hInfo->num_front_channels = 2;
682
1.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
1.30k
            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.30k
            break;
689
11.3k
        case 2:
690
11.3k
            hInfo->num_front_channels = 2;
691
11.3k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
11.3k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
11.3k
            break;
694
1.76k
        case 3:
695
1.76k
            hInfo->num_front_channels = 3;
696
1.76k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
1.76k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
1.76k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
1.76k
            break;
700
2.86k
        case 4:
701
2.86k
            hInfo->num_front_channels = 3;
702
2.86k
            hInfo->num_back_channels = 1;
703
2.86k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
2.86k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
2.86k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
2.86k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
2.86k
            break;
708
1.82k
        case 5:
709
1.82k
            hInfo->num_front_channels = 3;
710
1.82k
            hInfo->num_back_channels = 2;
711
1.82k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
1.82k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
1.82k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
1.82k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
1.82k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
1.82k
            break;
717
936
        case 6:
718
936
            hInfo->num_front_channels = 3;
719
936
            hInfo->num_back_channels = 2;
720
936
            hInfo->num_lfe_channels = 1;
721
936
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
936
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
936
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
936
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
936
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
936
            hInfo->channel_position[5] = LFE_CHANNEL;
727
936
            break;
728
4.30k
        case 7:
729
4.30k
            hInfo->num_front_channels = 3;
730
4.30k
            hInfo->num_side_channels = 2;
731
4.30k
            hInfo->num_back_channels = 2;
732
4.30k
            hInfo->num_lfe_channels = 1;
733
4.30k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
4.30k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
4.30k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
4.30k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
4.30k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
4.30k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
4.30k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
4.30k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
4.30k
            break;
742
704
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
704
            {
744
704
                uint8_t i;
745
704
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
704
                if (ch & 1) /* there's either a center front or a center back channel */
747
256
                {
748
256
                    uint8_t ch1 = (ch-1)/2;
749
256
                    if (hDecoder->first_syn_ele == ID_SCE)
750
172
                    {
751
172
                        hInfo->num_front_channels = ch1 + 1;
752
172
                        hInfo->num_back_channels = ch1;
753
172
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
736
                        for (i = 1; i <= ch1; i+=2)
755
564
                        {
756
564
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
564
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
564
                        }
759
736
                        for (i = ch1+1; i < ch; i+=2)
760
564
                        {
761
564
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
564
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
564
                        }
764
172
                    } else {
765
84
                        hInfo->num_front_channels = ch1;
766
84
                        hInfo->num_back_channels = ch1 + 1;
767
552
                        for (i = 0; i < ch1; i+=2)
768
468
                        {
769
468
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
468
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
468
                        }
772
552
                        for (i = ch1; i < ch-1; i+=2)
773
468
                        {
774
468
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
468
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
468
                        }
777
84
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
84
                    }
779
448
                } else {
780
448
                    uint8_t ch1 = (ch)/2;
781
448
                    hInfo->num_front_channels = ch1;
782
448
                    hInfo->num_back_channels = ch1;
783
448
                    if (ch1 & 1)
784
220
                    {
785
220
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.35k
                        for (i = 1; i <= ch1; i+=2)
787
1.13k
                        {
788
1.13k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.13k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.13k
                        }
791
1.13k
                        for (i = ch1+1; i < ch-1; i+=2)
792
916
                        {
793
916
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
916
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
916
                        }
796
220
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
228
                    } else {
798
1.45k
                        for (i = 0; i < ch1; i+=2)
799
1.22k
                        {
800
1.22k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
1.22k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
1.22k
                        }
803
1.45k
                        for (i = ch1; i < ch; i+=2)
804
1.22k
                        {
805
1.22k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
1.22k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
1.22k
                        }
808
228
                    }
809
448
                }
810
704
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
1.55k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
852
                {
813
852
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
852
                }
815
704
            }
816
704
            break;
817
25.0k
        }
818
25.0k
    }
819
26.1k
}
decoder.c:create_channel_config
Line
Count
Source
599
7.38k
{
600
7.38k
    hInfo->num_front_channels = 0;
601
7.38k
    hInfo->num_side_channels = 0;
602
7.38k
    hInfo->num_back_channels = 0;
603
7.38k
    hInfo->num_lfe_channels = 0;
604
7.38k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
7.38k
    if (hDecoder->downMatrix)
607
831
    {
608
831
        hInfo->num_front_channels = 2;
609
831
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
831
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
831
        return;
612
831
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
6.54k
    if (hDecoder->pce_set)
617
284
    {
618
284
        uint8_t i, chpos = 0;
619
284
        uint8_t chdir, back_center = 0;
620
621
284
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
284
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
284
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
284
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
284
        chdir = hInfo->num_front_channels;
626
284
        if (chdir & 1)
627
133
        {
628
133
#if (defined(PS_DEC) || defined(DRM_PS))
629
133
            if (hInfo->num_front_channels == 1 &&
630
62
                hInfo->num_side_channels == 0 &&
631
39
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
17
            {
634
                /* When PS is enabled output is always stereo */
635
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
17
            } else
638
116
#endif
639
116
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
133
            chdir--;
641
133
        }
642
1.46k
        for (i = 0; i < chdir; i++)
643
1.18k
        {
644
1.18k
            hInfo->channel_position[chpos++] =
645
1.18k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.18k
        }
647
648
1.40k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
284
        chdir = hInfo->num_back_channels;
655
284
        if (chdir & 1)
656
50
        {
657
50
            back_center = 1;
658
50
            chdir--;
659
50
        }
660
1.15k
        for (i = 0; i < chdir; i++)
661
870
        {
662
870
            hInfo->channel_position[chpos++] =
663
870
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
870
        }
665
284
        if (back_center)
666
50
        {
667
50
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
50
        }
669
670
499
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
215
        {
672
215
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
215
        }
674
675
6.26k
    } else {
676
6.26k
        switch (hDecoder->channelConfiguration)
677
6.26k
        {
678
327
        case 1:
679
327
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
327
            hInfo->num_front_channels = 2;
682
327
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
327
            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
327
            break;
689
2.84k
        case 2:
690
2.84k
            hInfo->num_front_channels = 2;
691
2.84k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
2.84k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
2.84k
            break;
694
440
        case 3:
695
440
            hInfo->num_front_channels = 3;
696
440
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
440
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
440
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
440
            break;
700
717
        case 4:
701
717
            hInfo->num_front_channels = 3;
702
717
            hInfo->num_back_channels = 1;
703
717
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
717
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
717
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
717
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
717
            break;
708
456
        case 5:
709
456
            hInfo->num_front_channels = 3;
710
456
            hInfo->num_back_channels = 2;
711
456
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
456
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
456
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
456
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
456
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
456
            break;
717
234
        case 6:
718
234
            hInfo->num_front_channels = 3;
719
234
            hInfo->num_back_channels = 2;
720
234
            hInfo->num_lfe_channels = 1;
721
234
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
234
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
234
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
234
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
234
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
234
            hInfo->channel_position[5] = LFE_CHANNEL;
727
234
            break;
728
1.07k
        case 7:
729
1.07k
            hInfo->num_front_channels = 3;
730
1.07k
            hInfo->num_side_channels = 2;
731
1.07k
            hInfo->num_back_channels = 2;
732
1.07k
            hInfo->num_lfe_channels = 1;
733
1.07k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.07k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.07k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.07k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.07k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.07k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.07k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.07k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.07k
            break;
742
176
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
176
            {
744
176
                uint8_t i;
745
176
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
176
                if (ch & 1) /* there's either a center front or a center back channel */
747
64
                {
748
64
                    uint8_t ch1 = (ch-1)/2;
749
64
                    if (hDecoder->first_syn_ele == ID_SCE)
750
43
                    {
751
43
                        hInfo->num_front_channels = ch1 + 1;
752
43
                        hInfo->num_back_channels = ch1;
753
43
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
184
                        for (i = 1; i <= ch1; i+=2)
755
141
                        {
756
141
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
141
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
141
                        }
759
184
                        for (i = ch1+1; i < ch; i+=2)
760
141
                        {
761
141
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
141
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
141
                        }
764
43
                    } else {
765
21
                        hInfo->num_front_channels = ch1;
766
21
                        hInfo->num_back_channels = ch1 + 1;
767
138
                        for (i = 0; i < ch1; i+=2)
768
117
                        {
769
117
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
117
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
117
                        }
772
138
                        for (i = ch1; i < ch-1; i+=2)
773
117
                        {
774
117
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
117
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
117
                        }
777
21
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
21
                    }
779
112
                } else {
780
112
                    uint8_t ch1 = (ch)/2;
781
112
                    hInfo->num_front_channels = ch1;
782
112
                    hInfo->num_back_channels = ch1;
783
112
                    if (ch1 & 1)
784
55
                    {
785
55
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
339
                        for (i = 1; i <= ch1; i+=2)
787
284
                        {
788
284
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
284
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
284
                        }
791
284
                        for (i = ch1+1; i < ch-1; i+=2)
792
229
                        {
793
229
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
229
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
229
                        }
796
55
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
57
                    } else {
798
364
                        for (i = 0; i < ch1; i+=2)
799
307
                        {
800
307
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
307
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
307
                        }
803
364
                        for (i = ch1; i < ch; i+=2)
804
307
                        {
805
307
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
307
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
307
                        }
808
57
                    }
809
112
                }
810
176
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
389
                for (i = ch; i < hDecoder->fr_channels; i++)
812
213
                {
813
213
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
213
                }
815
176
            }
816
176
            break;
817
6.26k
        }
818
6.26k
    }
819
6.54k
}
decoder.c:create_channel_config
Line
Count
Source
599
7.38k
{
600
7.38k
    hInfo->num_front_channels = 0;
601
7.38k
    hInfo->num_side_channels = 0;
602
7.38k
    hInfo->num_back_channels = 0;
603
7.38k
    hInfo->num_lfe_channels = 0;
604
7.38k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
7.38k
    if (hDecoder->downMatrix)
607
831
    {
608
831
        hInfo->num_front_channels = 2;
609
831
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
831
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
831
        return;
612
831
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
6.54k
    if (hDecoder->pce_set)
617
284
    {
618
284
        uint8_t i, chpos = 0;
619
284
        uint8_t chdir, back_center = 0;
620
621
284
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
284
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
284
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
284
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
284
        chdir = hInfo->num_front_channels;
626
284
        if (chdir & 1)
627
133
        {
628
133
#if (defined(PS_DEC) || defined(DRM_PS))
629
133
            if (hInfo->num_front_channels == 1 &&
630
62
                hInfo->num_side_channels == 0 &&
631
39
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
17
            {
634
                /* When PS is enabled output is always stereo */
635
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
17
            } else
638
116
#endif
639
116
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
133
            chdir--;
641
133
        }
642
1.46k
        for (i = 0; i < chdir; i++)
643
1.18k
        {
644
1.18k
            hInfo->channel_position[chpos++] =
645
1.18k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.18k
        }
647
648
1.40k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
284
        chdir = hInfo->num_back_channels;
655
284
        if (chdir & 1)
656
50
        {
657
50
            back_center = 1;
658
50
            chdir--;
659
50
        }
660
1.15k
        for (i = 0; i < chdir; i++)
661
870
        {
662
870
            hInfo->channel_position[chpos++] =
663
870
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
870
        }
665
284
        if (back_center)
666
50
        {
667
50
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
50
        }
669
670
499
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
215
        {
672
215
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
215
        }
674
675
6.26k
    } else {
676
6.26k
        switch (hDecoder->channelConfiguration)
677
6.26k
        {
678
327
        case 1:
679
327
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
327
            hInfo->num_front_channels = 2;
682
327
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
327
            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
327
            break;
689
2.84k
        case 2:
690
2.84k
            hInfo->num_front_channels = 2;
691
2.84k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
2.84k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
2.84k
            break;
694
440
        case 3:
695
440
            hInfo->num_front_channels = 3;
696
440
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
440
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
440
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
440
            break;
700
717
        case 4:
701
717
            hInfo->num_front_channels = 3;
702
717
            hInfo->num_back_channels = 1;
703
717
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
717
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
717
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
717
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
717
            break;
708
456
        case 5:
709
456
            hInfo->num_front_channels = 3;
710
456
            hInfo->num_back_channels = 2;
711
456
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
456
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
456
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
456
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
456
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
456
            break;
717
234
        case 6:
718
234
            hInfo->num_front_channels = 3;
719
234
            hInfo->num_back_channels = 2;
720
234
            hInfo->num_lfe_channels = 1;
721
234
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
234
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
234
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
234
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
234
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
234
            hInfo->channel_position[5] = LFE_CHANNEL;
727
234
            break;
728
1.07k
        case 7:
729
1.07k
            hInfo->num_front_channels = 3;
730
1.07k
            hInfo->num_side_channels = 2;
731
1.07k
            hInfo->num_back_channels = 2;
732
1.07k
            hInfo->num_lfe_channels = 1;
733
1.07k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.07k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.07k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.07k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.07k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.07k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.07k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.07k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.07k
            break;
742
176
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
176
            {
744
176
                uint8_t i;
745
176
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
176
                if (ch & 1) /* there's either a center front or a center back channel */
747
64
                {
748
64
                    uint8_t ch1 = (ch-1)/2;
749
64
                    if (hDecoder->first_syn_ele == ID_SCE)
750
43
                    {
751
43
                        hInfo->num_front_channels = ch1 + 1;
752
43
                        hInfo->num_back_channels = ch1;
753
43
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
184
                        for (i = 1; i <= ch1; i+=2)
755
141
                        {
756
141
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
141
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
141
                        }
759
184
                        for (i = ch1+1; i < ch; i+=2)
760
141
                        {
761
141
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
141
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
141
                        }
764
43
                    } else {
765
21
                        hInfo->num_front_channels = ch1;
766
21
                        hInfo->num_back_channels = ch1 + 1;
767
138
                        for (i = 0; i < ch1; i+=2)
768
117
                        {
769
117
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
117
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
117
                        }
772
138
                        for (i = ch1; i < ch-1; i+=2)
773
117
                        {
774
117
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
117
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
117
                        }
777
21
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
21
                    }
779
112
                } else {
780
112
                    uint8_t ch1 = (ch)/2;
781
112
                    hInfo->num_front_channels = ch1;
782
112
                    hInfo->num_back_channels = ch1;
783
112
                    if (ch1 & 1)
784
55
                    {
785
55
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
339
                        for (i = 1; i <= ch1; i+=2)
787
284
                        {
788
284
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
284
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
284
                        }
791
284
                        for (i = ch1+1; i < ch-1; i+=2)
792
229
                        {
793
229
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
229
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
229
                        }
796
55
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
57
                    } else {
798
364
                        for (i = 0; i < ch1; i+=2)
799
307
                        {
800
307
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
307
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
307
                        }
803
364
                        for (i = ch1; i < ch; i+=2)
804
307
                        {
805
307
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
307
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
307
                        }
808
57
                    }
809
112
                }
810
176
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
389
                for (i = ch; i < hDecoder->fr_channels; i++)
812
213
                {
813
213
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
213
                }
815
176
            }
816
176
            break;
817
6.26k
        }
818
6.26k
    }
819
6.54k
}
decoder.c:create_channel_config
Line
Count
Source
599
7.38k
{
600
7.38k
    hInfo->num_front_channels = 0;
601
7.38k
    hInfo->num_side_channels = 0;
602
7.38k
    hInfo->num_back_channels = 0;
603
7.38k
    hInfo->num_lfe_channels = 0;
604
7.38k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
7.38k
    if (hDecoder->downMatrix)
607
831
    {
608
831
        hInfo->num_front_channels = 2;
609
831
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
831
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
831
        return;
612
831
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
6.54k
    if (hDecoder->pce_set)
617
284
    {
618
284
        uint8_t i, chpos = 0;
619
284
        uint8_t chdir, back_center = 0;
620
621
284
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
284
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
284
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
284
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
284
        chdir = hInfo->num_front_channels;
626
284
        if (chdir & 1)
627
133
        {
628
133
#if (defined(PS_DEC) || defined(DRM_PS))
629
133
            if (hInfo->num_front_channels == 1 &&
630
62
                hInfo->num_side_channels == 0 &&
631
39
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
17
            {
634
                /* When PS is enabled output is always stereo */
635
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
17
            } else
638
116
#endif
639
116
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
133
            chdir--;
641
133
        }
642
1.46k
        for (i = 0; i < chdir; i++)
643
1.18k
        {
644
1.18k
            hInfo->channel_position[chpos++] =
645
1.18k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.18k
        }
647
648
1.40k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
284
        chdir = hInfo->num_back_channels;
655
284
        if (chdir & 1)
656
50
        {
657
50
            back_center = 1;
658
50
            chdir--;
659
50
        }
660
1.15k
        for (i = 0; i < chdir; i++)
661
870
        {
662
870
            hInfo->channel_position[chpos++] =
663
870
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
870
        }
665
284
        if (back_center)
666
50
        {
667
50
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
50
        }
669
670
499
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
215
        {
672
215
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
215
        }
674
675
6.26k
    } else {
676
6.26k
        switch (hDecoder->channelConfiguration)
677
6.26k
        {
678
327
        case 1:
679
327
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
327
            hInfo->num_front_channels = 2;
682
327
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
327
            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
327
            break;
689
2.84k
        case 2:
690
2.84k
            hInfo->num_front_channels = 2;
691
2.84k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
2.84k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
2.84k
            break;
694
440
        case 3:
695
440
            hInfo->num_front_channels = 3;
696
440
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
440
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
440
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
440
            break;
700
717
        case 4:
701
717
            hInfo->num_front_channels = 3;
702
717
            hInfo->num_back_channels = 1;
703
717
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
717
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
717
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
717
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
717
            break;
708
456
        case 5:
709
456
            hInfo->num_front_channels = 3;
710
456
            hInfo->num_back_channels = 2;
711
456
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
456
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
456
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
456
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
456
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
456
            break;
717
234
        case 6:
718
234
            hInfo->num_front_channels = 3;
719
234
            hInfo->num_back_channels = 2;
720
234
            hInfo->num_lfe_channels = 1;
721
234
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
234
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
234
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
234
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
234
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
234
            hInfo->channel_position[5] = LFE_CHANNEL;
727
234
            break;
728
1.07k
        case 7:
729
1.07k
            hInfo->num_front_channels = 3;
730
1.07k
            hInfo->num_side_channels = 2;
731
1.07k
            hInfo->num_back_channels = 2;
732
1.07k
            hInfo->num_lfe_channels = 1;
733
1.07k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.07k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.07k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.07k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.07k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.07k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.07k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.07k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.07k
            break;
742
176
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
176
            {
744
176
                uint8_t i;
745
176
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
176
                if (ch & 1) /* there's either a center front or a center back channel */
747
64
                {
748
64
                    uint8_t ch1 = (ch-1)/2;
749
64
                    if (hDecoder->first_syn_ele == ID_SCE)
750
43
                    {
751
43
                        hInfo->num_front_channels = ch1 + 1;
752
43
                        hInfo->num_back_channels = ch1;
753
43
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
184
                        for (i = 1; i <= ch1; i+=2)
755
141
                        {
756
141
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
141
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
141
                        }
759
184
                        for (i = ch1+1; i < ch; i+=2)
760
141
                        {
761
141
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
141
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
141
                        }
764
43
                    } else {
765
21
                        hInfo->num_front_channels = ch1;
766
21
                        hInfo->num_back_channels = ch1 + 1;
767
138
                        for (i = 0; i < ch1; i+=2)
768
117
                        {
769
117
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
117
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
117
                        }
772
138
                        for (i = ch1; i < ch-1; i+=2)
773
117
                        {
774
117
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
117
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
117
                        }
777
21
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
21
                    }
779
112
                } else {
780
112
                    uint8_t ch1 = (ch)/2;
781
112
                    hInfo->num_front_channels = ch1;
782
112
                    hInfo->num_back_channels = ch1;
783
112
                    if (ch1 & 1)
784
55
                    {
785
55
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
339
                        for (i = 1; i <= ch1; i+=2)
787
284
                        {
788
284
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
284
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
284
                        }
791
284
                        for (i = ch1+1; i < ch-1; i+=2)
792
229
                        {
793
229
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
229
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
229
                        }
796
55
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
57
                    } else {
798
364
                        for (i = 0; i < ch1; i+=2)
799
307
                        {
800
307
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
307
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
307
                        }
803
364
                        for (i = ch1; i < ch; i+=2)
804
307
                        {
805
307
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
307
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
307
                        }
808
57
                    }
809
112
                }
810
176
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
389
                for (i = ch; i < hDecoder->fr_channels; i++)
812
213
                {
813
213
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
213
                }
815
176
            }
816
176
            break;
817
6.26k
        }
818
6.26k
    }
819
6.54k
}
decoder.c:create_channel_config
Line
Count
Source
599
7.38k
{
600
7.38k
    hInfo->num_front_channels = 0;
601
7.38k
    hInfo->num_side_channels = 0;
602
7.38k
    hInfo->num_back_channels = 0;
603
7.38k
    hInfo->num_lfe_channels = 0;
604
7.38k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
7.38k
    if (hDecoder->downMatrix)
607
831
    {
608
831
        hInfo->num_front_channels = 2;
609
831
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
831
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
831
        return;
612
831
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
6.54k
    if (hDecoder->pce_set)
617
284
    {
618
284
        uint8_t i, chpos = 0;
619
284
        uint8_t chdir, back_center = 0;
620
621
284
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
284
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
284
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
284
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
284
        chdir = hInfo->num_front_channels;
626
284
        if (chdir & 1)
627
133
        {
628
133
#if (defined(PS_DEC) || defined(DRM_PS))
629
133
            if (hInfo->num_front_channels == 1 &&
630
62
                hInfo->num_side_channels == 0 &&
631
39
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
17
            {
634
                /* When PS is enabled output is always stereo */
635
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
17
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
17
            } else
638
116
#endif
639
116
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
133
            chdir--;
641
133
        }
642
1.46k
        for (i = 0; i < chdir; i++)
643
1.18k
        {
644
1.18k
            hInfo->channel_position[chpos++] =
645
1.18k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.18k
        }
647
648
1.40k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
284
        chdir = hInfo->num_back_channels;
655
284
        if (chdir & 1)
656
50
        {
657
50
            back_center = 1;
658
50
            chdir--;
659
50
        }
660
1.15k
        for (i = 0; i < chdir; i++)
661
870
        {
662
870
            hInfo->channel_position[chpos++] =
663
870
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
870
        }
665
284
        if (back_center)
666
50
        {
667
50
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
50
        }
669
670
499
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
215
        {
672
215
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
215
        }
674
675
6.26k
    } else {
676
6.26k
        switch (hDecoder->channelConfiguration)
677
6.26k
        {
678
327
        case 1:
679
327
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
327
            hInfo->num_front_channels = 2;
682
327
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
327
            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
327
            break;
689
2.84k
        case 2:
690
2.84k
            hInfo->num_front_channels = 2;
691
2.84k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
2.84k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
2.84k
            break;
694
440
        case 3:
695
440
            hInfo->num_front_channels = 3;
696
440
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
440
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
440
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
440
            break;
700
717
        case 4:
701
717
            hInfo->num_front_channels = 3;
702
717
            hInfo->num_back_channels = 1;
703
717
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
717
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
717
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
717
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
717
            break;
708
456
        case 5:
709
456
            hInfo->num_front_channels = 3;
710
456
            hInfo->num_back_channels = 2;
711
456
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
456
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
456
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
456
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
456
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
456
            break;
717
234
        case 6:
718
234
            hInfo->num_front_channels = 3;
719
234
            hInfo->num_back_channels = 2;
720
234
            hInfo->num_lfe_channels = 1;
721
234
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
234
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
234
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
234
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
234
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
234
            hInfo->channel_position[5] = LFE_CHANNEL;
727
234
            break;
728
1.07k
        case 7:
729
1.07k
            hInfo->num_front_channels = 3;
730
1.07k
            hInfo->num_side_channels = 2;
731
1.07k
            hInfo->num_back_channels = 2;
732
1.07k
            hInfo->num_lfe_channels = 1;
733
1.07k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.07k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.07k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.07k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.07k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.07k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.07k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.07k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.07k
            break;
742
176
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
176
            {
744
176
                uint8_t i;
745
176
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
176
                if (ch & 1) /* there's either a center front or a center back channel */
747
64
                {
748
64
                    uint8_t ch1 = (ch-1)/2;
749
64
                    if (hDecoder->first_syn_ele == ID_SCE)
750
43
                    {
751
43
                        hInfo->num_front_channels = ch1 + 1;
752
43
                        hInfo->num_back_channels = ch1;
753
43
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
184
                        for (i = 1; i <= ch1; i+=2)
755
141
                        {
756
141
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
141
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
141
                        }
759
184
                        for (i = ch1+1; i < ch; i+=2)
760
141
                        {
761
141
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
141
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
141
                        }
764
43
                    } else {
765
21
                        hInfo->num_front_channels = ch1;
766
21
                        hInfo->num_back_channels = ch1 + 1;
767
138
                        for (i = 0; i < ch1; i+=2)
768
117
                        {
769
117
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
117
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
117
                        }
772
138
                        for (i = ch1; i < ch-1; i+=2)
773
117
                        {
774
117
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
117
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
117
                        }
777
21
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
21
                    }
779
112
                } else {
780
112
                    uint8_t ch1 = (ch)/2;
781
112
                    hInfo->num_front_channels = ch1;
782
112
                    hInfo->num_back_channels = ch1;
783
112
                    if (ch1 & 1)
784
55
                    {
785
55
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
339
                        for (i = 1; i <= ch1; i+=2)
787
284
                        {
788
284
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
284
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
284
                        }
791
284
                        for (i = ch1+1; i < ch-1; i+=2)
792
229
                        {
793
229
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
229
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
229
                        }
796
55
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
57
                    } else {
798
364
                        for (i = 0; i < ch1; i+=2)
799
307
                        {
800
307
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
307
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
307
                        }
803
364
                        for (i = ch1; i < ch; i+=2)
804
307
                        {
805
307
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
307
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
307
                        }
808
57
                    }
809
112
                }
810
176
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
389
                for (i = ch; i < hDecoder->fr_channels; i++)
812
213
                {
813
213
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
213
                }
815
176
            }
816
176
            break;
817
6.26k
        }
818
6.26k
    }
819
6.54k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
28.2k
{
826
28.2k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
28.2k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
28.2k
}
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
9.45k
{
837
9.45k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
9.45k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
64
    {
840
64
        hInfo->error = 27;
841
64
        return NULL;
842
64
    }
843
844
9.38k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
9.38k
        sample_buffer, sample_buffer_size);
846
9.45k
}
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
37.6k
{
855
37.6k
    uint16_t i;
856
37.6k
    uint8_t channels = 0;
857
37.6k
    uint8_t output_channels = 0;
858
37.6k
    bitfile ld;
859
37.6k
    uint32_t bitsconsumed;
860
37.6k
    uint16_t frame_len;
861
37.6k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
37.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
37.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
37.6k
    frame_len = hDecoder->frameLength;
882
883
884
37.6k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
37.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
37.6k
    if (buffer_size >= 128)
903
5.05k
    {
904
5.05k
        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
5.05k
    }
912
913
914
    /* initialize the bitstream */
915
37.6k
    faad_initbits(&ld, buffer, buffer_size);
916
37.6k
    if (ld.error != 0)
917
14.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
8.53k
    if (hDecoder->object_type == DRM_ER_LC)
952
822
    {
953
        /* We do not support stereo right now */
954
822
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
822
        faad_getbits(&ld, 8
961
822
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
822
    }
963
8.53k
#endif
964
965
22.9k
    if (hDecoder->adts_header_present)
966
696
    {
967
696
        adts_header adts;
968
969
696
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
696
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
79
            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
696
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
8.52k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
822
    {
987
822
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
7.70k
    } else {
989
7.70k
#endif
990
14.3k
        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
14.3k
    channels = hDecoder->fr_channels;
1009
1010
22.8k
    if (hInfo->error > 0)
1011
15.3k
        goto error;
1012
1013
    /* safety check */
1014
7.47k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
88
    {
1016
        /* invalid number of channels */
1017
88
        hInfo->error = 12;
1018
88
        goto error;
1019
88
    }
1020
1021
    /* no more bit reading after this */
1022
7.38k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
7.38k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
7.38k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
7.38k
    faad_endbits(&ld);
1030
1031
1032
7.38k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
7.38k
        )
1037
7.20k
    {
1038
7.20k
        if (hDecoder->channelConfiguration == 0)
1039
254
            hDecoder->channelConfiguration = channels;
1040
1041
7.20k
        if (channels == 8) /* 7.1 */
1042
723
            hDecoder->channelConfiguration = 7;
1043
7.20k
        if (channels == 7) /* not a standard channelConfiguration */
1044
39
            hDecoder->channelConfiguration = 0;
1045
7.20k
    }
1046
1047
7.38k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
823
    {
1049
823
        hDecoder->downMatrix = 1;
1050
823
        output_channels = 2;
1051
6.56k
    } else {
1052
6.56k
        output_channels = channels;
1053
6.56k
    }
1054
1055
7.38k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
7.38k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
7.38k
    if (output_channels == 1)
1059
212
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
212
        hDecoder->upMatrix = 1;
1062
212
        output_channels = 2;
1063
212
    }
1064
7.38k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
7.38k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
293
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
293
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
9
        {
1072
9
            hInfo->error = 22;
1073
9
            return NULL;
1074
9
        }
1075
293
    }
1076
7.38k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
7.38k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
7.38k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
7.38k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
7.38k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
7.38k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
7.38k
    hInfo->header_type = RAW;
1090
7.38k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
7.38k
    if (hDecoder->adts_header_present)
1093
187
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
7.38k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
7.38k
    hInfo->ps = hDecoder->ps_used_global;
1100
7.38k
#endif
1101
1102
    /* check if frame has channel elements */
1103
7.38k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
7.38k
    {
1110
7.38k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
7.38k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
7.38k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
7.38k
        };
1114
7.38k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
7.38k
#ifdef SBR_DEC
1116
7.38k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
4.12k
        {
1118
4.12k
            stride = 2 * stride;
1119
4.12k
        }
1120
7.38k
#endif
1121
7.38k
        required_buffer_size = frame_len*output_channels*stride;
1122
7.38k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
7.38k
    if (sample_buffer_size == 0)
1126
4.68k
    {
1127
        /* allocate the buffer for the final samples */
1128
4.68k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
4.11k
        {
1130
4.11k
            if (hDecoder->sample_buffer)
1131
89
                faad_free(hDecoder->sample_buffer);
1132
4.11k
            hDecoder->sample_buffer = NULL;
1133
4.11k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
4.11k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
4.11k
        }
1136
4.68k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.02k
        hInfo->error = 27;
1139
1.02k
        return NULL;
1140
1.02k
    }
1141
1142
6.35k
    if (sample_buffer_size == 0)
1143
4.68k
    {
1144
4.68k
        sample_buffer = hDecoder->sample_buffer;
1145
4.68k
    } else {
1146
1.67k
        sample_buffer = *sample_buffer2;
1147
1.67k
    }
1148
1149
6.35k
#ifdef SBR_DEC
1150
6.35k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
4.39k
    {
1152
4.39k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
4.39k
        if (!hDecoder->downSampledSBR)
1156
3.36k
        {
1157
3.36k
            frame_len *= 2;
1158
3.36k
            hInfo->samples *= 2;
1159
3.36k
            hInfo->samplerate *= 2;
1160
3.36k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
24.6k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
20.2k
        {
1165
20.2k
            if (hDecoder->sbr[ele] == NULL)
1166
9
            {
1167
9
                hInfo->error = 25;
1168
9
                goto error;
1169
9
            }
1170
20.2k
        }
1171
1172
        /* sbr */
1173
4.38k
        if (hDecoder->sbr_present_flag == 1)
1174
2.84k
        {
1175
2.84k
            hInfo->object_type = HE_AAC;
1176
2.84k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.84k
        } else {
1178
1.53k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.53k
        }
1180
4.38k
        if (hDecoder->downSampledSBR)
1181
1.01k
        {
1182
1.01k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.01k
        }
1184
4.38k
    }
1185
6.34k
#endif
1186
1187
1188
6.34k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
6.34k
        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
6.34k
    hDecoder->postSeekResetFlag = 0;
1198
1199
6.34k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
4.13k
    if (hDecoder->object_type != LD)
1202
3.58k
    {
1203
3.58k
#endif
1204
5.79k
        if (hDecoder->frame <= 1)
1205
2.15k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
552
    } else {
1208
        /* LD encoders will give lower delay */
1209
552
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
552
    }
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
6.34k
    return sample_buffer;
1225
1226
15.5k
error:
1227
1228
    /* reset filterbank state */
1229
1.01M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
996k
    {
1231
996k
        if (hDecoder->fb_intermed[i] != NULL)
1232
480k
        {
1233
480k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
480k
        }
1235
996k
    }
1236
15.5k
#ifdef SBR_DEC
1237
763k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
747k
    {
1239
747k
        if (hDecoder->sbr[i] != NULL)
1240
347k
        {
1241
347k
            sbrReset(hDecoder->sbr[i]);
1242
347k
        }
1243
747k
    }
1244
15.5k
#endif
1245
1246
1247
15.5k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
6.35k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
14.0k
{
855
14.0k
    uint16_t i;
856
14.0k
    uint8_t channels = 0;
857
14.0k
    uint8_t output_channels = 0;
858
14.0k
    bitfile ld;
859
14.0k
    uint32_t bitsconsumed;
860
14.0k
    uint16_t frame_len;
861
14.0k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
14.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
14.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
14.0k
    frame_len = hDecoder->frameLength;
882
883
884
14.0k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
14.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
14.0k
    if (buffer_size >= 128)
903
2.30k
    {
904
2.30k
        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.30k
    }
912
913
914
    /* initialize the bitstream */
915
14.0k
    faad_initbits(&ld, buffer, buffer_size);
916
14.0k
    if (ld.error != 0)
917
5.47k
        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
8.53k
#ifdef DRM
951
8.53k
    if (hDecoder->object_type == DRM_ER_LC)
952
822
    {
953
        /* We do not support stereo right now */
954
822
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
822
        faad_getbits(&ld, 8
961
822
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
822
    }
963
8.53k
#endif
964
965
8.53k
    if (hDecoder->adts_header_present)
966
80
    {
967
80
        adts_header adts;
968
969
80
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
80
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
4
            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
80
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
8.52k
#ifdef DRM
985
8.52k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
822
    {
987
822
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
7.70k
    } else {
989
7.70k
#endif
990
7.70k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
7.70k
#ifdef DRM
992
7.70k
    }
993
8.52k
#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
8.52k
    channels = hDecoder->fr_channels;
1009
1010
8.52k
    if (hInfo->error > 0)
1011
5.73k
        goto error;
1012
1013
    /* safety check */
1014
2.79k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
32
    {
1016
        /* invalid number of channels */
1017
32
        hInfo->error = 12;
1018
32
        goto error;
1019
32
    }
1020
1021
    /* no more bit reading after this */
1022
2.76k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
2.76k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
2.76k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
2.76k
    faad_endbits(&ld);
1030
1031
1032
2.76k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
2.76k
        )
1037
2.73k
    {
1038
2.73k
        if (hDecoder->channelConfiguration == 0)
1039
119
            hDecoder->channelConfiguration = channels;
1040
1041
2.73k
        if (channels == 8) /* 7.1 */
1042
101
            hDecoder->channelConfiguration = 7;
1043
2.73k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
2.73k
    }
1046
1047
2.76k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
410
    {
1049
410
        hDecoder->downMatrix = 1;
1050
410
        output_channels = 2;
1051
2.35k
    } else {
1052
2.35k
        output_channels = channels;
1053
2.35k
    }
1054
1055
2.76k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
2.76k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
2.76k
    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
2.76k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
2.76k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
135
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
135
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
3
        {
1072
3
            hInfo->error = 22;
1073
3
            return NULL;
1074
3
        }
1075
135
    }
1076
2.76k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
2.76k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
2.76k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
2.76k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
2.76k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
2.76k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
2.76k
    hInfo->header_type = RAW;
1090
2.76k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
2.76k
    if (hDecoder->adts_header_present)
1093
27
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
2.76k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
2.76k
    hInfo->ps = hDecoder->ps_used_global;
1100
2.76k
#endif
1101
1102
    /* check if frame has channel elements */
1103
2.76k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
2.76k
    {
1110
2.76k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
2.76k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
2.76k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
2.76k
        };
1114
2.76k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
2.76k
#ifdef SBR_DEC
1116
2.76k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
1.51k
        {
1118
1.51k
            stride = 2 * stride;
1119
1.51k
        }
1120
2.76k
#endif
1121
2.76k
        required_buffer_size = frame_len*output_channels*stride;
1122
2.76k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
2.76k
    if (sample_buffer_size == 0)
1126
1.38k
    {
1127
        /* allocate the buffer for the final samples */
1128
1.38k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
1.22k
        {
1130
1.22k
            if (hDecoder->sample_buffer)
1131
38
                faad_free(hDecoder->sample_buffer);
1132
1.22k
            hDecoder->sample_buffer = NULL;
1133
1.22k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
1.22k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
1.22k
        }
1136
1.38k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
548
        hInfo->error = 27;
1139
548
        return NULL;
1140
548
    }
1141
1142
2.21k
    if (sample_buffer_size == 0)
1143
1.38k
    {
1144
1.38k
        sample_buffer = hDecoder->sample_buffer;
1145
1.38k
    } else {
1146
828
        sample_buffer = *sample_buffer2;
1147
828
    }
1148
1149
2.21k
#ifdef SBR_DEC
1150
2.21k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
1.54k
    {
1152
1.54k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
1.54k
        if (!hDecoder->downSampledSBR)
1156
1.10k
        {
1157
1.10k
            frame_len *= 2;
1158
1.10k
            hInfo->samples *= 2;
1159
1.10k
            hInfo->samplerate *= 2;
1160
1.10k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
8.72k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
7.18k
        {
1165
7.18k
            if (hDecoder->sbr[ele] == NULL)
1166
3
            {
1167
3
                hInfo->error = 25;
1168
3
                goto error;
1169
3
            }
1170
7.18k
        }
1171
1172
        /* sbr */
1173
1.54k
        if (hDecoder->sbr_present_flag == 1)
1174
1.20k
        {
1175
1.20k
            hInfo->object_type = HE_AAC;
1176
1.20k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.20k
        } else {
1178
338
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
338
        }
1180
1.54k
        if (hDecoder->downSampledSBR)
1181
444
        {
1182
444
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
444
        }
1184
1.54k
    }
1185
2.20k
#endif
1186
1187
1188
2.20k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
2.20k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
2.20k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
2.20k
#endif
1195
1196
1197
2.20k
    hDecoder->postSeekResetFlag = 0;
1198
1199
2.20k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
2.20k
        if (hDecoder->frame <= 1)
1205
883
            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.20k
    return sample_buffer;
1225
1226
5.77k
error:
1227
1228
    /* reset filterbank state */
1229
375k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
369k
    {
1231
369k
        if (hDecoder->fb_intermed[i] != NULL)
1232
137k
        {
1233
137k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
137k
        }
1235
369k
    }
1236
5.77k
#ifdef SBR_DEC
1237
282k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
277k
    {
1239
277k
        if (hDecoder->sbr[i] != NULL)
1240
105k
        {
1241
105k
            sbrReset(hDecoder->sbr[i]);
1242
105k
        }
1243
277k
    }
1244
5.77k
#endif
1245
1246
1247
5.77k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
2.21k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
23.6k
{
855
23.6k
    uint16_t i;
856
23.6k
    uint8_t channels = 0;
857
23.6k
    uint8_t output_channels = 0;
858
23.6k
    bitfile ld;
859
23.6k
    uint32_t bitsconsumed;
860
23.6k
    uint16_t frame_len;
861
23.6k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
23.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
23.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
23.6k
    frame_len = hDecoder->frameLength;
882
883
884
23.6k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
23.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
23.6k
    if (buffer_size >= 128)
903
2.75k
    {
904
2.75k
        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.75k
    }
912
913
914
    /* initialize the bitstream */
915
23.6k
    faad_initbits(&ld, buffer, buffer_size);
916
23.6k
    if (ld.error != 0)
917
9.24k
        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
14.4k
    if (hDecoder->adts_header_present)
966
616
    {
967
616
        adts_header adts;
968
969
616
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
616
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
75
            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
616
    }
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
14.3k
        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
14.3k
    channels = hDecoder->fr_channels;
1009
1010
14.3k
    if (hInfo->error > 0)
1011
9.66k
        goto error;
1012
1013
    /* safety check */
1014
4.68k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
56
    {
1016
        /* invalid number of channels */
1017
56
        hInfo->error = 12;
1018
56
        goto error;
1019
56
    }
1020
1021
    /* no more bit reading after this */
1022
4.62k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
4.62k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
4.62k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
4.62k
    faad_endbits(&ld);
1030
1031
1032
4.62k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
4.62k
        )
1037
4.46k
    {
1038
4.46k
        if (hDecoder->channelConfiguration == 0)
1039
135
            hDecoder->channelConfiguration = channels;
1040
1041
4.46k
        if (channels == 8) /* 7.1 */
1042
622
            hDecoder->channelConfiguration = 7;
1043
4.46k
        if (channels == 7) /* not a standard channelConfiguration */
1044
39
            hDecoder->channelConfiguration = 0;
1045
4.46k
    }
1046
1047
4.62k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
413
    {
1049
413
        hDecoder->downMatrix = 1;
1050
413
        output_channels = 2;
1051
4.21k
    } else {
1052
4.21k
        output_channels = channels;
1053
4.21k
    }
1054
1055
4.62k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
4.62k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
4.62k
    if (output_channels == 1)
1059
212
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
212
        hDecoder->upMatrix = 1;
1062
212
        output_channels = 2;
1063
212
    }
1064
4.62k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
4.62k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
158
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
158
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
158
    }
1076
4.62k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
4.62k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
4.62k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
4.62k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
4.62k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
4.62k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
4.62k
    hInfo->header_type = RAW;
1090
4.62k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
4.62k
    if (hDecoder->adts_header_present)
1093
160
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
4.62k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
4.62k
    hInfo->ps = hDecoder->ps_used_global;
1100
4.62k
#endif
1101
1102
    /* check if frame has channel elements */
1103
4.62k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
4.62k
    {
1110
4.62k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
4.62k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
4.62k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
4.62k
        };
1114
4.62k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
4.62k
#ifdef SBR_DEC
1116
4.62k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
2.60k
        {
1118
2.60k
            stride = 2 * stride;
1119
2.60k
        }
1120
4.62k
#endif
1121
4.62k
        required_buffer_size = frame_len*output_channels*stride;
1122
4.62k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
4.62k
    if (sample_buffer_size == 0)
1126
3.30k
    {
1127
        /* allocate the buffer for the final samples */
1128
3.30k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
2.88k
        {
1130
2.88k
            if (hDecoder->sample_buffer)
1131
51
                faad_free(hDecoder->sample_buffer);
1132
2.88k
            hDecoder->sample_buffer = NULL;
1133
2.88k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
2.88k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
2.88k
        }
1136
3.30k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
476
        hInfo->error = 27;
1139
476
        return NULL;
1140
476
    }
1141
1142
4.14k
    if (sample_buffer_size == 0)
1143
3.30k
    {
1144
3.30k
        sample_buffer = hDecoder->sample_buffer;
1145
3.30k
    } else {
1146
843
        sample_buffer = *sample_buffer2;
1147
843
    }
1148
1149
4.14k
#ifdef SBR_DEC
1150
4.14k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
2.84k
    {
1152
2.84k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
2.84k
        if (!hDecoder->downSampledSBR)
1156
2.26k
        {
1157
2.26k
            frame_len *= 2;
1158
2.26k
            hInfo->samples *= 2;
1159
2.26k
            hInfo->samplerate *= 2;
1160
2.26k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
15.9k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
13.0k
        {
1165
13.0k
            if (hDecoder->sbr[ele] == NULL)
1166
6
            {
1167
6
                hInfo->error = 25;
1168
6
                goto error;
1169
6
            }
1170
13.0k
        }
1171
1172
        /* sbr */
1173
2.83k
        if (hDecoder->sbr_present_flag == 1)
1174
1.64k
        {
1175
1.64k
            hInfo->object_type = HE_AAC;
1176
1.64k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.64k
        } else {
1178
1.19k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.19k
        }
1180
2.83k
        if (hDecoder->downSampledSBR)
1181
572
        {
1182
572
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
572
        }
1184
2.83k
    }
1185
4.13k
#endif
1186
1187
1188
4.13k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
4.13k
        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
4.13k
    hDecoder->postSeekResetFlag = 0;
1198
1199
4.13k
    hDecoder->frame++;
1200
4.13k
#ifdef LD_DEC
1201
4.13k
    if (hDecoder->object_type != LD)
1202
3.58k
    {
1203
3.58k
#endif
1204
3.58k
        if (hDecoder->frame <= 1)
1205
1.27k
            hInfo->samples = 0;
1206
3.58k
#ifdef LD_DEC
1207
3.58k
    } else {
1208
        /* LD encoders will give lower delay */
1209
552
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
552
    }
1212
4.13k
#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
4.13k
    return sample_buffer;
1225
1226
9.80k
error:
1227
1228
    /* reset filterbank state */
1229
637k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
627k
    {
1231
627k
        if (hDecoder->fb_intermed[i] != NULL)
1232
342k
        {
1233
342k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
342k
        }
1235
627k
    }
1236
9.80k
#ifdef SBR_DEC
1237
480k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
470k
    {
1239
470k
        if (hDecoder->sbr[i] != NULL)
1240
241k
        {
1241
241k
            sbrReset(hDecoder->sbr[i]);
1242
241k
        }
1243
470k
    }
1244
9.80k
#endif
1245
1246
1247
9.80k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
4.14k
}