Libav 0.7.1
|
00001 /* 00002 * NellyMoser audio decoder 00003 * Copyright (c) 2007 a840bda5870ba11f19698ff6eb9581dfb0f95fa5, 00004 * 539459aeb7d425140b62a3ec7dbf6dc8e408a306, and 00005 * 520e17cd55896441042b14df2566a6eb610ed444 00006 * Copyright (c) 2007 Loic Minier <lool at dooz.org> 00007 * Benjamin Larsson 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a 00010 * copy of this software and associated documentation files (the "Software"), 00011 * to deal in the Software without restriction, including without limitation 00012 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00013 * and/or sell copies of the Software, and to permit persons to whom the 00014 * Software is furnished to do so, subject to the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be included in 00017 * all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00022 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00024 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00025 * DEALINGS IN THE SOFTWARE. 00026 */ 00027 00034 #include "nellymoser.h" 00035 #include "libavutil/lfg.h" 00036 #include "libavutil/random_seed.h" 00037 #include "libavutil/audioconvert.h" 00038 #include "avcodec.h" 00039 #include "dsputil.h" 00040 #include "fft.h" 00041 #include "fmtconvert.h" 00042 #include "sinewin.h" 00043 00044 #define ALT_BITSTREAM_READER_LE 00045 #include "get_bits.h" 00046 00047 00048 typedef struct NellyMoserDecodeContext { 00049 AVCodecContext* avctx; 00050 DECLARE_ALIGNED(32, float, float_buf)[NELLY_SAMPLES]; 00051 float state[128]; 00052 AVLFG random_state; 00053 GetBitContext gb; 00054 float scale_bias; 00055 DSPContext dsp; 00056 FFTContext imdct_ctx; 00057 FmtConvertContext fmt_conv; 00058 DECLARE_ALIGNED(32, float, imdct_out)[NELLY_BUF_LEN * 2]; 00059 } NellyMoserDecodeContext; 00060 00061 static void overlap_and_window(NellyMoserDecodeContext *s, float *state, float *audio, float *a_in) 00062 { 00063 int bot, top; 00064 00065 bot = 0; 00066 top = NELLY_BUF_LEN-1; 00067 00068 while (bot < NELLY_BUF_LEN) { 00069 audio[bot] = a_in [bot]*ff_sine_128[bot] 00070 +state[bot]*ff_sine_128[top]; 00071 00072 bot++; 00073 top--; 00074 } 00075 memcpy(state, a_in + NELLY_BUF_LEN, sizeof(float)*NELLY_BUF_LEN); 00076 } 00077 00078 static void nelly_decode_block(NellyMoserDecodeContext *s, 00079 const unsigned char block[NELLY_BLOCK_LEN], 00080 float audio[NELLY_SAMPLES]) 00081 { 00082 int i,j; 00083 float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN]; 00084 float *aptr, *bptr, *pptr, val, pval; 00085 int bits[NELLY_BUF_LEN]; 00086 unsigned char v; 00087 00088 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); 00089 00090 bptr = buf; 00091 pptr = pows; 00092 val = ff_nelly_init_table[get_bits(&s->gb, 6)]; 00093 for (i=0 ; i<NELLY_BANDS ; i++) { 00094 if (i > 0) 00095 val += ff_nelly_delta_table[get_bits(&s->gb, 5)]; 00096 pval = -pow(2, val/2048) * s->scale_bias; 00097 for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) { 00098 *bptr++ = val; 00099 *pptr++ = pval; 00100 } 00101 00102 } 00103 00104 ff_nelly_get_sample_bits(buf, bits); 00105 00106 for (i = 0; i < 2; i++) { 00107 aptr = audio + i * NELLY_BUF_LEN; 00108 00109 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); 00110 skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS); 00111 00112 for (j = 0; j < NELLY_FILL_LEN; j++) { 00113 if (bits[j] <= 0) { 00114 aptr[j] = M_SQRT1_2*pows[j]; 00115 if (av_lfg_get(&s->random_state) & 1) 00116 aptr[j] *= -1.0; 00117 } else { 00118 v = get_bits(&s->gb, bits[j]); 00119 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j]; 00120 } 00121 } 00122 memset(&aptr[NELLY_FILL_LEN], 0, 00123 (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float)); 00124 00125 s->imdct_ctx.imdct_calc(&s->imdct_ctx, s->imdct_out, aptr); 00126 /* XXX: overlapping and windowing should be part of a more 00127 generic imdct function */ 00128 overlap_and_window(s, s->state, aptr, s->imdct_out); 00129 } 00130 } 00131 00132 static av_cold int decode_init(AVCodecContext * avctx) { 00133 NellyMoserDecodeContext *s = avctx->priv_data; 00134 00135 s->avctx = avctx; 00136 av_lfg_init(&s->random_state, 0); 00137 ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0); 00138 00139 dsputil_init(&s->dsp, avctx); 00140 ff_fmt_convert_init(&s->fmt_conv, avctx); 00141 00142 s->scale_bias = 1.0/(1*8); 00143 00144 /* Generate overlap window */ 00145 if (!ff_sine_128[127]) 00146 ff_init_ff_sine_windows(7); 00147 00148 avctx->sample_fmt = AV_SAMPLE_FMT_S16; 00149 avctx->channel_layout = AV_CH_LAYOUT_MONO; 00150 return 0; 00151 } 00152 00153 static int decode_tag(AVCodecContext * avctx, 00154 void *data, int *data_size, 00155 AVPacket *avpkt) { 00156 const uint8_t *buf = avpkt->data; 00157 int buf_size = avpkt->size; 00158 NellyMoserDecodeContext *s = avctx->priv_data; 00159 int blocks, i, block_size; 00160 int16_t* samples; 00161 samples = (int16_t*)data; 00162 00163 if (buf_size < avctx->block_align) { 00164 *data_size = 0; 00165 return buf_size; 00166 } 00167 00168 if (buf_size % 64) { 00169 av_log(avctx, AV_LOG_ERROR, "Tag size %d.\n", buf_size); 00170 *data_size = 0; 00171 return buf_size; 00172 } 00173 block_size = NELLY_SAMPLES * av_get_bytes_per_sample(avctx->sample_fmt); 00174 blocks = FFMIN(buf_size / 64, *data_size / block_size); 00175 if (blocks <= 0) { 00176 av_log(avctx, AV_LOG_ERROR, "Output buffer is too small\n"); 00177 return AVERROR(EINVAL); 00178 } 00179 /* Normal numbers of blocks for sample rates: 00180 * 8000 Hz - 1 00181 * 11025 Hz - 2 00182 * 16000 Hz - 3 00183 * 22050 Hz - 4 00184 * 44100 Hz - 8 00185 */ 00186 00187 for (i=0 ; i<blocks ; i++) { 00188 nelly_decode_block(s, &buf[i*NELLY_BLOCK_LEN], s->float_buf); 00189 s->fmt_conv.float_to_int16(&samples[i*NELLY_SAMPLES], s->float_buf, NELLY_SAMPLES); 00190 } 00191 *data_size = blocks * block_size; 00192 00193 return buf_size; 00194 } 00195 00196 static av_cold int decode_end(AVCodecContext * avctx) { 00197 NellyMoserDecodeContext *s = avctx->priv_data; 00198 00199 ff_mdct_end(&s->imdct_ctx); 00200 return 0; 00201 } 00202 00203 AVCodec ff_nellymoser_decoder = { 00204 "nellymoser", 00205 AVMEDIA_TYPE_AUDIO, 00206 CODEC_ID_NELLYMOSER, 00207 sizeof(NellyMoserDecodeContext), 00208 decode_init, 00209 NULL, 00210 decode_end, 00211 decode_tag, 00212 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"), 00213 }; 00214