Coverage Report

Created: 2025-11-09 06:08

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