Coverage Report

Created: 2026-03-20 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/libfaad/decoder.c
Line
Count
Source
1
/*
2
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3
** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4
**
5
** This program is free software; you can redistribute it and/or modify
6
** it under the terms of the GNU General Public License as published by
7
** the Free Software Foundation; either version 2 of the License, or
8
** (at your option) any later version.
9
**
10
** This program is distributed in the hope that it will be useful,
11
** but WITHOUT ANY WARRANTY; without even the implied warranty of
12
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
** GNU General Public License for more details.
14
**
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
**
19
** Any non-GPL usage of this software or parts of this software is strictly
20
** forbidden.
21
**
22
** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23
** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
24
**
25
** Commercial non-GPL licensing of this software is possible.
26
** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27
**
28
** $Id: decoder.c,v 1.119 2015/01/24 14:21:05 knik Exp $
29
**/
30
31
#include "common.h"
32
#include "structs.h"
33
34
#include <stdlib.h>
35
#include <stdio.h>
36
#include <string.h>
37
38
#include "mp4.h"
39
#include "syntax.h"
40
#include "error.h"
41
#include "output.h"
42
#include "filtbank.h"
43
#include "drc.h"
44
#ifdef SBR_DEC
45
#include "sbr_dec.h"
46
#include "sbr_syntax.h"
47
#endif
48
#ifdef SSR_DEC
49
#include "ssr.h"
50
#endif
51
52
#ifdef ANALYSIS
53
uint16_t dbg_count;
54
#endif
55
56
#if !defined(PACKAGE_VERSION)
57
#error "PACKAGE_VERSION must be defined"
58
#endif
59
60
/* static function declarations */
61
static void* aac_frame_decode(NeAACDecStruct *hDecoder,
62
                              NeAACDecFrameInfo *hInfo,
63
                              unsigned char *buffer,
64
                              unsigned long buffer_size,
65
                              void **sample_buffer2,
66
                              unsigned long sample_buffer_size);
67
static void create_channel_config(NeAACDecStruct *hDecoder,
68
                                  NeAACDecFrameInfo *hInfo);
69
70
71
int NeAACDecGetVersion(char **faad_id_string,
72
                                   char **faad_copyright_string)
73
336
{
74
336
    static char *libfaadName = PACKAGE_VERSION;
75
336
    static char *libCopyright =
76
336
        " Copyright 2002-2004: Ahead Software AG\n"
77
336
  " http://www.audiocoding.com\n"
78
336
  " bug tracking: https://sourceforge.net/p/faac/bugs/\n";
79
80
336
    if (faad_id_string)
81
336
        *faad_id_string = libfaadName;
82
83
336
    if (faad_copyright_string)
84
336
        *faad_copyright_string = libCopyright;
85
86
336
    return 0;
87
336
}
88
89
char* NeAACDecGetErrorMessage(unsigned char errcode)
90
336
{
91
336
    if (errcode >= NUM_ERROR_MESSAGES)
92
200
        return NULL;
93
136
    return err_msg[errcode];
94
336
}
95
96
unsigned long NeAACDecGetCapabilities(void)
97
1.34k
{
98
1.34k
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
1.34k
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
336
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
672
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
672
    cap += LD_DEC_CAP;
111
#endif
112
1.34k
#ifdef ERROR_RESILIENCE
113
1.34k
    cap += ERROR_RESILIENCE_CAP;
114
1.34k
#endif
115
#ifdef FIXED_POINT
116
672
    cap += FIXED_POINT_CAP;
117
#endif
118
119
1.34k
    return cap;
120
1.34k
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
    cap += LD_DEC_CAP;
111
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
336
#ifdef FIXED_POINT
116
336
    cap += FIXED_POINT_CAP;
117
336
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
336
#ifdef MAIN_DEC
104
336
    cap += MAIN_DEC_CAP;
105
336
#endif
106
336
#ifdef LTP_DEC
107
336
    cap += LTP_DEC_CAP;
108
336
#endif
109
336
#ifdef LD_DEC
110
336
    cap += LD_DEC_CAP;
111
336
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
336
#ifdef LTP_DEC
107
336
    cap += LTP_DEC_CAP;
108
336
#endif
109
336
#ifdef LD_DEC
110
336
    cap += LD_DEC_CAP;
111
336
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
336
#ifdef FIXED_POINT
116
336
    cap += FIXED_POINT_CAP;
117
336
#endif
118
119
336
    return cap;
120
336
}
NeAACDecGetCapabilities
Line
Count
Source
97
336
{
98
336
    uint32_t cap = 0;
99
100
    /* can't do without it */
101
336
    cap += LC_DEC_CAP;
102
103
#ifdef MAIN_DEC
104
    cap += MAIN_DEC_CAP;
105
#endif
106
#ifdef LTP_DEC
107
    cap += LTP_DEC_CAP;
108
#endif
109
#ifdef LD_DEC
110
    cap += LD_DEC_CAP;
111
#endif
112
336
#ifdef ERROR_RESILIENCE
113
336
    cap += ERROR_RESILIENCE_CAP;
114
336
#endif
115
#ifdef FIXED_POINT
116
    cap += FIXED_POINT_CAP;
117
#endif
118
119
336
    return cap;
120
336
}
121
122
const unsigned char mes[] = { 0x67,0x20,0x61,0x20,0x20,0x20,0x6f,0x20,0x72,0x20,0x65,0x20,0x6e,0x20,0x20,0x20,0x74,0x20,0x68,0x20,0x67,0x20,0x69,0x20,0x72,0x20,0x79,0x20,0x70,0x20,0x6f,0x20,0x63 };
123
NeAACDecHandle NeAACDecOpen(void)
124
54.2k
{
125
54.2k
    uint8_t i;
126
54.2k
    NeAACDecStruct *hDecoder = NULL;
127
128
54.2k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
54.2k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
54.2k
    hDecoder->cmes = mes;
134
54.2k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
54.2k
    hDecoder->config.defObjectType = MAIN;
136
54.2k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
54.2k
    hDecoder->config.downMatrix = 0;
138
54.2k
    hDecoder->adts_header_present = 0;
139
54.2k
    hDecoder->adif_header_present = 0;
140
54.2k
    hDecoder->latm_header_present = 0;
141
54.2k
#ifdef ERROR_RESILIENCE
142
54.2k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
54.2k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
54.2k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
54.2k
#endif
146
54.2k
    hDecoder->frameLength = 1024;
147
148
54.2k
    hDecoder->frame = 0;
149
54.2k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
54.2k
    hDecoder->__r1 = 0x2bb431ea;
153
54.2k
    hDecoder->__r2 = 0x206155b7;
154
155
3.52M
    for (i = 0; i < MAX_CHANNELS; i++)
156
3.47M
    {
157
3.47M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
3.47M
        hDecoder->window_shape_prev[i] = 0;
159
3.47M
        hDecoder->time_out[i] = NULL;
160
3.47M
        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.47M
    }
173
174
54.2k
#ifdef SBR_DEC
175
2.65M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
2.60M
    {
177
2.60M
        hDecoder->sbr[i] = NULL;
178
2.60M
    }
179
54.2k
#endif
180
181
54.2k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
54.2k
    return hDecoder;
184
54.2k
}
NeAACDecOpen
Line
Count
Source
124
9.80k
{
125
9.80k
    uint8_t i;
126
9.80k
    NeAACDecStruct *hDecoder = NULL;
127
128
9.80k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
9.80k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
9.80k
    hDecoder->cmes = mes;
134
9.80k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
9.80k
    hDecoder->config.defObjectType = MAIN;
136
9.80k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
9.80k
    hDecoder->config.downMatrix = 0;
138
9.80k
    hDecoder->adts_header_present = 0;
139
9.80k
    hDecoder->adif_header_present = 0;
140
9.80k
    hDecoder->latm_header_present = 0;
141
9.80k
#ifdef ERROR_RESILIENCE
142
9.80k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
9.80k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
9.80k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
9.80k
#endif
146
9.80k
    hDecoder->frameLength = 1024;
147
148
9.80k
    hDecoder->frame = 0;
149
9.80k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
9.80k
    hDecoder->__r1 = 0x2bb431ea;
153
9.80k
    hDecoder->__r2 = 0x206155b7;
154
155
637k
    for (i = 0; i < MAX_CHANNELS; i++)
156
627k
    {
157
627k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
627k
        hDecoder->window_shape_prev[i] = 0;
159
627k
        hDecoder->time_out[i] = NULL;
160
627k
        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
627k
    }
173
174
9.80k
#ifdef SBR_DEC
175
480k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
470k
    {
177
470k
        hDecoder->sbr[i] = NULL;
178
470k
    }
179
9.80k
#endif
180
181
9.80k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
9.80k
    return hDecoder;
184
9.80k
}
NeAACDecOpen
Line
Count
Source
124
17.3k
{
125
17.3k
    uint8_t i;
126
17.3k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.3k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.3k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.3k
    hDecoder->cmes = mes;
134
17.3k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.3k
    hDecoder->config.defObjectType = MAIN;
136
17.3k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.3k
    hDecoder->config.downMatrix = 0;
138
17.3k
    hDecoder->adts_header_present = 0;
139
17.3k
    hDecoder->adif_header_present = 0;
140
17.3k
    hDecoder->latm_header_present = 0;
141
17.3k
#ifdef ERROR_RESILIENCE
142
17.3k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.3k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.3k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.3k
#endif
146
17.3k
    hDecoder->frameLength = 1024;
147
148
17.3k
    hDecoder->frame = 0;
149
17.3k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.3k
    hDecoder->__r1 = 0x2bb431ea;
153
17.3k
    hDecoder->__r2 = 0x206155b7;
154
155
1.12M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.10M
    {
157
1.10M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.10M
        hDecoder->window_shape_prev[i] = 0;
159
1.10M
        hDecoder->time_out[i] = NULL;
160
1.10M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
1.10M
#ifdef MAIN_DEC
166
1.10M
        hDecoder->pred_stat[i] = NULL;
167
1.10M
#endif
168
1.10M
#ifdef LTP_DEC
169
1.10M
        hDecoder->ltp_lag[i] = 0;
170
1.10M
        hDecoder->lt_pred_stat[i] = NULL;
171
1.10M
#endif
172
1.10M
    }
173
174
17.3k
#ifdef SBR_DEC
175
848k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
831k
    {
177
831k
        hDecoder->sbr[i] = NULL;
178
831k
    }
179
17.3k
#endif
180
181
17.3k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.3k
    return hDecoder;
184
17.3k
}
NeAACDecOpen
Line
Count
Source
124
9.80k
{
125
9.80k
    uint8_t i;
126
9.80k
    NeAACDecStruct *hDecoder = NULL;
127
128
9.80k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
9.80k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
9.80k
    hDecoder->cmes = mes;
134
9.80k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
9.80k
    hDecoder->config.defObjectType = MAIN;
136
9.80k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
9.80k
    hDecoder->config.downMatrix = 0;
138
9.80k
    hDecoder->adts_header_present = 0;
139
9.80k
    hDecoder->adif_header_present = 0;
140
9.80k
    hDecoder->latm_header_present = 0;
141
9.80k
#ifdef ERROR_RESILIENCE
142
9.80k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
9.80k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
9.80k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
9.80k
#endif
146
9.80k
    hDecoder->frameLength = 1024;
147
148
9.80k
    hDecoder->frame = 0;
149
9.80k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
9.80k
    hDecoder->__r1 = 0x2bb431ea;
153
9.80k
    hDecoder->__r2 = 0x206155b7;
154
155
637k
    for (i = 0; i < MAX_CHANNELS; i++)
156
627k
    {
157
627k
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
627k
        hDecoder->window_shape_prev[i] = 0;
159
627k
        hDecoder->time_out[i] = NULL;
160
627k
        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
627k
#ifdef LTP_DEC
169
627k
        hDecoder->ltp_lag[i] = 0;
170
627k
        hDecoder->lt_pred_stat[i] = NULL;
171
627k
#endif
172
627k
    }
173
174
9.80k
#ifdef SBR_DEC
175
480k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
470k
    {
177
470k
        hDecoder->sbr[i] = NULL;
178
470k
    }
179
9.80k
#endif
180
181
9.80k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
9.80k
    return hDecoder;
184
9.80k
}
NeAACDecOpen
Line
Count
Source
124
17.3k
{
125
17.3k
    uint8_t i;
126
17.3k
    NeAACDecStruct *hDecoder = NULL;
127
128
17.3k
    if ((hDecoder = (NeAACDecStruct*)faad_malloc(sizeof(NeAACDecStruct))) == NULL)
129
0
        return NULL;
130
131
17.3k
    memset(hDecoder, 0, sizeof(NeAACDecStruct));
132
133
17.3k
    hDecoder->cmes = mes;
134
17.3k
    hDecoder->config.outputFormat  = FAAD_FMT_16BIT;
135
17.3k
    hDecoder->config.defObjectType = MAIN;
136
17.3k
    hDecoder->config.defSampleRate = 44100; /* Default: 44.1kHz */
137
17.3k
    hDecoder->config.downMatrix = 0;
138
17.3k
    hDecoder->adts_header_present = 0;
139
17.3k
    hDecoder->adif_header_present = 0;
140
17.3k
    hDecoder->latm_header_present = 0;
141
17.3k
#ifdef ERROR_RESILIENCE
142
17.3k
    hDecoder->aacSectionDataResilienceFlag = 0;
143
17.3k
    hDecoder->aacScalefactorDataResilienceFlag = 0;
144
17.3k
    hDecoder->aacSpectralDataResilienceFlag = 0;
145
17.3k
#endif
146
17.3k
    hDecoder->frameLength = 1024;
147
148
17.3k
    hDecoder->frame = 0;
149
17.3k
    hDecoder->sample_buffer = NULL;
150
151
    // Same as (1, 1) after 1024 iterations; otherwise first values does not look random at all.
152
17.3k
    hDecoder->__r1 = 0x2bb431ea;
153
17.3k
    hDecoder->__r2 = 0x206155b7;
154
155
1.12M
    for (i = 0; i < MAX_CHANNELS; i++)
156
1.10M
    {
157
1.10M
        hDecoder->element_id[i] = INVALID_ELEMENT_ID;
158
1.10M
        hDecoder->window_shape_prev[i] = 0;
159
1.10M
        hDecoder->time_out[i] = NULL;
160
1.10M
        hDecoder->fb_intermed[i] = NULL;
161
#ifdef SSR_DEC
162
        hDecoder->ssr_overlap[i] = NULL;
163
        hDecoder->prev_fmd[i] = NULL;
164
#endif
165
#ifdef MAIN_DEC
166
        hDecoder->pred_stat[i] = NULL;
167
#endif
168
#ifdef LTP_DEC
169
        hDecoder->ltp_lag[i] = 0;
170
        hDecoder->lt_pred_stat[i] = NULL;
171
#endif
172
1.10M
    }
173
174
17.3k
#ifdef SBR_DEC
175
848k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
176
831k
    {
177
831k
        hDecoder->sbr[i] = NULL;
178
831k
    }
179
17.3k
#endif
180
181
17.3k
    hDecoder->drc = drc_init(REAL_CONST(1.0), REAL_CONST(1.0));
182
183
17.3k
    return hDecoder;
184
17.3k
}
185
186
NeAACDecConfigurationPtr NeAACDecGetCurrentConfiguration(NeAACDecHandle hpDecoder)
187
26.1k
{
188
26.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
189
26.1k
    if (hDecoder)
190
26.1k
    {
191
26.1k
        NeAACDecConfigurationPtr config = &(hDecoder->config);
192
193
26.1k
        return config;
194
26.1k
    }
195
196
0
    return NULL;
197
26.1k
}
198
199
unsigned char NeAACDecSetConfiguration(NeAACDecHandle hpDecoder,
200
                                                   NeAACDecConfigurationPtr config)
201
26.3k
{
202
26.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
203
26.3k
    if (hDecoder && config)
204
26.3k
    {
205
        /* check if we can decode this object type */
206
26.3k
        if (can_decode_ot(config->defObjectType) < 0)
207
16
            return 0;
208
26.3k
        hDecoder->config.defObjectType = config->defObjectType;
209
210
        /* samplerate: anything but 0 should be possible */
211
26.3k
        if (config->defSampleRate == 0)
212
3
            return 0;
213
26.3k
        hDecoder->config.defSampleRate = config->defSampleRate;
214
215
        /* check output format */
216
26.3k
#ifdef FIXED_POINT
217
26.3k
        if ((config->outputFormat < 1) || (config->outputFormat > 4))
218
184
            return 0;
219
#else
220
        if ((config->outputFormat < 1) || (config->outputFormat > 5))
221
            return 0;
222
#endif
223
26.1k
        hDecoder->config.outputFormat = config->outputFormat;
224
225
26.1k
        if (config->downMatrix > 1)
226
32
            return 0;
227
26.1k
        hDecoder->config.downMatrix = config->downMatrix;
228
229
        /* OK */
230
26.1k
        return 1;
231
26.1k
    }
232
233
0
    return 0;
234
26.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
17.7k
{
269
17.7k
    uint32_t bits = 0;
270
17.7k
    bitfile ld;
271
17.7k
    adif_header adif;
272
17.7k
    adts_header adts;
273
17.7k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
17.7k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
41
        return -1;
278
279
17.6k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
17.6k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
17.6k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
17.6k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
17.6k
    *channels = 1;
284
285
17.6k
    if (buffer != NULL)
286
17.6k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
17.6k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
17.6k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
442
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
397
        {
314
397
            hDecoder->adif_header_present = 1;
315
316
397
            get_adif_header(&adif, &ld);
317
397
            faad_byte_align(&ld);
318
319
397
            hDecoder->sf_index = adif.pce[0].sf_index;
320
397
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
397
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
397
            *channels = adif.pce[0].channels;
324
325
397
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
397
            hDecoder->pce_set = 1;
327
328
397
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
17.3k
        } else if (adts_frame(&adts, &ld) == 0) {
332
606
            hDecoder->adts_header_present = 1;
333
334
606
            hDecoder->sf_index = adts.sf_index;
335
606
            hDecoder->object_type = adts.profile + 1;
336
337
606
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
606
            *channels = (adts.channel_configuration > 6) ?
339
544
                2 : adts.channel_configuration;
340
606
        }
341
342
17.6k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
17.6k
        faad_endbits(&ld);
348
17.6k
    }
349
350
17.6k
    if (!*samplerate)
351
161
        return -1;
352
353
17.5k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
17.5k
    if (*channels == 1)
356
16.7k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
16.7k
        *channels = 2;
359
16.7k
    }
360
17.5k
#endif
361
362
17.5k
    hDecoder->channelConfiguration = *channels;
363
364
17.5k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
17.5k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
8.26k
    {
368
8.26k
        *samplerate *= 2;
369
8.26k
        hDecoder->forceUpSampling = 1;
370
9.26k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
9.26k
        hDecoder->downSampledSBR = 1;
372
9.26k
    }
373
17.5k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
17.5k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
11.0k
    if (hDecoder->object_type == LD)
385
481
        hDecoder->frameLength >>= 1;
386
#endif
387
388
17.5k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
196
        return -1;
390
391
17.3k
    return bits;
392
17.5k
}
NeAACDecInit
Line
Count
Source
268
6.57k
{
269
6.57k
    uint32_t bits = 0;
270
6.57k
    bitfile ld;
271
6.57k
    adif_header adif;
272
6.57k
    adts_header adts;
273
6.57k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
6.57k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
22
        return -1;
278
279
6.55k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
6.55k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
6.55k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
6.55k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
6.55k
    *channels = 1;
284
285
6.55k
    if (buffer != NULL)
286
6.55k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
6.55k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
6.55k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
188
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
165
        {
314
165
            hDecoder->adif_header_present = 1;
315
316
165
            get_adif_header(&adif, &ld);
317
165
            faad_byte_align(&ld);
318
319
165
            hDecoder->sf_index = adif.pce[0].sf_index;
320
165
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
165
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
165
            *channels = adif.pce[0].channels;
324
325
165
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
165
            hDecoder->pce_set = 1;
327
328
165
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
6.38k
        } else if (adts_frame(&adts, &ld) == 0) {
332
101
            hDecoder->adts_header_present = 1;
333
334
101
            hDecoder->sf_index = adts.sf_index;
335
101
            hDecoder->object_type = adts.profile + 1;
336
337
101
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
101
            *channels = (adts.channel_configuration > 6) ?
339
85
                2 : adts.channel_configuration;
340
101
        }
341
342
6.55k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
6.55k
        faad_endbits(&ld);
348
6.55k
    }
349
350
6.55k
    if (!*samplerate)
351
76
        return -1;
352
353
6.47k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
6.47k
    if (*channels == 1)
356
6.29k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
6.29k
        *channels = 2;
359
6.29k
    }
360
6.47k
#endif
361
362
6.47k
    hDecoder->channelConfiguration = *channels;
363
364
6.47k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
6.47k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
3.08k
    {
368
3.08k
        *samplerate *= 2;
369
3.08k
        hDecoder->forceUpSampling = 1;
370
3.39k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
3.39k
        hDecoder->downSampledSBR = 1;
372
3.39k
    }
373
6.47k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
6.47k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
#ifdef LD_DEC
384
    if (hDecoder->object_type == LD)
385
        hDecoder->frameLength >>= 1;
386
#endif
387
388
6.47k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
107
        return -1;
390
391
6.36k
    return bits;
392
6.47k
}
NeAACDecInit
Line
Count
Source
268
11.1k
{
269
11.1k
    uint32_t bits = 0;
270
11.1k
    bitfile ld;
271
11.1k
    adif_header adif;
272
11.1k
    adts_header adts;
273
11.1k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
274
275
276
11.1k
    if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL) || (buffer_size == 0))
277
19
        return -1;
278
279
11.1k
    adts.old_format = hDecoder->config.useOldADTSFormat;
280
11.1k
    hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate);
281
11.1k
    hDecoder->object_type = hDecoder->config.defObjectType;
282
11.1k
    *samplerate = get_sample_rate(hDecoder->sf_index);
283
11.1k
    *channels = 1;
284
285
11.1k
    if (buffer != NULL)
286
11.1k
    {
287
#if 0
288
        int is_latm;
289
        latm_header *l = &hDecoder->latm_config;
290
#endif
291
292
11.1k
        faad_initbits(&ld, buffer, buffer_size);
293
294
#if 0
295
        memset(l, 0, sizeof(latm_header));
296
        is_latm = latmCheck(l, &ld);
297
        l->inited = 0;
298
        l->frameLength = 0;
299
        faad_rewindbits(&ld);
300
        if(is_latm && l->ASCbits>0)
301
        {
302
            int32_t x;
303
            hDecoder->latm_header_present = 1;
304
            x = NeAACDecInit2(hDecoder, l->ASC, (l->ASCbits+7)/8, samplerate, channels);
305
            if(x!=0)
306
                hDecoder->latm_header_present = 0;
307
            return x;
308
        } else
309
#endif
310
        /* Check if an ADIF header is present */
311
11.1k
        if ((buffer_size >= 8) && (buffer[0] == 'A') && (buffer[1] == 'D') &&
312
254
            (buffer[2] == 'I') && (buffer[3] == 'F'))
313
232
        {
314
232
            hDecoder->adif_header_present = 1;
315
316
232
            get_adif_header(&adif, &ld);
317
232
            faad_byte_align(&ld);
318
319
232
            hDecoder->sf_index = adif.pce[0].sf_index;
320
232
            hDecoder->object_type = adif.pce[0].object_type + 1;
321
322
232
            *samplerate = get_sample_rate(hDecoder->sf_index);
323
232
            *channels = adif.pce[0].channels;
324
325
232
            memcpy(&(hDecoder->pce), &(adif.pce[0]), sizeof(program_config));
326
232
            hDecoder->pce_set = 1;
327
328
232
            bits = bit2byte(faad_get_processed_bits(&ld));
329
330
        /* Check if an ADTS header is present */
331
10.9k
        } else if (adts_frame(&adts, &ld) == 0) {
332
505
            hDecoder->adts_header_present = 1;
333
334
505
            hDecoder->sf_index = adts.sf_index;
335
505
            hDecoder->object_type = adts.profile + 1;
336
337
505
            *samplerate = get_sample_rate(hDecoder->sf_index);
338
505
            *channels = (adts.channel_configuration > 6) ?
339
459
                2 : adts.channel_configuration;
340
505
        }
341
342
11.1k
        if (ld.error)
343
0
        {
344
0
            faad_endbits(&ld);
345
0
            return -1;
346
0
        }
347
11.1k
        faad_endbits(&ld);
348
11.1k
    }
349
350
11.1k
    if (!*samplerate)
351
85
        return -1;
352
353
11.0k
#if (defined(PS_DEC) || defined(DRM_PS))
354
    /* check if we have a mono file */
355
11.0k
    if (*channels == 1)
356
10.4k
    {
357
        /* upMatrix to 2 channels for implicit signalling of PS */
358
10.4k
        *channels = 2;
359
10.4k
    }
360
11.0k
#endif
361
362
11.0k
    hDecoder->channelConfiguration = *channels;
363
364
11.0k
#ifdef SBR_DEC
365
    /* implicit signalling */
366
11.0k
    if (*samplerate <= 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0))
367
5.18k
    {
368
5.18k
        *samplerate *= 2;
369
5.18k
        hDecoder->forceUpSampling = 1;
370
5.87k
    } else if (*samplerate > 24000 && (hDecoder->config.dontUpSampleImplicitSBR == 0)) {
371
5.87k
        hDecoder->downSampledSBR = 1;
372
5.87k
    }
373
11.0k
#endif
374
375
    /* must be done before frameLength is divided by 2 for LD */
376
#ifdef SSR_DEC
377
    if (hDecoder->object_type == SSR)
378
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
379
    else
380
#endif
381
11.0k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
382
383
11.0k
#ifdef LD_DEC
384
11.0k
    if (hDecoder->object_type == LD)
385
481
        hDecoder->frameLength >>= 1;
386
11.0k
#endif
387
388
11.0k
    if (can_decode_ot(hDecoder->object_type) < 0)
389
89
        return -1;
390
391
10.9k
    return bits;
392
11.0k
}
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.37k
{
401
8.37k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
8.37k
    int8_t rc;
403
8.37k
    mp4AudioSpecificConfig mp4ASC;
404
405
8.37k
    if((hDecoder == NULL)
406
8.37k
        || (pBuffer == NULL)
407
8.37k
        || (SizeOfDecoderSpecificInfo < 2)
408
8.36k
        || (samplerate == NULL)
409
8.36k
        || (channels == NULL))
410
13
    {
411
13
        return -1;
412
13
    }
413
414
8.36k
    hDecoder->adif_header_present = 0;
415
8.36k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
8.36k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
8.36k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
8.36k
    *samplerate = mp4ASC.samplingFrequency;
423
8.36k
    if (mp4ASC.channelsConfiguration)
424
7.00k
    {
425
7.00k
        *channels = mp4ASC.channelsConfiguration;
426
7.00k
    } else {
427
1.35k
        *channels = hDecoder->pce.channels;
428
1.35k
        hDecoder->pce_set = 1;
429
1.35k
    }
430
8.36k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
8.36k
    if (*channels == 1)
433
31
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
31
        *channels = 2;
436
31
    }
437
8.36k
#endif
438
8.36k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
8.36k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
8.36k
#ifdef ERROR_RESILIENCE
441
8.36k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
8.36k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
8.36k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
8.36k
#endif
445
8.36k
#ifdef SBR_DEC
446
8.36k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
8.36k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
8.36k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
8.36k
        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.36k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
5.19k
    {
456
5.19k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
5.19k
    }
458
8.36k
#endif
459
460
8.36k
    if (rc != 0)
461
92
    {
462
92
        return rc;
463
92
    }
464
8.27k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
8.27k
    if (mp4ASC.frameLengthFlag)
466
5.11k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
5.11k
        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.27k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
5.41k
    if (hDecoder->object_type == LD)
482
627
        hDecoder->frameLength >>= 1;
483
#endif
484
485
8.27k
    return 0;
486
8.36k
}
NeAACDecInit2
Line
Count
Source
400
2.89k
{
401
2.89k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
2.89k
    int8_t rc;
403
2.89k
    mp4AudioSpecificConfig mp4ASC;
404
405
2.89k
    if((hDecoder == NULL)
406
2.89k
        || (pBuffer == NULL)
407
2.89k
        || (SizeOfDecoderSpecificInfo < 2)
408
2.88k
        || (samplerate == NULL)
409
2.88k
        || (channels == NULL))
410
7
    {
411
7
        return -1;
412
7
    }
413
414
2.88k
    hDecoder->adif_header_present = 0;
415
2.88k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
2.88k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
2.88k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
2.88k
    *samplerate = mp4ASC.samplingFrequency;
423
2.88k
    if (mp4ASC.channelsConfiguration)
424
2.32k
    {
425
2.32k
        *channels = mp4ASC.channelsConfiguration;
426
2.32k
    } else {
427
558
        *channels = hDecoder->pce.channels;
428
558
        hDecoder->pce_set = 1;
429
558
    }
430
2.88k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
2.88k
    if (*channels == 1)
433
15
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
15
        *channels = 2;
436
15
    }
437
2.88k
#endif
438
2.88k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
2.88k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
2.88k
#ifdef ERROR_RESILIENCE
441
2.88k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
2.88k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
2.88k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
2.88k
#endif
445
2.88k
#ifdef SBR_DEC
446
2.88k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
2.88k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
2.88k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
2.88k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
2.88k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
1.74k
    {
456
1.74k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
1.74k
    }
458
2.88k
#endif
459
460
2.88k
    if (rc != 0)
461
33
    {
462
33
        return rc;
463
33
    }
464
2.85k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
2.85k
    if (mp4ASC.frameLengthFlag)
466
1.92k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
1.92k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
2.85k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
#ifdef LD_DEC
481
    if (hDecoder->object_type == LD)
482
        hDecoder->frameLength >>= 1;
483
#endif
484
485
2.85k
    return 0;
486
2.88k
}
NeAACDecInit2
Line
Count
Source
400
5.48k
{
401
5.48k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
402
5.48k
    int8_t rc;
403
5.48k
    mp4AudioSpecificConfig mp4ASC;
404
405
5.48k
    if((hDecoder == NULL)
406
5.48k
        || (pBuffer == NULL)
407
5.48k
        || (SizeOfDecoderSpecificInfo < 2)
408
5.47k
        || (samplerate == NULL)
409
5.47k
        || (channels == NULL))
410
6
    {
411
6
        return -1;
412
6
    }
413
414
5.47k
    hDecoder->adif_header_present = 0;
415
5.47k
    hDecoder->adts_header_present = 0;
416
417
    /* decode the audio specific config */
418
5.47k
    rc = AudioSpecificConfig2(pBuffer, SizeOfDecoderSpecificInfo, &mp4ASC,
419
5.47k
        &(hDecoder->pce), hDecoder->latm_header_present);
420
421
    /* copy the relevant info to the decoder handle */
422
5.47k
    *samplerate = mp4ASC.samplingFrequency;
423
5.47k
    if (mp4ASC.channelsConfiguration)
424
4.67k
    {
425
4.67k
        *channels = mp4ASC.channelsConfiguration;
426
4.67k
    } else {
427
800
        *channels = hDecoder->pce.channels;
428
800
        hDecoder->pce_set = 1;
429
800
    }
430
5.47k
#if (defined(PS_DEC) || defined(DRM_PS))
431
    /* check if we have a mono file */
432
5.47k
    if (*channels == 1)
433
16
    {
434
        /* upMatrix to 2 channels for implicit signalling of PS */
435
16
        *channels = 2;
436
16
    }
437
5.47k
#endif
438
5.47k
    hDecoder->sf_index = mp4ASC.samplingFrequencyIndex;
439
5.47k
    hDecoder->object_type = mp4ASC.objectTypeIndex;
440
5.47k
#ifdef ERROR_RESILIENCE
441
5.47k
    hDecoder->aacSectionDataResilienceFlag = mp4ASC.aacSectionDataResilienceFlag;
442
5.47k
    hDecoder->aacScalefactorDataResilienceFlag = mp4ASC.aacScalefactorDataResilienceFlag;
443
5.47k
    hDecoder->aacSpectralDataResilienceFlag = mp4ASC.aacSpectralDataResilienceFlag;
444
5.47k
#endif
445
5.47k
#ifdef SBR_DEC
446
5.47k
    hDecoder->sbr_present_flag = mp4ASC.sbr_present_flag;
447
5.47k
    hDecoder->downSampledSBR = mp4ASC.downSampledSBR;
448
5.47k
    if (hDecoder->config.dontUpSampleImplicitSBR == 0)
449
5.47k
        hDecoder->forceUpSampling = mp4ASC.forceUpSampling;
450
0
    else
451
0
        hDecoder->forceUpSampling = 0;
452
453
    /* AAC core decoder samplerate is 2 times as low */
454
5.47k
    if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || hDecoder->forceUpSampling == 1)
455
3.44k
    {
456
3.44k
        hDecoder->sf_index = get_sr_index(mp4ASC.samplingFrequency / 2);
457
3.44k
    }
458
5.47k
#endif
459
460
5.47k
    if (rc != 0)
461
59
    {
462
59
        return rc;
463
59
    }
464
5.41k
    hDecoder->channelConfiguration = mp4ASC.channelsConfiguration;
465
5.41k
    if (mp4ASC.frameLengthFlag)
466
3.19k
#ifdef ALLOW_SMALL_FRAMELENGTH
467
3.19k
        hDecoder->frameLength = 960;
468
#else
469
        return -1;
470
#endif
471
472
    /* must be done before frameLength is divided by 2 for LD */
473
#ifdef SSR_DEC
474
    if (hDecoder->object_type == SSR)
475
        hDecoder->fb = ssr_filter_bank_init(hDecoder->frameLength/SSR_BANDS);
476
    else
477
#endif
478
5.41k
        hDecoder->fb = filter_bank_init(hDecoder->frameLength);
479
480
5.41k
#ifdef LD_DEC
481
5.41k
    if (hDecoder->object_type == LD)
482
627
        hDecoder->frameLength >>= 1;
483
5.41k
#endif
484
485
5.41k
    return 0;
486
5.47k
}
487
488
#ifdef DRM
489
char NeAACDecInitDRM(NeAACDecHandle *hpDecoder,
490
                                 unsigned long samplerate,
491
                                 unsigned char channels)
492
780
{
493
780
    NeAACDecStruct** hDecoder = (NeAACDecStruct**)hpDecoder;
494
780
    if (hDecoder == NULL)
495
0
        return 1; /* error */
496
497
780
    NeAACDecClose(*hDecoder);
498
499
780
    *hDecoder = NeAACDecOpen();
500
501
    /* Special object type defined for DRM */
502
780
    (*hDecoder)->config.defObjectType = DRM_ER_LC;
503
504
780
    (*hDecoder)->config.defSampleRate = samplerate;
505
780
#ifdef ERROR_RESILIENCE // This shoudl always be defined for DRM
506
780
    (*hDecoder)->aacSectionDataResilienceFlag = 1; /* VCB11 */
507
780
    (*hDecoder)->aacScalefactorDataResilienceFlag = 0; /* no RVLC */
508
780
    (*hDecoder)->aacSpectralDataResilienceFlag = 1; /* HCR */
509
780
#endif
510
780
    (*hDecoder)->frameLength = 960;
511
780
    (*hDecoder)->sf_index = get_sr_index((*hDecoder)->config.defSampleRate);
512
780
    (*hDecoder)->object_type = (*hDecoder)->config.defObjectType;
513
514
780
    if ((channels == DRMCH_STEREO) || (channels == DRMCH_SBR_STEREO))
515
122
        (*hDecoder)->channelConfiguration = 2;
516
658
    else
517
658
        (*hDecoder)->channelConfiguration = 1;
518
519
780
#ifdef SBR_DEC
520
780
    if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO))
521
97
        (*hDecoder)->sbr_present_flag = 0;
522
683
    else
523
683
        (*hDecoder)->sbr_present_flag = 1;
524
780
#endif
525
526
780
    (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength);
527
528
780
    return 0;
529
780
}
530
#endif
531
532
void NeAACDecClose(NeAACDecHandle hpDecoder)
533
10.3k
{
534
10.3k
    uint8_t i;
535
10.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
536
537
10.3k
    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
670k
    for (i = 0; i < MAX_CHANNELS; i++)
549
660k
    {
550
660k
        if (hDecoder->time_out[i]) faad_free(hDecoder->time_out[i]);
551
660k
        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
660k
    }
563
564
#ifdef SSR_DEC
565
    if (hDecoder->object_type == SSR)
566
        ssr_filter_bank_end(hDecoder->fb);
567
    else
568
#endif
569
10.3k
        filter_bank_end(hDecoder->fb);
570
571
10.3k
    drc_end(hDecoder->drc);
572
573
10.3k
    if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);
574
575
10.3k
#ifdef SBR_DEC
576
505k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
577
495k
    {
578
495k
        if (hDecoder->sbr[i])
579
122k
            sbrDecodeEnd(hDecoder->sbr[i]);
580
495k
    }
581
10.3k
#endif
582
583
10.3k
    if (hDecoder) faad_free(hDecoder);
584
10.3k
}
585
586
void NeAACDecPostSeekReset(NeAACDecHandle hpDecoder, long frame)
587
20.0k
{
588
20.0k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
589
20.0k
    if (hDecoder)
590
20.0k
    {
591
20.0k
        hDecoder->postSeekResetFlag = 1;
592
593
20.0k
        if (frame != -1)
594
20.0k
            hDecoder->frame = frame;
595
20.0k
    }
596
20.0k
}
597
598
static void create_channel_config(NeAACDecStruct *hDecoder, NeAACDecFrameInfo *hInfo)
599
39.6k
{
600
39.6k
    hInfo->num_front_channels = 0;
601
39.6k
    hInfo->num_side_channels = 0;
602
39.6k
    hInfo->num_back_channels = 0;
603
39.6k
    hInfo->num_lfe_channels = 0;
604
39.6k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
39.6k
    if (hDecoder->downMatrix)
607
4.46k
    {
608
4.46k
        hInfo->num_front_channels = 2;
609
4.46k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
4.46k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
4.46k
        return;
612
4.46k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
35.2k
    if (hDecoder->pce_set)
617
1.50k
    {
618
1.50k
        uint8_t i, chpos = 0;
619
1.50k
        uint8_t chdir, back_center = 0;
620
621
1.50k
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
1.50k
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
1.50k
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
1.50k
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
1.50k
        chdir = hInfo->num_front_channels;
626
1.50k
        if (chdir & 1)
627
568
        {
628
568
#if (defined(PS_DEC) || defined(DRM_PS))
629
568
            if (hInfo->num_front_channels == 1 &&
630
264
                hInfo->num_side_channels == 0 &&
631
160
                hInfo->num_back_channels == 0 &&
632
116
                hInfo->num_lfe_channels == 0)
633
80
            {
634
                /* When PS is enabled output is always stereo */
635
80
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
80
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
80
            } else
638
488
#endif
639
488
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
568
            chdir--;
641
568
        }
642
7.76k
        for (i = 0; i < chdir; i++)
643
6.25k
        {
644
6.25k
            hInfo->channel_position[chpos++] =
645
6.25k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
6.25k
        }
647
648
6.92k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
5.41k
        {
650
5.41k
            hInfo->channel_position[chpos++] =
651
5.41k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
5.41k
        }
653
654
1.50k
        chdir = hInfo->num_back_channels;
655
1.50k
        if (chdir & 1)
656
220
        {
657
220
            back_center = 1;
658
220
            chdir--;
659
220
        }
660
4.94k
        for (i = 0; i < chdir; i++)
661
3.43k
        {
662
3.43k
            hInfo->channel_position[chpos++] =
663
3.43k
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
3.43k
        }
665
1.50k
        if (back_center)
666
220
        {
667
220
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
220
        }
669
670
2.27k
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
768
        {
672
768
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
768
        }
674
675
33.7k
    } else {
676
33.7k
        switch (hDecoder->channelConfiguration)
677
33.7k
        {
678
1.65k
        case 1:
679
1.65k
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
1.65k
            hInfo->num_front_channels = 2;
682
1.65k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
1.65k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
684
#else
685
            hInfo->num_front_channels = 1;
686
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
687
#endif
688
1.65k
            break;
689
15.1k
        case 2:
690
15.1k
            hInfo->num_front_channels = 2;
691
15.1k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
15.1k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
15.1k
            break;
694
2.42k
        case 3:
695
2.42k
            hInfo->num_front_channels = 3;
696
2.42k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
2.42k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
2.42k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
2.42k
            break;
700
4.44k
        case 4:
701
4.44k
            hInfo->num_front_channels = 3;
702
4.44k
            hInfo->num_back_channels = 1;
703
4.44k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
4.44k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
4.44k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
4.44k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
4.44k
            break;
708
2.46k
        case 5:
709
2.46k
            hInfo->num_front_channels = 3;
710
2.46k
            hInfo->num_back_channels = 2;
711
2.46k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
2.46k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
2.46k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
2.46k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
2.46k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
2.46k
            break;
717
1.37k
        case 6:
718
1.37k
            hInfo->num_front_channels = 3;
719
1.37k
            hInfo->num_back_channels = 2;
720
1.37k
            hInfo->num_lfe_channels = 1;
721
1.37k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
1.37k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
1.37k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
1.37k
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
1.37k
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
1.37k
            hInfo->channel_position[5] = LFE_CHANNEL;
727
1.37k
            break;
728
5.36k
        case 7:
729
5.36k
            hInfo->num_front_channels = 3;
730
5.36k
            hInfo->num_side_channels = 2;
731
5.36k
            hInfo->num_back_channels = 2;
732
5.36k
            hInfo->num_lfe_channels = 1;
733
5.36k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
5.36k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
5.36k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
5.36k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
5.36k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
5.36k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
5.36k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
5.36k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
5.36k
            break;
742
864
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
864
            {
744
864
                uint8_t i;
745
864
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
864
                if (ch & 1) /* there's either a center front or a center back channel */
747
320
                {
748
320
                    uint8_t ch1 = (ch-1)/2;
749
320
                    if (hDecoder->first_syn_ele == ID_SCE)
750
204
                    {
751
204
                        hInfo->num_front_channels = ch1 + 1;
752
204
                        hInfo->num_back_channels = ch1;
753
204
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
924
                        for (i = 1; i <= ch1; i+=2)
755
720
                        {
756
720
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
720
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
720
                        }
759
924
                        for (i = ch1+1; i < ch; i+=2)
760
720
                        {
761
720
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
720
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
720
                        }
764
204
                    } else {
765
116
                        hInfo->num_front_channels = ch1;
766
116
                        hInfo->num_back_channels = ch1 + 1;
767
784
                        for (i = 0; i < ch1; i+=2)
768
668
                        {
769
668
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
668
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
668
                        }
772
784
                        for (i = ch1; i < ch-1; i+=2)
773
668
                        {
774
668
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
668
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
668
                        }
777
116
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
116
                    }
779
544
                } else {
780
544
                    uint8_t ch1 = (ch)/2;
781
544
                    hInfo->num_front_channels = ch1;
782
544
                    hInfo->num_back_channels = ch1;
783
544
                    if (ch1 & 1)
784
256
                    {
785
256
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
1.70k
                        for (i = 1; i <= ch1; i+=2)
787
1.44k
                        {
788
1.44k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
1.44k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
1.44k
                        }
791
1.44k
                        for (i = ch1+1; i < ch-1; i+=2)
792
1.19k
                        {
793
1.19k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
1.19k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
1.19k
                        }
796
256
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
288
                    } else {
798
1.78k
                        for (i = 0; i < ch1; i+=2)
799
1.50k
                        {
800
1.50k
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
1.50k
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
1.50k
                        }
803
1.78k
                        for (i = ch1; i < ch; i+=2)
804
1.50k
                        {
805
1.50k
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
1.50k
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
1.50k
                        }
808
288
                    }
809
544
                }
810
864
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
2.04k
                for (i = ch; i < hDecoder->fr_channels; i++)
812
1.17k
                {
813
1.17k
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
1.17k
                }
815
864
            }
816
864
            break;
817
33.7k
        }
818
33.7k
    }
819
35.2k
}
decoder.c:create_channel_config
Line
Count
Source
599
9.92k
{
600
9.92k
    hInfo->num_front_channels = 0;
601
9.92k
    hInfo->num_side_channels = 0;
602
9.92k
    hInfo->num_back_channels = 0;
603
9.92k
    hInfo->num_lfe_channels = 0;
604
9.92k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
9.92k
    if (hDecoder->downMatrix)
607
1.11k
    {
608
1.11k
        hInfo->num_front_channels = 2;
609
1.11k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.11k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.11k
        return;
612
1.11k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
8.80k
    if (hDecoder->pce_set)
617
377
    {
618
377
        uint8_t i, chpos = 0;
619
377
        uint8_t chdir, back_center = 0;
620
621
377
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
377
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
377
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
377
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
377
        chdir = hInfo->num_front_channels;
626
377
        if (chdir & 1)
627
142
        {
628
142
#if (defined(PS_DEC) || defined(DRM_PS))
629
142
            if (hInfo->num_front_channels == 1 &&
630
66
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
29
                hInfo->num_lfe_channels == 0)
633
20
            {
634
                /* When PS is enabled output is always stereo */
635
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
20
            } else
638
122
#endif
639
122
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
142
            chdir--;
641
142
        }
642
1.94k
        for (i = 0; i < chdir; i++)
643
1.56k
        {
644
1.56k
            hInfo->channel_position[chpos++] =
645
1.56k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.56k
        }
647
648
1.73k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.35k
        {
650
1.35k
            hInfo->channel_position[chpos++] =
651
1.35k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.35k
        }
653
654
377
        chdir = hInfo->num_back_channels;
655
377
        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
858
        {
662
858
            hInfo->channel_position[chpos++] =
663
858
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
858
        }
665
377
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
569
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
192
        {
672
192
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
192
        }
674
675
8.42k
    } else {
676
8.42k
        switch (hDecoder->channelConfiguration)
677
8.42k
        {
678
414
        case 1:
679
414
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
414
            hInfo->num_front_channels = 2;
682
414
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
414
            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
414
            break;
689
3.78k
        case 2:
690
3.78k
            hInfo->num_front_channels = 2;
691
3.78k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
3.78k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
3.78k
            break;
694
607
        case 3:
695
607
            hInfo->num_front_channels = 3;
696
607
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
607
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
607
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
607
            break;
700
1.11k
        case 4:
701
1.11k
            hInfo->num_front_channels = 3;
702
1.11k
            hInfo->num_back_channels = 1;
703
1.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.11k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.11k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.11k
            break;
708
615
        case 5:
709
615
            hInfo->num_front_channels = 3;
710
615
            hInfo->num_back_channels = 2;
711
615
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
615
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
615
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
615
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
615
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
615
            break;
717
343
        case 6:
718
343
            hInfo->num_front_channels = 3;
719
343
            hInfo->num_back_channels = 2;
720
343
            hInfo->num_lfe_channels = 1;
721
343
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
343
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
343
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
343
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
343
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
343
            hInfo->channel_position[5] = LFE_CHANNEL;
727
343
            break;
728
1.34k
        case 7:
729
1.34k
            hInfo->num_front_channels = 3;
730
1.34k
            hInfo->num_side_channels = 2;
731
1.34k
            hInfo->num_back_channels = 2;
732
1.34k
            hInfo->num_lfe_channels = 1;
733
1.34k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.34k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.34k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.34k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.34k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.34k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.34k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.34k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.34k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
80
                {
748
80
                    uint8_t ch1 = (ch-1)/2;
749
80
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
231
                        for (i = 1; i <= ch1; i+=2)
755
180
                        {
756
180
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
180
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
180
                        }
759
231
                        for (i = ch1+1; i < ch; i+=2)
760
180
                        {
761
180
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
180
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
180
                        }
764
51
                    } else {
765
29
                        hInfo->num_front_channels = ch1;
766
29
                        hInfo->num_back_channels = ch1 + 1;
767
196
                        for (i = 0; i < ch1; i+=2)
768
167
                        {
769
167
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
167
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
167
                        }
772
196
                        for (i = ch1; i < ch-1; i+=2)
773
167
                        {
774
167
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
167
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
167
                        }
777
29
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
29
                    }
779
136
                } else {
780
136
                    uint8_t ch1 = (ch)/2;
781
136
                    hInfo->num_front_channels = ch1;
782
136
                    hInfo->num_back_channels = ch1;
783
136
                    if (ch1 & 1)
784
64
                    {
785
64
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
426
                        for (i = 1; i <= ch1; i+=2)
787
362
                        {
788
362
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
362
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
362
                        }
791
362
                        for (i = ch1+1; i < ch-1; i+=2)
792
298
                        {
793
298
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
298
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
298
                        }
796
64
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
447
                        for (i = 0; i < ch1; i+=2)
799
375
                        {
800
375
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
375
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
375
                        }
803
447
                        for (i = ch1; i < ch; i+=2)
804
375
                        {
805
375
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
375
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
375
                        }
808
72
                    }
809
136
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
510
                for (i = ch; i < hDecoder->fr_channels; i++)
812
294
                {
813
294
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
294
                }
815
216
            }
816
216
            break;
817
8.42k
        }
818
8.42k
    }
819
8.80k
}
decoder.c:create_channel_config
Line
Count
Source
599
9.92k
{
600
9.92k
    hInfo->num_front_channels = 0;
601
9.92k
    hInfo->num_side_channels = 0;
602
9.92k
    hInfo->num_back_channels = 0;
603
9.92k
    hInfo->num_lfe_channels = 0;
604
9.92k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
9.92k
    if (hDecoder->downMatrix)
607
1.11k
    {
608
1.11k
        hInfo->num_front_channels = 2;
609
1.11k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.11k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.11k
        return;
612
1.11k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
8.80k
    if (hDecoder->pce_set)
617
377
    {
618
377
        uint8_t i, chpos = 0;
619
377
        uint8_t chdir, back_center = 0;
620
621
377
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
377
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
377
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
377
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
377
        chdir = hInfo->num_front_channels;
626
377
        if (chdir & 1)
627
142
        {
628
142
#if (defined(PS_DEC) || defined(DRM_PS))
629
142
            if (hInfo->num_front_channels == 1 &&
630
66
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
29
                hInfo->num_lfe_channels == 0)
633
20
            {
634
                /* When PS is enabled output is always stereo */
635
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
20
            } else
638
122
#endif
639
122
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
142
            chdir--;
641
142
        }
642
1.94k
        for (i = 0; i < chdir; i++)
643
1.56k
        {
644
1.56k
            hInfo->channel_position[chpos++] =
645
1.56k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.56k
        }
647
648
1.73k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.35k
        {
650
1.35k
            hInfo->channel_position[chpos++] =
651
1.35k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.35k
        }
653
654
377
        chdir = hInfo->num_back_channels;
655
377
        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
858
        {
662
858
            hInfo->channel_position[chpos++] =
663
858
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
858
        }
665
377
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
569
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
192
        {
672
192
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
192
        }
674
675
8.42k
    } else {
676
8.42k
        switch (hDecoder->channelConfiguration)
677
8.42k
        {
678
414
        case 1:
679
414
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
414
            hInfo->num_front_channels = 2;
682
414
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
414
            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
414
            break;
689
3.78k
        case 2:
690
3.78k
            hInfo->num_front_channels = 2;
691
3.78k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
3.78k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
3.78k
            break;
694
607
        case 3:
695
607
            hInfo->num_front_channels = 3;
696
607
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
607
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
607
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
607
            break;
700
1.11k
        case 4:
701
1.11k
            hInfo->num_front_channels = 3;
702
1.11k
            hInfo->num_back_channels = 1;
703
1.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.11k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.11k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.11k
            break;
708
615
        case 5:
709
615
            hInfo->num_front_channels = 3;
710
615
            hInfo->num_back_channels = 2;
711
615
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
615
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
615
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
615
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
615
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
615
            break;
717
343
        case 6:
718
343
            hInfo->num_front_channels = 3;
719
343
            hInfo->num_back_channels = 2;
720
343
            hInfo->num_lfe_channels = 1;
721
343
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
343
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
343
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
343
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
343
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
343
            hInfo->channel_position[5] = LFE_CHANNEL;
727
343
            break;
728
1.34k
        case 7:
729
1.34k
            hInfo->num_front_channels = 3;
730
1.34k
            hInfo->num_side_channels = 2;
731
1.34k
            hInfo->num_back_channels = 2;
732
1.34k
            hInfo->num_lfe_channels = 1;
733
1.34k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.34k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.34k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.34k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.34k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.34k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.34k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.34k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.34k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
80
                {
748
80
                    uint8_t ch1 = (ch-1)/2;
749
80
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
231
                        for (i = 1; i <= ch1; i+=2)
755
180
                        {
756
180
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
180
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
180
                        }
759
231
                        for (i = ch1+1; i < ch; i+=2)
760
180
                        {
761
180
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
180
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
180
                        }
764
51
                    } else {
765
29
                        hInfo->num_front_channels = ch1;
766
29
                        hInfo->num_back_channels = ch1 + 1;
767
196
                        for (i = 0; i < ch1; i+=2)
768
167
                        {
769
167
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
167
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
167
                        }
772
196
                        for (i = ch1; i < ch-1; i+=2)
773
167
                        {
774
167
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
167
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
167
                        }
777
29
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
29
                    }
779
136
                } else {
780
136
                    uint8_t ch1 = (ch)/2;
781
136
                    hInfo->num_front_channels = ch1;
782
136
                    hInfo->num_back_channels = ch1;
783
136
                    if (ch1 & 1)
784
64
                    {
785
64
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
426
                        for (i = 1; i <= ch1; i+=2)
787
362
                        {
788
362
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
362
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
362
                        }
791
362
                        for (i = ch1+1; i < ch-1; i+=2)
792
298
                        {
793
298
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
298
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
298
                        }
796
64
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
447
                        for (i = 0; i < ch1; i+=2)
799
375
                        {
800
375
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
375
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
375
                        }
803
447
                        for (i = ch1; i < ch; i+=2)
804
375
                        {
805
375
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
375
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
375
                        }
808
72
                    }
809
136
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
510
                for (i = ch; i < hDecoder->fr_channels; i++)
812
294
                {
813
294
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
294
                }
815
216
            }
816
216
            break;
817
8.42k
        }
818
8.42k
    }
819
8.80k
}
decoder.c:create_channel_config
Line
Count
Source
599
9.92k
{
600
9.92k
    hInfo->num_front_channels = 0;
601
9.92k
    hInfo->num_side_channels = 0;
602
9.92k
    hInfo->num_back_channels = 0;
603
9.92k
    hInfo->num_lfe_channels = 0;
604
9.92k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
9.92k
    if (hDecoder->downMatrix)
607
1.11k
    {
608
1.11k
        hInfo->num_front_channels = 2;
609
1.11k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.11k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.11k
        return;
612
1.11k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
8.80k
    if (hDecoder->pce_set)
617
377
    {
618
377
        uint8_t i, chpos = 0;
619
377
        uint8_t chdir, back_center = 0;
620
621
377
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
377
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
377
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
377
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
377
        chdir = hInfo->num_front_channels;
626
377
        if (chdir & 1)
627
142
        {
628
142
#if (defined(PS_DEC) || defined(DRM_PS))
629
142
            if (hInfo->num_front_channels == 1 &&
630
66
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
29
                hInfo->num_lfe_channels == 0)
633
20
            {
634
                /* When PS is enabled output is always stereo */
635
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
20
            } else
638
122
#endif
639
122
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
142
            chdir--;
641
142
        }
642
1.94k
        for (i = 0; i < chdir; i++)
643
1.56k
        {
644
1.56k
            hInfo->channel_position[chpos++] =
645
1.56k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.56k
        }
647
648
1.73k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.35k
        {
650
1.35k
            hInfo->channel_position[chpos++] =
651
1.35k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.35k
        }
653
654
377
        chdir = hInfo->num_back_channels;
655
377
        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
858
        {
662
858
            hInfo->channel_position[chpos++] =
663
858
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
858
        }
665
377
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
569
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
192
        {
672
192
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
192
        }
674
675
8.42k
    } else {
676
8.42k
        switch (hDecoder->channelConfiguration)
677
8.42k
        {
678
414
        case 1:
679
414
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
414
            hInfo->num_front_channels = 2;
682
414
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
414
            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
414
            break;
689
3.78k
        case 2:
690
3.78k
            hInfo->num_front_channels = 2;
691
3.78k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
3.78k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
3.78k
            break;
694
607
        case 3:
695
607
            hInfo->num_front_channels = 3;
696
607
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
607
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
607
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
607
            break;
700
1.11k
        case 4:
701
1.11k
            hInfo->num_front_channels = 3;
702
1.11k
            hInfo->num_back_channels = 1;
703
1.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.11k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.11k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.11k
            break;
708
615
        case 5:
709
615
            hInfo->num_front_channels = 3;
710
615
            hInfo->num_back_channels = 2;
711
615
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
615
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
615
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
615
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
615
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
615
            break;
717
343
        case 6:
718
343
            hInfo->num_front_channels = 3;
719
343
            hInfo->num_back_channels = 2;
720
343
            hInfo->num_lfe_channels = 1;
721
343
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
343
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
343
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
343
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
343
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
343
            hInfo->channel_position[5] = LFE_CHANNEL;
727
343
            break;
728
1.34k
        case 7:
729
1.34k
            hInfo->num_front_channels = 3;
730
1.34k
            hInfo->num_side_channels = 2;
731
1.34k
            hInfo->num_back_channels = 2;
732
1.34k
            hInfo->num_lfe_channels = 1;
733
1.34k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.34k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.34k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.34k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.34k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.34k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.34k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.34k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.34k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
80
                {
748
80
                    uint8_t ch1 = (ch-1)/2;
749
80
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
231
                        for (i = 1; i <= ch1; i+=2)
755
180
                        {
756
180
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
180
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
180
                        }
759
231
                        for (i = ch1+1; i < ch; i+=2)
760
180
                        {
761
180
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
180
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
180
                        }
764
51
                    } else {
765
29
                        hInfo->num_front_channels = ch1;
766
29
                        hInfo->num_back_channels = ch1 + 1;
767
196
                        for (i = 0; i < ch1; i+=2)
768
167
                        {
769
167
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
167
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
167
                        }
772
196
                        for (i = ch1; i < ch-1; i+=2)
773
167
                        {
774
167
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
167
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
167
                        }
777
29
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
29
                    }
779
136
                } else {
780
136
                    uint8_t ch1 = (ch)/2;
781
136
                    hInfo->num_front_channels = ch1;
782
136
                    hInfo->num_back_channels = ch1;
783
136
                    if (ch1 & 1)
784
64
                    {
785
64
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
426
                        for (i = 1; i <= ch1; i+=2)
787
362
                        {
788
362
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
362
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
362
                        }
791
362
                        for (i = ch1+1; i < ch-1; i+=2)
792
298
                        {
793
298
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
298
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
298
                        }
796
64
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
447
                        for (i = 0; i < ch1; i+=2)
799
375
                        {
800
375
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
375
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
375
                        }
803
447
                        for (i = ch1; i < ch; i+=2)
804
375
                        {
805
375
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
375
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
375
                        }
808
72
                    }
809
136
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
510
                for (i = ch; i < hDecoder->fr_channels; i++)
812
294
                {
813
294
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
294
                }
815
216
            }
816
216
            break;
817
8.42k
        }
818
8.42k
    }
819
8.80k
}
decoder.c:create_channel_config
Line
Count
Source
599
9.92k
{
600
9.92k
    hInfo->num_front_channels = 0;
601
9.92k
    hInfo->num_side_channels = 0;
602
9.92k
    hInfo->num_back_channels = 0;
603
9.92k
    hInfo->num_lfe_channels = 0;
604
9.92k
    memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t));
605
606
9.92k
    if (hDecoder->downMatrix)
607
1.11k
    {
608
1.11k
        hInfo->num_front_channels = 2;
609
1.11k
        hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
610
1.11k
        hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
611
1.11k
        return;
612
1.11k
    }
613
614
    /* check if there is a PCE */
615
    /* TODO: why CPE flag is ignored? */
616
8.80k
    if (hDecoder->pce_set)
617
377
    {
618
377
        uint8_t i, chpos = 0;
619
377
        uint8_t chdir, back_center = 0;
620
621
377
        hInfo->num_front_channels = hDecoder->pce.num_front_channels;
622
377
        hInfo->num_side_channels = hDecoder->pce.num_side_channels;
623
377
        hInfo->num_back_channels = hDecoder->pce.num_back_channels;
624
377
        hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels;
625
377
        chdir = hInfo->num_front_channels;
626
377
        if (chdir & 1)
627
142
        {
628
142
#if (defined(PS_DEC) || defined(DRM_PS))
629
142
            if (hInfo->num_front_channels == 1 &&
630
66
                hInfo->num_side_channels == 0 &&
631
40
                hInfo->num_back_channels == 0 &&
632
29
                hInfo->num_lfe_channels == 0)
633
20
            {
634
                /* When PS is enabled output is always stereo */
635
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT;
636
20
                hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT;
637
20
            } else
638
122
#endif
639
122
            hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER;
640
142
            chdir--;
641
142
        }
642
1.94k
        for (i = 0; i < chdir; i++)
643
1.56k
        {
644
1.56k
            hInfo->channel_position[chpos++] =
645
1.56k
                (i & 1) ? FRONT_CHANNEL_RIGHT : FRONT_CHANNEL_LEFT;
646
1.56k
        }
647
648
1.73k
        for (i = 0; i < hInfo->num_side_channels; i++)
649
1.35k
        {
650
1.35k
            hInfo->channel_position[chpos++] =
651
1.35k
                (i & 1) ? SIDE_CHANNEL_RIGHT : SIDE_CHANNEL_LEFT;
652
1.35k
        }
653
654
377
        chdir = hInfo->num_back_channels;
655
377
        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
858
        {
662
858
            hInfo->channel_position[chpos++] =
663
858
                (i & 1) ? BACK_CHANNEL_RIGHT : BACK_CHANNEL_LEFT;
664
858
        }
665
377
        if (back_center)
666
55
        {
667
55
            hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER;
668
55
        }
669
670
569
        for (i = 0; i < hInfo->num_lfe_channels; i++)
671
192
        {
672
192
            hInfo->channel_position[chpos++] = LFE_CHANNEL;
673
192
        }
674
675
8.42k
    } else {
676
8.42k
        switch (hDecoder->channelConfiguration)
677
8.42k
        {
678
414
        case 1:
679
414
#if (defined(PS_DEC) || defined(DRM_PS))
680
            /* When PS is enabled output is always stereo */
681
414
            hInfo->num_front_channels = 2;
682
414
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
683
414
            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
414
            break;
689
3.78k
        case 2:
690
3.78k
            hInfo->num_front_channels = 2;
691
3.78k
            hInfo->channel_position[0] = FRONT_CHANNEL_LEFT;
692
3.78k
            hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT;
693
3.78k
            break;
694
607
        case 3:
695
607
            hInfo->num_front_channels = 3;
696
607
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
697
607
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
698
607
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
699
607
            break;
700
1.11k
        case 4:
701
1.11k
            hInfo->num_front_channels = 3;
702
1.11k
            hInfo->num_back_channels = 1;
703
1.11k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
704
1.11k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
705
1.11k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
706
1.11k
            hInfo->channel_position[3] = BACK_CHANNEL_CENTER;
707
1.11k
            break;
708
615
        case 5:
709
615
            hInfo->num_front_channels = 3;
710
615
            hInfo->num_back_channels = 2;
711
615
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
712
615
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
713
615
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
714
615
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
715
615
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
716
615
            break;
717
343
        case 6:
718
343
            hInfo->num_front_channels = 3;
719
343
            hInfo->num_back_channels = 2;
720
343
            hInfo->num_lfe_channels = 1;
721
343
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
722
343
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
723
343
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
724
343
            hInfo->channel_position[3] = BACK_CHANNEL_LEFT;
725
343
            hInfo->channel_position[4] = BACK_CHANNEL_RIGHT;
726
343
            hInfo->channel_position[5] = LFE_CHANNEL;
727
343
            break;
728
1.34k
        case 7:
729
1.34k
            hInfo->num_front_channels = 3;
730
1.34k
            hInfo->num_side_channels = 2;
731
1.34k
            hInfo->num_back_channels = 2;
732
1.34k
            hInfo->num_lfe_channels = 1;
733
1.34k
            hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
734
1.34k
            hInfo->channel_position[1] = FRONT_CHANNEL_LEFT;
735
1.34k
            hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT;
736
1.34k
            hInfo->channel_position[3] = SIDE_CHANNEL_LEFT;
737
1.34k
            hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT;
738
1.34k
            hInfo->channel_position[5] = BACK_CHANNEL_LEFT;
739
1.34k
            hInfo->channel_position[6] = BACK_CHANNEL_RIGHT;
740
1.34k
            hInfo->channel_position[7] = LFE_CHANNEL;
741
1.34k
            break;
742
216
        default: /* channelConfiguration == 0 || channelConfiguration > 7 */
743
216
            {
744
216
                uint8_t i;
745
216
                uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe;
746
216
                if (ch & 1) /* there's either a center front or a center back channel */
747
80
                {
748
80
                    uint8_t ch1 = (ch-1)/2;
749
80
                    if (hDecoder->first_syn_ele == ID_SCE)
750
51
                    {
751
51
                        hInfo->num_front_channels = ch1 + 1;
752
51
                        hInfo->num_back_channels = ch1;
753
51
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
754
231
                        for (i = 1; i <= ch1; i+=2)
755
180
                        {
756
180
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
757
180
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
758
180
                        }
759
231
                        for (i = ch1+1; i < ch; i+=2)
760
180
                        {
761
180
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
762
180
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
763
180
                        }
764
51
                    } else {
765
29
                        hInfo->num_front_channels = ch1;
766
29
                        hInfo->num_back_channels = ch1 + 1;
767
196
                        for (i = 0; i < ch1; i+=2)
768
167
                        {
769
167
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
770
167
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
771
167
                        }
772
196
                        for (i = ch1; i < ch-1; i+=2)
773
167
                        {
774
167
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
775
167
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
776
167
                        }
777
29
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
778
29
                    }
779
136
                } else {
780
136
                    uint8_t ch1 = (ch)/2;
781
136
                    hInfo->num_front_channels = ch1;
782
136
                    hInfo->num_back_channels = ch1;
783
136
                    if (ch1 & 1)
784
64
                    {
785
64
                        hInfo->channel_position[0] = FRONT_CHANNEL_CENTER;
786
426
                        for (i = 1; i <= ch1; i+=2)
787
362
                        {
788
362
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
789
362
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
790
362
                        }
791
362
                        for (i = ch1+1; i < ch-1; i+=2)
792
298
                        {
793
298
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
794
298
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
795
298
                        }
796
64
                        hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER;
797
72
                    } else {
798
447
                        for (i = 0; i < ch1; i+=2)
799
375
                        {
800
375
                            hInfo->channel_position[i] = FRONT_CHANNEL_LEFT;
801
375
                            hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT;
802
375
                        }
803
447
                        for (i = ch1; i < ch; i+=2)
804
375
                        {
805
375
                            hInfo->channel_position[i] = BACK_CHANNEL_LEFT;
806
375
                            hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT;
807
375
                        }
808
72
                    }
809
136
                }
810
216
                hInfo->num_lfe_channels = hDecoder->has_lfe;
811
510
                for (i = ch; i < hDecoder->fr_channels; i++)
812
294
                {
813
294
                    hInfo->channel_position[i] = LFE_CHANNEL;
814
294
                }
815
216
            }
816
216
            break;
817
8.42k
        }
818
8.42k
    }
819
8.80k
}
820
821
void* NeAACDecDecode(NeAACDecHandle hpDecoder,
822
                                 NeAACDecFrameInfo *hInfo,
823
                                 unsigned char *buffer,
824
                                 unsigned long buffer_size)
825
38.3k
{
826
38.3k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
827
38.3k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, NULL, 0);
828
38.3k
}
829
830
void* NeAACDecDecode2(NeAACDecHandle hpDecoder,
831
                                  NeAACDecFrameInfo *hInfo,
832
                                  unsigned char *buffer,
833
                                  unsigned long buffer_size,
834
                                  void **sample_buffer,
835
                                  unsigned long sample_buffer_size)
836
12.6k
{
837
12.6k
    NeAACDecStruct* hDecoder = (NeAACDecStruct*)hpDecoder;
838
12.6k
    if ((sample_buffer_size == 0) || (sample_buffer == NULL) || (*sample_buffer == NULL))
839
101
    {
840
101
        hInfo->error = 27;
841
101
        return NULL;
842
101
    }
843
844
12.5k
    return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size,
845
12.5k
        sample_buffer, sample_buffer_size);
846
12.6k
}
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
50.8k
{
855
50.8k
    uint16_t i;
856
50.8k
    uint8_t channels = 0;
857
50.8k
    uint8_t output_channels = 0;
858
50.8k
    bitfile ld;
859
50.8k
    uint32_t bitsconsumed;
860
50.8k
    uint16_t frame_len;
861
50.8k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
50.8k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
50.8k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
50.8k
    frame_len = hDecoder->frameLength;
882
883
884
50.8k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
50.8k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
50.8k
    if (buffer_size >= 128)
903
6.13k
    {
904
6.13k
        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.13k
    }
912
913
914
    /* initialize the bitstream */
915
50.8k
    faad_initbits(&ld, buffer, buffer_size);
916
50.8k
    if (ld.error != 0)
917
20.3k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
11.0k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.00k
    {
953
        /* We do not support stereo right now */
954
1.00k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.00k
        faad_getbits(&ld, 8
961
1.00k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.00k
    }
963
11.0k
#endif
964
965
30.5k
    if (hDecoder->adts_header_present)
966
868
    {
967
868
        adts_header adts;
968
969
868
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
868
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
106
            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
868
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
#ifdef DRM
985
11.0k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.00k
    {
987
1.00k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.0k
    } else {
989
10.0k
#endif
990
19.4k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
19.4k
    channels = hDecoder->fr_channels;
1009
1010
30.4k
    if (hInfo->error > 0)
1011
20.4k
        goto error;
1012
1013
    /* safety check */
1014
10.0k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
110
    {
1016
        /* invalid number of channels */
1017
110
        hInfo->error = 12;
1018
110
        goto error;
1019
110
    }
1020
1021
    /* no more bit reading after this */
1022
9.93k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
9.93k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
9.93k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
9.93k
    faad_endbits(&ld);
1030
1031
1032
9.93k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
9.93k
        )
1037
9.70k
    {
1038
9.70k
        if (hDecoder->channelConfiguration == 0)
1039
341
            hDecoder->channelConfiguration = channels;
1040
1041
9.70k
        if (channels == 8) /* 7.1 */
1042
931
            hDecoder->channelConfiguration = 7;
1043
9.70k
        if (channels == 7) /* not a standard channelConfiguration */
1044
47
            hDecoder->channelConfiguration = 0;
1045
9.70k
    }
1046
1047
9.93k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
1.10k
    {
1049
1.10k
        hDecoder->downMatrix = 1;
1050
1.10k
        output_channels = 2;
1051
8.82k
    } else {
1052
8.82k
        output_channels = channels;
1053
8.82k
    }
1054
1055
9.93k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
9.93k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
9.93k
    if (output_channels == 1)
1059
250
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
250
        hDecoder->upMatrix = 1;
1062
250
        output_channels = 2;
1063
250
    }
1064
9.93k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
9.93k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
386
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
386
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
9
        {
1072
9
            hInfo->error = 22;
1073
9
            return NULL;
1074
9
        }
1075
386
    }
1076
9.92k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
9.92k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
9.92k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
9.92k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
9.92k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
9.92k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
9.92k
    hInfo->header_type = RAW;
1090
9.92k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
9.92k
    if (hDecoder->adts_header_present)
1093
228
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
9.92k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
9.92k
    hInfo->ps = hDecoder->ps_used_global;
1100
9.92k
#endif
1101
1102
    /* check if frame has channel elements */
1103
9.92k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
9.92k
    {
1110
9.92k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
9.92k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
9.92k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
9.92k
        };
1114
9.92k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
9.92k
#ifdef SBR_DEC
1116
9.92k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
5.49k
        {
1118
5.49k
            stride = 2 * stride;
1119
5.49k
        }
1120
9.92k
#endif
1121
9.92k
        required_buffer_size = frame_len*output_channels*stride;
1122
9.92k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
9.92k
    if (sample_buffer_size == 0)
1126
6.41k
    {
1127
        /* allocate the buffer for the final samples */
1128
6.41k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
5.56k
        {
1130
5.56k
            if (hDecoder->sample_buffer)
1131
101
                faad_free(hDecoder->sample_buffer);
1132
5.56k
            hDecoder->sample_buffer = NULL;
1133
5.56k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
5.56k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
5.56k
        }
1136
6.41k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
1.32k
        hInfo->error = 27;
1139
1.32k
        return NULL;
1140
1.32k
    }
1141
1142
8.60k
    if (sample_buffer_size == 0)
1143
6.41k
    {
1144
6.41k
        sample_buffer = hDecoder->sample_buffer;
1145
6.41k
    } else {
1146
2.18k
        sample_buffer = *sample_buffer2;
1147
2.18k
    }
1148
1149
8.60k
#ifdef SBR_DEC
1150
8.60k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
5.85k
    {
1152
5.85k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
5.85k
        if (!hDecoder->downSampledSBR)
1156
4.55k
        {
1157
4.55k
            frame_len *= 2;
1158
4.55k
            hInfo->samples *= 2;
1159
4.55k
            hInfo->samplerate *= 2;
1160
4.55k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
30.6k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
24.8k
        {
1165
24.8k
            if (hDecoder->sbr[ele] == NULL)
1166
7
            {
1167
7
                hInfo->error = 25;
1168
7
                goto error;
1169
7
            }
1170
24.8k
        }
1171
1172
        /* sbr */
1173
5.85k
        if (hDecoder->sbr_present_flag == 1)
1174
3.84k
        {
1175
3.84k
            hInfo->object_type = HE_AAC;
1176
3.84k
            hInfo->sbr = SBR_UPSAMPLED;
1177
3.84k
        } else {
1178
2.01k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
2.01k
        }
1180
5.85k
        if (hDecoder->downSampledSBR)
1181
1.30k
        {
1182
1.30k
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
1.30k
        }
1184
5.85k
    }
1185
8.59k
#endif
1186
1187
1188
8.59k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
8.59k
        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.59k
    hDecoder->postSeekResetFlag = 0;
1198
1199
8.59k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
5.62k
    if (hDecoder->object_type != LD)
1202
4.86k
    {
1203
4.86k
#endif
1204
7.83k
        if (hDecoder->frame <= 1)
1205
2.84k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
763
    } else {
1208
        /* LD encoders will give lower delay */
1209
763
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
763
    }
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.59k
    return sample_buffer;
1225
1226
20.6k
error:
1227
1228
    /* reset filterbank state */
1229
1.34M
    for (i = 0; i < MAX_CHANNELS; i++)
1230
1.32M
    {
1231
1.32M
        if (hDecoder->fb_intermed[i] != NULL)
1232
644k
        {
1233
644k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
644k
        }
1235
1.32M
    }
1236
20.6k
#ifdef SBR_DEC
1237
1.01M
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
990k
    {
1239
990k
        if (hDecoder->sbr[i] != NULL)
1240
467k
        {
1241
467k
            sbrReset(hDecoder->sbr[i]);
1242
467k
        }
1243
990k
    }
1244
20.6k
#endif
1245
1246
1247
20.6k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
8.60k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
18.3k
{
855
18.3k
    uint16_t i;
856
18.3k
    uint8_t channels = 0;
857
18.3k
    uint8_t output_channels = 0;
858
18.3k
    bitfile ld;
859
18.3k
    uint32_t bitsconsumed;
860
18.3k
    uint16_t frame_len;
861
18.3k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
18.3k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
18.3k
    if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL))
873
0
    {
874
0
        return NULL;
875
0
    }
876
877
#if 0
878
    printf("%d\n", buffer_size*8);
879
#endif
880
881
18.3k
    frame_len = hDecoder->frameLength;
882
883
884
18.3k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
18.3k
    memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0]));
886
887
#ifdef USE_TIME_LIMIT
888
    if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count)
889
    {
890
        hDecoder->TL_count += 1024;
891
    } else {
892
        hInfo->error = (NUM_ERROR_MESSAGES-1);
893
        goto error;
894
    }
895
#endif
896
897
898
    /* check for some common metadata tag types in the bitstream
899
     * No need to return an error
900
     */
901
    /* ID3 */
902
18.3k
    if (buffer_size >= 128)
903
2.74k
    {
904
2.74k
        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.74k
    }
912
913
914
    /* initialize the bitstream */
915
18.3k
    faad_initbits(&ld, buffer, buffer_size);
916
18.3k
    if (ld.error != 0)
917
7.26k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
11.0k
#ifdef DRM
951
11.0k
    if (hDecoder->object_type == DRM_ER_LC)
952
1.00k
    {
953
        /* We do not support stereo right now */
954
1.00k
        if (0) //(hDecoder->channelConfiguration == 2)
955
0
        {
956
0
            hInfo->error = 28; // Throw CRC error
957
0
            goto error;
958
0
        }
959
960
1.00k
        faad_getbits(&ld, 8
961
1.00k
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
1.00k
    }
963
11.0k
#endif
964
965
11.0k
    if (hDecoder->adts_header_present)
966
87
    {
967
87
        adts_header adts;
968
969
87
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
87
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
3
            goto error;
972
973
        /* MPEG2 does byte_alignment() here,
974
         * but ADTS header is always multiple of 8 bits in MPEG2
975
         * so not needed to actually do it.
976
         */
977
87
    }
978
979
#ifdef ANALYSIS
980
    dbg_count = 0;
981
#endif
982
983
    /* decode the complete bitstream */
984
11.0k
#ifdef DRM
985
11.0k
    if (/*(hDecoder->object_type == 6) ||*/ hDecoder->object_type == DRM_ER_LC)
986
1.00k
    {
987
1.00k
        DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
988
10.0k
    } else {
989
10.0k
#endif
990
10.0k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
10.0k
#ifdef DRM
992
10.0k
    }
993
11.0k
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
11.0k
    channels = hDecoder->fr_channels;
1009
1010
11.0k
    if (hInfo->error > 0)
1011
7.40k
        goto error;
1012
1013
    /* safety check */
1014
3.65k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
33
    {
1016
        /* invalid number of channels */
1017
33
        hInfo->error = 12;
1018
33
        goto error;
1019
33
    }
1020
1021
    /* no more bit reading after this */
1022
3.62k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
3.62k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
3.62k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
3.62k
    faad_endbits(&ld);
1030
1031
1032
3.62k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
3.62k
        )
1037
3.58k
    {
1038
3.58k
        if (hDecoder->channelConfiguration == 0)
1039
152
            hDecoder->channelConfiguration = channels;
1040
1041
3.58k
        if (channels == 8) /* 7.1 */
1042
153
            hDecoder->channelConfiguration = 7;
1043
3.58k
        if (channels == 7) /* not a standard channelConfiguration */
1044
0
            hDecoder->channelConfiguration = 0;
1045
3.58k
    }
1046
1047
3.62k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
570
    {
1049
570
        hDecoder->downMatrix = 1;
1050
570
        output_channels = 2;
1051
3.05k
    } else {
1052
3.05k
        output_channels = channels;
1053
3.05k
    }
1054
1055
3.62k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
3.62k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
3.62k
    if (output_channels == 1)
1059
0
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
0
        hDecoder->upMatrix = 1;
1062
0
        output_channels = 2;
1063
0
    }
1064
3.62k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
3.62k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
171
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
171
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
3
        {
1072
3
            hInfo->error = 22;
1073
3
            return NULL;
1074
3
        }
1075
171
    }
1076
3.61k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
3.61k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
3.61k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
3.61k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
3.61k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
3.61k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
3.61k
    hInfo->header_type = RAW;
1090
3.61k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
3.61k
    if (hDecoder->adts_header_present)
1093
32
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
3.61k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
3.61k
    hInfo->ps = hDecoder->ps_used_global;
1100
3.61k
#endif
1101
1102
    /* check if frame has channel elements */
1103
3.61k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
3.61k
    {
1110
3.61k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
3.61k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
3.61k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
3.61k
        };
1114
3.61k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
3.61k
#ifdef SBR_DEC
1116
3.61k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
1.97k
        {
1118
1.97k
            stride = 2 * stride;
1119
1.97k
        }
1120
3.61k
#endif
1121
3.61k
        required_buffer_size = frame_len*output_channels*stride;
1122
3.61k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
3.61k
    if (sample_buffer_size == 0)
1126
1.85k
    {
1127
        /* allocate the buffer for the final samples */
1128
1.85k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
1.61k
        {
1130
1.61k
            if (hDecoder->sample_buffer)
1131
42
                faad_free(hDecoder->sample_buffer);
1132
1.61k
            hDecoder->sample_buffer = NULL;
1133
1.61k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
1.61k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
1.61k
        }
1136
1.85k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
647
        hInfo->error = 27;
1139
647
        return NULL;
1140
647
    }
1141
1142
2.97k
    if (sample_buffer_size == 0)
1143
1.85k
    {
1144
1.85k
        sample_buffer = hDecoder->sample_buffer;
1145
1.85k
    } else {
1146
1.12k
        sample_buffer = *sample_buffer2;
1147
1.12k
    }
1148
1149
2.97k
#ifdef SBR_DEC
1150
2.97k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
2.07k
    {
1152
2.07k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
2.07k
        if (!hDecoder->downSampledSBR)
1156
1.51k
        {
1157
1.51k
            frame_len *= 2;
1158
1.51k
            hInfo->samples *= 2;
1159
1.51k
            hInfo->samplerate *= 2;
1160
1.51k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
10.6k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
8.54k
        {
1165
8.54k
            if (hDecoder->sbr[ele] == NULL)
1166
3
            {
1167
3
                hInfo->error = 25;
1168
3
                goto error;
1169
3
            }
1170
8.54k
        }
1171
1172
        /* sbr */
1173
2.07k
        if (hDecoder->sbr_present_flag == 1)
1174
1.59k
        {
1175
1.59k
            hInfo->object_type = HE_AAC;
1176
1.59k
            hInfo->sbr = SBR_UPSAMPLED;
1177
1.59k
        } else {
1178
480
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
480
        }
1180
2.07k
        if (hDecoder->downSampledSBR)
1181
562
        {
1182
562
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
562
        }
1184
2.07k
    }
1185
2.96k
#endif
1186
1187
1188
2.96k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
2.96k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
2.96k
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
2.96k
#endif
1195
1196
1197
2.96k
    hDecoder->postSeekResetFlag = 0;
1198
1199
2.96k
    hDecoder->frame++;
1200
#ifdef LD_DEC
1201
    if (hDecoder->object_type != LD)
1202
    {
1203
#endif
1204
2.96k
        if (hDecoder->frame <= 1)
1205
1.15k
            hInfo->samples = 0;
1206
#ifdef LD_DEC
1207
    } else {
1208
        /* LD encoders will give lower delay */
1209
        if (hDecoder->frame <= 0)
1210
            hInfo->samples = 0;
1211
    }
1212
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
2.96k
    return sample_buffer;
1225
1226
7.44k
error:
1227
1228
    /* reset filterbank state */
1229
483k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
476k
    {
1231
476k
        if (hDecoder->fb_intermed[i] != NULL)
1232
180k
        {
1233
180k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
180k
        }
1235
476k
    }
1236
7.44k
#ifdef SBR_DEC
1237
364k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
357k
    {
1239
357k
        if (hDecoder->sbr[i] != NULL)
1240
138k
        {
1241
138k
            sbrReset(hDecoder->sbr[i]);
1242
138k
        }
1243
357k
    }
1244
7.44k
#endif
1245
1246
1247
7.44k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
2.97k
}
decoder.c:aac_frame_decode
Line
Count
Source
854
32.5k
{
855
32.5k
    uint16_t i;
856
32.5k
    uint8_t channels = 0;
857
32.5k
    uint8_t output_channels = 0;
858
32.5k
    bitfile ld;
859
32.5k
    uint32_t bitsconsumed;
860
32.5k
    uint16_t frame_len;
861
32.5k
    void *sample_buffer;
862
#if 0
863
    uint32_t startbit=0, endbit=0, payload_bits=0;
864
#endif
865
32.5k
    uint32_t required_buffer_size=0;
866
867
#ifdef PROFILE
868
    int64_t count = faad_get_ts();
869
#endif
870
871
    /* safety checks */
872
32.5k
    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
32.5k
    frame_len = hDecoder->frameLength;
882
883
884
32.5k
    memset(hInfo, 0, sizeof(NeAACDecFrameInfo));
885
32.5k
    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
32.5k
    if (buffer_size >= 128)
903
3.39k
    {
904
3.39k
        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.39k
    }
912
913
914
    /* initialize the bitstream */
915
32.5k
    faad_initbits(&ld, buffer, buffer_size);
916
32.5k
    if (ld.error != 0)
917
13.0k
        return NULL;
918
919
#if 0
920
    {
921
        int i;
922
        for (i = 0; i < ((buffer_size+3)>>2); i++)
923
        {
924
            uint8_t *buf;
925
            uint32_t temp = 0;
926
            buf = faad_getbitbuffer(&ld, 32);
927
            //temp = getdword((void*)buf);
928
            temp = *((uint32_t*)buf);
929
            printf("0x%.8X\n", temp);
930
            free(buf);
931
        }
932
        faad_endbits(&ld);
933
        faad_initbits(&ld, buffer, buffer_size);
934
    }
935
#endif
936
937
#if 0
938
    if(hDecoder->latm_header_present)
939
    {
940
        payload_bits = faad_latm_frame(&hDecoder->latm_config, &ld);
941
        startbit = faad_get_processed_bits(&ld);
942
        if(payload_bits == -1U)
943
        {
944
            hInfo->error = 1;
945
            goto error;
946
        }
947
    }
948
#endif
949
950
#ifdef DRM
951
    if (hDecoder->object_type == DRM_ER_LC)
952
    {
953
        /* We do not support stereo right now */
954
        if (0) //(hDecoder->channelConfiguration == 2)
955
        {
956
            hInfo->error = 28; // Throw CRC error
957
            goto error;
958
        }
959
960
        faad_getbits(&ld, 8
961
            DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC"));
962
    }
963
#endif
964
965
19.5k
    if (hDecoder->adts_header_present)
966
781
    {
967
781
        adts_header adts;
968
969
781
        adts.old_format = hDecoder->config.useOldADTSFormat;
970
781
        if ((hInfo->error = adts_frame(&adts, &ld)) > 0)
971
103
            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
781
    }
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
19.4k
        raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);
991
#ifdef DRM
992
    }
993
#endif
994
995
#if 0
996
    if(hDecoder->latm_header_present)
997
    {
998
        endbit = faad_get_processed_bits(&ld);
999
        if(endbit-startbit > payload_bits)
1000
            fprintf(stderr, "\r\nERROR, too many payload bits read: %u > %d. Please. report with a link to a sample\n",
1001
                endbit-startbit, payload_bits);
1002
        if(hDecoder->latm_config.otherDataLenBits > 0)
1003
            faad_getbits(&ld, hDecoder->latm_config.otherDataLenBits);
1004
        faad_byte_align(&ld);
1005
    }
1006
#endif
1007
1008
19.4k
    channels = hDecoder->fr_channels;
1009
1010
19.4k
    if (hInfo->error > 0)
1011
13.0k
        goto error;
1012
1013
    /* safety check */
1014
6.38k
    if (channels == 0 || channels > MAX_CHANNELS)
1015
77
    {
1016
        /* invalid number of channels */
1017
77
        hInfo->error = 12;
1018
77
        goto error;
1019
77
    }
1020
1021
    /* no more bit reading after this */
1022
6.31k
    bitsconsumed = faad_get_processed_bits(&ld);
1023
6.31k
    hInfo->bytesconsumed = bit2byte(bitsconsumed);
1024
6.31k
    if (ld.error)
1025
0
    {
1026
0
        hInfo->error = 14;
1027
0
        goto error;
1028
0
    }
1029
6.31k
    faad_endbits(&ld);
1030
1031
1032
6.31k
    if (!hDecoder->adts_header_present && !hDecoder->adif_header_present
1033
#if 0
1034
        && !hDecoder->latm_header_present
1035
#endif
1036
6.31k
        )
1037
6.11k
    {
1038
6.11k
        if (hDecoder->channelConfiguration == 0)
1039
189
            hDecoder->channelConfiguration = channels;
1040
1041
6.11k
        if (channels == 8) /* 7.1 */
1042
778
            hDecoder->channelConfiguration = 7;
1043
6.11k
        if (channels == 7) /* not a standard channelConfiguration */
1044
47
            hDecoder->channelConfiguration = 0;
1045
6.11k
    }
1046
1047
6.31k
    if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix)
1048
535
    {
1049
535
        hDecoder->downMatrix = 1;
1050
535
        output_channels = 2;
1051
5.77k
    } else {
1052
5.77k
        output_channels = channels;
1053
5.77k
    }
1054
1055
6.31k
#if (defined(PS_DEC) || defined(DRM_PS))
1056
6.31k
    hDecoder->upMatrix = 0;
1057
    /* check if we have a mono file */
1058
6.31k
    if (output_channels == 1)
1059
250
    {
1060
        /* upMatrix to 2 channels for implicit signalling of PS */
1061
250
        hDecoder->upMatrix = 1;
1062
250
        output_channels = 2;
1063
250
    }
1064
6.31k
#endif
1065
1066
    /* Make a channel configuration based on either a PCE or a channelConfiguration */
1067
6.31k
    if (!hDecoder->downMatrix && hDecoder->pce_set)
1068
215
    {
1069
        /* In some codepath program_config_element result is ignored. */
1070
215
        if (hDecoder->pce.channels > MAX_CHANNELS)
1071
6
        {
1072
6
            hInfo->error = 22;
1073
6
            return NULL;
1074
6
        }
1075
215
    }
1076
6.30k
    create_channel_config(hDecoder, hInfo);
1077
1078
    /* number of samples in this frame */
1079
6.30k
    hInfo->samples = frame_len*output_channels;
1080
    /* number of channels in this frame */
1081
6.30k
    hInfo->channels = output_channels;
1082
    /* samplerate */
1083
6.30k
    hInfo->samplerate = get_sample_rate(hDecoder->sf_index);
1084
    /* object type */
1085
6.30k
    hInfo->object_type = hDecoder->object_type;
1086
    /* sbr */
1087
6.30k
    hInfo->sbr = NO_SBR;
1088
    /* header type */
1089
6.30k
    hInfo->header_type = RAW;
1090
6.30k
    if (hDecoder->adif_header_present)
1091
0
        hInfo->header_type = ADIF;
1092
6.30k
    if (hDecoder->adts_header_present)
1093
196
        hInfo->header_type = ADTS;
1094
#if 0
1095
    if (hDecoder->latm_header_present)
1096
        hInfo->header_type = LATM;
1097
#endif
1098
6.30k
#if (defined(PS_DEC) || defined(DRM_PS))
1099
6.30k
    hInfo->ps = hDecoder->ps_used_global;
1100
6.30k
#endif
1101
1102
    /* check if frame has channel elements */
1103
6.30k
    if (channels == 0)
1104
0
    {
1105
0
        hDecoder->frame++;
1106
0
        return NULL;
1107
0
    }
1108
1109
6.30k
    {
1110
6.30k
        static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t),
1111
6.30k
            sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t),
1112
6.30k
            sizeof(int16_t), sizeof(int16_t), 0, 0, 0
1113
6.30k
        };
1114
6.30k
        uint8_t stride = str[hDecoder->config.outputFormat-1];
1115
6.30k
#ifdef SBR_DEC
1116
6.30k
        if (((hDecoder->sbr_present_flag == 1)&&(!hDecoder->downSampledSBR)) || (hDecoder->forceUpSampling == 1))
1117
3.51k
        {
1118
3.51k
            stride = 2 * stride;
1119
3.51k
        }
1120
6.30k
#endif
1121
6.30k
        required_buffer_size = frame_len*output_channels*stride;
1122
6.30k
    }
1123
1124
    /* check if we want to use internal sample_buffer */
1125
6.30k
    if (sample_buffer_size == 0)
1126
4.56k
    {
1127
        /* allocate the buffer for the final samples */
1128
4.56k
        if (hDecoder->sample_buffer_size != required_buffer_size) 
1129
3.95k
        {
1130
3.95k
            if (hDecoder->sample_buffer)
1131
59
                faad_free(hDecoder->sample_buffer);
1132
3.95k
            hDecoder->sample_buffer = NULL;
1133
3.95k
            hDecoder->sample_buffer = faad_malloc(required_buffer_size);
1134
3.95k
            hDecoder->sample_buffer_size = required_buffer_size;
1135
3.95k
        }
1136
4.56k
    } else if (sample_buffer_size < required_buffer_size) {
1137
        /* provided sample buffer is not big enough */
1138
674
        hInfo->error = 27;
1139
674
        return NULL;
1140
674
    }
1141
1142
5.63k
    if (sample_buffer_size == 0)
1143
4.56k
    {
1144
4.56k
        sample_buffer = hDecoder->sample_buffer;
1145
4.56k
    } else {
1146
1.06k
        sample_buffer = *sample_buffer2;
1147
1.06k
    }
1148
1149
5.63k
#ifdef SBR_DEC
1150
5.63k
    if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1151
3.78k
    {
1152
3.78k
        uint8_t ele;
1153
1154
        /* this data is different when SBR is used or when the data is upsampled */
1155
3.78k
        if (!hDecoder->downSampledSBR)
1156
3.04k
        {
1157
3.04k
            frame_len *= 2;
1158
3.04k
            hInfo->samples *= 2;
1159
3.04k
            hInfo->samplerate *= 2;
1160
3.04k
        }
1161
1162
        /* check if every element was provided with SBR data */
1163
20.0k
        for (ele = 0; ele < hDecoder->fr_ch_ele; ele++)
1164
16.2k
        {
1165
16.2k
            if (hDecoder->sbr[ele] == NULL)
1166
4
            {
1167
4
                hInfo->error = 25;
1168
4
                goto error;
1169
4
            }
1170
16.2k
        }
1171
1172
        /* sbr */
1173
3.77k
        if (hDecoder->sbr_present_flag == 1)
1174
2.24k
        {
1175
2.24k
            hInfo->object_type = HE_AAC;
1176
2.24k
            hInfo->sbr = SBR_UPSAMPLED;
1177
2.24k
        } else {
1178
1.53k
            hInfo->sbr = NO_SBR_UPSAMPLED;
1179
1.53k
        }
1180
3.77k
        if (hDecoder->downSampledSBR)
1181
738
        {
1182
738
            hInfo->sbr = SBR_DOWNSAMPLED;
1183
738
        }
1184
3.77k
    }
1185
5.62k
#endif
1186
1187
1188
5.62k
    sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer,
1189
5.62k
        output_channels, frame_len, hDecoder->config.outputFormat);
1190
1191
1192
#ifdef DRM
1193
    //conceal_output(hDecoder, frame_len, output_channels, sample_buffer);
1194
#endif
1195
1196
1197
5.62k
    hDecoder->postSeekResetFlag = 0;
1198
1199
5.62k
    hDecoder->frame++;
1200
5.62k
#ifdef LD_DEC
1201
5.62k
    if (hDecoder->object_type != LD)
1202
4.86k
    {
1203
4.86k
#endif
1204
4.86k
        if (hDecoder->frame <= 1)
1205
1.68k
            hInfo->samples = 0;
1206
4.86k
#ifdef LD_DEC
1207
4.86k
    } else {
1208
        /* LD encoders will give lower delay */
1209
763
        if (hDecoder->frame <= 0)
1210
0
            hInfo->samples = 0;
1211
763
    }
1212
5.62k
#endif
1213
1214
    /* cleanup */
1215
#ifdef ANALYSIS
1216
    fflush(stdout);
1217
#endif
1218
1219
#ifdef PROFILE
1220
    count = faad_get_ts() - count;
1221
    hDecoder->cycles += count;
1222
#endif
1223
1224
5.62k
    return sample_buffer;
1225
1226
13.2k
error:
1227
1228
    /* reset filterbank state */
1229
858k
    for (i = 0; i < MAX_CHANNELS; i++)
1230
845k
    {
1231
845k
        if (hDecoder->fb_intermed[i] != NULL)
1232
463k
        {
1233
463k
            memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t));
1234
463k
        }
1235
845k
    }
1236
13.2k
#ifdef SBR_DEC
1237
646k
    for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++)
1238
633k
    {
1239
633k
        if (hDecoder->sbr[i] != NULL)
1240
328k
        {
1241
328k
            sbrReset(hDecoder->sbr[i]);
1242
328k
        }
1243
633k
    }
1244
13.2k
#endif
1245
1246
1247
13.2k
    faad_endbits(&ld);
1248
1249
    /* cleanup */
1250
#ifdef ANALYSIS
1251
    fflush(stdout);
1252
#endif
1253
1254
    return NULL;
1255
5.63k
}