8svx.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Jaikrishnan Menon
3  * Copyright (C) 2011 Stefano Sabatini
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 /**
23  * @file
24  * 8svx audio decoder
25  * @author Jaikrishnan Menon
26  *
27  * supports: fibonacci delta encoding
28  * : exponential encoding
29  *
30  * For more information about the 8SVX format:
31  * http://netghost.narod.ru/gff/vendspec/iff/iff.txt
32  * http://sox.sourceforge.net/AudioFormats-11.html
33  * http://aminet.net/package/mus/misc/wavepak
34  * http://amigan.1emu.net/reg/8SVX.txt
35  *
36  * Samples can be found here:
37  * http://aminet.net/mods/smpl/
38  */
39 
40 #include "libavutil/avassert.h"
41 #include "avcodec.h"
42 #include "internal.h"
43 #include "libavutil/common.h"
44 
45 /** decoder context */
46 typedef struct EightSvxContext {
48  const int8_t *table;
49 
50  /* buffer used to store the whole first packet.
51  data is only sent as one large packet */
53  int data_size;
54  int data_idx;
56 
57 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
58 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
59 
60 #define MAX_FRAME_SIZE 2048
61 
62 /**
63  * Delta decode the compressed values in src, and put the resulting
64  * decoded samples in dst.
65  *
66  * @param[in,out] state starting value. it is saved for use in the next call.
67  * @param table delta sequence table
68  */
69 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
70  uint8_t *state, const int8_t *table)
71 {
72  uint8_t val = *state;
73 
74  while (src_size--) {
75  uint8_t d = *src++;
76  val = av_clip_uint8(val + table[d & 0xF]);
77  *dst++ = val;
78  val = av_clip_uint8(val + table[d >> 4]);
79  *dst++ = val;
80  }
81 
82  *state = val;
83 }
84 
85 /** decode a frame */
86 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
87  int *got_frame_ptr, AVPacket *avpkt)
88 {
89  EightSvxContext *esc = avctx->priv_data;
90  AVFrame *frame = data;
91  int buf_size;
92  int ch, ret;
93  int hdr_size = 2;
94 
95  /* decode and interleave the first packet */
96  if (!esc->data[0] && avpkt) {
97  int chan_size = avpkt->size / avctx->channels - hdr_size;
98 
99  if (avpkt->size % avctx->channels) {
100  av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
101  }
102  if (avpkt->size < (hdr_size + 1) * avctx->channels) {
103  av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
104  return AVERROR(EINVAL);
105  }
106 
107  esc->fib_acc[0] = avpkt->data[1] + 128;
108  if (avctx->channels == 2)
109  esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
110 
111  esc->data_idx = 0;
112  esc->data_size = chan_size;
113  if (!(esc->data[0] = av_malloc(chan_size)))
114  return AVERROR(ENOMEM);
115  if (avctx->channels == 2) {
116  if (!(esc->data[1] = av_malloc(chan_size))) {
117  av_freep(&esc->data[0]);
118  return AVERROR(ENOMEM);
119  }
120  }
121  memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
122  if (avctx->channels == 2)
123  memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
124  }
125  if (!esc->data[0]) {
126  av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
127  return AVERROR(EINVAL);
128  }
129 
130  /* decode next piece of data from the buffer */
131  buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
132  if (buf_size <= 0) {
133  *got_frame_ptr = 0;
134  return avpkt->size;
135  }
136 
137  /* get output buffer */
138  frame->nb_samples = buf_size * 2;
139  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
140  return ret;
141 
142  for (ch = 0; ch < avctx->channels; ch++) {
143  delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
144  buf_size, &esc->fib_acc[ch], esc->table);
145  }
146 
147  esc->data_idx += buf_size;
148 
149  *got_frame_ptr = 1;
150 
151  return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
152 }
153 
155 {
156  EightSvxContext *esc = avctx->priv_data;
157 
158  if (avctx->channels < 1 || avctx->channels > 2) {
159  av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
160  return AVERROR_INVALIDDATA;
161  }
162 
163  switch (avctx->codec->id) {
164  case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
165  case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
166  default:
167  av_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
168  return AVERROR_INVALIDDATA;
169  }
170  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
171 
172  return 0;
173 }
174 
176 {
177  EightSvxContext *esc = avctx->priv_data;
178 
179  av_freep(&esc->data[0]);
180  av_freep(&esc->data[1]);
181  esc->data_size = 0;
182  esc->data_idx = 0;
183 
184  return 0;
185 }
186 
187 #if CONFIG_EIGHTSVX_FIB_DECODER
188 AVCodec ff_eightsvx_fib_decoder = {
189  .name = "8svx_fib",
190  .type = AVMEDIA_TYPE_AUDIO,
191  .id = AV_CODEC_ID_8SVX_FIB,
192  .priv_data_size = sizeof (EightSvxContext),
196  .capabilities = CODEC_CAP_DR1,
197  .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
198  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
200 };
201 #endif
202 #if CONFIG_EIGHTSVX_EXP_DECODER
203 AVCodec ff_eightsvx_exp_decoder = {
204  .name = "8svx_exp",
205  .type = AVMEDIA_TYPE_AUDIO,
206  .id = AV_CODEC_ID_8SVX_EXP,
207  .priv_data_size = sizeof (EightSvxContext),
211  .capabilities = CODEC_CAP_DR1,
212  .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
213  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
215 };
216 #endif
const struct AVCodec * codec
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static const int8_t fibonacci[16]
Definition: 8svx.c:57
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
#define MAX_FRAME_SIZE
Definition: 8svx.c:60
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
set threshold d
enum AVSampleFormat sample_fmt
audio sample format
uint8_t
#define av_cold
Definition: attributes.h:78
static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
decode a frame
Definition: 8svx.c:86
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
uint8_t * data
static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
Definition: 8svx.c:175
static const int8_t exponential[16]
Definition: 8svx.c:58
frame
Definition: stft.m:14
enum AVCodecID id
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size, uint8_t *state, const int8_t *table)
Delta decode the compressed values in src, and put the resulting decoded samples in dst...
Definition: 8svx.c:69
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
const char * name
Name of the codec implementation.
external API header
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
const int8_t * table
Definition: 8svx.c:48
int data_idx
Definition: 8svx.c:54
AVS_Value src
Definition: avisynth_c.h:523
struct EightSvxContext EightSvxContext
decoder context
main external API structure.
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:375
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
uint8_t fib_acc[2]
Definition: 8svx.c:47
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
static uint32_t state
Definition: trasher.c:27
int data_size
Definition: 8svx.c:53
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
decoder context
Definition: 8svx.c:46
common internal api header.
common internal and external API header
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
Definition: 8svx.c:154
unsigned 8 bits, planar
Definition: samplefmt.h:57
uint8_t * data[2]
Definition: 8svx.c:52
int channels
number of audio channels
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
int frame_number
Frame counter, set by libavcodec.
static int decode(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: crystalhd.c:868
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127