• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/ra288.c

Go to the documentation of this file.
00001 /*
00002  * RealAudio 2.0 (28.8K)
00003  * Copyright (c) 2003 the ffmpeg project
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 typedef struct {
00031     float sp_lpc[36];      
00032     float gain_lpc[10];    
00033 
00037     float sp_hist[111];
00038 
00040     float sp_rec[37];
00041 
00045     float gain_hist[38];
00046 
00048     float gain_rec[11];
00049 } RA288Context;
00050 
00051 static av_cold int ra288_decode_init(AVCodecContext *avctx)
00052 {
00053     avctx->sample_fmt = SAMPLE_FMT_FLT;
00054     return 0;
00055 }
00056 
00057 static void apply_window(float *tgt, const float *m1, const float *m2, int n)
00058 {
00059     while (n--)
00060         *tgt++ = *m1++ * *m2++;
00061 }
00062 
00063 static void convolve(float *tgt, const float *src, int len, int n)
00064 {
00065     for (; n >= 0; n--)
00066         tgt[n] = ff_dot_productf(src, src - n, len);
00067 
00068 }
00069 
00070 static void decode(RA288Context *ractx, float gain, int cb_coef)
00071 {
00072     int i;
00073     double sumsum;
00074     float sum, buffer[5];
00075     float *block = ractx->sp_hist + 70 + 36; // current block
00076     float *gain_block = ractx->gain_hist + 28;
00077 
00078     memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
00079 
00080     /* block 46 of G.728 spec */
00081     sum = 32.;
00082     for (i=0; i < 10; i++)
00083         sum -= gain_block[9-i] * ractx->gain_lpc[i];
00084 
00085     /* block 47 of G.728 spec */
00086     sum = av_clipf(sum, 0, 60);
00087 
00088     /* block 48 of G.728 spec */
00089     /* exp(sum * 0.1151292546497) == pow(10.0,sum/20) */
00090     sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23));
00091 
00092     for (i=0; i < 5; i++)
00093         buffer[i] = codetable[cb_coef][i] * sumsum;
00094 
00095     sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.);
00096 
00097     sum = FFMAX(sum, 1);
00098 
00099     /* shift and store */
00100     memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block));
00101 
00102     gain_block[9] = 10 * log10(sum) - 32;
00103 
00104     ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36);
00105 }
00106 
00119 static void do_hybrid_window(int order, int n, int non_rec, float *out,
00120                              float *hist, float *out2, const float *window)
00121 {
00122     int i;
00123     float buffer1[order + 1];
00124     float buffer2[order + 1];
00125     float work[order + n + non_rec];
00126 
00127     apply_window(work, window, hist, order + n + non_rec);
00128 
00129     convolve(buffer1, work + order    , n      , order);
00130     convolve(buffer2, work + order + n, non_rec, order);
00131 
00132     for (i=0; i <= order; i++) {
00133         out2[i] = out2[i] * 0.5625 + buffer1[i];
00134         out [i] = out2[i]          + buffer2[i];
00135     }
00136 
00137     /* Multiply by the white noise correcting factor (WNCF). */
00138     *out *= 257./256.;
00139 }
00140 
00144 static void backward_filter(float *hist, float *rec, const float *window,
00145                             float *lpc, const float *tab,
00146                             int order, int n, int non_rec, int move_size)
00147 {
00148     float temp[order+1];
00149 
00150     do_hybrid_window(order, n, non_rec, temp, hist, rec, window);
00151 
00152     if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
00153         apply_window(lpc, lpc, tab, order);
00154 
00155     memmove(hist, hist + n, move_size*sizeof(*hist));
00156 }
00157 
00158 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
00159                               int *data_size, AVPacket *avpkt)
00160 {
00161     const uint8_t *buf = avpkt->data;
00162     int buf_size = avpkt->size;
00163     float *out = data;
00164     int i, j;
00165     RA288Context *ractx = avctx->priv_data;
00166     GetBitContext gb;
00167 
00168     if (buf_size < avctx->block_align) {
00169         av_log(avctx, AV_LOG_ERROR,
00170                "Error! Input buffer is too small [%d<%d]\n",
00171                buf_size, avctx->block_align);
00172         return 0;
00173     }
00174 
00175     if (*data_size < 32*5*4)
00176         return -1;
00177 
00178     init_get_bits(&gb, buf, avctx->block_align * 8);
00179 
00180     for (i=0; i < 32; i++) {
00181         float gain = amptable[get_bits(&gb, 3)];
00182         int cb_coef = get_bits(&gb, 6 + (i&1));
00183 
00184         decode(ractx, gain, cb_coef);
00185 
00186         for (j=0; j < 5; j++)
00187             *(out++) = ractx->sp_hist[70 + 36 + j];
00188 
00189         if ((i & 7) == 3) {
00190             backward_filter(ractx->sp_hist, ractx->sp_rec, syn_window,
00191                             ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
00192 
00193             backward_filter(ractx->gain_hist, ractx->gain_rec, gain_window,
00194                             ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
00195         }
00196     }
00197 
00198     *data_size = (char *)out - (char *)data;
00199     return avctx->block_align;
00200 }
00201 
00202 AVCodec ra_288_decoder =
00203 {
00204     "real_288",
00205     AVMEDIA_TYPE_AUDIO,
00206     CODEC_ID_RA_288,
00207     sizeof(RA288Context),
00208     ra288_decode_init,
00209     NULL,
00210     NULL,
00211     ra288_decode_frame,
00212     .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
00213 };

Generated on Fri Sep 16 2011 17:17:42 for FFmpeg by  doxygen 1.7.1