]> git.sesse.net Git - ffmpeg/blob - libavcodec/8svx.c
libstagefright: limit the output queue size
[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
33 /** decoder context */
34 typedef struct EightSvxContext {
35     int16_t fib_acc;
36     const int16_t *table;
37 } EightSvxContext;
38
39 static const int16_t fibonacci[16]   = { -34<<8, -21<<8, -13<<8,  -8<<8, -5<<8, -3<<8, -2<<8, -1<<8,
40                                           0, 1<<8, 2<<8, 3<<8, 5<<8, 8<<8, 13<<8, 21<<8 };
41 static const int16_t exponential[16] = { -128<<8, -64<<8, -32<<8, -16<<8, -8<<8, -4<<8, -2<<8, -1<<8,
42                                           0, 1<<8, 2<<8, 4<<8, 8<<8, 16<<8, 32<<8, 64<<8 };
43
44 /** decode a frame */
45 static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
46                                  AVPacket *avpkt)
47 {
48     const uint8_t *buf = avpkt->data;
49     int buf_size = avpkt->size;
50     EightSvxContext *esc = avctx->priv_data;
51     int16_t *out_data = data;
52     int consumed = buf_size;
53     const uint8_t *buf_end = buf + buf_size;
54
55     if((*data_size >> 2) < buf_size)
56         return -1;
57
58     if(avctx->frame_number == 0) {
59         esc->fib_acc = buf[1] << 8;
60         buf_size -= 2;
61         buf += 2;
62     }
63
64     *data_size = buf_size << 2;
65
66     while(buf < buf_end) {
67         uint8_t d = *buf++;
68         esc->fib_acc += esc->table[d & 0x0f];
69         *out_data++ = esc->fib_acc;
70         esc->fib_acc += esc->table[d >> 4];
71         *out_data++ = esc->fib_acc;
72     }
73
74     return consumed;
75 }
76
77 /** initialize 8svx decoder */
78 static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
79 {
80     EightSvxContext *esc = avctx->priv_data;
81
82     switch(avctx->codec->id) {
83         case CODEC_ID_8SVX_FIB:
84           esc->table = fibonacci;
85           break;
86         case CODEC_ID_8SVX_EXP:
87           esc->table = exponential;
88           break;
89         default:
90           return -1;
91     }
92     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
93     return 0;
94 }
95
96 AVCodec ff_eightsvx_fib_decoder = {
97   .name           = "8svx_fib",
98   .type           = AVMEDIA_TYPE_AUDIO,
99   .id             = CODEC_ID_8SVX_FIB,
100   .priv_data_size = sizeof (EightSvxContext),
101   .init           = eightsvx_decode_init,
102   .decode         = eightsvx_decode_frame,
103   .long_name      = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
104 };
105
106 AVCodec ff_eightsvx_exp_decoder = {
107   .name           = "8svx_exp",
108   .type           = AVMEDIA_TYPE_AUDIO,
109   .id             = CODEC_ID_8SVX_EXP,
110   .priv_data_size = sizeof (EightSvxContext),
111   .init           = eightsvx_decode_init,
112   .decode         = eightsvx_decode_frame,
113   .long_name      = NULL_IF_CONFIG_SMALL("8SVX exponential"),
114 };