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