]> git.sesse.net Git - ffmpeg/blob - libavcodec/ws-snd1.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / ws-snd1.c
1 /*
2  * Westwood SNDx codecs
3  * Copyright (c) 2005 Konstantin Shishkov
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 #include <stdint.h>
23 #include "libavutil/intreadwrite.h"
24 #include "avcodec.h"
25
26 /**
27  * @file
28  * Westwood SNDx codecs
29  *
30  * Reference documents about VQA format and its audio codecs
31  * can be found here:
32  * http://www.multimedia.cx
33  */
34
35 static const int8_t ws_adpcm_4bit[] = {
36     -9, -8, -6, -5, -4, -3, -2, -1,
37      0,  1,  2,  3,  4,  5,  6,  8
38 };
39
40 static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
41 {
42     if (avctx->channels != 1) {
43         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
44         return AVERROR(EINVAL);
45     }
46
47     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
48     return 0;
49 }
50
51 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
52                                int *data_size, AVPacket *avpkt)
53 {
54     const uint8_t *buf = avpkt->data;
55     int buf_size       = avpkt->size;
56
57     int in_size, out_size;
58     int sample = 128;
59     uint8_t *samples = data;
60     uint8_t *samples_end;
61
62     if (!buf_size)
63         return 0;
64
65     if (buf_size < 4) {
66         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
67         return AVERROR(EINVAL);
68     }
69
70     out_size = AV_RL16(&buf[0]);
71     in_size  = AV_RL16(&buf[2]);
72     buf += 4;
73
74     if (out_size > *data_size) {
75         av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
76         return -1;
77     }
78     if (in_size > buf_size) {
79         av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
80         return -1;
81     }
82     samples_end = samples + out_size;
83
84     if (in_size == out_size) {
85         memcpy(samples, buf, out_size);
86         *data_size = out_size;
87         return buf_size;
88     }
89
90     while (samples < samples_end && buf - avpkt->data < buf_size) {
91         int code, smp, size;
92         uint8_t count;
93         code  = *buf >> 6;
94         count = *buf & 0x3F;
95         buf++;
96
97         /* make sure we don't write past the output buffer */
98         switch (code) {
99         case 0:  smp = 4;                              break;
100         case 1:  smp = 2;                              break;
101         case 2:  smp = (count & 0x20) ? 1 : count + 1; break;
102         default: smp = count + 1;                      break;
103         }
104         if (samples_end - samples < smp)
105             break;
106
107         /* make sure we don't read past the input buffer */
108         size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
109         if ((buf - avpkt->data) + size > buf_size)
110             break;
111
112         switch (code) {
113         case 0: /* ADPCM 2-bit */
114             for (count++; count > 0; count--) {
115                 code = *buf++;
116                 sample += ( code       & 0x3) - 2;
117                 sample = av_clip_uint8(sample);
118                 *samples++ = sample;
119                 sample += ((code >> 2) & 0x3) - 2;
120                 sample = av_clip_uint8(sample);
121                 *samples++ = sample;
122                 sample += ((code >> 4) & 0x3) - 2;
123                 sample = av_clip_uint8(sample);
124                 *samples++ = sample;
125                 sample +=  (code >> 6)        - 2;
126                 sample = av_clip_uint8(sample);
127                 *samples++ = sample;
128             }
129             break;
130         case 1: /* ADPCM 4-bit */
131             for (count++; count > 0; count--) {
132                 code = *buf++;
133                 sample += ws_adpcm_4bit[code & 0xF];
134                 sample = av_clip_uint8(sample);
135                 *samples++ = sample;
136                 sample += ws_adpcm_4bit[code >> 4];
137                 sample = av_clip_uint8(sample);
138                 *samples++ = sample;
139             }
140             break;
141         case 2: /* no compression */
142             if (count & 0x20) { /* big delta */
143                 int8_t t;
144                 t = count;
145                 t <<= 3;
146                 sample += t >> 3;
147                 sample = av_clip_uint8(sample);
148                 *samples++ = sample;
149             } else { /* copy */
150                 memcpy(samples, buf, smp);
151                 samples += smp;
152                 buf     += smp;
153                 sample = buf[-1];
154             }
155             break;
156         default: /* run */
157             memset(samples, sample, smp);
158             samples += smp;
159         }
160     }
161
162     *data_size = samples - (uint8_t *)data;
163
164     return buf_size;
165 }
166
167 AVCodec ff_ws_snd1_decoder = {
168     .name           = "ws_snd1",
169     .type           = AVMEDIA_TYPE_AUDIO,
170     .id             = CODEC_ID_WESTWOOD_SND1,
171     .init           = ws_snd_decode_init,
172     .decode         = ws_snd_decode_frame,
173     .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
174 };