adx: change short to int16_t
[libav.git] / libavcodec / adxdec.c
CommitLineData
f5610466
MM
1/*
2 * ADX ADPCM codecs
3 * Copyright (c) 2001,2003 BERO
4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
f5610466
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
f5610466 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
f5610466
MM
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
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f5610466 20 */
6a5d31ac
DB
21
22#include "libavutil/intreadwrite.h"
f5610466 23#include "avcodec.h"
c31dea61 24#include "adx.h"
f5610466
MM
25
26/**
ba87f080 27 * @file
f5610466
MM
28 * SEGA CRI adx codecs.
29 *
30 * Reference documents:
31 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
32 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
33 */
34
defa0cd6 35static av_cold int adx_decode_init(AVCodecContext *avctx)
fd76c37f 36{
5d6e4c16 37 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
fd76c37f
PR
38 return 0;
39}
40
f19af812 41/* 18 bytes <-> 32 samples */
f5610466 42
e0722d7f 43static void adx_decode(int16_t *out,const unsigned char *in,
dd1b9f7c 44 ADXChannelState *prev)
f5610466 45{
67a5daf0 46 int scale = AV_RB16(in);
f19af812
MM
47 int i;
48 int s0,s1,s2,d;
49
50// printf("%x ",scale);
51
52 in+=2;
53 s1 = prev->s1;
54 s2 = prev->s2;
55 for(i=0;i<16;i++) {
56 d = in[i];
57 // d>>=4; if (d&8) d-=16;
58 d = ((signed char)d >> 4);
59 s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
f19af812 60 s2 = s1;
295f3737
AJ
61 s1 = av_clip_int16(s0);
62 *out++=s1;
f19af812
MM
63
64 d = in[i];
65 //d&=15; if (d&8) d-=16;
66 d = ((signed char)(d<<4) >> 4);
67 s0 = (BASEVOL*d*scale + SCALE1*s1 - SCALE2*s2)>>14;
f19af812 68 s2 = s1;
295f3737
AJ
69 s1 = av_clip_int16(s0);
70 *out++=s1;
f19af812
MM
71 }
72 prev->s1 = s1;
73 prev->s2 = s2;
f5610466
MM
74
75}
76
e0722d7f 77static void adx_decode_stereo(int16_t *out,const unsigned char *in,
dd1b9f7c 78 ADXChannelState *prev)
f5610466 79{
f19af812
MM
80 short tmp[32*2];
81 int i;
82
83 adx_decode(tmp ,in ,prev);
84 adx_decode(tmp+32,in+18,prev+1);
85 for(i=0;i<32;i++) {
86 out[i*2] = tmp[i];
87 out[i*2+1] = tmp[i+32];
88 }
f5610466
MM
89}
90
c4cc3610
AJ
91/* return data offset or 0 */
92static int adx_decode_header(AVCodecContext *avctx,const unsigned char *buf,size_t bufsize)
f5610466 93{
c4cc3610 94 int offset;
f5610466 95
f19af812 96 if (buf[0]!=0x80) return 0;
67a5daf0 97 offset = (AV_RB32(buf)^0x80000000)+4;
f19af812 98 if (bufsize<offset || memcmp(buf+offset-6,"(c)CRI",6)) return 0;
f5610466 99
c4cc3610
AJ
100 avctx->channels = buf[7];
101 avctx->sample_rate = AV_RB32(buf+8);
102 avctx->bit_rate = avctx->sample_rate*avctx->channels*18*8/32;
f5610466 103
f19af812 104 return offset;
f5610466
MM
105}
106
f5610466 107static int adx_decode_frame(AVCodecContext *avctx,
f19af812 108 void *data, int *data_size,
7a00bbad 109 AVPacket *avpkt)
f5610466 110{
7a00bbad
TB
111 const uint8_t *buf0 = avpkt->data;
112 int buf_size = avpkt->size;
f19af812 113 ADXContext *c = avctx->priv_data;
e0722d7f 114 int16_t *samples = data;
f19af812
MM
115 const uint8_t *buf = buf0;
116 int rest = buf_size;
117
118 if (!c->header_parsed) {
119 int hdrsize = adx_decode_header(avctx,buf,rest);
120 if (hdrsize==0) return -1;
121 c->header_parsed = 1;
122 buf += hdrsize;
123 rest -= hdrsize;
124 }
125
4783cf0d
KS
126 /* 18 bytes of data are expanded into 32*2 bytes of audio,
127 so guard against buffer overflows */
128 if(rest/18 > *data_size/64)
129 rest = (*data_size/64) * 18;
130
f19af812
MM
131 if (c->in_temp) {
132 int copysize = 18*avctx->channels - c->in_temp;
133 memcpy(c->dec_temp+c->in_temp,buf,copysize);
134 rest -= copysize;
135 buf += copysize;
136 if (avctx->channels==1) {
137 adx_decode(samples,c->dec_temp,c->prev);
138 samples += 32;
139 } else {
140 adx_decode_stereo(samples,c->dec_temp,c->prev);
141 samples += 32*2;
142 }
143 }
144 //
145 if (avctx->channels==1) {
146 while(rest>=18) {
147 adx_decode(samples,buf,c->prev);
148 rest-=18;
149 buf+=18;
150 samples+=32;
151 }
152 } else {
153 while(rest>=18*2) {
154 adx_decode_stereo(samples,buf,c->prev);
155 rest-=18*2;
156 buf+=18*2;
157 samples+=32*2;
158 }
159 }
160 //
161 c->in_temp = rest;
162 if (rest) {
163 memcpy(c->dec_temp,buf,rest);
164 buf+=rest;
165 }
166 *data_size = (uint8_t*)samples - (uint8_t*)data;
167// printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
168 return buf-buf0;
f5610466
MM
169}
170
d36beb3f 171AVCodec ff_adpcm_adx_decoder = {
ec6402b7
AK
172 .name = "adpcm_adx",
173 .type = AVMEDIA_TYPE_AUDIO,
174 .id = CODEC_ID_ADPCM_ADX,
175 .priv_data_size = sizeof(ADXContext),
176 .init = adx_decode_init,
177 .decode = adx_decode_frame,
0ffbc258 178 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
f5610466
MM
179};
180