3 * Copyright (c) 2001,2003 BERO
5 * This file is part of FFmpeg.
7 * FFmpeg 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.
12 * FFmpeg 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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * SEGA CRI adx codecs.
28 * Reference documents:
29 * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
30 * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
33 /* 18 bytes <-> 32 samples */
35 static void adx_decode(short *out
,const unsigned char *in
,PREV
*prev
)
37 int scale
= AV_RB16(in
);
41 // printf("%x ",scale);
48 // d>>=4; if (d&8) d-=16;
49 d
= ((signed char)d
>> 4);
50 s0
= (BASEVOL
*d
*scale
+ SCALE1
*s1
- SCALE2
*s2
)>>14;
52 s1
= av_clip_int16(s0
);
56 //d&=15; if (d&8) d-=16;
57 d
= ((signed char)(d
<<4) >> 4);
58 s0
= (BASEVOL
*d
*scale
+ SCALE1
*s1
- SCALE2
*s2
)>>14;
60 s1
= av_clip_int16(s0
);
68 static void adx_decode_stereo(short *out
,const unsigned char *in
,PREV
*prev
)
73 adx_decode(tmp
,in
,prev
);
74 adx_decode(tmp
+32,in
+18,prev
+1);
77 out
[i
*2+1] = tmp
[i
+32];
81 /* return data offset or 0 */
82 static int adx_decode_header(AVCodecContext
*avctx
,const unsigned char *buf
,size_t bufsize
)
86 if (buf
[0]!=0x80) return 0;
87 offset
= (AV_RB32(buf
)^0x80000000)+4;
88 if (bufsize
<offset
|| memcmp(buf
+offset
-6,"(c)CRI",6)) return 0;
90 avctx
->channels
= buf
[7];
91 avctx
->sample_rate
= AV_RB32(buf
+8);
92 avctx
->bit_rate
= avctx
->sample_rate
*avctx
->channels
*18*8/32;
97 static int adx_decode_frame(AVCodecContext
*avctx
,
98 void *data
, int *data_size
,
99 const uint8_t *buf0
, int buf_size
)
101 ADXContext
*c
= avctx
->priv_data
;
102 short *samples
= data
;
103 const uint8_t *buf
= buf0
;
106 if (!c
->header_parsed
) {
107 int hdrsize
= adx_decode_header(avctx
,buf
,rest
);
108 if (hdrsize
==0) return -1;
109 c
->header_parsed
= 1;
114 /* 18 bytes of data are expanded into 32*2 bytes of audio,
115 so guard against buffer overflows */
116 if(rest
/18 > *data_size
/64)
117 rest
= (*data_size
/64) * 18;
120 int copysize
= 18*avctx
->channels
- c
->in_temp
;
121 memcpy(c
->dec_temp
+c
->in_temp
,buf
,copysize
);
124 if (avctx
->channels
==1) {
125 adx_decode(samples
,c
->dec_temp
,c
->prev
);
128 adx_decode_stereo(samples
,c
->dec_temp
,c
->prev
);
133 if (avctx
->channels
==1) {
135 adx_decode(samples
,buf
,c
->prev
);
142 adx_decode_stereo(samples
,buf
,c
->prev
);
151 memcpy(c
->dec_temp
,buf
,rest
);
154 *data_size
= (uint8_t*)samples
- (uint8_t*)data
;
155 // printf("%d:%d ",buf-buf0,*data_size); fflush(stdout);
159 AVCodec adpcm_adx_decoder
= {
168 .long_name
= NULL_IF_CONFIG_SMALL("SEGA CRI ADX"),