ws-snd1.c 5.76 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 41 42 43
typedef struct WSSndContext {
    AVFrame frame;
} WSSndContext;

44
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
45
{
46 47
    WSSndContext *s = avctx->priv_data;

48 49 50 51
    if (avctx->channels != 1) {
        av_log_ask_for_sample(avctx, "unsupported number of channels\n");
        return AVERROR(EINVAL);
    }
52

53
    avctx->sample_fmt = AV_SAMPLE_FMT_U8;
54 55 56 57

    avcodec_get_frame_defaults(&s->frame);
    avctx->coded_frame = &s->frame;

58 59 60
    return 0;
}

61
static 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 74 75
    if (!buf_size)
        return 0;

76 77 78 79 80
    if (buf_size < 4) {
        av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
        return AVERROR(EINVAL);
    }

81
    out_size = AV_RL16(&buf[0]);
82
    in_size  = AV_RL16(&buf[2]);
83
    buf += 4;
84

85 86 87 88
    if (in_size > buf_size) {
        av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
        return -1;
    }
89 90 91 92 93 94 95 96

    /* get output buffer */
    s->frame.nb_samples = out_size;
    if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
        av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
        return ret;
    }
    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 102
        *got_frame_ptr   = 1;
        *(AVFrame *)data = s->frame;
103 104
        return buf_size;
    }
105

106
    while (samples < samples_end && buf - avpkt->data < buf_size) {
107
        int code, smp, size;
108
        uint8_t count;
109 110
        code  = *buf >> 6;
        count = *buf & 0x3F;
111
        buf++;
112

113
        /* make sure we don't write past the output buffer */
114
        switch (code) {
115 116
        case 0:  smp = 4 * (count + 1);                break;
        case 1:  smp = 2 * (count + 1);                break;
117 118 119
        case 2:  smp = (count & 0x20) ? 1 : count + 1; break;
        default: smp = count + 1;                      break;
        }
120
        if (samples_end - samples < smp)
121 122 123 124 125 126 127
            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;

128
        switch (code) {
129 130 131
        case 0: /* ADPCM 2-bit */
            for (count++; count > 0; count--) {
                code = *buf++;
132
                sample += ( code       & 0x3) - 2;
133 134
                sample = av_clip_uint8(sample);
                *samples++ = sample;
135
                sample += ((code >> 2) & 0x3) - 2;
136 137
                sample = av_clip_uint8(sample);
                *samples++ = sample;
138
                sample += ((code >> 4) & 0x3) - 2;
139 140
                sample = av_clip_uint8(sample);
                *samples++ = sample;
141
                sample +=  (code >> 6)        - 2;
142 143
                sample = av_clip_uint8(sample);
                *samples++ = sample;
144 145 146 147 148 149
            }
            break;
        case 1: /* ADPCM 4-bit */
            for (count++; count > 0; count--) {
                code = *buf++;
                sample += ws_adpcm_4bit[code & 0xF];
150 151
                sample = av_clip_uint8(sample);
                *samples++ = sample;
152
                sample += ws_adpcm_4bit[code >> 4];
153 154
                sample = av_clip_uint8(sample);
                *samples++ = sample;
155 156 157 158
            }
            break;
        case 2: /* no compression */
            if (count & 0x20) { /* big delta */
159
                int8_t t;
160 161 162
                t = count;
                t <<= 3;
                sample += t >> 3;
163 164
                sample = av_clip_uint8(sample);
                *samples++ = sample;
165
            } else { /* copy */
166 167 168
                memcpy(samples, buf, smp);
                samples += smp;
                buf     += smp;
169
                sample = buf[-1];
170 171 172
            }
            break;
        default: /* run */
173 174
            memset(samples, sample, smp);
            samples += smp;
175 176
        }
    }
177

178 179 180
    s->frame.nb_samples = samples - s->frame.data[0];
    *got_frame_ptr   = 1;
    *(AVFrame *)data = s->frame;
181

182 183 184
    return buf_size;
}

185
AVCodec ff_ws_snd1_decoder = {
186 187 188
    .name           = "ws_snd1",
    .type           = AVMEDIA_TYPE_AUDIO,
    .id             = CODEC_ID_WESTWOOD_SND1,
189
    .priv_data_size = sizeof(WSSndContext),
190 191
    .init           = ws_snd_decode_init,
    .decode         = ws_snd_decode_frame,
192
    .capabilities   = CODEC_CAP_DR1,
193
    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
194
};