annotate ffmpeg/libavcodec/nellymoserdec.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * NellyMoser audio decoder
yading@10 3 * Copyright (c) 2007 a840bda5870ba11f19698ff6eb9581dfb0f95fa5,
yading@10 4 * 539459aeb7d425140b62a3ec7dbf6dc8e408a306, and
yading@10 5 * 520e17cd55896441042b14df2566a6eb610ed444
yading@10 6 * Copyright (c) 2007 Loic Minier <lool at dooz.org>
yading@10 7 * Benjamin Larsson
yading@10 8 *
yading@10 9 * Permission is hereby granted, free of charge, to any person obtaining a
yading@10 10 * copy of this software and associated documentation files (the "Software"),
yading@10 11 * to deal in the Software without restriction, including without limitation
yading@10 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
yading@10 13 * and/or sell copies of the Software, and to permit persons to whom the
yading@10 14 * Software is furnished to do so, subject to the following conditions:
yading@10 15 *
yading@10 16 * The above copyright notice and this permission notice shall be included in
yading@10 17 * all copies or substantial portions of the Software.
yading@10 18 *
yading@10 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yading@10 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yading@10 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
yading@10 22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yading@10 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
yading@10 24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
yading@10 25 * DEALINGS IN THE SOFTWARE.
yading@10 26 */
yading@10 27
yading@10 28 /**
yading@10 29 * @file
yading@10 30 * The 3 alphanumeric copyright notices are md5summed they are from the original
yading@10 31 * implementors. The original code is available from http://code.google.com/p/nelly2pcm/
yading@10 32 */
yading@10 33
yading@10 34 #include "libavutil/channel_layout.h"
yading@10 35 #include "libavutil/float_dsp.h"
yading@10 36 #include "libavutil/lfg.h"
yading@10 37 #include "libavutil/random_seed.h"
yading@10 38 #include "avcodec.h"
yading@10 39 #include "fft.h"
yading@10 40 #include "fmtconvert.h"
yading@10 41 #include "internal.h"
yading@10 42 #include "nellymoser.h"
yading@10 43 #include "sinewin.h"
yading@10 44
yading@10 45 #define BITSTREAM_READER_LE
yading@10 46 #include "get_bits.h"
yading@10 47
yading@10 48
yading@10 49 typedef struct NellyMoserDecodeContext {
yading@10 50 AVCodecContext* avctx;
yading@10 51 AVLFG random_state;
yading@10 52 GetBitContext gb;
yading@10 53 float scale_bias;
yading@10 54 AVFloatDSPContext fdsp;
yading@10 55 FFTContext imdct_ctx;
yading@10 56 DECLARE_ALIGNED(32, float, imdct_buf)[2][NELLY_BUF_LEN];
yading@10 57 float *imdct_out;
yading@10 58 float *imdct_prev;
yading@10 59 } NellyMoserDecodeContext;
yading@10 60
yading@10 61 static void nelly_decode_block(NellyMoserDecodeContext *s,
yading@10 62 const unsigned char block[NELLY_BLOCK_LEN],
yading@10 63 float audio[NELLY_SAMPLES])
yading@10 64 {
yading@10 65 int i,j;
yading@10 66 float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];
yading@10 67 float *aptr, *bptr, *pptr, val, pval;
yading@10 68 int bits[NELLY_BUF_LEN];
yading@10 69 unsigned char v;
yading@10 70
yading@10 71 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
yading@10 72
yading@10 73 bptr = buf;
yading@10 74 pptr = pows;
yading@10 75 val = ff_nelly_init_table[get_bits(&s->gb, 6)];
yading@10 76 for (i=0 ; i<NELLY_BANDS ; i++) {
yading@10 77 if (i > 0)
yading@10 78 val += ff_nelly_delta_table[get_bits(&s->gb, 5)];
yading@10 79 pval = -pow(2, val/2048) * s->scale_bias;
yading@10 80 for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) {
yading@10 81 *bptr++ = val;
yading@10 82 *pptr++ = pval;
yading@10 83 }
yading@10 84
yading@10 85 }
yading@10 86
yading@10 87 ff_nelly_get_sample_bits(buf, bits);
yading@10 88
yading@10 89 for (i = 0; i < 2; i++) {
yading@10 90 aptr = audio + i * NELLY_BUF_LEN;
yading@10 91
yading@10 92 init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);
yading@10 93 skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);
yading@10 94
yading@10 95 for (j = 0; j < NELLY_FILL_LEN; j++) {
yading@10 96 if (bits[j] <= 0) {
yading@10 97 aptr[j] = M_SQRT1_2*pows[j];
yading@10 98 if (av_lfg_get(&s->random_state) & 1)
yading@10 99 aptr[j] *= -1.0;
yading@10 100 } else {
yading@10 101 v = get_bits(&s->gb, bits[j]);
yading@10 102 aptr[j] = ff_nelly_dequantization_table[(1<<bits[j])-1+v]*pows[j];
yading@10 103 }
yading@10 104 }
yading@10 105 memset(&aptr[NELLY_FILL_LEN], 0,
yading@10 106 (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));
yading@10 107
yading@10 108 s->imdct_ctx.imdct_half(&s->imdct_ctx, s->imdct_out, aptr);
yading@10 109 s->fdsp.vector_fmul_window(aptr, s->imdct_prev + NELLY_BUF_LEN / 2,
yading@10 110 s->imdct_out, ff_sine_128,
yading@10 111 NELLY_BUF_LEN / 2);
yading@10 112 FFSWAP(float *, s->imdct_out, s->imdct_prev);
yading@10 113 }
yading@10 114 }
yading@10 115
yading@10 116 static av_cold int decode_init(AVCodecContext * avctx) {
yading@10 117 NellyMoserDecodeContext *s = avctx->priv_data;
yading@10 118
yading@10 119 s->avctx = avctx;
yading@10 120 s->imdct_out = s->imdct_buf[0];
yading@10 121 s->imdct_prev = s->imdct_buf[1];
yading@10 122 av_lfg_init(&s->random_state, 0);
yading@10 123 ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0);
yading@10 124
yading@10 125 avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT);
yading@10 126
yading@10 127 s->scale_bias = 1.0/(32768*8);
yading@10 128 avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
yading@10 129
yading@10 130 /* Generate overlap window */
yading@10 131 if (!ff_sine_128[127])
yading@10 132 ff_init_ff_sine_windows(7);
yading@10 133
yading@10 134 avctx->channels = 1;
yading@10 135 avctx->channel_layout = AV_CH_LAYOUT_MONO;
yading@10 136
yading@10 137 return 0;
yading@10 138 }
yading@10 139
yading@10 140 static int decode_tag(AVCodecContext *avctx, void *data,
yading@10 141 int *got_frame_ptr, AVPacket *avpkt)
yading@10 142 {
yading@10 143 AVFrame *frame = data;
yading@10 144 const uint8_t *buf = avpkt->data;
yading@10 145 const uint8_t *side=av_packet_get_side_data(avpkt, 'F', NULL);
yading@10 146 int buf_size = avpkt->size;
yading@10 147 NellyMoserDecodeContext *s = avctx->priv_data;
yading@10 148 int blocks, i, ret;
yading@10 149 float *samples_flt;
yading@10 150
yading@10 151 blocks = buf_size / NELLY_BLOCK_LEN;
yading@10 152
yading@10 153 if (blocks <= 0) {
yading@10 154 av_log(avctx, AV_LOG_ERROR, "Packet is too small\n");
yading@10 155 return AVERROR_INVALIDDATA;
yading@10 156 }
yading@10 157
yading@10 158 if (buf_size % NELLY_BLOCK_LEN) {
yading@10 159 av_log(avctx, AV_LOG_WARNING, "Leftover bytes: %d.\n",
yading@10 160 buf_size % NELLY_BLOCK_LEN);
yading@10 161 }
yading@10 162 /* Normal numbers of blocks for sample rates:
yading@10 163 * 8000 Hz - 1
yading@10 164 * 11025 Hz - 2
yading@10 165 * 16000 Hz - 3
yading@10 166 * 22050 Hz - 4
yading@10 167 * 44100 Hz - 8
yading@10 168 */
yading@10 169 if(side && blocks>1 && avctx->sample_rate%11025==0 && (1<<((side[0]>>2)&3)) == blocks)
yading@10 170 avctx->sample_rate= 11025*(blocks/2);
yading@10 171
yading@10 172 /* get output buffer */
yading@10 173 frame->nb_samples = NELLY_SAMPLES * blocks;
yading@10 174 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
yading@10 175 return ret;
yading@10 176 samples_flt = (float *)frame->data[0];
yading@10 177
yading@10 178 for (i=0 ; i<blocks ; i++) {
yading@10 179 nelly_decode_block(s, buf, samples_flt);
yading@10 180 samples_flt += NELLY_SAMPLES;
yading@10 181 buf += NELLY_BLOCK_LEN;
yading@10 182 }
yading@10 183
yading@10 184 *got_frame_ptr = 1;
yading@10 185
yading@10 186 return buf_size;
yading@10 187 }
yading@10 188
yading@10 189 static av_cold int decode_end(AVCodecContext * avctx) {
yading@10 190 NellyMoserDecodeContext *s = avctx->priv_data;
yading@10 191
yading@10 192 ff_mdct_end(&s->imdct_ctx);
yading@10 193
yading@10 194 return 0;
yading@10 195 }
yading@10 196
yading@10 197 AVCodec ff_nellymoser_decoder = {
yading@10 198 .name = "nellymoser",
yading@10 199 .type = AVMEDIA_TYPE_AUDIO,
yading@10 200 .id = AV_CODEC_ID_NELLYMOSER,
yading@10 201 .priv_data_size = sizeof(NellyMoserDecodeContext),
yading@10 202 .init = decode_init,
yading@10 203 .close = decode_end,
yading@10 204 .decode = decode_tag,
yading@10 205 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE,
yading@10 206 .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"),
yading@10 207 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
yading@10 208 AV_SAMPLE_FMT_NONE },
yading@10 209 };