Coverage Report

Created: 2026-01-10 06:29

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
326
{
74
326
    static char *libfaadName = PACKAGE_VERSION;
75
326
    static char *libCopyright =
76
326
        " Copyright 2002-2004: Ahead Software AG\n"
77
326
  " http://www.audiocoding.com\n"
78
326
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
326
    if (faad_id_string)
81
326
        *faad_id_string = libfaadName;
82
83
326
    if (faad_copyright_string)
84
326
        *faad_copyright_string = libCopyright;
85
86
326
    return 0;
87
326
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
326
{
91
326
    if (errcode >= NUM_ERROR_MESSAGES)
92
184
        return NULL;
93
142
    return err_msg[errcode];
94
326
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.30k
{
98
1.30k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.30k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
326
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
652
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
652
    cap += LD_DEC_CAP;
111
#endif
112
1.30k
#ifdef ERROR_RESILIENCE
113
1.30k
    cap += ERROR_RESILIENCE_CAP;
114
1.30k
#endif
115
#ifdef FIXED_POINT
116
652
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.30k
    return cap;
120
1.30k
}
NeAACDecGetCapabilities
Line
Count
Source
97
326
{
98
326
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
326
    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
326
#ifdef ERROR_RESILIENCE
113
326
    cap += ERROR_RESILIENCE_CAP;
114
326
#endif
115
326
#ifdef FIXED_POINT
116
326
    cap += FIXED_POINT_CAP;
117
326
#endif
118
119
326
    return cap;
120
326
}
NeAACDecGetCapabilities
Line
Count
Source
97
326
{
98
326
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
326
    cap += LC_DEC_CAP;
102
103
326
#ifdef MAIN_DEC
104
326
    cap += MAIN_DEC_CAP;
105
326
#endif
106
326
#ifdef LTP_DEC
107
326
    cap += LTP_DEC_CAP;
108
326
#endif
109
326
#ifdef LD_DEC
110
326
    cap += LD_DEC_CAP;
111
326
#endif
112
326
#ifdef ERROR_RESILIENCE
113
326
    cap += ERROR_RESILIENCE_CAP;
114
326
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
326
    return cap;
120
326
}
NeAACDecGetCapabilities
Line
Count
Source
97
326
{
98
326
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
326
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
326
#ifdef LTP_DEC
107
326
    cap += LTP_DEC_CAP;
108
326
#endif
109
326
#ifdef LD_DEC
110
326
    cap += LD_DEC_CAP;
111
326
#endif
112
326
#ifdef ERROR_RESILIENCE
113
326
    cap += ERROR_RESILIENCE_CAP;
114
326
#endif
115
326
#ifdef FIXED_POINT
116
326
    cap += FIXED_POINT_CAP;
117
326
#endif
118
119
326
    return cap;
120
326
}
NeAACDecGetCapabilities
Line
Count
Source
97
326
{
98
326
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
326
    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
326
#ifdef ERROR_RESILIENCE
113
326
    cap += ERROR_RESILIENCE_CAP;
114
326
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
326
    return cap;
120
326
}
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.5k
{
125
59.5k
    uint8_t i;
126
59.5k
    NeAACDecStruct *hDecoder = NULL;
127
128
59.5k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
59.5k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
59.5k
    hDecoder->cmes = mes;
134
59.5k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
59.5k
    hDecoder->config.defObjectType = MAIN;
136
59.5k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
59.5k
    hDecoder->config.downMatrix = 0;
138
59.5k
    hDecoder->adts_header_present = 0;
139
59.5k
    hDecoder->adif_header_present = 0;
140
59.5k
    hDecoder->latm_header_present = 0;
141
59.5k
#ifdef ERROR_RESILIENCE
142
59.5k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
59.5k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
59.5k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
59.5k
#endif
146
59.5k
    hDecoder->frameLength = 1024;
147
148
59.5k
    hDecoder->frame = 0;
149
59.5k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
59.5k
    hDecoder->__r1 = 0x2bb431ea;
153
59.5k
    hDecoder->__r2 = 0x206155b7;
154
155
3.86M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.80M
    {
157
3.80M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.80M
        hDecoder->window_shape_prev[i] = 0;
159
3.80M
        hDecoder->time_out[i] = NULL;
160
3.80M
        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.80M
    }
173
174
59.5k
#ifdef SBR_DEC
175
2.91M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.85M
    {
177
2.85M
        hDecoder->sbr[i] = NULL;
178
2.85M
    }
179
59.5k
#endif
180
181
59.5k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
59.5k
    return hDecoder;
184
59.5k
}
NeAACDecOpen
Line
Count
Source
124
11.4k
{
125
11.4k
    uint8_t i;
126
11.4k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.4k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.4k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.4k
    hDecoder->cmes = mes;
134
11.4k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.4k
    hDecoder->config.defObjectType = MAIN;
136
11.4k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.4k
    hDecoder->config.downMatrix = 0;
138
11.4k
    hDecoder->adts_header_present = 0;
139
11.4k
    hDecoder->adif_header_present = 0;
140
11.4k
    hDecoder->latm_header_present = 0;
141
11.4k
#ifdef ERROR_RESILIENCE
142
11.4k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.4k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.4k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.4k
#endif
146
11.4k
    hDecoder->frameLength = 1024;
147
148
11.4k
    hDecoder->frame = 0;
149
11.4k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.4k
    hDecoder->__r1 = 0x2bb431ea;
153
11.4k
    hDecoder->__r2 = 0x206155b7;
154
155
745k
    for (i = 0; i < MAX_CHANNELS; i++)
156
734k
    {
157
734k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
734k
        hDecoder->window_shape_prev[i] = 0;
159
734k
        hDecoder->time_out[i] = NULL;
160
734k
        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
734k
    }
173
174
11.4k
#ifdef SBR_DEC
175
561k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
550k
    {
177
550k
        hDecoder->sbr[i] = NULL;
178
550k
    }
179
11.4k
#endif
180
181
11.4k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.4k
    return hDecoder;
184
11.4k
}
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.17M
    {
157
1.17M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.17M
        hDecoder->window_shape_prev[i] = 0;
159
1.17M
        hDecoder->time_out[i] = NULL;
160
1.17M
        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.17M
#ifdef MAIN_DEC
166
1.17M
        hDecoder->pred_stat[i] = NULL;
167
1.17M
#endif
168
1.17M
#ifdef LTP_DEC
169
1.17M
        hDecoder->ltp_lag[i] = 0;
170
1.17M
        hDecoder->lt_pred_stat[i] = NULL;
171
1.17M
#endif
172
1.17M
    }
173
174
18.2k
#ifdef SBR_DEC
175
896k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
877k
    {
177
877k
        hDecoder->sbr[i] = NULL;
178
877k
    }
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.4k
{
125
11.4k
    uint8_t i;
126
11.4k
    NeAACDecStruct *hDecoder = NULL;
127
128
11.4k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
11.4k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
11.4k
    hDecoder->cmes = mes;
134
11.4k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
11.4k
    hDecoder->config.defObjectType = MAIN;
136
11.4k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
11.4k
    hDecoder->config.downMatrix = 0;
138
11.4k
    hDecoder->adts_header_present = 0;
139
11.4k
    hDecoder->adif_header_present = 0;
140
11.4k
    hDecoder->latm_header_present = 0;
141
11.4k
#ifdef ERROR_RESILIENCE
142
11.4k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
11.4k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
11.4k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
11.4k
#endif
146
11.4k
    hDecoder->frameLength = 1024;
147
148
11.4k
    hDecoder->frame = 0;
149
11.4k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
11.4k
    hDecoder->__r1 = 0x2bb431ea;
153
11.4k
    hDecoder->__r2 = 0x206155b7;
154
155
745k
    for (i = 0; i < MAX_CHANNELS; i++)
156
734k
    {
157
734k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
734k
        hDecoder->window_shape_prev[i] = 0;
159
734k
        hDecoder->time_out[i] = NULL;
160
734k
        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
734k
#ifdef LTP_DEC
169
734k
        hDecoder->ltp_lag[i] = 0;
170
734k
        hDecoder->lt_pred_stat[i] = NULL;
171
734k
#endif
172
734k
    }
173
174
11.4k
#ifdef SBR_DEC
175
561k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
550k
    {
177
550k
        hDecoder->sbr[i] = NULL;
178
550k
    }
179
11.4k
#endif
180
181
11.4k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
11.4k
    return hDecoder;
184
11.4k
}
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.17M
    {
157
1.17M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.17M
        hDecoder->window_shape_prev[i] = 0;
159
1.17M
        hDecoder->time_out[i] = NULL;
160
1.17M
        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.17M
    }
173
174
18.2k
#ifdef SBR_DEC
175
896k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
877k
    {
177
877k
        hDecoder->sbr[i] = NULL;
178
877k
    }
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.6k
{
188
28.6k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
28.6k
    if (hDecoder)
190
28.6k
    {
191
28.6k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
28.6k
        return config;
194
28.6k
    }
195
196
0
    return NULL;
197
28.6k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
28.9k
{
202
28.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
28.9k
    if (hDecoder && config)
204
28.9k
    {
205
        /* check if we can decode this object type */
206
28.9k
        if (can_decode_ot(config->defObjectType) < 0)
207
17
            return 0;
208
28.9k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
28.9k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
28.9k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
28.9k
#ifdef FIXED_POINT
217
28.9k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
203
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
28.7k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
28.7k
        if (config->downMatrix > 1)
226
26
            return 0;
227
28.6k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
28.6k
        return 1;
231
28.7k
    }
232
233
0
    return 0;
234
28.9k
}
235
236
237
#if 0
238
static int latmCheck(latm_header *latm, bitfile *ld)
239
{
240
    uint32_t good=0, bad=0, bits, m;
241
242
    while (ld->bytes_left)
243
    {
244
        bits = faad_latm_frame(latm, ld);
245
        if(bits==0xFFFFFFFF)
246
            bad++;
247
        else
248
        {
249
            good++;
250
            while(bits>0)
251
            {
252
                m = min(bits, 8);
253
                faad_getbits(ld, m);
254
                bits -= m;
255
            }
256
        }
257
    }
258
259
    return (good>0);
260
}
261
#endif
262
263
long NeAACDecInit(NeAACDecHandle hpDecoder,
264
                              unsigned char *buffer,
265
                              unsigned long buffer_size,
266
                              unsigned long *samplerate,
267
                              unsigned char *channels)
268
19.9k
{
269
19.9k
    uint32_t bits = 0;
270
19.9k
    bitfile ld;
271
19.9k
    adif_header adif;
272
19.9k
    adts_header adts;
273
19.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
19.9k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
54
        return -1;
278
279
19.8k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
19.8k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
19.8k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
19.8k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
19.8k
    *channels = 1;
284
285
19.8k
    if (buffer != NULL)
286
19.8k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
19.8k
        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.8k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
460
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
409
        {
314
409
            hDecoder->adif_header_present = 1;
315
316
409
            get_adif_header(&adif, &ld);
317
409
            faad_byte_align(&ld);
318
319
409
            hDecoder->sf_index = adif.pce[0].sf_index;
320
409
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
409
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
409
            *channels = adif.pce[0].channels;
324
325
409
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
409
            hDecoder->pce_set = 1;
327
328
409
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
19.4k
        } else if (adts_frame(&adts, &ld) == 0) {
332
586
            hDecoder->adts_header_present = 1;
333
334
586
            hDecoder->sf_index = adts.sf_index;
335
586
            hDecoder->object_type = adts.profile + 1;
336
337
586
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
586
            *channels = (adts.channel_configuration > 6) ?
339
529
                2 : adts.channel_configuration;
340
586
        }
341
342
19.8k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
19.8k
        faad_endbits(&ld);
348
19.8k
    }
349
350
19.8k
    if (!*samplerate)
351
157
        return -1;
352
353
19.6k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
19.6k
    if (*channels == 1)
356
18.8k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
18.8k
        *channels = 2;
359
18.8k
    }
360
19.6k
#endif
361
362
19.6k
    hDecoder->channelConfiguration = *channels;
363
364
19.6k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
19.6k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
9.17k
    {
368
9.17k
        *samplerate *= 2;
369
9.17k
        hDecoder->forceUpSampling = 1;
370
10.5k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
10.5k
        hDecoder->downSampledSBR = 1;
372
10.5k
    }
373
19.6k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
19.6k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
12.8k
    if (hDecoder->object_type == LD)
385
528
        hDecoder->frameLength >>= 1;
386
#endif
387
388
19.6k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
209
        return -1;
390
391
19.4k
    return bits;
392
19.6k
}
NeAACDecInit
Line
Count
Source
268
6.94k
{
269
6.94k
    uint32_t bits = 0;
270
6.94k
    bitfile ld;
271
6.94k
    adif_header adif;
272
6.94k
    adts_header adts;
273
6.94k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
6.94k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
23
        return -1;
278
279
6.91k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
6.91k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
6.91k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
6.91k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
6.91k
    *channels = 1;
284
285
6.91k
    if (buffer != NULL)
286
6.91k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
6.91k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
6.91k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
193
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
167
        {
314
167
            hDecoder->adif_header_present = 1;
315
316
167
            get_adif_header(&adif, &ld);
317
167
            faad_byte_align(&ld);
318
319
167
            hDecoder->sf_index = adif.pce[0].sf_index;
320
167
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
167
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
167
            *channels = adif.pce[0].channels;
324
325
167
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
167
            hDecoder->pce_set = 1;
327
328
167
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
6.75k
        } else if (adts_frame(&adts, &ld) == 0) {
332
101
            hDecoder->adts_header_present = 1;
333
334
101
            hDecoder->sf_index = adts.sf_index;
335
101
            hDecoder->object_type = adts.profile + 1;
336
337
101
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
101
            *channels = (adts.channel_configuration > 6) ?
339
88
                2 : adts.channel_configuration;
340
101
        }
341
342
6.91k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
6.91k
        faad_endbits(&ld);
348
6.91k
    }
349
350
6.91k
    if (!*samplerate)
351
73
        return -1;
352
353
6.84k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
6.84k
    if (*channels == 1)
356
6.65k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
6.65k
        *channels = 2;
359
6.65k
    }
360
6.84k
#endif
361
362
6.84k
    hDecoder->channelConfiguration = *channels;
363
364
6.84k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
6.84k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
3.24k
    {
368
3.24k
        *samplerate *= 2;
369
3.24k
        hDecoder->forceUpSampling = 1;
370
3.60k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
3.60k
        hDecoder->downSampledSBR = 1;
372
3.60k
    }
373
6.84k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
6.84k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
    if (hDecoder->object_type == LD)
385
        hDecoder->frameLength >>= 1;
386
#endif
387
388
6.84k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
109
        return -1;
390
391
6.73k
    return bits;
392
6.84k
}
NeAACDecInit
Line
Count
Source
268
12.9k
{
269
12.9k
    uint32_t bits = 0;
270
12.9k
    bitfile ld;
271
12.9k
    adif_header adif;
272
12.9k
    adts_header adts;
273
12.9k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
12.9k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
31
        return -1;
278
279
12.9k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
12.9k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
12.9k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
12.9k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
12.9k
    *channels = 1;
284
285
12.9k
    if (buffer != NULL)
286
12.9k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
12.9k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
12.9k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
267
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
242
        {
314
242
            hDecoder->adif_header_present = 1;
315
316
242
            get_adif_header(&adif, &ld);
317
242
            faad_byte_align(&ld);
318
319
242
            hDecoder->sf_index = adif.pce[0].sf_index;
320
242
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
242
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
242
            *channels = adif.pce[0].channels;
324
325
242
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
242
            hDecoder->pce_set = 1;
327
328
242
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
12.6k
        } else if (adts_frame(&adts, &ld) == 0) {
332
485
            hDecoder->adts_header_present = 1;
333
334
485
            hDecoder->sf_index = adts.sf_index;
335
485
            hDecoder->object_type = adts.profile + 1;
336
337
485
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
485
            *channels = (adts.channel_configuration > 6) ?
339
441
                2 : adts.channel_configuration;
340
485
        }
341
342
12.9k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
12.9k
        faad_endbits(&ld);
348
12.9k
    }
349
350
12.9k
    if (!*samplerate)
351
84
        return -1;
352
353
12.8k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
12.8k
    if (*channels == 1)
356
12.2k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
12.2k
        *channels = 2;
359
12.2k
    }
360
12.8k
#endif
361
362
12.8k
    hDecoder->channelConfiguration = *channels;
363
364
12.8k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
12.8k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
5.93k
    {
368
5.93k
        *samplerate *= 2;
369
5.93k
        hDecoder->forceUpSampling = 1;
370
6.91k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
6.91k
        hDecoder->downSampledSBR = 1;
372
6.91k
    }
373
12.8k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
12.8k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
12.8k
#ifdef LD_DEC
384
12.8k
    if (hDecoder->object_type == LD)
385
528
        hDecoder->frameLength >>= 1;
386
12.8k
#endif
387
388
12.8k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
100
        return -1;
390
391
12.7k
    return bits;
392
12.8k
}
393
394
/* Init the library using a DecoderSpecificInfo */
395
char NeAACDecInit2(NeAACDecHandle hpDecoder,
396
                               unsigned char *pBuffer,
397
                               unsigned long SizeOfDecoderSpecificInfo,
398
                               unsigned long *samplerate,
399
                               unsigned char *channels)
400
8.79k
{
401
8.79k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
8.79k
    int8_t rc;
403
8.79k
    mp4AudioSpecificConfig mp4ASC;
404
405
8.79k
    if((hDecoder == NULL)
406
8.79k
        || (pBuffer == NULL)
407
8.79k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.77k
        || (samplerate == NULL)
409
8.77k
        || (channels == NULL))
410
20
    {
411
20
        return -1;
412
20
    }
413
414
8.77k
    hDecoder->adif_header_present = 0;
415
8.77k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.77k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.77k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.77k
    *samplerate = mp4ASC.samplingFrequency;
423
8.77k
    if (mp4ASC.channelsConfiguration)
424
7.35k
    {
425
7.35k
        *channels = mp4ASC.channelsConfiguration;
426
7.35k
    } else {
427
1.41k
        *channels = hDecoder->pce.channels;
428
1.41k
        hDecoder->pce_set = 1;
429
1.41k
    }
430
8.77k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.77k
    if (*channels == 1)
433
34
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
34
        *channels = 2;
436
34
    }
437
8.77k
#endif
438
8.77k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.77k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.77k
#ifdef ERROR_RESILIENCE
441
8.77k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.77k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.77k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.77k
#endif
445
8.77k
#ifdef SBR_DEC
446
8.77k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.77k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.77k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.77k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
8.77k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
5.55k
    {
456
5.55k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
5.55k
    }
458
8.77k
#endif
459
460
8.77k
    if (rc != 0)
461
101
    {
462
101
        return rc;
463
101
    }
464
8.67k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.67k
    if (mp4ASC.frameLengthFlag)
466
5.41k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
5.41k
        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.67k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
5.78k
    if (hDecoder->object_type == LD)
482
597
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.67k
    return 0;
486
8.77k
}
NeAACDecInit2
Line
Count
Source
400
2.92k
{
401
2.92k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
2.92k
    int8_t rc;
403
2.92k
    mp4AudioSpecificConfig mp4ASC;
404
405
2.92k
    if((hDecoder == NULL)
406
2.92k
        || (pBuffer == NULL)
407
2.92k
        || (SizeOfDecoderSpecificInfo < 2)
408
2.91k
        || (samplerate == NULL)
409
2.91k
        || (channels == NULL))
410
8
    {
411
8
        return -1;
412
8
    }
413
414
2.91k
    hDecoder->adif_header_present = 0;
415
2.91k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
2.91k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
2.91k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
2.91k
    *samplerate = mp4ASC.samplingFrequency;
423
2.91k
    if (mp4ASC.channelsConfiguration)
424
2.32k
    {
425
2.32k
        *channels = mp4ASC.channelsConfiguration;
426
2.32k
    } else {
427
594
        *channels = hDecoder->pce.channels;
428
594
        hDecoder->pce_set = 1;
429
594
    }
430
2.91k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
2.91k
    if (*channels == 1)
433
20
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
20
        *channels = 2;
436
20
    }
437
2.91k
#endif
438
2.91k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
2.91k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
2.91k
#ifdef ERROR_RESILIENCE
441
2.91k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
2.91k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
2.91k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
2.91k
#endif
445
2.91k
#ifdef SBR_DEC
446
2.91k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
2.91k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
2.91k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
2.91k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
2.91k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
1.75k
    {
456
1.75k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
1.75k
    }
458
2.91k
#endif
459
460
2.91k
    if (rc != 0)
461
32
    {
462
32
        return rc;
463
32
    }
464
2.88k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
2.88k
    if (mp4ASC.frameLengthFlag)
466
1.94k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
1.94k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
2.88k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
    if (hDecoder->object_type == LD)
482
        hDecoder->frameLength >>= 1;
483
#endif
484
485
2.88k
    return 0;
486
2.91k
}
NeAACDecInit2
Line
Count
Source
400
5.86k
{
401
5.86k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
5.86k
    int8_t rc;
403
5.86k
    mp4AudioSpecificConfig mp4ASC;
404
405
5.86k
    if((hDecoder == NULL)
406
5.86k
        || (pBuffer == NULL)
407
5.86k
        || (SizeOfDecoderSpecificInfo < 2)
408
5.85k
        || (samplerate == NULL)
409
5.85k
        || (channels == NULL))
410
12
    {
411
12
        return -1;
412
12
    }
413
414
5.85k
    hDecoder->adif_header_present = 0;
415
5.85k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
5.85k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
5.85k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
5.85k
    *samplerate = mp4ASC.samplingFrequency;
423
5.85k
    if (mp4ASC.channelsConfiguration)
424
5.03k
    {
425
5.03k
        *channels = mp4ASC.channelsConfiguration;
426
5.03k
    } else {
427
823
        *channels = hDecoder->pce.channels;
428
823
        hDecoder->pce_set = 1;
429
823
    }
430
5.85k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
5.85k
    if (*channels == 1)
433
14
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
14
        *channels = 2;
436
14
    }
437
5.85k
#endif
438
5.85k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
5.85k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
5.85k
#ifdef ERROR_RESILIENCE
441
5.85k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
5.85k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
5.85k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
5.85k
#endif
445
5.85k
#ifdef SBR_DEC
446
5.85k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
5.85k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
5.85k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
5.85k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
5.85k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
3.79k
    {
456
3.79k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
3.79k
    }
458
5.85k
#endif
459
460
5.85k
    if (rc != 0)
461
69
    {
462
69
        return rc;
463
69
    }
464
5.78k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
5.78k
    if (mp4ASC.frameLengthFlag)
466
3.46k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.46k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
5.78k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
5.78k
#ifdef LD_DEC
481
5.78k
    if (hDecoder->object_type == LD)
482
597
        hDecoder->frameLength >>= 1;
483
5.78k
#endif
484
485
5.78k
    return 0;
486
5.85k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
811
{
493
811
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
811
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
811
    NeAACDecClose(*hDecoder);
498
499
811
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
811
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
811
    (*hDecoder)->config.defSampleRate = samplerate;
505
811
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
811
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
811
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
811
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
811
#endif
510
811
    (*hDecoder)->frameLength = 960;
511
811
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
811
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
811
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
127
        (*hDecoder)->channelConfiguration = 2;
516
684
    else
517
684
        (*hDecoder)->channelConfiguration = 1;
518
519
811
#ifdef SBR_DEC
520
811
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
101
        (*hDecoder)->sbr_present_flag = 0;
522
710
    else
523
710
        (*hDecoder)->sbr_present_flag = 1;
524
811
#endif
525
526
811
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
811
    return 0;
529
811
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
10.7k
{
534
10.7k
    uint8_t i;
535
10.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
10.7k
    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
699k
    for (i = 0; i < MAX_CHANNELS; i++)
549
688k
    {
550
688k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
688k
        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
688k
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
10.7k
        filter_bank_end(hDecoder->fb);
570
571
10.7k
    drc_end(hDecoder->drc);
572
573
10.7k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
10.7k
#ifdef SBR_DEC
576
527k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
516k
    {
578
516k
        if (hDecoder->sbr[i])
579
130k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
516k
    }
581
10.7k
#endif
582
583
10.7k
    if (hDecoder) faad_free(hDecoder);
584
10.7k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
22.1k
{
588
22.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
22.1k
    if (hDecoder)
590
22.1k
    {
591
22.1k
        hDecoder->postSeekResetFlag = 1;
592
593
22.1k
        if (frame != -1)
594
22.1k
            hDecoder->frame = frame;
595
22.1k
    }
596
22.1k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
42.1k
{
600
42.1k
    hInfo->num_front_channels = 0;
601
42.1k
    hInfo->num_side_channels = 0;
602
42.1k
    hInfo->num_back_channels = 0;
603
42.1k
    hInfo->num_lfe_channels = 0;
604
42.1k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
42.1k
    if (hDecoder->downMatrix)
607
4.53k
    {
608
4.53k
        hInfo->num_front_channels = 2;
609
4.53k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.53k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.53k
        return;
612
4.53k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
37.6k
    if (hDecoder->pce_set)
617
1.47k
    {
618
1.47k
        uint8_t i, chpos = 0;
619
1.47k
        uint8_t chdir, back_center = 0;
620
621
1.47k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.47k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.47k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.47k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.47k
        chdir = hInfo->num_front_channels;
626
1.47k
        if (chdir & 1)
627
616
        {
628
616
#if (defined(PS_DEC) || defined(DRM_PS))
629
616
            if (hInfo->num_front_channels == 1 &&
630
304
                hInfo->num_side_channels == 0 &&
631
160
                hInfo->num_back_channels == 0 &&
632
104
                hInfo->num_lfe_channels == 0)
633
64
            {
634
                /* When PS is enabled output is always stereo */
635
64
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
64
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
64
            } else
638
552
#endif
639
552
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
616
            chdir--;
641
616
        }
642
6.87k
        for (i = 0; i < chdir; i++)
643
5.40k
        {
644
5.40k
            hInfo->channel_position[chpos++] =
645
5.40k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
5.40k
        }
647
648
5.95k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
4.48k
        {
650
4.48k
            hInfo->channel_position[chpos++] =
651
4.48k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
4.48k
        }
653
654
1.47k
        chdir = hInfo->num_back_channels;
655
1.47k
        if (chdir & 1)
656
160
        {
657
160
            back_center = 1;
658
160
            chdir--;
659
160
        }
660
4.82k
        for (i = 0; i < chdir; i++)
661
3.35k
        {
662
3.35k
            hInfo->channel_position[chpos++] =
663
3.35k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.35k
        }
665
1.47k
        if (back_center)
666
160
        {
667
160
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
160
        }
669
670
2.25k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
784
        {
672
784
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
784
        }
674
675
36.1k
    } else {
676
36.1k
        switch (hDecoder->channelConfiguration)
677
36.1k
        {
678
1.62k
        case 1:
679
1.62k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
1.62k
            hInfo->num_front_channels = 2;
682
1.62k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
1.62k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
1.62k
            break;
689
16.5k
        case 2:
690
16.5k
            hInfo->num_front_channels = 2;
691
16.5k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
16.5k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
16.5k
            break;
694
2.86k
        case 3:
695
2.86k
            hInfo->num_front_channels = 3;
696
2.86k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
2.86k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
2.86k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
2.86k
            break;
700
4.35k
        case 4:
701
4.35k
            hInfo->num_front_channels = 3;
702
4.35k
            hInfo->num_back_channels = 1;
703
4.35k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.35k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.35k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.35k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.35k
            break;
708
2.53k
        case 5:
709
2.53k
            hInfo->num_front_channels = 3;
710
2.53k
            hInfo->num_back_channels = 2;
711
2.53k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.53k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.53k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.53k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.53k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.53k
            break;
717
1.31k
        case 6:
718
1.31k
            hInfo->num_front_channels = 3;
719
1.31k
            hInfo->num_back_channels = 2;
720
1.31k
            hInfo->num_lfe_channels = 1;
721
1.31k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.31k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.31k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.31k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.31k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.31k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.31k
            break;
728
6.02k
        case 7:
729
6.02k
            hInfo->num_front_channels = 3;
730
6.02k
            hInfo->num_side_channels = 2;
731
6.02k
            hInfo->num_back_channels = 2;
732
6.02k
            hInfo->num_lfe_channels = 1;
733
6.02k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
6.02k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
6.02k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
6.02k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
6.02k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
6.02k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
6.02k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
6.02k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
6.02k
            break;
742
876
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
876
            {
744
876
                uint8_t i;
745
876
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
876
                if (ch & 1) /* there's either a center front or a center back channel */
747
356
                {
748
356
                    uint8_t ch1 = (ch-1)/2;
749
356
                    if (hDecoder->first_syn_ele == ID_SCE)
750
244
                    {
751
244
                        hInfo->num_front_channels = ch1 + 1;
752
244
                        hInfo->num_back_channels = ch1;
753
244
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
988
                        for (i = 1; i <= ch1; i+=2)
755
744
                        {
756
744
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
744
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
744
                        }
759
988
                        for (i = ch1+1; i < ch; i+=2)
760
744
                        {
761
744
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
744
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
744
                        }
764
244
                    } else {
765
112
                        hInfo->num_front_channels = ch1;
766
112
                        hInfo->num_back_channels = ch1 + 1;
767
628
                        for (i = 0; i < ch1; i+=2)
768
516
                        {
769
516
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
516
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
516
                        }
772
628
                        for (i = ch1; i < ch-1; i+=2)
773
516
                        {
774
516
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
516
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
516
                        }
777
112
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
112
                    }
779
520
                } else {
780
520
                    uint8_t ch1 = (ch)/2;
781
520
                    hInfo->num_front_channels = ch1;
782
520
                    hInfo->num_back_channels = ch1;
783
520
                    if (ch1 & 1)
784
224
                    {
785
224
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.48k
                        for (i = 1; i <= ch1; i+=2)
787
1.25k
                        {
788
1.25k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.25k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.25k
                        }
791
1.25k
                        for (i = ch1+1; i < ch-1; i+=2)
792
1.03k
                        {
793
1.03k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
1.03k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
1.03k
                        }
796
224
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
296
                    } else {
798
1.66k
                        for (i = 0; i < ch1; i+=2)
799
1.36k
                        {
800
1.36k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
1.36k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
1.36k
                        }
803
1.66k
                        for (i = ch1; i < ch; i+=2)
804
1.36k
                        {
805
1.36k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
1.36k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
1.36k
                        }
808
296
                    }
809
520
                }
810
876
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
1.76k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
892
                {
813
892
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
892
                }
815
876
            }
816
876
            break;
817
36.1k
        }
818
36.1k
    }
819
37.6k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.5k
{
600
10.5k
    hInfo->num_front_channels = 0;
601
10.5k
    hInfo->num_side_channels = 0;
602
10.5k
    hInfo->num_back_channels = 0;
603
10.5k
    hInfo->num_lfe_channels = 0;
604
10.5k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.5k
    if (hDecoder->downMatrix)
607
1.13k
    {
608
1.13k
        hInfo->num_front_channels = 2;
609
1.13k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.13k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.13k
        return;
612
1.13k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.41k
    if (hDecoder->pce_set)
617
368
    {
618
368
        uint8_t i, chpos = 0;
619
368
        uint8_t chdir, back_center = 0;
620
621
368
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
368
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
368
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
368
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
368
        chdir = hInfo->num_front_channels;
626
368
        if (chdir & 1)
627
154
        {
628
154
#if (defined(PS_DEC) || defined(DRM_PS))
629
154
            if (hInfo->num_front_channels == 1 &&
630
76
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
16
            {
634
                /* When PS is enabled output is always stereo */
635
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
16
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
154
            chdir--;
641
154
        }
642
1.71k
        for (i = 0; i < chdir; i++)
643
1.35k
        {
644
1.35k
            hInfo->channel_position[chpos++] =
645
1.35k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.35k
        }
647
648
1.48k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
368
        chdir = hInfo->num_back_channels;
655
368
        if (chdir & 1)
656
40
        {
657
40
            back_center = 1;
658
40
            chdir--;
659
40
        }
660
1.20k
        for (i = 0; i < chdir; i++)
661
838
        {
662
838
            hInfo->channel_position[chpos++] =
663
838
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
838
        }
665
368
        if (back_center)
666
40
        {
667
40
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
40
        }
669
670
564
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
196
        {
672
196
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
196
        }
674
675
9.04k
    } else {
676
9.04k
        switch (hDecoder->channelConfiguration)
677
9.04k
        {
678
405
        case 1:
679
405
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
405
            hInfo->num_front_channels = 2;
682
405
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
405
            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
405
            break;
689
4.14k
        case 2:
690
4.14k
            hInfo->num_front_channels = 2;
691
4.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.14k
            break;
694
716
        case 3:
695
716
            hInfo->num_front_channels = 3;
696
716
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
716
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
716
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
716
            break;
700
1.08k
        case 4:
701
1.08k
            hInfo->num_front_channels = 3;
702
1.08k
            hInfo->num_back_channels = 1;
703
1.08k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.08k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.08k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.08k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.08k
            break;
708
634
        case 5:
709
634
            hInfo->num_front_channels = 3;
710
634
            hInfo->num_back_channels = 2;
711
634
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
634
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
634
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
634
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
634
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
634
            break;
717
328
        case 6:
718
328
            hInfo->num_front_channels = 3;
719
328
            hInfo->num_back_channels = 2;
720
328
            hInfo->num_lfe_channels = 1;
721
328
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
328
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
328
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
328
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
328
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
328
            hInfo->channel_position[5] = LFE_CHANNEL;
727
328
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
219
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
219
            {
744
219
                uint8_t i;
745
219
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
219
                if (ch & 1) /* there's either a center front or a center back channel */
747
89
                {
748
89
                    uint8_t ch1 = (ch-1)/2;
749
89
                    if (hDecoder->first_syn_ele == ID_SCE)
750
61
                    {
751
61
                        hInfo->num_front_channels = ch1 + 1;
752
61
                        hInfo->num_back_channels = ch1;
753
61
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
247
                        for (i = 1; i <= ch1; i+=2)
755
186
                        {
756
186
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
186
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
186
                        }
759
247
                        for (i = ch1+1; i < ch; i+=2)
760
186
                        {
761
186
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
186
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
186
                        }
764
61
                    } else {
765
28
                        hInfo->num_front_channels = ch1;
766
28
                        hInfo->num_back_channels = ch1 + 1;
767
157
                        for (i = 0; i < ch1; i+=2)
768
129
                        {
769
129
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
129
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
129
                        }
772
157
                        for (i = ch1; i < ch-1; i+=2)
773
129
                        {
774
129
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
129
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
129
                        }
777
28
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
28
                    }
779
130
                } else {
780
130
                    uint8_t ch1 = (ch)/2;
781
130
                    hInfo->num_front_channels = ch1;
782
130
                    hInfo->num_back_channels = ch1;
783
130
                    if (ch1 & 1)
784
56
                    {
785
56
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
370
                        for (i = 1; i <= ch1; i+=2)
787
314
                        {
788
314
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
314
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
314
                        }
791
314
                        for (i = ch1+1; i < ch-1; i+=2)
792
258
                        {
793
258
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
258
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
258
                        }
796
56
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
416
                        for (i = 0; i < ch1; i+=2)
799
342
                        {
800
342
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
342
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
342
                        }
803
416
                        for (i = ch1; i < ch; i+=2)
804
342
                        {
805
342
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
342
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
342
                        }
808
74
                    }
809
130
                }
810
219
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
442
                for (i = ch; i < hDecoder->fr_channels; i++)
812
223
                {
813
223
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
223
                }
815
219
            }
816
219
            break;
817
9.04k
        }
818
9.04k
    }
819
9.41k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.5k
{
600
10.5k
    hInfo->num_front_channels = 0;
601
10.5k
    hInfo->num_side_channels = 0;
602
10.5k
    hInfo->num_back_channels = 0;
603
10.5k
    hInfo->num_lfe_channels = 0;
604
10.5k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.5k
    if (hDecoder->downMatrix)
607
1.13k
    {
608
1.13k
        hInfo->num_front_channels = 2;
609
1.13k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.13k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.13k
        return;
612
1.13k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.41k
    if (hDecoder->pce_set)
617
368
    {
618
368
        uint8_t i, chpos = 0;
619
368
        uint8_t chdir, back_center = 0;
620
621
368
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
368
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
368
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
368
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
368
        chdir = hInfo->num_front_channels;
626
368
        if (chdir & 1)
627
154
        {
628
154
#if (defined(PS_DEC) || defined(DRM_PS))
629
154
            if (hInfo->num_front_channels == 1 &&
630
76
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
16
            {
634
                /* When PS is enabled output is always stereo */
635
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
16
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
154
            chdir--;
641
154
        }
642
1.71k
        for (i = 0; i < chdir; i++)
643
1.35k
        {
644
1.35k
            hInfo->channel_position[chpos++] =
645
1.35k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.35k
        }
647
648
1.48k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
368
        chdir = hInfo->num_back_channels;
655
368
        if (chdir & 1)
656
40
        {
657
40
            back_center = 1;
658
40
            chdir--;
659
40
        }
660
1.20k
        for (i = 0; i < chdir; i++)
661
838
        {
662
838
            hInfo->channel_position[chpos++] =
663
838
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
838
        }
665
368
        if (back_center)
666
40
        {
667
40
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
40
        }
669
670
564
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
196
        {
672
196
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
196
        }
674
675
9.04k
    } else {
676
9.04k
        switch (hDecoder->channelConfiguration)
677
9.04k
        {
678
405
        case 1:
679
405
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
405
            hInfo->num_front_channels = 2;
682
405
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
405
            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
405
            break;
689
4.14k
        case 2:
690
4.14k
            hInfo->num_front_channels = 2;
691
4.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.14k
            break;
694
716
        case 3:
695
716
            hInfo->num_front_channels = 3;
696
716
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
716
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
716
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
716
            break;
700
1.08k
        case 4:
701
1.08k
            hInfo->num_front_channels = 3;
702
1.08k
            hInfo->num_back_channels = 1;
703
1.08k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.08k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.08k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.08k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.08k
            break;
708
634
        case 5:
709
634
            hInfo->num_front_channels = 3;
710
634
            hInfo->num_back_channels = 2;
711
634
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
634
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
634
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
634
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
634
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
634
            break;
717
328
        case 6:
718
328
            hInfo->num_front_channels = 3;
719
328
            hInfo->num_back_channels = 2;
720
328
            hInfo->num_lfe_channels = 1;
721
328
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
328
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
328
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
328
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
328
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
328
            hInfo->channel_position[5] = LFE_CHANNEL;
727
328
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
219
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
219
            {
744
219
                uint8_t i;
745
219
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
219
                if (ch & 1) /* there's either a center front or a center back channel */
747
89
                {
748
89
                    uint8_t ch1 = (ch-1)/2;
749
89
                    if (hDecoder->first_syn_ele == ID_SCE)
750
61
                    {
751
61
                        hInfo->num_front_channels = ch1 + 1;
752
61
                        hInfo->num_back_channels = ch1;
753
61
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
247
                        for (i = 1; i <= ch1; i+=2)
755
186
                        {
756
186
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
186
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
186
                        }
759
247
                        for (i = ch1+1; i < ch; i+=2)
760
186
                        {
761
186
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
186
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
186
                        }
764
61
                    } else {
765
28
                        hInfo->num_front_channels = ch1;
766
28
                        hInfo->num_back_channels = ch1 + 1;
767
157
                        for (i = 0; i < ch1; i+=2)
768
129
                        {
769
129
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
129
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
129
                        }
772
157
                        for (i = ch1; i < ch-1; i+=2)
773
129
                        {
774
129
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
129
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
129
                        }
777
28
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
28
                    }
779
130
                } else {
780
130
                    uint8_t ch1 = (ch)/2;
781
130
                    hInfo->num_front_channels = ch1;
782
130
                    hInfo->num_back_channels = ch1;
783
130
                    if (ch1 & 1)
784
56
                    {
785
56
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
370
                        for (i = 1; i <= ch1; i+=2)
787
314
                        {
788
314
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
314
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
314
                        }
791
314
                        for (i = ch1+1; i < ch-1; i+=2)
792
258
                        {
793
258
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
258
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
258
                        }
796
56
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
416
                        for (i = 0; i < ch1; i+=2)
799
342
                        {
800
342
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
342
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
342
                        }
803
416
                        for (i = ch1; i < ch; i+=2)
804
342
                        {
805
342
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
342
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
342
                        }
808
74
                    }
809
130
                }
810
219
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
442
                for (i = ch; i < hDecoder->fr_channels; i++)
812
223
                {
813
223
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
223
                }
815
219
            }
816
219
            break;
817
9.04k
        }
818
9.04k
    }
819
9.41k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.5k
{
600
10.5k
    hInfo->num_front_channels = 0;
601
10.5k
    hInfo->num_side_channels = 0;
602
10.5k
    hInfo->num_back_channels = 0;
603
10.5k
    hInfo->num_lfe_channels = 0;
604
10.5k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.5k
    if (hDecoder->downMatrix)
607
1.13k
    {
608
1.13k
        hInfo->num_front_channels = 2;
609
1.13k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.13k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.13k
        return;
612
1.13k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.41k
    if (hDecoder->pce_set)
617
368
    {
618
368
        uint8_t i, chpos = 0;
619
368
        uint8_t chdir, back_center = 0;
620
621
368
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
368
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
368
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
368
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
368
        chdir = hInfo->num_front_channels;
626
368
        if (chdir & 1)
627
154
        {
628
154
#if (defined(PS_DEC) || defined(DRM_PS))
629
154
            if (hInfo->num_front_channels == 1 &&
630
76
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
16
            {
634
                /* When PS is enabled output is always stereo */
635
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
16
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
154
            chdir--;
641
154
        }
642
1.71k
        for (i = 0; i < chdir; i++)
643
1.35k
        {
644
1.35k
            hInfo->channel_position[chpos++] =
645
1.35k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.35k
        }
647
648
1.48k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
368
        chdir = hInfo->num_back_channels;
655
368
        if (chdir & 1)
656
40
        {
657
40
            back_center = 1;
658
40
            chdir--;
659
40
        }
660
1.20k
        for (i = 0; i < chdir; i++)
661
838
        {
662
838
            hInfo->channel_position[chpos++] =
663
838
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
838
        }
665
368
        if (back_center)
666
40
        {
667
40
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
40
        }
669
670
564
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
196
        {
672
196
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
196
        }
674
675
9.04k
    } else {
676
9.04k
        switch (hDecoder->channelConfiguration)
677
9.04k
        {
678
405
        case 1:
679
405
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
405
            hInfo->num_front_channels = 2;
682
405
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
405
            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
405
            break;
689
4.14k
        case 2:
690
4.14k
            hInfo->num_front_channels = 2;
691
4.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.14k
            break;
694
716
        case 3:
695
716
            hInfo->num_front_channels = 3;
696
716
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
716
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
716
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
716
            break;
700
1.08k
        case 4:
701
1.08k
            hInfo->num_front_channels = 3;
702
1.08k
            hInfo->num_back_channels = 1;
703
1.08k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.08k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.08k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.08k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.08k
            break;
708
634
        case 5:
709
634
            hInfo->num_front_channels = 3;
710
634
            hInfo->num_back_channels = 2;
711
634
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
634
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
634
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
634
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
634
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
634
            break;
717
328
        case 6:
718
328
            hInfo->num_front_channels = 3;
719
328
            hInfo->num_back_channels = 2;
720
328
            hInfo->num_lfe_channels = 1;
721
328
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
328
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
328
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
328
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
328
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
328
            hInfo->channel_position[5] = LFE_CHANNEL;
727
328
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
219
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
219
            {
744
219
                uint8_t i;
745
219
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
219
                if (ch & 1) /* there's either a center front or a center back channel */
747
89
                {
748
89
                    uint8_t ch1 = (ch-1)/2;
749
89
                    if (hDecoder->first_syn_ele == ID_SCE)
750
61
                    {
751
61
                        hInfo->num_front_channels = ch1 + 1;
752
61
                        hInfo->num_back_channels = ch1;
753
61
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
247
                        for (i = 1; i <= ch1; i+=2)
755
186
                        {
756
186
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
186
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
186
                        }
759
247
                        for (i = ch1+1; i < ch; i+=2)
760
186
                        {
761
186
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
186
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
186
                        }
764
61
                    } else {
765
28
                        hInfo->num_front_channels = ch1;
766
28
                        hInfo->num_back_channels = ch1 + 1;
767
157
                        for (i = 0; i < ch1; i+=2)
768
129
                        {
769
129
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
129
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
129
                        }
772
157
                        for (i = ch1; i < ch-1; i+=2)
773
129
                        {
774
129
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
129
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
129
                        }
777
28
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
28
                    }
779
130
                } else {
780
130
                    uint8_t ch1 = (ch)/2;
781
130
                    hInfo->num_front_channels = ch1;
782
130
                    hInfo->num_back_channels = ch1;
783
130
                    if (ch1 & 1)
784
56
                    {
785
56
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
370
                        for (i = 1; i <= ch1; i+=2)
787
314
                        {
788
314
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
314
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
314
                        }
791
314
                        for (i = ch1+1; i < ch-1; i+=2)
792
258
                        {
793
258
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
258
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
258
                        }
796
56
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
416
                        for (i = 0; i < ch1; i+=2)
799
342
                        {
800
342
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
342
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
342
                        }
803
416
                        for (i = ch1; i < ch; i+=2)
804
342
                        {
805
342
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
342
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
342
                        }
808
74
                    }
809
130
                }
810
219
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
442
                for (i = ch; i < hDecoder->fr_channels; i++)
812
223
                {
813
223
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
223
                }
815
219
            }
816
219
            break;
817
9.04k
        }
818
9.04k
    }
819
9.41k
}
decoder.c:create_channel_config
Line
Count
Source
599
10.5k
{
600
10.5k
    hInfo->num_front_channels = 0;
601
10.5k
    hInfo->num_side_channels = 0;
602
10.5k
    hInfo->num_back_channels = 0;
603
10.5k
    hInfo->num_lfe_channels = 0;
604
10.5k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
10.5k
    if (hDecoder->downMatrix)
607
1.13k
    {
608
1.13k
        hInfo->num_front_channels = 2;
609
1.13k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.13k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.13k
        return;
612
1.13k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
9.41k
    if (hDecoder->pce_set)
617
368
    {
618
368
        uint8_t i, chpos = 0;
619
368
        uint8_t chdir, back_center = 0;
620
621
368
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
368
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
368
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
368
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
368
        chdir = hInfo->num_front_channels;
626
368
        if (chdir & 1)
627
154
        {
628
154
#if (defined(PS_DEC) || defined(DRM_PS))
629
154
            if (hInfo->num_front_channels == 1 &&
630
76
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
26
                hInfo->num_lfe_channels == 0)
633
16
            {
634
                /* When PS is enabled output is always stereo */
635
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
16
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
16
            } else
638
138
#endif
639
138
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
154
            chdir--;
641
154
        }
642
1.71k
        for (i = 0; i < chdir; i++)
643
1.35k
        {
644
1.35k
            hInfo->channel_position[chpos++] =
645
1.35k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.35k
        }
647
648
1.48k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.12k
        {
650
1.12k
            hInfo->channel_position[chpos++] =
651
1.12k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.12k
        }
653
654
368
        chdir = hInfo->num_back_channels;
655
368
        if (chdir & 1)
656
40
        {
657
40
            back_center = 1;
658
40
            chdir--;
659
40
        }
660
1.20k
        for (i = 0; i < chdir; i++)
661
838
        {
662
838
            hInfo->channel_position[chpos++] =
663
838
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
838
        }
665
368
        if (back_center)
666
40
        {
667
40
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
40
        }
669
670
564
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
196
        {
672
196
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
196
        }
674
675
9.04k
    } else {
676
9.04k
        switch (hDecoder->channelConfiguration)
677
9.04k
        {
678
405
        case 1:
679
405
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
405
            hInfo->num_front_channels = 2;
682
405
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
405
            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
405
            break;
689
4.14k
        case 2:
690
4.14k
            hInfo->num_front_channels = 2;
691
4.14k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
4.14k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
4.14k
            break;
694
716
        case 3:
695
716
            hInfo->num_front_channels = 3;
696
716
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
716
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
716
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
716
            break;
700
1.08k
        case 4:
701
1.08k
            hInfo->num_front_channels = 3;
702
1.08k
            hInfo->num_back_channels = 1;
703
1.08k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.08k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.08k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.08k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.08k
            break;
708
634
        case 5:
709
634
            hInfo->num_front_channels = 3;
710
634
            hInfo->num_back_channels = 2;
711
634
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
634
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
634
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
634
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
634
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
634
            break;
717
328
        case 6:
718
328
            hInfo->num_front_channels = 3;
719
328
            hInfo->num_back_channels = 2;
720
328
            hInfo->num_lfe_channels = 1;
721
328
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
328
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
328
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
328
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
328
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
328
            hInfo->channel_position[5] = LFE_CHANNEL;
727
328
            break;
728
1.50k
        case 7:
729
1.50k
            hInfo->num_front_channels = 3;
730
1.50k
            hInfo->num_side_channels = 2;
731
1.50k
            hInfo->num_back_channels = 2;
732
1.50k
            hInfo->num_lfe_channels = 1;
733
1.50k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.50k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.50k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.50k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.50k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.50k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.50k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.50k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.50k
            break;
742
219
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
219
            {
744
219
                uint8_t i;
745
219
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
219
                if (ch & 1) /* there's either a center front or a center back channel */
747
89
                {
748
89
                    uint8_t ch1 = (ch-1)/2;
749
89
                    if (hDecoder->first_syn_ele == ID_SCE)
750
61
                    {
751
61
                        hInfo->num_front_channels = ch1 + 1;
752
61
                        hInfo->num_back_channels = ch1;
753
61
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
247
                        for (i = 1; i <= ch1; i+=2)
755
186
                        {
756
186
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
186
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
186
                        }
759
247
                        for (i = ch1+1; i < ch; i+=2)
760
186
                        {
761
186
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
186
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
186
                        }
764
61
                    } else {
765
28
                        hInfo->num_front_channels = ch1;
766
28
                        hInfo->num_back_channels = ch1 + 1;
767
157
                        for (i = 0; i < ch1; i+=2)
768
129
                        {
769
129
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
129
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
129
                        }
772
157
                        for (i = ch1; i < ch-1; i+=2)
773
129
                        {
774
129
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
129
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
129
                        }
777
28
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
28
                    }
779
130
                } else {
780
130
                    uint8_t ch1 = (ch)/2;
781
130
                    hInfo->num_front_channels = ch1;
782
130
                    hInfo->num_back_channels = ch1;
783
130
                    if (ch1 & 1)
784
56
                    {
785
56
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
370
                        for (i = 1; i <= ch1; i+=2)
787
314
                        {
788
314
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
314
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
314
                        }
791
314
                        for (i = ch1+1; i < ch-1; i+=2)
792
258
                        {
793
258
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
258
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
258
                        }
796
56
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
74
                    } else {
798
416
                        for (i = 0; i < ch1; i+=2)
799
342
                        {
800
342
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
342
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
342
                        }
803
416
                        for (i = ch1; i < ch; i+=2)
804
342
                        {
805
342
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
342
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
342
                        }
808
74
                    }
809
130
                }
810
219
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
442
                for (i = ch; i < hDecoder->fr_channels; i++)
812
223
                {
813
223
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
223
                }
815
219
            }
816
219
            break;
817
9.04k
        }
818
9.04k
    }
819
9.41k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
42.6k
{
826
42.6k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
42.6k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
42.6k
}
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
13.3k
{
837
13.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
13.3k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
92
    {
840
92
        hInfo->error = 27;
841
92
        return NULL;
842
92
    }
843
844
13.2k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
13.2k
        sample_buffer, sample_buffer_size);
846
13.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.9k
{
855
55.9k
    uint16_t i;
856
55.9k
    uint8_t channels = 0;
857
55.9k
    uint8_t output_channels = 0;
858
55.9k
    bitfile ld;
859
55.9k
    uint32_t bitsconsumed;
860
55.9k
    uint16_t frame_len;
861
55.9k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
55.9k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
55.9k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
55.9k
    frame_len = hDecoder->frameLength;
882
883
884
55.9k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
55.9k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
55.9k
    if (buffer_size >= 128)
903
6.24k
    {
904
6.24k
        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.24k
    }
912
913
914
    /* initialize the bitstream */
915
55.9k
    faad_initbits(&ld, buffer, buffer_size);
916
55.9k
    if (ld.error != 0)
917
22.6k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
11.4k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.01k
    {
953
        /* We do not support stereo right now */
954
1.01k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.01k
        faad_getbits(&ld, 8
961
1.01k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.01k
    }
963
11.4k
#endif
964
965
33.2k
    if (hDecoder->adts_header_present)
966
805
    {
967
805
        adts_header adts;
968
969
805
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
805
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
93
            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
805
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
11.4k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.01k
    {
987
1.01k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.4k
    } else {
989
10.4k
#endif
990
21.7k
        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
21.7k
    channels = hDecoder->fr_channels;
1009
1010
33.1k
    if (hInfo->error > 0)
1011
22.5k
        goto error;
1012
1013
    /* safety check */
1014
10.6k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
105
    {
1016
        /* invalid number of channels */
1017
105
        hInfo->error = 12;
1018
105
        goto error;
1019
105
    }
1020
1021
    /* no more bit reading after this */
1022
10.5k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
10.5k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
10.5k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
10.5k
    faad_endbits(&ld);
1030
1031
1032
10.5k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
10.5k
        )
1037
10.3k
    {
1038
10.3k
        if (hDecoder->channelConfiguration == 0)
1039
328
            hDecoder->channelConfiguration = channels;
1040
1041
10.3k
        if (channels == 8) /* 7.1 */
1042
1.03k
            hDecoder->channelConfiguration = 7;
1043
10.3k
        if (channels == 7) /* not a standard channelConfiguration */
1044
62
            hDecoder->channelConfiguration = 0;
1045
10.3k
    }
1046
1047
10.5k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.12k
    {
1049
1.12k
        hDecoder->downMatrix = 1;
1050
1.12k
        output_channels = 2;
1051
9.43k
    } else {
1052
9.43k
        output_channels = channels;
1053
9.43k
    }
1054
1055
10.5k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
10.5k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
10.5k
    if (output_channels == 1)
1059
283
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
283
        hDecoder->upMatrix = 1;
1062
283
        output_channels = 2;
1063
283
    }
1064
10.5k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
10.5k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
377
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
377
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
9
        {
1072
9
            hInfo->error = 22;
1073
9
            return NULL;
1074
9
        }
1075
377
    }
1076
10.5k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
10.5k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
10.5k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
10.5k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
10.5k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
10.5k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
10.5k
    hInfo->header_type = RAW;
1090
10.5k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
10.5k
    if (hDecoder->adts_header_present)
1093
213
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
10.5k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
10.5k
    hInfo->ps = hDecoder->ps_used_global;
1100
10.5k
#endif
1101
1102
    /* check if frame has channel elements */
1103
10.5k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
10.5k
    {
1110
10.5k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
10.5k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
10.5k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
10.5k
        };
1114
10.5k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
10.5k
#ifdef SBR_DEC
1116
10.5k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
5.86k
        {
1118
5.86k
            stride = 2 * stride;
1119
5.86k
        }
1120
10.5k
#endif
1121
10.5k
        required_buffer_size = frame_len*output_channels*stride;
1122
10.5k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
10.5k
    if (sample_buffer_size == 0)
1126
6.94k
    {
1127
        /* allocate the buffer for the final samples */
1128
6.94k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
6.07k
        {
1130
6.07k
            if (hDecoder->sample_buffer)
1131
105
                faad_free(hDecoder->sample_buffer);
1132
6.07k
            hDecoder->sample_buffer = NULL;
1133
6.07k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
6.07k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
6.07k
        }
1136
6.94k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.45k
        hInfo->error = 27;
1139
1.45k
        return NULL;
1140
1.45k
    }
1141
1142
9.09k
    if (sample_buffer_size == 0)
1143
6.94k
    {
1144
6.94k
        sample_buffer = hDecoder->sample_buffer;
1145
6.94k
    } else {
1146
2.14k
        sample_buffer = *sample_buffer2;
1147
2.14k
    }
1148
1149
9.09k
#ifdef SBR_DEC
1150
9.09k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
6.13k
    {
1152
6.13k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
6.13k
        if (!hDecoder->downSampledSBR)
1156
4.81k
        {
1157
4.81k
            frame_len *= 2;
1158
4.81k
            hInfo->samples *= 2;
1159
4.81k
            hInfo->samplerate *= 2;
1160
4.81k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
31.7k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
25.6k
        {
1165
25.6k
            if (hDecoder->sbr[ele] == NULL)
1166
7
            {
1167
7
                hInfo->error = 25;
1168
7
                goto error;
1169
7
            }
1170
25.6k
        }
1171
1172
        /* sbr */
1173
6.12k
        if (hDecoder->sbr_present_flag == 1)
1174
3.80k
        {
1175
3.80k
            hInfo->object_type = HE_AAC;
1176
3.80k
            hInfo->sbr = SBR_UPSAMPLED;
1177
3.80k
        } else {
1178
2.32k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.32k
        }
1180
6.12k
        if (hDecoder->downSampledSBR)
1181
1.30k
        {
1182
1.30k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.30k
        }
1184
6.12k
    }
1185
9.08k
#endif
1186
1187
1188
9.08k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
9.08k
        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.08k
    hDecoder->postSeekResetFlag = 0;
1198
1199
9.08k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
6.11k
    if (hDecoder->object_type != LD)
1202
5.38k
    {
1203
5.38k
#endif
1204
8.35k
        if (hDecoder->frame <= 1)
1205
2.90k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
731
    } else {
1208
        /* LD encoders will give lower delay */
1209
731
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
731
    }
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.08k
    return sample_buffer;
1225
1226
22.7k
error:
1227
1228
    /* reset filterbank state */
1229
1.47M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.45M
    {
1231
1.45M
        if (hDecoder->fb_intermed[i] != NULL)
1232
729k
        {
1233
729k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
729k
        }
1235
1.45M
    }
1236
22.7k
#ifdef SBR_DEC
1237
1.11M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
1.09M
    {
1239
1.09M
        if (hDecoder->sbr[i] != NULL)
1240
530k
        {
1241
530k
            sbrReset(hDecoder->sbr[i]);
1242
530k
        }
1243
1.09M
    }
1244
22.7k
#endif
1245
1246
1247
22.7k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
9.09k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
19.1k
{
855
19.1k
    uint16_t i;
856
19.1k
    uint8_t channels = 0;
857
19.1k
    uint8_t output_channels = 0;
858
19.1k
    bitfile ld;
859
19.1k
    uint32_t bitsconsumed;
860
19.1k
    uint16_t frame_len;
861
19.1k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
19.1k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
19.1k
    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
19.1k
    frame_len = hDecoder->frameLength;
882
883
884
19.1k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
19.1k
    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
19.1k
    if (buffer_size >= 128)
903
2.83k
    {
904
2.83k
        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.83k
    }
912
913
914
    /* initialize the bitstream */
915
19.1k
    faad_initbits(&ld, buffer, buffer_size);
916
19.1k
    if (ld.error != 0)
917
7.64k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
11.4k
#ifdef DRM
951
11.4k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.01k
    {
953
        /* We do not support stereo right now */
954
1.01k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.01k
        faad_getbits(&ld, 8
961
1.01k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.01k
    }
963
11.4k
#endif
964
965
11.4k
    if (hDecoder->adts_header_present)
966
80
    {
967
80
        adts_header adts;
968
969
80
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
80
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
3
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
80
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
11.4k
#ifdef DRM
985
11.4k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.01k
    {
987
1.01k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.4k
    } else {
989
10.4k
#endif
990
10.4k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
10.4k
#ifdef DRM
992
10.4k
    }
993
11.4k
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
11.4k
    channels = hDecoder->fr_channels;
1009
1010
11.4k
    if (hInfo->error > 0)
1011
7.76k
        goto error;
1012
1013
    /* safety check */
1014
3.69k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
25
    {
1016
        /* invalid number of channels */
1017
25
        hInfo->error = 12;
1018
25
        goto error;
1019
25
    }
1020
1021
    /* no more bit reading after this */
1022
3.67k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
3.67k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
3.67k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
3.67k
    faad_endbits(&ld);
1030
1031
1032
3.67k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
3.67k
        )
1037
3.63k
    {
1038
3.63k
        if (hDecoder->channelConfiguration == 0)
1039
147
            hDecoder->channelConfiguration = channels;
1040
1041
3.63k
        if (channels == 8) /* 7.1 */
1042
131
            hDecoder->channelConfiguration = 7;
1043
3.63k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
3.63k
    }
1046
1047
3.67k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
516
    {
1049
516
        hDecoder->downMatrix = 1;
1050
516
        output_channels = 2;
1051
3.15k
    } else {
1052
3.15k
        output_channels = channels;
1053
3.15k
    }
1054
1055
3.67k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
3.67k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
3.67k
    if (output_channels == 1)
1059
0
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
0
        hDecoder->upMatrix = 1;
1062
0
        output_channels = 2;
1063
0
    }
1064
3.67k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
3.67k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
169
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
169
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
3
        {
1072
3
            hInfo->error = 22;
1073
3
            return NULL;
1074
3
        }
1075
169
    }
1076
3.66k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
3.66k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
3.66k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
3.66k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
3.66k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
3.66k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
3.66k
    hInfo->header_type = RAW;
1090
3.66k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
3.66k
    if (hDecoder->adts_header_present)
1093
35
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
3.66k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
3.66k
    hInfo->ps = hDecoder->ps_used_global;
1100
3.66k
#endif
1101
1102
    /* check if frame has channel elements */
1103
3.66k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
3.66k
    {
1110
3.66k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
3.66k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
3.66k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
3.66k
        };
1114
3.66k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
3.66k
#ifdef SBR_DEC
1116
3.66k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
1.98k
        {
1118
1.98k
            stride = 2 * stride;
1119
1.98k
        }
1120
3.66k
#endif
1121
3.66k
        required_buffer_size = frame_len*output_channels*stride;
1122
3.66k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
3.66k
    if (sample_buffer_size == 0)
1126
1.86k
    {
1127
        /* allocate the buffer for the final samples */
1128
1.86k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
1.64k
        {
1130
1.64k
            if (hDecoder->sample_buffer)
1131
48
                faad_free(hDecoder->sample_buffer);
1132
1.64k
            hDecoder->sample_buffer = NULL;
1133
1.64k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
1.64k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
1.64k
        }
1136
1.86k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
694
        hInfo->error = 27;
1139
694
        return NULL;
1140
694
    }
1141
1142
2.97k
    if (sample_buffer_size == 0)
1143
1.86k
    {
1144
1.86k
        sample_buffer = hDecoder->sample_buffer;
1145
1.86k
    } else {
1146
1.11k
        sample_buffer = *sample_buffer2;
1147
1.11k
    }
1148
1149
2.97k
#ifdef SBR_DEC
1150
2.97k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
2.05k
    {
1152
2.05k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
2.05k
        if (!hDecoder->downSampledSBR)
1156
1.48k
        {
1157
1.48k
            frame_len *= 2;
1158
1.48k
            hInfo->samples *= 2;
1159
1.48k
            hInfo->samplerate *= 2;
1160
1.48k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
10.8k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
8.76k
        {
1165
8.76k
            if (hDecoder->sbr[ele] == NULL)
1166
2
            {
1167
2
                hInfo->error = 25;
1168
2
                goto error;
1169
2
            }
1170
8.76k
        }
1171
1172
        /* sbr */
1173
2.05k
        if (hDecoder->sbr_present_flag == 1)
1174
1.54k
        {
1175
1.54k
            hInfo->object_type = HE_AAC;
1176
1.54k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.54k
        } else {
1178
511
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
511
        }
1180
2.05k
        if (hDecoder->downSampledSBR)
1181
574
        {
1182
574
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
574
        }
1184
2.05k
    }
1185
2.97k
#endif
1186
1187
1188
2.97k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
2.97k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
2.97k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
2.97k
#endif
1195
1196
1197
2.97k
    hDecoder->postSeekResetFlag = 0;
1198
1199
2.97k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
2.97k
        if (hDecoder->frame <= 1)
1205
1.18k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
    } else {
1208
        /* LD encoders will give lower delay */
1209
        if (hDecoder->frame <= 0)
1210
            hInfo->samples = 0;
1211
    }
1212
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
2.97k
    return sample_buffer;
1225
1226
7.79k
error:
1227
1228
    /* reset filterbank state */
1229
506k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
498k
    {
1231
498k
        if (hDecoder->fb_intermed[i] != NULL)
1232
193k
        {
1233
193k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
193k
        }
1235
498k
    }
1236
7.79k
#ifdef SBR_DEC
1237
381k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
374k
    {
1239
374k
        if (hDecoder->sbr[i] != NULL)
1240
147k
        {
1241
147k
            sbrReset(hDecoder->sbr[i]);
1242
147k
        }
1243
374k
    }
1244
7.79k
#endif
1245
1246
1247
7.79k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
2.97k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
36.8k
{
855
36.8k
    uint16_t i;
856
36.8k
    uint8_t channels = 0;
857
36.8k
    uint8_t output_channels = 0;
858
36.8k
    bitfile ld;
859
36.8k
    uint32_t bitsconsumed;
860
36.8k
    uint16_t frame_len;
861
36.8k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
36.8k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
36.8k
    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
36.8k
    frame_len = hDecoder->frameLength;
882
883
884
36.8k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
36.8k
    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
36.8k
    if (buffer_size >= 128)
903
3.41k
    {
904
3.41k
        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.41k
    }
912
913
914
    /* initialize the bitstream */
915
36.8k
    faad_initbits(&ld, buffer, buffer_size);
916
36.8k
    if (ld.error != 0)
917
15.0k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
    if (hDecoder->object_type == DRM_ER_LC)
952
    {
953
        /* We do not support stereo right now */
954
        if (0) //(hDecoder->channelConfiguration == 2)
955
        {
956
            hInfo->error = 28; // Throw CRC error
957
            goto error;
958
        }
959
960
        faad_getbits(&ld, 8
961
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
    }
963
#endif
964
965
21.7k
    if (hDecoder->adts_header_present)
966
725
    {
967
725
        adts_header adts;
968
969
725
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
725
        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
725
    }
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
21.7k
        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
21.7k
    channels = hDecoder->fr_channels;
1009
1010
21.7k
    if (hInfo->error > 0)
1011
14.7k
        goto error;
1012
1013
    /* safety check */
1014
6.96k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
80
    {
1016
        /* invalid number of channels */
1017
80
        hInfo->error = 12;
1018
80
        goto error;
1019
80
    }
1020
1021
    /* no more bit reading after this */
1022
6.88k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
6.88k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
6.88k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
6.88k
    faad_endbits(&ld);
1030
1031
1032
6.88k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
6.88k
        )
1037
6.70k
    {
1038
6.70k
        if (hDecoder->channelConfiguration == 0)
1039
181
            hDecoder->channelConfiguration = channels;
1040
1041
6.70k
        if (channels == 8) /* 7.1 */
1042
903
            hDecoder->channelConfiguration = 7;
1043
6.70k
        if (channels == 7) /* not a standard channelConfiguration */
1044
62
            hDecoder->channelConfiguration = 0;
1045
6.70k
    }
1046
1047
6.88k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
607
    {
1049
607
        hDecoder->downMatrix = 1;
1050
607
        output_channels = 2;
1051
6.27k
    } else {
1052
6.27k
        output_channels = channels;
1053
6.27k
    }
1054
1055
6.88k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
6.88k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
6.88k
    if (output_channels == 1)
1059
283
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
283
        hDecoder->upMatrix = 1;
1062
283
        output_channels = 2;
1063
283
    }
1064
6.88k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
6.88k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
208
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
208
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
208
    }
1076
6.87k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
6.87k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
6.87k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
6.87k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
6.87k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
6.87k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
6.87k
    hInfo->header_type = RAW;
1090
6.87k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
6.87k
    if (hDecoder->adts_header_present)
1093
178
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
6.87k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
6.87k
    hInfo->ps = hDecoder->ps_used_global;
1100
6.87k
#endif
1101
1102
    /* check if frame has channel elements */
1103
6.87k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
6.87k
    {
1110
6.87k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
6.87k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
6.87k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
6.87k
        };
1114
6.87k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
6.87k
#ifdef SBR_DEC
1116
6.87k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.88k
        {
1118
3.88k
            stride = 2 * stride;
1119
3.88k
        }
1120
6.87k
#endif
1121
6.87k
        required_buffer_size = frame_len*output_channels*stride;
1122
6.87k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
6.87k
    if (sample_buffer_size == 0)
1126
5.08k
    {
1127
        /* allocate the buffer for the final samples */
1128
5.08k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
4.42k
        {
1130
4.42k
            if (hDecoder->sample_buffer)
1131
57
                faad_free(hDecoder->sample_buffer);
1132
4.42k
            hDecoder->sample_buffer = NULL;
1133
4.42k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
4.42k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
4.42k
        }
1136
5.08k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
759
        hInfo->error = 27;
1139
759
        return NULL;
1140
759
    }
1141
1142
6.11k
    if (sample_buffer_size == 0)
1143
5.08k
    {
1144
5.08k
        sample_buffer = hDecoder->sample_buffer;
1145
5.08k
    } else {
1146
1.03k
        sample_buffer = *sample_buffer2;
1147
1.03k
    }
1148
1149
6.11k
#ifdef SBR_DEC
1150
6.11k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
4.07k
    {
1152
4.07k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
4.07k
        if (!hDecoder->downSampledSBR)
1156
3.33k
        {
1157
3.33k
            frame_len *= 2;
1158
3.33k
            hInfo->samples *= 2;
1159
3.33k
            hInfo->samplerate *= 2;
1160
3.33k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
20.9k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
16.9k
        {
1165
16.9k
            if (hDecoder->sbr[ele] == NULL)
1166
5
            {
1167
5
                hInfo->error = 25;
1168
5
                goto error;
1169
5
            }
1170
16.9k
        }
1171
1172
        /* sbr */
1173
4.06k
        if (hDecoder->sbr_present_flag == 1)
1174
2.25k
        {
1175
2.25k
            hInfo->object_type = HE_AAC;
1176
2.25k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.25k
        } else {
1178
1.81k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.81k
        }
1180
4.06k
        if (hDecoder->downSampledSBR)
1181
734
        {
1182
734
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
734
        }
1184
4.06k
    }
1185
6.11k
#endif
1186
1187
1188
6.11k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
6.11k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
6.11k
    hDecoder->postSeekResetFlag = 0;
1198
1199
6.11k
    hDecoder->frame++;
1200
6.11k
#ifdef LD_DEC
1201
6.11k
    if (hDecoder->object_type != LD)
1202
5.38k
    {
1203
5.38k
#endif
1204
5.38k
        if (hDecoder->frame <= 1)
1205
1.72k
            hInfo->samples = 0;
1206
5.38k
#ifdef LD_DEC
1207
5.38k
    } else {
1208
        /* LD encoders will give lower delay */
1209
731
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
731
    }
1212
6.11k
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
6.11k
    return sample_buffer;
1225
1226
14.9k
error:
1227
1228
    /* reset filterbank state */
1229
969k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
954k
    {
1231
954k
        if (hDecoder->fb_intermed[i] != NULL)
1232
535k
        {
1233
535k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
535k
        }
1235
954k
    }
1236
14.9k
#ifdef SBR_DEC
1237
731k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
716k
    {
1239
716k
        if (hDecoder->sbr[i] != NULL)
1240
383k
        {
1241
383k
            sbrReset(hDecoder->sbr[i]);
1242
383k
        }
1243
716k
    }
1244
14.9k
#endif
1245
1246
1247
14.9k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
6.11k
}