avconv: improve sample format negotiation for decoder request
[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"
1d9c2dc8 32#include "libavutil/common.h"
cb21e0a7 33
1b41f260 34/** decoder context */
cb21e0a7 35typedef struct EightSvxContext {
0eea2129 36 AVFrame frame;
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,
1993c684 61 uint8_t *state, const int8_t *table, int channels)
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]);
1993c684
JR
68 *dst = val;
69 dst += channels;
e3718784 70 val = av_clip_uint8(val + table[d >> 4]);
1993c684
JR
71 *dst = val;
72 dst += channels;
ac68607b
JR
73 }
74
75 *state = val;
76}
77
f1f6d361
JR
78static void raw_decode(uint8_t *dst, const int8_t *src, int src_size,
79 int channels)
80{
81 while (src_size--) {
82 *dst = *src++ + 128;
83 dst += channels;
84 }
85}
86
1b41f260 87/** decode a frame */
0eea2129
JR
88static int eightsvx_decode_frame(AVCodecContext *avctx, void *data,
89 int *got_frame_ptr, AVPacket *avpkt)
cb21e0a7
JM
90{
91 EightSvxContext *esc = avctx->priv_data;
1993c684 92 int buf_size;
0eea2129
JR
93 uint8_t *out_data;
94 int ret;
36ef5369 95 int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
1993c684
JR
96
97 /* for the first packet, copy data to buffer */
98 if (avpkt->data) {
f1f6d361
JR
99 int hdr_size = is_compr ? 2 : 0;
100 int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
cb21e0a7 101
f1f6d361 102 if (avpkt->size < hdr_size * avctx->channels) {
0ac3b8fc
JR
103 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
104 return AVERROR(EINVAL);
105 }
1993c684
JR
106 if (esc->data[0]) {
107 av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
108 return AVERROR(EINVAL);
109 }
110
f1f6d361 111 if (is_compr) {
1993c684
JR
112 esc->fib_acc[0] = avpkt->data[1] + 128;
113 if (avctx->channels == 2)
114 esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
f1f6d361 115 }
1993c684
JR
116
117 esc->data_idx = 0;
118 esc->data_size = chan_size;
119 if (!(esc->data[0] = av_malloc(chan_size)))
120 return AVERROR(ENOMEM);
121 if (avctx->channels == 2) {
122 if (!(esc->data[1] = av_malloc(chan_size))) {
123 av_freep(&esc->data[0]);
124 return AVERROR(ENOMEM);
125 }
126 }
f1f6d361 127 memcpy(esc->data[0], &avpkt->data[hdr_size], chan_size);
1993c684 128 if (avctx->channels == 2)
f1f6d361 129 memcpy(esc->data[1], &avpkt->data[2*hdr_size+chan_size], chan_size);
1993c684
JR
130 }
131 if (!esc->data[0]) {
132 av_log(avctx, AV_LOG_ERROR, "unexpected empty packet\n");
133 return AVERROR(EINVAL);
cb21e0a7
JM
134 }
135
1993c684
JR
136 /* decode next piece of data from the buffer */
137 buf_size = FFMIN(MAX_FRAME_SIZE, esc->data_size - esc->data_idx);
138 if (buf_size <= 0) {
0eea2129 139 *got_frame_ptr = 0;
1993c684
JR
140 return avpkt->size;
141 }
0eea2129
JR
142
143 /* get output buffer */
144 esc->frame.nb_samples = buf_size * (is_compr + 1);
145 if ((ret = avctx->get_buffer(avctx, &esc->frame)) < 0) {
146 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
147 return ret;
fda459ce 148 }
0eea2129
JR
149 out_data = esc->frame.data[0];
150
f1f6d361 151 if (is_compr) {
1993c684
JR
152 delta_decode(out_data, &esc->data[0][esc->data_idx], buf_size,
153 &esc->fib_acc[0], esc->table, avctx->channels);
154 if (avctx->channels == 2) {
155 delta_decode(&out_data[1], &esc->data[1][esc->data_idx], buf_size,
156 &esc->fib_acc[1], esc->table, avctx->channels);
157 }
f1f6d361
JR
158 } else {
159 int ch;
160 for (ch = 0; ch < avctx->channels; ch++) {
161 raw_decode((int8_t *)&out_data[ch], &esc->data[ch][esc->data_idx],
162 buf_size, avctx->channels);
163 }
164 }
1993c684 165 esc->data_idx += buf_size;
0eea2129
JR
166
167 *got_frame_ptr = 1;
168 *(AVFrame *)data = esc->frame;
cb21e0a7 169
1993c684 170 return avpkt->size;
cb21e0a7
JM
171}
172
1b41f260 173/** initialize 8svx decoder */
cb21e0a7
JM
174static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
175{
176 EightSvxContext *esc = avctx->priv_data;
177
1993c684
JR
178 if (avctx->channels < 1 || avctx->channels > 2) {
179 av_log(avctx, AV_LOG_ERROR, "8SVX does not support more than 2 channels\n");
180 return AVERROR(EINVAL);
181 }
182
cb21e0a7 183 switch(avctx->codec->id) {
36ef5369 184 case AV_CODEC_ID_8SVX_FIB:
cb21e0a7
JM
185 esc->table = fibonacci;
186 break;
36ef5369 187 case AV_CODEC_ID_8SVX_EXP:
cb21e0a7
JM
188 esc->table = exponential;
189 break;
36ef5369 190 case AV_CODEC_ID_PCM_S8_PLANAR:
f1f6d361 191 break;
cb21e0a7
JM
192 default:
193 return -1;
194 }
e3718784 195 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
0eea2129
JR
196
197 avcodec_get_frame_defaults(&esc->frame);
198 avctx->coded_frame = &esc->frame;
199
cb21e0a7
JM
200 return 0;
201}
202
1993c684
JR
203static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
204{
205 EightSvxContext *esc = avctx->priv_data;
206
207 av_freep(&esc->data[0]);
208 av_freep(&esc->data[1]);
209
210 return 0;
211}
212
d36beb3f 213AVCodec ff_eightsvx_fib_decoder = {
99e3913d 214 .name = "8svx_fib",
72415b2a 215 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 216 .id = AV_CODEC_ID_8SVX_FIB,
cb21e0a7
JM
217 .priv_data_size = sizeof (EightSvxContext),
218 .init = eightsvx_decode_init,
1993c684 219 .close = eightsvx_decode_close,
cb21e0a7 220 .decode = eightsvx_decode_frame,
0eea2129 221 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
fe4bf374 222 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
cb21e0a7
JM
223};
224
d36beb3f 225AVCodec ff_eightsvx_exp_decoder = {
99e3913d 226 .name = "8svx_exp",
72415b2a 227 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 228 .id = AV_CODEC_ID_8SVX_EXP,
cb21e0a7
JM
229 .priv_data_size = sizeof (EightSvxContext),
230 .init = eightsvx_decode_init,
1993c684 231 .close = eightsvx_decode_close,
cb21e0a7 232 .decode = eightsvx_decode_frame,
0eea2129 233 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
fe4bf374 234 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
cb21e0a7 235};
f1f6d361
JR
236
237AVCodec ff_pcm_s8_planar_decoder = {
238 .name = "pcm_s8_planar",
239 .type = AVMEDIA_TYPE_AUDIO,
36ef5369 240 .id = AV_CODEC_ID_PCM_S8_PLANAR,
f1f6d361
JR
241 .priv_data_size = sizeof(EightSvxContext),
242 .init = eightsvx_decode_init,
243 .close = eightsvx_decode_close,
244 .decode = eightsvx_decode_frame,
0eea2129 245 .capabilities = CODEC_CAP_DELAY | CODEC_CAP_DR1,
f1f6d361
JR
246 .long_name = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
247};