00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avcodec.h"
00023 #include "internal.h"
00024 #define BITSTREAM_READER_LE
00025 #include "get_bits.h"
00026 #include "ra288.h"
00027 #include "lpc.h"
00028 #include "celp_math.h"
00029 #include "celp_filters.h"
00030 #include "dsputil.h"
00031
00032 #define MAX_BACKWARD_FILTER_ORDER 36
00033 #define MAX_BACKWARD_FILTER_LEN 40
00034 #define MAX_BACKWARD_FILTER_NONREC 35
00035
00036 #define RA288_BLOCK_SIZE 5
00037 #define RA288_BLOCKS_PER_FRAME 32
00038
00039 typedef struct {
00040 AVFrame frame;
00041 DSPContext dsp;
00042 DECLARE_ALIGNED(16, float, sp_lpc)[FFALIGN(36, 8)];
00043 DECLARE_ALIGNED(16, float, gain_lpc)[FFALIGN(10, 8)];
00044
00048 float sp_hist[111];
00049
00051 float sp_rec[37];
00052
00056 float gain_hist[38];
00057
00059 float gain_rec[11];
00060 } RA288Context;
00061
00062 static av_cold int ra288_decode_init(AVCodecContext *avctx)
00063 {
00064 RA288Context *ractx = avctx->priv_data;
00065 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00066 dsputil_init(&ractx->dsp, avctx);
00067
00068 avcodec_get_frame_defaults(&ractx->frame);
00069 avctx->coded_frame = &ractx->frame;
00070
00071 return 0;
00072 }
00073
00074 static void convolve(float *tgt, const float *src, int len, int n)
00075 {
00076 for (; n >= 0; n--)
00077 tgt[n] = ff_dot_productf(src, src - n, len);
00078
00079 }
00080
00081 static void decode(RA288Context *ractx, float gain, int cb_coef)
00082 {
00083 int i;
00084 double sumsum;
00085 float sum, buffer[5];
00086 float *block = ractx->sp_hist + 70 + 36;
00087 float *gain_block = ractx->gain_hist + 28;
00088
00089 memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
00090
00091
00092 sum = 32.;
00093 for (i=0; i < 10; i++)
00094 sum -= gain_block[9-i] * ractx->gain_lpc[i];
00095
00096
00097 sum = av_clipf(sum, 0, 60);
00098
00099
00100
00101 sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23));
00102
00103 for (i=0; i < 5; i++)
00104 buffer[i] = codetable[cb_coef][i] * sumsum;
00105
00106 sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.);
00107
00108 sum = FFMAX(sum, 1);
00109
00110
00111 memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block));
00112
00113 gain_block[9] = 10 * log10(sum) - 32;
00114
00115 ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36);
00116 }
00117
00130 static void do_hybrid_window(RA288Context *ractx,
00131 int order, int n, int non_rec, float *out,
00132 float *hist, float *out2, const float *window)
00133 {
00134 int i;
00135 float buffer1[MAX_BACKWARD_FILTER_ORDER + 1];
00136 float buffer2[MAX_BACKWARD_FILTER_ORDER + 1];
00137 LOCAL_ALIGNED_16(float, work, [FFALIGN(MAX_BACKWARD_FILTER_ORDER +
00138 MAX_BACKWARD_FILTER_LEN +
00139 MAX_BACKWARD_FILTER_NONREC, 8)]);
00140
00141 ractx->dsp.vector_fmul(work, window, hist, FFALIGN(order + n + non_rec, 8));
00142
00143 convolve(buffer1, work + order , n , order);
00144 convolve(buffer2, work + order + n, non_rec, order);
00145
00146 for (i=0; i <= order; i++) {
00147 out2[i] = out2[i] * 0.5625 + buffer1[i];
00148 out [i] = out2[i] + buffer2[i];
00149 }
00150
00151
00152 *out *= 257./256.;
00153 }
00154
00158 static void backward_filter(RA288Context *ractx,
00159 float *hist, float *rec, const float *window,
00160 float *lpc, const float *tab,
00161 int order, int n, int non_rec, int move_size)
00162 {
00163 float temp[MAX_BACKWARD_FILTER_ORDER+1];
00164
00165 do_hybrid_window(ractx, order, n, non_rec, temp, hist, rec, window);
00166
00167 if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
00168 ractx->dsp.vector_fmul(lpc, lpc, tab, FFALIGN(order, 8));
00169
00170 memmove(hist, hist + n, move_size*sizeof(*hist));
00171 }
00172
00173 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
00174 int *got_frame_ptr, AVPacket *avpkt)
00175 {
00176 const uint8_t *buf = avpkt->data;
00177 int buf_size = avpkt->size;
00178 float *out;
00179 int i, ret;
00180 RA288Context *ractx = avctx->priv_data;
00181 GetBitContext gb;
00182
00183 if (buf_size < avctx->block_align) {
00184 av_log(avctx, AV_LOG_ERROR,
00185 "Error! Input buffer is too small [%d<%d]\n",
00186 buf_size, avctx->block_align);
00187 return AVERROR_INVALIDDATA;
00188 }
00189
00190
00191 ractx->frame.nb_samples = RA288_BLOCK_SIZE * RA288_BLOCKS_PER_FRAME;
00192 if ((ret = ff_get_buffer(avctx, &ractx->frame)) < 0) {
00193 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00194 return ret;
00195 }
00196 out = (float *)ractx->frame.data[0];
00197
00198 init_get_bits(&gb, buf, avctx->block_align * 8);
00199
00200 for (i=0; i < RA288_BLOCKS_PER_FRAME; i++) {
00201 float gain = amptable[get_bits(&gb, 3)];
00202 int cb_coef = get_bits(&gb, 6 + (i&1));
00203
00204 decode(ractx, gain, cb_coef);
00205
00206 memcpy(out, &ractx->sp_hist[70 + 36], RA288_BLOCK_SIZE * sizeof(*out));
00207 out += RA288_BLOCK_SIZE;
00208
00209 if ((i & 7) == 3) {
00210 backward_filter(ractx, ractx->sp_hist, ractx->sp_rec, syn_window,
00211 ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
00212
00213 backward_filter(ractx, ractx->gain_hist, ractx->gain_rec, gain_window,
00214 ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
00215 }
00216 }
00217
00218 *got_frame_ptr = 1;
00219 *(AVFrame *)data = ractx->frame;
00220
00221 return avctx->block_align;
00222 }
00223
00224 AVCodec ff_ra_288_decoder = {
00225 .name = "real_288",
00226 .type = AVMEDIA_TYPE_AUDIO,
00227 .id = CODEC_ID_RA_288,
00228 .priv_data_size = sizeof(RA288Context),
00229 .init = ra288_decode_init,
00230 .decode = ra288_decode_frame,
00231 .capabilities = CODEC_CAP_DR1,
00232 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
00233 };