Coverage Report

Created: 2026-08-31 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/decoder.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: decoder.c,v 1.119 2015/01/24 14:21:05 knik Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
#include <stdio.h>
36
#include <string.h>
37
38
#include "mp4.h"
39
#include "syntax.h"
40
#include "error.h"
41
#include "output.h"
42
#include "filtbank.h"
43
#include "drc.h"
44
#ifdef SBR_DEC
45
#include "sbr_dec.h"
46
#include "sbr_syntax.h"
47
#endif
48
#ifdef SSR_DEC
49
#include "ssr.h"
50
#endif
51
52
#ifdef ANALYSIS
53
uint16_t dbg_count;
54
#endif
55
56
#if !defined(PACKAGE_VERSION)
57
#error "PACKAGE_VERSION must be defined"
58
#endif
59
60
/* static function declarations */
61
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
62
                              NeAACDecFrameInfo *hInfo,
63
                              unsigned char *buffer,
64
                              unsigned long buffer_size,
65
                              void **sample_buffer2,
66
                              unsigned long sample_buffer_size);
67
static void create_channel_config(NeAACDecStruct *hDecoder,
68
                                  NeAACDecFrameInfo *hInfo);
69
70
71
int NeAACDecGetVersion(char **faad_id_string,
72
                                   char **faad_copyright_string)
73
323
{
74
323
    static char *libfaadName = PACKAGE_VERSION;
75
323
    static char *libCopyright =
76
323
        " Copyright 2002-2004: Ahead Software AG\n"
77
323
  " http://www.audiocoding.com\n"
78
323
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
323
    if (faad_id_string)
81
323
        *faad_id_string = libfaadName;
82
83
323
    if (faad_copyright_string)
84
323
        *faad_copyright_string = libCopyright;
85
86
323
    return 0;
87
323
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
323
{
91
323
    if (errcode >= NUM_ERROR_MESSAGES)
92
210
        return NULL;
93
113
    return err_msg[errcode];
94
323
}
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
323
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
646
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
646
    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
646
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.29k
    return cap;
120
1.29k
}
NeAACDecGetCapabilities
Line
Count
Source
97
323
{
98
323
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
323
    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
323
#ifdef ERROR_RESILIENCE
113
323
    cap += ERROR_RESILIENCE_CAP;
114
323
#endif
115
323
#ifdef FIXED_POINT
116
323
    cap += FIXED_POINT_CAP;
117
323
#endif
118
119
323
    return cap;
120
323
}
NeAACDecGetCapabilities
Line
Count
Source
97
323
{
98
323
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
323
    cap += LC_DEC_CAP;
102
103
323
#ifdef MAIN_DEC
104
323
    cap += MAIN_DEC_CAP;
105
323
#endif
106
323
#ifdef LTP_DEC
107
323
    cap += LTP_DEC_CAP;
108
323
#endif
109
323
#ifdef LD_DEC
110
323
    cap += LD_DEC_CAP;
111
323
#endif
112
323
#ifdef ERROR_RESILIENCE
113
323
    cap += ERROR_RESILIENCE_CAP;
114
323
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
323
    return cap;
120
323
}
NeAACDecGetCapabilities
Line
Count
Source
97
323
{
98
323
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
323
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
323
#ifdef LTP_DEC
107
323
    cap += LTP_DEC_CAP;
108
323
#endif
109
323
#ifdef LD_DEC
110
323
    cap += LD_DEC_CAP;
111
323
#endif
112
323
#ifdef ERROR_RESILIENCE
113
323
    cap += ERROR_RESILIENCE_CAP;
114
323
#endif
115
323
#ifdef FIXED_POINT
116
323
    cap += FIXED_POINT_CAP;
117
323
#endif
118
119
323
    return cap;
120
323
}
NeAACDecGetCapabilities
Line
Count
Source
97
323
{
98
323
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
323
    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
323
#ifdef ERROR_RESILIENCE
113
323
    cap += ERROR_RESILIENCE_CAP;
114
323
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
323
    return cap;
120
323
}
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.9k
{
125
59.9k
    uint8_t i;
126
59.9k
    NeAACDecStruct *hDecoder = NULL;
127
128
59.9k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
59.9k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
59.9k
    hDecoder->cmes = mes;
134
59.9k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
59.9k
    hDecoder->config.defObjectType = MAIN;
136
59.9k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
59.9k
    hDecoder->config.downMatrix = 0;
138
59.9k
    hDecoder->adts_header_present = 0;
139
59.9k
    hDecoder->adif_header_present = 0;
140
59.9k
    hDecoder->latm_header_present = 0;
141
59.9k
#ifdef ERROR_RESILIENCE
142
59.9k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
59.9k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
59.9k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
59.9k
#endif
146
59.9k
    hDecoder->frameLength = 1024;
147
148
59.9k
    hDecoder->frame = 0;
149
59.9k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
59.9k
    hDecoder->__r1 = 0x2bb431ea;
153
59.9k
    hDecoder->__r2 = 0x206155b7;
154
155
3.89M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.83M
    {
157
3.83M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.83M
        hDecoder->window_shape_prev[i] = 0;
159
3.83M
        hDecoder->time_out[i] = NULL;
160
3.83M
        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.83M
    }
173
174
59.9k
#ifdef SBR_DEC
175
2.93M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.87M
    {
177
2.87M
        hDecoder->sbr[i] = NULL;
178
2.87M
    }
179
59.9k
#endif
180
181
59.9k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
59.9k
    return hDecoder;
184
59.9k
}
NeAACDecOpen
Line
Count
Source
124
11.7k
{
125
11.7k
    uint8_t i;
126
11.7k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.7k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.7k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.7k
    hDecoder->cmes = mes;
134
11.7k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.7k
    hDecoder->config.defObjectType = MAIN;
136
11.7k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.7k
    hDecoder->config.downMatrix = 0;
138
11.7k
    hDecoder->adts_header_present = 0;
139
11.7k
    hDecoder->adif_header_present = 0;
140
11.7k
    hDecoder->latm_header_present = 0;
141
11.7k
#ifdef ERROR_RESILIENCE
142
11.7k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.7k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.7k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.7k
#endif
146
11.7k
    hDecoder->frameLength = 1024;
147
148
11.7k
    hDecoder->frame = 0;
149
11.7k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.7k
    hDecoder->__r1 = 0x2bb431ea;
153
11.7k
    hDecoder->__r2 = 0x206155b7;
154
155
762k
    for (i = 0; i < MAX_CHANNELS; i++)
156
750k
    {
157
750k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
750k
        hDecoder->window_shape_prev[i] = 0;
159
750k
        hDecoder->time_out[i] = NULL;
160
750k
        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
750k
    }
173
174
11.7k
#ifdef SBR_DEC
175
574k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
562k
    {
177
562k
        hDecoder->sbr[i] = NULL;
178
562k
    }
179
11.7k
#endif
180
181
11.7k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.7k
    return hDecoder;
184
11.7k
}
NeAACDecOpen
Line
Count
Source
124
18.2k
{
125
18.2k
    uint8_t i;
126
18.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
18.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
18.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
18.2k
    hDecoder->cmes = mes;
134
18.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
18.2k
    hDecoder->config.defObjectType = MAIN;
136
18.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
18.2k
    hDecoder->config.downMatrix = 0;
138
18.2k
    hDecoder->adts_header_present = 0;
139
18.2k
    hDecoder->adif_header_present = 0;
140
18.2k
    hDecoder->latm_header_present = 0;
141
18.2k
#ifdef ERROR_RESILIENCE
142
18.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
18.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
18.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
18.2k
#endif
146
18.2k
    hDecoder->frameLength = 1024;
147
148
18.2k
    hDecoder->frame = 0;
149
18.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
18.2k
    hDecoder->__r1 = 0x2bb431ea;
153
18.2k
    hDecoder->__r2 = 0x206155b7;
154
155
1.18M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.16M
    {
157
1.16M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.16M
        hDecoder->window_shape_prev[i] = 0;
159
1.16M
        hDecoder->time_out[i] = NULL;
160
1.16M
        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.16M
#ifdef MAIN_DEC
166
1.16M
        hDecoder->pred_stat[i] = NULL;
167
1.16M
#endif
168
1.16M
#ifdef LTP_DEC
169
1.16M
        hDecoder->ltp_lag[i] = 0;
170
1.16M
        hDecoder->lt_pred_stat[i] = NULL;
171
1.16M
#endif
172
1.16M
    }
173
174
18.2k
#ifdef SBR_DEC
175
893k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
875k
    {
177
875k
        hDecoder->sbr[i] = NULL;
178
875k
    }
179
18.2k
#endif
180
181
18.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
18.2k
    return hDecoder;
184
18.2k
}
NeAACDecOpen
Line
Count
Source
124
11.7k
{
125
11.7k
    uint8_t i;
126
11.7k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.7k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.7k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.7k
    hDecoder->cmes = mes;
134
11.7k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.7k
    hDecoder->config.defObjectType = MAIN;
136
11.7k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.7k
    hDecoder->config.downMatrix = 0;
138
11.7k
    hDecoder->adts_header_present = 0;
139
11.7k
    hDecoder->adif_header_present = 0;
140
11.7k
    hDecoder->latm_header_present = 0;
141
11.7k
#ifdef ERROR_RESILIENCE
142
11.7k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.7k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.7k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.7k
#endif
146
11.7k
    hDecoder->frameLength = 1024;
147
148
11.7k
    hDecoder->frame = 0;
149
11.7k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.7k
    hDecoder->__r1 = 0x2bb431ea;
153
11.7k
    hDecoder->__r2 = 0x206155b7;
154
155
762k
    for (i = 0; i < MAX_CHANNELS; i++)
156
750k
    {
157
750k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
750k
        hDecoder->window_shape_prev[i] = 0;
159
750k
        hDecoder->time_out[i] = NULL;
160
750k
        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
750k
#ifdef LTP_DEC
169
750k
        hDecoder->ltp_lag[i] = 0;
170
750k
        hDecoder->lt_pred_stat[i] = NULL;
171
750k
#endif
172
750k
    }
173
174
11.7k
#ifdef SBR_DEC
175
574k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
562k
    {
177
562k
        hDecoder->sbr[i] = NULL;
178
562k
    }
179
11.7k
#endif
180
181
11.7k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.7k
    return hDecoder;
184
11.7k
}
NeAACDecOpen
Line
Count
Source
124
18.2k
{
125
18.2k
    uint8_t i;
126
18.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
18.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
18.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
18.2k
    hDecoder->cmes = mes;
134
18.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
18.2k
    hDecoder->config.defObjectType = MAIN;
136
18.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
18.2k
    hDecoder->config.downMatrix = 0;
138
18.2k
    hDecoder->adts_header_present = 0;
139
18.2k
    hDecoder->adif_header_present = 0;
140
18.2k
    hDecoder->latm_header_present = 0;
141
18.2k
#ifdef ERROR_RESILIENCE
142
18.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
18.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
18.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
18.2k
#endif
146
18.2k
    hDecoder->frameLength = 1024;
147
148
18.2k
    hDecoder->frame = 0;
149
18.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
18.2k
    hDecoder->__r1 = 0x2bb431ea;
153
18.2k
    hDecoder->__r2 = 0x206155b7;
154
155
1.18M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.16M
    {
157
1.16M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.16M
        hDecoder->window_shape_prev[i] = 0;
159
1.16M
        hDecoder->time_out[i] = NULL;
160
1.16M
        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.16M
    }
173
174
18.2k
#ifdef SBR_DEC
175
893k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
875k
    {
177
875k
        hDecoder->sbr[i] = NULL;
178
875k
    }
179
18.2k
#endif
180
181
18.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
18.2k
    return hDecoder;
184
18.2k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
28.4k
{
188
28.4k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
28.4k
    if (hDecoder)
190
28.4k
    {
191
28.4k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
28.4k
        return config;
194
28.4k
    }
195
196
0
    return NULL;
197
28.4k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
28.7k
{
202
28.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
28.7k
    if (hDecoder && config)
204
28.7k
    {
205
        /* check if we can decode this object type */
206
28.7k
        if (can_decode_ot(config->defObjectType) < 0)
207
11
            return 0;
208
28.7k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
28.7k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
28.7k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
28.7k
#ifdef FIXED_POINT
217
28.7k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
200
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
28.5k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
28.5k
        if (config->downMatrix > 1)
226
26
            return 0;
227
28.4k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
28.4k
        return 1;
231
28.5k
    }
232
233
0
    return 0;
234
28.7k
}
235
236
237
#if 0
238
static int latmCheck(latm_header *latm, bitfile *ld)
239
{
240
    uint32_t good=0, bad=0, bits, m;
241
242
    while (ld->bytes_left)
243
    {
244
        bits = faad_latm_frame(latm, ld);
245
        if(bits==0xFFFFFFFF)
246
            bad++;
247
        else
248
        {
249
            good++;
250
            while(bits>0)
251
            {
252
                m = min(bits, 8);
253
                faad_getbits(ld, m);
254
                bits -= m;
255
            }
256
        }
257
    }
258
259
    return (good>0);
260
}
261
#endif
262
263
long NeAACDecInit(NeAACDecHandle hpDecoder,
264
                              unsigned char *buffer,
265
                              unsigned long buffer_size,
266
                              unsigned long *samplerate,
267
                              unsigned char *channels)
268
19.4k
{
269
19.4k
    uint32_t bits = 0;
270
19.4k
    bitfile ld;
271
19.4k
    adif_header adif;
272
19.4k
    adts_header adts;
273
19.4k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
19.4k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
57
        return -1;
278
279
19.4k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
19.4k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
19.4k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
19.4k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
19.4k
    *channels = 1;
284
285
19.4k
    if (buffer != NULL)
286
19.4k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
19.4k
        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.4k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
465
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
422
        {
314
422
            hDecoder->adif_header_present = 1;
315
316
422
            get_adif_header(&adif, &ld);
317
422
            faad_byte_align(&ld);
318
319
422
            hDecoder->sf_index = adif.pce[0].sf_index;
320
422
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
422
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
422
            *channels = adif.pce[0].channels;
324
325
422
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
422
            hDecoder->pce_set = 1;
327
328
422
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
19.0k
        } else if (adts_frame(&adts, &ld) == 0) {
332
592
            hDecoder->adts_header_present = 1;
333
334
592
            hDecoder->sf_index = adts.sf_index;
335
592
            hDecoder->object_type = adts.profile + 1;
336
337
592
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
592
            *channels = (adts.channel_configuration > 6) ?
339
541
                2 : adts.channel_configuration;
340
592
        }
341
342
19.4k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
19.4k
        faad_endbits(&ld);
348
19.4k
    }
349
350
19.4k
    if (!*samplerate)
351
181
        return -1;
352
353
19.2k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
19.2k
    if (*channels == 1)
356
18.4k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
18.4k
        *channels = 2;
359
18.4k
    }
360
19.2k
#endif
361
362
19.2k
    hDecoder->channelConfiguration = *channels;
363
364
19.2k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
19.2k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
8.91k
    {
368
8.91k
        *samplerate *= 2;
369
8.91k
        hDecoder->forceUpSampling = 1;
370
10.3k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
10.3k
        hDecoder->downSampledSBR = 1;
372
10.3k
    }
373
19.2k
#endif
374
375
19.2k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
194
        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
19.0k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
385
386
#ifdef LD_DEC
387
8.34k
    if (hDecoder->object_type == LD)
388
288
        hDecoder->frameLength >>= 1;
389
#endif
390
391
19.0k
    return bits;
392
19.2k
}
NeAACDecInit
Line
Count
Source
268
10.9k
{
269
10.9k
    uint32_t bits = 0;
270
10.9k
    bitfile ld;
271
10.9k
    adif_header adif;
272
10.9k
    adts_header adts;
273
10.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
10.9k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
40
        return -1;
278
279
10.9k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
10.9k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
10.9k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
10.9k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
10.9k
    *channels = 1;
284
285
10.9k
    if (buffer != NULL)
286
10.9k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
10.9k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
10.9k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
252
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
223
        {
314
223
            hDecoder->adif_header_present = 1;
315
316
223
            get_adif_header(&adif, &ld);
317
223
            faad_byte_align(&ld);
318
319
223
            hDecoder->sf_index = adif.pce[0].sf_index;
320
223
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
223
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
223
            *channels = adif.pce[0].channels;
324
325
223
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
223
            hDecoder->pce_set = 1;
327
328
223
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
10.7k
        } else if (adts_frame(&adts, &ld) == 0) {
332
183
            hDecoder->adts_header_present = 1;
333
334
183
            hDecoder->sf_index = adts.sf_index;
335
183
            hDecoder->object_type = adts.profile + 1;
336
337
183
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
183
            *channels = (adts.channel_configuration > 6) ?
339
163
                2 : adts.channel_configuration;
340
183
        }
341
342
10.9k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
10.9k
        faad_endbits(&ld);
348
10.9k
    }
349
350
10.9k
    if (!*samplerate)
351
101
        return -1;
352
353
10.8k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
10.8k
    if (*channels == 1)
356
10.5k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
10.5k
        *channels = 2;
359
10.5k
    }
360
10.8k
#endif
361
362
10.8k
    hDecoder->channelConfiguration = *channels;
363
364
10.8k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
10.8k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
4.88k
    {
368
4.88k
        *samplerate *= 2;
369
4.88k
        hDecoder->forceUpSampling = 1;
370
5.97k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
5.97k
        hDecoder->downSampledSBR = 1;
372
5.97k
    }
373
10.8k
#endif
374
375
10.8k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
149
        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.7k
        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.7k
    return bits;
392
10.8k
}
NeAACDecInit
Line
Count
Source
268
8.48k
{
269
8.48k
    uint32_t bits = 0;
270
8.48k
    bitfile ld;
271
8.48k
    adif_header adif;
272
8.48k
    adts_header adts;
273
8.48k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
8.48k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
17
        return -1;
278
279
8.46k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
8.46k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
8.46k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
8.46k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
8.46k
    *channels = 1;
284
285
8.46k
    if (buffer != NULL)
286
8.46k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
8.46k
        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.46k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
213
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
199
        {
314
199
            hDecoder->adif_header_present = 1;
315
316
199
            get_adif_header(&adif, &ld);
317
199
            faad_byte_align(&ld);
318
319
199
            hDecoder->sf_index = adif.pce[0].sf_index;
320
199
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
199
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
199
            *channels = adif.pce[0].channels;
324
325
199
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
199
            hDecoder->pce_set = 1;
327
328
199
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
8.27k
        } else if (adts_frame(&adts, &ld) == 0) {
332
409
            hDecoder->adts_header_present = 1;
333
334
409
            hDecoder->sf_index = adts.sf_index;
335
409
            hDecoder->object_type = adts.profile + 1;
336
337
409
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
409
            *channels = (adts.channel_configuration > 6) ?
339
378
                2 : adts.channel_configuration;
340
409
        }
341
342
8.46k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
8.46k
        faad_endbits(&ld);
348
8.46k
    }
349
350
8.46k
    if (!*samplerate)
351
80
        return -1;
352
353
8.38k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
8.38k
    if (*channels == 1)
356
7.86k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
7.86k
        *channels = 2;
359
7.86k
    }
360
8.38k
#endif
361
362
8.38k
    hDecoder->channelConfiguration = *channels;
363
364
8.38k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
8.38k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
4.03k
    {
368
4.03k
        *samplerate *= 2;
369
4.03k
        hDecoder->forceUpSampling = 1;
370
4.35k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
4.35k
        hDecoder->downSampledSBR = 1;
372
4.35k
    }
373
8.38k
#endif
374
375
8.38k
    if (can_decode_ot(hDecoder->object_type) < 0)
376
45
        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.34k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
385
386
8.34k
#ifdef LD_DEC
387
8.34k
    if (hDecoder->object_type == LD)
388
288
        hDecoder->frameLength >>= 1;
389
8.34k
#endif
390
391
8.34k
    return bits;
392
8.38k
}
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
9.00k
{
401
9.00k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
9.00k
    int8_t rc;
403
9.00k
    mp4AudioSpecificConfig mp4ASC;
404
405
9.00k
    if((hDecoder == NULL)
406
9.00k
        || (pBuffer == NULL)
407
9.00k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.98k
        || (samplerate == NULL)
409
8.98k
        || (channels == NULL))
410
16
    {
411
16
        return -1;
412
16
    }
413
414
8.98k
    hDecoder->adif_header_present = 0;
415
8.98k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.98k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.98k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.98k
    *samplerate = mp4ASC.samplingFrequency;
423
8.98k
    if (mp4ASC.channelsConfiguration)
424
7.61k
    {
425
7.61k
        *channels = mp4ASC.channelsConfiguration;
426
7.61k
    } else {
427
1.37k
        *channels = hDecoder->pce.channels;
428
1.37k
        hDecoder->pce_set = 1;
429
1.37k
    }
430
8.98k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.98k
    if (*channels == 1)
433
44
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
44
        *channels = 2;
436
44
    }
437
8.98k
#endif
438
8.98k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.98k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.98k
#ifdef ERROR_RESILIENCE
441
8.98k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.98k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.98k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.98k
#endif
445
8.98k
#ifdef SBR_DEC
446
8.98k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.98k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.98k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.98k
        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.98k
    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.98k
#endif
459
460
8.98k
    if (rc != 0)
461
96
    {
462
96
        return rc;
463
96
    }
464
8.89k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.89k
    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.89k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
4.14k
    if (hDecoder->object_type == LD)
482
495
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.89k
    return 0;
486
8.98k
}
NeAACDecInit2
Line
Count
Source
400
4.81k
{
401
4.81k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
4.81k
    int8_t rc;
403
4.81k
    mp4AudioSpecificConfig mp4ASC;
404
405
4.81k
    if((hDecoder == NULL)
406
4.81k
        || (pBuffer == NULL)
407
4.81k
        || (SizeOfDecoderSpecificInfo < 2)
408
4.80k
        || (samplerate == NULL)
409
4.80k
        || (channels == NULL))
410
10
    {
411
10
        return -1;
412
10
    }
413
414
4.80k
    hDecoder->adif_header_present = 0;
415
4.80k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
4.80k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
4.80k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
4.80k
    *samplerate = mp4ASC.samplingFrequency;
423
4.80k
    if (mp4ASC.channelsConfiguration)
424
4.07k
    {
425
4.07k
        *channels = mp4ASC.channelsConfiguration;
426
4.07k
    } else {
427
729
        *channels = hDecoder->pce.channels;
428
729
        hDecoder->pce_set = 1;
429
729
    }
430
4.80k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
4.80k
    if (*channels == 1)
433
30
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
30
        *channels = 2;
436
30
    }
437
4.80k
#endif
438
4.80k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
4.80k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
4.80k
#ifdef ERROR_RESILIENCE
441
4.80k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
4.80k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
4.80k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
4.80k
#endif
445
4.80k
#ifdef SBR_DEC
446
4.80k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
4.80k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
4.80k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
4.80k
        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.80k
    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.80k
#endif
459
460
4.80k
    if (rc != 0)
461
57
    {
462
57
        return rc;
463
57
    }
464
4.74k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
4.74k
    if (mp4ASC.frameLengthFlag)
466
3.01k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.01k
        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.74k
        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.74k
    return 0;
486
4.80k
}
NeAACDecInit2
Line
Count
Source
400
4.19k
{
401
4.19k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
4.19k
    int8_t rc;
403
4.19k
    mp4AudioSpecificConfig mp4ASC;
404
405
4.19k
    if((hDecoder == NULL)
406
4.19k
        || (pBuffer == NULL)
407
4.19k
        || (SizeOfDecoderSpecificInfo < 2)
408
4.18k
        || (samplerate == NULL)
409
4.18k
        || (channels == NULL))
410
6
    {
411
6
        return -1;
412
6
    }
413
414
4.18k
    hDecoder->adif_header_present = 0;
415
4.18k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
4.18k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
4.18k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
4.18k
    *samplerate = mp4ASC.samplingFrequency;
423
4.18k
    if (mp4ASC.channelsConfiguration)
424
3.53k
    {
425
3.53k
        *channels = mp4ASC.channelsConfiguration;
426
3.53k
    } else {
427
645
        *channels = hDecoder->pce.channels;
428
645
        hDecoder->pce_set = 1;
429
645
    }
430
4.18k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
4.18k
    if (*channels == 1)
433
14
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
14
        *channels = 2;
436
14
    }
437
4.18k
#endif
438
4.18k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
4.18k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
4.18k
#ifdef ERROR_RESILIENCE
441
4.18k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
4.18k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
4.18k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
4.18k
#endif
445
4.18k
#ifdef SBR_DEC
446
4.18k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
4.18k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
4.18k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
4.18k
        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.18k
    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.18k
#endif
459
460
4.18k
    if (rc != 0)
461
39
    {
462
39
        return rc;
463
39
    }
464
4.14k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
4.14k
    if (mp4ASC.frameLengthFlag)
466
2.52k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
2.52k
        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.14k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
4.14k
#ifdef LD_DEC
481
4.14k
    if (hDecoder->object_type == LD)
482
495
        hDecoder->frameLength >>= 1;
483
4.14k
#endif
484
485
4.14k
    return 0;
486
4.18k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
1.24k
{
493
1.24k
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
1.24k
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
1.24k
    NeAACDecClose(*hDecoder);
498
499
1.24k
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
1.24k
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
1.24k
    (*hDecoder)->config.defSampleRate = samplerate;
505
1.24k
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
1.24k
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
1.24k
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
1.24k
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
1.24k
#endif
510
1.24k
    (*hDecoder)->frameLength = 960;
511
1.24k
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
1.24k
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
1.24k
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
224
        (*hDecoder)->channelConfiguration = 2;
516
1.01k
    else
517
1.01k
        (*hDecoder)->channelConfiguration = 1;
518
519
1.24k
#ifdef SBR_DEC
520
1.24k
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
209
        (*hDecoder)->sbr_present_flag = 0;
522
1.03k
    else
523
1.03k
        (*hDecoder)->sbr_present_flag = 1;
524
1.24k
#endif
525
526
1.24k
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
1.24k
    return 0;
529
1.24k
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
17.2k
{
534
17.2k
    uint8_t i;
535
17.2k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
17.2k
    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.11M
    for (i = 0; i < MAX_CHANNELS; i++)
549
1.10M
    {
550
1.10M
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
1.10M
        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.10M
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
17.2k
        filter_bank_end(hDecoder->fb);
570
571
17.2k
    drc_end(hDecoder->drc);
572
573
17.2k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
17.2k
#ifdef SBR_DEC
576
842k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
825k
    {
578
825k
        if (hDecoder->sbr[i])
579
190k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
825k
    }
581
17.2k
#endif
582
583
17.2k
    if (hDecoder) faad_free(hDecoder);
584
17.2k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
20.7k
{
588
20.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
20.7k
    if (hDecoder)
590
20.7k
    {
591
20.7k
        hDecoder->postSeekResetFlag = 1;
592
593
20.7k
        if (frame != -1)
594
20.7k
            hDecoder->frame = frame;
595
20.7k
    }
596
20.7k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
44.0k
{
600
44.0k
    hInfo->num_front_channels = 0;
601
44.0k
    hInfo->num_side_channels = 0;
602
44.0k
    hInfo->num_back_channels = 0;
603
44.0k
    hInfo->num_lfe_channels = 0;
604
44.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
44.0k
    if (hDecoder->downMatrix)
607
4.75k
    {
608
4.75k
        hInfo->num_front_channels = 2;
609
4.75k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.75k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.75k
        return;
612
4.75k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
39.3k
    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
648
        {
628
648
#if (defined(PS_DEC) || defined(DRM_PS))
629
648
            if (hInfo->num_front_channels == 1 &&
630
288
                hInfo->num_side_channels == 0 &&
631
144
                hInfo->num_back_channels == 0 &&
632
108
                hInfo->num_lfe_channels == 0)
633
72
            {
634
                /* When PS is enabled output is always stereo */
635
72
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
72
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
72
            } else
638
576
#endif
639
576
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
648
            chdir--;
641
648
        }
642
8.81k
        for (i = 0; i < chdir; i++)
643
7.16k
        {
644
7.16k
            hInfo->channel_position[chpos++] =
645
7.16k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
7.16k
        }
647
648
7.56k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
5.91k
        {
650
5.91k
            hInfo->channel_position[chpos++] =
651
5.91k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
5.91k
        }
653
654
1.64k
        chdir = hInfo->num_back_channels;
655
1.64k
        if (chdir & 1)
656
208
        {
657
208
            back_center = 1;
658
208
            chdir--;
659
208
        }
660
5.28k
        for (i = 0; i < chdir; i++)
661
3.64k
        {
662
3.64k
            hInfo->channel_position[chpos++] =
663
3.64k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.64k
        }
665
1.64k
        if (back_center)
666
208
        {
667
208
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
208
        }
669
670
2.70k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
1.05k
        {
672
1.05k
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
1.05k
        }
674
675
37.6k
    } else {
676
37.6k
        switch (hDecoder->channelConfiguration)
677
37.6k
        {
678
2.47k
        case 1:
679
2.47k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
2.47k
            hInfo->num_front_channels = 2;
682
2.47k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
2.47k
            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.47k
            break;
689
16.8k
        case 2:
690
16.8k
            hInfo->num_front_channels = 2;
691
16.8k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
16.8k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
16.8k
            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.23k
        case 4:
701
4.23k
            hInfo->num_front_channels = 3;
702
4.23k
            hInfo->num_back_channels = 1;
703
4.23k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.23k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.23k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.23k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.23k
            break;
708
2.81k
        case 5:
709
2.81k
            hInfo->num_front_channels = 3;
710
2.81k
            hInfo->num_back_channels = 2;
711
2.81k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.81k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.81k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.81k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.81k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.81k
            break;
717
1.07k
        case 6:
718
1.07k
            hInfo->num_front_channels = 3;
719
1.07k
            hInfo->num_back_channels = 2;
720
1.07k
            hInfo->num_lfe_channels = 1;
721
1.07k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.07k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.07k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.07k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.07k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.07k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.07k
            break;
728
6.24k
        case 7:
729
6.24k
            hInfo->num_front_channels = 3;
730
6.24k
            hInfo->num_side_channels = 2;
731
6.24k
            hInfo->num_back_channels = 2;
732
6.24k
            hInfo->num_lfe_channels = 1;
733
6.24k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
6.24k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
6.24k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
6.24k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
6.24k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
6.24k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
6.24k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
6.24k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
6.24k
            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
296
                {
748
296
                    uint8_t ch1 = (ch-1)/2;
749
296
                    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
996
                        for (i = 1; i <= ch1; i+=2)
755
796
                        {
756
796
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
796
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
796
                        }
759
996
                        for (i = ch1+1; i < ch; i+=2)
760
796
                        {
761
796
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
796
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
796
                        }
764
200
                    } else {
765
96
                        hInfo->num_front_channels = ch1;
766
96
                        hInfo->num_back_channels = ch1 + 1;
767
604
                        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
604
                        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
96
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
96
                    }
779
640
                } else {
780
640
                    uint8_t ch1 = (ch)/2;
781
640
                    hInfo->num_front_channels = ch1;
782
640
                    hInfo->num_back_channels = ch1;
783
640
                    if (ch1 & 1)
784
304
                    {
785
304
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.82k
                        for (i = 1; i <= ch1; i+=2)
787
1.51k
                        {
788
1.51k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.51k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.51k
                        }
791
1.51k
                        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
304
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
336
                    } else {
798
2.83k
                        for (i = 0; i < ch1; i+=2)
799
2.50k
                        {
800
2.50k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
2.50k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
2.50k
                        }
803
2.83k
                        for (i = ch1; i < ch; i+=2)
804
2.50k
                        {
805
2.50k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
2.50k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
2.50k
                        }
808
336
                    }
809
640
                }
810
936
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
1.80k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
872
                {
813
872
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
872
                }
815
936
            }
816
936
            break;
817
37.6k
        }
818
37.6k
    }
819
39.3k
}
decoder.c:create_channel_config
Line
Count
Source
599
11.0k
{
600
11.0k
    hInfo->num_front_channels = 0;
601
11.0k
    hInfo->num_side_channels = 0;
602
11.0k
    hInfo->num_back_channels = 0;
603
11.0k
    hInfo->num_lfe_channels = 0;
604
11.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
11.0k
    if (hDecoder->downMatrix)
607
1.18k
    {
608
1.18k
        hInfo->num_front_channels = 2;
609
1.18k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.18k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.18k
        return;
612
1.18k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.82k
    if (hDecoder->pce_set)
617
411
    {
618
411
        uint8_t i, chpos = 0;
619
411
        uint8_t chdir, back_center = 0;
620
621
411
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
411
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
411
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
411
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
411
        chdir = hInfo->num_front_channels;
626
411
        if (chdir & 1)
627
162
        {
628
162
#if (defined(PS_DEC) || defined(DRM_PS))
629
162
            if (hInfo->num_front_channels == 1 &&
630
72
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
27
                hInfo->num_lfe_channels == 0)
633
18
            {
634
                /* When PS is enabled output is always stereo */
635
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
18
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
162
            chdir--;
641
162
        }
642
2.20k
        for (i = 0; i < chdir; i++)
643
1.79k
        {
644
1.79k
            hInfo->channel_position[chpos++] =
645
1.79k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.79k
        }
647
648
1.89k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
411
        chdir = hInfo->num_back_channels;
655
411
        if (chdir & 1)
656
52
        {
657
52
            back_center = 1;
658
52
            chdir--;
659
52
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
910
        {
662
910
            hInfo->channel_position[chpos++] =
663
910
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
910
        }
665
411
        if (back_center)
666
52
        {
667
52
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
52
        }
669
670
675
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
264
        {
672
264
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
264
        }
674
675
9.41k
    } else {
676
9.41k
        switch (hDecoder->channelConfiguration)
677
9.41k
        {
678
618
        case 1:
679
618
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
618
            hInfo->num_front_channels = 2;
682
618
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
618
            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
618
            break;
689
4.20k
        case 2:
690
4.20k
            hInfo->num_front_channels = 2;
691
4.20k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.20k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.20k
            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.05k
        case 4:
701
1.05k
            hInfo->num_front_channels = 3;
702
1.05k
            hInfo->num_back_channels = 1;
703
1.05k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.05k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.05k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.05k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.05k
            break;
708
704
        case 5:
709
704
            hInfo->num_front_channels = 3;
710
704
            hInfo->num_back_channels = 2;
711
704
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
704
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
704
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
704
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
704
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
704
            break;
717
268
        case 6:
718
268
            hInfo->num_front_channels = 3;
719
268
            hInfo->num_back_channels = 2;
720
268
            hInfo->num_lfe_channels = 1;
721
268
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
268
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
268
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
268
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
268
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
268
            hInfo->channel_position[5] = LFE_CHANNEL;
727
268
            break;
728
1.56k
        case 7:
729
1.56k
            hInfo->num_front_channels = 3;
730
1.56k
            hInfo->num_side_channels = 2;
731
1.56k
            hInfo->num_back_channels = 2;
732
1.56k
            hInfo->num_lfe_channels = 1;
733
1.56k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.56k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.56k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.56k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.56k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.56k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.56k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.56k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.56k
            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
74
                {
748
74
                    uint8_t ch1 = (ch-1)/2;
749
74
                    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
249
                        for (i = 1; i <= ch1; i+=2)
755
199
                        {
756
199
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
199
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
199
                        }
759
249
                        for (i = ch1+1; i < ch; i+=2)
760
199
                        {
761
199
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
199
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
199
                        }
764
50
                    } else {
765
24
                        hInfo->num_front_channels = ch1;
766
24
                        hInfo->num_back_channels = ch1 + 1;
767
151
                        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
151
                        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
24
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
24
                    }
779
160
                } else {
780
160
                    uint8_t ch1 = (ch)/2;
781
160
                    hInfo->num_front_channels = ch1;
782
160
                    hInfo->num_back_channels = ch1;
783
160
                    if (ch1 & 1)
784
76
                    {
785
76
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
455
                        for (i = 1; i <= ch1; i+=2)
787
379
                        {
788
379
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
379
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
379
                        }
791
379
                        for (i = ch1+1; i < ch-1; i+=2)
792
303
                        {
793
303
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
303
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
303
                        }
796
76
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
84
                    } else {
798
709
                        for (i = 0; i < ch1; i+=2)
799
625
                        {
800
625
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
625
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
625
                        }
803
709
                        for (i = ch1; i < ch; i+=2)
804
625
                        {
805
625
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
625
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
625
                        }
808
84
                    }
809
160
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
452
                for (i = ch; i < hDecoder->fr_channels; i++)
812
218
                {
813
218
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
218
                }
815
234
            }
816
234
            break;
817
9.41k
        }
818
9.41k
    }
819
9.82k
}
decoder.c:create_channel_config
Line
Count
Source
599
11.0k
{
600
11.0k
    hInfo->num_front_channels = 0;
601
11.0k
    hInfo->num_side_channels = 0;
602
11.0k
    hInfo->num_back_channels = 0;
603
11.0k
    hInfo->num_lfe_channels = 0;
604
11.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
11.0k
    if (hDecoder->downMatrix)
607
1.18k
    {
608
1.18k
        hInfo->num_front_channels = 2;
609
1.18k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.18k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.18k
        return;
612
1.18k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.82k
    if (hDecoder->pce_set)
617
411
    {
618
411
        uint8_t i, chpos = 0;
619
411
        uint8_t chdir, back_center = 0;
620
621
411
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
411
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
411
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
411
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
411
        chdir = hInfo->num_front_channels;
626
411
        if (chdir & 1)
627
162
        {
628
162
#if (defined(PS_DEC) || defined(DRM_PS))
629
162
            if (hInfo->num_front_channels == 1 &&
630
72
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
27
                hInfo->num_lfe_channels == 0)
633
18
            {
634
                /* When PS is enabled output is always stereo */
635
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
18
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
162
            chdir--;
641
162
        }
642
2.20k
        for (i = 0; i < chdir; i++)
643
1.79k
        {
644
1.79k
            hInfo->channel_position[chpos++] =
645
1.79k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.79k
        }
647
648
1.89k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
411
        chdir = hInfo->num_back_channels;
655
411
        if (chdir & 1)
656
52
        {
657
52
            back_center = 1;
658
52
            chdir--;
659
52
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
910
        {
662
910
            hInfo->channel_position[chpos++] =
663
910
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
910
        }
665
411
        if (back_center)
666
52
        {
667
52
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
52
        }
669
670
675
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
264
        {
672
264
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
264
        }
674
675
9.41k
    } else {
676
9.41k
        switch (hDecoder->channelConfiguration)
677
9.41k
        {
678
618
        case 1:
679
618
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
618
            hInfo->num_front_channels = 2;
682
618
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
618
            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
618
            break;
689
4.20k
        case 2:
690
4.20k
            hInfo->num_front_channels = 2;
691
4.20k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.20k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.20k
            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.05k
        case 4:
701
1.05k
            hInfo->num_front_channels = 3;
702
1.05k
            hInfo->num_back_channels = 1;
703
1.05k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.05k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.05k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.05k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.05k
            break;
708
704
        case 5:
709
704
            hInfo->num_front_channels = 3;
710
704
            hInfo->num_back_channels = 2;
711
704
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
704
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
704
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
704
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
704
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
704
            break;
717
268
        case 6:
718
268
            hInfo->num_front_channels = 3;
719
268
            hInfo->num_back_channels = 2;
720
268
            hInfo->num_lfe_channels = 1;
721
268
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
268
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
268
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
268
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
268
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
268
            hInfo->channel_position[5] = LFE_CHANNEL;
727
268
            break;
728
1.56k
        case 7:
729
1.56k
            hInfo->num_front_channels = 3;
730
1.56k
            hInfo->num_side_channels = 2;
731
1.56k
            hInfo->num_back_channels = 2;
732
1.56k
            hInfo->num_lfe_channels = 1;
733
1.56k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.56k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.56k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.56k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.56k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.56k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.56k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.56k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.56k
            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
74
                {
748
74
                    uint8_t ch1 = (ch-1)/2;
749
74
                    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
249
                        for (i = 1; i <= ch1; i+=2)
755
199
                        {
756
199
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
199
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
199
                        }
759
249
                        for (i = ch1+1; i < ch; i+=2)
760
199
                        {
761
199
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
199
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
199
                        }
764
50
                    } else {
765
24
                        hInfo->num_front_channels = ch1;
766
24
                        hInfo->num_back_channels = ch1 + 1;
767
151
                        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
151
                        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
24
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
24
                    }
779
160
                } else {
780
160
                    uint8_t ch1 = (ch)/2;
781
160
                    hInfo->num_front_channels = ch1;
782
160
                    hInfo->num_back_channels = ch1;
783
160
                    if (ch1 & 1)
784
76
                    {
785
76
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
455
                        for (i = 1; i <= ch1; i+=2)
787
379
                        {
788
379
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
379
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
379
                        }
791
379
                        for (i = ch1+1; i < ch-1; i+=2)
792
303
                        {
793
303
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
303
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
303
                        }
796
76
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
84
                    } else {
798
709
                        for (i = 0; i < ch1; i+=2)
799
625
                        {
800
625
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
625
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
625
                        }
803
709
                        for (i = ch1; i < ch; i+=2)
804
625
                        {
805
625
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
625
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
625
                        }
808
84
                    }
809
160
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
452
                for (i = ch; i < hDecoder->fr_channels; i++)
812
218
                {
813
218
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
218
                }
815
234
            }
816
234
            break;
817
9.41k
        }
818
9.41k
    }
819
9.82k
}
decoder.c:create_channel_config
Line
Count
Source
599
11.0k
{
600
11.0k
    hInfo->num_front_channels = 0;
601
11.0k
    hInfo->num_side_channels = 0;
602
11.0k
    hInfo->num_back_channels = 0;
603
11.0k
    hInfo->num_lfe_channels = 0;
604
11.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
11.0k
    if (hDecoder->downMatrix)
607
1.18k
    {
608
1.18k
        hInfo->num_front_channels = 2;
609
1.18k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.18k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.18k
        return;
612
1.18k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.82k
    if (hDecoder->pce_set)
617
411
    {
618
411
        uint8_t i, chpos = 0;
619
411
        uint8_t chdir, back_center = 0;
620
621
411
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
411
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
411
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
411
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
411
        chdir = hInfo->num_front_channels;
626
411
        if (chdir & 1)
627
162
        {
628
162
#if (defined(PS_DEC) || defined(DRM_PS))
629
162
            if (hInfo->num_front_channels == 1 &&
630
72
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
27
                hInfo->num_lfe_channels == 0)
633
18
            {
634
                /* When PS is enabled output is always stereo */
635
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
18
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
162
            chdir--;
641
162
        }
642
2.20k
        for (i = 0; i < chdir; i++)
643
1.79k
        {
644
1.79k
            hInfo->channel_position[chpos++] =
645
1.79k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.79k
        }
647
648
1.89k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
411
        chdir = hInfo->num_back_channels;
655
411
        if (chdir & 1)
656
52
        {
657
52
            back_center = 1;
658
52
            chdir--;
659
52
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
910
        {
662
910
            hInfo->channel_position[chpos++] =
663
910
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
910
        }
665
411
        if (back_center)
666
52
        {
667
52
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
52
        }
669
670
675
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
264
        {
672
264
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
264
        }
674
675
9.41k
    } else {
676
9.41k
        switch (hDecoder->channelConfiguration)
677
9.41k
        {
678
618
        case 1:
679
618
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
618
            hInfo->num_front_channels = 2;
682
618
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
618
            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
618
            break;
689
4.20k
        case 2:
690
4.20k
            hInfo->num_front_channels = 2;
691
4.20k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.20k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.20k
            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.05k
        case 4:
701
1.05k
            hInfo->num_front_channels = 3;
702
1.05k
            hInfo->num_back_channels = 1;
703
1.05k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.05k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.05k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.05k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.05k
            break;
708
704
        case 5:
709
704
            hInfo->num_front_channels = 3;
710
704
            hInfo->num_back_channels = 2;
711
704
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
704
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
704
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
704
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
704
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
704
            break;
717
268
        case 6:
718
268
            hInfo->num_front_channels = 3;
719
268
            hInfo->num_back_channels = 2;
720
268
            hInfo->num_lfe_channels = 1;
721
268
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
268
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
268
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
268
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
268
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
268
            hInfo->channel_position[5] = LFE_CHANNEL;
727
268
            break;
728
1.56k
        case 7:
729
1.56k
            hInfo->num_front_channels = 3;
730
1.56k
            hInfo->num_side_channels = 2;
731
1.56k
            hInfo->num_back_channels = 2;
732
1.56k
            hInfo->num_lfe_channels = 1;
733
1.56k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.56k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.56k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.56k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.56k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.56k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.56k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.56k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.56k
            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
74
                {
748
74
                    uint8_t ch1 = (ch-1)/2;
749
74
                    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
249
                        for (i = 1; i <= ch1; i+=2)
755
199
                        {
756
199
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
199
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
199
                        }
759
249
                        for (i = ch1+1; i < ch; i+=2)
760
199
                        {
761
199
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
199
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
199
                        }
764
50
                    } else {
765
24
                        hInfo->num_front_channels = ch1;
766
24
                        hInfo->num_back_channels = ch1 + 1;
767
151
                        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
151
                        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
24
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
24
                    }
779
160
                } else {
780
160
                    uint8_t ch1 = (ch)/2;
781
160
                    hInfo->num_front_channels = ch1;
782
160
                    hInfo->num_back_channels = ch1;
783
160
                    if (ch1 & 1)
784
76
                    {
785
76
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
455
                        for (i = 1; i <= ch1; i+=2)
787
379
                        {
788
379
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
379
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
379
                        }
791
379
                        for (i = ch1+1; i < ch-1; i+=2)
792
303
                        {
793
303
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
303
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
303
                        }
796
76
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
84
                    } else {
798
709
                        for (i = 0; i < ch1; i+=2)
799
625
                        {
800
625
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
625
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
625
                        }
803
709
                        for (i = ch1; i < ch; i+=2)
804
625
                        {
805
625
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
625
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
625
                        }
808
84
                    }
809
160
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
452
                for (i = ch; i < hDecoder->fr_channels; i++)
812
218
                {
813
218
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
218
                }
815
234
            }
816
234
            break;
817
9.41k
        }
818
9.41k
    }
819
9.82k
}
decoder.c:create_channel_config
Line
Count
Source
599
11.0k
{
600
11.0k
    hInfo->num_front_channels = 0;
601
11.0k
    hInfo->num_side_channels = 0;
602
11.0k
    hInfo->num_back_channels = 0;
603
11.0k
    hInfo->num_lfe_channels = 0;
604
11.0k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
11.0k
    if (hDecoder->downMatrix)
607
1.18k
    {
608
1.18k
        hInfo->num_front_channels = 2;
609
1.18k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.18k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.18k
        return;
612
1.18k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.82k
    if (hDecoder->pce_set)
617
411
    {
618
411
        uint8_t i, chpos = 0;
619
411
        uint8_t chdir, back_center = 0;
620
621
411
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
411
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
411
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
411
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
411
        chdir = hInfo->num_front_channels;
626
411
        if (chdir & 1)
627
162
        {
628
162
#if (defined(PS_DEC) || defined(DRM_PS))
629
162
            if (hInfo->num_front_channels == 1 &&
630
72
                hInfo->num_side_channels == 0 &&
631
36
                hInfo->num_back_channels == 0 &&
632
27
                hInfo->num_lfe_channels == 0)
633
18
            {
634
                /* When PS is enabled output is always stereo */
635
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
18
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
18
            } else
638
144
#endif
639
144
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
162
            chdir--;
641
162
        }
642
2.20k
        for (i = 0; i < chdir; i++)
643
1.79k
        {
644
1.79k
            hInfo->channel_position[chpos++] =
645
1.79k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.79k
        }
647
648
1.89k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.47k
        {
650
1.47k
            hInfo->channel_position[chpos++] =
651
1.47k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.47k
        }
653
654
411
        chdir = hInfo->num_back_channels;
655
411
        if (chdir & 1)
656
52
        {
657
52
            back_center = 1;
658
52
            chdir--;
659
52
        }
660
1.32k
        for (i = 0; i < chdir; i++)
661
910
        {
662
910
            hInfo->channel_position[chpos++] =
663
910
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
910
        }
665
411
        if (back_center)
666
52
        {
667
52
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
52
        }
669
670
675
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
264
        {
672
264
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
264
        }
674
675
9.41k
    } else {
676
9.41k
        switch (hDecoder->channelConfiguration)
677
9.41k
        {
678
618
        case 1:
679
618
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
618
            hInfo->num_front_channels = 2;
682
618
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
618
            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
618
            break;
689
4.20k
        case 2:
690
4.20k
            hInfo->num_front_channels = 2;
691
4.20k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.20k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.20k
            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.05k
        case 4:
701
1.05k
            hInfo->num_front_channels = 3;
702
1.05k
            hInfo->num_back_channels = 1;
703
1.05k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.05k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.05k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.05k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.05k
            break;
708
704
        case 5:
709
704
            hInfo->num_front_channels = 3;
710
704
            hInfo->num_back_channels = 2;
711
704
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
704
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
704
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
704
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
704
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
704
            break;
717
268
        case 6:
718
268
            hInfo->num_front_channels = 3;
719
268
            hInfo->num_back_channels = 2;
720
268
            hInfo->num_lfe_channels = 1;
721
268
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
268
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
268
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
268
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
268
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
268
            hInfo->channel_position[5] = LFE_CHANNEL;
727
268
            break;
728
1.56k
        case 7:
729
1.56k
            hInfo->num_front_channels = 3;
730
1.56k
            hInfo->num_side_channels = 2;
731
1.56k
            hInfo->num_back_channels = 2;
732
1.56k
            hInfo->num_lfe_channels = 1;
733
1.56k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.56k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.56k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.56k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.56k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.56k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.56k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.56k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.56k
            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
74
                {
748
74
                    uint8_t ch1 = (ch-1)/2;
749
74
                    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
249
                        for (i = 1; i <= ch1; i+=2)
755
199
                        {
756
199
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
199
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
199
                        }
759
249
                        for (i = ch1+1; i < ch; i+=2)
760
199
                        {
761
199
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
199
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
199
                        }
764
50
                    } else {
765
24
                        hInfo->num_front_channels = ch1;
766
24
                        hInfo->num_back_channels = ch1 + 1;
767
151
                        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
151
                        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
24
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
24
                    }
779
160
                } else {
780
160
                    uint8_t ch1 = (ch)/2;
781
160
                    hInfo->num_front_channels = ch1;
782
160
                    hInfo->num_back_channels = ch1;
783
160
                    if (ch1 & 1)
784
76
                    {
785
76
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
455
                        for (i = 1; i <= ch1; i+=2)
787
379
                        {
788
379
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
379
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
379
                        }
791
379
                        for (i = ch1+1; i < ch-1; i+=2)
792
303
                        {
793
303
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
303
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
303
                        }
796
76
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
84
                    } else {
798
709
                        for (i = 0; i < ch1; i+=2)
799
625
                        {
800
625
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
625
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
625
                        }
803
709
                        for (i = ch1; i < ch; i+=2)
804
625
                        {
805
625
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
625
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
625
                        }
808
84
                    }
809
160
                }
810
234
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
452
                for (i = ch; i < hDecoder->fr_channels; i++)
812
218
                {
813
218
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
218
                }
815
234
            }
816
234
            break;
817
9.41k
        }
818
9.41k
    }
819
9.82k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
43.2k
{
826
43.2k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
43.2k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
43.2k
}
829
830
void* NeAACDecDecode2(NeAACDecHandle hpDecoder,
831
                                  NeAACDecFrameInfo *hInfo,
832
                                  unsigned char *buffer,
833
                                  unsigned long buffer_size,
834
                                  void **sample_buffer,
835
                                  unsigned long sample_buffer_size)
836
12.3k
{
837
12.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
12.3k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
136
    {
840
136
        hInfo->error = 27;
841
136
        return NULL;
842
136
    }
843
844
12.2k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
12.2k
        sample_buffer, sample_buffer_size);
846
12.3k
}
847
848
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
849
                              NeAACDecFrameInfo *hInfo,
850
                              unsigned char *buffer,
851
                              unsigned long buffer_size,
852
                              void **sample_buffer2,
853
                              unsigned long sample_buffer_size)
854
55.4k
{
855
55.4k
    uint16_t i;
856
55.4k
    uint8_t channels = 0;
857
55.4k
    uint8_t output_channels = 0;
858
55.4k
    bitfile ld;
859
55.4k
    uint32_t bitsconsumed;
860
55.4k
    uint16_t frame_len;
861
55.4k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
55.4k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
55.4k
    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
55.4k
    frame_len = hDecoder->frameLength;
882
883
884
55.4k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
55.4k
    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
55.4k
    if (buffer_size >= 128)
903
6.30k
    {
904
6.30k
        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.30k
    }
912
913
914
    /* initialize the bitstream */
915
55.4k
    faad_initbits(&ld, buffer, buffer_size);
916
55.4k
    if (ld.error != 0)
917
21.8k
        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.3k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.55k
    {
953
        /* We do not support stereo right now */
954
1.55k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.55k
        faad_getbits(&ld, 8
961
1.55k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.55k
    }
963
18.3k
#endif
964
965
33.5k
    if (hDecoder->adts_header_present)
966
789
    {
967
789
        adts_header adts;
968
969
789
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
789
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
90
            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
789
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
18.3k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.55k
    {
987
1.55k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
16.8k
    } else {
989
16.8k
#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.4k
    if (hInfo->error > 0)
1011
22.3k
        goto error;
1012
1013
    /* safety check */
1014
11.1k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
137
    {
1016
        /* invalid number of channels */
1017
137
        hInfo->error = 12;
1018
137
        goto error;
1019
137
    }
1020
1021
    /* no more bit reading after this */
1022
11.0k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
11.0k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
11.0k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
11.0k
    faad_endbits(&ld);
1030
1031
1032
11.0k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
11.0k
        )
1037
10.7k
    {
1038
10.7k
        if (hDecoder->channelConfiguration == 0)
1039
352
            hDecoder->channelConfiguration = channels;
1040
1041
10.7k
        if (channels == 8) /* 7.1 */
1042
865
            hDecoder->channelConfiguration = 7;
1043
10.7k
        if (channels == 7) /* not a standard channelConfiguration */
1044
44
            hDecoder->channelConfiguration = 0;
1045
10.7k
    }
1046
1047
11.0k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.18k
    {
1049
1.18k
        hDecoder->downMatrix = 1;
1050
1.18k
        output_channels = 2;
1051
9.84k
    } else {
1052
9.84k
        output_channels = channels;
1053
9.84k
    }
1054
1055
11.0k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
11.0k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
11.0k
    if (output_channels == 1)
1059
190
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
190
        hDecoder->upMatrix = 1;
1062
190
        output_channels = 2;
1063
190
    }
1064
11.0k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
11.0k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
423
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
423
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
12
        {
1072
12
            hInfo->error = 22;
1073
12
            return NULL;
1074
12
        }
1075
423
    }
1076
11.0k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
11.0k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
11.0k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
11.0k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
11.0k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
11.0k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
11.0k
    hInfo->header_type = RAW;
1090
11.0k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
11.0k
    if (hDecoder->adts_header_present)
1093
233
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
11.0k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
11.0k
    hInfo->ps = hDecoder->ps_used_global;
1100
11.0k
#endif
1101
1102
    /* check if frame has channel elements */
1103
11.0k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
11.0k
    {
1110
11.0k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
11.0k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
11.0k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
11.0k
        };
1114
11.0k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
11.0k
#ifdef SBR_DEC
1116
11.0k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
6.19k
        {
1118
6.19k
            stride = 2 * stride;
1119
6.19k
        }
1120
11.0k
#endif
1121
11.0k
        required_buffer_size = frame_len*output_channels*stride;
1122
11.0k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
11.0k
    if (sample_buffer_size == 0)
1126
7.63k
    {
1127
        /* allocate the buffer for the final samples */
1128
7.63k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
6.44k
        {
1130
6.44k
            if (hDecoder->sample_buffer)
1131
99
                faad_free(hDecoder->sample_buffer);
1132
6.44k
            hDecoder->sample_buffer = NULL;
1133
6.44k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
6.44k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
6.44k
        }
1136
7.63k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.25k
        hInfo->error = 27;
1139
1.25k
        return NULL;
1140
1.25k
    }
1141
1142
9.76k
    if (sample_buffer_size == 0)
1143
7.63k
    {
1144
7.63k
        sample_buffer = hDecoder->sample_buffer;
1145
7.63k
    } else {
1146
2.13k
        sample_buffer = *sample_buffer2;
1147
2.13k
    }
1148
1149
9.76k
#ifdef SBR_DEC
1150
9.76k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
6.66k
    {
1152
6.66k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
6.66k
        if (!hDecoder->downSampledSBR)
1156
5.28k
        {
1157
5.28k
            frame_len *= 2;
1158
5.28k
            hInfo->samples *= 2;
1159
5.28k
            hInfo->samplerate *= 2;
1160
5.28k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
32.8k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
26.2k
        {
1165
26.2k
            if (hDecoder->sbr[ele] == NULL)
1166
12
            {
1167
12
                hInfo->error = 25;
1168
12
                goto error;
1169
12
            }
1170
26.2k
        }
1171
1172
        /* sbr */
1173
6.65k
        if (hDecoder->sbr_present_flag == 1)
1174
4.45k
        {
1175
4.45k
            hInfo->object_type = HE_AAC;
1176
4.45k
            hInfo->sbr = SBR_UPSAMPLED;
1177
4.45k
        } else {
1178
2.19k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.19k
        }
1180
6.65k
        if (hDecoder->downSampledSBR)
1181
1.37k
        {
1182
1.37k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.37k
        }
1184
6.65k
    }
1185
9.75k
#endif
1186
1187
1188
9.75k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
9.75k
        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.75k
    hDecoder->postSeekResetFlag = 0;
1198
1199
9.75k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
4.38k
    if (hDecoder->object_type != LD)
1202
3.79k
    {
1203
3.79k
#endif
1204
9.15k
        if (hDecoder->frame <= 1)
1205
3.40k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
595
    } else {
1208
        /* LD encoders will give lower delay */
1209
595
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
595
    }
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.75k
    return sample_buffer;
1225
1226
22.5k
error:
1227
1228
    /* reset filterbank state */
1229
1.46M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.44M
    {
1231
1.44M
        if (hDecoder->fb_intermed[i] != NULL)
1232
642k
        {
1233
642k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
642k
        }
1235
1.44M
    }
1236
22.5k
#ifdef SBR_DEC
1237
1.10M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
1.08M
    {
1239
1.08M
        if (hDecoder->sbr[i] != NULL)
1240
467k
        {
1241
467k
            sbrReset(hDecoder->sbr[i]);
1242
467k
        }
1243
1.08M
    }
1244
22.5k
#endif
1245
1246
1247
22.5k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
9.76k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
30.7k
{
855
30.7k
    uint16_t i;
856
30.7k
    uint8_t channels = 0;
857
30.7k
    uint8_t output_channels = 0;
858
30.7k
    bitfile ld;
859
30.7k
    uint32_t bitsconsumed;
860
30.7k
    uint16_t frame_len;
861
30.7k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
30.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
30.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
30.7k
    frame_len = hDecoder->frameLength;
882
883
884
30.7k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
30.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
30.7k
    if (buffer_size >= 128)
903
2.87k
    {
904
2.87k
        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.87k
    }
912
913
914
    /* initialize the bitstream */
915
30.7k
    faad_initbits(&ld, buffer, buffer_size);
916
30.7k
    if (ld.error != 0)
917
12.3k
        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.3k
#ifdef DRM
951
18.3k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.55k
    {
953
        /* We do not support stereo right now */
954
1.55k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.55k
        faad_getbits(&ld, 8
961
1.55k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.55k
    }
963
18.3k
#endif
964
965
18.3k
    if (hDecoder->adts_header_present)
966
152
    {
967
152
        adts_header adts;
968
969
152
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
152
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
9
            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
152
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
18.3k
#ifdef DRM
985
18.3k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.55k
    {
987
1.55k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
16.8k
    } else {
989
16.8k
#endif
990
16.8k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
16.8k
#ifdef DRM
992
16.8k
    }
993
18.3k
#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.3k
    channels = hDecoder->fr_channels;
1009
1010
18.3k
    if (hInfo->error > 0)
1011
12.1k
        goto error;
1012
1013
    /* safety check */
1014
6.21k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
56
    {
1016
        /* invalid number of channels */
1017
56
        hInfo->error = 12;
1018
56
        goto error;
1019
56
    }
1020
1021
    /* no more bit reading after this */
1022
6.16k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
6.16k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
6.16k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
6.16k
    faad_endbits(&ld);
1030
1031
1032
6.16k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
6.16k
        )
1037
6.10k
    {
1038
6.10k
        if (hDecoder->channelConfiguration == 0)
1039
219
            hDecoder->channelConfiguration = channels;
1040
1041
6.10k
        if (channels == 8) /* 7.1 */
1042
231
            hDecoder->channelConfiguration = 7;
1043
6.10k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
6.10k
    }
1046
1047
6.16k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
734
    {
1049
734
        hDecoder->downMatrix = 1;
1050
734
        output_channels = 2;
1051
5.42k
    } else {
1052
5.42k
        output_channels = channels;
1053
5.42k
    }
1054
1055
6.16k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
6.16k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
6.16k
    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
6.16k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
6.16k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
271
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
271
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
271
    }
1076
6.15k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
6.15k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
6.15k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
6.15k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
6.15k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
6.15k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
6.15k
    hInfo->header_type = RAW;
1090
6.15k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
6.15k
    if (hDecoder->adts_header_present)
1093
60
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
6.15k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
6.15k
    hInfo->ps = hDecoder->ps_used_global;
1100
6.15k
#endif
1101
1102
    /* check if frame has channel elements */
1103
6.15k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
6.15k
    {
1110
6.15k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
6.15k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
6.15k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
6.15k
        };
1114
6.15k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
6.15k
#ifdef SBR_DEC
1116
6.15k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.35k
        {
1118
3.35k
            stride = 2 * stride;
1119
3.35k
        }
1120
6.15k
#endif
1121
6.15k
        required_buffer_size = frame_len*output_channels*stride;
1122
6.15k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
6.15k
    if (sample_buffer_size == 0)
1126
4.01k
    {
1127
        /* allocate the buffer for the final samples */
1128
4.01k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
3.37k
        {
1130
3.37k
            if (hDecoder->sample_buffer)
1131
57
                faad_free(hDecoder->sample_buffer);
1132
3.37k
            hDecoder->sample_buffer = NULL;
1133
3.37k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
3.37k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
3.37k
        }
1136
4.01k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
784
        hInfo->error = 27;
1139
784
        return NULL;
1140
784
    }
1141
1142
5.37k
    if (sample_buffer_size == 0)
1143
4.01k
    {
1144
4.01k
        sample_buffer = hDecoder->sample_buffer;
1145
4.01k
    } else {
1146
1.35k
        sample_buffer = *sample_buffer2;
1147
1.35k
    }
1148
1149
5.37k
#ifdef SBR_DEC
1150
5.37k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.59k
    {
1152
3.59k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.59k
        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.4k
        {
1165
12.4k
            if (hDecoder->sbr[ele] == NULL)
1166
5
            {
1167
5
                hInfo->error = 25;
1168
5
                goto error;
1169
5
            }
1170
12.4k
        }
1171
1172
        /* sbr */
1173
3.58k
        if (hDecoder->sbr_present_flag == 1)
1174
2.57k
        {
1175
2.57k
            hInfo->object_type = HE_AAC;
1176
2.57k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.57k
        } else {
1178
1.00k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.00k
        }
1180
3.58k
        if (hDecoder->downSampledSBR)
1181
800
        {
1182
800
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
800
        }
1184
3.58k
    }
1185
5.36k
#endif
1186
1187
1188
5.36k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
5.36k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
5.36k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
5.36k
#endif
1195
1196
1197
5.36k
    hDecoder->postSeekResetFlag = 0;
1198
1199
5.36k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
5.36k
        if (hDecoder->frame <= 1)
1205
1.85k
            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.36k
    return sample_buffer;
1225
1226
12.2k
error:
1227
1228
    /* reset filterbank state */
1229
793k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
781k
    {
1231
781k
        if (hDecoder->fb_intermed[i] != NULL)
1232
275k
        {
1233
275k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
275k
        }
1235
781k
    }
1236
12.2k
#ifdef SBR_DEC
1237
598k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
586k
    {
1239
586k
        if (hDecoder->sbr[i] != NULL)
1240
211k
        {
1241
211k
            sbrReset(hDecoder->sbr[i]);
1242
211k
        }
1243
586k
    }
1244
12.2k
#endif
1245
1246
1247
12.2k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
5.37k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
24.7k
{
855
24.7k
    uint16_t i;
856
24.7k
    uint8_t channels = 0;
857
24.7k
    uint8_t output_channels = 0;
858
24.7k
    bitfile ld;
859
24.7k
    uint32_t bitsconsumed;
860
24.7k
    uint16_t frame_len;
861
24.7k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
24.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
24.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
24.7k
    frame_len = hDecoder->frameLength;
882
883
884
24.7k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
24.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
24.7k
    if (buffer_size >= 128)
903
3.43k
    {
904
3.43k
        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.43k
    }
912
913
914
    /* initialize the bitstream */
915
24.7k
    faad_initbits(&ld, buffer, buffer_size);
916
24.7k
    if (ld.error != 0)
917
9.52k
        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.2k
    if (hDecoder->adts_header_present)
966
637
    {
967
637
        adts_header adts;
968
969
637
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
637
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
81
            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
637
    }
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.95k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
81
    {
1016
        /* invalid number of channels */
1017
81
        hInfo->error = 12;
1018
81
        goto error;
1019
81
    }
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
133
            hDecoder->channelConfiguration = channels;
1040
1041
4.69k
        if (channels == 8) /* 7.1 */
1042
634
            hDecoder->channelConfiguration = 7;
1043
4.69k
        if (channels == 7) /* not a standard channelConfiguration */
1044
44
            hDecoder->channelConfiguration = 0;
1045
4.69k
    }
1046
1047
4.86k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
449
    {
1049
449
        hDecoder->downMatrix = 1;
1050
449
        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
190
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
190
        hDecoder->upMatrix = 1;
1062
190
        output_channels = 2;
1063
190
    }
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
152
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
152
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
152
    }
1076
4.86k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
4.86k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
4.86k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
4.86k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
4.86k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
4.86k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
4.86k
    hInfo->header_type = RAW;
1090
4.86k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
4.86k
    if (hDecoder->adts_header_present)
1093
173
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
4.86k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
4.86k
    hInfo->ps = hDecoder->ps_used_global;
1100
4.86k
#endif
1101
1102
    /* check if frame has channel elements */
1103
4.86k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
4.86k
    {
1110
4.86k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
4.86k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
4.86k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
4.86k
        };
1114
4.86k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
4.86k
#ifdef SBR_DEC
1116
4.86k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
2.83k
        {
1118
2.83k
            stride = 2 * stride;
1119
2.83k
        }
1120
4.86k
#endif
1121
4.86k
        required_buffer_size = frame_len*output_channels*stride;
1122
4.86k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
4.86k
    if (sample_buffer_size == 0)
1126
3.61k
    {
1127
        /* allocate the buffer for the final samples */
1128
3.61k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
3.07k
        {
1130
3.07k
            if (hDecoder->sample_buffer)
1131
42
                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.61k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
469
        hInfo->error = 27;
1139
469
        return NULL;
1140
469
    }
1141
1142
4.39k
    if (sample_buffer_size == 0)
1143
3.61k
    {
1144
3.61k
        sample_buffer = hDecoder->sample_buffer;
1145
3.61k
    } else {
1146
776
        sample_buffer = *sample_buffer2;
1147
776
    }
1148
1149
4.39k
#ifdef SBR_DEC
1150
4.39k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.07k
    {
1152
3.07k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.07k
        if (!hDecoder->downSampledSBR)
1156
2.49k
        {
1157
2.49k
            frame_len *= 2;
1158
2.49k
            hInfo->samples *= 2;
1159
2.49k
            hInfo->samplerate *= 2;
1160
2.49k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
16.8k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
13.8k
        {
1165
13.8k
            if (hDecoder->sbr[ele] == NULL)
1166
7
            {
1167
7
                hInfo->error = 25;
1168
7
                goto error;
1169
7
            }
1170
13.8k
        }
1171
1172
        /* sbr */
1173
3.06k
        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.18k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.18k
        }
1180
3.06k
        if (hDecoder->downSampledSBR)
1181
573
        {
1182
573
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
573
        }
1184
3.06k
    }
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.79k
    {
1203
3.79k
#endif
1204
3.79k
        if (hDecoder->frame <= 1)
1205
1.54k
            hInfo->samples = 0;
1206
3.79k
#ifdef LD_DEC
1207
3.79k
    } else {
1208
        /* LD encoders will give lower delay */
1209
595
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
595
    }
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
672k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
662k
    {
1231
662k
        if (hDecoder->fb_intermed[i] != NULL)
1232
366k
        {
1233
366k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
366k
        }
1235
662k
    }
1236
10.3k
#ifdef SBR_DEC
1237
506k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
496k
    {
1239
496k
        if (hDecoder->sbr[i] != NULL)
1240
256k
        {
1241
256k
            sbrReset(hDecoder->sbr[i]);
1242
256k
        }
1243
496k
    }
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.39k
}