Coverage Report

Created: 2025-12-14 06:24

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