Coverage Report

Created: 2026-02-26 06:56

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