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 {
39  const int8_t *table;
40 
41  /* buffer used to store the whole first packet.
42  data is only sent as one large packet */
44  int data_size;
45  int data_idx;
47 
48 static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
49  0, 1, 2, 3, 5, 8, 13, 21 };
50 static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
51  0, 1, 2, 4, 8, 16, 32, 64 };
52 
53 #define MAX_FRAME_SIZE 32768
54 
61 static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
62  uint8_t *state, const int8_t *table)
63 {
64  uint8_t val = *state;
65 
66  while (src_size--) {
67  uint8_t d = *src++;
68  val = av_clip_uint8(val + table[d & 0xF]);
69  *dst++ = val;
70  val = av_clip_uint8(val + table[d >> 4]);
71  *dst++ = val;
72  }
73 
74  *state = val;
75 }
76 
77 static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
78 {
79  while (src_size--)
80  *dst++ = *src++ + 128;
81 }
82 
84 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
85  int *got_frame_ptr, AVPacket *avpkt)
86 {
87  EightSvxContext *esc = avctx->priv_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  esc->frame.nb_samples = buf_size * (is_compr + 1);
140  if ((ret = ff_get_buffer(avctx, &esc->frame)) < 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(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
148  buf_size, &esc->fib_acc[ch], esc->table);
149  } else {
150  raw_decode(esc->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  *(AVFrame *)data = esc->frame;
159 
160  return avpkt->size;
161 }
162 
165 {
166  EightSvxContext *esc = avctx->priv_data;
167 
168  if (avctx->channels < 1 || avctx->channels > 2) {
169  av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
170  return AVERROR(EINVAL);
171  }
172 
173  switch(avctx->codec->id) {
175  esc->table = fibonacci;
176  break;
178  esc->table = exponential;
179  break;
181  break;
182  default:
183  return -1;
184  }
185  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
186 
188  avctx->coded_frame = &esc->frame;
189 
190  return 0;
191 }
192 
194 {
195  EightSvxContext *esc = avctx->priv_data;
196 
197  av_freep(&esc->data[0]);
198  av_freep(&esc->data[1]);
199 
200  return 0;
201 }
202 
204  .name = "8svx_fib",
205  .type = AVMEDIA_TYPE_AUDIO,
206  .id = AV_CODEC_ID_8SVX_FIB,
207  .priv_data_size = sizeof (EightSvxContext),
211  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
212  .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
213  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
215 };
216 
218  .name = "8svx_exp",
219  .type = AVMEDIA_TYPE_AUDIO,
220  .id = AV_CODEC_ID_8SVX_EXP,
221  .priv_data_size = sizeof (EightSvxContext),
225  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
226  .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
227  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
229 };
230 
232  .name = "pcm_s8_planar",
233  .type = AVMEDIA_TYPE_AUDIO,
235  .priv_data_size = sizeof(EightSvxContext),
239  .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
240  .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
241  .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
243 };