• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • 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 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 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 #include "dsputil.h"
00030 
00031 #define MAX_BACKWARD_FILTER_ORDER  36
00032 #define MAX_BACKWARD_FILTER_LEN    40
00033 #define MAX_BACKWARD_FILTER_NONREC 35
00034 
00035 #define RA288_BLOCK_SIZE        5
00036 #define RA288_BLOCKS_PER_FRAME 32
00037 
00038 typedef struct {
00039     AVFrame frame;
00040     DSPContext dsp;
00041     DECLARE_ALIGNED(16, float,   sp_lpc)[FFALIGN(36, 8)];   
00042     DECLARE_ALIGNED(16, float, gain_lpc)[FFALIGN(10, 8)];   
00043 
00047     float sp_hist[111];
00048 
00050     float sp_rec[37];
00051 
00055     float gain_hist[38];
00056 
00058     float gain_rec[11];
00059 } RA288Context;
00060 
00061 static av_cold int ra288_decode_init(AVCodecContext *avctx)
00062 {
00063     RA288Context *ractx = avctx->priv_data;
00064     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00065     dsputil_init(&ractx->dsp, avctx);
00066 
00067     avcodec_get_frame_defaults(&ractx->frame);
00068     avctx->coded_frame = &ractx->frame;
00069 
00070     return 0;
00071 }
00072 
00073 static void convolve(float *tgt, const float *src, int len, int n)
00074 {
00075     for (; n >= 0; n--)
00076         tgt[n] = ff_dot_productf(src, src - n, len);
00077 
00078 }
00079 
00080 static void decode(RA288Context *ractx, float gain, int cb_coef)
00081 {
00082     int i;
00083     double sumsum;
00084     float sum, buffer[5];
00085     float *block = ractx->sp_hist + 70 + 36; // current block
00086     float *gain_block = ractx->gain_hist + 28;
00087 
00088     memmove(ractx->sp_hist + 70, ractx->sp_hist + 75, 36*sizeof(*block));
00089 
00090     /* block 46 of G.728 spec */
00091     sum = 32.;
00092     for (i=0; i < 10; i++)
00093         sum -= gain_block[9-i] * ractx->gain_lpc[i];
00094 
00095     /* block 47 of G.728 spec */
00096     sum = av_clipf(sum, 0, 60);
00097 
00098     /* block 48 of G.728 spec */
00099     /* exp(sum * 0.1151292546497) == pow(10.0,sum/20) */
00100     sumsum = exp(sum * 0.1151292546497) * gain * (1.0/(1<<23));
00101 
00102     for (i=0; i < 5; i++)
00103         buffer[i] = codetable[cb_coef][i] * sumsum;
00104 
00105     sum = ff_dot_productf(buffer, buffer, 5) * ((1<<24)/5.);
00106 
00107     sum = FFMAX(sum, 1);
00108 
00109     /* shift and store */
00110     memmove(gain_block, gain_block + 1, 9 * sizeof(*gain_block));
00111 
00112     gain_block[9] = 10 * log10(sum) - 32;
00113 
00114     ff_celp_lp_synthesis_filterf(block, ractx->sp_lpc, buffer, 5, 36);
00115 }
00116 
00129 static void do_hybrid_window(RA288Context *ractx,
00130                              int order, int n, int non_rec, float *out,
00131                              float *hist, float *out2, const float *window)
00132 {
00133     int i;
00134     float buffer1[MAX_BACKWARD_FILTER_ORDER + 1];
00135     float buffer2[MAX_BACKWARD_FILTER_ORDER + 1];
00136     LOCAL_ALIGNED_16(float, work, [FFALIGN(MAX_BACKWARD_FILTER_ORDER +
00137                                            MAX_BACKWARD_FILTER_LEN   +
00138                                            MAX_BACKWARD_FILTER_NONREC, 8)]);
00139 
00140     ractx->dsp.vector_fmul(work, window, hist, FFALIGN(order + n + non_rec, 8));
00141 
00142     convolve(buffer1, work + order    , n      , order);
00143     convolve(buffer2, work + order + n, non_rec, order);
00144 
00145     for (i=0; i <= order; i++) {
00146         out2[i] = out2[i] * 0.5625 + buffer1[i];
00147         out [i] = out2[i]          + buffer2[i];
00148     }
00149 
00150     /* Multiply by the white noise correcting factor (WNCF). */
00151     *out *= 257./256.;
00152 }
00153 
00157 static void backward_filter(RA288Context *ractx,
00158                             float *hist, float *rec, const float *window,
00159                             float *lpc, const float *tab,
00160                             int order, int n, int non_rec, int move_size)
00161 {
00162     float temp[MAX_BACKWARD_FILTER_ORDER+1];
00163 
00164     do_hybrid_window(ractx, order, n, non_rec, temp, hist, rec, window);
00165 
00166     if (!compute_lpc_coefs(temp, order, lpc, 0, 1, 1))
00167         ractx->dsp.vector_fmul(lpc, lpc, tab, FFALIGN(order, 8));
00168 
00169     memmove(hist, hist + n, move_size*sizeof(*hist));
00170 }
00171 
00172 static int ra288_decode_frame(AVCodecContext * avctx, void *data,
00173                               int *got_frame_ptr, AVPacket *avpkt)
00174 {
00175     const uint8_t *buf = avpkt->data;
00176     int buf_size = avpkt->size;
00177     float *out;
00178     int i, ret;
00179     RA288Context *ractx = avctx->priv_data;
00180     GetBitContext gb;
00181 
00182     if (buf_size < avctx->block_align) {
00183         av_log(avctx, AV_LOG_ERROR,
00184                "Error! Input buffer is too small [%d<%d]\n",
00185                buf_size, avctx->block_align);
00186         return AVERROR_INVALIDDATA;
00187     }
00188 
00189     /* get output buffer */
00190     ractx->frame.nb_samples = RA288_BLOCK_SIZE * RA288_BLOCKS_PER_FRAME;
00191     if ((ret = avctx->get_buffer(avctx, &ractx->frame)) < 0) {
00192         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00193         return ret;
00194     }
00195     out = (float *)ractx->frame.data[0];
00196 
00197     init_get_bits(&gb, buf, avctx->block_align * 8);
00198 
00199     for (i=0; i < RA288_BLOCKS_PER_FRAME; i++) {
00200         float gain = amptable[get_bits(&gb, 3)];
00201         int cb_coef = get_bits(&gb, 6 + (i&1));
00202 
00203         decode(ractx, gain, cb_coef);
00204 
00205         memcpy(out, &ractx->sp_hist[70 + 36], RA288_BLOCK_SIZE * sizeof(*out));
00206         out += RA288_BLOCK_SIZE;
00207 
00208         if ((i & 7) == 3) {
00209             backward_filter(ractx, ractx->sp_hist, ractx->sp_rec, syn_window,
00210                             ractx->sp_lpc, syn_bw_tab, 36, 40, 35, 70);
00211 
00212             backward_filter(ractx, ractx->gain_hist, ractx->gain_rec, gain_window,
00213                             ractx->gain_lpc, gain_bw_tab, 10, 8, 20, 28);
00214         }
00215     }
00216 
00217     *got_frame_ptr   = 1;
00218     *(AVFrame *)data = ractx->frame;
00219 
00220     return avctx->block_align;
00221 }
00222 
00223 AVCodec ff_ra_288_decoder = {
00224     .name           = "real_288",
00225     .type           = AVMEDIA_TYPE_AUDIO,
00226     .id             = CODEC_ID_RA_288,
00227     .priv_data_size = sizeof(RA288Context),
00228     .init           = ra288_decode_init,
00229     .decode         = ra288_decode_frame,
00230     .capabilities   = CODEC_CAP_DR1,
00231     .long_name      = NULL_IF_CONFIG_SMALL("RealAudio 2.0 (28.8K)"),
00232 };
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1