Libav
8svx.c
Go to the documentation of this file.
1 /*
2  * 8SVX audio decoder
3  * Copyright (C) 2008 Jaikrishnan Menon
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 
31 #include "avcodec.h"
32 #include "internal.h"
33 #include "libavutil/common.h"
34 
36 typedef struct EightSvxContext {
38  const int8_t *table;
39 
40  /* buffer used to store the whole first packet.
41  data is only sent as one large packet */
43  int data_size;
44  int data_idx;
46 
47 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
48  0, 1, 2, 3, 5, 8, 13, 21 };
49 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
50  0, 1, 2, 4, 8, 16, 32, 64 };
51 
52 #define MAX_FRAME_SIZE 32768
53 
60 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
61  uint8_t *state, const int8_t *table)
62 {
63  uint8_t val = *state;
64 
65  while (src_size--) {
66  uint8_t d = *src++;
67  val = av_clip_uint8(val + table[d & 0xF]);
68  *dst++ = val;
69  val = av_clip_uint8(val + table[d >> 4]);
70  *dst++ = val;
71  }
72 
73  *state = val;
74 }
75 
76 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
77 {
78  while (src_size--)
79  *dst++ = *src++ + 128;
80 }
81 
83 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
84  int *got_frame_ptr, AVPacket *avpkt)
85 {
86  EightSvxContext *esc = avctx->priv_data;
87  AVFrame *frame = data;
88  int buf_size;
89  int ch, ret;
90  int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
91 
92  /* for the first packet, copy data to buffer */
93  if (avpkt->data) {
94  int hdr_size = is_compr ? 2 : 0;
95  int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
96 
97  if (avpkt->size < hdr_size * avctx->channels) {
98  av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
99  return AVERROR(EINVAL);
100  }
101  if (esc->data[0]) {
102  av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
103  return AVERROR(EINVAL);
104  }
105 
106  if (is_compr) {
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 
112  esc->data_idx = 0;
113  esc->data_size = chan_size;
114  if (!(esc->data[0] = av_malloc(chan_size)))
115  return AVERROR(ENOMEM);
116  if (avctx->channels == 2) {
117  if (!(esc->data[1] = av_malloc(chan_size))) {
118  av_freep(&esc->data[0]);
119  return AVERROR(ENOMEM);
120  }
121  }
122  memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
123  if (avctx->channels == 2)
124  memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
125  }
126  if (!esc->data[0]) {
127  av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
128  return AVERROR(EINVAL);
129  }
130 
131  /* decode next piece of data from the buffer */
132  buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
133  if (buf_size <= 0) {
134  *got_frame_ptr = 0;
135  return avpkt->size;
136  }
137 
138  /* get output buffer */
139  frame->nb_samples = buf_size * (is_compr + 1);
140  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
141  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
142  return ret;
143  }
144 
145  for (ch = 0; ch < avctx->channels; ch++) {
146  if (is_compr) {
147  delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
148  buf_size, &esc->fib_acc[ch], esc->table);
149  } else {
150  raw_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
151  buf_size);
152  }
153  }
154 
155  esc->data_idx += buf_size;
156 
157  *got_frame_ptr = 1;
158 
159  return avpkt->size;
160 }
161 
164 {
165  EightSvxContext *esc = avctx->priv_data;
166 
167  if (avctx->channels < 1 || avctx->channels > 2) {
168  av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
169  return AVERROR(EINVAL);
170  }
171 
172  switch(avctx->codec->id) {
174  esc->table = fibonacci;
175  break;
177  esc->table = exponential;
178  break;
180  break;
181  default:
182  return -1;
183  }
184  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
185 
186  return 0;
187 }
188 
190 {
191  EightSvxContext *esc = avctx->priv_data;
192 
193  av_freep(&esc->data[0]);
194  av_freep(&esc->data[1]);
195 
196  return 0;
197 }
198 
200  .name = "8svx_fib",
201  .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
202  .type = AVMEDIA_TYPE_AUDIO,
203  .id = AV_CODEC_ID_8SVX_FIB,
204  .priv_data_size = sizeof (EightSvxContext),
208  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
209  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
211 };
212 
214  .name = "8svx_exp",
215  .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
216  .type = AVMEDIA_TYPE_AUDIO,
217  .id = AV_CODEC_ID_8SVX_EXP,
218  .priv_data_size = sizeof (EightSvxContext),
222  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
223  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
225 };
226 
228  .name = "pcm_s8_planar",
229  .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
230  .type = AVMEDIA_TYPE_AUDIO,
232  .priv_data_size = sizeof(EightSvxContext),
236  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
237  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
239 };
const struct AVCodec * codec
Definition: avcodec.h:1059
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:62
static const int8_t fibonacci[16]
Definition: 8svx.c:47
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
#define MAX_FRAME_SIZE
Definition: 8svx.c:52
int size
Definition: avcodec.h:974
AVCodec.
Definition: avcodec.h:2812
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
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1815
uint8_t
#define av_cold
Definition: attributes.h:66
static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr, AVPacket *avpkt)
decode a frame
Definition: 8svx.c:83
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
AVCodec ff_eightsvx_fib_decoder
Definition: 8svx.c:199
uint8_t * data
Definition: avcodec.h:973
static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
Definition: 8svx.c:189
static const int8_t exponential[16]
Definition: 8svx.c:49
enum AVCodecID id
Definition: avcodec.h:2826
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: avcodec.h:713
#define AVERROR(e)
Definition: error.h:43
sample_fmts
Definition: avconv_filter.c:68
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
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:60
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
AVCodec ff_eightsvx_exp_decoder
Definition: 8svx.c:213
#define FFMIN(a, b)
Definition: common.h:57
const int8_t * table
Definition: 8svx.c:38
unsigned 8 bits, planar
Definition: samplefmt.h:69
if(ac->has_optimized_func)
int data_idx
Definition: 8svx.c:44
Libavcodec external API header.
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:61
enum AVCodecID codec_id
Definition: avcodec.h:1067
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
main external API structure.
Definition: avcodec.h:1050
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:490
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t fib_acc[2]
Definition: 8svx.c:37
static uint32_t state
Definition: trasher.c:27
int data_size
Definition: 8svx.c:43
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
AVCodec ff_pcm_s8_planar_decoder
Definition: 8svx.c:227
decoder context
Definition: 8svx.c:36
common internal api header.
common internal and external API header
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
initialize 8svx decoder
Definition: 8svx.c:163
void * priv_data
Definition: avcodec.h:1092
uint8_t * data[2]
Definition: 8svx.c:42
int channels
number of audio channels
Definition: avcodec.h:1808
static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
Definition: 8svx.c:76
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