Coverage Report

Created: 2026-09-01 06:57

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