Coverage Report

Created: 2026-10-17 06:34

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