]> git.sesse.net Git - ffmpeg/blob - libavcodec/ws-snd1.c
amrwbdec: set channels, channel_layout, and sample_rate
[ffmpeg] / libavcodec / ws-snd1.c
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/common.h"
24 #include "libavutil/intreadwrite.h"
25 #include "avcodec.h"
26
27 /**
28  * @file
29  * Westwood SNDx codecs
30  *
31  * Reference documents about VQA format and its audio codecs
32  * can be found here:
33  * http://www.multimedia.cx
34  */
35
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
41 typedef struct WSSndContext {
42     AVFrame frame;
43 } WSSndContext;
44
45 static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
46 {
47     WSSndContext *s = avctx->priv_data;
48
49     if (avctx->channels != 1) {
50         av_log_ask_for_sample(avctx, "unsupported number of channels\n");
51         return AVERROR(EINVAL);
52     }
53
54     avctx->sample_fmt = AV_SAMPLE_FMT_U8;
55
56     avcodec_get_frame_defaults(&s->frame);
57     avctx->coded_frame = &s->frame;
58
59     return 0;
60 }
61
62 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
63                                int *got_frame_ptr, AVPacket *avpkt)
64 {
65     WSSndContext *s = avctx->priv_data;
66     const uint8_t *buf = avpkt->data;
67     int buf_size       = avpkt->size;
68
69     int in_size, out_size, ret;
70     int sample = 128;
71     uint8_t *samples;
72     uint8_t *samples_end;
73
74     if (!buf_size)
75         return 0;
76
77     if (buf_size < 4) {
78         av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
79         return AVERROR(EINVAL);
80     }
81
82     out_size = AV_RL16(&buf[0]);
83     in_size  = AV_RL16(&buf[2]);
84     buf += 4;
85
86     if (in_size > buf_size) {
87         av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
88         return -1;
89     }
90
91     /* get output buffer */
92     s->frame.nb_samples = out_size;
93     if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
94         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95         return ret;
96     }
97     samples     = s->frame.data[0];
98     samples_end = samples + out_size;
99
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;
105     }
106
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++;
113
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;
120         }
121         if (samples_end - samples < smp)
122             break;
123
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;
128
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;
145             }
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;
156             }
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];
171             }
172             break;
173         default: /* run */
174             memset(samples, sample, smp);
175             samples += smp;
176         }
177     }
178
179     s->frame.nb_samples = samples - s->frame.data[0];
180     *got_frame_ptr   = 1;
181     *(AVFrame *)data = s->frame;
182
183     return buf_size;
184 }
185
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)"),
195 };