yading@10: /* yading@10: * NellyMoser audio decoder yading@10: * Copyright (c) 2007 a840bda5870ba11f19698ff6eb9581dfb0f95fa5, yading@10: * 539459aeb7d425140b62a3ec7dbf6dc8e408a306, and yading@10: * 520e17cd55896441042b14df2566a6eb610ed444 yading@10: * Copyright (c) 2007 Loic Minier yading@10: * Benjamin Larsson yading@10: * yading@10: * Permission is hereby granted, free of charge, to any person obtaining a yading@10: * copy of this software and associated documentation files (the "Software"), yading@10: * to deal in the Software without restriction, including without limitation yading@10: * the rights to use, copy, modify, merge, publish, distribute, sublicense, yading@10: * and/or sell copies of the Software, and to permit persons to whom the yading@10: * Software is furnished to do so, subject to the following conditions: yading@10: * yading@10: * The above copyright notice and this permission notice shall be included in yading@10: * all copies or substantial portions of the Software. yading@10: * yading@10: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR yading@10: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, yading@10: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL yading@10: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER yading@10: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING yading@10: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER yading@10: * DEALINGS IN THE SOFTWARE. yading@10: */ yading@10: yading@10: /** yading@10: * @file yading@10: * The 3 alphanumeric copyright notices are md5summed they are from the original yading@10: * implementors. The original code is available from http://code.google.com/p/nelly2pcm/ yading@10: */ yading@10: yading@10: #include "libavutil/channel_layout.h" yading@10: #include "libavutil/float_dsp.h" yading@10: #include "libavutil/lfg.h" yading@10: #include "libavutil/random_seed.h" yading@10: #include "avcodec.h" yading@10: #include "fft.h" yading@10: #include "fmtconvert.h" yading@10: #include "internal.h" yading@10: #include "nellymoser.h" yading@10: #include "sinewin.h" yading@10: yading@10: #define BITSTREAM_READER_LE yading@10: #include "get_bits.h" yading@10: yading@10: yading@10: typedef struct NellyMoserDecodeContext { yading@10: AVCodecContext* avctx; yading@10: AVLFG random_state; yading@10: GetBitContext gb; yading@10: float scale_bias; yading@10: AVFloatDSPContext fdsp; yading@10: FFTContext imdct_ctx; yading@10: DECLARE_ALIGNED(32, float, imdct_buf)[2][NELLY_BUF_LEN]; yading@10: float *imdct_out; yading@10: float *imdct_prev; yading@10: } NellyMoserDecodeContext; yading@10: yading@10: static void nelly_decode_block(NellyMoserDecodeContext *s, yading@10: const unsigned char block[NELLY_BLOCK_LEN], yading@10: float audio[NELLY_SAMPLES]) yading@10: { yading@10: int i,j; yading@10: float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN]; yading@10: float *aptr, *bptr, *pptr, val, pval; yading@10: int bits[NELLY_BUF_LEN]; yading@10: unsigned char v; yading@10: yading@10: init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); yading@10: yading@10: bptr = buf; yading@10: pptr = pows; yading@10: val = ff_nelly_init_table[get_bits(&s->gb, 6)]; yading@10: for (i=0 ; i 0) yading@10: val += ff_nelly_delta_table[get_bits(&s->gb, 5)]; yading@10: pval = -pow(2, val/2048) * s->scale_bias; yading@10: for (j = 0; j < ff_nelly_band_sizes_table[i]; j++) { yading@10: *bptr++ = val; yading@10: *pptr++ = pval; yading@10: } yading@10: yading@10: } yading@10: yading@10: ff_nelly_get_sample_bits(buf, bits); yading@10: yading@10: for (i = 0; i < 2; i++) { yading@10: aptr = audio + i * NELLY_BUF_LEN; yading@10: yading@10: init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8); yading@10: skip_bits_long(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS); yading@10: yading@10: for (j = 0; j < NELLY_FILL_LEN; j++) { yading@10: if (bits[j] <= 0) { yading@10: aptr[j] = M_SQRT1_2*pows[j]; yading@10: if (av_lfg_get(&s->random_state) & 1) yading@10: aptr[j] *= -1.0; yading@10: } else { yading@10: v = get_bits(&s->gb, bits[j]); yading@10: aptr[j] = ff_nelly_dequantization_table[(1<imdct_ctx.imdct_half(&s->imdct_ctx, s->imdct_out, aptr); yading@10: s->fdsp.vector_fmul_window(aptr, s->imdct_prev + NELLY_BUF_LEN / 2, yading@10: s->imdct_out, ff_sine_128, yading@10: NELLY_BUF_LEN / 2); yading@10: FFSWAP(float *, s->imdct_out, s->imdct_prev); yading@10: } yading@10: } yading@10: yading@10: static av_cold int decode_init(AVCodecContext * avctx) { yading@10: NellyMoserDecodeContext *s = avctx->priv_data; yading@10: yading@10: s->avctx = avctx; yading@10: s->imdct_out = s->imdct_buf[0]; yading@10: s->imdct_prev = s->imdct_buf[1]; yading@10: av_lfg_init(&s->random_state, 0); yading@10: ff_mdct_init(&s->imdct_ctx, 8, 1, 1.0); yading@10: yading@10: avpriv_float_dsp_init(&s->fdsp, avctx->flags & CODEC_FLAG_BITEXACT); yading@10: yading@10: s->scale_bias = 1.0/(32768*8); yading@10: avctx->sample_fmt = AV_SAMPLE_FMT_FLT; yading@10: yading@10: /* Generate overlap window */ yading@10: if (!ff_sine_128[127]) yading@10: ff_init_ff_sine_windows(7); yading@10: yading@10: avctx->channels = 1; yading@10: avctx->channel_layout = AV_CH_LAYOUT_MONO; yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: static int decode_tag(AVCodecContext *avctx, void *data, yading@10: int *got_frame_ptr, AVPacket *avpkt) yading@10: { yading@10: AVFrame *frame = data; yading@10: const uint8_t *buf = avpkt->data; yading@10: const uint8_t *side=av_packet_get_side_data(avpkt, 'F', NULL); yading@10: int buf_size = avpkt->size; yading@10: NellyMoserDecodeContext *s = avctx->priv_data; yading@10: int blocks, i, ret; yading@10: float *samples_flt; yading@10: yading@10: blocks = buf_size / NELLY_BLOCK_LEN; yading@10: yading@10: if (blocks <= 0) { yading@10: av_log(avctx, AV_LOG_ERROR, "Packet is too small\n"); yading@10: return AVERROR_INVALIDDATA; yading@10: } yading@10: yading@10: if (buf_size % NELLY_BLOCK_LEN) { yading@10: av_log(avctx, AV_LOG_WARNING, "Leftover bytes: %d.\n", yading@10: buf_size % NELLY_BLOCK_LEN); yading@10: } yading@10: /* Normal numbers of blocks for sample rates: yading@10: * 8000 Hz - 1 yading@10: * 11025 Hz - 2 yading@10: * 16000 Hz - 3 yading@10: * 22050 Hz - 4 yading@10: * 44100 Hz - 8 yading@10: */ yading@10: if(side && blocks>1 && avctx->sample_rate%11025==0 && (1<<((side[0]>>2)&3)) == blocks) yading@10: avctx->sample_rate= 11025*(blocks/2); yading@10: yading@10: /* get output buffer */ yading@10: frame->nb_samples = NELLY_SAMPLES * blocks; yading@10: if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) yading@10: return ret; yading@10: samples_flt = (float *)frame->data[0]; yading@10: yading@10: for (i=0 ; ipriv_data; yading@10: yading@10: ff_mdct_end(&s->imdct_ctx); yading@10: yading@10: return 0; yading@10: } yading@10: yading@10: AVCodec ff_nellymoser_decoder = { yading@10: .name = "nellymoser", yading@10: .type = AVMEDIA_TYPE_AUDIO, yading@10: .id = AV_CODEC_ID_NELLYMOSER, yading@10: .priv_data_size = sizeof(NellyMoserDecodeContext), yading@10: .init = decode_init, yading@10: .close = decode_end, yading@10: .decode = decode_tag, yading@10: .capabilities = CODEC_CAP_DR1 | CODEC_CAP_PARAM_CHANGE, yading@10: .long_name = NULL_IF_CONFIG_SMALL("Nellymoser Asao"), yading@10: .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT, yading@10: AV_SAMPLE_FMT_NONE }, yading@10: };