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