ws-snd1.c 5.24 KB
Newer Older
1 2 3 4
/*
 * Westwood SNDx codecs
 * Copyright (c) 2005 Konstantin Shishkov
 *
5 6 7
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * 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 14 15 16 17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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 25 26
#include "avcodec.h"

/**
27
 * @file
28
 * Westwood SNDx codecs
29 30 31 32 33 34
 *
 * Reference documents about VQA format and its audio codecs
 * can be found here:
 * http://www.multimedia.cx
 */

35
static const int8_t ws_adpcm_4bit[] = {
36
    -9, -8, -6, -5, -4, -3, -2, -1,
37 38
     0,  1,  2,  3,  4,  5,  6,  8
};
39

40
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
41
{
42 43 44 45
    if (avctx->channels != 1) {
        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
        return AVERROR(EINVAL);
    }
46

47
    avctx->sample_fmt = AV_SAMPLE_FMT_U8;
48 49 50
    return 0;
}

51 52
static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
                               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 59
    int sample = 128;
    uint8_t *samples = data;
60
    uint8_t *samples_end;
61

62 63 64
    if (!buf_size)
        return 0;

65 66 67 68 69
    if (buf_size < 4) {
        av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
        return AVERROR(EINVAL);
    }

70
    out_size = AV_RL16(&buf[0]);
71
    in_size  = AV_RL16(&buf[2]);
72
    buf += 4;
73

74 75 76 77 78 79 80 81
    if (out_size > *data_size) {
        av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
        return -1;
    }
    if (in_size > buf_size) {
        av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
        return -1;
    }
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 88
        return buf_size;
    }
89

90
    while (samples < samples_end && buf - avpkt->data < buf_size) {
91
        int code, smp, size;
92
        uint8_t count;
93 94
        code  = *buf >> 6;
        count = *buf & 0x3F;
95
        buf++;
96

97
        /* make sure we don't write past the output buffer */
98 99 100 101 102 103
        switch (code) {
        case 0:  smp = 4;                              break;
        case 1:  smp = 2;                              break;
        case 2:  smp = (count & 0x20) ? 1 : count + 1; break;
        default: smp = count + 1;                      break;
        }
104
        if (samples_end - samples < smp)
105 106 107 108 109 110 111
            break;

        /* make sure we don't read past the input buffer */
        size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
        if ((buf - avpkt->data) + size > buf_size)
            break;

112
        switch (code) {
113 114 115
        case 0: /* ADPCM 2-bit */
            for (count++; count > 0; count--) {
                code = *buf++;
116
                sample += ( code       & 0x3) - 2;
117 118
                sample = av_clip_uint8(sample);
                *samples++ = sample;
119
                sample += ((code >> 2) & 0x3) - 2;
120 121
                sample = av_clip_uint8(sample);
                *samples++ = sample;
122
                sample += ((code >> 4) & 0x3) - 2;
123 124
                sample = av_clip_uint8(sample);
                *samples++ = sample;
125
                sample +=  (code >> 6)        - 2;
126 127
                sample = av_clip_uint8(sample);
                *samples++ = sample;
128 129 130 131 132 133
            }
            break;
        case 1: /* ADPCM 4-bit */
            for (count++; count > 0; count--) {
                code = *buf++;
                sample += ws_adpcm_4bit[code & 0xF];
134 135
                sample = av_clip_uint8(sample);
                *samples++ = sample;
136
                sample += ws_adpcm_4bit[code >> 4];
137 138
                sample = av_clip_uint8(sample);
                *samples++ = sample;
139 140 141 142
            }
            break;
        case 2: /* no compression */
            if (count & 0x20) { /* big delta */
143
                int8_t t;
144 145 146
                t = count;
                t <<= 3;
                sample += t >> 3;
147 148
                sample = av_clip_uint8(sample);
                *samples++ = sample;
149
            } else { /* copy */
150 151 152
                memcpy(samples, buf, smp);
                samples += smp;
                buf     += smp;
153
                sample = buf[-1];
154 155 156
            }
            break;
        default: /* run */
157 158
            memset(samples, sample, smp);
            samples += smp;
159 160
        }
    }
161

162 163
    *data_size = samples - (uint8_t *)data;

164 165 166
    return buf_size;
}

167
AVCodec ff_ws_snd1_decoder = {
168 169 170 171 172
    .name           = "ws_snd1",
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = CODEC_ID_WESTWOOD_SND1,
    .init           = ws_snd_decode_init,
    .decode         = ws_snd_decode_frame,
173
    .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
174
};