]> git.sesse.net Git - ffmpeg/blob - libavcodec/8svx.c
Merge remote-tracking branch 'mans/dnxhd'
[ffmpeg] / libavcodec / 8svx.c
1 /*
2  * Copyright (C) 2008 Jaikrishnan Menon
3  * Copyright (C) 2011 Stefano Sabatini
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
24  * 8svx audio decoder
25  * @author Jaikrishnan Menon
26  *
27  * supports: fibonacci delta encoding
28  *         : exponential encoding
29  *
30  * For more information about the 8SVX format:
31  * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
32  * http://sox.sourceforge.net/AudioFormats-11.html
33  * http://aminet.net/package/mus/misc/wavepak
34  * http://amigan.1emu.net/reg/8SVX.txt
35  *
36  * Samples can be found here:
37  * http://aminet.net/mods/smpl/
38  */
39
40 #include "avcodec.h"
41
42 /** decoder context */
43 typedef struct EightSvxContext {
44     const int8_t *table;
45
46     /* buffer used to store the whole audio decoded/interleaved chunk,
47      * which is sent with the first packet */
48     uint8_t *samples;
49     size_t samples_size;
50     int samples_idx;
51 } EightSvxContext;
52
53 static const int8_t fibonacci[16]   = { -34,  -21, -13,  -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8,  13, 21 };
54 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
55
56 #define MAX_FRAME_SIZE 2048
57
58 /**
59  * Interleave samples in buffer containing all left channel samples
60  * at the beginning, and right channel samples at the end.
61  * Each sample is assumed to be in signed 8-bit format.
62  *
63  * @param size the size in bytes of the dst and src buffer
64  */
65 static void interleave_stereo(uint8_t *dst, const uint8_t *src, int size)
66 {
67     uint8_t *dst_end = dst + size;
68     size = size>>1;
69
70     while (dst < dst_end) {
71         *dst++ = *src;
72         *dst++ = *(src+size);
73         src++;
74     }
75 }
76
77 /**
78  * Delta decode the compressed values in src, and put the resulting
79  * decoded n samples in dst.
80  *
81  * @param val starting value assumed by the delta sequence
82  * @param table delta sequence table
83  * @return size in bytes of the decoded data, must be src_size*2
84  */
85 static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
86                         int8_t val, const int8_t *table)
87 {
88     int n = src_size;
89     int8_t *dst0 = dst;
90
91     while (n--) {
92         uint8_t d = *src++;
93         val = av_clip(val + table[d & 0x0f], -127, 128);
94         *dst++ = val;
95         val = av_clip(val + table[d >> 4]  , -127, 128);
96         *dst++ = val;
97     }
98
99     return dst-dst0;
100 }
101
102 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
103                                  AVPacket *avpkt)
104 {
105     EightSvxContext *esc = avctx->priv_data;
106     int out_data_size, n;
107     uint8_t *src, *dst;
108
109     /* decode and interleave the first packet */
110     if (!esc->samples && avpkt) {
111         uint8_t *deinterleaved_samples;
112
113         esc->samples_size = avctx->codec->id == CODEC_ID_8SVX_RAW ?
114             avpkt->size : avctx->channels + (avpkt->size-avctx->channels) * 2;
115         if (!(esc->samples = av_malloc(esc->samples_size)))
116             return AVERROR(ENOMEM);
117
118         /* decompress */
119         if (avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP) {
120             const uint8_t *buf = avpkt->data;
121             int buf_size = avpkt->size;
122             int n = esc->samples_size;
123
124             if (buf_size < 2) {
125                 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
126                 return AVERROR(EINVAL);
127             }
128             if (!(deinterleaved_samples = av_mallocz(n)))
129                 return AVERROR(ENOMEM);
130
131             /* the uncompressed starting value is contained in the first byte */
132             if (avctx->channels == 2) {
133                 delta_decode(deinterleaved_samples      , buf+1, buf_size/2-1, buf[0], esc->table);
134                 buf += buf_size/2;
135                 delta_decode(deinterleaved_samples+n/2-1, buf+1, buf_size/2-1, buf[0], esc->table);
136             } else
137                 delta_decode(deinterleaved_samples      , buf+1, buf_size-1  , buf[0], esc->table);
138         } else {
139             deinterleaved_samples = avpkt->data;
140         }
141
142         if (avctx->channels == 2)
143             interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
144         else
145             memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
146     }
147
148     /* return single packed with fixed size */
149     out_data_size = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx);
150     if (*data_size < out_data_size) {
151         av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n", *data_size);
152         return AVERROR(EINVAL);
153     }
154
155     *data_size = out_data_size;
156     dst = data;
157     src = esc->samples + esc->samples_idx;
158     for (n = out_data_size; n > 0; n--)
159         *dst++ = *src++ + 128;
160     esc->samples_idx += *data_size;
161
162     return avctx->codec->id == CODEC_ID_8SVX_FIB || avctx->codec->id == CODEC_ID_8SVX_EXP ?
163         (avctx->frame_number == 0)*2 + out_data_size / 2 :
164         out_data_size;
165 }
166
167 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
168 {
169     EightSvxContext *esc = avctx->priv_data;
170
171     if (avctx->channels > 2) {
172         av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
173         return AVERROR_INVALIDDATA;
174     }
175
176     switch (avctx->codec->id) {
177     case CODEC_ID_8SVX_FIB: esc->table = fibonacci;    break;
178     case CODEC_ID_8SVX_EXP: esc->table = exponential;  break;
179     case CODEC_ID_8SVX_RAW: esc->table = NULL;         break;
180     default:
181         av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
182         return AVERROR_INVALIDDATA;
183     }
184     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
185
186     return 0;
187 }
188
189 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
190 {
191     EightSvxContext *esc = avctx->priv_data;
192
193     av_freep(&esc->samples);
194     esc->samples_size = 0;
195     esc->samples_idx = 0;
196
197     return 0;
198 }
199
200 AVCodec ff_eightsvx_fib_decoder = {
201   .name           = "8svx_fib",
202   .type           = AVMEDIA_TYPE_AUDIO,
203   .id             = CODEC_ID_8SVX_FIB,
204   .priv_data_size = sizeof (EightSvxContext),
205   .init           = eightsvx_decode_init,
206   .decode         = eightsvx_decode_frame,
207   .close          = eightsvx_decode_close,
208   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
209 };
210
211 AVCodec ff_eightsvx_exp_decoder = {
212   .name           = "8svx_exp",
213   .type           = AVMEDIA_TYPE_AUDIO,
214   .id             = CODEC_ID_8SVX_EXP,
215   .priv_data_size = sizeof (EightSvxContext),
216   .init           = eightsvx_decode_init,
217   .decode         = eightsvx_decode_frame,
218   .close          = eightsvx_decode_close,
219   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
220 };
221
222 AVCodec ff_eightsvx_raw_decoder = {
223   .name           = "8svx_raw",
224   .type           = AVMEDIA_TYPE_AUDIO,
225   .id             = CODEC_ID_8SVX_RAW,
226   .priv_data_size = sizeof(EightSvxContext),
227   .init           = eightsvx_decode_init,
228   .decode         = eightsvx_decode_frame,
229   .close          = eightsvx_decode_close,
230   .long_name      = NULL_IF_CONFIG_SMALL("8SVX rawaudio"),
231 };