8svx: log an error message if output buffer is too small
[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"
32
1b41f260 33/** decoder context */
cb21e0a7 34typedef struct EightSvxContext {
e3718784
JR
35 uint8_t fib_acc;
36 const int8_t *table;
cb21e0a7
JM
37} EightSvxContext;
38
e3718784
JR
39static const int8_t fibonacci[16] = { -34, -21, -13, -8, -5, -3, -2, -1,
40 0, 1, 2, 3, 5, 8, 13, 21 };
41static const int8_t exponential[16] = { -128, -64, -32, -16, -8, -4, -2, -1,
42 0, 1, 2, 4, 8, 16, 32, 64 };
cb21e0a7 43
ac68607b
JR
44/**
45 * Delta decode the compressed values in src, and put the resulting
46 * decoded samples in dst.
47 *
48 * @param[in,out] state starting value. it is saved for use in the next call.
49 */
e3718784
JR
50static void delta_decode(uint8_t *dst, const uint8_t *src, int src_size,
51 uint8_t *state, const int8_t *table)
ac68607b 52{
e3718784 53 uint8_t val = *state;
ac68607b
JR
54
55 while (src_size--) {
56 uint8_t d = *src++;
e3718784 57 val = av_clip_uint8(val + table[d & 0xF]);
ac68607b 58 *dst++ = val;
e3718784 59 val = av_clip_uint8(val + table[d >> 4]);
ac68607b
JR
60 *dst++ = val;
61 }
62
63 *state = val;
64}
65
1b41f260 66/** decode a frame */
cb21e0a7 67static int eightsvx_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
7a00bbad 68 AVPacket *avpkt)
cb21e0a7 69{
7a00bbad
TB
70 const uint8_t *buf = avpkt->data;
71 int buf_size = avpkt->size;
cb21e0a7 72 EightSvxContext *esc = avctx->priv_data;
e3718784 73 uint8_t *out_data = data;
cb21e0a7 74 int consumed = buf_size;
cb21e0a7
JM
75
76 if(avctx->frame_number == 0) {
0ac3b8fc
JR
77 if (buf_size < 2) {
78 av_log(avctx, AV_LOG_ERROR, "packet size is too small\n");
79 return AVERROR(EINVAL);
80 }
e3718784 81 esc->fib_acc = (int8_t)buf[1] + 128;
cb21e0a7
JM
82 buf_size -= 2;
83 buf += 2;
84 }
85
fda459ce
JR
86 if (*data_size < buf_size * 2) {
87 av_log(avctx, AV_LOG_ERROR, "Provided buffer with size %d is too small.\n",
88 *data_size);
e3718784 89 return AVERROR(EINVAL);
fda459ce 90 }
cb21e0a7 91
ac68607b 92 delta_decode(out_data, buf, buf_size, &esc->fib_acc, esc->table);
cb21e0a7 93
e3718784
JR
94 *data_size = buf_size * 2;
95
cb21e0a7
JM
96 return consumed;
97}
98
1b41f260 99/** initialize 8svx decoder */
cb21e0a7
JM
100static av_cold int eightsvx_decode_init(AVCodecContext *avctx)
101{
102 EightSvxContext *esc = avctx->priv_data;
103
104 switch(avctx->codec->id) {
105 case CODEC_ID_8SVX_FIB:
106 esc->table = fibonacci;
107 break;
108 case CODEC_ID_8SVX_EXP:
109 esc->table = exponential;
110 break;
111 default:
112 return -1;
113 }
e3718784 114 avctx->sample_fmt = AV_SAMPLE_FMT_U8;
cb21e0a7
JM
115 return 0;
116}
117
d36beb3f 118AVCodec ff_eightsvx_fib_decoder = {
99e3913d 119 .name = "8svx_fib",
72415b2a 120 .type = AVMEDIA_TYPE_AUDIO,
cb21e0a7
JM
121 .id = CODEC_ID_8SVX_FIB,
122 .priv_data_size = sizeof (EightSvxContext),
123 .init = eightsvx_decode_init,
124 .decode = eightsvx_decode_frame,
fe4bf374 125 .long_name = NULL_IF_CONFIG_SMALL("8SVX fibonacci"),
cb21e0a7
JM
126};
127
d36beb3f 128AVCodec ff_eightsvx_exp_decoder = {
99e3913d 129 .name = "8svx_exp",
72415b2a 130 .type = AVMEDIA_TYPE_AUDIO,
cb21e0a7
JM
131 .id = CODEC_ID_8SVX_EXP,
132 .priv_data_size = sizeof (EightSvxContext),
133 .init = eightsvx_decode_init,
134 .decode = eightsvx_decode_frame,
fe4bf374 135 .long_name = NULL_IF_CONFIG_SMALL("8SVX exponential"),
cb21e0a7 136};