Coverage Report

Created: 2025-11-24 06:22

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