Coverage Report

Created: 2026-05-30 06:09

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
336
{
74
336
    static char *libfaadName = PACKAGE_VERSION;
75
336
    static char *libCopyright =
76
336
        " Copyright 2002-2004: Ahead Software AG\n"
77
336
  " http://www.audiocoding.com\n"
78
336
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
336
    if (faad_id_string)
81
336
        *faad_id_string = libfaadName;
82
83
336
    if (faad_copyright_string)
84
336
        *faad_copyright_string = libCopyright;
85
86
336
    return 0;
87
336
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
336
{
91
336
    if (errcode >= NUM_ERROR_MESSAGES)
92
200
        return NULL;
93
136
    return err_msg[errcode];
94
336
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.34k
{
98
1.34k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.34k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
336
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
672
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
672
    cap += LD_DEC_CAP;
111
#endif
112
1.34k
#ifdef ERROR_RESILIENCE
113
1.34k
    cap += ERROR_RESILIENCE_CAP;
114
1.34k
#endif
115
#ifdef FIXED_POINT
116
672
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.34k
    return cap;
120
1.34k
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    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
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
336
#ifdef FIXED_POINT
116
336
    cap += FIXED_POINT_CAP;
117
336
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
336
#ifdef MAIN_DEC
104
336
    cap += MAIN_DEC_CAP;
105
336
#endif
106
336
#ifdef LTP_DEC
107
336
    cap += LTP_DEC_CAP;
108
336
#endif
109
336
#ifdef LD_DEC
110
336
    cap += LD_DEC_CAP;
111
336
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
336
#ifdef LTP_DEC
107
336
    cap += LTP_DEC_CAP;
108
336
#endif
109
336
#ifdef LD_DEC
110
336
    cap += LD_DEC_CAP;
111
336
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
336
#ifdef FIXED_POINT
116
336
    cap += FIXED_POINT_CAP;
117
336
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    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
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
336
    return cap;
120
336
}
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
55.2k
{
125
55.2k
    uint8_t i;
126
55.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
55.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
55.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
55.2k
    hDecoder->cmes = mes;
134
55.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
55.2k
    hDecoder->config.defObjectType = MAIN;
136
55.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
55.2k
    hDecoder->config.downMatrix = 0;
138
55.2k
    hDecoder->adts_header_present = 0;
139
55.2k
    hDecoder->adif_header_present = 0;
140
55.2k
    hDecoder->latm_header_present = 0;
141
55.2k
#ifdef ERROR_RESILIENCE
142
55.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
55.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
55.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
55.2k
#endif
146
55.2k
    hDecoder->frameLength = 1024;
147
148
55.2k
    hDecoder->frame = 0;
149
55.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
55.2k
    hDecoder->__r1 = 0x2bb431ea;
153
55.2k
    hDecoder->__r2 = 0x206155b7;
154
155
3.59M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.53M
    {
157
3.53M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.53M
        hDecoder->window_shape_prev[i] = 0;
159
3.53M
        hDecoder->time_out[i] = NULL;
160
3.53M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
3.53M
    }
173
174
55.2k
#ifdef SBR_DEC
175
2.70M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.65M
    {
177
2.65M
        hDecoder->sbr[i] = NULL;
178
2.65M
    }
179
55.2k
#endif
180
181
55.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
55.2k
    return hDecoder;
184
55.2k
}
NeAACDecOpen
Line
Count
Source
124
10.0k
{
125
10.0k
    uint8_t i;
126
10.0k
    NeAACDecStruct *hDecoder = NULL;
127
128
10.0k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
10.0k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
10.0k
    hDecoder->cmes = mes;
134
10.0k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
10.0k
    hDecoder->config.defObjectType = MAIN;
136
10.0k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
10.0k
    hDecoder->config.downMatrix = 0;
138
10.0k
    hDecoder->adts_header_present = 0;
139
10.0k
    hDecoder->adif_header_present = 0;
140
10.0k
    hDecoder->latm_header_present = 0;
141
10.0k
#ifdef ERROR_RESILIENCE
142
10.0k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
10.0k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
10.0k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
10.0k
#endif
146
10.0k
    hDecoder->frameLength = 1024;
147
148
10.0k
    hDecoder->frame = 0;
149
10.0k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
10.0k
    hDecoder->__r1 = 0x2bb431ea;
153
10.0k
    hDecoder->__r2 = 0x206155b7;
154
155
653k
    for (i = 0; i < MAX_CHANNELS; i++)
156
643k
    {
157
643k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
643k
        hDecoder->window_shape_prev[i] = 0;
159
643k
        hDecoder->time_out[i] = NULL;
160
643k
        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
643k
    }
173
174
10.0k
#ifdef SBR_DEC
175
492k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
482k
    {
177
482k
        hDecoder->sbr[i] = NULL;
178
482k
    }
179
10.0k
#endif
180
181
10.0k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
10.0k
    return hDecoder;
184
10.0k
}
NeAACDecOpen
Line
Count
Source
124
17.5k
{
125
17.5k
    uint8_t i;
126
17.5k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.5k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.5k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.5k
    hDecoder->cmes = mes;
134
17.5k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.5k
    hDecoder->config.defObjectType = MAIN;
136
17.5k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.5k
    hDecoder->config.downMatrix = 0;
138
17.5k
    hDecoder->adts_header_present = 0;
139
17.5k
    hDecoder->adif_header_present = 0;
140
17.5k
    hDecoder->latm_header_present = 0;
141
17.5k
#ifdef ERROR_RESILIENCE
142
17.5k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.5k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.5k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.5k
#endif
146
17.5k
    hDecoder->frameLength = 1024;
147
148
17.5k
    hDecoder->frame = 0;
149
17.5k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.5k
    hDecoder->__r1 = 0x2bb431ea;
153
17.5k
    hDecoder->__r2 = 0x206155b7;
154
155
1.14M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.12M
    {
157
1.12M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.12M
        hDecoder->window_shape_prev[i] = 0;
159
1.12M
        hDecoder->time_out[i] = NULL;
160
1.12M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
1.12M
#ifdef MAIN_DEC
166
1.12M
        hDecoder->pred_stat[i] = NULL;
167
1.12M
#endif
168
1.12M
#ifdef LTP_DEC
169
1.12M
        hDecoder->ltp_lag[i] = 0;
170
1.12M
        hDecoder->lt_pred_stat[i] = NULL;
171
1.12M
#endif
172
1.12M
    }
173
174
17.5k
#ifdef SBR_DEC
175
860k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
843k
    {
177
843k
        hDecoder->sbr[i] = NULL;
178
843k
    }
179
17.5k
#endif
180
181
17.5k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.5k
    return hDecoder;
184
17.5k
}
NeAACDecOpen
Line
Count
Source
124
10.0k
{
125
10.0k
    uint8_t i;
126
10.0k
    NeAACDecStruct *hDecoder = NULL;
127
128
10.0k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
10.0k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
10.0k
    hDecoder->cmes = mes;
134
10.0k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
10.0k
    hDecoder->config.defObjectType = MAIN;
136
10.0k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
10.0k
    hDecoder->config.downMatrix = 0;
138
10.0k
    hDecoder->adts_header_present = 0;
139
10.0k
    hDecoder->adif_header_present = 0;
140
10.0k
    hDecoder->latm_header_present = 0;
141
10.0k
#ifdef ERROR_RESILIENCE
142
10.0k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
10.0k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
10.0k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
10.0k
#endif
146
10.0k
    hDecoder->frameLength = 1024;
147
148
10.0k
    hDecoder->frame = 0;
149
10.0k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
10.0k
    hDecoder->__r1 = 0x2bb431ea;
153
10.0k
    hDecoder->__r2 = 0x206155b7;
154
155
653k
    for (i = 0; i < MAX_CHANNELS; i++)
156
643k
    {
157
643k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
643k
        hDecoder->window_shape_prev[i] = 0;
159
643k
        hDecoder->time_out[i] = NULL;
160
643k
        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
643k
#ifdef LTP_DEC
169
643k
        hDecoder->ltp_lag[i] = 0;
170
643k
        hDecoder->lt_pred_stat[i] = NULL;
171
643k
#endif
172
643k
    }
173
174
10.0k
#ifdef SBR_DEC
175
492k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
482k
    {
177
482k
        hDecoder->sbr[i] = NULL;
178
482k
    }
179
10.0k
#endif
180
181
10.0k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
10.0k
    return hDecoder;
184
10.0k
}
NeAACDecOpen
Line
Count
Source
124
17.5k
{
125
17.5k
    uint8_t i;
126
17.5k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.5k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.5k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.5k
    hDecoder->cmes = mes;
134
17.5k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.5k
    hDecoder->config.defObjectType = MAIN;
136
17.5k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.5k
    hDecoder->config.downMatrix = 0;
138
17.5k
    hDecoder->adts_header_present = 0;
139
17.5k
    hDecoder->adif_header_present = 0;
140
17.5k
    hDecoder->latm_header_present = 0;
141
17.5k
#ifdef ERROR_RESILIENCE
142
17.5k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.5k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.5k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.5k
#endif
146
17.5k
    hDecoder->frameLength = 1024;
147
148
17.5k
    hDecoder->frame = 0;
149
17.5k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.5k
    hDecoder->__r1 = 0x2bb431ea;
153
17.5k
    hDecoder->__r2 = 0x206155b7;
154
155
1.14M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.12M
    {
157
1.12M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.12M
        hDecoder->window_shape_prev[i] = 0;
159
1.12M
        hDecoder->time_out[i] = NULL;
160
1.12M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
1.12M
    }
173
174
17.5k
#ifdef SBR_DEC
175
860k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
843k
    {
177
843k
        hDecoder->sbr[i] = NULL;
178
843k
    }
179
17.5k
#endif
180
181
17.5k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.5k
    return hDecoder;
184
17.5k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
26.5k
{
188
26.5k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
26.5k
    if (hDecoder)
190
26.5k
    {
191
26.5k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
26.5k
        return config;
194
26.5k
    }
195
196
0
    return NULL;
197
26.5k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
26.8k
{
202
26.8k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
26.8k
    if (hDecoder && config)
204
26.8k
    {
205
        /* check if we can decode this object type */
206
26.8k
        if (can_decode_ot(config->defObjectType) < 0)
207
11
            return 0;
208
26.8k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
26.8k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
26.8k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
26.8k
#ifdef FIXED_POINT
217
26.8k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
179
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
26.6k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
26.6k
        if (config->downMatrix > 1)
226
30
            return 0;
227
26.5k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
26.5k
        return 1;
231
26.6k
    }
232
233
0
    return 0;
234
26.8k
}
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
18.2k
{
269
18.2k
    uint32_t bits = 0;
270
18.2k
    bitfile ld;
271
18.2k
    adif_header adif;
272
18.2k
    adts_header adts;
273
18.2k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
18.2k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
46
        return -1;
278
279
18.1k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
18.1k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
18.1k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
18.1k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
18.1k
    *channels = 1;
284
285
18.1k
    if (buffer != NULL)
286
18.1k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
18.1k
        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
18.1k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
463
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
415
        {
314
415
            hDecoder->adif_header_present = 1;
315
316
415
            get_adif_header(&adif, &ld);
317
415
            faad_byte_align(&ld);
318
319
415
            hDecoder->sf_index = adif.pce[0].sf_index;
320
415
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
415
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
415
            *channels = adif.pce[0].channels;
324
325
415
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
415
            hDecoder->pce_set = 1;
327
328
415
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
17.7k
        } else if (adts_frame(&adts, &ld) == 0) {
332
604
            hDecoder->adts_header_present = 1;
333
334
604
            hDecoder->sf_index = adts.sf_index;
335
604
            hDecoder->object_type = adts.profile + 1;
336
337
604
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
604
            *channels = (adts.channel_configuration > 6) ?
339
552
                2 : adts.channel_configuration;
340
604
        }
341
342
18.1k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
18.1k
        faad_endbits(&ld);
348
18.1k
    }
349
350
18.1k
    if (!*samplerate)
351
188
        return -1;
352
353
17.9k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
17.9k
    if (*channels == 1)
356
17.1k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
17.1k
        *channels = 2;
359
17.1k
    }
360
17.9k
#endif
361
362
17.9k
    hDecoder->channelConfiguration = *channels;
363
364
17.9k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
17.9k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
8.43k
    {
368
8.43k
        *samplerate *= 2;
369
8.43k
        hDecoder->forceUpSampling = 1;
370
9.56k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
9.56k
        hDecoder->downSampledSBR = 1;
372
9.56k
    }
373
17.9k
#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
17.9k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
11.6k
    if (hDecoder->object_type == LD)
385
517
        hDecoder->frameLength >>= 1;
386
#endif
387
388
17.9k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
193
        return -1;
390
391
17.8k
    return bits;
392
17.9k
}
NeAACDecInit
Line
Count
Source
268
6.46k
{
269
6.46k
    uint32_t bits = 0;
270
6.46k
    bitfile ld;
271
6.46k
    adif_header adif;
272
6.46k
    adts_header adts;
273
6.46k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
6.46k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
21
        return -1;
278
279
6.44k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
6.44k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
6.44k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
6.44k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
6.44k
    *channels = 1;
284
285
6.44k
    if (buffer != NULL)
286
6.44k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
6.44k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
6.44k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
186
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
169
        {
314
169
            hDecoder->adif_header_present = 1;
315
316
169
            get_adif_header(&adif, &ld);
317
169
            faad_byte_align(&ld);
318
319
169
            hDecoder->sf_index = adif.pce[0].sf_index;
320
169
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
169
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
169
            *channels = adif.pce[0].channels;
324
325
169
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
169
            hDecoder->pce_set = 1;
327
328
169
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
6.27k
        } else if (adts_frame(&adts, &ld) == 0) {
332
108
            hDecoder->adts_header_present = 1;
333
334
108
            hDecoder->sf_index = adts.sf_index;
335
108
            hDecoder->object_type = adts.profile + 1;
336
337
108
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
108
            *channels = (adts.channel_configuration > 6) ?
339
96
                2 : adts.channel_configuration;
340
108
        }
341
342
6.44k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
6.44k
        faad_endbits(&ld);
348
6.44k
    }
349
350
6.44k
    if (!*samplerate)
351
91
        return -1;
352
353
6.34k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
6.34k
    if (*channels == 1)
356
6.16k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
6.16k
        *channels = 2;
359
6.16k
    }
360
6.34k
#endif
361
362
6.34k
    hDecoder->channelConfiguration = *channels;
363
364
6.34k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
6.34k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
2.99k
    {
368
2.99k
        *samplerate *= 2;
369
2.99k
        hDecoder->forceUpSampling = 1;
370
3.35k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
3.35k
        hDecoder->downSampledSBR = 1;
372
3.35k
    }
373
6.34k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
6.34k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
    if (hDecoder->object_type == LD)
385
        hDecoder->frameLength >>= 1;
386
#endif
387
388
6.34k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
99
        return -1;
390
391
6.25k
    return bits;
392
6.34k
}
NeAACDecInit
Line
Count
Source
268
11.7k
{
269
11.7k
    uint32_t bits = 0;
270
11.7k
    bitfile ld;
271
11.7k
    adif_header adif;
272
11.7k
    adts_header adts;
273
11.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
11.7k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
25
        return -1;
278
279
11.7k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
11.7k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
11.7k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
11.7k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
11.7k
    *channels = 1;
284
285
11.7k
    if (buffer != NULL)
286
11.7k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
11.7k
        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
11.7k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
277
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
246
        {
314
246
            hDecoder->adif_header_present = 1;
315
316
246
            get_adif_header(&adif, &ld);
317
246
            faad_byte_align(&ld);
318
319
246
            hDecoder->sf_index = adif.pce[0].sf_index;
320
246
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
246
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
246
            *channels = adif.pce[0].channels;
324
325
246
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
246
            hDecoder->pce_set = 1;
327
328
246
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
11.4k
        } else if (adts_frame(&adts, &ld) == 0) {
332
496
            hDecoder->adts_header_present = 1;
333
334
496
            hDecoder->sf_index = adts.sf_index;
335
496
            hDecoder->object_type = adts.profile + 1;
336
337
496
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
496
            *channels = (adts.channel_configuration > 6) ?
339
456
                2 : adts.channel_configuration;
340
496
        }
341
342
11.7k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
11.7k
        faad_endbits(&ld);
348
11.7k
    }
349
350
11.7k
    if (!*samplerate)
351
97
        return -1;
352
353
11.6k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
11.6k
    if (*channels == 1)
356
11.0k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
11.0k
        *channels = 2;
359
11.0k
    }
360
11.6k
#endif
361
362
11.6k
    hDecoder->channelConfiguration = *channels;
363
364
11.6k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
11.6k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
5.43k
    {
368
5.43k
        *samplerate *= 2;
369
5.43k
        hDecoder->forceUpSampling = 1;
370
6.20k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
6.20k
        hDecoder->downSampledSBR = 1;
372
6.20k
    }
373
11.6k
#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
11.6k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
11.6k
#ifdef LD_DEC
384
11.6k
    if (hDecoder->object_type == LD)
385
517
        hDecoder->frameLength >>= 1;
386
11.6k
#endif
387
388
11.6k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
94
        return -1;
390
391
11.5k
    return bits;
392
11.6k
}
393
394
/* Init the library using a DecoderSpecificInfo */
395
char NeAACDecInit2(NeAACDecHandle hpDecoder,
396
                               unsigned char *pBuffer,
397
                               unsigned long SizeOfDecoderSpecificInfo,
398
                               unsigned long *samplerate,
399
                               unsigned char *channels)
400
8.37k
{
401
8.37k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
8.37k
    int8_t rc;
403
8.37k
    mp4AudioSpecificConfig mp4ASC;
404
405
8.37k
    if((hDecoder == NULL)
406
8.37k
        || (pBuffer == NULL)
407
8.37k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.34k
        || (samplerate == NULL)
409
8.34k
        || (channels == NULL))
410
24
    {
411
24
        return -1;
412
24
    }
413
414
8.34k
    hDecoder->adif_header_present = 0;
415
8.34k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.34k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.34k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.34k
    *samplerate = mp4ASC.samplingFrequency;
423
8.34k
    if (mp4ASC.channelsConfiguration)
424
7.04k
    {
425
7.04k
        *channels = mp4ASC.channelsConfiguration;
426
7.04k
    } else {
427
1.29k
        *channels = hDecoder->pce.channels;
428
1.29k
        hDecoder->pce_set = 1;
429
1.29k
    }
430
8.34k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.34k
    if (*channels == 1)
433
26
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
26
        *channels = 2;
436
26
    }
437
8.34k
#endif
438
8.34k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.34k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.34k
#ifdef ERROR_RESILIENCE
441
8.34k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.34k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.34k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.34k
#endif
445
8.34k
#ifdef SBR_DEC
446
8.34k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.34k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.34k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.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
8.34k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
5.13k
    {
456
5.13k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
5.13k
    }
458
8.34k
#endif
459
460
8.34k
    if (rc != 0)
461
99
    {
462
99
        return rc;
463
99
    }
464
8.24k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.24k
    if (mp4ASC.frameLengthFlag)
466
5.08k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
5.08k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
8.24k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
5.45k
    if (hDecoder->object_type == LD)
482
654
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.24k
    return 0;
486
8.34k
}
NeAACDecInit2
Line
Count
Source
400
2.83k
{
401
2.83k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
2.83k
    int8_t rc;
403
2.83k
    mp4AudioSpecificConfig mp4ASC;
404
405
2.83k
    if((hDecoder == NULL)
406
2.83k
        || (pBuffer == NULL)
407
2.83k
        || (SizeOfDecoderSpecificInfo < 2)
408
2.82k
        || (samplerate == NULL)
409
2.82k
        || (channels == NULL))
410
7
    {
411
7
        return -1;
412
7
    }
413
414
2.82k
    hDecoder->adif_header_present = 0;
415
2.82k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
2.82k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
2.82k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
2.82k
    *samplerate = mp4ASC.samplingFrequency;
423
2.82k
    if (mp4ASC.channelsConfiguration)
424
2.28k
    {
425
2.28k
        *channels = mp4ASC.channelsConfiguration;
426
2.28k
    } else {
427
541
        *channels = hDecoder->pce.channels;
428
541
        hDecoder->pce_set = 1;
429
541
    }
430
2.82k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
2.82k
    if (*channels == 1)
433
13
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
13
        *channels = 2;
436
13
    }
437
2.82k
#endif
438
2.82k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
2.82k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
2.82k
#ifdef ERROR_RESILIENCE
441
2.82k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
2.82k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
2.82k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
2.82k
#endif
445
2.82k
#ifdef SBR_DEC
446
2.82k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
2.82k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
2.82k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
2.82k
        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.82k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
1.62k
    {
456
1.62k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
1.62k
    }
458
2.82k
#endif
459
460
2.82k
    if (rc != 0)
461
33
    {
462
33
        return rc;
463
33
    }
464
2.79k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
2.79k
    if (mp4ASC.frameLengthFlag)
466
1.87k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
1.87k
        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.79k
        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.79k
    return 0;
486
2.82k
}
NeAACDecInit2
Line
Count
Source
400
5.54k
{
401
5.54k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
5.54k
    int8_t rc;
403
5.54k
    mp4AudioSpecificConfig mp4ASC;
404
405
5.54k
    if((hDecoder == NULL)
406
5.54k
        || (pBuffer == NULL)
407
5.54k
        || (SizeOfDecoderSpecificInfo < 2)
408
5.52k
        || (samplerate == NULL)
409
5.52k
        || (channels == NULL))
410
17
    {
411
17
        return -1;
412
17
    }
413
414
5.52k
    hDecoder->adif_header_present = 0;
415
5.52k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
5.52k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
5.52k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
5.52k
    *samplerate = mp4ASC.samplingFrequency;
423
5.52k
    if (mp4ASC.channelsConfiguration)
424
4.76k
    {
425
4.76k
        *channels = mp4ASC.channelsConfiguration;
426
4.76k
    } else {
427
758
        *channels = hDecoder->pce.channels;
428
758
        hDecoder->pce_set = 1;
429
758
    }
430
5.52k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
5.52k
    if (*channels == 1)
433
13
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
13
        *channels = 2;
436
13
    }
437
5.52k
#endif
438
5.52k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
5.52k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
5.52k
#ifdef ERROR_RESILIENCE
441
5.52k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
5.52k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
5.52k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
5.52k
#endif
445
5.52k
#ifdef SBR_DEC
446
5.52k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
5.52k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
5.52k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
5.52k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
5.52k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
3.51k
    {
456
3.51k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
3.51k
    }
458
5.52k
#endif
459
460
5.52k
    if (rc != 0)
461
66
    {
462
66
        return rc;
463
66
    }
464
5.45k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
5.45k
    if (mp4ASC.frameLengthFlag)
466
3.20k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.20k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
5.45k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
5.45k
#ifdef LD_DEC
481
5.45k
    if (hDecoder->object_type == LD)
482
654
        hDecoder->frameLength >>= 1;
483
5.45k
#endif
484
485
5.45k
    return 0;
486
5.52k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
796
{
493
796
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
796
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
796
    NeAACDecClose(*hDecoder);
498
499
796
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
796
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
796
    (*hDecoder)->config.defSampleRate = samplerate;
505
796
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
796
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
796
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
796
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
796
#endif
510
796
    (*hDecoder)->frameLength = 960;
511
796
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
796
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
796
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
125
        (*hDecoder)->channelConfiguration = 2;
516
671
    else
517
671
        (*hDecoder)->channelConfiguration = 1;
518
519
796
#ifdef SBR_DEC
520
796
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
105
        (*hDecoder)->sbr_present_flag = 0;
522
691
    else
523
691
        (*hDecoder)->sbr_present_flag = 1;
524
796
#endif
525
526
796
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
796
    return 0;
529
796
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
10.1k
{
534
10.1k
    uint8_t i;
535
10.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
10.1k
    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
660k
    for (i = 0; i < MAX_CHANNELS; i++)
549
650k
    {
550
650k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
650k
        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
650k
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
10.1k
        filter_bank_end(hDecoder->fb);
570
571
10.1k
    drc_end(hDecoder->drc);
572
573
10.1k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
10.1k
#ifdef SBR_DEC
576
498k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
487k
    {
578
487k
        if (hDecoder->sbr[i])
579
117k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
487k
    }
581
10.1k
#endif
582
583
10.1k
    if (hDecoder) faad_free(hDecoder);
584
10.1k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
20.5k
{
588
20.5k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
20.5k
    if (hDecoder)
590
20.5k
    {
591
20.5k
        hDecoder->postSeekResetFlag = 1;
592
593
20.5k
        if (frame != -1)
594
20.5k
            hDecoder->frame = frame;
595
20.5k
    }
596
20.5k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
40.9k
{
600
40.9k
    hInfo->num_front_channels = 0;
601
40.9k
    hInfo->num_side_channels = 0;
602
40.9k
    hInfo->num_back_channels = 0;
603
40.9k
    hInfo->num_lfe_channels = 0;
604
40.9k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
40.9k
    if (hDecoder->downMatrix)
607
4.39k
    {
608
4.39k
        hInfo->num_front_channels = 2;
609
4.39k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.39k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.39k
        return;
612
4.39k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
36.5k
    if (hDecoder->pce_set)
617
1.53k
    {
618
1.53k
        uint8_t i, chpos = 0;
619
1.53k
        uint8_t chdir, back_center = 0;
620
621
1.53k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.53k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.53k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.53k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.53k
        chdir = hInfo->num_front_channels;
626
1.53k
        if (chdir & 1)
627
612
        {
628
612
#if (defined(PS_DEC) || defined(DRM_PS))
629
612
            if (hInfo->num_front_channels == 1 &&
630
252
                hInfo->num_side_channels == 0 &&
631
144
                hInfo->num_back_channels == 0 &&
632
96
                hInfo->num_lfe_channels == 0)
633
60
            {
634
                /* When PS is enabled output is always stereo */
635
60
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
60
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
60
            } else
638
552
#endif
639
552
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
612
            chdir--;
641
612
        }
642
8.04k
        for (i = 0; i < chdir; i++)
643
6.51k
        {
644
6.51k
            hInfo->channel_position[chpos++] =
645
6.51k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
6.51k
        }
647
648
7.41k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
5.88k
        {
650
5.88k
            hInfo->channel_position[chpos++] =
651
5.88k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
5.88k
        }
653
654
1.53k
        chdir = hInfo->num_back_channels;
655
1.53k
        if (chdir & 1)
656
220
        {
657
220
            back_center = 1;
658
220
            chdir--;
659
220
        }
660
5.30k
        for (i = 0; i < chdir; i++)
661
3.77k
        {
662
3.77k
            hInfo->channel_position[chpos++] =
663
3.77k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.77k
        }
665
1.53k
        if (back_center)
666
220
        {
667
220
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
220
        }
669
670
2.47k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
944
        {
672
944
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
944
        }
674
675
35.0k
    } else {
676
35.0k
        switch (hDecoder->channelConfiguration)
677
35.0k
        {
678
1.61k
        case 1:
679
1.61k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
1.61k
            hInfo->num_front_channels = 2;
682
1.61k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
1.61k
            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.61k
            break;
689
16.0k
        case 2:
690
16.0k
            hInfo->num_front_channels = 2;
691
16.0k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
16.0k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
16.0k
            break;
694
2.36k
        case 3:
695
2.36k
            hInfo->num_front_channels = 3;
696
2.36k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
2.36k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
2.36k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
2.36k
            break;
700
4.58k
        case 4:
701
4.58k
            hInfo->num_front_channels = 3;
702
4.58k
            hInfo->num_back_channels = 1;
703
4.58k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.58k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.58k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.58k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.58k
            break;
708
2.68k
        case 5:
709
2.68k
            hInfo->num_front_channels = 3;
710
2.68k
            hInfo->num_back_channels = 2;
711
2.68k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.68k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.68k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.68k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.68k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.68k
            break;
717
1.34k
        case 6:
718
1.34k
            hInfo->num_front_channels = 3;
719
1.34k
            hInfo->num_back_channels = 2;
720
1.34k
            hInfo->num_lfe_channels = 1;
721
1.34k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.34k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.34k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.34k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.34k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.34k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.34k
            break;
728
5.56k
        case 7:
729
5.56k
            hInfo->num_front_channels = 3;
730
5.56k
            hInfo->num_side_channels = 2;
731
5.56k
            hInfo->num_back_channels = 2;
732
5.56k
            hInfo->num_lfe_channels = 1;
733
5.56k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
5.56k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
5.56k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
5.56k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
5.56k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
5.56k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
5.56k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
5.56k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
5.56k
            break;
742
864
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
864
            {
744
864
                uint8_t i;
745
864
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
864
                if (ch & 1) /* there's either a center front or a center back channel */
747
312
                {
748
312
                    uint8_t ch1 = (ch-1)/2;
749
312
                    if (hDecoder->first_syn_ele == ID_SCE)
750
204
                    {
751
204
                        hInfo->num_front_channels = ch1 + 1;
752
204
                        hInfo->num_back_channels = ch1;
753
204
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
976
                        for (i = 1; i <= ch1; i+=2)
755
772
                        {
756
772
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
772
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
772
                        }
759
976
                        for (i = ch1+1; i < ch; i+=2)
760
772
                        {
761
772
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
772
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
772
                        }
764
204
                    } else {
765
108
                        hInfo->num_front_channels = ch1;
766
108
                        hInfo->num_back_channels = ch1 + 1;
767
736
                        for (i = 0; i < ch1; i+=2)
768
628
                        {
769
628
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
628
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
628
                        }
772
736
                        for (i = ch1; i < ch-1; i+=2)
773
628
                        {
774
628
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
628
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
628
                        }
777
108
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
108
                    }
779
552
                } else {
780
552
                    uint8_t ch1 = (ch)/2;
781
552
                    hInfo->num_front_channels = ch1;
782
552
                    hInfo->num_back_channels = ch1;
783
552
                    if (ch1 & 1)
784
264
                    {
785
264
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.59k
                        for (i = 1; i <= ch1; i+=2)
787
1.33k
                        {
788
1.33k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.33k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.33k
                        }
791
1.33k
                        for (i = ch1+1; i < ch-1; i+=2)
792
1.06k
                        {
793
1.06k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
1.06k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
1.06k
                        }
796
264
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
288
                    } else {
798
2.02k
                        for (i = 0; i < ch1; i+=2)
799
1.73k
                        {
800
1.73k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
1.73k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
1.73k
                        }
803
2.02k
                        for (i = ch1; i < ch; i+=2)
804
1.73k
                        {
805
1.73k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
1.73k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
1.73k
                        }
808
288
                    }
809
552
                }
810
864
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
2.02k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
1.15k
                {
813
1.15k
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
1.15k
                }
815
864
            }
816
864
            break;
817
35.0k
        }
818
35.0k
    }
819
36.5k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.14k
    if (hDecoder->pce_set)
617
383
    {
618
383
        uint8_t i, chpos = 0;
619
383
        uint8_t chdir, back_center = 0;
620
621
383
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
383
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
383
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
383
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
383
        chdir = hInfo->num_front_channels;
626
383
        if (chdir & 1)
627
153
        {
628
153
#if (defined(PS_DEC) || defined(DRM_PS))
629
153
            if (hInfo->num_front_channels == 1 &&
630
63
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
24
                hInfo->num_lfe_channels == 0)
633
15
            {
634
                /* When PS is enabled output is always stereo */
635
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
15
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
153
            chdir--;
641
153
        }
642
2.01k
        for (i = 0; i < chdir; i++)
643
1.62k
        {
644
1.62k
            hInfo->channel_position[chpos++] =
645
1.62k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.62k
        }
647
648
1.85k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
383
        chdir = hInfo->num_back_channels;
655
383
        if (chdir & 1)
656
55
        {
657
55
            back_center = 1;
658
55
            chdir--;
659
55
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
944
        {
662
944
            hInfo->channel_position[chpos++] =
663
944
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
944
        }
665
383
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
619
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
236
        {
672
236
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
236
        }
674
675
8.76k
    } else {
676
8.76k
        switch (hDecoder->channelConfiguration)
677
8.76k
        {
678
403
        case 1:
679
403
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
403
            hInfo->num_front_channels = 2;
682
403
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
403
            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
403
            break;
689
4.00k
        case 2:
690
4.00k
            hInfo->num_front_channels = 2;
691
4.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.00k
            break;
694
590
        case 3:
695
590
            hInfo->num_front_channels = 3;
696
590
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
590
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
590
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
590
            break;
700
1.14k
        case 4:
701
1.14k
            hInfo->num_front_channels = 3;
702
1.14k
            hInfo->num_back_channels = 1;
703
1.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.14k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.14k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.14k
            break;
708
672
        case 5:
709
672
            hInfo->num_front_channels = 3;
710
672
            hInfo->num_back_channels = 2;
711
672
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
672
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
672
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
672
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
672
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
672
            break;
717
336
        case 6:
718
336
            hInfo->num_front_channels = 3;
719
336
            hInfo->num_back_channels = 2;
720
336
            hInfo->num_lfe_channels = 1;
721
336
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
336
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
336
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
336
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
336
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
336
            hInfo->channel_position[5] = LFE_CHANNEL;
727
336
            break;
728
1.39k
        case 7:
729
1.39k
            hInfo->num_front_channels = 3;
730
1.39k
            hInfo->num_side_channels = 2;
731
1.39k
            hInfo->num_back_channels = 2;
732
1.39k
            hInfo->num_lfe_channels = 1;
733
1.39k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.39k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.39k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.39k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.39k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.39k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.39k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.39k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.39k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
78
                {
748
78
                    uint8_t ch1 = (ch-1)/2;
749
78
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
244
                        for (i = 1; i <= ch1; i+=2)
755
193
                        {
756
193
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
193
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
193
                        }
759
244
                        for (i = ch1+1; i < ch; i+=2)
760
193
                        {
761
193
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
193
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
193
                        }
764
51
                    } else {
765
27
                        hInfo->num_front_channels = ch1;
766
27
                        hInfo->num_back_channels = ch1 + 1;
767
184
                        for (i = 0; i < ch1; i+=2)
768
157
                        {
769
157
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
157
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
157
                        }
772
184
                        for (i = ch1; i < ch-1; i+=2)
773
157
                        {
774
157
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
157
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
157
                        }
777
27
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
27
                    }
779
138
                } else {
780
138
                    uint8_t ch1 = (ch)/2;
781
138
                    hInfo->num_front_channels = ch1;
782
138
                    hInfo->num_back_channels = ch1;
783
138
                    if (ch1 & 1)
784
66
                    {
785
66
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
399
                        for (i = 1; i <= ch1; i+=2)
787
333
                        {
788
333
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
333
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
333
                        }
791
333
                        for (i = ch1+1; i < ch-1; i+=2)
792
267
                        {
793
267
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
267
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
267
                        }
796
66
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
505
                        for (i = 0; i < ch1; i+=2)
799
433
                        {
800
433
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
433
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
433
                        }
803
505
                        for (i = ch1; i < ch; i+=2)
804
433
                        {
805
433
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
433
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
433
                        }
808
72
                    }
809
138
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
505
                for (i = ch; i < hDecoder->fr_channels; i++)
812
289
                {
813
289
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
289
                }
815
216
            }
816
216
            break;
817
8.76k
        }
818
8.76k
    }
819
9.14k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.14k
    if (hDecoder->pce_set)
617
383
    {
618
383
        uint8_t i, chpos = 0;
619
383
        uint8_t chdir, back_center = 0;
620
621
383
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
383
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
383
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
383
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
383
        chdir = hInfo->num_front_channels;
626
383
        if (chdir & 1)
627
153
        {
628
153
#if (defined(PS_DEC) || defined(DRM_PS))
629
153
            if (hInfo->num_front_channels == 1 &&
630
63
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
24
                hInfo->num_lfe_channels == 0)
633
15
            {
634
                /* When PS is enabled output is always stereo */
635
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
15
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
153
            chdir--;
641
153
        }
642
2.01k
        for (i = 0; i < chdir; i++)
643
1.62k
        {
644
1.62k
            hInfo->channel_position[chpos++] =
645
1.62k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.62k
        }
647
648
1.85k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
383
        chdir = hInfo->num_back_channels;
655
383
        if (chdir & 1)
656
55
        {
657
55
            back_center = 1;
658
55
            chdir--;
659
55
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
944
        {
662
944
            hInfo->channel_position[chpos++] =
663
944
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
944
        }
665
383
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
619
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
236
        {
672
236
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
236
        }
674
675
8.76k
    } else {
676
8.76k
        switch (hDecoder->channelConfiguration)
677
8.76k
        {
678
403
        case 1:
679
403
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
403
            hInfo->num_front_channels = 2;
682
403
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
403
            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
403
            break;
689
4.00k
        case 2:
690
4.00k
            hInfo->num_front_channels = 2;
691
4.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.00k
            break;
694
590
        case 3:
695
590
            hInfo->num_front_channels = 3;
696
590
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
590
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
590
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
590
            break;
700
1.14k
        case 4:
701
1.14k
            hInfo->num_front_channels = 3;
702
1.14k
            hInfo->num_back_channels = 1;
703
1.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.14k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.14k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.14k
            break;
708
672
        case 5:
709
672
            hInfo->num_front_channels = 3;
710
672
            hInfo->num_back_channels = 2;
711
672
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
672
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
672
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
672
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
672
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
672
            break;
717
336
        case 6:
718
336
            hInfo->num_front_channels = 3;
719
336
            hInfo->num_back_channels = 2;
720
336
            hInfo->num_lfe_channels = 1;
721
336
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
336
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
336
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
336
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
336
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
336
            hInfo->channel_position[5] = LFE_CHANNEL;
727
336
            break;
728
1.39k
        case 7:
729
1.39k
            hInfo->num_front_channels = 3;
730
1.39k
            hInfo->num_side_channels = 2;
731
1.39k
            hInfo->num_back_channels = 2;
732
1.39k
            hInfo->num_lfe_channels = 1;
733
1.39k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.39k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.39k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.39k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.39k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.39k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.39k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.39k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.39k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
78
                {
748
78
                    uint8_t ch1 = (ch-1)/2;
749
78
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
244
                        for (i = 1; i <= ch1; i+=2)
755
193
                        {
756
193
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
193
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
193
                        }
759
244
                        for (i = ch1+1; i < ch; i+=2)
760
193
                        {
761
193
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
193
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
193
                        }
764
51
                    } else {
765
27
                        hInfo->num_front_channels = ch1;
766
27
                        hInfo->num_back_channels = ch1 + 1;
767
184
                        for (i = 0; i < ch1; i+=2)
768
157
                        {
769
157
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
157
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
157
                        }
772
184
                        for (i = ch1; i < ch-1; i+=2)
773
157
                        {
774
157
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
157
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
157
                        }
777
27
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
27
                    }
779
138
                } else {
780
138
                    uint8_t ch1 = (ch)/2;
781
138
                    hInfo->num_front_channels = ch1;
782
138
                    hInfo->num_back_channels = ch1;
783
138
                    if (ch1 & 1)
784
66
                    {
785
66
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
399
                        for (i = 1; i <= ch1; i+=2)
787
333
                        {
788
333
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
333
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
333
                        }
791
333
                        for (i = ch1+1; i < ch-1; i+=2)
792
267
                        {
793
267
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
267
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
267
                        }
796
66
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
505
                        for (i = 0; i < ch1; i+=2)
799
433
                        {
800
433
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
433
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
433
                        }
803
505
                        for (i = ch1; i < ch; i+=2)
804
433
                        {
805
433
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
433
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
433
                        }
808
72
                    }
809
138
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
505
                for (i = ch; i < hDecoder->fr_channels; i++)
812
289
                {
813
289
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
289
                }
815
216
            }
816
216
            break;
817
8.76k
        }
818
8.76k
    }
819
9.14k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.14k
    if (hDecoder->pce_set)
617
383
    {
618
383
        uint8_t i, chpos = 0;
619
383
        uint8_t chdir, back_center = 0;
620
621
383
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
383
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
383
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
383
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
383
        chdir = hInfo->num_front_channels;
626
383
        if (chdir & 1)
627
153
        {
628
153
#if (defined(PS_DEC) || defined(DRM_PS))
629
153
            if (hInfo->num_front_channels == 1 &&
630
63
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
24
                hInfo->num_lfe_channels == 0)
633
15
            {
634
                /* When PS is enabled output is always stereo */
635
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
15
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
153
            chdir--;
641
153
        }
642
2.01k
        for (i = 0; i < chdir; i++)
643
1.62k
        {
644
1.62k
            hInfo->channel_position[chpos++] =
645
1.62k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.62k
        }
647
648
1.85k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
383
        chdir = hInfo->num_back_channels;
655
383
        if (chdir & 1)
656
55
        {
657
55
            back_center = 1;
658
55
            chdir--;
659
55
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
944
        {
662
944
            hInfo->channel_position[chpos++] =
663
944
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
944
        }
665
383
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
619
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
236
        {
672
236
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
236
        }
674
675
8.76k
    } else {
676
8.76k
        switch (hDecoder->channelConfiguration)
677
8.76k
        {
678
403
        case 1:
679
403
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
403
            hInfo->num_front_channels = 2;
682
403
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
403
            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
403
            break;
689
4.00k
        case 2:
690
4.00k
            hInfo->num_front_channels = 2;
691
4.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.00k
            break;
694
590
        case 3:
695
590
            hInfo->num_front_channels = 3;
696
590
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
590
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
590
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
590
            break;
700
1.14k
        case 4:
701
1.14k
            hInfo->num_front_channels = 3;
702
1.14k
            hInfo->num_back_channels = 1;
703
1.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.14k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.14k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.14k
            break;
708
672
        case 5:
709
672
            hInfo->num_front_channels = 3;
710
672
            hInfo->num_back_channels = 2;
711
672
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
672
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
672
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
672
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
672
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
672
            break;
717
336
        case 6:
718
336
            hInfo->num_front_channels = 3;
719
336
            hInfo->num_back_channels = 2;
720
336
            hInfo->num_lfe_channels = 1;
721
336
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
336
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
336
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
336
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
336
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
336
            hInfo->channel_position[5] = LFE_CHANNEL;
727
336
            break;
728
1.39k
        case 7:
729
1.39k
            hInfo->num_front_channels = 3;
730
1.39k
            hInfo->num_side_channels = 2;
731
1.39k
            hInfo->num_back_channels = 2;
732
1.39k
            hInfo->num_lfe_channels = 1;
733
1.39k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.39k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.39k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.39k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.39k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.39k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.39k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.39k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.39k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
78
                {
748
78
                    uint8_t ch1 = (ch-1)/2;
749
78
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
244
                        for (i = 1; i <= ch1; i+=2)
755
193
                        {
756
193
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
193
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
193
                        }
759
244
                        for (i = ch1+1; i < ch; i+=2)
760
193
                        {
761
193
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
193
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
193
                        }
764
51
                    } else {
765
27
                        hInfo->num_front_channels = ch1;
766
27
                        hInfo->num_back_channels = ch1 + 1;
767
184
                        for (i = 0; i < ch1; i+=2)
768
157
                        {
769
157
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
157
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
157
                        }
772
184
                        for (i = ch1; i < ch-1; i+=2)
773
157
                        {
774
157
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
157
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
157
                        }
777
27
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
27
                    }
779
138
                } else {
780
138
                    uint8_t ch1 = (ch)/2;
781
138
                    hInfo->num_front_channels = ch1;
782
138
                    hInfo->num_back_channels = ch1;
783
138
                    if (ch1 & 1)
784
66
                    {
785
66
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
399
                        for (i = 1; i <= ch1; i+=2)
787
333
                        {
788
333
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
333
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
333
                        }
791
333
                        for (i = ch1+1; i < ch-1; i+=2)
792
267
                        {
793
267
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
267
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
267
                        }
796
66
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
505
                        for (i = 0; i < ch1; i+=2)
799
433
                        {
800
433
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
433
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
433
                        }
803
505
                        for (i = ch1; i < ch; i+=2)
804
433
                        {
805
433
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
433
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
433
                        }
808
72
                    }
809
138
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
505
                for (i = ch; i < hDecoder->fr_channels; i++)
812
289
                {
813
289
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
289
                }
815
216
            }
816
216
            break;
817
8.76k
        }
818
8.76k
    }
819
9.14k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.2k
{
600
10.2k
    hInfo->num_front_channels = 0;
601
10.2k
    hInfo->num_side_channels = 0;
602
10.2k
    hInfo->num_back_channels = 0;
603
10.2k
    hInfo->num_lfe_channels = 0;
604
10.2k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.2k
    if (hDecoder->downMatrix)
607
1.09k
    {
608
1.09k
        hInfo->num_front_channels = 2;
609
1.09k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.09k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.09k
        return;
612
1.09k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.14k
    if (hDecoder->pce_set)
617
383
    {
618
383
        uint8_t i, chpos = 0;
619
383
        uint8_t chdir, back_center = 0;
620
621
383
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
383
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
383
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
383
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
383
        chdir = hInfo->num_front_channels;
626
383
        if (chdir & 1)
627
153
        {
628
153
#if (defined(PS_DEC) || defined(DRM_PS))
629
153
            if (hInfo->num_front_channels == 1 &&
630
63
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
24
                hInfo->num_lfe_channels == 0)
633
15
            {
634
                /* When PS is enabled output is always stereo */
635
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
15
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
15
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
153
            chdir--;
641
153
        }
642
2.01k
        for (i = 0; i < chdir; i++)
643
1.62k
        {
644
1.62k
            hInfo->channel_position[chpos++] =
645
1.62k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.62k
        }
647
648
1.85k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
383
        chdir = hInfo->num_back_channels;
655
383
        if (chdir & 1)
656
55
        {
657
55
            back_center = 1;
658
55
            chdir--;
659
55
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
944
        {
662
944
            hInfo->channel_position[chpos++] =
663
944
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
944
        }
665
383
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
619
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
236
        {
672
236
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
236
        }
674
675
8.76k
    } else {
676
8.76k
        switch (hDecoder->channelConfiguration)
677
8.76k
        {
678
403
        case 1:
679
403
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
403
            hInfo->num_front_channels = 2;
682
403
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
403
            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
403
            break;
689
4.00k
        case 2:
690
4.00k
            hInfo->num_front_channels = 2;
691
4.00k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.00k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.00k
            break;
694
590
        case 3:
695
590
            hInfo->num_front_channels = 3;
696
590
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
590
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
590
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
590
            break;
700
1.14k
        case 4:
701
1.14k
            hInfo->num_front_channels = 3;
702
1.14k
            hInfo->num_back_channels = 1;
703
1.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.14k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.14k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.14k
            break;
708
672
        case 5:
709
672
            hInfo->num_front_channels = 3;
710
672
            hInfo->num_back_channels = 2;
711
672
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
672
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
672
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
672
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
672
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
672
            break;
717
336
        case 6:
718
336
            hInfo->num_front_channels = 3;
719
336
            hInfo->num_back_channels = 2;
720
336
            hInfo->num_lfe_channels = 1;
721
336
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
336
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
336
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
336
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
336
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
336
            hInfo->channel_position[5] = LFE_CHANNEL;
727
336
            break;
728
1.39k
        case 7:
729
1.39k
            hInfo->num_front_channels = 3;
730
1.39k
            hInfo->num_side_channels = 2;
731
1.39k
            hInfo->num_back_channels = 2;
732
1.39k
            hInfo->num_lfe_channels = 1;
733
1.39k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.39k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.39k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.39k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.39k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.39k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.39k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.39k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.39k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
78
                {
748
78
                    uint8_t ch1 = (ch-1)/2;
749
78
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
244
                        for (i = 1; i <= ch1; i+=2)
755
193
                        {
756
193
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
193
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
193
                        }
759
244
                        for (i = ch1+1; i < ch; i+=2)
760
193
                        {
761
193
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
193
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
193
                        }
764
51
                    } else {
765
27
                        hInfo->num_front_channels = ch1;
766
27
                        hInfo->num_back_channels = ch1 + 1;
767
184
                        for (i = 0; i < ch1; i+=2)
768
157
                        {
769
157
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
157
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
157
                        }
772
184
                        for (i = ch1; i < ch-1; i+=2)
773
157
                        {
774
157
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
157
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
157
                        }
777
27
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
27
                    }
779
138
                } else {
780
138
                    uint8_t ch1 = (ch)/2;
781
138
                    hInfo->num_front_channels = ch1;
782
138
                    hInfo->num_back_channels = ch1;
783
138
                    if (ch1 & 1)
784
66
                    {
785
66
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
399
                        for (i = 1; i <= ch1; i+=2)
787
333
                        {
788
333
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
333
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
333
                        }
791
333
                        for (i = ch1+1; i < ch-1; i+=2)
792
267
                        {
793
267
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
267
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
267
                        }
796
66
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
505
                        for (i = 0; i < ch1; i+=2)
799
433
                        {
800
433
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
433
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
433
                        }
803
505
                        for (i = ch1; i < ch; i+=2)
804
433
                        {
805
433
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
433
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
433
                        }
808
72
                    }
809
138
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
505
                for (i = ch; i < hDecoder->fr_channels; i++)
812
289
                {
813
289
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
289
                }
815
216
            }
816
216
            break;
817
8.76k
        }
818
8.76k
    }
819
9.14k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
39.1k
{
826
39.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
39.1k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
39.1k
}
829
830
void* NeAACDecDecode2(NeAACDecHandle hpDecoder,
831
                                  NeAACDecFrameInfo *hInfo,
832
                                  unsigned char *buffer,
833
                                  unsigned long buffer_size,
834
                                  void **sample_buffer,
835
                                  unsigned long sample_buffer_size)
836
12.6k
{
837
12.6k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
12.6k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
104
    {
840
104
        hInfo->error = 27;
841
104
        return NULL;
842
104
    }
843
844
12.5k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
12.5k
        sample_buffer, sample_buffer_size);
846
12.6k
}
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
51.7k
{
855
51.7k
    uint16_t i;
856
51.7k
    uint8_t channels = 0;
857
51.7k
    uint8_t output_channels = 0;
858
51.7k
    bitfile ld;
859
51.7k
    uint32_t bitsconsumed;
860
51.7k
    uint16_t frame_len;
861
51.7k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
51.7k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
51.7k
    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
51.7k
    frame_len = hDecoder->frameLength;
882
883
884
51.7k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
51.7k
    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
51.7k
    if (buffer_size >= 128)
903
6.05k
    {
904
6.05k
        if (memcmp(buffer, "TAG", 3) == 0)
905
10
        {
906
            /* found it */
907
10
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
10
            return NULL;
910
10
        }
911
6.05k
    }
912
913
914
    /* initialize the bitstream */
915
51.7k
    faad_initbits(&ld, buffer, buffer_size);
916
51.7k
    if (ld.error != 0)
917
20.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
10.8k
    if (hDecoder->object_type == DRM_ER_LC)
952
982
    {
953
        /* We do not support stereo right now */
954
982
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
982
        faad_getbits(&ld, 8
961
982
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
982
    }
963
10.8k
#endif
964
965
30.9k
    if (hDecoder->adts_header_present)
966
829
    {
967
829
        adts_header adts;
968
969
829
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
829
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
103
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
829
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
10.8k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
982
    {
987
982
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
9.89k
    } else {
989
9.89k
#endif
990
19.9k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
19.9k
    channels = hDecoder->fr_channels;
1009
1010
30.8k
    if (hInfo->error > 0)
1011
20.4k
        goto error;
1012
1013
    /* safety check */
1014
10.3k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
101
    {
1016
        /* invalid number of channels */
1017
101
        hInfo->error = 12;
1018
101
        goto error;
1019
101
    }
1020
1021
    /* no more bit reading after this */
1022
10.2k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
10.2k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
10.2k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
10.2k
    faad_endbits(&ld);
1030
1031
1032
10.2k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
10.2k
        )
1037
10.0k
    {
1038
10.0k
        if (hDecoder->channelConfiguration == 0)
1039
332
            hDecoder->channelConfiguration = channels;
1040
1041
10.0k
        if (channels == 8) /* 7.1 */
1042
977
            hDecoder->channelConfiguration = 7;
1043
10.0k
        if (channels == 7) /* not a standard channelConfiguration */
1044
53
            hDecoder->channelConfiguration = 0;
1045
10.0k
    }
1046
1047
10.2k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.08k
    {
1049
1.08k
        hDecoder->downMatrix = 1;
1050
1.08k
        output_channels = 2;
1051
9.16k
    } else {
1052
9.16k
        output_channels = channels;
1053
9.16k
    }
1054
1055
10.2k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
10.2k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
10.2k
    if (output_channels == 1)
1059
291
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
291
        hDecoder->upMatrix = 1;
1062
291
        output_channels = 2;
1063
291
    }
1064
10.2k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
10.2k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
392
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
392
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
9
        {
1072
9
            hInfo->error = 22;
1073
9
            return NULL;
1074
9
        }
1075
392
    }
1076
10.2k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
10.2k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
10.2k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
10.2k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
10.2k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
10.2k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
10.2k
    hInfo->header_type = RAW;
1090
10.2k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
10.2k
    if (hDecoder->adts_header_present)
1093
221
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
10.2k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
10.2k
    hInfo->ps = hDecoder->ps_used_global;
1100
10.2k
#endif
1101
1102
    /* check if frame has channel elements */
1103
10.2k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
10.2k
    {
1110
10.2k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
10.2k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
10.2k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
10.2k
        };
1114
10.2k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
10.2k
#ifdef SBR_DEC
1116
10.2k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
5.61k
        {
1118
5.61k
            stride = 2 * stride;
1119
5.61k
        }
1120
10.2k
#endif
1121
10.2k
        required_buffer_size = frame_len*output_channels*stride;
1122
10.2k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
10.2k
    if (sample_buffer_size == 0)
1126
6.72k
    {
1127
        /* allocate the buffer for the final samples */
1128
6.72k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
5.86k
        {
1130
5.86k
            if (hDecoder->sample_buffer)
1131
97
                faad_free(hDecoder->sample_buffer);
1132
5.86k
            hDecoder->sample_buffer = NULL;
1133
5.86k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
5.86k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
5.86k
        }
1136
6.72k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.36k
        hInfo->error = 27;
1139
1.36k
        return NULL;
1140
1.36k
    }
1141
1142
8.87k
    if (sample_buffer_size == 0)
1143
6.72k
    {
1144
6.72k
        sample_buffer = hDecoder->sample_buffer;
1145
6.72k
    } else {
1146
2.14k
        sample_buffer = *sample_buffer2;
1147
2.14k
    }
1148
1149
8.87k
#ifdef SBR_DEC
1150
8.87k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
5.93k
    {
1152
5.93k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
5.93k
        if (!hDecoder->downSampledSBR)
1156
4.64k
        {
1157
4.64k
            frame_len *= 2;
1158
4.64k
            hInfo->samples *= 2;
1159
4.64k
            hInfo->samplerate *= 2;
1160
4.64k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
30.7k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
24.8k
        {
1165
24.8k
            if (hDecoder->sbr[ele] == NULL)
1166
6
            {
1167
6
                hInfo->error = 25;
1168
6
                goto error;
1169
6
            }
1170
24.8k
        }
1171
1172
        /* sbr */
1173
5.93k
        if (hDecoder->sbr_present_flag == 1)
1174
3.75k
        {
1175
3.75k
            hInfo->object_type = HE_AAC;
1176
3.75k
            hInfo->sbr = SBR_UPSAMPLED;
1177
3.75k
        } else {
1178
2.17k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.17k
        }
1180
5.93k
        if (hDecoder->downSampledSBR)
1181
1.28k
        {
1182
1.28k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.28k
        }
1184
5.93k
    }
1185
8.87k
#endif
1186
1187
1188
8.87k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
8.87k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
8.87k
    hDecoder->postSeekResetFlag = 0;
1198
1199
8.87k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
5.98k
    if (hDecoder->object_type != LD)
1202
5.14k
    {
1203
5.14k
#endif
1204
8.02k
        if (hDecoder->frame <= 1)
1205
2.85k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
843
    } else {
1208
        /* LD encoders will give lower delay */
1209
843
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
843
    }
1212
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
8.87k
    return sample_buffer;
1225
1226
20.6k
error:
1227
1228
    /* reset filterbank state */
1229
1.34M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.32M
    {
1231
1.32M
        if (hDecoder->fb_intermed[i] != NULL)
1232
650k
        {
1233
650k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
650k
        }
1235
1.32M
    }
1236
20.6k
#ifdef SBR_DEC
1237
1.01M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
993k
    {
1239
993k
        if (hDecoder->sbr[i] != NULL)
1240
472k
        {
1241
472k
            sbrReset(hDecoder->sbr[i]);
1242
472k
        }
1243
993k
    }
1244
20.6k
#endif
1245
1246
1247
20.6k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
8.87k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
17.9k
{
855
17.9k
    uint16_t i;
856
17.9k
    uint8_t channels = 0;
857
17.9k
    uint8_t output_channels = 0;
858
17.9k
    bitfile ld;
859
17.9k
    uint32_t bitsconsumed;
860
17.9k
    uint16_t frame_len;
861
17.9k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
17.9k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
17.9k
    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
17.9k
    frame_len = hDecoder->frameLength;
882
883
884
17.9k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
17.9k
    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
17.9k
    if (buffer_size >= 128)
903
2.75k
    {
904
2.75k
        if (memcmp(buffer, "TAG", 3) == 0)
905
4
        {
906
            /* found it */
907
4
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
4
            return NULL;
910
4
        }
911
2.75k
    }
912
913
914
    /* initialize the bitstream */
915
17.9k
    faad_initbits(&ld, buffer, buffer_size);
916
17.9k
    if (ld.error != 0)
917
7.06k
        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
10.8k
#ifdef DRM
951
10.8k
    if (hDecoder->object_type == DRM_ER_LC)
952
982
    {
953
        /* We do not support stereo right now */
954
982
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
982
        faad_getbits(&ld, 8
961
982
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
982
    }
963
10.8k
#endif
964
965
10.8k
    if (hDecoder->adts_header_present)
966
85
    {
967
85
        adts_header adts;
968
969
85
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
85
        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
85
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
10.8k
#ifdef DRM
985
10.8k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
982
    {
987
982
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
9.89k
    } else {
989
9.89k
#endif
990
9.89k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
9.89k
#ifdef DRM
992
9.89k
    }
993
10.8k
#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
10.8k
    channels = hDecoder->fr_channels;
1009
1010
10.8k
    if (hInfo->error > 0)
1011
7.25k
        goto error;
1012
1013
    /* safety check */
1014
3.62k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
33
    {
1016
        /* invalid number of channels */
1017
33
        hInfo->error = 12;
1018
33
        goto error;
1019
33
    }
1020
1021
    /* no more bit reading after this */
1022
3.59k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
3.59k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
3.59k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
3.59k
    faad_endbits(&ld);
1030
1031
1032
3.59k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
3.59k
        )
1037
3.56k
    {
1038
3.56k
        if (hDecoder->channelConfiguration == 0)
1039
152
            hDecoder->channelConfiguration = channels;
1040
1041
3.56k
        if (channels == 8) /* 7.1 */
1042
144
            hDecoder->channelConfiguration = 7;
1043
3.56k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
3.56k
    }
1046
1047
3.59k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
522
    {
1049
522
        hDecoder->downMatrix = 1;
1050
522
        output_channels = 2;
1051
3.07k
    } else {
1052
3.07k
        output_channels = channels;
1053
3.07k
    }
1054
1055
3.59k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
3.59k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
3.59k
    if (output_channels == 1)
1059
0
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
0
        hDecoder->upMatrix = 1;
1062
0
        output_channels = 2;
1063
0
    }
1064
3.59k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
3.59k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
178
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
178
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
3
        {
1072
3
            hInfo->error = 22;
1073
3
            return NULL;
1074
3
        }
1075
178
    }
1076
3.59k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
3.59k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
3.59k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
3.59k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
3.59k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
3.59k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
3.59k
    hInfo->header_type = RAW;
1090
3.59k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
3.59k
    if (hDecoder->adts_header_present)
1093
35
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
3.59k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
3.59k
    hInfo->ps = hDecoder->ps_used_global;
1100
3.59k
#endif
1101
1102
    /* check if frame has channel elements */
1103
3.59k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
3.59k
    {
1110
3.59k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
3.59k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
3.59k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
3.59k
        };
1114
3.59k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
3.59k
#ifdef SBR_DEC
1116
3.59k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
1.93k
        {
1118
1.93k
            stride = 2 * stride;
1119
1.93k
        }
1120
3.59k
#endif
1121
3.59k
        required_buffer_size = frame_len*output_channels*stride;
1122
3.59k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
3.59k
    if (sample_buffer_size == 0)
1126
1.82k
    {
1127
        /* allocate the buffer for the final samples */
1128
1.82k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
1.62k
        {
1130
1.62k
            if (hDecoder->sample_buffer)
1131
42
                faad_free(hDecoder->sample_buffer);
1132
1.62k
            hDecoder->sample_buffer = NULL;
1133
1.62k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
1.62k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
1.62k
        }
1136
1.82k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
704
        hInfo->error = 27;
1139
704
        return NULL;
1140
704
    }
1141
1142
2.88k
    if (sample_buffer_size == 0)
1143
1.82k
    {
1144
1.82k
        sample_buffer = hDecoder->sample_buffer;
1145
1.82k
    } else {
1146
1.06k
        sample_buffer = *sample_buffer2;
1147
1.06k
    }
1148
1149
2.88k
#ifdef SBR_DEC
1150
2.88k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
1.99k
    {
1152
1.99k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
1.99k
        if (!hDecoder->downSampledSBR)
1156
1.43k
        {
1157
1.43k
            frame_len *= 2;
1158
1.43k
            hInfo->samples *= 2;
1159
1.43k
            hInfo->samplerate *= 2;
1160
1.43k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
10.3k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
8.33k
        {
1165
8.33k
            if (hDecoder->sbr[ele] == NULL)
1166
2
            {
1167
2
                hInfo->error = 25;
1168
2
                goto error;
1169
2
            }
1170
8.33k
        }
1171
1172
        /* sbr */
1173
1.99k
        if (hDecoder->sbr_present_flag == 1)
1174
1.53k
        {
1175
1.53k
            hInfo->object_type = HE_AAC;
1176
1.53k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.53k
        } else {
1178
462
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
462
        }
1180
1.99k
        if (hDecoder->downSampledSBR)
1181
563
        {
1182
563
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
563
        }
1184
1.99k
    }
1185
2.88k
#endif
1186
1187
1188
2.88k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
2.88k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
2.88k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
2.88k
#endif
1195
1196
1197
2.88k
    hDecoder->postSeekResetFlag = 0;
1198
1199
2.88k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
2.88k
        if (hDecoder->frame <= 1)
1205
1.14k
            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.88k
    return sample_buffer;
1225
1226
7.28k
error:
1227
1228
    /* reset filterbank state */
1229
473k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
466k
    {
1231
466k
        if (hDecoder->fb_intermed[i] != NULL)
1232
177k
        {
1233
177k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
177k
        }
1235
466k
    }
1236
7.28k
#ifdef SBR_DEC
1237
357k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
349k
    {
1239
349k
        if (hDecoder->sbr[i] != NULL)
1240
134k
        {
1241
134k
            sbrReset(hDecoder->sbr[i]);
1242
134k
        }
1243
349k
    }
1244
7.28k
#endif
1245
1246
1247
7.28k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
2.88k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
33.7k
{
855
33.7k
    uint16_t i;
856
33.7k
    uint8_t channels = 0;
857
33.7k
    uint8_t output_channels = 0;
858
33.7k
    bitfile ld;
859
33.7k
    uint32_t bitsconsumed;
860
33.7k
    uint16_t frame_len;
861
33.7k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
33.7k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
33.7k
    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
33.7k
    frame_len = hDecoder->frameLength;
882
883
884
33.7k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
33.7k
    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
33.7k
    if (buffer_size >= 128)
903
3.29k
    {
904
3.29k
        if (memcmp(buffer, "TAG", 3) == 0)
905
6
        {
906
            /* found it */
907
6
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
6
            return NULL;
910
6
        }
911
3.29k
    }
912
913
914
    /* initialize the bitstream */
915
33.7k
    faad_initbits(&ld, buffer, buffer_size);
916
33.7k
    if (ld.error != 0)
917
13.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
    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
20.0k
    if (hDecoder->adts_header_present)
966
744
    {
967
744
        adts_header adts;
968
969
744
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
744
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
99
            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
744
    }
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
19.9k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
19.9k
    channels = hDecoder->fr_channels;
1009
1010
19.9k
    if (hInfo->error > 0)
1011
13.2k
        goto error;
1012
1013
    /* safety check */
1014
6.72k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
68
    {
1016
        /* invalid number of channels */
1017
68
        hInfo->error = 12;
1018
68
        goto error;
1019
68
    }
1020
1021
    /* no more bit reading after this */
1022
6.65k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
6.65k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
6.65k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
6.65k
    faad_endbits(&ld);
1030
1031
1032
6.65k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
6.65k
        )
1037
6.46k
    {
1038
6.46k
        if (hDecoder->channelConfiguration == 0)
1039
180
            hDecoder->channelConfiguration = channels;
1040
1041
6.46k
        if (channels == 8) /* 7.1 */
1042
833
            hDecoder->channelConfiguration = 7;
1043
6.46k
        if (channels == 7) /* not a standard channelConfiguration */
1044
53
            hDecoder->channelConfiguration = 0;
1045
6.46k
    }
1046
1047
6.65k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
566
    {
1049
566
        hDecoder->downMatrix = 1;
1050
566
        output_channels = 2;
1051
6.08k
    } else {
1052
6.08k
        output_channels = channels;
1053
6.08k
    }
1054
1055
6.65k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
6.65k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
6.65k
    if (output_channels == 1)
1059
291
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
291
        hDecoder->upMatrix = 1;
1062
291
        output_channels = 2;
1063
291
    }
1064
6.65k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
6.65k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
214
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
214
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
214
    }
1076
6.64k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
6.64k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
6.64k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
6.64k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
6.64k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
6.64k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
6.64k
    hInfo->header_type = RAW;
1090
6.64k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
6.64k
    if (hDecoder->adts_header_present)
1093
186
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
6.64k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
6.64k
    hInfo->ps = hDecoder->ps_used_global;
1100
6.64k
#endif
1101
1102
    /* check if frame has channel elements */
1103
6.64k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
6.64k
    {
1110
6.64k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
6.64k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
6.64k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
6.64k
        };
1114
6.64k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
6.64k
#ifdef SBR_DEC
1116
6.64k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.68k
        {
1118
3.68k
            stride = 2 * stride;
1119
3.68k
        }
1120
6.64k
#endif
1121
6.64k
        required_buffer_size = frame_len*output_channels*stride;
1122
6.64k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
6.64k
    if (sample_buffer_size == 0)
1126
4.90k
    {
1127
        /* allocate the buffer for the final samples */
1128
4.90k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
4.24k
        {
1130
4.24k
            if (hDecoder->sample_buffer)
1131
55
                faad_free(hDecoder->sample_buffer);
1132
4.24k
            hDecoder->sample_buffer = NULL;
1133
4.24k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
4.24k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
4.24k
        }
1136
4.90k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
661
        hInfo->error = 27;
1139
661
        return NULL;
1140
661
    }
1141
1142
5.98k
    if (sample_buffer_size == 0)
1143
4.90k
    {
1144
4.90k
        sample_buffer = hDecoder->sample_buffer;
1145
4.90k
    } else {
1146
1.08k
        sample_buffer = *sample_buffer2;
1147
1.08k
    }
1148
1149
5.98k
#ifdef SBR_DEC
1150
5.98k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.94k
    {
1152
3.94k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.94k
        if (!hDecoder->downSampledSBR)
1156
3.21k
        {
1157
3.21k
            frame_len *= 2;
1158
3.21k
            hInfo->samples *= 2;
1159
3.21k
            hInfo->samplerate *= 2;
1160
3.21k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
20.4k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
16.4k
        {
1165
16.4k
            if (hDecoder->sbr[ele] == NULL)
1166
4
            {
1167
4
                hInfo->error = 25;
1168
4
                goto error;
1169
4
            }
1170
16.4k
        }
1171
1172
        /* sbr */
1173
3.93k
        if (hDecoder->sbr_present_flag == 1)
1174
2.22k
        {
1175
2.22k
            hInfo->object_type = HE_AAC;
1176
2.22k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.22k
        } else {
1178
1.71k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.71k
        }
1180
3.93k
        if (hDecoder->downSampledSBR)
1181
720
        {
1182
720
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
720
        }
1184
3.93k
    }
1185
5.98k
#endif
1186
1187
1188
5.98k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
5.98k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
5.98k
    hDecoder->postSeekResetFlag = 0;
1198
1199
5.98k
    hDecoder->frame++;
1200
5.98k
#ifdef LD_DEC
1201
5.98k
    if (hDecoder->object_type != LD)
1202
5.14k
    {
1203
5.14k
#endif
1204
5.14k
        if (hDecoder->frame <= 1)
1205
1.71k
            hInfo->samples = 0;
1206
5.14k
#ifdef LD_DEC
1207
5.14k
    } else {
1208
        /* LD encoders will give lower delay */
1209
843
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
843
    }
1212
5.98k
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
5.98k
    return sample_buffer;
1225
1226
13.4k
error:
1227
1228
    /* reset filterbank state */
1229
871k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
858k
    {
1231
858k
        if (hDecoder->fb_intermed[i] != NULL)
1232
473k
        {
1233
473k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
473k
        }
1235
858k
    }
1236
13.4k
#ifdef SBR_DEC
1237
656k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
643k
    {
1239
643k
        if (hDecoder->sbr[i] != NULL)
1240
337k
        {
1241
337k
            sbrReset(hDecoder->sbr[i]);
1242
337k
        }
1243
643k
    }
1244
13.4k
#endif
1245
1246
1247
13.4k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
5.98k
}