8SVX decoder
[libav.git] / libavcodec / 8svx.c
CommitLineData
cb21e0a7
JM
1/*
2 * 8SVX Audio Decoder
3 * Copyright (C) 2008 Jaikrishnan Menon
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file 8svx.c
24 * 8svx audio decoder
25 * @author Jaikrishnan Menon
26 * supports: fibonacci delta encoding
27 * : exponential encoding
28 */
29
30#include "avcodec.h"
31
32/**
33 * decoder context
34 */
35typedef struct EightSvxContext {
36 int16_t fib_acc;
37 int16_t *table;
38} EightSvxContext;
39
40const static int16_t fibonacci[16] = { -34<<8, -21<<8, -13<<8, -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
41 0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
42const static int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
43 0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
44
45/**
46 * decode a frame
47 */
48static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
49 const uint8_t *buf, int buf_size)
50{
51 EightSvxContext *esc = avctx->priv_data;
52 int16_t *out_data = data;
53 int consumed = buf_size;
54 const uint8_t *buf_end = buf + buf_size;
55
56 if((*data_size >> 2) < buf_size)
57 return -1;
58
59 if(avctx->frame_number == 0) {
60 esc->fib_acc = buf[1] << 8;
61 buf_size -= 2;
62 buf += 2;
63 }
64
65 *data_size = buf_size << 2;
66
67 while(buf < buf_end) {
68 uint8_t d = *buf++;
69 esc->fib_acc += esc->table[d & 0x0f];
70 *out_data++ = esc->fib_acc;
71 esc->fib_acc += esc->table[d >> 4];
72 *out_data++ = esc->fib_acc;
73 }
74
75 return consumed;
76}
77
78/**
79 * initialize 8svx decoder
80 */
81static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
82{
83 EightSvxContext *esc = avctx->priv_data;
84
85 switch(avctx->codec->id) {
86 case CODEC_ID_8SVX_FIB:
87 esc->table = fibonacci;
88 break;
89 case CODEC_ID_8SVX_EXP:
90 esc->table = exponential;
91 break;
92 default:
93 return -1;
94 }
95 return 0;
96}
97
98AVCodec eightsvx_fib_decoder = {
99 .name = "8svx fibonacci decoder",
100 .type = CODEC_TYPE_AUDIO,
101 .id = CODEC_ID_8SVX_FIB,
102 .priv_data_size = sizeof (EightSvxContext),
103 .init = eightsvx_decode_init,
104 .decode = eightsvx_decode_frame,
105};
106
107AVCodec eightsvx_exp_decoder = {
108 .name = "8svx exponential decoder",
109 .type = CODEC_TYPE_AUDIO,
110 .id = CODEC_ID_8SVX_EXP,
111 .priv_data_size = sizeof (EightSvxContext),
112 .init = eightsvx_decode_init,
113 .decode = eightsvx_decode_frame,
114};