Coverage Report

Created: 2026-04-01 06:58

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