Coverage Report

Created: 2026-07-16 06:20

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