• 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 "bitstream.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     /* output */
00107     for (i=0; i < 5; i++)
00108         block[i] = av_clipf(block[i], -4095./4096., 4095./4096.);
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[order + 1];
00128     float buffer2[order + 1];
00129     float work[order + n + non_rec];
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     /* Multiply by the white noise correcting factor (WNCF). */
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[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, const uint8_t * buf,
00164                               int buf_size)
00165 {
00166     float *out = data;
00167     int i, j;
00168     RA288Context *ractx = avctx->priv_data;
00169     GetBitContext gb;
00170 
00171     if (buf_size < avctx->block_align) {
00172         av_log(avctx, AV_LOG_ERROR,
00173                "Error! Input buffer is too small [%d<%d]\n",
00174                buf_size, avctx->block_align);
00175         return 0;
00176     }
00177 
00178     if (*data_size < 32*5*4)
00179         return -1;
00180 
00181     init_get_bits(&gb, buf, avctx->block_align * 8);
00182 
00183     for (i=0; i < 32; i++) {
00184         float gain = amptable[get_bits(&gb, 3)];
00185         int cb_coef = get_bits(&gb, 6 + (i&1));
00186 
00187         decode(ractx, gain, cb_coef);
00188 
00189         for (j=0; j < 5; j++)
00190             *(out++) = ractx->sp_hist[70 + 36 + j];
00191 
00192         if ((i & 7) == 3) {
00193             backward_filter(ractx->sp_hist, ractx->sp_rec, syn_window,
00194                             ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
00195 
00196             backward_filter(ractx->gain_hist, ractx->gain_rec, gain_window,
00197                             ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
00198         }
00199     }
00200 
00201     *data_size = (char *)out - (char *)data;
00202     return avctx->block_align;
00203 }
00204 
00205 AVCodec ra_288_decoder =
00206 {
00207     "real_288",
00208     CODEC_TYPE_AUDIO,
00209     CODEC_ID_RA_288,
00210     sizeof(RA288Context),
00211     ra288_decode_init,
00212     NULL,
00213     NULL,
00214     ra288_decode_frame,
00215     .long_name = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
00216 };

Generated on Sat Feb 16 2013 09:23:13 for ffmpeg by  doxygen 1.7.1