]> git.sesse.net Git - ffmpeg/blob - libavcodec/8svx.c
amrwbdec: set channels, channel_layout, and sample_rate
[ffmpeg] / libavcodec / 8svx.c
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
22 /**
23  * @file
24  * 8svx audio decoder
25  * @author Jaikrishnan Menon
26  *
27  * supports: fibonacci delta encoding
28  *         : exponential encoding
29  */
30
31 #include "avcodec.h"
32 #include "libavutil/common.h"
33
34 /** decoder context */
35 typedef struct EightSvxContext {
36     AVFrame frame;
37     uint8_t fib_acc[2];
38     const int8_t *table;
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;
45 } EightSvxContext;
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
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  */
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
82 /** decode a frame */
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     int buf_size;
88     int ch, ret;
89     int is_compr = (avctx->codec_id != AV_CODEC_ID_PCM_S8_PLANAR);
90
91     /* for the first packet, copy data to buffer */
92     if (avpkt->data) {
93         int hdr_size  = is_compr ? 2 : 0;
94         int chan_size = (avpkt->size - hdr_size * avctx->channels) / avctx->channels;
95
96         if (avpkt->size < hdr_size * avctx->channels) {
97             av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
98             return AVERROR(EINVAL);
99         }
100         if (esc->data[0]) {
101             av_log(avctx, AV_LOG_ERROR, "unexpected data after first packet\n");
102             return AVERROR(EINVAL);
103         }
104
105         if (is_compr) {
106         esc->fib_acc[0] = avpkt->data[1] + 128;
107         if (avctx->channels == 2)
108             esc->fib_acc[1] = avpkt->data[2+chan_size+1] + 128;
109         }
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 * (is_compr + 1);
139     if ((ret = avctx->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         if (is_compr) {
146             delta_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
147                          buf_size, &esc->fib_acc[ch], esc->table);
148         } else {
149             raw_decode(esc->frame.data[ch], &esc->data[ch][esc->data_idx],
150                        buf_size);
151         }
152     }
153
154     esc->data_idx += buf_size;
155
156     *got_frame_ptr   = 1;
157     *(AVFrame *)data = esc->frame;
158
159     return avpkt->size;
160 }
161
162 /** initialize 8svx decoder */
163 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
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) {
173         case AV_CODEC_ID_8SVX_FIB:
174           esc->table = fibonacci;
175           break;
176         case AV_CODEC_ID_8SVX_EXP:
177           esc->table = exponential;
178           break;
179         case AV_CODEC_ID_PCM_S8_PLANAR:
180             break;
181         default:
182           return -1;
183     }
184     avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
185
186     avcodec_get_frame_defaults(&esc->frame);
187     avctx->coded_frame = &esc->frame;
188
189     return 0;
190 }
191
192 static av_cold int eightsvx_decode_close(AVCodecContext *avctx)
193 {
194     EightSvxContext *esc = avctx->priv_data;
195
196     av_freep(&esc->data[0]);
197     av_freep(&esc->data[1]);
198
199     return 0;
200 }
201
202 AVCodec ff_eightsvx_fib_decoder = {
203   .name           = "8svx_fib",
204   .type           = AVMEDIA_TYPE_AUDIO,
205   .id             = AV_CODEC_ID_8SVX_FIB,
206   .priv_data_size = sizeof (EightSvxContext),
207   .init           = eightsvx_decode_init,
208   .close          = eightsvx_decode_close,
209   .decode         = eightsvx_decode_frame,
210   .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
211   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
212   .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
213                                                     AV_SAMPLE_FMT_NONE },
214 };
215
216 AVCodec ff_eightsvx_exp_decoder = {
217   .name           = "8svx_exp",
218   .type           = AVMEDIA_TYPE_AUDIO,
219   .id             = AV_CODEC_ID_8SVX_EXP,
220   .priv_data_size = sizeof (EightSvxContext),
221   .init           = eightsvx_decode_init,
222   .close          = eightsvx_decode_close,
223   .decode         = eightsvx_decode_frame,
224   .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
225   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
226   .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
227                                                     AV_SAMPLE_FMT_NONE },
228 };
229
230 AVCodec ff_pcm_s8_planar_decoder = {
231     .name           = "pcm_s8_planar",
232     .type           = AVMEDIA_TYPE_AUDIO,
233     .id             = AV_CODEC_ID_PCM_S8_PLANAR,
234     .priv_data_size = sizeof(EightSvxContext),
235     .init           = eightsvx_decode_init,
236     .close          = eightsvx_decode_close,
237     .decode         = eightsvx_decode_frame,
238     .capabilities   = CODEC_CAP_DELAY | CODEC_CAP_DR1,
239     .long_name      = NULL_IF_CONFIG_SMALL("PCM signed 8-bit planar"),
240     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
241                                                       AV_SAMPLE_FMT_NONE },
242 };