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 |
*/
|
a903f8f0 |
#include "libavutil/channel_layout.h" |
d5a7229b |
#include "libavutil/float_dsp.h" |
b8414bbd |
#include "avcodec.h" |
594d4d5d |
#include "internal.h" |
aaf47bcd |
#define BITSTREAM_READER_LE |
9106a698 |
#include "get_bits.h" |
9085af07 |
#include "ra288.h" |
1be0fc29 |
#include "lpc.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
|
fa6be04b |
#define RA288_BLOCK_SIZE 5
#define RA288_BLOCKS_PER_FRAME 32
|
b8414bbd |
typedef struct { |
0eea2129 |
AVFrame frame; |
0131e70a |
DSPContext dsp; |
d5a7229b |
AVFloatDSPContext fdsp; |
5ff01259 |
DECLARE_ALIGNED(32, float, sp_lpc)[FFALIGN(36, 16)]; ///< LPC coefficients for speech data (spec: A)
DECLARE_ALIGNED(32, float, gain_lpc)[FFALIGN(10, 16)]; ///< 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)
{ |
0131e70a |
RA288Context *ractx = avctx->priv_data; |
4e13e504 |
avctx->channels = 1;
avctx->channel_layout = AV_CH_LAYOUT_MONO;
avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
|
5c392bac |
if (avctx->block_align <= 0) {
av_log_ask_for_sample(avctx, "unsupported block align\n");
return AVERROR_PATCHWELCOME;
}
|
d5a7229b |
avpriv_float_dsp_init(&ractx->fdsp, avctx->flags & CODEC_FLAG_BITEXACT); |
0eea2129 |
avcodec_get_frame_defaults(&ractx->frame);
avctx->coded_frame = &ractx->frame;
|
fd76c37f |
return 0;
}
|
e60daa34 |
static void convolve(float *tgt, const float *src, int len, int n)
{
for (; n >= 0; n--) |
dafcbfe4 |
tgt[n] = ff_scalarproduct_float_c(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 |
|
416d2f7a |
sum = ff_scalarproduct_float_c(buffer, buffer, 5); |
0e3510a3 |
|
aae1c247 |
sum = FFMAX(sum, 5. / (1<<24)); |
eec7ade2 |
/* shift and store */ |
111734de |
memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block)); |
eec7ade2 |
|
aae1c247 |
gain_block[9] = 10 * log10(sum) + (10*log10(((1<<24)/5.)) - 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
*/ |
0131e70a |
static void do_hybrid_window(RA288Context *ractx,
int order, int n, int non_rec, float *out, |
9547cadb |
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]; |
5ff01259 |
LOCAL_ALIGNED(32, float, work, [FFALIGN(MAX_BACKWARD_FILTER_ORDER +
MAX_BACKWARD_FILTER_LEN +
MAX_BACKWARD_FILTER_NONREC, 16)]); |
eec7ade2 |
|
f5581266 |
av_assert2(order>=0);
|
d5a7229b |
ractx->fdsp.vector_fmul(work, window, hist, FFALIGN(order + n + non_rec, 16)); |
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 |
*/ |
0131e70a |
static void backward_filter(RA288Context *ractx,
float *hist, float *rec, const float *window, |
d552d04d |
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 |
|
0131e70a |
do_hybrid_window(ractx, order, n, non_rec, temp, hist, rec, window); |
44528363 |
|
d552d04d |
if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1)) |
d5a7229b |
ractx->fdsp.vector_fmul(lpc, lpc, tab, FFALIGN(order, 16)); |
44528363 |
|
d552d04d |
memmove(hist, hist + n, move_size*sizeof(*hist)); |
b8414bbd |
}
|
f38deb44 |
static int ra288_decode_frame(AVCodecContext * avctx, void *data, |
0eea2129 |
int *got_frame_ptr, AVPacket *avpkt) |
860208a4 |
{ |
7a00bbad |
const uint8_t *buf = avpkt->data;
int buf_size = avpkt->size; |
0eea2129 |
float *out;
int i, ret; |
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); |
2467d8d9 |
return AVERROR_INVALIDDATA; |
f38deb44 |
}
|
0eea2129 |
/* get output buffer */
ractx->frame.nb_samples = RA288_BLOCK_SIZE * RA288_BLOCKS_PER_FRAME; |
594d4d5d |
if ((ret = ff_get_buffer(avctx, &ractx->frame)) < 0) { |
0eea2129 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
return ret; |
8f8f5ae9 |
} |
0eea2129 |
out = (float *)ractx->frame.data[0]; |
8089c652 |
|
f38deb44 |
init_get_bits(&gb, buf, avctx->block_align * 8); |
eec7ade2 |
|
fa6be04b |
for (i=0; i < RA288_BLOCKS_PER_FRAME; 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 |
|
03e5d611 |
memcpy(out, &ractx->sp_hist[70 + 36], RA288_BLOCK_SIZE * sizeof(*out));
out += RA288_BLOCK_SIZE; |
eec7ade2 |
|
d552d04d |
if ((i & 7) == 3) { |
0131e70a |
backward_filter(ractx, ractx->sp_hist, ractx->sp_rec, syn_window, |
d552d04d |
ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
|
0131e70a |
backward_filter(ractx, ractx->gain_hist, ractx->gain_rec, gain_window, |
d552d04d |
ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
} |
eec7ade2 |
}
|
0eea2129 |
*got_frame_ptr = 1;
*(AVFrame *)data = ractx->frame;
|
e0f7e329 |
return avctx->block_align; |
b8414bbd |
}
|
86714887 |
AVCodec ff_ra_288_decoder = {
.name = "real_288",
.type = AVMEDIA_TYPE_AUDIO, |
36ef5369 |
.id = AV_CODEC_ID_RA_288, |
86714887 |
.priv_data_size = sizeof(RA288Context),
.init = ra288_decode_init,
.decode = ra288_decode_frame, |
0eea2129 |
.capabilities = CODEC_CAP_DR1, |
86714887 |
.long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"), |
b8414bbd |
}; |