1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/*
* CD-ROM XA ADPCM codecs
* Copyright (c) 1999,2003 BERO
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "avcodec.h"
/**
* @file xa.c
* CD-ROM XA ADPCM codecs.
*
* Reference documents:
* http://ku-www.ss.titech.ac.jp/~yatsushi/xaadpcm.html
* vagpack & depack http://homepages.compuserve.de/bITmASTER32/psx-index.html
* readstr http://www.geocities.co.jp/Playtown/2004/
*/
typedef struct {
int s1,s2;
} PREV;
typedef struct {
PREV prev[2];
} XAContext;
#define CLIP(s) if (s<-32768) s=-32768; else if (s>32767) s=32767
static void xa_decode(short *out,const unsigned char *in,PREV *prev,int inc)
{
const static int f[5][2] = {
{0,0},
{60,0},
{115,-52},
{98,-55},
{122,-60}
};
int i,j;
for(i=0;i<4;i++) {
int shift,filter,f0,f1;
int s_1,s_2;
shift = 12 - (in[4+i*2] & 15);
filter = in[4+i*2] >> 4;
f0 = f[filter][0];
f1 = f[filter][1];
s_1 = prev->s1;
s_2 = prev->s2;
for(j=0;j<28;j++) {
int d,s,t;
d = in[16+i+j*4];
//t = d&0xf;
//if (t>=8) t-=16;
t = (signed char)(d<<4)>>4;
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
CLIP(s);
*out = s;
out += inc;
s_2 = s_1;
s_1 = s;
}
if (inc==2) { /* stereo */
prev->s1 = s_1;
prev->s2 = s_2;
s_1 = prev[1].s1;
s_2 = prev[1].s2;
out = out + 1 - 28*2;
}
shift = 12 - (in[5+i*2] & 15);
filter = in[5+i*2] >> 4;
f0 = f[filter][0];
f1 = f[filter][1];
for(j=0;j<28;j++) {
int d,s,t;
d = in[16+i+j*4];
//t = d>>4;
//if (t>=8) t-=16;
t = (signed char)d >> 4;
s = ( t<<shift ) + ((s_1*f0 + s_2*f1+32)>>6);
CLIP(s);
*out = s;
out += inc;
s_2 = s_1;
s_1 = s;
}
if (inc==2) { /* stereo */
prev[1].s1 = s_1;
prev[1].s2 = s_2;
out -= 1;
} else {
prev[0].s1 = s_1;
prev[0].s2 = s_2;
}
}
}
static int xa_decode_init(AVCodecContext * avctx)
{
XAContext *c = avctx->priv_data;
printf("xa init %d\n",avctx->channels);
c->prev[0].s1 = 0;
c->prev[0].s2 = 0;
c->prev[1].s1 = 0;
c->prev[1].s2 = 0;
switch(avctx->channels) {
case 1:
case 2:
return 0;
default:
return -1;
}
}
static int xa_decode_frame(AVCodecContext *avctx,
void *data, int *data_size,
const uint8_t *buf0, int buf_size)
{
XAContext *c = avctx->priv_data;
short *samples = data;
const uint8_t *buf = buf0;
// printf("xa decode %d\n",buf_size);
c->prev[0].s1 = 0;
c->prev[0].s2 = 0;
c->prev[1].s1 = 0;
c->prev[1].s2 = 0;
while(buf_size>=128) {
xa_decode(samples,buf,c->prev,avctx->channels);
buf+=128;
samples += 28*8;
buf_size-=128;
}
*data_size = (char*)samples - (char*)data;
return buf-buf0;
}
#define DEFINE_ENCODER(id, name,context) \
AVCodec name ## _encoder = { \
#name, \
CODEC_TYPE_AUDIO, \
id, \
sizeof(context), \
name ## _encode_init, \
name ## _encode_frame, \
name ## _encode_close, \
NULL, \
};
#define DEFINE_DECODER(id, name,context) \
AVCodec name ## _decoder = { \
#name, \
CODEC_TYPE_AUDIO, \
id, \
sizeof(context), \
name ## _decode_init, \
NULL, \
NULL, \
name ## _decode_frame, \
};
DEFINE_DECODER(CODEC_ID_ADPCM_XA,xa,XAContext)
|