Libav 0.7.1
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 Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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 #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; // current block
00080     float *gain_block = ractx->gain_hist + 28;
00081 
00082     memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
00083 
00084     /* block 46 of G.728 spec */
00085     sum = 32.;
00086     for (i=0; i < 10; i++)
00087         sum -= gain_block[9-i] * ractx->gain_lpc[i];
00088 
00089     /* block 47 of G.728 spec */
00090     sum = av_clipf(sum, 0, 60);
00091 
00092     /* block 48 of G.728 spec */
00093     /* exp(sum * 0.1151292546497) == pow(10.0,sum/20) */
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     /* shift and store */
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     /* 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[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 };