]> git.sesse.net Git - ffmpeg/blob - libavcodec/8svx.c
Merge commit '117d8c6d1f1c187ffc6098d9618457e00534e013'
[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 "libavutil/avassert.h"
41 #include "avcodec.h"
42 #include "libavutil/common.h"
43
44 /** decoder context */
45 typedef struct EightSvxContext {
46     AVFrame frame;
47     const int8_t *table;
48
49     /* buffer used to store the whole audio decoded/interleaved chunk,
50      * which is sent with the first packet */
51     uint8_t *samples;
52     int64_t samples_size;
53     int samples_idx;
54 } EightSvxContext;
55
56 static const int8_t fibonacci[16]   = { -34,  -21, -13,  -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8,  13, 21 };
57 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
58
59 #define MAX_FRAME_SIZE 2048
60
61 /**
62  * Interleave samples in buffer containing all left channel samples
63  * at the beginning, and right channel samples at the end.
64  * Each sample is assumed to be in signed 8-bit format.
65  *
66  * @param size the size in bytes of the dst and src buffer
67  */
68 static void interleave_stereo(uint8_t *dst, const uint8_t *src, int size)
69 {
70     uint8_t *dst_end = dst + size;
71     size = size>>1;
72
73     while (dst < dst_end) {
74         *dst++ = *src;
75         *dst++ = *(src+size);
76         src++;
77     }
78 }
79
80 /**
81  * Delta decode the compressed values in src, and put the resulting
82  * decoded n samples in dst.
83  *
84  * @param val starting value assumed by the delta sequence
85  * @param table delta sequence table
86  * @return size in bytes of the decoded data, must be src_size*2
87  */
88 static int delta_decode(int8_t *dst, const uint8_t *src, int src_size,
89                         int8_t val, const int8_t *table)
90 {
91     int n = src_size;
92     int8_t *dst0 = dst;
93
94     while (n--) {
95         uint8_t d = *src++;
96         val = av_clip(val + table[d & 0x0f], -127, 128);
97         *dst++ = val;
98         val = av_clip(val + table[d >> 4]  , -127, 128);
99         *dst++ = val;
100     }
101
102     return dst-dst0;
103 }
104
105 /** decode a frame */
106 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
107                                  int *got_frame_ptr, AVPacket *avpkt)
108 {
109     EightSvxContext *esc = avctx->priv_data;
110     int n, out_data_size, ret;
111     uint8_t *src, *dst;
112
113     /* decode and interleave the first packet */
114     if (!esc->samples && avpkt) {
115         uint8_t *deinterleaved_samples, *p = NULL;
116         int packet_size = avpkt->size;
117
118         if (packet_size % avctx->channels) {
119             av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
120             if (packet_size < avctx->channels)
121                 return packet_size;
122             packet_size -= packet_size % avctx->channels;
123         }
124         esc->samples_size = !esc->table ?
125             packet_size : avctx->channels + (packet_size-avctx->channels) * 2;
126         if (!(esc->samples = av_malloc(esc->samples_size)))
127             return AVERROR(ENOMEM);
128
129         /* decompress */
130         if (esc->table) {
131             const uint8_t *buf = avpkt->data;
132             uint8_t *dst;
133             int buf_size = avpkt->size;
134             int i, n = esc->samples_size;
135
136             if (buf_size < 2) {
137                 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
138                 return AVERROR(EINVAL);
139             }
140             if (!(deinterleaved_samples = av_mallocz(n)))
141                 return AVERROR(ENOMEM);
142             dst = p = deinterleaved_samples;
143
144             /* the uncompressed starting value is contained in the first byte */
145             dst = deinterleaved_samples;
146             for (i = 0; i < avctx->channels; i++) {
147                 delta_decode(dst, buf + 1, buf_size / avctx->channels - 1, buf[0], esc->table);
148                 buf += buf_size / avctx->channels;
149                 dst += n / avctx->channels - 1;
150             }
151         } else {
152             deinterleaved_samples = avpkt->data;
153         }
154
155         if (avctx->channels == 2)
156             interleave_stereo(esc->samples, deinterleaved_samples, esc->samples_size);
157         else
158             memcpy(esc->samples, deinterleaved_samples, esc->samples_size);
159         av_freep(&p);
160     }
161
162     /* get output buffer */
163     av_assert1(!(esc->samples_size % avctx->channels || esc->samples_idx % avctx->channels));
164     esc->frame.nb_samples = FFMIN(MAX_FRAME_SIZE, esc->samples_size - esc->samples_idx)  / avctx->channels;
165     if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
166         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
167         return ret;
168     }
169
170     *got_frame_ptr   = 1;
171     *(AVFrame *)data = esc->frame;
172
173     dst = esc->frame.data[0];
174     src = esc->samples + esc->samples_idx;
175     out_data_size = esc->frame.nb_samples * avctx->channels;
176     for (n = out_data_size; n > 0; n--)
177         *dst++ = *src++ + 128;
178     esc->samples_idx += out_data_size;
179
180     return esc->table ?
181         (avctx->frame_number == 0)*2 + out_data_size / 2 :
182         out_data_size;
183 }
184
185 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
186 {
187     EightSvxContext *esc = avctx->priv_data;
188
189     if (avctx->channels < 1 || avctx->channels > 2) {
190         av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
191         return AVERROR_INVALIDDATA;
192     }
193
194     switch (avctx->codec->id) {
195     case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci;    break;
196     case AV_CODEC_ID_8SVX_EXP: esc->table = exponential;  break;
197     case AV_CODEC_ID_PCM_S8_PLANAR:
198     case AV_CODEC_ID_8SVX_RAW: esc->table = NULL;         break;
199     default:
200         av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
201         return AVERROR_INVALIDDATA;
202     }
203     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
204
205     avcodec_get_frame_defaults(&esc->frame);
206     avctx->coded_frame = &esc->frame;
207
208     return 0;
209 }
210
211 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
212 {
213     EightSvxContext *esc = avctx->priv_data;
214
215     av_freep(&esc->samples);
216     esc->samples_size = 0;
217     esc->samples_idx = 0;
218
219     return 0;
220 }
221
222 #if CONFIG_EIGHTSVX_FIB_DECODER
223 AVCodec ff_eightsvx_fib_decoder = {
224   .name           = "8svx_fib",
225   .type           = AVMEDIA_TYPE_AUDIO,
226   .id             = AV_CODEC_ID_8SVX_FIB,
227   .priv_data_size = sizeof (EightSvxContext),
228   .init           = eightsvx_decode_init,
229   .decode         = eightsvx_decode_frame,
230   .close          = eightsvx_decode_close,
231   .capabilities   = CODEC_CAP_DR1,
232   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
233 };
234 #endif
235 #if CONFIG_EIGHTSVX_EXP_DECODER
236 AVCodec ff_eightsvx_exp_decoder = {
237   .name           = "8svx_exp",
238   .type           = AVMEDIA_TYPE_AUDIO,
239   .id             = AV_CODEC_ID_8SVX_EXP,
240   .priv_data_size = sizeof (EightSvxContext),
241   .init           = eightsvx_decode_init,
242   .decode         = eightsvx_decode_frame,
243   .close          = eightsvx_decode_close,
244   .capabilities   = CODEC_CAP_DR1,
245   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
246 };
247 #endif
248 #if CONFIG_PCM_S8_PLANAR_DECODER
249 AVCodec ff_pcm_s8_planar_decoder = {
250     .name           = "pcm_s8_planar",
251     .type           = AVMEDIA_TYPE_AUDIO,
252     .id             = AV_CODEC_ID_PCM_S8_PLANAR,
253     .priv_data_size = sizeof(EightSvxContext),
254     .init           = eightsvx_decode_init,
255     .close          = eightsvx_decode_close,
256     .decode         = eightsvx_decode_frame,
257     .capabilities   = CODEC_CAP_DR1,
258     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
259 };
260 #endif