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