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