FFmpeg
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 "codec_internal.h"
29 #include "decode.h"
30 
31 /**
32  * @file
33  * Westwood SNDx codecs
34  *
35  * Reference documents about VQA format and its audio codecs
36  * can be found here:
37  * http://www.multimedia.cx
38  */
39 
40 static const int8_t ws_adpcm_4bit[] = {
41  -9, -8, -6, -5, -4, -3, -2, -1,
42  0, 1, 2, 3, 4, 5, 6, 8
43 };
44 
46 {
50 
51  return 0;
52 }
53 
55  int *got_frame_ptr, AVPacket *avpkt)
56 {
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 AVERROR_INVALIDDATA;
80  }
81 
82  /* get output buffer */
84  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
85  return ret;
86  samples = frame->data[0];
87  samples_end = samples + out_size;
88 
89  if (in_size == out_size) {
90  memcpy(samples, buf, out_size);
91  *got_frame_ptr = 1;
92  return buf_size;
93  }
94 
95  while (samples < samples_end && buf - avpkt->data < buf_size) {
96  int code, smp, size;
97  uint8_t count;
98  code = *buf >> 6;
99  count = *buf & 0x3F;
100  buf++;
101 
102  /* make sure we don't write past the output buffer */
103  switch (code) {
104  case 0: smp = 4 * (count + 1); break;
105  case 1: smp = 2 * (count + 1); break;
106  case 2: smp = (count & 0x20) ? 1 : count + 1; break;
107  default: smp = count + 1; break;
108  }
109  if (samples_end - samples < smp)
110  break;
111 
112  /* make sure we don't read past the input buffer */
113  size = ((code == 2 && (count & 0x20)) || code == 3) ? 0 : count + 1;
114  if ((buf - avpkt->data) + size > buf_size)
115  break;
116 
117  switch (code) {
118  case 0: /* ADPCM 2-bit */
119  for (count++; count > 0; count--) {
120  code = *buf++;
121  sample += ( code & 0x3) - 2;
123  *samples++ = sample;
124  sample += ((code >> 2) & 0x3) - 2;
126  *samples++ = sample;
127  sample += ((code >> 4) & 0x3) - 2;
129  *samples++ = sample;
130  sample += (code >> 6) - 2;
132  *samples++ = sample;
133  }
134  break;
135  case 1: /* ADPCM 4-bit */
136  for (count++; count > 0; count--) {
137  code = *buf++;
138  sample += ws_adpcm_4bit[code & 0xF];
140  *samples++ = sample;
141  sample += ws_adpcm_4bit[code >> 4];
143  *samples++ = sample;
144  }
145  break;
146  case 2: /* no compression */
147  if (count & 0x20) { /* big delta */
148  int8_t t;
149  t = count;
150  t <<= 3;
151  sample += t >> 3;
153  *samples++ = sample;
154  } else { /* copy */
155  memcpy(samples, buf, smp);
156  samples += smp;
157  buf += smp;
158  sample = buf[-1];
159  }
160  break;
161  default: /* run */
162  memset(samples, sample, smp);
163  samples += smp;
164  }
165  }
166 
167  frame->nb_samples = samples - frame->data[0];
168  *got_frame_ptr = 1;
169 
170  return buf_size;
171 }
172 
174  .p.name = "ws_snd1",
175  CODEC_LONG_NAME("Westwood Audio (SND1)"),
176  .p.type = AVMEDIA_TYPE_AUDIO,
178  .init = ws_snd_decode_init,
180  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
181 };
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
out_size
int out_size
Definition: movenc.c:55
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:491
data
const char data[16]
Definition: mxf.c:148
FFCodec
Definition: codec_internal.h:127
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2107
ws_snd_decode_init
static av_cold int ws_snd_decode_init(AVCodecContext *avctx)
Definition: ws-snd1.c:45
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
decode.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_ws_snd1_decoder
const FFCodec ff_ws_snd1_decoder
Definition: ws-snd1.c:173
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1617
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:492
AVChannelLayout
An AVChannelLayout holds information about the channel layout of audio data.
Definition: channel_layout.h:307
codec_internal.h
ws_snd_decode_frame
static int ws_snd_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: ws-snd1.c:54
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1080
sample
#define sample
Definition: flacdsp_template.c:44
size
int size
Definition: twinvq_data.h:10344
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AV_SAMPLE_FMT_U8
@ AV_SAMPLE_FMT_U8
unsigned 8 bits
Definition: samplefmt.h:57
common.h
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
avcodec.h
ret
ret
Definition: filter_design.txt:187
AV_CODEC_ID_WESTWOOD_SND1
@ AV_CODEC_ID_WESTWOOD_SND1
Definition: codec_id.h:459
AVCodecContext
main external API structure.
Definition: avcodec.h:441
channel_layout.h
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:640
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
av_clip_uint8
#define av_clip_uint8
Definition: common.h:102
ws_adpcm_4bit
static const int8_t ws_adpcm_4bit[]
Definition: ws-snd1.c:40
AV_CHANNEL_LAYOUT_MONO
#define AV_CHANNEL_LAYOUT_MONO
Definition: channel_layout.h:382
AVPacket
This structure stores compressed data.
Definition: packet.h:468
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61