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
00030 #define MAX_BACKWARD_FILTER_ORDER 36
00031 #define MAX_BACKWARD_FILTER_LEN 40
00032 #define MAX_BACKWARD_FILTER_NONREC 35
00033
00034 typedef struct {
00035 float sp_lpc[36];
00036 float gain_lpc[10];
00037
00041 float sp_hist[111];
00042
00044 float sp_rec[37];
00045
00049 float gain_hist[38];
00050
00052 float gain_rec[11];
00053 } RA288Context;
00054
00055 static av_cold int ra288_decode_init(AVCodecContext *avctx)
00056 {
00057 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00058 return 0;
00059 }
00060
00061 static void apply_window(float *tgt, const float *m1, const float *m2, int n)
00062 {
00063 while (n--)
00064 *tgt++ = *m1++ * *m2++;
00065 }
00066
00067 static void convolve(float *tgt, const float *src, int len, int n)
00068 {
00069 for (; n >= 0; n--)
00070 tgt[n] = ff_dot_productf(src, src - n, len);
00071
00072 }
00073
00074 static void decode(RA288Context *ractx, float gain, int cb_coef)
00075 {
00076 int i;
00077 double sumsum;
00078 float sum, buffer[5];
00079 float *block = ractx->sp_hist + 70 + 36;
00080 float *gain_block = ractx->gain_hist + 28;
00081
00082 memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
00083
00084
00085 sum = 32.;
00086 for (i=0; i < 10; i++)
00087 sum -= gain_block[9-i] * ractx->gain_lpc[i];
00088
00089
00090 sum = av_clipf(sum, 0, 60);
00091
00092
00093
00094 sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23));
00095
00096 for (i=0; i < 5; i++)
00097 buffer[i] = codetable[cb_coef][i] * sumsum;
00098
00099 sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.);
00100
00101 sum = FFMAX(sum, 1);
00102
00103
00104 memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block));
00105
00106 gain_block[9] = 10 * log10(sum) - 32;
00107
00108 ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36);
00109 }
00110
00123 static void do_hybrid_window(int order, int n, int non_rec, float *out,
00124 float *hist, float *out2, const float *window)
00125 {
00126 int i;
00127 float buffer1[MAX_BACKWARD_FILTER_ORDER + 1];
00128 float buffer2[MAX_BACKWARD_FILTER_ORDER + 1];
00129 float work[MAX_BACKWARD_FILTER_ORDER + MAX_BACKWARD_FILTER_LEN + MAX_BACKWARD_FILTER_NONREC];
00130
00131 apply_window(work, window, hist, order + n + non_rec);
00132
00133 convolve(buffer1, work + order , n , order);
00134 convolve(buffer2, work + order + n, non_rec, order);
00135
00136 for (i=0; i <= order; i++) {
00137 out2[i] = out2[i] * 0.5625 + buffer1[i];
00138 out [i] = out2[i] + buffer2[i];
00139 }
00140
00141
00142 *out *= 257./256.;
00143 }
00144
00148 static void backward_filter(float *hist, float *rec, const float *window,
00149 float *lpc, const float *tab,
00150 int order, int n, int non_rec, int move_size)
00151 {
00152 float temp[MAX_BACKWARD_FILTER_ORDER+1];
00153
00154 do_hybrid_window(order, n, non_rec, temp, hist, rec, window);
00155
00156 if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
00157 apply_window(lpc, lpc, tab, order);
00158
00159 memmove(hist, hist + n, move_size*sizeof(*hist));
00160 }
00161
00162 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
00163 int *data_size, AVPacket *avpkt)
00164 {
00165 const uint8_t *buf = avpkt->data;
00166 int buf_size = avpkt->size;
00167 float *out = data;
00168 int i, j;
00169 RA288Context *ractx = avctx->priv_data;
00170 GetBitContext gb;
00171
00172 if (buf_size < avctx->block_align) {
00173 av_log(avctx, AV_LOG_ERROR,
00174 "Error! Input buffer is too small [%d<%d]\n",
00175 buf_size, avctx->block_align);
00176 return 0;
00177 }
00178
00179 if (*data_size < 32*5*4)
00180 return -1;
00181
00182 init_get_bits(&gb, buf, avctx->block_align * 8);
00183
00184 for (i=0; i < 32; i++) {
00185 float gain = amptable[get_bits(&gb, 3)];
00186 int cb_coef = get_bits(&gb, 6 + (i&1));
00187
00188 decode(ractx, gain, cb_coef);
00189
00190 for (j=0; j < 5; j++)
00191 *(out++) = ractx->sp_hist[70 + 36 + j];
00192
00193 if ((i & 7) == 3) {
00194 backward_filter(ractx->sp_hist, ractx->sp_rec, syn_window,
00195 ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
00196
00197 backward_filter(ractx->gain_hist, ractx->gain_rec, gain_window,
00198 ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
00199 }
00200 }
00201
00202 *data_size = (char *)out - (char *)data;
00203 return avctx->block_align;
00204 }
00205
00206 AVCodec ff_ra_288_decoder =
00207 {
00208 "real_288",
00209 AVMEDIA_TYPE_AUDIO,
00210 CODEC_ID_RA_288,
00211 sizeof(RA288Context),
00212 ra288_decode_init,
00213 NULL,
00214 NULL,
00215 ra288_decode_frame,
00216 .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
00217 };