📄 mpadecl2.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "statname.h"
#include "hlxclib/string.h"
#include "hlxclib/math.h"
#include "mhead.h"
#include "mpadecl2.h"
#include "mpalow.h" // low level extern C prototypes
///////////////////////////////////////////////////////////////////////////////
// Static Data:
///////////////////////////////////////////////////////////////////////////////
static const int look_joint_L2[16] = { /* lookup stereo sb's by mode+ext */
64, 64, 64, 64, /* stereo */
2*4, 2*8, 2*12, 2*16, /* joint */
64, 64, 64, 64, /* dual */
32, 32, 32, 32, /* mono */
};
static const int bat_bit_master_L2[] = {
0, 5, 7, 9, 10, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48 };
//
// -------- init data -----------------
//
static const int steps[18] = {
0, 3, 5, 7, 9, 15, 31, 63, 127,
255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535 };
/* ABCD_INDEX = lookqt[mode][sr_index][br_index] */
/* -1 = invalid */
static const signed char lookqt[4][3][16]={ /* Flawfinder: ignore */
1,-1,-1,-1,2,-1,2,0,0,0,1,1,1,1,1,-1, /* 44ks stereo */
0,-1,-1,-1,2,-1,2,0,0,0,0,0,0,0,0,-1, /* 48ks */
1,-1,-1,-1,3,-1,3,0,0,0,1,1,1,1,1,-1, /* 32ks */
1,-1,-1,-1,2,-1,2,0,0,0,1,1,1,1,1,-1, /* 44ks joint stereo */
0,-1,-1,-1,2,-1,2,0,0,0,0,0,0,0,0,-1, /* 48ks */
1,-1,-1,-1,3,-1,3,0,0,0,1,1,1,1,1,-1, /* 32ks */
1,-1,-1,-1,2,-1,2,0,0,0,1,1,1,1,1,-1, /* 44ks dual chan */
0,-1,-1,-1,2,-1,2,0,0,0,0,0,0,0,0,-1, /* 48ks */
1,-1,-1,-1,3,-1,3,0,0,0,1,1,1,1,1,-1, /* 32ks */
// mono extended beyond legal br index
// 1,2,2,0,0,0,1,1,1,1,1,1,1,1,1,-1, /* 44ks single chan */
// 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,-1, /* 48ks */
// 1,3,3,0,0,0,1,1,1,1,1,1,1,1,1,-1, /* 32ks */
// legal mono
1,2,2,0,0,0,1,1,1,1,1,-1,-1,-1,-1,-1, /* 44ks single chan */
0,2,2,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1, /* 48ks */
1,3,3,0,0,0,1,1,1,1,1,-1,-1,-1,-1,-1, /* 32ks */
};
static const int sr_table_L2[8] =
{ 22050, 24000, 16000, 1,
44100, 48000, 32000, 1 };
/* bit allocation table look up */
/* table per mpeg spec tables 3b2a/b/c/d /e is mpeg2 */
/* look_bat[abcd_index][4][16] */
static const unsigned char look_bat[5][4][16] = { /* Flawfinder: ignore */
/* LOOK_BATA */
0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17,
0, 1, 2, 3, 4, 5, 6, 17, 0,0,0,0, 0,0,0,0,
0, 1, 2, 17, 0,0,0,0, 0,0,0,0, 0,0,0,0,
/* LOOK_BATB */
0, 1, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17,
0, 1, 2, 3, 4, 5, 6, 17, 0,0,0,0, 0,0,0,0,
0, 1, 2, 17, 0,0,0,0, 0,0,0,0, 0,0,0,0,
/* LOOK_BATC */
0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0, 1, 2, 4, 5, 6, 7, 8, 0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
/* LOOK_BATD */
0, 1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0, 1, 2, 4, 5, 6, 7, 8, 0,0,0,0, 0,0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
/* LOOK_BATE */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
0, 1, 2, 4, 5, 6, 7, 8, 0,0,0,0, 0,0,0,0,
0, 1, 2, 4, 0,0,0,0, 0,0,0,0, 0,0,0,0,
};
/* look_nbat[abcd_index]][4] */
static const unsigned char look_nbat[5][4] = { /* Flawfinder: ignore */
3, 8, 12, 4,
3, 8, 12, 7,
2, 0, 6, 0,
2, 0, 10, 0,
4, 0, 7, 19,
};
static const int out_chans_L2[5] = { 1, 2, 1, 1, 1 };
static const SBT_FUNCTION sbt_table_L2[2][3][5] = {
// 16 bit (usually short) pcm output
sbt_mono,
sbt_dual,
sbt_dual_mono,
#ifdef REDUCTION
sbt_dual_left,
sbt_dual_right,
sbt16_mono,
sbt16_dual,
sbt16_dual_mono,
sbt16_dual_left,
sbt16_dual_right,
sbt8_mono,
sbt8_dual,
sbt8_dual_mono,
sbt8_dual_left,
sbt8_dual_right,
#else // no reduction
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
#endif
// 8 bit pcm output
#ifdef EIGHT_BIT
sbtB_mono,
sbtB_dual,
sbtB_dual_mono,
#ifdef REDUCTION
sbtB_dual_left,
sbtB_dual_right,
sbtB16_mono,
sbtB16_dual,
sbtB16_dual_mono,
sbtB16_dual_left,
sbtB16_dual_right,
sbtB8_mono,
sbtB8_dual,
sbtB8_dual_mono,
sbtB8_dual_left,
sbtB8_dual_right,
#else // no reduction
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
#endif
#else // no eight_bit
NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL,
#endif
};
///////////////////////////////////////////////////////////////////////////////
// Public Functions
///////////////////////////////////////////////////////////////////////////////
CMpaDecoderL2::CMpaDecoderL2()
: CMpaDecoder()
{
table_init();
}
CMpaDecoderL2::~CMpaDecoderL2()
{
}
#ifdef REFORMAT
IN_OUT CMpaDecoderL2::audio_decode_reformat(unsigned char *bs,
unsigned char *bs_out)
{
IN_OUT x;
x.in_bytes = 0; // signal fail, not relevant for Layer 2
x.out_bytes = 0;
return x;
}
#endif
//================================================================
int CMpaDecoderL2::audio_decode_init(MPEG_HEAD *h,
int framebytes_arg,
int reduction_code,
int transform_code,
int convert_code,
int freq_limit,
int conceal_enable)
{
int i, j, k;
int abcd_index;
long samprate;
int limit;
int bit_code;
/* check if code handles */
if( h->option != 2 ) return 0; /* layer II only */
#ifndef REDUCTION
reduction_code = 0;
#endif
#ifndef EIGHT_BIT
convert_code = 0;
#endif
m_bMpeg1 = h->id;
m_nSampsPerFrame = 1152;
transform_code = transform_code; /* not used, asm compatability */
bit_code = 0;
if( convert_code & 8 ) bit_code = 1;
convert_code = convert_code & 3; /* higher bits used by dec8 freq cvt */
if( reduction_code < 0 ) reduction_code = 0;
if( reduction_code > 2 ) reduction_code = 2;
if( freq_limit < 1000 ) freq_limit = 1000;
framebytes = framebytes_arg;
/* check if code handles */
if( h->sr_index == 3 ) return 0; /* reserved */
/* compute abcd index for bit allo table selection */
if( h->id ) /* mpeg 1 */
abcd_index = lookqt[h->mode][h->sr_index][h->br_index];
else abcd_index = 4; /* mpeg 2 */
if( abcd_index < 0 ) return 0; // fail invalid Layer II bit rate index
for(i=0;i<4;i++) {
for(j=0;j<16;j++) bat[i][j] = look_bat[abcd_index][i][j];
}
for(i=0;i<4;i++) nbat[i] = look_nbat[abcd_index][i];
max_sb = nbat[0] + nbat[1] + nbat[2] + nbat[3];
/*----- compute nsb_limit --------*/
samprate = sr_table_L2[4*h->id + h->sr_index];
nsb_limit = (freq_limit*64L + samprate/2)/samprate; /*- caller limit -*/
/*---- limit = 0.94*(32>>reduction_code); ----*/
limit = (32>>reduction_code);
if( limit > 8 ) limit--;
if( nsb_limit > limit ) nsb_limit = limit;
if( nsb_limit > max_sb ) nsb_limit = max_sb;
outvalues = 1152 >> reduction_code;
if( h->mode != 3 ) { /* adjust for 2 channel modes */
for(i=0;i<4;i++) nbat[i] *= 2;
max_sb *= 2;
nsb_limit *= 2;
}
/* set sbt function */
k = 1 + convert_code;
if( h->mode == 3 ) {
k = 0;
}
sbt = sbt_table_L2[bit_code][reduction_code][k];
outvalues *= out_chans_L2[k];
if( bit_code ) outbytes = outvalues;
else outbytes = sizeof(short)*outvalues;
decinfo.channels = out_chans_L2[k];
decinfo.outvalues = outvalues;
decinfo.samprate = samprate >> reduction_code;
if( bit_code ) decinfo.bits = 8;
else decinfo.bits = sizeof(short)*8;
decinfo.framebytes = framebytes;
decinfo.type = 0;
/* clear sample buffer, unused sub bands must be 0 */
for(i=0;i<2304;i++) sample[i] = 0.0F;
return 1;
}
/*---------------------------------------------------------*/
IN_OUT CMpaDecoderL2::audio_decode(unsigned char *bs,
unsigned char *pcm,
int size)
{
int sync, prot;
IN_OUT in_out;
load_init(bs); /* initialize bit getter */
/* test sync */
in_out.in_bytes = 0; /* assume fail */
in_out.out_bytes = 0;
sync = load(12);
if( sync != 0xFFF ) return in_out; /* sync fail */
load(3); /* skip id and option (checked by init) */
prot = load(1); /* load prot bit */
load(6); /* skip to pad */
pad = load(1);
load(1); /* skip to mode */
stereo_sb = look_joint_L2[load(4)];
if( prot ) load(4); /* skip to data */
else load(20); /* skip crc */
unpack_ba(); /* unpack bit allocation */
unpack_sfs(); /* unpack scale factor selectors */
unpack_sf(); /* unpack scale factor */
unpack_samp(); /* unpack samples */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -