b8414bbd |
/*
* RealAudio 2.0 (28.8K)
* Copyright (c) 2003 the ffmpeg project
* |
b78e7197 |
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or |
b8414bbd |
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either |
b78e7197 |
* version 2.1 of the License, or (at your option) any later version. |
b8414bbd |
* |
b78e7197 |
* FFmpeg is distributed in the hope that it will be useful, |
b8414bbd |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public |
b78e7197 |
* License along with FFmpeg; if not, write to the Free Software |
5509bffa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
b8414bbd |
*/
#include "avcodec.h" |
a40b2c2a |
#define ALT_BITSTREAM_READER_LE |
9106a698 |
#include "get_bits.h" |
9085af07 |
#include "ra288.h" |
1be0fc29 |
#include "lpc.h" |
08c43397 |
#include "celp_math.h" |
66d4c628 |
#include "celp_filters.h" |
115329f1 |
|
0b37ccca |
#define MAX_BACKWARD_FILTER_ORDER 36
#define MAX_BACKWARD_FILTER_LEN 40
#define MAX_BACKWARD_FILTER_NONREC 35
|
b8414bbd |
typedef struct { |
6509507f |
float sp_lpc[36]; ///< LPC coefficients for speech data (spec: A) |
9c10ab6c |
float gain_lpc[10]; ///< LPC coefficients for gain (spec: GB) |
eec7ade2 |
|
111734de |
/** speech data history (spec: SB).
* Its first 70 coefficients are updated only at backward filtering.
*/
float sp_hist[111]; |
272d258a |
|
9c10ab6c |
/// speech part of the gain autocorrelation (spec: REXP) |
272d258a |
float sp_rec[37];
|
111734de |
/** log-gain history (spec: SBLG).
* Its first 28 coefficients are updated only at backward filtering.
*/
float gain_hist[38]; |
272d258a |
|
9c10ab6c |
/// recursive part of the gain autocorrelation (spec: REXPLG) |
272d258a |
float gain_rec[11]; |
029e1c01 |
} RA288Context; |
b8414bbd |
|
fd76c37f |
static av_cold int ra288_decode_init(AVCodecContext *avctx)
{ |
5d6e4c16 |
avctx->sample_fmt = AV_SAMPLE_FMT_FLT; |
fd76c37f |
return 0;
}
|
5381a00a |
static void apply_window(float *tgt, const float *m1, const float *m2, int n) |
e07c5ade |
{
while (n--) |
efa86ebe |
*tgt++ = *m1++ * *m2++; |
e07c5ade |
}
|
e60daa34 |
static void convolve(float *tgt, const float *src, int len, int n)
{
for (; n >= 0; n--) |
08c43397 |
tgt[n] = ff_dot_productf(src, src - n, len); |
e60daa34 |
}
|
952a980d |
static void decode(RA288Context *ractx, float gain, int cb_coef) |
b8414bbd |
{ |
66d4c628 |
int i; |
aa64ee30 |
double sumsum;
float sum, buffer[5]; |
111734de |
float *block = ractx->sp_hist + 70 + 36; // current block
float *gain_block = ractx->gain_hist + 28; |
b8414bbd |
|
111734de |
memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block)); |
b8414bbd |
|
b26d3205 |
/* block 46 of G.728 spec */ |
e3751aa6 |
sum = 32.;
for (i=0; i < 10; i++) |
111734de |
sum -= gain_block[9-i] * ractx->gain_lpc[i]; |
b8414bbd |
|
b26d3205 |
/* block 47 of G.728 spec */ |
3819081f |
sum = av_clipf(sum, 0, 60); |
eec7ade2 |
|
b26d3205 |
/* block 48 of G.728 spec */ |
a987a126 |
/* exp(sum * 0.1151292546497) == pow(10.0,sum/20) */ |
64e4af2a |
sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23)); |
eec7ade2 |
|
4e33ed36 |
for (i=0; i < 5; i++) |
a987a126 |
buffer[i] = codetable[cb_coef][i] * sumsum; |
eec7ade2 |
|
08c43397 |
sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.); |
0e3510a3 |
sum = FFMAX(sum, 1); |
eec7ade2 |
/* shift and store */ |
111734de |
memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block)); |
eec7ade2 |
|
111734de |
gain_block[9] = 10 * log10(sum) - 32; |
eec7ade2 |
|
66d4c628 |
ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36); |
b8414bbd |
}
|
475d0e14 |
/** |
36b3e36e |
* Hybrid window filtering, see blocks 36 and 49 of the G.728 specification. |
475d0e14 |
* |
36b3e36e |
* @param order filter order
* @param n input length
* @param non_rec number of non-recursive samples
* @param out filter output |
111734de |
* @param hist pointer to the input history of the filter |
a53b5450 |
* @param out pointer to the non-recursive part of the output |
475d0e14 |
* @param out2 pointer to the recursive part of the output
* @param window pointer to the windowing function table
*/ |
9547cadb |
static void do_hybrid_window(int order, int n, int non_rec, float *out,
float *hist, float *out2, const float *window) |
b8414bbd |
{ |
4ca7e74c |
int i; |
0b37ccca |
float buffer1[MAX_BACKWARD_FILTER_ORDER + 1];
float buffer2[MAX_BACKWARD_FILTER_ORDER + 1];
float work[MAX_BACKWARD_FILTER_ORDER + MAX_BACKWARD_FILTER_LEN + MAX_BACKWARD_FILTER_NONREC]; |
eec7ade2 |
|
5381a00a |
apply_window(work, window, hist, order + n + non_rec); |
115329f1 |
|
6888b4fc |
convolve(buffer1, work + order , n , order);
convolve(buffer2, work + order + n, non_rec, order); |
b8414bbd |
|
4e33ed36 |
for (i=0; i <= order; i++) {
out2[i] = out2[i] * 0.5625 + buffer1[i];
out [i] = out2[i] + buffer2[i]; |
eec7ade2 |
} |
427981c7 |
|
36b3e36e |
/* Multiply by the white noise correcting factor (WNCF). */ |
427981c7 |
*out *= 257./256.; |
b8414bbd |
}
|
2477d609 |
/** |
36b3e36e |
* Backward synthesis filter, find the LPC coefficients from past speech data. |
2477d609 |
*/ |
d552d04d |
static void backward_filter(float *hist, float *rec, const float *window,
float *lpc, const float *tab,
int order, int n, int non_rec, int move_size) |
b8414bbd |
{ |
0b37ccca |
float temp[MAX_BACKWARD_FILTER_ORDER+1]; |
aed39f6c |
|
d552d04d |
do_hybrid_window(order, n, non_rec, temp, hist, rec, window); |
44528363 |
|
d552d04d |
if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
apply_window(lpc, lpc, tab, order); |
44528363 |
|
d552d04d |
memmove(hist, hist + n, move_size*sizeof(*hist)); |
b8414bbd |
}
|
f38deb44 |
static int ra288_decode_frame(AVCodecContext * avctx, void *data, |
7a00bbad |
int *data_size, AVPacket *avpkt) |
860208a4 |
{ |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
faf6d696 |
float *out = data; |
4e33ed36 |
int i, j; |
952a980d |
RA288Context *ractx = avctx->priv_data; |
6091357f |
GetBitContext gb; |
eec7ade2 |
|
f38deb44 |
if (buf_size < avctx->block_align) {
av_log(avctx, AV_LOG_ERROR,
"Error! Input buffer is too small [%d<%d]\n",
buf_size, avctx->block_align);
return 0;
}
|
faf6d696 |
if (*data_size < 32*5*4) |
8089c652 |
return -1;
|
f38deb44 |
init_get_bits(&gb, buf, avctx->block_align * 8); |
eec7ade2 |
|
4e33ed36 |
for (i=0; i < 32; i++) { |
12ea267b |
float gain = amptable[get_bits(&gb, 3)]; |
4e33ed36 |
int cb_coef = get_bits(&gb, 6 + (i&1)); |
045e21cc |
|
952a980d |
decode(ractx, gain, cb_coef); |
eec7ade2 |
|
4e33ed36 |
for (j=0; j < 5; j++) |
b6c77581 |
*(out++) = ractx->sp_hist[70 + 36 + j]; |
eec7ade2 |
|
d552d04d |
if ((i & 7) == 3) {
backward_filter(ractx->sp_hist, ractx->sp_rec, syn_window,
ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
backward_filter(ractx->gain_hist, ractx->gain_rec, gain_window,
ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
} |
eec7ade2 |
}
|
f38deb44 |
*data_size = (char *)out - (char *)data; |
e0f7e329 |
return avctx->block_align; |
b8414bbd |
}
|
e7e2df27 |
AVCodec ff_ra_288_decoder = |
b8414bbd |
{
"real_288", |
72415b2a |
AVMEDIA_TYPE_AUDIO, |
b8414bbd |
CODEC_ID_RA_288, |
029e1c01 |
sizeof(RA288Context), |
fd76c37f |
ra288_decode_init, |
b8414bbd |
NULL,
NULL,
ra288_decode_frame, |
fe4bf374 |
.long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"), |
b8414bbd |
}; |