* encoding of AC3 with more than 2 channels
by Takashi Iwai <tiwai@suse.de> Originally committed as revision 383 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
parent
10bb7023a2
commit
30b68f33aa
@ -201,6 +201,9 @@ AVFormat ac3_format = {
|
|||||||
raw_write_header,
|
raw_write_header,
|
||||||
raw_write_packet,
|
raw_write_packet,
|
||||||
raw_write_trailer,
|
raw_write_trailer,
|
||||||
|
raw_read_header,
|
||||||
|
raw_read_packet,
|
||||||
|
raw_read_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
AVFormat h263_format = {
|
AVFormat h263_format = {
|
||||||
|
@ -26,6 +26,7 @@ typedef struct AC3DecodeState {
|
|||||||
UINT8 *inbuf_ptr;
|
UINT8 *inbuf_ptr;
|
||||||
int frame_size;
|
int frame_size;
|
||||||
int flags;
|
int flags;
|
||||||
|
int channels;
|
||||||
ac3_state_t state;
|
ac3_state_t state;
|
||||||
} AC3DecodeState;
|
} AC3DecodeState;
|
||||||
|
|
||||||
@ -52,24 +53,16 @@ static inline int blah (int32_t i)
|
|||||||
return i - 0x43c00000;
|
return i - 0x43c00000;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void float_to_int (float * _f, INT16 * s16)
|
static inline void float_to_int (float * _f, INT16 * s16, int nchannels)
|
||||||
{
|
{
|
||||||
int i;
|
int i, j, c;
|
||||||
int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
|
int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
nchannels *= 256;
|
||||||
for (i = 0; i < 256; i++) {
|
for (i = 0; i < 256; i++) {
|
||||||
s16[2*i] = blah (f[i]);
|
for (c = 0; c < nchannels; c += 256)
|
||||||
s16[2*i+1] = blah (f[i+256]);
|
s16[j++] = blah (f[i + c]);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void float_to_int_mono (float * _f, INT16 * s16)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int32_t * f = (int32_t *) _f; // XXX assumes IEEE float format
|
|
||||||
|
|
||||||
for (i = 0; i < 256; i++) {
|
|
||||||
s16[i] = blah (f[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +80,9 @@ static int ac3_decode_frame(AVCodecContext *avctx,
|
|||||||
int sample_rate, bit_rate;
|
int sample_rate, bit_rate;
|
||||||
short *out_samples = data;
|
short *out_samples = data;
|
||||||
float level;
|
float level;
|
||||||
|
static int ac3_channels[8] = {
|
||||||
|
2, 1, 2, 3, 3, 4, 4, 5
|
||||||
|
};
|
||||||
|
|
||||||
*data_size = 0;
|
*data_size = 0;
|
||||||
buf_ptr = buf;
|
buf_ptr = buf;
|
||||||
@ -111,10 +107,13 @@ static int ac3_decode_frame(AVCodecContext *avctx,
|
|||||||
s->frame_size = len;
|
s->frame_size = len;
|
||||||
/* update codec info */
|
/* update codec info */
|
||||||
avctx->sample_rate = sample_rate;
|
avctx->sample_rate = sample_rate;
|
||||||
if ((s->flags & AC3_CHANNEL_MASK) == AC3_MONO)
|
s->channels = ac3_channels[s->flags & 7];
|
||||||
avctx->channels = 1;
|
if (s->flags & AC3_LFE)
|
||||||
else
|
s->channels++;
|
||||||
avctx->channels = 2;
|
if (s->channels < avctx->channels) {
|
||||||
|
fprintf(stderr, "Source channels are less than specified: output to %d channels..\n", s->channels);
|
||||||
|
avctx->channels = s->channels;
|
||||||
|
}
|
||||||
avctx->bit_rate = bit_rate;
|
avctx->bit_rate = bit_rate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,11 +127,14 @@ static int ac3_decode_frame(AVCodecContext *avctx,
|
|||||||
s->inbuf_ptr += len;
|
s->inbuf_ptr += len;
|
||||||
buf_size -= len;
|
buf_size -= len;
|
||||||
} else {
|
} else {
|
||||||
|
#if 0
|
||||||
if (avctx->channels == 1)
|
if (avctx->channels == 1)
|
||||||
flags = AC3_MONO;
|
flags = AC3_MONO;
|
||||||
else
|
else
|
||||||
flags = AC3_STEREO;
|
flags = AC3_STEREO;
|
||||||
|
#else
|
||||||
|
flags = s->flags;
|
||||||
|
#endif
|
||||||
flags |= AC3_ADJUST_LEVEL;
|
flags |= AC3_ADJUST_LEVEL;
|
||||||
level = 1;
|
level = 1;
|
||||||
if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
|
if (ac3_frame (&s->state, s->inbuf, &flags, &level, 384)) {
|
||||||
@ -144,10 +146,7 @@ static int ac3_decode_frame(AVCodecContext *avctx,
|
|||||||
for (i = 0; i < 6; i++) {
|
for (i = 0; i < 6; i++) {
|
||||||
if (ac3_block (&s->state))
|
if (ac3_block (&s->state))
|
||||||
goto fail;
|
goto fail;
|
||||||
if (avctx->channels == 1)
|
float_to_int (*samples, out_samples + i * 256 * avctx->channels, avctx->channels);
|
||||||
float_to_int_mono (*samples, out_samples + i * 256);
|
|
||||||
else
|
|
||||||
float_to_int (*samples, out_samples + i * 512);
|
|
||||||
}
|
}
|
||||||
s->inbuf_ptr = s->inbuf;
|
s->inbuf_ptr = s->inbuf;
|
||||||
s->frame_size = 0;
|
s->frame_size = 0;
|
||||||
|
@ -93,7 +93,7 @@ static inline int calc_lowcomp(int a, int b0, int b1, int bin)
|
|||||||
assumptions. */
|
assumptions. */
|
||||||
void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
|
void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
|
||||||
INT8 *exp, int start, int end,
|
INT8 *exp, int start, int end,
|
||||||
int snroffset, int fgain)
|
int snroffset, int fgain, int is_lfe)
|
||||||
{
|
{
|
||||||
int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
|
int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
|
||||||
int fastleak,slowleak,address,tmp;
|
int fastleak,slowleak,address,tmp;
|
||||||
@ -146,21 +146,25 @@ void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
|
|||||||
excite[1] = bndpsd[1] - fgain - lowcomp ;
|
excite[1] = bndpsd[1] - fgain - lowcomp ;
|
||||||
begin = 7 ;
|
begin = 7 ;
|
||||||
for (bin = 2; bin < 7; bin++) {
|
for (bin = 2; bin < 7; bin++) {
|
||||||
lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
|
if (!(is_lfe && bin == 6))
|
||||||
|
lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
|
||||||
fastleak = bndpsd[bin] - fgain ;
|
fastleak = bndpsd[bin] - fgain ;
|
||||||
slowleak = bndpsd[bin] - s->sgain ;
|
slowleak = bndpsd[bin] - s->sgain ;
|
||||||
excite[bin] = fastleak - lowcomp ;
|
excite[bin] = fastleak - lowcomp ;
|
||||||
if (bndpsd[bin] <= bndpsd[bin+1]) {
|
if (!(is_lfe && bin == 6)) {
|
||||||
begin = bin + 1 ;
|
if (bndpsd[bin] <= bndpsd[bin+1]) {
|
||||||
break ;
|
begin = bin + 1 ;
|
||||||
}
|
break ;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end1=bndend;
|
end1=bndend;
|
||||||
if (end1 > 22) end1=22;
|
if (end1 > 22) end1=22;
|
||||||
|
|
||||||
for (bin = begin; bin < end1; bin++) {
|
for (bin = begin; bin < end1; bin++) {
|
||||||
lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
|
if (!(is_lfe && bin == 6))
|
||||||
|
lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
|
||||||
|
|
||||||
fastleak -= s->fdecay ;
|
fastleak -= s->fdecay ;
|
||||||
v = bndpsd[bin] - fgain;
|
v = bndpsd[bin] - fgain;
|
||||||
@ -395,7 +399,7 @@ static int calc_exp_diff(UINT8 *exp1, UINT8 *exp2, int n)
|
|||||||
|
|
||||||
static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
|
static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],
|
||||||
UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
|
UINT8 exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
|
||||||
int ch)
|
int ch, int is_lfe)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
int exp_diff;
|
int exp_diff;
|
||||||
@ -413,6 +417,9 @@ static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]
|
|||||||
else
|
else
|
||||||
exp_strategy[i][ch] = EXP_REUSE;
|
exp_strategy[i][ch] = EXP_REUSE;
|
||||||
}
|
}
|
||||||
|
if (is_lfe)
|
||||||
|
return;
|
||||||
|
|
||||||
/* now select the encoding strategy type : if exponents are often
|
/* now select the encoding strategy type : if exponents are often
|
||||||
recoded, we use a coarse encoding */
|
recoded, we use a coarse encoding */
|
||||||
i = 0;
|
i = 0;
|
||||||
@ -432,7 +439,7 @@ static void compute_exp_strategy(UINT8 exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS]
|
|||||||
exp_strategy[i][ch] = EXP_D15;
|
exp_strategy[i][ch] = EXP_D15;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
i = j;
|
i = j;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -593,12 +600,13 @@ static int bit_alloc(AC3EncodeContext *s,
|
|||||||
s->mant1_cnt = 0;
|
s->mant1_cnt = 0;
|
||||||
s->mant2_cnt = 0;
|
s->mant2_cnt = 0;
|
||||||
s->mant4_cnt = 0;
|
s->mant4_cnt = 0;
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_all_channels;ch++) {
|
||||||
parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch],
|
parametric_bit_allocation(s, bap[i][ch], (INT8 *)encoded_exp[i][ch],
|
||||||
0, s->nb_coefs[ch],
|
0, s->nb_coefs[ch],
|
||||||
(((csnroffst-15) << 4) +
|
(((csnroffst-15) << 4) +
|
||||||
fsnroffst) << 2,
|
fsnroffst) << 2,
|
||||||
fgaintab[s->fgaincod[ch]]);
|
fgaintab[s->fgaincod[ch]],
|
||||||
|
ch == s->lfe_channel);
|
||||||
frame_bits += compute_mantissa_size(s, bap[i][ch],
|
frame_bits += compute_mantissa_size(s, bap[i][ch],
|
||||||
s->nb_coefs[ch]);
|
s->nb_coefs[ch]);
|
||||||
}
|
}
|
||||||
@ -622,6 +630,7 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
int i, ch;
|
int i, ch;
|
||||||
int csnroffst, fsnroffst;
|
int csnroffst, fsnroffst;
|
||||||
UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
|
UINT8 bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];
|
||||||
|
static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };
|
||||||
|
|
||||||
/* init default parameters */
|
/* init default parameters */
|
||||||
s->sdecaycod = 2;
|
s->sdecaycod = 2;
|
||||||
@ -629,7 +638,7 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
s->sgaincod = 1;
|
s->sgaincod = 1;
|
||||||
s->dbkneecod = 2;
|
s->dbkneecod = 2;
|
||||||
s->floorcod = 4;
|
s->floorcod = 4;
|
||||||
for(ch=0;ch<s->nb_channels;ch++)
|
for(ch=0;ch<s->nb_all_channels;ch++)
|
||||||
s->fgaincod[ch] = 4;
|
s->fgaincod[ch] = 4;
|
||||||
|
|
||||||
/* compute real values */
|
/* compute real values */
|
||||||
@ -641,18 +650,21 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
|
|
||||||
/* header size */
|
/* header size */
|
||||||
frame_bits += 65;
|
frame_bits += 65;
|
||||||
if (s->acmod == 2)
|
// if (s->acmod == 2)
|
||||||
frame_bits += 2;
|
// frame_bits += 2;
|
||||||
|
frame_bits += frame_bits_inc[s->acmod];
|
||||||
|
|
||||||
/* audio blocks */
|
/* audio blocks */
|
||||||
for(i=0;i<NB_BLOCKS;i++) {
|
for(i=0;i<NB_BLOCKS;i++) {
|
||||||
frame_bits += s->nb_channels * 2 + 2;
|
frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */
|
||||||
if (s->acmod == 2)
|
if (s->acmod == 2)
|
||||||
frame_bits++;
|
frame_bits++; /* rematstr */
|
||||||
frame_bits += 2 * s->nb_channels;
|
frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */
|
||||||
|
if (s->lfe)
|
||||||
|
frame_bits++; /* lfeexpstr */
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_channels;ch++) {
|
||||||
if (exp_strategy[i][ch] != EXP_REUSE)
|
if (exp_strategy[i][ch] != EXP_REUSE)
|
||||||
frame_bits += 6 + 2;
|
frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */
|
||||||
}
|
}
|
||||||
frame_bits++; /* baie */
|
frame_bits++; /* baie */
|
||||||
frame_bits++; /* snr */
|
frame_bits++; /* snr */
|
||||||
@ -660,7 +672,10 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
}
|
}
|
||||||
frame_bits++; /* cplinu for block 0 */
|
frame_bits++; /* cplinu for block 0 */
|
||||||
/* bit alloc info */
|
/* bit alloc info */
|
||||||
frame_bits += 2*4 + 3 + 6 + s->nb_channels * (4 + 3);
|
/* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */
|
||||||
|
/* csnroffset[6] */
|
||||||
|
/* (fsnoffset[4] + fgaincod[4]) * c */
|
||||||
|
frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);
|
||||||
|
|
||||||
/* CRC */
|
/* CRC */
|
||||||
frame_bits += 16;
|
frame_bits += 16;
|
||||||
@ -670,11 +685,11 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
|
|
||||||
csnroffst = s->csnroffst;
|
csnroffst = s->csnroffst;
|
||||||
while (csnroffst >= 0 &&
|
while (csnroffst >= 0 &&
|
||||||
bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
|
bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)
|
||||||
csnroffst -= SNR_INC1;
|
csnroffst -= SNR_INC1;
|
||||||
if (csnroffst < 0) {
|
if (csnroffst < 0) {
|
||||||
fprintf(stderr, "Error !!!\n");
|
fprintf(stderr, "Yack, Error !!!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
while ((csnroffst + SNR_INC1) <= 63 &&
|
while ((csnroffst + SNR_INC1) <= 63 &&
|
||||||
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
|
bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits,
|
||||||
@ -703,14 +718,14 @@ static int compute_bit_allocation(AC3EncodeContext *s,
|
|||||||
}
|
}
|
||||||
|
|
||||||
s->csnroffst = csnroffst;
|
s->csnroffst = csnroffst;
|
||||||
for(ch=0;ch<s->nb_channels;ch++)
|
for(ch=0;ch<s->nb_all_channels;ch++)
|
||||||
s->fsnroffst[ch] = fsnroffst;
|
s->fsnroffst[ch] = fsnroffst;
|
||||||
#if defined(DEBUG_BITALLOC)
|
#if defined(DEBUG_BITALLOC)
|
||||||
{
|
{
|
||||||
int j;
|
int j;
|
||||||
|
|
||||||
for(i=0;i<6;i++) {
|
for(i=0;i<6;i++) {
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_all_channels;ch++) {
|
||||||
printf("Block #%d Ch%d:\n", i, ch);
|
printf("Block #%d Ch%d:\n", i, ch);
|
||||||
printf("bap=");
|
printf("bap=");
|
||||||
for(j=0;j<s->nb_coefs[ch];j++) {
|
for(j=0;j<s->nb_coefs[ch];j++) {
|
||||||
@ -733,18 +748,26 @@ static int AC3_encode_init(AVCodecContext *avctx)
|
|||||||
int i, j, k, l, ch, v;
|
int i, j, k, l, ch, v;
|
||||||
float alpha;
|
float alpha;
|
||||||
static unsigned short freqs[3] = { 48000, 44100, 32000 };
|
static unsigned short freqs[3] = { 48000, 44100, 32000 };
|
||||||
|
static int acmod_defs[6] = {
|
||||||
|
0x01, /* C */
|
||||||
|
0x02, /* L R */
|
||||||
|
0x03, /* L C R */
|
||||||
|
0x06, /* L R SL SR */
|
||||||
|
0x07, /* L C R SL SR */
|
||||||
|
0x07, /* L C R SL SR (+LFE) */
|
||||||
|
};
|
||||||
|
|
||||||
avctx->frame_size = AC3_FRAME_SIZE;
|
avctx->frame_size = AC3_FRAME_SIZE;
|
||||||
avctx->key_frame = 1; /* always key frame */
|
avctx->key_frame = 1; /* always key frame */
|
||||||
|
|
||||||
/* number of channels */
|
/* number of channels */
|
||||||
if (channels == 1)
|
if (channels < 1 || channels > 6)
|
||||||
s->acmod = 1;
|
return -1;
|
||||||
else if (channels == 2)
|
s->acmod = acmod_defs[channels - 1];
|
||||||
s->acmod = 2;
|
s->lfe = (channels == 6) ? 1 : 0;
|
||||||
else
|
s->nb_all_channels = channels;
|
||||||
return -1;
|
s->nb_channels = channels > 5 ? 5 : channels;
|
||||||
s->nb_channels = channels;
|
s->lfe_channel = s->lfe ? 5 : -1;
|
||||||
|
|
||||||
/* frequency */
|
/* frequency */
|
||||||
for(i=0;i<3;i++) {
|
for(i=0;i<3;i++) {
|
||||||
@ -782,6 +805,9 @@ static int AC3_encode_init(AVCodecContext *avctx)
|
|||||||
s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
|
s->chbwcod[ch] = 50; /* sample bandwidth as mpeg audio layer 2 table 0 */
|
||||||
s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
|
s->nb_coefs[ch] = ((s->chbwcod[ch] + 12) * 3) + 37;
|
||||||
}
|
}
|
||||||
|
if (s->lfe) {
|
||||||
|
s->nb_coefs[s->lfe_channel] = 7; /* fixed */
|
||||||
|
}
|
||||||
/* initial snr offset */
|
/* initial snr offset */
|
||||||
s->csnroffst = 40;
|
s->csnroffst = 40;
|
||||||
|
|
||||||
@ -821,10 +847,13 @@ static void output_frame_header(AC3EncodeContext *s, unsigned char *frame)
|
|||||||
put_bits(&s->pb, 5, s->bsid);
|
put_bits(&s->pb, 5, s->bsid);
|
||||||
put_bits(&s->pb, 3, s->bsmod);
|
put_bits(&s->pb, 3, s->bsmod);
|
||||||
put_bits(&s->pb, 3, s->acmod);
|
put_bits(&s->pb, 3, s->acmod);
|
||||||
if (s->acmod == 2) {
|
if ((s->acmod & 0x01) && s->acmod != 0x01)
|
||||||
|
put_bits(&s->pb, 2, 1); /* XXX -4.5 dB */
|
||||||
|
if (s->acmod & 0x04)
|
||||||
|
put_bits(&s->pb, 2, 1); /* XXX -6 dB */
|
||||||
|
if (s->acmod == 0x02)
|
||||||
put_bits(&s->pb, 2, 0); /* surround not indicated */
|
put_bits(&s->pb, 2, 0); /* surround not indicated */
|
||||||
}
|
put_bits(&s->pb, 1, s->lfe); /* LFE */
|
||||||
put_bits(&s->pb, 1, 0); /* no LFE */
|
|
||||||
put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
|
put_bits(&s->pb, 5, 31); /* dialog norm: -31 db */
|
||||||
put_bits(&s->pb, 1, 0); /* no compression control word */
|
put_bits(&s->pb, 1, 0); /* no compression control word */
|
||||||
put_bits(&s->pb, 1, 0); /* no lang code */
|
put_bits(&s->pb, 1, 0); /* no lang code */
|
||||||
@ -920,13 +949,17 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
put_bits(&s->pb, 2, exp_strategy[ch]);
|
put_bits(&s->pb, 2, exp_strategy[ch]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (s->lfe) {
|
||||||
|
put_bits(&s->pb, 1, exp_strategy[s->lfe_channel]);
|
||||||
|
}
|
||||||
|
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_channels;ch++) {
|
||||||
if (exp_strategy[ch] != EXP_REUSE)
|
if (exp_strategy[ch] != EXP_REUSE)
|
||||||
put_bits(&s->pb, 6, s->chbwcod[ch]);
|
put_bits(&s->pb, 6, s->chbwcod[ch]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* exponents */
|
/* exponents */
|
||||||
for (ch = 0; ch < s->nb_channels; ch++) {
|
for (ch = 0; ch < s->nb_all_channels; ch++) {
|
||||||
switch(exp_strategy[ch]) {
|
switch(exp_strategy[ch]) {
|
||||||
case EXP_REUSE:
|
case EXP_REUSE:
|
||||||
continue;
|
continue;
|
||||||
@ -941,7 +974,7 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
group_size = 4;
|
group_size = 4;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
|
nb_groups = (s->nb_coefs[ch] + (group_size * 3) - 4) / (3 * group_size);
|
||||||
p = encoded_exp[ch];
|
p = encoded_exp[ch];
|
||||||
|
|
||||||
/* first exponent */
|
/* first exponent */
|
||||||
@ -969,7 +1002,8 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
|
put_bits(&s->pb, 7, ((delta0 * 5 + delta1) * 5) + delta2);
|
||||||
}
|
}
|
||||||
|
|
||||||
put_bits(&s->pb, 2, 0); /* no gain range info */
|
if (ch != s->lfe_channel)
|
||||||
|
put_bits(&s->pb, 2, 0); /* no gain range info */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bit allocation info */
|
/* bit allocation info */
|
||||||
@ -987,7 +1021,7 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
put_bits(&s->pb, 1, baie); /* always present with bai */
|
put_bits(&s->pb, 1, baie); /* always present with bai */
|
||||||
if (baie) {
|
if (baie) {
|
||||||
put_bits(&s->pb, 6, s->csnroffst);
|
put_bits(&s->pb, 6, s->csnroffst);
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_all_channels;ch++) {
|
||||||
put_bits(&s->pb, 4, s->fsnroffst[ch]);
|
put_bits(&s->pb, 4, s->fsnroffst[ch]);
|
||||||
put_bits(&s->pb, 3, s->fgaincod[ch]);
|
put_bits(&s->pb, 3, s->fgaincod[ch]);
|
||||||
}
|
}
|
||||||
@ -1004,7 +1038,7 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
mant1_cnt = mant2_cnt = mant4_cnt = 0;
|
mant1_cnt = mant2_cnt = mant4_cnt = 0;
|
||||||
qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
|
qmant1_ptr = qmant2_ptr = qmant4_ptr = NULL;
|
||||||
|
|
||||||
for (ch = 0; ch < s->nb_channels; ch++) {
|
for (ch = 0; ch < s->nb_all_channels; ch++) {
|
||||||
int b, c, e, v;
|
int b, c, e, v;
|
||||||
|
|
||||||
for(i=0;i<s->nb_coefs[ch];i++) {
|
for(i=0;i<s->nb_coefs[ch];i++) {
|
||||||
@ -1091,7 +1125,7 @@ static void output_audio_block(AC3EncodeContext *s,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* second pass : output the values */
|
/* second pass : output the values */
|
||||||
for (ch = 0; ch < s->nb_channels; ch++) {
|
for (ch = 0; ch < s->nb_all_channels; ch++) {
|
||||||
int b, q;
|
int b, q;
|
||||||
|
|
||||||
for(i=0;i<s->nb_coefs[ch];i++) {
|
for(i=0;i<s->nb_coefs[ch];i++) {
|
||||||
@ -1265,7 +1299,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
|
|||||||
int frame_bits;
|
int frame_bits;
|
||||||
|
|
||||||
frame_bits = 0;
|
frame_bits = 0;
|
||||||
for(ch=0;ch<s->nb_channels;ch++) {
|
for(ch=0;ch<s->nb_all_channels;ch++) {
|
||||||
/* fixed mdct to the six sub blocks & exponent computation */
|
/* fixed mdct to the six sub blocks & exponent computation */
|
||||||
for(i=0;i<NB_BLOCKS;i++) {
|
for(i=0;i<NB_BLOCKS;i++) {
|
||||||
INT16 *sptr;
|
INT16 *sptr;
|
||||||
@ -1273,7 +1307,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
|
|||||||
|
|
||||||
/* compute input samples */
|
/* compute input samples */
|
||||||
memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
|
memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(INT16));
|
||||||
sinc = s->nb_channels;
|
sinc = s->nb_all_channels;
|
||||||
sptr = samples + (sinc * (N/2) * i) + ch;
|
sptr = samples + (sinc * (N/2) * i) + ch;
|
||||||
for(j=0;j<N/2;j++) {
|
for(j=0;j<N/2;j++) {
|
||||||
v = *sptr;
|
v = *sptr;
|
||||||
@ -1319,7 +1353,7 @@ int AC3_encode_frame(AVCodecContext *avctx,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compute_exp_strategy(exp_strategy, exp, ch);
|
compute_exp_strategy(exp_strategy, exp, ch, ch == s->lfe_channel);
|
||||||
|
|
||||||
/* compute the exponents as the decoder will see them. The
|
/* compute the exponents as the decoder will see them. The
|
||||||
EXP_REUSE case must be handled carefully : we select the
|
EXP_REUSE case must be handled carefully : we select the
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
|
|
||||||
#define AC3_FRAME_SIZE (6*256)
|
#define AC3_FRAME_SIZE (6*256)
|
||||||
#define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
|
#define AC3_MAX_CODED_FRAME_SIZE 3840 /* in bytes */
|
||||||
#define AC3_MAX_CHANNELS 2 /* we handle at most two channels, although
|
#define AC3_MAX_CHANNELS 6
|
||||||
AC3 allows 6 channels */
|
|
||||||
|
|
||||||
typedef struct AC3EncodeContext {
|
typedef struct AC3EncodeContext {
|
||||||
PutBitContext pb;
|
PutBitContext pb;
|
||||||
int nb_channels;
|
int nb_channels;
|
||||||
|
int nb_all_channels;
|
||||||
|
int lfe_channel;
|
||||||
int bit_rate;
|
int bit_rate;
|
||||||
int sample_rate;
|
int sample_rate;
|
||||||
int bsid;
|
int bsid;
|
||||||
@ -16,6 +17,7 @@ typedef struct AC3EncodeContext {
|
|||||||
int frmsizecod;
|
int frmsizecod;
|
||||||
int fscod; /* frequency */
|
int fscod; /* frequency */
|
||||||
int acmod;
|
int acmod;
|
||||||
|
int lfe;
|
||||||
int bsmod;
|
int bsmod;
|
||||||
short last_samples[AC3_MAX_CHANNELS][256];
|
short last_samples[AC3_MAX_CHANNELS][256];
|
||||||
int chbwcod[AC3_MAX_CHANNELS];
|
int chbwcod[AC3_MAX_CHANNELS];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user