Coverage Report

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