Coverage Report

Created: 2026-09-04 07:01

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
324
{
74
324
    static char *libfaadName = PACKAGE_VERSION;
75
324
    static char *libCopyright =
76
324
        " Copyright 2002-2004: Ahead Software AG\n"
77
324
  " http://www.audiocoding.com\n"
78
324
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
324
    if (faad_id_string)
81
324
        *faad_id_string = libfaadName;
82
83
324
    if (faad_copyright_string)
84
324
        *faad_copyright_string = libCopyright;
85
86
324
    return 0;
87
324
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
324
{
91
324
    if (errcode >= NUM_ERROR_MESSAGES)
92
211
        return NULL;
93
113
    return err_msg[errcode];
94
324
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.29k
{
98
1.29k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.29k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
324
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
648
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
648
    cap += LD_DEC_CAP;
111
#endif
112
1.29k
#ifdef ERROR_RESILIENCE
113
1.29k
    cap += ERROR_RESILIENCE_CAP;
114
1.29k
#endif
115
#ifdef FIXED_POINT
116
648
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.29k
    return cap;
120
1.29k
}
NeAACDecGetCapabilities
Line
Count
Source
97
324
{
98
324
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
324
    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
324
#ifdef ERROR_RESILIENCE
113
324
    cap += ERROR_RESILIENCE_CAP;
114
324
#endif
115
324
#ifdef FIXED_POINT
116
324
    cap += FIXED_POINT_CAP;
117
324
#endif
118
119
324
    return cap;
120
324
}
NeAACDecGetCapabilities
Line
Count
Source
97
324
{
98
324
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
324
    cap += LC_DEC_CAP;
102
103
324
#ifdef MAIN_DEC
104
324
    cap += MAIN_DEC_CAP;
105
324
#endif
106
324
#ifdef LTP_DEC
107
324
    cap += LTP_DEC_CAP;
108
324
#endif
109
324
#ifdef LD_DEC
110
324
    cap += LD_DEC_CAP;
111
324
#endif
112
324
#ifdef ERROR_RESILIENCE
113
324
    cap += ERROR_RESILIENCE_CAP;
114
324
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
324
    return cap;
120
324
}
NeAACDecGetCapabilities
Line
Count
Source
97
324
{
98
324
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
324
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
324
#ifdef LTP_DEC
107
324
    cap += LTP_DEC_CAP;
108
324
#endif
109
324
#ifdef LD_DEC
110
324
    cap += LD_DEC_CAP;
111
324
#endif
112
324
#ifdef ERROR_RESILIENCE
113
324
    cap += ERROR_RESILIENCE_CAP;
114
324
#endif
115
324
#ifdef FIXED_POINT
116
324
    cap += FIXED_POINT_CAP;
117
324
#endif
118
119
324
    return cap;
120
324
}
NeAACDecGetCapabilities
Line
Count
Source
97
324
{
98
324
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
324
    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
324
#ifdef ERROR_RESILIENCE
113
324
    cap += ERROR_RESILIENCE_CAP;
114
324
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
324
    return cap;
120
324
}
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
59.1k
{
125
59.1k
    uint8_t i;
126
59.1k
    NeAACDecStruct *hDecoder = NULL;
127
128
59.1k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
59.1k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
59.1k
    hDecoder->cmes = mes;
134
59.1k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
59.1k
    hDecoder->config.defObjectType = MAIN;
136
59.1k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
59.1k
    hDecoder->config.downMatrix = 0;
138
59.1k
    hDecoder->adts_header_present = 0;
139
59.1k
    hDecoder->adif_header_present = 0;
140
59.1k
    hDecoder->latm_header_present = 0;
141
59.1k
#ifdef ERROR_RESILIENCE
142
59.1k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
59.1k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
59.1k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
59.1k
#endif
146
59.1k
    hDecoder->frameLength = 1024;
147
148
59.1k
    hDecoder->frame = 0;
149
59.1k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
59.1k
    hDecoder->__r1 = 0x2bb431ea;
153
59.1k
    hDecoder->__r2 = 0x206155b7;
154
155
3.84M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.78M
    {
157
3.78M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.78M
        hDecoder->window_shape_prev[i] = 0;
159
3.78M
        hDecoder->time_out[i] = NULL;
160
3.78M
        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.78M
    }
173
174
59.1k
#ifdef SBR_DEC
175
2.89M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.84M
    {
177
2.84M
        hDecoder->sbr[i] = NULL;
178
2.84M
    }
179
59.1k
#endif
180
181
59.1k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
59.1k
    return hDecoder;
184
59.1k
}
NeAACDecOpen
Line
Count
Source
124
11.9k
{
125
11.9k
    uint8_t i;
126
11.9k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.9k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.9k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.9k
    hDecoder->cmes = mes;
134
11.9k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.9k
    hDecoder->config.defObjectType = MAIN;
136
11.9k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.9k
    hDecoder->config.downMatrix = 0;
138
11.9k
    hDecoder->adts_header_present = 0;
139
11.9k
    hDecoder->adif_header_present = 0;
140
11.9k
    hDecoder->latm_header_present = 0;
141
11.9k
#ifdef ERROR_RESILIENCE
142
11.9k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.9k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.9k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.9k
#endif
146
11.9k
    hDecoder->frameLength = 1024;
147
148
11.9k
    hDecoder->frame = 0;
149
11.9k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.9k
    hDecoder->__r1 = 0x2bb431ea;
153
11.9k
    hDecoder->__r2 = 0x206155b7;
154
155
777k
    for (i = 0; i < MAX_CHANNELS; i++)
156
765k
    {
157
765k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
765k
        hDecoder->window_shape_prev[i] = 0;
159
765k
        hDecoder->time_out[i] = NULL;
160
765k
        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
765k
    }
173
174
11.9k
#ifdef SBR_DEC
175
585k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
574k
    {
177
574k
        hDecoder->sbr[i] = NULL;
178
574k
    }
179
11.9k
#endif
180
181
11.9k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.9k
    return hDecoder;
184
11.9k
}
NeAACDecOpen
Line
Count
Source
124
17.6k
{
125
17.6k
    uint8_t i;
126
17.6k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.6k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.6k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.6k
    hDecoder->cmes = mes;
134
17.6k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.6k
    hDecoder->config.defObjectType = MAIN;
136
17.6k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.6k
    hDecoder->config.downMatrix = 0;
138
17.6k
    hDecoder->adts_header_present = 0;
139
17.6k
    hDecoder->adif_header_present = 0;
140
17.6k
    hDecoder->latm_header_present = 0;
141
17.6k
#ifdef ERROR_RESILIENCE
142
17.6k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.6k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.6k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.6k
#endif
146
17.6k
    hDecoder->frameLength = 1024;
147
148
17.6k
    hDecoder->frame = 0;
149
17.6k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.6k
    hDecoder->__r1 = 0x2bb431ea;
153
17.6k
    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.6k
#ifdef SBR_DEC
175
863k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
846k
    {
177
846k
        hDecoder->sbr[i] = NULL;
178
846k
    }
179
17.6k
#endif
180
181
17.6k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.6k
    return hDecoder;
184
17.6k
}
NeAACDecOpen
Line
Count
Source
124
11.9k
{
125
11.9k
    uint8_t i;
126
11.9k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.9k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.9k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.9k
    hDecoder->cmes = mes;
134
11.9k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.9k
    hDecoder->config.defObjectType = MAIN;
136
11.9k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.9k
    hDecoder->config.downMatrix = 0;
138
11.9k
    hDecoder->adts_header_present = 0;
139
11.9k
    hDecoder->adif_header_present = 0;
140
11.9k
    hDecoder->latm_header_present = 0;
141
11.9k
#ifdef ERROR_RESILIENCE
142
11.9k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.9k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.9k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.9k
#endif
146
11.9k
    hDecoder->frameLength = 1024;
147
148
11.9k
    hDecoder->frame = 0;
149
11.9k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.9k
    hDecoder->__r1 = 0x2bb431ea;
153
11.9k
    hDecoder->__r2 = 0x206155b7;
154
155
777k
    for (i = 0; i < MAX_CHANNELS; i++)
156
765k
    {
157
765k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
765k
        hDecoder->window_shape_prev[i] = 0;
159
765k
        hDecoder->time_out[i] = NULL;
160
765k
        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
765k
#ifdef LTP_DEC
169
765k
        hDecoder->ltp_lag[i] = 0;
170
765k
        hDecoder->lt_pred_stat[i] = NULL;
171
765k
#endif
172
765k
    }
173
174
11.9k
#ifdef SBR_DEC
175
585k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
574k
    {
177
574k
        hDecoder->sbr[i] = NULL;
178
574k
    }
179
11.9k
#endif
180
181
11.9k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.9k
    return hDecoder;
184
11.9k
}
NeAACDecOpen
Line
Count
Source
124
17.6k
{
125
17.6k
    uint8_t i;
126
17.6k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.6k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.6k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.6k
    hDecoder->cmes = mes;
134
17.6k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.6k
    hDecoder->config.defObjectType = MAIN;
136
17.6k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.6k
    hDecoder->config.downMatrix = 0;
138
17.6k
    hDecoder->adts_header_present = 0;
139
17.6k
    hDecoder->adif_header_present = 0;
140
17.6k
    hDecoder->latm_header_present = 0;
141
17.6k
#ifdef ERROR_RESILIENCE
142
17.6k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.6k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.6k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.6k
#endif
146
17.6k
    hDecoder->frameLength = 1024;
147
148
17.6k
    hDecoder->frame = 0;
149
17.6k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.6k
    hDecoder->__r1 = 0x2bb431ea;
153
17.6k
    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.6k
#ifdef SBR_DEC
175
863k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
846k
    {
177
846k
        hDecoder->sbr[i] = NULL;
178
846k
    }
179
17.6k
#endif
180
181
17.6k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.6k
    return hDecoder;
184
17.6k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
28.1k
{
188
28.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
28.1k
    if (hDecoder)
190
28.1k
    {
191
28.1k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
28.1k
        return config;
194
28.1k
    }
195
196
0
    return NULL;
197
28.1k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
28.3k
{
202
28.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
28.3k
    if (hDecoder && config)
204
28.3k
    {
205
        /* check if we can decode this object type */
206
28.3k
        if (can_decode_ot(config->defObjectType) < 0)
207
13
            return 0;
208
28.3k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
28.3k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
28.3k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
28.3k
#ifdef FIXED_POINT
217
28.3k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
201
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
28.1k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
28.1k
        if (config->downMatrix > 1)
226
31
            return 0;
227
28.1k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
28.1k
        return 1;
231
28.1k
    }
232
233
0
    return 0;
234
28.3k
}
235
236
237
#if 0
238
static int latmCheck(latm_header *latm, bitfile *ld)
239
{
240
    uint32_t good=0, bad=0, bits, m;
241
242
    while (ld->bytes_left)
243
    {
244
        bits = faad_latm_frame(latm, ld);
245
        if(bits==0xFFFFFFFF)
246
            bad++;
247
        else
248
        {
249
            good++;
250
            while(bits>0)
251
            {
252
                m = min(bits, 8);
253
                faad_getbits(ld, m);
254
                bits -= m;
255
            }
256
        }
257
    }
258
259
    return (good>0);
260
}
261
#endif
262
263
long NeAACDecInit(NeAACDecHandle hpDecoder,
264
                              unsigned char *buffer,
265
                              unsigned long buffer_size,
266
                              unsigned long *samplerate,
267
                              unsigned char *channels)
268
19.1k
{
269
19.1k
    uint32_t bits = 0;
270
19.1k
    bitfile ld;
271
19.1k
    adif_header adif;
272
19.1k
    adts_header adts;
273
19.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
19.1k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
48
        return -1;
278
279
19.1k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
19.1k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
19.1k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
19.1k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
19.1k
    *channels = 1;
284
285
19.1k
    if (buffer != NULL)
286
19.1k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
19.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
19.1k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
475
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
427
        {
314
427
            hDecoder->adif_header_present = 1;
315
316
427
            get_adif_header(&adif, &ld);
317
427
            faad_byte_align(&ld);
318
319
427
            hDecoder->sf_index = adif.pce[0].sf_index;
320
427
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
427
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
427
            *channels = adif.pce[0].channels;
324
325
427
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
427
            hDecoder->pce_set = 1;
327
328
427
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
18.6k
        } else if (adts_frame(&adts, &ld) == 0) {
332
594
            hDecoder->adts_header_present = 1;
333
334
594
            hDecoder->sf_index = adts.sf_index;
335
594
            hDecoder->object_type = adts.profile + 1;
336
337
594
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
594
            *channels = (adts.channel_configuration > 6) ?
339
547
                2 : adts.channel_configuration;
340
594
        }
341
342
19.1k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
19.1k
        faad_endbits(&ld);
348
19.1k
    }
349
350
19.1k
    if (!*samplerate)
351
174
        return -1;
352
353
18.9k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
18.9k
    if (*channels == 1)
356
18.1k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
18.1k
        *channels = 2;
359
18.1k
    }
360
18.9k
#endif
361
362
18.9k
    hDecoder->channelConfiguration = *channels;
363
364
18.9k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
18.9k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
8.82k
    {
368
8.82k
        *samplerate *= 2;
369
8.82k
        hDecoder->forceUpSampling = 1;
370
10.1k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
10.1k
        hDecoder->downSampledSBR = 1;
372
10.1k
    }
373
18.9k
#endif
374
375
18.9k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
205
        return -1;
377
378
    /* must be done before frameLength is divided by 2 for LD */
379
#ifdef SSR_DEC
380
    if (hDecoder->object_type == SSR)
381
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
382
    else
383
#endif
384
18.7k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
385
386
#ifdef LD_DEC
387
8.32k
    if (hDecoder->object_type == LD)
388
285
        hDecoder->frameLength >>= 1;
389
#endif
390
391
18.7k
    return bits;
392
18.9k
}
NeAACDecInit
Line
Count
Source
268
10.7k
{
269
10.7k
    uint32_t bits = 0;
270
10.7k
    bitfile ld;
271
10.7k
    adif_header adif;
272
10.7k
    adts_header adts;
273
10.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
10.7k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
36
        return -1;
278
279
10.6k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
10.6k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
10.6k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
10.6k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
10.6k
    *channels = 1;
284
285
10.6k
    if (buffer != NULL)
286
10.6k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
10.6k
        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
10.6k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
257
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
225
        {
314
225
            hDecoder->adif_header_present = 1;
315
316
225
            get_adif_header(&adif, &ld);
317
225
            faad_byte_align(&ld);
318
319
225
            hDecoder->sf_index = adif.pce[0].sf_index;
320
225
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
225
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
225
            *channels = adif.pce[0].channels;
324
325
225
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
225
            hDecoder->pce_set = 1;
327
328
225
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
10.4k
        } else if (adts_frame(&adts, &ld) == 0) {
332
180
            hDecoder->adts_header_present = 1;
333
334
180
            hDecoder->sf_index = adts.sf_index;
335
180
            hDecoder->object_type = adts.profile + 1;
336
337
180
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
180
            *channels = (adts.channel_configuration > 6) ?
339
167
                2 : adts.channel_configuration;
340
180
        }
341
342
10.6k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
10.6k
        faad_endbits(&ld);
348
10.6k
    }
349
350
10.6k
    if (!*samplerate)
351
88
        return -1;
352
353
10.5k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
10.5k
    if (*channels == 1)
356
10.2k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
10.2k
        *channels = 2;
359
10.2k
    }
360
10.5k
#endif
361
362
10.5k
    hDecoder->channelConfiguration = *channels;
363
364
10.5k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
10.5k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
4.81k
    {
368
4.81k
        *samplerate *= 2;
369
4.81k
        hDecoder->forceUpSampling = 1;
370
5.76k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
5.76k
        hDecoder->downSampledSBR = 1;
372
5.76k
    }
373
10.5k
#endif
374
375
10.5k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
161
        return -1;
377
378
    /* must be done before frameLength is divided by 2 for LD */
379
#ifdef SSR_DEC
380
    if (hDecoder->object_type == SSR)
381
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
382
    else
383
#endif
384
10.4k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
385
386
#ifdef LD_DEC
387
    if (hDecoder->object_type == LD)
388
        hDecoder->frameLength >>= 1;
389
#endif
390
391
10.4k
    return bits;
392
10.5k
}
NeAACDecInit
Line
Count
Source
268
8.47k
{
269
8.47k
    uint32_t bits = 0;
270
8.47k
    bitfile ld;
271
8.47k
    adif_header adif;
272
8.47k
    adts_header adts;
273
8.47k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
8.47k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
12
        return -1;
278
279
8.45k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
8.45k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
8.45k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
8.45k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
8.45k
    *channels = 1;
284
285
8.45k
    if (buffer != NULL)
286
8.45k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
8.45k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
8.45k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
218
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
202
        {
314
202
            hDecoder->adif_header_present = 1;
315
316
202
            get_adif_header(&adif, &ld);
317
202
            faad_byte_align(&ld);
318
319
202
            hDecoder->sf_index = adif.pce[0].sf_index;
320
202
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
202
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
202
            *channels = adif.pce[0].channels;
324
325
202
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
202
            hDecoder->pce_set = 1;
327
328
202
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
8.25k
        } else if (adts_frame(&adts, &ld) == 0) {
332
414
            hDecoder->adts_header_present = 1;
333
334
414
            hDecoder->sf_index = adts.sf_index;
335
414
            hDecoder->object_type = adts.profile + 1;
336
337
414
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
414
            *channels = (adts.channel_configuration > 6) ?
339
380
                2 : adts.channel_configuration;
340
414
        }
341
342
8.45k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
8.45k
        faad_endbits(&ld);
348
8.45k
    }
349
350
8.45k
    if (!*samplerate)
351
86
        return -1;
352
353
8.37k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
8.37k
    if (*channels == 1)
356
7.85k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
7.85k
        *channels = 2;
359
7.85k
    }
360
8.37k
#endif
361
362
8.37k
    hDecoder->channelConfiguration = *channels;
363
364
8.37k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
8.37k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
4.01k
    {
368
4.01k
        *samplerate *= 2;
369
4.01k
        hDecoder->forceUpSampling = 1;
370
4.36k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
4.36k
        hDecoder->downSampledSBR = 1;
372
4.36k
    }
373
8.37k
#endif
374
375
8.37k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
44
        return -1;
377
378
    /* must be done before frameLength is divided by 2 for LD */
379
#ifdef SSR_DEC
380
    if (hDecoder->object_type == SSR)
381
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
382
    else
383
#endif
384
8.32k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
385
386
8.32k
#ifdef LD_DEC
387
8.32k
    if (hDecoder->object_type == LD)
388
285
        hDecoder->frameLength >>= 1;
389
8.32k
#endif
390
391
8.32k
    return bits;
392
8.37k
}
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.94k
{
401
8.94k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
8.94k
    int8_t rc;
403
8.94k
    mp4AudioSpecificConfig mp4ASC;
404
405
8.94k
    if((hDecoder == NULL)
406
8.94k
        || (pBuffer == NULL)
407
8.94k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.92k
        || (samplerate == NULL)
409
8.92k
        || (channels == NULL))
410
17
    {
411
17
        return -1;
412
17
    }
413
414
8.92k
    hDecoder->adif_header_present = 0;
415
8.92k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.92k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.92k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.92k
    *samplerate = mp4ASC.samplingFrequency;
423
8.92k
    if (mp4ASC.channelsConfiguration)
424
7.55k
    {
425
7.55k
        *channels = mp4ASC.channelsConfiguration;
426
7.55k
    } else {
427
1.37k
        *channels = hDecoder->pce.channels;
428
1.37k
        hDecoder->pce_set = 1;
429
1.37k
    }
430
8.92k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.92k
    if (*channels == 1)
433
49
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
49
        *channels = 2;
436
49
    }
437
8.92k
#endif
438
8.92k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.92k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.92k
#ifdef ERROR_RESILIENCE
441
8.92k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.92k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.92k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.92k
#endif
445
8.92k
#ifdef SBR_DEC
446
8.92k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.92k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.92k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.92k
        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.92k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
5.40k
    {
456
5.40k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
5.40k
    }
458
8.92k
#endif
459
460
8.92k
    if (rc != 0)
461
94
    {
462
94
        return rc;
463
94
    }
464
8.83k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.83k
    if (mp4ASC.frameLengthFlag)
466
5.53k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
5.53k
        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.83k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
4.13k
    if (hDecoder->object_type == LD)
482
489
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.83k
    return 0;
486
8.92k
}
NeAACDecInit2
Line
Count
Source
400
4.76k
{
401
4.76k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
4.76k
    int8_t rc;
403
4.76k
    mp4AudioSpecificConfig mp4ASC;
404
405
4.76k
    if((hDecoder == NULL)
406
4.76k
        || (pBuffer == NULL)
407
4.76k
        || (SizeOfDecoderSpecificInfo < 2)
408
4.75k
        || (samplerate == NULL)
409
4.75k
        || (channels == NULL))
410
10
    {
411
10
        return -1;
412
10
    }
413
414
4.75k
    hDecoder->adif_header_present = 0;
415
4.75k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
4.75k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
4.75k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
4.75k
    *samplerate = mp4ASC.samplingFrequency;
423
4.75k
    if (mp4ASC.channelsConfiguration)
424
4.03k
    {
425
4.03k
        *channels = mp4ASC.channelsConfiguration;
426
4.03k
    } else {
427
720
        *channels = hDecoder->pce.channels;
428
720
        hDecoder->pce_set = 1;
429
720
    }
430
4.75k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
4.75k
    if (*channels == 1)
433
35
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
35
        *channels = 2;
436
35
    }
437
4.75k
#endif
438
4.75k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
4.75k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
4.75k
#ifdef ERROR_RESILIENCE
441
4.75k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
4.75k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
4.75k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
4.75k
#endif
445
4.75k
#ifdef SBR_DEC
446
4.75k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
4.75k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
4.75k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
4.75k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
4.75k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
2.79k
    {
456
2.79k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
2.79k
    }
458
4.75k
#endif
459
460
4.75k
    if (rc != 0)
461
54
    {
462
54
        return rc;
463
54
    }
464
4.69k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
4.69k
    if (mp4ASC.frameLengthFlag)
466
2.99k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
2.99k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
4.69k
        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
4.69k
    return 0;
486
4.75k
}
NeAACDecInit2
Line
Count
Source
400
4.18k
{
401
4.18k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
4.18k
    int8_t rc;
403
4.18k
    mp4AudioSpecificConfig mp4ASC;
404
405
4.18k
    if((hDecoder == NULL)
406
4.18k
        || (pBuffer == NULL)
407
4.18k
        || (SizeOfDecoderSpecificInfo < 2)
408
4.17k
        || (samplerate == NULL)
409
4.17k
        || (channels == NULL))
410
7
    {
411
7
        return -1;
412
7
    }
413
414
4.17k
    hDecoder->adif_header_present = 0;
415
4.17k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
4.17k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
4.17k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
4.17k
    *samplerate = mp4ASC.samplingFrequency;
423
4.17k
    if (mp4ASC.channelsConfiguration)
424
3.51k
    {
425
3.51k
        *channels = mp4ASC.channelsConfiguration;
426
3.51k
    } else {
427
654
        *channels = hDecoder->pce.channels;
428
654
        hDecoder->pce_set = 1;
429
654
    }
430
4.17k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
4.17k
    if (*channels == 1)
433
14
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
14
        *channels = 2;
436
14
    }
437
4.17k
#endif
438
4.17k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
4.17k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
4.17k
#ifdef ERROR_RESILIENCE
441
4.17k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
4.17k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
4.17k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
4.17k
#endif
445
4.17k
#ifdef SBR_DEC
446
4.17k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
4.17k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
4.17k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
4.17k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
4.17k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
2.61k
    {
456
2.61k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
2.61k
    }
458
4.17k
#endif
459
460
4.17k
    if (rc != 0)
461
40
    {
462
40
        return rc;
463
40
    }
464
4.13k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
4.13k
    if (mp4ASC.frameLengthFlag)
466
2.54k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
2.54k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
4.13k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
4.13k
#ifdef LD_DEC
481
4.13k
    if (hDecoder->object_type == LD)
482
489
        hDecoder->frameLength >>= 1;
483
4.13k
#endif
484
485
4.13k
    return 0;
486
4.17k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
1.22k
{
493
1.22k
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
1.22k
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
1.22k
    NeAACDecClose(*hDecoder);
498
499
1.22k
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
1.22k
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
1.22k
    (*hDecoder)->config.defSampleRate = samplerate;
505
1.22k
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
1.22k
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
1.22k
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
1.22k
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
1.22k
#endif
510
1.22k
    (*hDecoder)->frameLength = 960;
511
1.22k
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
1.22k
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
1.22k
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
223
        (*hDecoder)->channelConfiguration = 2;
516
1.00k
    else
517
1.00k
        (*hDecoder)->channelConfiguration = 1;
518
519
1.22k
#ifdef SBR_DEC
520
1.22k
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
200
        (*hDecoder)->sbr_present_flag = 0;
522
1.02k
    else
523
1.02k
        (*hDecoder)->sbr_present_flag = 1;
524
1.22k
#endif
525
526
1.22k
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
1.22k
    return 0;
529
1.22k
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
16.8k
{
534
16.8k
    uint8_t i;
535
16.8k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
16.8k
    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
1.09M
    for (i = 0; i < MAX_CHANNELS; i++)
549
1.07M
    {
550
1.07M
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
1.07M
        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
1.07M
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
16.8k
        filter_bank_end(hDecoder->fb);
570
571
16.8k
    drc_end(hDecoder->drc);
572
573
16.8k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
16.8k
#ifdef SBR_DEC
576
825k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
808k
    {
578
808k
        if (hDecoder->sbr[i])
579
188k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
808k
    }
581
16.8k
#endif
582
583
16.8k
    if (hDecoder) faad_free(hDecoder);
584
16.8k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
20.4k
{
588
20.4k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
20.4k
    if (hDecoder)
590
20.4k
    {
591
20.4k
        hDecoder->postSeekResetFlag = 1;
592
593
20.4k
        if (frame != -1)
594
20.4k
            hDecoder->frame = frame;
595
20.4k
    }
596
20.4k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
43.3k
{
600
43.3k
    hInfo->num_front_channels = 0;
601
43.3k
    hInfo->num_side_channels = 0;
602
43.3k
    hInfo->num_back_channels = 0;
603
43.3k
    hInfo->num_lfe_channels = 0;
604
43.3k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
43.3k
    if (hDecoder->downMatrix)
607
4.64k
    {
608
4.64k
        hInfo->num_front_channels = 2;
609
4.64k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.64k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.64k
        return;
612
4.64k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
38.6k
    if (hDecoder->pce_set)
617
1.64k
    {
618
1.64k
        uint8_t i, chpos = 0;
619
1.64k
        uint8_t chdir, back_center = 0;
620
621
1.64k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.64k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.64k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.64k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.64k
        chdir = hInfo->num_front_channels;
626
1.64k
        if (chdir & 1)
627
684
        {
628
684
#if (defined(PS_DEC) || defined(DRM_PS))
629
684
            if (hInfo->num_front_channels == 1 &&
630
316
                hInfo->num_side_channels == 0 &&
631
188
                hInfo->num_back_channels == 0 &&
632
144
                hInfo->num_lfe_channels == 0)
633
108
            {
634
                /* When PS is enabled output is always stereo */
635
108
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
108
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
108
            } else
638
576
#endif
639
576
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
684
            chdir--;
641
684
        }
642
9.08k
        for (i = 0; i < chdir; i++)
643
7.44k
        {
644
7.44k
            hInfo->channel_position[chpos++] =
645
7.44k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
7.44k
        }
647
648
8.04k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
6.40k
        {
650
6.40k
            hInfo->channel_position[chpos++] =
651
6.40k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
6.40k
        }
653
654
1.64k
        chdir = hInfo->num_back_channels;
655
1.64k
        if (chdir & 1)
656
260
        {
657
260
            back_center = 1;
658
260
            chdir--;
659
260
        }
660
5.35k
        for (i = 0; i < chdir; i++)
661
3.71k
        {
662
3.71k
            hInfo->channel_position[chpos++] =
663
3.71k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.71k
        }
665
1.64k
        if (back_center)
666
260
        {
667
260
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
260
        }
669
670
2.63k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
992
        {
672
992
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
992
        }
674
675
37.0k
    } else {
676
37.0k
        switch (hDecoder->channelConfiguration)
677
37.0k
        {
678
2.48k
        case 1:
679
2.48k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
2.48k
            hInfo->num_front_channels = 2;
682
2.48k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
2.48k
            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
2.48k
            break;
689
16.4k
        case 2:
690
16.4k
            hInfo->num_front_channels = 2;
691
16.4k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
16.4k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
16.4k
            break;
694
3.08k
        case 3:
695
3.08k
            hInfo->num_front_channels = 3;
696
3.08k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
3.08k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
3.08k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
3.08k
            break;
700
4.12k
        case 4:
701
4.12k
            hInfo->num_front_channels = 3;
702
4.12k
            hInfo->num_back_channels = 1;
703
4.12k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.12k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.12k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.12k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.12k
            break;
708
2.80k
        case 5:
709
2.80k
            hInfo->num_front_channels = 3;
710
2.80k
            hInfo->num_back_channels = 2;
711
2.80k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.80k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.80k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.80k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.80k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.80k
            break;
717
1.09k
        case 6:
718
1.09k
            hInfo->num_front_channels = 3;
719
1.09k
            hInfo->num_back_channels = 2;
720
1.09k
            hInfo->num_lfe_channels = 1;
721
1.09k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.09k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.09k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.09k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.09k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.09k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.09k
            break;
728
6.03k
        case 7:
729
6.03k
            hInfo->num_front_channels = 3;
730
6.03k
            hInfo->num_side_channels = 2;
731
6.03k
            hInfo->num_back_channels = 2;
732
6.03k
            hInfo->num_lfe_channels = 1;
733
6.03k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
6.03k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
6.03k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
6.03k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
6.03k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
6.03k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
6.03k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
6.03k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
6.03k
            break;
742
936
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
936
            {
744
936
                uint8_t i;
745
936
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
936
                if (ch & 1) /* there's either a center front or a center back channel */
747
300
                {
748
300
                    uint8_t ch1 = (ch-1)/2;
749
300
                    if (hDecoder->first_syn_ele == ID_SCE)
750
200
                    {
751
200
                        hInfo->num_front_channels = ch1 + 1;
752
200
                        hInfo->num_back_channels = ch1;
753
200
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
1.00k
                        for (i = 1; i <= ch1; i+=2)
755
804
                        {
756
804
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
804
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
804
                        }
759
1.00k
                        for (i = ch1+1; i < ch; i+=2)
760
804
                        {
761
804
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
804
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
804
                        }
764
200
                    } else {
765
100
                        hInfo->num_front_channels = ch1;
766
100
                        hInfo->num_back_channels = ch1 + 1;
767
608
                        for (i = 0; i < ch1; i+=2)
768
508
                        {
769
508
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
508
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
508
                        }
772
608
                        for (i = ch1; i < ch-1; i+=2)
773
508
                        {
774
508
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
508
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
508
                        }
777
100
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
100
                    }
779
636
                } else {
780
636
                    uint8_t ch1 = (ch)/2;
781
636
                    hInfo->num_front_channels = ch1;
782
636
                    hInfo->num_back_channels = ch1;
783
636
                    if (ch1 & 1)
784
308
                    {
785
308
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.83k
                        for (i = 1; i <= ch1; i+=2)
787
1.52k
                        {
788
1.52k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.52k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.52k
                        }
791
1.52k
                        for (i = ch1+1; i < ch-1; i+=2)
792
1.21k
                        {
793
1.21k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
1.21k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
1.21k
                        }
796
308
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
328
                    } else {
798
2.77k
                        for (i = 0; i < ch1; i+=2)
799
2.44k
                        {
800
2.44k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
2.44k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
2.44k
                        }
803
2.77k
                        for (i = ch1; i < ch; i+=2)
804
2.44k
                        {
805
2.44k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
2.44k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
2.44k
                        }
808
328
                    }
809
636
                }
810
936
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
1.82k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
884
                {
813
884
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
884
                }
815
936
            }
816
936
            break;
817
37.0k
        }
818
37.0k
    }
819
38.6k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.8k
{
600
10.8k
    hInfo->num_front_channels = 0;
601
10.8k
    hInfo->num_side_channels = 0;
602
10.8k
    hInfo->num_back_channels = 0;
603
10.8k
    hInfo->num_lfe_channels = 0;
604
10.8k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.8k
    if (hDecoder->downMatrix)
607
1.16k
    {
608
1.16k
        hInfo->num_front_channels = 2;
609
1.16k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.16k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.16k
        return;
612
1.16k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.66k
    if (hDecoder->pce_set)
617
410
    {
618
410
        uint8_t i, chpos = 0;
619
410
        uint8_t chdir, back_center = 0;
620
621
410
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
410
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
410
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
410
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
410
        chdir = hInfo->num_front_channels;
626
410
        if (chdir & 1)
627
171
        {
628
171
#if (defined(PS_DEC) || defined(DRM_PS))
629
171
            if (hInfo->num_front_channels == 1 &&
630
79
                hInfo->num_side_channels == 0 &&
631
47
                hInfo->num_back_channels == 0 &&
632
36
                hInfo->num_lfe_channels == 0)
633
27
            {
634
                /* When PS is enabled output is always stereo */
635
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
27
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
171
            chdir--;
641
171
        }
642
2.27k
        for (i = 0; i < chdir; i++)
643
1.86k
        {
644
1.86k
            hInfo->channel_position[chpos++] =
645
1.86k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.86k
        }
647
648
2.01k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.60k
        {
650
1.60k
            hInfo->channel_position[chpos++] =
651
1.60k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.60k
        }
653
654
410
        chdir = hInfo->num_back_channels;
655
410
        if (chdir & 1)
656
65
        {
657
65
            back_center = 1;
658
65
            chdir--;
659
65
        }
660
1.33k
        for (i = 0; i < chdir; i++)
661
928
        {
662
928
            hInfo->channel_position[chpos++] =
663
928
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
928
        }
665
410
        if (back_center)
666
65
        {
667
65
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
65
        }
669
670
658
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
248
        {
672
248
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
248
        }
674
675
9.25k
    } else {
676
9.25k
        switch (hDecoder->channelConfiguration)
677
9.25k
        {
678
621
        case 1:
679
621
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
621
            hInfo->num_front_channels = 2;
682
621
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
621
            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
621
            break;
689
4.11k
        case 2:
690
4.11k
            hInfo->num_front_channels = 2;
691
4.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.11k
            break;
694
771
        case 3:
695
771
            hInfo->num_front_channels = 3;
696
771
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
771
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
771
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
771
            break;
700
1.03k
        case 4:
701
1.03k
            hInfo->num_front_channels = 3;
702
1.03k
            hInfo->num_back_channels = 1;
703
1.03k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.03k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.03k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.03k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.03k
            break;
708
700
        case 5:
709
700
            hInfo->num_front_channels = 3;
710
700
            hInfo->num_back_channels = 2;
711
700
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
700
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
700
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
700
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
700
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
700
            break;
717
273
        case 6:
718
273
            hInfo->num_front_channels = 3;
719
273
            hInfo->num_back_channels = 2;
720
273
            hInfo->num_lfe_channels = 1;
721
273
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
273
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
273
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
273
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
273
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
273
            hInfo->channel_position[5] = LFE_CHANNEL;
727
273
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
234
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
234
            {
744
234
                uint8_t i;
745
234
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
234
                if (ch & 1) /* there's either a center front or a center back channel */
747
75
                {
748
75
                    uint8_t ch1 = (ch-1)/2;
749
75
                    if (hDecoder->first_syn_ele == ID_SCE)
750
50
                    {
751
50
                        hInfo->num_front_channels = ch1 + 1;
752
50
                        hInfo->num_back_channels = ch1;
753
50
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
251
                        for (i = 1; i <= ch1; i+=2)
755
201
                        {
756
201
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
201
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
201
                        }
759
251
                        for (i = ch1+1; i < ch; i+=2)
760
201
                        {
761
201
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
201
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
201
                        }
764
50
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
152
                        for (i = 0; i < ch1; i+=2)
768
127
                        {
769
127
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
127
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
127
                        }
772
152
                        for (i = ch1; i < ch-1; i+=2)
773
127
                        {
774
127
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
127
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
127
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
159
                } else {
780
159
                    uint8_t ch1 = (ch)/2;
781
159
                    hInfo->num_front_channels = ch1;
782
159
                    hInfo->num_back_channels = ch1;
783
159
                    if (ch1 & 1)
784
77
                    {
785
77
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
458
                        for (i = 1; i <= ch1; i+=2)
787
381
                        {
788
381
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
381
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
381
                        }
791
381
                        for (i = ch1+1; i < ch-1; i+=2)
792
304
                        {
793
304
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
304
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
304
                        }
796
77
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
82
                    } else {
798
694
                        for (i = 0; i < ch1; i+=2)
799
612
                        {
800
612
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
612
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
612
                        }
803
694
                        for (i = ch1; i < ch; i+=2)
804
612
                        {
805
612
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
612
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
612
                        }
808
82
                    }
809
159
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
455
                for (i = ch; i < hDecoder->fr_channels; i++)
812
221
                {
813
221
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
221
                }
815
234
            }
816
234
            break;
817
9.25k
        }
818
9.25k
    }
819
9.66k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.8k
{
600
10.8k
    hInfo->num_front_channels = 0;
601
10.8k
    hInfo->num_side_channels = 0;
602
10.8k
    hInfo->num_back_channels = 0;
603
10.8k
    hInfo->num_lfe_channels = 0;
604
10.8k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.8k
    if (hDecoder->downMatrix)
607
1.16k
    {
608
1.16k
        hInfo->num_front_channels = 2;
609
1.16k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.16k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.16k
        return;
612
1.16k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.66k
    if (hDecoder->pce_set)
617
410
    {
618
410
        uint8_t i, chpos = 0;
619
410
        uint8_t chdir, back_center = 0;
620
621
410
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
410
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
410
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
410
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
410
        chdir = hInfo->num_front_channels;
626
410
        if (chdir & 1)
627
171
        {
628
171
#if (defined(PS_DEC) || defined(DRM_PS))
629
171
            if (hInfo->num_front_channels == 1 &&
630
79
                hInfo->num_side_channels == 0 &&
631
47
                hInfo->num_back_channels == 0 &&
632
36
                hInfo->num_lfe_channels == 0)
633
27
            {
634
                /* When PS is enabled output is always stereo */
635
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
27
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
171
            chdir--;
641
171
        }
642
2.27k
        for (i = 0; i < chdir; i++)
643
1.86k
        {
644
1.86k
            hInfo->channel_position[chpos++] =
645
1.86k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.86k
        }
647
648
2.01k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.60k
        {
650
1.60k
            hInfo->channel_position[chpos++] =
651
1.60k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.60k
        }
653
654
410
        chdir = hInfo->num_back_channels;
655
410
        if (chdir & 1)
656
65
        {
657
65
            back_center = 1;
658
65
            chdir--;
659
65
        }
660
1.33k
        for (i = 0; i < chdir; i++)
661
928
        {
662
928
            hInfo->channel_position[chpos++] =
663
928
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
928
        }
665
410
        if (back_center)
666
65
        {
667
65
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
65
        }
669
670
658
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
248
        {
672
248
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
248
        }
674
675
9.25k
    } else {
676
9.25k
        switch (hDecoder->channelConfiguration)
677
9.25k
        {
678
621
        case 1:
679
621
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
621
            hInfo->num_front_channels = 2;
682
621
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
621
            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
621
            break;
689
4.11k
        case 2:
690
4.11k
            hInfo->num_front_channels = 2;
691
4.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.11k
            break;
694
771
        case 3:
695
771
            hInfo->num_front_channels = 3;
696
771
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
771
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
771
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
771
            break;
700
1.03k
        case 4:
701
1.03k
            hInfo->num_front_channels = 3;
702
1.03k
            hInfo->num_back_channels = 1;
703
1.03k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.03k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.03k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.03k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.03k
            break;
708
700
        case 5:
709
700
            hInfo->num_front_channels = 3;
710
700
            hInfo->num_back_channels = 2;
711
700
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
700
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
700
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
700
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
700
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
700
            break;
717
273
        case 6:
718
273
            hInfo->num_front_channels = 3;
719
273
            hInfo->num_back_channels = 2;
720
273
            hInfo->num_lfe_channels = 1;
721
273
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
273
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
273
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
273
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
273
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
273
            hInfo->channel_position[5] = LFE_CHANNEL;
727
273
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
234
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
234
            {
744
234
                uint8_t i;
745
234
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
234
                if (ch & 1) /* there's either a center front or a center back channel */
747
75
                {
748
75
                    uint8_t ch1 = (ch-1)/2;
749
75
                    if (hDecoder->first_syn_ele == ID_SCE)
750
50
                    {
751
50
                        hInfo->num_front_channels = ch1 + 1;
752
50
                        hInfo->num_back_channels = ch1;
753
50
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
251
                        for (i = 1; i <= ch1; i+=2)
755
201
                        {
756
201
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
201
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
201
                        }
759
251
                        for (i = ch1+1; i < ch; i+=2)
760
201
                        {
761
201
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
201
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
201
                        }
764
50
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
152
                        for (i = 0; i < ch1; i+=2)
768
127
                        {
769
127
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
127
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
127
                        }
772
152
                        for (i = ch1; i < ch-1; i+=2)
773
127
                        {
774
127
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
127
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
127
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
159
                } else {
780
159
                    uint8_t ch1 = (ch)/2;
781
159
                    hInfo->num_front_channels = ch1;
782
159
                    hInfo->num_back_channels = ch1;
783
159
                    if (ch1 & 1)
784
77
                    {
785
77
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
458
                        for (i = 1; i <= ch1; i+=2)
787
381
                        {
788
381
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
381
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
381
                        }
791
381
                        for (i = ch1+1; i < ch-1; i+=2)
792
304
                        {
793
304
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
304
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
304
                        }
796
77
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
82
                    } else {
798
694
                        for (i = 0; i < ch1; i+=2)
799
612
                        {
800
612
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
612
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
612
                        }
803
694
                        for (i = ch1; i < ch; i+=2)
804
612
                        {
805
612
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
612
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
612
                        }
808
82
                    }
809
159
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
455
                for (i = ch; i < hDecoder->fr_channels; i++)
812
221
                {
813
221
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
221
                }
815
234
            }
816
234
            break;
817
9.25k
        }
818
9.25k
    }
819
9.66k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.8k
{
600
10.8k
    hInfo->num_front_channels = 0;
601
10.8k
    hInfo->num_side_channels = 0;
602
10.8k
    hInfo->num_back_channels = 0;
603
10.8k
    hInfo->num_lfe_channels = 0;
604
10.8k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.8k
    if (hDecoder->downMatrix)
607
1.16k
    {
608
1.16k
        hInfo->num_front_channels = 2;
609
1.16k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.16k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.16k
        return;
612
1.16k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.66k
    if (hDecoder->pce_set)
617
410
    {
618
410
        uint8_t i, chpos = 0;
619
410
        uint8_t chdir, back_center = 0;
620
621
410
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
410
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
410
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
410
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
410
        chdir = hInfo->num_front_channels;
626
410
        if (chdir & 1)
627
171
        {
628
171
#if (defined(PS_DEC) || defined(DRM_PS))
629
171
            if (hInfo->num_front_channels == 1 &&
630
79
                hInfo->num_side_channels == 0 &&
631
47
                hInfo->num_back_channels == 0 &&
632
36
                hInfo->num_lfe_channels == 0)
633
27
            {
634
                /* When PS is enabled output is always stereo */
635
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
27
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
171
            chdir--;
641
171
        }
642
2.27k
        for (i = 0; i < chdir; i++)
643
1.86k
        {
644
1.86k
            hInfo->channel_position[chpos++] =
645
1.86k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.86k
        }
647
648
2.01k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.60k
        {
650
1.60k
            hInfo->channel_position[chpos++] =
651
1.60k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.60k
        }
653
654
410
        chdir = hInfo->num_back_channels;
655
410
        if (chdir & 1)
656
65
        {
657
65
            back_center = 1;
658
65
            chdir--;
659
65
        }
660
1.33k
        for (i = 0; i < chdir; i++)
661
928
        {
662
928
            hInfo->channel_position[chpos++] =
663
928
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
928
        }
665
410
        if (back_center)
666
65
        {
667
65
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
65
        }
669
670
658
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
248
        {
672
248
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
248
        }
674
675
9.25k
    } else {
676
9.25k
        switch (hDecoder->channelConfiguration)
677
9.25k
        {
678
621
        case 1:
679
621
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
621
            hInfo->num_front_channels = 2;
682
621
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
621
            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
621
            break;
689
4.11k
        case 2:
690
4.11k
            hInfo->num_front_channels = 2;
691
4.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.11k
            break;
694
771
        case 3:
695
771
            hInfo->num_front_channels = 3;
696
771
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
771
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
771
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
771
            break;
700
1.03k
        case 4:
701
1.03k
            hInfo->num_front_channels = 3;
702
1.03k
            hInfo->num_back_channels = 1;
703
1.03k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.03k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.03k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.03k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.03k
            break;
708
700
        case 5:
709
700
            hInfo->num_front_channels = 3;
710
700
            hInfo->num_back_channels = 2;
711
700
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
700
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
700
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
700
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
700
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
700
            break;
717
273
        case 6:
718
273
            hInfo->num_front_channels = 3;
719
273
            hInfo->num_back_channels = 2;
720
273
            hInfo->num_lfe_channels = 1;
721
273
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
273
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
273
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
273
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
273
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
273
            hInfo->channel_position[5] = LFE_CHANNEL;
727
273
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
234
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
234
            {
744
234
                uint8_t i;
745
234
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
234
                if (ch & 1) /* there's either a center front or a center back channel */
747
75
                {
748
75
                    uint8_t ch1 = (ch-1)/2;
749
75
                    if (hDecoder->first_syn_ele == ID_SCE)
750
50
                    {
751
50
                        hInfo->num_front_channels = ch1 + 1;
752
50
                        hInfo->num_back_channels = ch1;
753
50
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
251
                        for (i = 1; i <= ch1; i+=2)
755
201
                        {
756
201
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
201
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
201
                        }
759
251
                        for (i = ch1+1; i < ch; i+=2)
760
201
                        {
761
201
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
201
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
201
                        }
764
50
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
152
                        for (i = 0; i < ch1; i+=2)
768
127
                        {
769
127
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
127
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
127
                        }
772
152
                        for (i = ch1; i < ch-1; i+=2)
773
127
                        {
774
127
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
127
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
127
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
159
                } else {
780
159
                    uint8_t ch1 = (ch)/2;
781
159
                    hInfo->num_front_channels = ch1;
782
159
                    hInfo->num_back_channels = ch1;
783
159
                    if (ch1 & 1)
784
77
                    {
785
77
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
458
                        for (i = 1; i <= ch1; i+=2)
787
381
                        {
788
381
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
381
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
381
                        }
791
381
                        for (i = ch1+1; i < ch-1; i+=2)
792
304
                        {
793
304
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
304
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
304
                        }
796
77
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
82
                    } else {
798
694
                        for (i = 0; i < ch1; i+=2)
799
612
                        {
800
612
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
612
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
612
                        }
803
694
                        for (i = ch1; i < ch; i+=2)
804
612
                        {
805
612
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
612
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
612
                        }
808
82
                    }
809
159
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
455
                for (i = ch; i < hDecoder->fr_channels; i++)
812
221
                {
813
221
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
221
                }
815
234
            }
816
234
            break;
817
9.25k
        }
818
9.25k
    }
819
9.66k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.8k
{
600
10.8k
    hInfo->num_front_channels = 0;
601
10.8k
    hInfo->num_side_channels = 0;
602
10.8k
    hInfo->num_back_channels = 0;
603
10.8k
    hInfo->num_lfe_channels = 0;
604
10.8k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.8k
    if (hDecoder->downMatrix)
607
1.16k
    {
608
1.16k
        hInfo->num_front_channels = 2;
609
1.16k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.16k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.16k
        return;
612
1.16k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.66k
    if (hDecoder->pce_set)
617
410
    {
618
410
        uint8_t i, chpos = 0;
619
410
        uint8_t chdir, back_center = 0;
620
621
410
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
410
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
410
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
410
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
410
        chdir = hInfo->num_front_channels;
626
410
        if (chdir & 1)
627
171
        {
628
171
#if (defined(PS_DEC) || defined(DRM_PS))
629
171
            if (hInfo->num_front_channels == 1 &&
630
79
                hInfo->num_side_channels == 0 &&
631
47
                hInfo->num_back_channels == 0 &&
632
36
                hInfo->num_lfe_channels == 0)
633
27
            {
634
                /* When PS is enabled output is always stereo */
635
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
27
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
27
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
171
            chdir--;
641
171
        }
642
2.27k
        for (i = 0; i < chdir; i++)
643
1.86k
        {
644
1.86k
            hInfo->channel_position[chpos++] =
645
1.86k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.86k
        }
647
648
2.01k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.60k
        {
650
1.60k
            hInfo->channel_position[chpos++] =
651
1.60k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.60k
        }
653
654
410
        chdir = hInfo->num_back_channels;
655
410
        if (chdir & 1)
656
65
        {
657
65
            back_center = 1;
658
65
            chdir--;
659
65
        }
660
1.33k
        for (i = 0; i < chdir; i++)
661
928
        {
662
928
            hInfo->channel_position[chpos++] =
663
928
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
928
        }
665
410
        if (back_center)
666
65
        {
667
65
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
65
        }
669
670
658
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
248
        {
672
248
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
248
        }
674
675
9.25k
    } else {
676
9.25k
        switch (hDecoder->channelConfiguration)
677
9.25k
        {
678
621
        case 1:
679
621
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
621
            hInfo->num_front_channels = 2;
682
621
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
621
            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
621
            break;
689
4.11k
        case 2:
690
4.11k
            hInfo->num_front_channels = 2;
691
4.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.11k
            break;
694
771
        case 3:
695
771
            hInfo->num_front_channels = 3;
696
771
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
771
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
771
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
771
            break;
700
1.03k
        case 4:
701
1.03k
            hInfo->num_front_channels = 3;
702
1.03k
            hInfo->num_back_channels = 1;
703
1.03k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.03k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.03k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.03k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.03k
            break;
708
700
        case 5:
709
700
            hInfo->num_front_channels = 3;
710
700
            hInfo->num_back_channels = 2;
711
700
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
700
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
700
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
700
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
700
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
700
            break;
717
273
        case 6:
718
273
            hInfo->num_front_channels = 3;
719
273
            hInfo->num_back_channels = 2;
720
273
            hInfo->num_lfe_channels = 1;
721
273
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
273
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
273
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
273
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
273
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
273
            hInfo->channel_position[5] = LFE_CHANNEL;
727
273
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
234
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
234
            {
744
234
                uint8_t i;
745
234
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
234
                if (ch & 1) /* there's either a center front or a center back channel */
747
75
                {
748
75
                    uint8_t ch1 = (ch-1)/2;
749
75
                    if (hDecoder->first_syn_ele == ID_SCE)
750
50
                    {
751
50
                        hInfo->num_front_channels = ch1 + 1;
752
50
                        hInfo->num_back_channels = ch1;
753
50
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
251
                        for (i = 1; i <= ch1; i+=2)
755
201
                        {
756
201
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
201
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
201
                        }
759
251
                        for (i = ch1+1; i < ch; i+=2)
760
201
                        {
761
201
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
201
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
201
                        }
764
50
                    } else {
765
25
                        hInfo->num_front_channels = ch1;
766
25
                        hInfo->num_back_channels = ch1 + 1;
767
152
                        for (i = 0; i < ch1; i+=2)
768
127
                        {
769
127
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
127
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
127
                        }
772
152
                        for (i = ch1; i < ch-1; i+=2)
773
127
                        {
774
127
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
127
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
127
                        }
777
25
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
25
                    }
779
159
                } else {
780
159
                    uint8_t ch1 = (ch)/2;
781
159
                    hInfo->num_front_channels = ch1;
782
159
                    hInfo->num_back_channels = ch1;
783
159
                    if (ch1 & 1)
784
77
                    {
785
77
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
458
                        for (i = 1; i <= ch1; i+=2)
787
381
                        {
788
381
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
381
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
381
                        }
791
381
                        for (i = ch1+1; i < ch-1; i+=2)
792
304
                        {
793
304
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
304
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
304
                        }
796
77
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
82
                    } else {
798
694
                        for (i = 0; i < ch1; i+=2)
799
612
                        {
800
612
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
612
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
612
                        }
803
694
                        for (i = ch1; i < ch; i+=2)
804
612
                        {
805
612
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
612
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
612
                        }
808
82
                    }
809
159
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
455
                for (i = ch; i < hDecoder->fr_channels; i++)
812
221
                {
813
221
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
221
                }
815
234
            }
816
234
            break;
817
9.25k
        }
818
9.25k
    }
819
9.66k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
42.8k
{
826
42.8k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
42.8k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
42.8k
}
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.0k
{
837
12.0k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
12.0k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
134
    {
840
134
        hInfo->error = 27;
841
134
        return NULL;
842
134
    }
843
844
11.8k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
11.8k
        sample_buffer, sample_buffer_size);
846
12.0k
}
847
848
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
849
                              NeAACDecFrameInfo *hInfo,
850
                              unsigned char *buffer,
851
                              unsigned long buffer_size,
852
                              void **sample_buffer2,
853
                              unsigned long sample_buffer_size)
854
54.7k
{
855
54.7k
    uint16_t i;
856
54.7k
    uint8_t channels = 0;
857
54.7k
    uint8_t output_channels = 0;
858
54.7k
    bitfile ld;
859
54.7k
    uint32_t bitsconsumed;
860
54.7k
    uint16_t frame_len;
861
54.7k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
54.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
54.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
54.7k
    frame_len = hDecoder->frameLength;
882
883
884
54.7k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
54.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
54.7k
    if (buffer_size >= 128)
903
6.33k
    {
904
6.33k
        if (memcmp(buffer, "TAG", 3) == 0)
905
12
        {
906
            /* found it */
907
12
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
12
            return NULL;
910
12
        }
911
6.33k
    }
912
913
914
    /* initialize the bitstream */
915
54.7k
    faad_initbits(&ld, buffer, buffer_size);
916
54.7k
    if (ld.error != 0)
917
21.5k
        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
18.0k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.49k
    {
953
        /* We do not support stereo right now */
954
1.49k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.49k
        faad_getbits(&ld, 8
961
1.49k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.49k
    }
963
18.0k
#endif
964
965
33.2k
    if (hDecoder->adts_header_present)
966
807
    {
967
807
        adts_header adts;
968
969
807
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
807
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
95
            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
807
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
18.0k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.49k
    {
987
1.49k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
16.5k
    } else {
989
16.5k
#endif
990
15.1k
        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
15.1k
    channels = hDecoder->fr_channels;
1009
1010
33.1k
    if (hInfo->error > 0)
1011
22.1k
        goto error;
1012
1013
    /* safety check */
1014
10.9k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
151
    {
1016
        /* invalid number of channels */
1017
151
        hInfo->error = 12;
1018
151
        goto error;
1019
151
    }
1020
1021
    /* no more bit reading after this */
1022
10.8k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
10.8k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
10.8k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
10.8k
    faad_endbits(&ld);
1030
1031
1032
10.8k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
10.8k
        )
1037
10.6k
    {
1038
10.6k
        if (hDecoder->channelConfiguration == 0)
1039
351
            hDecoder->channelConfiguration = channels;
1040
1041
10.6k
        if (channels == 8) /* 7.1 */
1042
861
            hDecoder->channelConfiguration = 7;
1043
10.6k
        if (channels == 7) /* not a standard channelConfiguration */
1044
46
            hDecoder->channelConfiguration = 0;
1045
10.6k
    }
1046
1047
10.8k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.15k
    {
1049
1.15k
        hDecoder->downMatrix = 1;
1050
1.15k
        output_channels = 2;
1051
9.68k
    } else {
1052
9.68k
        output_channels = channels;
1053
9.68k
    }
1054
1055
10.8k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
10.8k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
10.8k
    if (output_channels == 1)
1059
195
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
195
        hDecoder->upMatrix = 1;
1062
195
        output_channels = 2;
1063
195
    }
1064
10.8k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
10.8k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
422
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
422
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
12
        {
1072
12
            hInfo->error = 22;
1073
12
            return NULL;
1074
12
        }
1075
422
    }
1076
10.8k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
10.8k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
10.8k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
10.8k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
10.8k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
10.8k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
10.8k
    hInfo->header_type = RAW;
1090
10.8k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
10.8k
    if (hDecoder->adts_header_present)
1093
231
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
10.8k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
10.8k
    hInfo->ps = hDecoder->ps_used_global;
1100
10.8k
#endif
1101
1102
    /* check if frame has channel elements */
1103
10.8k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
10.8k
    {
1110
10.8k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
10.8k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
10.8k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
10.8k
        };
1114
10.8k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
10.8k
#ifdef SBR_DEC
1116
10.8k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
6.19k
        {
1118
6.19k
            stride = 2 * stride;
1119
6.19k
        }
1120
10.8k
#endif
1121
10.8k
        required_buffer_size = frame_len*output_channels*stride;
1122
10.8k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
10.8k
    if (sample_buffer_size == 0)
1126
7.54k
    {
1127
        /* allocate the buffer for the final samples */
1128
7.54k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
6.35k
        {
1130
6.35k
            if (hDecoder->sample_buffer)
1131
102
                faad_free(hDecoder->sample_buffer);
1132
6.35k
            hDecoder->sample_buffer = NULL;
1133
6.35k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
6.35k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
6.35k
        }
1136
7.54k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.20k
        hInfo->error = 27;
1139
1.20k
        return NULL;
1140
1.20k
    }
1141
1142
9.62k
    if (sample_buffer_size == 0)
1143
7.54k
    {
1144
7.54k
        sample_buffer = hDecoder->sample_buffer;
1145
7.54k
    } else {
1146
2.08k
        sample_buffer = *sample_buffer2;
1147
2.08k
    }
1148
1149
9.62k
#ifdef SBR_DEC
1150
9.62k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
6.62k
    {
1152
6.62k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
6.62k
        if (!hDecoder->downSampledSBR)
1156
5.29k
        {
1157
5.29k
            frame_len *= 2;
1158
5.29k
            hInfo->samples *= 2;
1159
5.29k
            hInfo->samplerate *= 2;
1160
5.29k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
33.2k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
26.6k
        {
1165
26.6k
            if (hDecoder->sbr[ele] == NULL)
1166
13
            {
1167
13
                hInfo->error = 25;
1168
13
                goto error;
1169
13
            }
1170
26.6k
        }
1171
1172
        /* sbr */
1173
6.61k
        if (hDecoder->sbr_present_flag == 1)
1174
4.42k
        {
1175
4.42k
            hInfo->object_type = HE_AAC;
1176
4.42k
            hInfo->sbr = SBR_UPSAMPLED;
1177
4.42k
        } else {
1178
2.18k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.18k
        }
1180
6.61k
        if (hDecoder->downSampledSBR)
1181
1.31k
        {
1182
1.31k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.31k
        }
1184
6.61k
    }
1185
9.61k
#endif
1186
1187
1188
9.61k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
9.61k
        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
9.61k
    hDecoder->postSeekResetFlag = 0;
1198
1199
9.61k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
4.38k
    if (hDecoder->object_type != LD)
1202
3.80k
    {
1203
3.80k
#endif
1204
9.04k
        if (hDecoder->frame <= 1)
1205
3.39k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
571
    } else {
1208
        /* LD encoders will give lower delay */
1209
571
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
571
    }
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
9.61k
    return sample_buffer;
1225
1226
22.3k
error:
1227
1228
    /* reset filterbank state */
1229
1.45M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.43M
    {
1231
1.43M
        if (hDecoder->fb_intermed[i] != NULL)
1232
640k
        {
1233
640k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
640k
        }
1235
1.43M
    }
1236
22.3k
#ifdef SBR_DEC
1237
1.09M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
1.07M
    {
1239
1.07M
        if (hDecoder->sbr[i] != NULL)
1240
466k
        {
1241
466k
            sbrReset(hDecoder->sbr[i]);
1242
466k
        }
1243
1.07M
    }
1244
22.3k
#endif
1245
1246
1247
22.3k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
9.62k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
30.0k
{
855
30.0k
    uint16_t i;
856
30.0k
    uint8_t channels = 0;
857
30.0k
    uint8_t output_channels = 0;
858
30.0k
    bitfile ld;
859
30.0k
    uint32_t bitsconsumed;
860
30.0k
    uint16_t frame_len;
861
30.0k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
30.0k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
30.0k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
30.0k
    frame_len = hDecoder->frameLength;
882
883
884
30.0k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
30.0k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
30.0k
    if (buffer_size >= 128)
903
2.86k
    {
904
2.86k
        if (memcmp(buffer, "TAG", 3) == 0)
905
6
        {
906
            /* found it */
907
6
            hInfo->bytesconsumed = 128; /* 128 bytes fixed size */
908
            /* no error, but no output either */
909
6
            return NULL;
910
6
        }
911
2.86k
    }
912
913
914
    /* initialize the bitstream */
915
30.0k
    faad_initbits(&ld, buffer, buffer_size);
916
30.0k
    if (ld.error != 0)
917
12.0k
        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
18.0k
#ifdef DRM
951
18.0k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.49k
    {
953
        /* We do not support stereo right now */
954
1.49k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.49k
        faad_getbits(&ld, 8
961
1.49k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.49k
    }
963
18.0k
#endif
964
965
18.0k
    if (hDecoder->adts_header_present)
966
160
    {
967
160
        adts_header adts;
968
969
160
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
160
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
11
            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
160
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
18.0k
#ifdef DRM
985
18.0k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.49k
    {
987
1.49k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
16.5k
    } else {
989
16.5k
#endif
990
16.5k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
16.5k
#ifdef DRM
992
16.5k
    }
993
18.0k
#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
18.0k
    channels = hDecoder->fr_channels;
1009
1010
18.0k
    if (hInfo->error > 0)
1011
11.9k
        goto error;
1012
1013
    /* safety check */
1014
6.04k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
71
    {
1016
        /* invalid number of channels */
1017
71
        hInfo->error = 12;
1018
71
        goto error;
1019
71
    }
1020
1021
    /* no more bit reading after this */
1022
5.97k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
5.97k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
5.97k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
5.97k
    faad_endbits(&ld);
1030
1031
1032
5.97k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
5.97k
        )
1037
5.91k
    {
1038
5.91k
        if (hDecoder->channelConfiguration == 0)
1039
214
            hDecoder->channelConfiguration = channels;
1040
1041
5.91k
        if (channels == 8) /* 7.1 */
1042
220
            hDecoder->channelConfiguration = 7;
1043
5.91k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
5.91k
    }
1046
1047
5.97k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
718
    {
1049
718
        hDecoder->downMatrix = 1;
1050
718
        output_channels = 2;
1051
5.25k
    } else {
1052
5.25k
        output_channels = channels;
1053
5.25k
    }
1054
1055
5.97k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
5.97k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
5.97k
    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
5.97k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
5.97k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
267
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
267
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
267
    }
1076
5.96k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
5.96k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
5.96k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
5.96k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
5.96k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
5.96k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
5.96k
    hInfo->header_type = RAW;
1090
5.96k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
5.96k
    if (hDecoder->adts_header_present)
1093
59
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
5.96k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
5.96k
    hInfo->ps = hDecoder->ps_used_global;
1100
5.96k
#endif
1101
1102
    /* check if frame has channel elements */
1103
5.96k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
5.96k
    {
1110
5.96k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
5.96k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
5.96k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
5.96k
        };
1114
5.96k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
5.96k
#ifdef SBR_DEC
1116
5.96k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.33k
        {
1118
3.33k
            stride = 2 * stride;
1119
3.33k
        }
1120
5.96k
#endif
1121
5.96k
        required_buffer_size = frame_len*output_channels*stride;
1122
5.96k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
5.96k
    if (sample_buffer_size == 0)
1126
3.90k
    {
1127
        /* allocate the buffer for the final samples */
1128
3.90k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
3.28k
        {
1130
3.28k
            if (hDecoder->sample_buffer)
1131
58
                faad_free(hDecoder->sample_buffer);
1132
3.28k
            hDecoder->sample_buffer = NULL;
1133
3.28k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
3.28k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
3.28k
        }
1136
3.90k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
728
        hInfo->error = 27;
1139
728
        return NULL;
1140
728
    }
1141
1142
5.23k
    if (sample_buffer_size == 0)
1143
3.90k
    {
1144
3.90k
        sample_buffer = hDecoder->sample_buffer;
1145
3.90k
    } else {
1146
1.33k
        sample_buffer = *sample_buffer2;
1147
1.33k
    }
1148
1149
5.23k
#ifdef SBR_DEC
1150
5.23k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.52k
    {
1152
3.52k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.52k
        if (!hDecoder->downSampledSBR)
1156
2.78k
        {
1157
2.78k
            frame_len *= 2;
1158
2.78k
            hInfo->samples *= 2;
1159
2.78k
            hInfo->samplerate *= 2;
1160
2.78k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
16.0k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
12.5k
        {
1165
12.5k
            if (hDecoder->sbr[ele] == NULL)
1166
6
            {
1167
6
                hInfo->error = 25;
1168
6
                goto error;
1169
6
            }
1170
12.5k
        }
1171
1172
        /* sbr */
1173
3.52k
        if (hDecoder->sbr_present_flag == 1)
1174
2.54k
        {
1175
2.54k
            hInfo->object_type = HE_AAC;
1176
2.54k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.54k
        } else {
1178
983
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
983
        }
1180
3.52k
        if (hDecoder->downSampledSBR)
1181
743
        {
1182
743
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
743
        }
1184
3.52k
    }
1185
5.23k
#endif
1186
1187
1188
5.23k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
5.23k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
5.23k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
5.23k
#endif
1195
1196
1197
5.23k
    hDecoder->postSeekResetFlag = 0;
1198
1199
5.23k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
5.23k
        if (hDecoder->frame <= 1)
1205
1.86k
            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
5.23k
    return sample_buffer;
1225
1226
12.0k
error:
1227
1228
    /* reset filterbank state */
1229
784k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
772k
    {
1231
772k
        if (hDecoder->fb_intermed[i] != NULL)
1232
272k
        {
1233
272k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
272k
        }
1235
772k
    }
1236
12.0k
#ifdef SBR_DEC
1237
591k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
579k
    {
1239
579k
        if (hDecoder->sbr[i] != NULL)
1240
209k
        {
1241
209k
            sbrReset(hDecoder->sbr[i]);
1242
209k
        }
1243
579k
    }
1244
12.0k
#endif
1245
1246
1247
12.0k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
5.23k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
24.6k
{
855
24.6k
    uint16_t i;
856
24.6k
    uint8_t channels = 0;
857
24.6k
    uint8_t output_channels = 0;
858
24.6k
    bitfile ld;
859
24.6k
    uint32_t bitsconsumed;
860
24.6k
    uint16_t frame_len;
861
24.6k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
24.6k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
24.6k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
24.6k
    frame_len = hDecoder->frameLength;
882
883
884
24.6k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
24.6k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
24.6k
    if (buffer_size >= 128)
903
3.46k
    {
904
3.46k
        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.46k
    }
912
913
914
    /* initialize the bitstream */
915
24.6k
    faad_initbits(&ld, buffer, buffer_size);
916
24.6k
    if (ld.error != 0)
917
9.49k
        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
15.1k
    if (hDecoder->adts_header_present)
966
647
    {
967
647
        adts_header adts;
968
969
647
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
647
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
84
            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
647
    }
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
15.1k
        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
15.1k
    channels = hDecoder->fr_channels;
1009
1010
15.1k
    if (hInfo->error > 0)
1011
10.1k
        goto error;
1012
1013
    /* safety check */
1014
4.94k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
80
    {
1016
        /* invalid number of channels */
1017
80
        hInfo->error = 12;
1018
80
        goto error;
1019
80
    }
1020
1021
    /* no more bit reading after this */
1022
4.86k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
4.86k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
4.86k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
4.86k
    faad_endbits(&ld);
1030
1031
1032
4.86k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
4.86k
        )
1037
4.69k
    {
1038
4.69k
        if (hDecoder->channelConfiguration == 0)
1039
137
            hDecoder->channelConfiguration = channels;
1040
1041
4.69k
        if (channels == 8) /* 7.1 */
1042
641
            hDecoder->channelConfiguration = 7;
1043
4.69k
        if (channels == 7) /* not a standard channelConfiguration */
1044
46
            hDecoder->channelConfiguration = 0;
1045
4.69k
    }
1046
1047
4.86k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
436
    {
1049
436
        hDecoder->downMatrix = 1;
1050
436
        output_channels = 2;
1051
4.42k
    } else {
1052
4.42k
        output_channels = channels;
1053
4.42k
    }
1054
1055
4.86k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
4.86k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
4.86k
    if (output_channels == 1)
1059
195
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
195
        hDecoder->upMatrix = 1;
1062
195
        output_channels = 2;
1063
195
    }
1064
4.86k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
4.86k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
155
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
155
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
155
    }
1076
4.85k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
4.85k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
4.85k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
4.85k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
4.85k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
4.85k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
4.85k
    hInfo->header_type = RAW;
1090
4.85k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
4.85k
    if (hDecoder->adts_header_present)
1093
172
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
4.85k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
4.85k
    hInfo->ps = hDecoder->ps_used_global;
1100
4.85k
#endif
1101
1102
    /* check if frame has channel elements */
1103
4.85k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
4.85k
    {
1110
4.85k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
4.85k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
4.85k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
4.85k
        };
1114
4.85k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
4.85k
#ifdef SBR_DEC
1116
4.85k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
2.86k
        {
1118
2.86k
            stride = 2 * stride;
1119
2.86k
        }
1120
4.85k
#endif
1121
4.85k
        required_buffer_size = frame_len*output_channels*stride;
1122
4.85k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
4.85k
    if (sample_buffer_size == 0)
1126
3.63k
    {
1127
        /* allocate the buffer for the final samples */
1128
3.63k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
3.07k
        {
1130
3.07k
            if (hDecoder->sample_buffer)
1131
44
                faad_free(hDecoder->sample_buffer);
1132
3.07k
            hDecoder->sample_buffer = NULL;
1133
3.07k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
3.07k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
3.07k
        }
1136
3.63k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
472
        hInfo->error = 27;
1139
472
        return NULL;
1140
472
    }
1141
1142
4.38k
    if (sample_buffer_size == 0)
1143
3.63k
    {
1144
3.63k
        sample_buffer = hDecoder->sample_buffer;
1145
3.63k
    } else {
1146
750
        sample_buffer = *sample_buffer2;
1147
750
    }
1148
1149
4.38k
#ifdef SBR_DEC
1150
4.38k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.09k
    {
1152
3.09k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.09k
        if (!hDecoder->downSampledSBR)
1156
2.51k
        {
1157
2.51k
            frame_len *= 2;
1158
2.51k
            hInfo->samples *= 2;
1159
2.51k
            hInfo->samplerate *= 2;
1160
2.51k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
17.2k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
14.1k
        {
1165
14.1k
            if (hDecoder->sbr[ele] == NULL)
1166
7
            {
1167
7
                hInfo->error = 25;
1168
7
                goto error;
1169
7
            }
1170
14.1k
        }
1171
1172
        /* sbr */
1173
3.08k
        if (hDecoder->sbr_present_flag == 1)
1174
1.88k
        {
1175
1.88k
            hInfo->object_type = HE_AAC;
1176
1.88k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.88k
        } else {
1178
1.20k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.20k
        }
1180
3.08k
        if (hDecoder->downSampledSBR)
1181
571
        {
1182
571
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
571
        }
1184
3.08k
    }
1185
4.38k
#endif
1186
1187
1188
4.38k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
4.38k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
4.38k
    hDecoder->postSeekResetFlag = 0;
1198
1199
4.38k
    hDecoder->frame++;
1200
4.38k
#ifdef LD_DEC
1201
4.38k
    if (hDecoder->object_type != LD)
1202
3.80k
    {
1203
3.80k
#endif
1204
3.80k
        if (hDecoder->frame <= 1)
1205
1.53k
            hInfo->samples = 0;
1206
3.80k
#ifdef LD_DEC
1207
3.80k
    } else {
1208
        /* LD encoders will give lower delay */
1209
571
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
571
    }
1212
4.38k
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
4.38k
    return sample_buffer;
1225
1226
10.3k
error:
1227
1228
    /* reset filterbank state */
1229
671k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
660k
    {
1231
660k
        if (hDecoder->fb_intermed[i] != NULL)
1232
368k
        {
1233
368k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
368k
        }
1235
660k
    }
1236
10.3k
#ifdef SBR_DEC
1237
505k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
495k
    {
1239
495k
        if (hDecoder->sbr[i] != NULL)
1240
257k
        {
1241
257k
            sbrReset(hDecoder->sbr[i]);
1242
257k
        }
1243
495k
    }
1244
10.3k
#endif
1245
1246
1247
10.3k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
4.38k
}