8svx.c

Go to the documentation of this file.
00001 /*
00002  * 8SVX audio decoder
00003  * Copyright (C) 2008 Jaikrishnan Menon
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 /**
00023  * @file 8svx.c
00024  * 8svx audio decoder
00025  * @author Jaikrishnan Menon
00026  * supports: fibonacci delta encoding
00027  *         : exponential encoding
00028  */
00029 
00030 #include "avcodec.h"
00031 
00032 /** decoder context */
00033 typedef struct EightSvxContext {
00034     int16_t fib_acc;
00035     int16_t *table;
00036 } EightSvxContext;
00037 
00038 const static int16_t fibonacci[16]   = { -34<<8, -21<<8, -13<<8,  -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
00039                                           0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
00040 const static int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
00041                                           0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
00042 
00043 /** decode a frame */
00044 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
00045                                  const uint8_t *buf, int buf_size)
00046 {
00047     EightSvxContext *esc = avctx->priv_data;
00048     int16_t *out_data = data;
00049     int consumed = buf_size;
00050     const uint8_t *buf_end = buf + buf_size;
00051 
00052     if((*data_size >> 2) < buf_size)
00053         return -1;
00054 
00055     if(avctx->frame_number == 0) {
00056         esc->fib_acc = buf[1] << 8;
00057         buf_size -= 2;
00058         buf += 2;
00059     }
00060 
00061     *data_size = buf_size << 2;
00062 
00063     while(buf < buf_end) {
00064         uint8_t d = *buf++;
00065         esc->fib_acc += esc->table[d & 0x0f];
00066         *out_data++ = esc->fib_acc;
00067         esc->fib_acc += esc->table[d >> 4];
00068         *out_data++ = esc->fib_acc;
00069     }
00070 
00071     return consumed;
00072 }
00073 
00074 /** initialize 8svx decoder */
00075 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
00076 {
00077     EightSvxContext *esc = avctx->priv_data;
00078 
00079     switch(avctx->codec->id) {
00080         case CODEC_ID_8SVX_FIB:
00081           esc->table = fibonacci;
00082           break;
00083         case CODEC_ID_8SVX_EXP:
00084           esc->table = exponential;
00085           break;
00086         default:
00087           return -1;
00088     }
00089     return 0;
00090 }
00091 
00092 AVCodec eightsvx_fib_decoder = {
00093   .name           = "8svx_fib",
00094   .type           = CODEC_TYPE_AUDIO,
00095   .id             = CODEC_ID_8SVX_FIB,
00096   .priv_data_size = sizeof (EightSvxContext),
00097   .init           = eightsvx_decode_init,
00098   .decode         = eightsvx_decode_frame,
00099   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
00100 };
00101 
00102 AVCodec eightsvx_exp_decoder = {
00103   .name           = "8svx_exp",
00104   .type           = CODEC_TYPE_AUDIO,
00105   .id             = CODEC_ID_8SVX_EXP,
00106   .priv_data_size = sizeof (EightSvxContext),
00107   .init           = eightsvx_decode_init,
00108   .decode         = eightsvx_decode_frame,
00109   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
00110 };

Generated on Thu Aug 28 16:44:14 2008 for libextractor by  doxygen 1.5.1