asfdec: also read Metadata Library Object
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / ws-snd1.c
blob62e0c6f656fff602d0679f081a1993a3562c38aa
1 /*
2 * Westwood SNDx codecs
3 * Copyright (c) 2005 Konstantin Shishkov
5 * This file is part of Libav.
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.
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.
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
22 #include <stdint.h>
24 #include "libavutil/channel_layout.h"
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "internal.h"
30 /**
31 * @file
32 * Westwood SNDx codecs
34 * Reference documents about VQA format and its audio codecs
35 * can be found here:
36 * http://www.multimedia.cx
39 static const int8_t ws_adpcm_4bit[] = {
40 -9, -8, -6, -5, -4, -3, -2, -1,
41 0, 1, 2, 3, 4, 5, 6, 8
44 typedef struct WSSndContext {
45 AVFrame frame;
46 } WSSndContext;
48 static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
50 WSSndContext *s = avctx->priv_data;
52 avctx->channels = 1;
53 avctx->channel_layout = AV_CH_LAYOUT_MONO;
54 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
56 avcodec_get_frame_defaults(&s->frame);
57 avctx->coded_frame = &s->frame;
59 return 0;
62 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
63 int *got_frame_ptr, AVPacket *avpkt)
65 WSSndContext *s = avctx->priv_data;
66 const uint8_t *buf = avpkt->data;
67 int buf_size = avpkt->size;
69 int in_size, out_size, ret;
70 int sample = 128;
71 uint8_t *samples;
72 uint8_t *samples_end;
74 if (!buf_size)
75 return 0;
77 if (buf_size < 4) {
78 av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
79 return AVERROR(EINVAL);
82 out_size = AV_RL16(&buf[0]);
83 in_size = AV_RL16(&buf[2]);
84 buf += 4;
86 if (in_size > buf_size) {
87 av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
88 return -1;
91 /* get output buffer */
92 s->frame.nb_samples = out_size;
93 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
94 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95 return ret;
97 samples = s->frame.data[0];
98 samples_end = samples + out_size;
100 if (in_size == out_size) {
101 memcpy(samples, buf, out_size);
102 *got_frame_ptr = 1;
103 *(AVFrame *)data = s->frame;
104 return buf_size;
107 while (samples < samples_end && buf - avpkt->data < buf_size) {
108 int code, smp, size;
109 uint8_t count;
110 code = *buf >> 6;
111 count = *buf & 0x3F;
112 buf++;
114 /* make sure we don't write past the output buffer */
115 switch (code) {
116 case 0: smp = 4 * (count + 1); break;
117 case 1: smp = 2 * (count + 1); break;
118 case 2: smp = (count & 0x20) ? 1 : count + 1; break;
119 default: smp = count + 1; break;
121 if (samples_end - samples < smp)
122 break;
124 /* make sure we don't read past the input buffer */
125 size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
126 if ((buf - avpkt->data) + size > buf_size)
127 break;
129 switch (code) {
130 case 0: /* ADPCM 2-bit */
131 for (count++; count > 0; count--) {
132 code = *buf++;
133 sample += ( code & 0x3) - 2;
134 sample = av_clip_uint8(sample);
135 *samples++ = sample;
136 sample += ((code >> 2) & 0x3) - 2;
137 sample = av_clip_uint8(sample);
138 *samples++ = sample;
139 sample += ((code >> 4) & 0x3) - 2;
140 sample = av_clip_uint8(sample);
141 *samples++ = sample;
142 sample += (code >> 6) - 2;
143 sample = av_clip_uint8(sample);
144 *samples++ = sample;
146 break;
147 case 1: /* ADPCM 4-bit */
148 for (count++; count > 0; count--) {
149 code = *buf++;
150 sample += ws_adpcm_4bit[code & 0xF];
151 sample = av_clip_uint8(sample);
152 *samples++ = sample;
153 sample += ws_adpcm_4bit[code >> 4];
154 sample = av_clip_uint8(sample);
155 *samples++ = sample;
157 break;
158 case 2: /* no compression */
159 if (count & 0x20) { /* big delta */
160 int8_t t;
161 t = count;
162 t <<= 3;
163 sample += t >> 3;
164 sample = av_clip_uint8(sample);
165 *samples++ = sample;
166 } else { /* copy */
167 memcpy(samples, buf, smp);
168 samples += smp;
169 buf += smp;
170 sample = buf[-1];
172 break;
173 default: /* run */
174 memset(samples, sample, smp);
175 samples += smp;
179 s->frame.nb_samples = samples - s->frame.data[0];
180 *got_frame_ptr = 1;
181 *(AVFrame *)data = s->frame;
183 return buf_size;
186 AVCodec ff_ws_snd1_decoder = {
187 .name = "ws_snd1",
188 .type = AVMEDIA_TYPE_AUDIO,
189 .id = AV_CODEC_ID_WESTWOOD_SND1,
190 .priv_data_size = sizeof(WSSndContext),
191 .init = ws_snd_decode_init,
192 .decode = ws_snd_decode_frame,
193 .capabilities = CODEC_CAP_DR1,
194 .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),