Coverage Report

Created: 2026-07-24 06:21

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