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 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 
25 #include "libavutil/common.h"
26 #include "libavutil/intreadwrite.h"
27 #include "avcodec.h"
28 #include "internal.h"
29 
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 -1;
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 };
static int16_t * samples
int size
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
int size
Definition: avcodec.h:916
#define AV_RL16
Definition: intreadwrite.h:42
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
Definition: ws-snd1.c:48
#define sample
AVCodec.
Definition: avcodec.h:2960
AVCodec ff_ws_snd1_decoder
Definition: ws-snd1.c:186
struct WSSndContext WSSndContext
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
uint8_t
AV_SAMPLE_FMT_U8
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2165
AVFrame frame
Definition: ws-snd1.c:45
audio channel layout utility functions
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
external API header
main external API structure.
Definition: avcodec.h:1339
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
static const int8_t ws_adpcm_4bit[]
Definition: ws-snd1.c:39
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1382
int channels
number of audio channels
Definition: avcodec.h:2105
#define AV_CH_LAYOUT_MONO
static int ws_snd_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
Definition: ws-snd1.c:62
This structure stores compressed data.
Definition: avcodec.h:898
int nb_samples
number of audio samples (per channel) described by this frame
Definition: avcodec.h:1042