build: Drop stray golomb dependencies
[libav.git] / libavcodec / 8svx.c
CommitLineData
cb21e0a7 1/*
4477dedc 2 * 8SVX audio decoder
cb21e0a7
JM
3 * Copyright (C) 2008 Jaikrishnan Menon
4 *
2912e87a 5 * This file is part of Libav.
cb21e0a7 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
cb21e0a7
JM
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
cb21e0a7
JM
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
cb21e0a7
JM
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
ba87f080 23 * @file
cb21e0a7
JM
24 * 8svx audio decoder
25 * @author Jaikrishnan Menon
ec679eb4 26 *
cb21e0a7
JM
27 * supports: fibonacci delta encoding
28 * : exponential encoding
29 */
30
31#include "avcodec.h"
594d4d5d 32#include "internal.h"
1d9c2dc8 33#include "libavutil/common.h"
cb21e0a7 34
1b41f260 35/** decoder context */
cb21e0a7 36typedef struct EightSvxContext {
1993c684 37 uint8_t fib_acc[2];
e3718784 38 const int8_t *table;
1993c684
JR
39
40 /* buffer used to store the whole first packet.
41 data is only sent as one large packet */
42 uint8_t *data[2];
43 int data_size;
44 int data_idx;
cb21e0a7
JM
45} EightSvxContext;
46
e3718784
JR
47static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
48 0, 1, 2, 3, 5, 8, 13, 21 };
49static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
50 0, 1, 2, 4, 8, 16, 32, 64 };
cb21e0a7 51
1993c684
JR
52#define MAX_FRAME_SIZE 32768
53
ac68607b
JR
54/**
55 * Delta decode the compressed values in src, and put the resulting
56 * decoded samples in dst.
57 *
58 * @param[in,out] state starting value. it is saved for use in the next call.
59 */
e3718784 60static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
cf8c93ad 61 uint8_t *state, const int8_t *table)
ac68607b 62{
e3718784 63 uint8_t val = *state;
ac68607b
JR
64
65 while (src_size--) {
66 uint8_t d = *src++;
e3718784 67 val = av_clip_uint8(val + table[d & 0xF]);
cf8c93ad 68 *dst++ = val;
e3718784 69 val = av_clip_uint8(val + table[d >> 4]);
cf8c93ad 70 *dst++ = val;
ac68607b
JR
71 }
72
73 *state = val;
74}
75
cf8c93ad 76static void raw_decode(uint8_t *dst, const int8_t *src, int src_size)
f1f6d361 77{
cf8c93ad
JR
78 while (src_size--)
79 *dst++ = *src++ + 128;
f1f6d361
JR
80}
81
1b41f260 82/** decode a frame */
0eea2129
JR
83static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
84 int *got_frame_ptr, AVPacket *avpkt)
cb21e0a7
JM
85{
86 EightSvxContext *esc = avctx->priv_data;
a3de4010 87 AVFrame *frame = data;
1993c684 88 int buf_size;
cf8c93ad 89 int ch, ret;
36ef5369 90 int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
1993c684
JR
91
92 /* for the first packet, copy data to buffer */
93 if (avpkt->data) {
f1f6d361
JR
94 int hdr_size = is_compr ? 2 : 0;
95 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
cb21e0a7 96
f1f6d361 97 if (avpkt->size < hdr_size * avctx->channels) {
0ac3b8fc 98 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
74512f7e 99 return AVERROR_INVALIDDATA;
0ac3b8fc 100 }
1993c684
JR
101 if (esc->data[0]) {
102 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
74512f7e 103 return AVERROR_INVALIDDATA;
1993c684
JR
104 }
105
f1f6d361 106 if (is_compr) {
1993c684
JR
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;
f1f6d361 110 }
1993c684
JR
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 }
f1f6d361 122 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
1993c684 123 if (avctx->channels == 2)
f1f6d361 124 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
1993c684
JR
125 }
126 if (!esc->data[0]) {
127 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
74512f7e 128 return AVERROR_INVALIDDATA;
cb21e0a7
JM
129 }
130
1993c684
JR
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) {
0eea2129 134 *got_frame_ptr = 0;
1993c684
JR
135 return avpkt->size;
136 }
0eea2129
JR
137
138 /* get output buffer */
a3de4010 139 frame->nb_samples = buf_size * (is_compr + 1);
759001c5 140 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) {
0eea2129
JR
141 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
142 return ret;
fda459ce 143 }
cf8c93ad
JR
144
145 for (ch = 0; ch < avctx->channels; ch++) {
146 if (is_compr) {
a3de4010 147 delta_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
cf8c93ad
JR
148 buf_size, &esc->fib_acc[ch], esc->table);
149 } else {
a3de4010 150 raw_decode(frame->data[ch], &esc->data[ch][esc->data_idx],
cf8c93ad 151 buf_size);
f1f6d361
JR
152 }
153 }
cf8c93ad 154
1993c684 155 esc->data_idx += buf_size;
0eea2129 156
a3de4010 157 *got_frame_ptr = 1;
cb21e0a7 158
1993c684 159 return avpkt->size;
cb21e0a7
JM
160}
161
1b41f260 162/** initialize 8svx decoder */
cb21e0a7
JM
163static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
164{
165 EightSvxContext *esc = avctx->priv_data;
166
1993c684
JR
167 if (avctx->channels < 1 || avctx->channels > 2) {
168 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
74512f7e 169 return AVERROR_INVALIDDATA;
1993c684
JR
170 }
171
cb21e0a7 172 switch(avctx->codec->id) {
36ef5369 173 case AV_CODEC_ID_8SVX_FIB:
cb21e0a7
JM
174 esc->table = fibonacci;
175 break;
36ef5369 176 case AV_CODEC_ID_8SVX_EXP:
cb21e0a7
JM
177 esc->table = exponential;
178 break;
36ef5369 179 case AV_CODEC_ID_PCM_S8_PLANAR:
f1f6d361 180 break;
cb21e0a7 181 default:
74512f7e 182 return AVERROR_INVALIDDATA;
cb21e0a7 183 }
cf8c93ad 184 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
0eea2129 185
cb21e0a7
JM
186 return 0;
187}
188
1993c684
JR
189static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
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
d36beb3f 199AVCodec ff_eightsvx_fib_decoder = {
99e3913d 200 .name = "8svx_fib",
b2bed932 201 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
72415b2a 202 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 203 .id = AV_CODEC_ID_8SVX_FIB,
cb21e0a7
JM
204 .priv_data_size = sizeof (EightSvxContext),
205 .init = eightsvx_decode_init,
1993c684 206 .close = eightsvx_decode_close,
cb21e0a7 207 .decode = eightsvx_decode_frame,
def97856 208 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
cf8c93ad
JR
209 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
210 AV_SAMPLE_FMT_NONE },
cb21e0a7
JM
211};
212
d36beb3f 213AVCodec ff_eightsvx_exp_decoder = {
99e3913d 214 .name = "8svx_exp",
b2bed932 215 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
72415b2a 216 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 217 .id = AV_CODEC_ID_8SVX_EXP,
cb21e0a7
JM
218 .priv_data_size = sizeof (EightSvxContext),
219 .init = eightsvx_decode_init,
1993c684 220 .close = eightsvx_decode_close,
cb21e0a7 221 .decode = eightsvx_decode_frame,
def97856 222 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
cf8c93ad
JR
223 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
224 AV_SAMPLE_FMT_NONE },
cb21e0a7 225};
f1f6d361
JR
226
227AVCodec ff_pcm_s8_planar_decoder = {
228 .name = "pcm_s8_planar",
b2bed932 229 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
f1f6d361 230 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 231 .id = AV_CODEC_ID_PCM_S8_PLANAR,
f1f6d361
JR
232 .priv_data_size = sizeof(EightSvxContext),
233 .init = eightsvx_decode_init,
234 .close = eightsvx_decode_close,
235 .decode = eightsvx_decode_frame,
def97856 236 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_DR1,
cf8c93ad
JR
237 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
238 AV_SAMPLE_FMT_NONE },
f1f6d361 239};