FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 {
49  const int8_t *table;
50 
51  /* buffer used to store the whole first packet.
52  data is only sent as one large packet */
54  int data_size;
55  int data_idx;
57 
58 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1, 0, 1, 2, 3, 5, 8, 13, 21 };
59 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1, 0, 1, 2, 4, 8, 16, 32, 64 };
60 
61 #define MAX_FRAME_SIZE 2048
62 
63 /**
64  * Delta decode the compressed values in src, and put the resulting
65  * decoded samples in dst.
66  *
67  * @param[in,out] state starting value. it is saved for use in the next call.
68  * @param table delta sequence table
69  */
70 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
71  uint8_t *state, const int8_t *table)
72 {
73  uint8_t val = *state;
74 
75  while (src_size--) {
76  uint8_t d = *src++;
77  val = av_clip_uint8(val + table[d & 0xF]);
78  *dst++ = val;
79  val = av_clip_uint8(val + table[d >> 4]);
80  *dst++ = val;
81  }
82 
83  *state = val;
84 }
85 
86 /** decode a frame */
87 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
88  int *got_frame_ptr, AVPacket *avpkt)
89 {
90  EightSvxContext *esc = avctx->priv_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  esc->frame.nb_samples = buf_size * 2;
139  if ((ret = ff_get_buffer(avctx, &esc->frame)) < 0) {
140  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
141  return ret;
142  }
143 
144  for (ch = 0; ch < avctx->channels; ch++) {
145  delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
146  buf_size, &esc->fib_acc[ch], esc->table);
147  }
148 
149  esc->data_idx += buf_size;
150 
151  *got_frame_ptr = 1;
152  *(AVFrame *)data = esc->frame;
153 
154  return ((avctx->frame_number == 0)*hdr_size + buf_size)*avctx->channels;
155 }
156 
158 {
159  EightSvxContext *esc = avctx->priv_data;
160 
161  if (avctx->channels < 1 || avctx->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_log(avctx, AV_LOG_ERROR, "Invalid codec id %d.\n", avctx->codec->id);
171  return AVERROR_INVALIDDATA;
172  }
173  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
174 
176  avctx->coded_frame = &esc->frame;
177 
178  return 0;
179 }
180 
182 {
183  EightSvxContext *esc = avctx->priv_data;
184 
185  av_freep(&esc->data[0]);
186  av_freep(&esc->data[1]);
187  esc->data_size = 0;
188  esc->data_idx = 0;
189 
190  return 0;
191 }
192 
193 #if CONFIG_EIGHTSVX_FIB_DECODER
194 AVCodec ff_eightsvx_fib_decoder = {
195  .name = "8svx_fib",
196  .type = AVMEDIA_TYPE_AUDIO,
197  .id = AV_CODEC_ID_8SVX_FIB,
198  .priv_data_size = sizeof (EightSvxContext),
202  .capabilities = CODEC_CAP_DR1,
203  .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
204  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
206 };
207 #endif
208 #if CONFIG_EIGHTSVX_EXP_DECODER
209 AVCodec ff_eightsvx_exp_decoder = {
210  .name = "8svx_exp",
211  .type = AVMEDIA_TYPE_AUDIO,
212  .id = AV_CODEC_ID_8SVX_EXP,
213  .priv_data_size = sizeof (EightSvxContext),
217  .capabilities = CODEC_CAP_DR1,
218  .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
219  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
221 };
222 #endif