FFmpeg
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 "config_components.h"
41 
42 #include "libavutil/avassert.h"
43 #include "avcodec.h"
44 #include "codec_internal.h"
45 #include "internal.h"
46 #include "libavutil/common.h"
47 
48 /** decoder context */
49 typedef struct EightSvxContext {
50  uint8_t fib_acc[2];
51  const int8_t *table;
52 
53  /* buffer used to store the whole first packet.
54  data is only sent as one large packet */
55  uint8_t *data[2];
56  int data_size;
57  int data_idx;
59 
60 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
61 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
62 
63 #define MAX_FRAME_SIZE 2048
64 
65 /**
66  * Delta decode the compressed values in src, and put the resulting
67  * decoded samples in dst.
68  *
69  * @param[in,out] state starting value. it is saved for use in the next call.
70  * @param table delta sequence table
71  */
72 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
73  uint8_t *state, const int8_t *table)
74 {
75  uint8_t val = *state;
76 
77  while (src_size--) {
78  uint8_t d = *src++;
79  val = av_clip_uint8(val + table[d & 0xF]);
80  *dst++ = val;
81  val = av_clip_uint8(val + table[d >> 4]);
82  *dst++ = val;
83  }
84 
85  *state = val;
86 }
87 
88 /** decode a frame */
90  int *got_frame_ptr, AVPacket *avpkt)
91 {
92  EightSvxContext *esc = avctx->priv_data;
93  int channels = avctx->ch_layout.nb_channels;
94  int buf_size;
95  int ch, ret;
96  int hdr_size = 2;
97 
98  /* decode and interleave the first packet */
99  if (!esc->data[0] && avpkt) {
100  int chan_size = avpkt->size / channels - hdr_size;
101 
102  if (avpkt->size % channels) {
103  av_log(avctx, AV_LOG_WARNING, "Packet with odd size, ignoring last byte\n");
104  }
105  if (avpkt->size < (hdr_size + 1) * channels) {
106  av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
107  return AVERROR_INVALIDDATA;
108  }
109 
110  esc->fib_acc[0] = avpkt->data[1] + 128;
111  if (channels == 2)
112  esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
113 
114  esc->data_idx = 0;
115  esc->data_size = chan_size;
116  if (!(esc->data[0] = av_malloc(chan_size)))
117  return AVERROR(ENOMEM);
118  if (channels == 2) {
119  if (!(esc->data[1] = av_malloc(chan_size))) {
120  av_freep(&esc->data[0]);
121  return AVERROR(ENOMEM);
122  }
123  }
124  memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
125  if (channels == 2)
126  memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
127  }
128  if (!esc->data[0]) {
129  av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
130  return AVERROR_INVALIDDATA;
131  }
132 
133  /* decode next piece of data from the buffer */
134  buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
135  if (buf_size <= 0) {
136  *got_frame_ptr = 0;
137  return avpkt->size;
138  }
139 
140  /* get output buffer */
141  frame->nb_samples = buf_size * 2;
142  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
143  return ret;
144 
145  for (ch = 0; ch < channels; ch++) {
146  delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
147  buf_size, &esc->fib_acc[ch], esc->table);
148  }
149 
150  esc->data_idx += buf_size;
151 
152  *got_frame_ptr = 1;
153 
154  return ((avctx->frame_number == 0) * hdr_size + buf_size) * channels;
155 }
156 
158 {
159  EightSvxContext *esc = avctx->priv_data;
160 
161  if (avctx->ch_layout.nb_channels < 1 || avctx->ch_layout.nb_channels > 2) {
162  av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
163  return AVERROR_INVALIDDATA;
164  }
165 
166  switch (avctx->codec->id) {
167  case AV_CODEC_ID_8SVX_FIB: esc->table = fibonacci; break;
168  case AV_CODEC_ID_8SVX_EXP: esc->table = exponential; break;
169  default:
170  av_assert1(0);
171  }
172  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
173 
174  return 0;
175 }
176 
178 {
179  EightSvxContext *esc = avctx->priv_data;
180 
181  av_freep(&esc->data[0]);
182  av_freep(&esc->data[1]);
183  esc->data_size = 0;
184  esc->data_idx = 0;
185 
186  return 0;
187 }
188 
189 #if CONFIG_EIGHTSVX_FIB_DECODER
191  .p.name = "8svx_fib",
192  .p.long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
193  .p.type = AVMEDIA_TYPE_AUDIO,
194  .p.id = AV_CODEC_ID_8SVX_FIB,
195  .priv_data_size = sizeof (EightSvxContext),
198  .close = eightsvx_decode_close,
199  .p.capabilities = AV_CODEC_CAP_DR1,
200  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
202  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
203 };
204 #endif
205 #if CONFIG_EIGHTSVX_EXP_DECODER
207  .p.name = "8svx_exp",
208  .p.long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
209  .p.type = AVMEDIA_TYPE_AUDIO,
210  .p.id = AV_CODEC_ID_8SVX_EXP,
211  .priv_data_size = sizeof (EightSvxContext),
214  .close = eightsvx_decode_close,
215  .p.capabilities = AV_CODEC_CAP_DR1,
216  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
218  .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE,
219 };
220 #endif
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
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
AV_CODEC_ID_8SVX_EXP
@ AV_CODEC_ID_8SVX_EXP
Definition: codec_id.h:481
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
table
static const uint16_t table[]
Definition: prosumer.c:206
FFCodec
Definition: codec_internal.h:112
exponential
static const int8_t exponential[16]
Definition: 8svx.c:61
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:300
MAX_FRAME_SIZE
#define MAX_FRAME_SIZE
Definition: 8svx.c:63
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
init
static int init
Definition: av_tx.c:47
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:398
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2056
eightsvx_decode_init
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
Definition: 8svx.c:157
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_eightsvx_exp_decoder
const FFCodec ff_eightsvx_exp_decoder
AV_CODEC_ID_8SVX_FIB
@ AV_CODEC_ID_8SVX_FIB
Definition: codec_id.h:482
avassert.h
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:254
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
EightSvxContext
decoder context
Definition: 8svx.c:49
channels
channels
Definition: aptx.h:32
ff_eightsvx_fib_decoder
const FFCodec ff_eightsvx_fib_decoder
EightSvxContext::data_idx
int data_idx
Definition: 8svx.c:57
EightSvxContext::table
const int8_t * table
Definition: 8svx.c:51
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
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:375
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
codec_internal.h
EightSvxContext::fib_acc
uint8_t fib_acc[2]
Definition: 8svx.c:50
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
state
static struct @327 state
eightsvx_decode_close
static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
Definition: 8svx.c:177
AVCodec::id
enum AVCodecID id
Definition: codec.h:210
delta_decode
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:72
common.h
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
FF_CODEC_CAP_INIT_THREADSAFE
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: codec_internal.h:31
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:203
fibonacci
static const int8_t fibonacci[16]
Definition: 8svx.c:60
avcodec.h
ret
ret
Definition: filter_design.txt:187
eightsvx_decode_frame
static int eightsvx_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
decode a frame
Definition: 8svx.c:89
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
EightSvxContext::data_size
int data_size
Definition: 8svx.c:56
AVCodecContext
main external API structure.
Definition: avcodec.h:389
EightSvxContext::data
uint8_t * data[2]
Definition: 8svx.c:55
av_clip_uint8
#define av_clip_uint8
Definition: common.h:101
AVCodecContext::frame_number
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1037
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
d
d
Definition: ffmpeg_filter.c:153
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