📄 syntax.c
字号:
/*
** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** Any non-GPL usage of this software or parts of this software is strictly
** forbidden.
**
** Commercial non-GPL licensing of this software is possible.
** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
**
** $Id: syntax.c,v 1.82 2004/09/04 14:56:29 menno Exp $
**/
/*
Reads the AAC bitstream as defined in 14496-3 (MPEG-4 Audio)
*/
#include "common.h"
#include "structs.h"
#include <stdlib.h>
#include <string.h>
#include "decoder.h"
#include "syntax.h"
#include "specrec.h"
#include "huffman.h"
#include "bits.h"
#include "pulse.h"
#include "analysis.h"
#include "drc.h"
#ifdef ERROR_RESILIENCE
#include "rvlc.h"
#endif
#ifdef SBR_DEC
#include "sbr_syntax.h"
#endif
/* static function declarations */
static void decode_sce_lfe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
uint8_t id_syn_ele);
static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
uint8_t id_syn_ele);
static uint8_t single_lfe_channel_element(NeAACDecHandle hDecoder, bitfile *ld,
uint8_t channel, uint8_t *tag);
static uint8_t channel_pair_element(NeAACDecHandle hDecoder, bitfile *ld,
uint8_t channel, uint8_t *tag);
#ifdef COUPLING_DEC
static uint8_t coupling_channel_element(NeAACDecHandle hDecoder, bitfile *ld);
#endif
static uint16_t data_stream_element(NeAACDecHandle hDecoder, bitfile *ld);
static uint8_t program_config_element(program_config *pce, bitfile *ld);
static uint8_t fill_element(NeAACDecHandle hDecoder, bitfile *ld, drc_info *drc
#ifdef SBR_DEC
,uint8_t sbr_ele
#endif
);
static uint8_t individual_channel_stream(NeAACDecHandle hDecoder,
bitfile *ld, ic_stream *ics, uint8_t scal_flag,
int16_t *spec_data);
static uint8_t ics_info(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
uint8_t common_window);
static uint8_t section_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
static uint8_t scale_factor_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld);
#ifdef SSR_DEC
static void gain_control_data(bitfile *ld, ic_stream *ics);
#endif
static uint8_t spectral_data(NeAACDecHandle hDecoder, ic_stream *ics, bitfile *ld,
int16_t *spectral_data);
static uint16_t extension_payload(bitfile *ld, drc_info *drc, uint16_t count);
static uint8_t pulse_data(ic_stream *ics, pulse_info *pul, bitfile *ld);
static void tns_data(ic_stream *ics, tns_info *tns, bitfile *ld);
#ifdef LTP_DEC
static uint8_t ltp_data(NeAACDecHandle hDecoder, ic_stream *ics, ltp_info *ltp, bitfile *ld);
#endif
static uint8_t adts_fixed_header(adts_header *adts, bitfile *ld);
static void adts_variable_header(adts_header *adts, bitfile *ld);
static void adts_error_check(adts_header *adts, bitfile *ld);
static uint8_t dynamic_range_info(bitfile *ld, drc_info *drc);
static uint8_t excluded_channels(bitfile *ld, drc_info *drc);
#ifdef SCALABLE_DEC
static int8_t aac_scalable_main_header(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
bitfile *ld, uint8_t this_layer_stereo);
#endif
/* Table 4.4.1 */
int8_t GASpecificConfig(bitfile *ld, mp4AudioSpecificConfig *mp4ASC,
program_config *pce_out)
{
program_config pce;
/* 1024 or 960 */
mp4ASC->frameLengthFlag = faad_get1bit(ld
DEBUGVAR(1,138,"GASpecificConfig(): FrameLengthFlag"));
#ifndef ALLOW_SMALL_FRAMELENGTH
if (mp4ASC->frameLengthFlag == 1)
return -3;
#endif
mp4ASC->dependsOnCoreCoder = faad_get1bit(ld
DEBUGVAR(1,139,"GASpecificConfig(): DependsOnCoreCoder"));
if (mp4ASC->dependsOnCoreCoder == 1)
{
mp4ASC->coreCoderDelay = (uint16_t)faad_getbits(ld, 14
DEBUGVAR(1,140,"GASpecificConfig(): CoreCoderDelay"));
}
mp4ASC->extensionFlag = faad_get1bit(ld DEBUGVAR(1,141,"GASpecificConfig(): ExtensionFlag"));
if (mp4ASC->channelsConfiguration == 0)
{
if (program_config_element(&pce, ld))
return -3;
//mp4ASC->channelsConfiguration = pce.channels;
if (pce_out != NULL)
memcpy(pce_out, &pce, sizeof(program_config));
/*
if (pce.num_valid_cc_elements)
return -3;
*/
}
#ifdef ERROR_RESILIENCE
if (mp4ASC->extensionFlag == 1)
{
/* Error resilience not supported yet */
if (mp4ASC->objectTypeIndex >= ER_OBJECT_START)
{
mp4ASC->aacSectionDataResilienceFlag = faad_get1bit(ld
DEBUGVAR(1,144,"GASpecificConfig(): aacSectionDataResilienceFlag"));
mp4ASC->aacScalefactorDataResilienceFlag = faad_get1bit(ld
DEBUGVAR(1,145,"GASpecificConfig(): aacScalefactorDataResilienceFlag"));
mp4ASC->aacSpectralDataResilienceFlag = faad_get1bit(ld
DEBUGVAR(1,146,"GASpecificConfig(): aacSpectralDataResilienceFlag"));
/* 1 bit: extensionFlag3 */
}
}
#endif
return 0;
}
/* Table 4.4.2 */
/* An MPEG-4 Audio decoder is only required to follow the Program
Configuration Element in GASpecificConfig(). The decoder shall ignore
any Program Configuration Elements that may occur in raw data blocks.
PCEs transmitted in raw data blocks cannot be used to convey decoder
configuration information.
*/
static uint8_t program_config_element(program_config *pce, bitfile *ld)
{
uint8_t i;
memset(pce, 0, sizeof(program_config));
pce->channels = 0;
pce->element_instance_tag = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,10,"program_config_element(): element_instance_tag"));
pce->object_type = (uint8_t)faad_getbits(ld, 2
DEBUGVAR(1,11,"program_config_element(): object_type"));
pce->sf_index = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,12,"program_config_element(): sf_index"));
pce->num_front_channel_elements = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,13,"program_config_element(): num_front_channel_elements"));
pce->num_side_channel_elements = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,14,"program_config_element(): num_side_channel_elements"));
pce->num_back_channel_elements = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,15,"program_config_element(): num_back_channel_elements"));
pce->num_lfe_channel_elements = (uint8_t)faad_getbits(ld, 2
DEBUGVAR(1,16,"program_config_element(): num_lfe_channel_elements"));
pce->num_assoc_data_elements = (uint8_t)faad_getbits(ld, 3
DEBUGVAR(1,17,"program_config_element(): num_assoc_data_elements"));
pce->num_valid_cc_elements = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,18,"program_config_element(): num_valid_cc_elements"));
pce->mono_mixdown_present = faad_get1bit(ld
DEBUGVAR(1,19,"program_config_element(): mono_mixdown_present"));
if (pce->mono_mixdown_present == 1)
{
pce->mono_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,20,"program_config_element(): mono_mixdown_element_number"));
}
pce->stereo_mixdown_present = faad_get1bit(ld
DEBUGVAR(1,21,"program_config_element(): stereo_mixdown_present"));
if (pce->stereo_mixdown_present == 1)
{
pce->stereo_mixdown_element_number = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,22,"program_config_element(): stereo_mixdown_element_number"));
}
pce->matrix_mixdown_idx_present = faad_get1bit(ld
DEBUGVAR(1,23,"program_config_element(): matrix_mixdown_idx_present"));
if (pce->matrix_mixdown_idx_present == 1)
{
pce->matrix_mixdown_idx = (uint8_t)faad_getbits(ld, 2
DEBUGVAR(1,24,"program_config_element(): matrix_mixdown_idx"));
pce->pseudo_surround_enable = faad_get1bit(ld
DEBUGVAR(1,25,"program_config_element(): pseudo_surround_enable"));
}
for (i = 0; i < pce->num_front_channel_elements; i++)
{
pce->front_element_is_cpe[i] = faad_get1bit(ld
DEBUGVAR(1,26,"program_config_element(): front_element_is_cpe"));
pce->front_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,27,"program_config_element(): front_element_tag_select"));
if (pce->front_element_is_cpe[i] & 1)
{
pce->cpe_channel[pce->front_element_tag_select[i]] = pce->channels;
pce->num_front_channels += 2;
pce->channels += 2;
} else {
pce->sce_channel[pce->front_element_tag_select[i]] = pce->channels;
pce->num_front_channels++;
pce->channels++;
}
}
for (i = 0; i < pce->num_side_channel_elements; i++)
{
pce->side_element_is_cpe[i] = faad_get1bit(ld
DEBUGVAR(1,28,"program_config_element(): side_element_is_cpe"));
pce->side_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,29,"program_config_element(): side_element_tag_select"));
if (pce->side_element_is_cpe[i] & 1)
{
pce->cpe_channel[pce->side_element_tag_select[i]] = pce->channels;
pce->num_side_channels += 2;
pce->channels += 2;
} else {
pce->sce_channel[pce->side_element_tag_select[i]] = pce->channels;
pce->num_side_channels++;
pce->channels++;
}
}
for (i = 0; i < pce->num_back_channel_elements; i++)
{
pce->back_element_is_cpe[i] = faad_get1bit(ld
DEBUGVAR(1,30,"program_config_element(): back_element_is_cpe"));
pce->back_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,31,"program_config_element(): back_element_tag_select"));
if (pce->back_element_is_cpe[i] & 1)
{
pce->cpe_channel[pce->back_element_tag_select[i]] = pce->channels;
pce->channels += 2;
pce->num_back_channels += 2;
} else {
pce->sce_channel[pce->back_element_tag_select[i]] = pce->channels;
pce->num_back_channels++;
pce->channels++;
}
}
for (i = 0; i < pce->num_lfe_channel_elements; i++)
{
pce->lfe_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,32,"program_config_element(): lfe_element_tag_select"));
pce->sce_channel[pce->lfe_element_tag_select[i]] = pce->channels;
pce->num_lfe_channels++;
pce->channels++;
}
for (i = 0; i < pce->num_assoc_data_elements; i++)
pce->assoc_data_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,33,"program_config_element(): assoc_data_element_tag_select"));
for (i = 0; i < pce->num_valid_cc_elements; i++)
{
pce->cc_element_is_ind_sw[i] = faad_get1bit(ld
DEBUGVAR(1,34,"program_config_element(): cc_element_is_ind_sw"));
pce->valid_cc_element_tag_select[i] = (uint8_t)faad_getbits(ld, 4
DEBUGVAR(1,35,"program_config_element(): valid_cc_element_tag_select"));
}
faad_byte_align(ld);
pce->comment_field_bytes = (uint8_t)faad_getbits(ld, 8
DEBUGVAR(1,36,"program_config_element(): comment_field_bytes"));
for (i = 0; i < pce->comment_field_bytes; i++)
{
pce->comment_field_data[i] = (uint8_t)faad_getbits(ld, 8
DEBUGVAR(1,37,"program_config_element(): comment_field_data"));
}
pce->comment_field_data[i] = 0;
if (pce->channels > MAX_CHANNELS)
return 22;
return 0;
}
static void decode_sce_lfe(NeAACDecHandle hDecoder,
NeAACDecFrameInfo *hInfo, bitfile *ld,
uint8_t id_syn_ele)
{
uint8_t channels = hDecoder->fr_channels;
uint8_t tag = 0;
if (channels+1 > MAX_CHANNELS)
{
hInfo->error = 12;
return;
}
if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
{
hInfo->error = 13;
return;
}
/* for SCE hDecoder->element_output_channels[] is not set here because this
can become 2 when some form of Parametric Stereo coding is used
*/
/* save the syntax element id */
hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
/* decode the element */
hInfo->error = single_lfe_channel_element(hDecoder, ld, channels, &tag);
/* map output channels position to internal data channels */
if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 2)
{
/* this might be faulty when pce_set is true */
hDecoder->internal_channel[channels] = channels;
hDecoder->internal_channel[channels+1] = channels+1;
} else {
if (hDecoder->pce_set)
hDecoder->internal_channel[hDecoder->pce.sce_channel[tag]] = channels;
else
hDecoder->internal_channel[channels] = channels;
}
hDecoder->fr_channels += hDecoder->element_output_channels[hDecoder->fr_ch_ele];
hDecoder->fr_ch_ele++;
}
static void decode_cpe(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, bitfile *ld,
uint8_t id_syn_ele)
{
uint8_t channels = hDecoder->fr_channels;
uint8_t tag = 0;
if (channels+2 > MAX_CHANNELS)
{
hInfo->error = 12;
return;
}
if (hDecoder->fr_ch_ele+1 > MAX_SYNTAX_ELEMENTS)
{
hInfo->error = 13;
return;
}
/* for CPE the number of output channels is always 2 */
if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
{
/* element_output_channels not set yet */
hDecoder->element_output_channels[hDecoder->fr_ch_ele] = 2;
} else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != 2) {
/* element inconsistency */
hInfo->error = 21;
return;
}
/* save the syntax element id */
hDecoder->element_id[hDecoder->fr_ch_ele] = id_syn_ele;
/* decode the element */
hInfo->error = channel_pair_element(hDecoder, ld, channels, &tag);
/* map output channel position to internal data channels */
if (hDecoder->pce_set)
{
hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]] = channels;
hDecoder->internal_channel[hDecoder->pce.cpe_channel[tag]+1] = channels+1;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -