Statistics
| Branch: | Revision:

ffmpeg / libavcodec / ws-snd1.c @ 2912e87a

History | View | Annotate | Download (4.51 KB)

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

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

    
40
#define CLIP8(a) if(a>127)a=127;if(a<-128)a=-128;
41

    
42
static av_cold int ws_snd_decode_init(AVCodecContext * avctx)
43
{
44
//    WSSNDContext *c = avctx->priv_data;
45

    
46
    avctx->sample_fmt = AV_SAMPLE_FMT_S16;
47
    return 0;
48
}
49

    
50
static int ws_snd_decode_frame(AVCodecContext *avctx,
51
                void *data, int *data_size,
52
                AVPacket *avpkt)
53
{
54
    const uint8_t *buf = avpkt->data;
55
    int buf_size = avpkt->size;
56
//    WSSNDContext *c = avctx->priv_data;
57

    
58
    int in_size, out_size;
59
    int sample = 0;
60
    int i;
61
    short *samples = data;
62

    
63
    if (!buf_size)
64
        return 0;
65

    
66
    out_size = AV_RL16(&buf[0]);
67
    *data_size = out_size * 2;
68
    in_size = AV_RL16(&buf[2]);
69
    buf += 4;
70

    
71
    if (out_size > *data_size) {
72
        av_log(avctx, AV_LOG_ERROR, "Frame is too large to fit in buffer\n");
73
        return -1;
74
    }
75
    if (in_size > buf_size) {
76
        av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
77
        return -1;
78
    }
79
    if (in_size == out_size) {
80
        for (i = 0; i < out_size; i++)
81
            *samples++ = (*buf++ - 0x80) << 8;
82
        return buf_size;
83
    }
84

    
85
    while (out_size > 0) {
86
        int code;
87
        uint8_t count;
88
        code = (*buf) >> 6;
89
        count = (*buf) & 0x3F;
90
        buf++;
91
        switch(code) {
92
        case 0: /* ADPCM 2-bit */
93
            for (count++; count > 0; count--) {
94
                code = *buf++;
95
                sample += ws_adpcm_2bit[code & 0x3];
96
                CLIP8(sample);
97
                *samples++ = sample << 8;
98
                sample += ws_adpcm_2bit[(code >> 2) & 0x3];
99
                CLIP8(sample);
100
                *samples++ = sample << 8;
101
                sample += ws_adpcm_2bit[(code >> 4) & 0x3];
102
                CLIP8(sample);
103
                *samples++ = sample << 8;
104
                sample += ws_adpcm_2bit[(code >> 6) & 0x3];
105
                CLIP8(sample);
106
                *samples++ = sample << 8;
107
                out_size -= 4;
108
            }
109
            break;
110
        case 1: /* ADPCM 4-bit */
111
            for (count++; count > 0; count--) {
112
                code = *buf++;
113
                sample += ws_adpcm_4bit[code & 0xF];
114
                CLIP8(sample);
115
                *samples++ = sample << 8;
116
                sample += ws_adpcm_4bit[code >> 4];
117
                CLIP8(sample);
118
                *samples++ = sample << 8;
119
                out_size -= 2;
120
            }
121
            break;
122
        case 2: /* no compression */
123
            if (count & 0x20) { /* big delta */
124
                int8_t t;
125
                t = count;
126
                t <<= 3;
127
                sample += t >> 3;
128
                *samples++ = sample << 8;
129
                out_size--;
130
            } else { /* copy */
131
                for (count++; count > 0; count--) {
132
                    *samples++ = (*buf++ - 0x80) << 8;
133
                    out_size--;
134
                }
135
                sample = buf[-1] - 0x80;
136
            }
137
            break;
138
        default: /* run */
139
            for(count++; count > 0; count--) {
140
                *samples++ = sample << 8;
141
                out_size--;
142
            }
143
        }
144
    }
145

    
146
    return buf_size;
147
}
148

    
149
AVCodec ff_ws_snd1_decoder = {
150
    "ws_snd1",
151
    AVMEDIA_TYPE_AUDIO,
152
    CODEC_ID_WESTWOOD_SND1,
153
    0,
154
    ws_snd_decode_init,
155
    NULL,
156
    NULL,
157
    ws_snd_decode_frame,
158
    .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
159
};