FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ws-snd1.c
Go to the documentation of this file.
1 /*
2  * Westwood SNDx codecs
3  * Copyright (c) 2005 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 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 
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 
30 /**
31  * @file
32  * Westwood SNDx codecs
33  *
34  * Reference documents about VQA format and its audio codecs
35  * can be found here:
36  * http://www.multimedia.cx
37  */
38 
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
42 };
43 
44 typedef struct WSSndContext {
46 } WSSndContext;
47 
49 {
50  WSSndContext *s = avctx->priv_data;
51 
52  avctx->channels = 1;
55 
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;
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 AVERROR_INVALIDDATA;
89  }
90 
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;
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 
187  .name = "ws_snd1",
188  .type = AVMEDIA_TYPE_AUDIO,
190  .priv_data_size = sizeof(WSSndContext),
193  .capabilities = CODEC_CAP_DR1,
194  .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
195 };