1/*
2 * Westwood SNDx codecs
3 * Copyright (c) 2005 Konstantin Shishkov
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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 * Libav 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 Libav; 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
35static 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
40typedef struct WSSndContext {
41    AVFrame frame;
42} WSSndContext;
43
44static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
45{
46    WSSndContext *s = avctx->priv_data;
47
48    if (avctx->channels != 1) {
49        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
50        return AVERROR(EINVAL);
51    }
52
53    avctx->sample_fmt = AV_SAMPLE_FMT_U8;
54
55    avcodec_get_frame_defaults(&s->frame);
56    avctx->coded_frame = &s->frame;
57
58    return 0;
59}
60
61static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
62                               int *got_frame_ptr, AVPacket *avpkt)
63{
64    WSSndContext *s = avctx->priv_data;
65    const uint8_t *buf = avpkt->data;
66    int buf_size       = avpkt->size;
67
68    int in_size, out_size, ret;
69    int sample = 128;
70    uint8_t *samples;
71    uint8_t *samples_end;
72
73    if (!buf_size)
74        return 0;
75
76    if (buf_size < 4) {
77        av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
78        return AVERROR(EINVAL);
79    }
80
81    out_size = AV_RL16(&buf[0]);
82    in_size  = AV_RL16(&buf[2]);
83    buf += 4;
84
85    if (in_size > buf_size) {
86        av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
87        return -1;
88    }
89
90    /* get output buffer */
91    s->frame.nb_samples = out_size;
92    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
93        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
94        return ret;
95    }
96    samples     = s->frame.data[0];
97    samples_end = samples + out_size;
98
99    if (in_size == out_size) {
100        memcpy(samples, buf, out_size);
101        *got_frame_ptr   = 1;
102        *(AVFrame *)data = s->frame;
103        return buf_size;
104    }
105
106    while (samples < samples_end && buf - avpkt->data < buf_size) {
107        int code, smp, size;
108        uint8_t count;
109        code  = *buf >> 6;
110        count = *buf & 0x3F;
111        buf++;
112
113        /* make sure we don't write past the output buffer */
114        switch (code) {
115        case 0:  smp = 4*(count+1);                    break;
116        case 1:  smp = 2*(count+1);                    break;
117        case 2:  smp = (count & 0x20) ? 1 : count + 1; break;
118        default: smp = count + 1;                      break;
119        }
120        if (samples_end - samples < smp)
121            break;
122
123        /* make sure we don't read past the input buffer */
124        size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
125        if ((buf - avpkt->data) + size > buf_size)
126            break;
127
128        switch (code) {
129        case 0: /* ADPCM 2-bit */
130            for (count++; count > 0; count--) {
131                code = *buf++;
132                sample += ( code       & 0x3) - 2;
133                sample = av_clip_uint8(sample);
134                *samples++ = sample;
135                sample += ((code >> 2) & 0x3) - 2;
136                sample = av_clip_uint8(sample);
137                *samples++ = sample;
138                sample += ((code >> 4) & 0x3) - 2;
139                sample = av_clip_uint8(sample);
140                *samples++ = sample;
141                sample +=  (code >> 6)        - 2;
142                sample = av_clip_uint8(sample);
143                *samples++ = sample;
144            }
145            break;
146        case 1: /* ADPCM 4-bit */
147            for (count++; count > 0; count--) {
148                code = *buf++;
149                sample += ws_adpcm_4bit[code & 0xF];
150                sample = av_clip_uint8(sample);
151                *samples++ = sample;
152                sample += ws_adpcm_4bit[code >> 4];
153                sample = av_clip_uint8(sample);
154                *samples++ = sample;
155            }
156            break;
157        case 2: /* no compression */
158            if (count & 0x20) { /* big delta */
159                int8_t t;
160                t = count;
161                t <<= 3;
162                sample += t >> 3;
163                sample = av_clip_uint8(sample);
164                *samples++ = sample;
165            } else { /* copy */
166                memcpy(samples, buf, smp);
167                samples += smp;
168                buf     += smp;
169                sample = buf[-1];
170            }
171            break;
172        default: /* run */
173            memset(samples, sample, smp);
174            samples += smp;
175        }
176    }
177
178    s->frame.nb_samples = samples - s->frame.data[0];
179    *got_frame_ptr   = 1;
180    *(AVFrame *)data = s->frame;
181
182    return buf_size;
183}
184
185AVCodec ff_ws_snd1_decoder = {
186    .name           = "ws_snd1",
187    .type           = AVMEDIA_TYPE_AUDIO,
188    .id             = CODEC_ID_WESTWOOD_SND1,
189    .priv_data_size = sizeof(WSSndContext),
190    .init           = ws_snd_decode_init,
191    .decode         = ws_snd_decode_frame,
192    .capabilities   = CODEC_CAP_DR1,
193    .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
194};
195