Coverage Report

Created: 2025-11-16 06:18

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