Libav
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 
45 {
46  avctx->channels = 1;
49 
50  return 0;
51 }
52 
53 static int ws_snd_decode_frame(AVCodecContext *avctx, void *data,
54  int *got_frame_ptr, AVPacket *avpkt)
55 {
56  AVFrame *frame = data;
57  const uint8_t *buf = avpkt->data;
58  int buf_size = avpkt->size;
59 
60  int in_size, out_size, ret;
61  int sample = 128;
62  uint8_t *samples;
63  uint8_t *samples_end;
64 
65  if (!buf_size)
66  return 0;
67 
68  if (buf_size < 4) {
69  av_log(avctx, AV_LOG_ERROR, "packet is too small\n");
70  return AVERROR(EINVAL);
71  }
72 
73  out_size = AV_RL16(&buf[0]);
74  in_size = AV_RL16(&buf[2]);
75  buf += 4;
76 
77  if (in_size > buf_size) {
78  av_log(avctx, AV_LOG_ERROR, "Frame data is larger than input buffer\n");
79  return -1;
80  }
81 
82  /* get output buffer */
83  frame->nb_samples = out_size;
84  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
85  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
86  return ret;
87  }
88  samples = frame->data[0];
89  samples_end = samples + out_size;
90 
91  if (in_size == out_size) {
92  memcpy(samples, buf, out_size);
93  *got_frame_ptr = 1;
94  return buf_size;
95  }
96 
97  while (samples < samples_end && buf - avpkt->data < buf_size) {
98  int code, smp, size;
99  uint8_t count;
100  code = *buf >> 6;
101  count = *buf & 0x3F;
102  buf++;
103 
104  /* make sure we don't write past the output buffer */
105  switch (code) {
106  case 0: smp = 4 * (count + 1); break;
107  case 1: smp = 2 * (count + 1); break;
108  case 2: smp = (count & 0x20) ? 1 : count + 1; break;
109  default: smp = count + 1; break;
110  }
111  if (samples_end - samples < smp)
112  break;
113 
114  /* make sure we don't read past the input buffer */
115  size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
116  if ((buf - avpkt->data) + size > buf_size)
117  break;
118 
119  switch (code) {
120  case 0: /* ADPCM 2-bit */
121  for (count++; count > 0; count--) {
122  code = *buf++;
123  sample += ( code & 0x3) - 2;
124  sample = av_clip_uint8(sample);
125  *samples++ = sample;
126  sample += ((code >> 2) & 0x3) - 2;
127  sample = av_clip_uint8(sample);
128  *samples++ = sample;
129  sample += ((code >> 4) & 0x3) - 2;
130  sample = av_clip_uint8(sample);
131  *samples++ = sample;
132  sample += (code >> 6) - 2;
133  sample = av_clip_uint8(sample);
134  *samples++ = sample;
135  }
136  break;
137  case 1: /* ADPCM 4-bit */
138  for (count++; count > 0; count--) {
139  code = *buf++;
140  sample += ws_adpcm_4bit[code & 0xF];
141  sample = av_clip_uint8(sample);
142  *samples++ = sample;
143  sample += ws_adpcm_4bit[code >> 4];
144  sample = av_clip_uint8(sample);
145  *samples++ = sample;
146  }
147  break;
148  case 2: /* no compression */
149  if (count & 0x20) { /* big delta */
150  int8_t t;
151  t = count;
152  t <<= 3;
153  sample += t >> 3;
154  sample = av_clip_uint8(sample);
155  *samples++ = sample;
156  } else { /* copy */
157  memcpy(samples, buf, smp);
158  samples += smp;
159  buf += smp;
160  sample = buf[-1];
161  }
162  break;
163  default: /* run */
164  memset(samples, sample, smp);
165  samples += smp;
166  }
167  }
168 
169  frame->nb_samples = samples - frame->data[0];
170  *got_frame_ptr = 1;
171 
172  return buf_size;
173 }
174 
176  .name = "ws_snd1",
177  .long_name = NULL_IF_CONFIG_SMALL("Westwood Audio (SND1)"),
178  .type = AVMEDIA_TYPE_AUDIO,
180  .init = ws_snd_decode_init,
181  .decode = ws_snd_decode_frame,
182  .capabilities = CODEC_CAP_DR1,
183 };
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
int size
Definition: avcodec.h:974
#define AV_RL16
Definition: intreadwrite.h:42
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
Definition: ws-snd1.c:44
#define sample
AVCodec.
Definition: avcodec.h:2812
AVCodec ff_ws_snd1_decoder
Definition: ws-snd1.c:175
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
uint8_t
#define av_cold
Definition: attributes.h:66
AV_SAMPLE_FMT_U8
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
const char * name
Name of the codec implementation.
Definition: avcodec.h:2819
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1868
audio channel layout utility functions
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
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: frame.h:141
common internal api header.
common internal and external API header
int channels
number of audio channels
Definition: avcodec.h:1808
#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:53
This structure stores compressed data.
Definition: avcodec.h:950
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:179