Coverage Report

Created: 2025-10-10 06:50

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