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