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