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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/*
* Copyright (C) 2000-2001 the xine project
*
* This file is part of xine, a unix video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*
* $Id: w32codec.c,v 1.1 2001/04/18 22:35:05 f1rmb Exp $
*
* routines for using w32 codecs
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include "wine/msacm.h"
#include "wine/driver.h"
#include "wine/avifmt.h"
#include "wine/vfw.h"
#include "wine/mmreg.h"
#include "../video_out/video_out.h"
#include "../audio_out/audio_out.h"
extern vo_driver_t *gVideoDriver;
extern char* win32_codec_name;
int w32c_yuv_supported ;
int w32c_yuv_hack_needed ;
int w32c_flipped ;
unsigned char w32c_buf[128*1024];
int w32c_size;
unsigned char w32c_audio_buf[16384];
int w32c_audio_size;
unsigned char w32c_sample_buf[40000];
BITMAPINFOHEADER w32c_bih, w32c_o_bih;
HIC w32c_hic;
void *our_out_buffer;
HACMSTREAM w32c_srcstream;
int w32c_rec_audio_src_size;
char* get_vids_codec_name(unsigned long fccHandler, BITMAPINFOHEADER *bih) {
w32c_yuv_supported=0;
w32c_yuv_hack_needed=0;
w32c_flipped=0;
switch(fccHandler){
case mmioFOURCC('M', 'P', 'G', '4'):
case mmioFOURCC('m', 'p', 'g', '4'):
case mmioFOURCC('M', 'P', '4', '2'):
case mmioFOURCC('m', 'p', '4', '2'):
/* case mmioFOURCC('M', 'P', '4', '3'):
case mmioFOURCC('m', 'p', '4', '3'): */
/* Video in Microsoft MPEG-4 format */
w32c_yuv_supported=1;
w32c_yuv_hack_needed=1;
return "mpg4c32.dll";
case mmioFOURCC('M', 'P', '4', '3'):
case mmioFOURCC('m', 'p', '4', '3'):
/* Video in MPEG-4 v3 (really DivX) format */
bih->biCompression=mmioFOURCC('d', 'i', 'v', '3'); /* hack */
w32c_yuv_supported=1;
w32c_yuv_hack_needed=1;
return "divxc32.dll";
case mmioFOURCC('D', 'I', 'V', '3'):
case mmioFOURCC('d', 'i', 'v', '3'):
case mmioFOURCC('D', 'I', 'V', '4'):
case mmioFOURCC('d', 'i', 'v', '4'):
case mmioFOURCC('M', 'P', '4', '1'):
case mmioFOURCC('m', 'p', '4', '1'):
/* Video in DivX ;-) format */
w32c_yuv_supported =1;
w32c_yuv_hack_needed=1;
return "divxc32.dll";
case mmioFOURCC('I', 'V', '5', '0'):
case mmioFOURCC('i', 'v', '5', '0'):
/* Video in Indeo Video 5 format */
w32c_yuv_supported=1; /* YUV pic is upside-down :( */
return "ir50_32.dll";
case mmioFOURCC('I', 'V', '4', '1'):
case mmioFOURCC('i', 'v', '4', '1'):
/* Video in Indeo Video 4.1 format */
w32c_flipped=1;
return "ir41_32.dll";
case mmioFOURCC('I', 'V', '3', '2'):
case mmioFOURCC('i', 'v', '3', '2'):
/* Video in Indeo Video 3.2 format */
w32c_flipped=1;
return "ir32_32.dll";
case mmioFOURCC('c', 'v', 'i', 'd'):
/* Video in Cinepak format */
w32c_yuv_supported=1;
return "iccvid.dll";
/*** Only 16bit .DLL available (can't load under linux) ***
case mmioFOURCC('V', 'C', 'R', '1'):
printf("Video in ATI VCR1 format\n");
return "ativcr1.dll";
*/
case mmioFOURCC('V', 'C', 'R', '2'):
/* Video in ATI VCR2 format */
w32c_yuv_supported=1;
return "ativcr2.dll";
case mmioFOURCC('I', '2', '6', '3'):
case mmioFOURCC('i', '2', '6', '3'):
/* Video in I263 format */
return "i263_32.drv";
case mmioFOURCC('M', 'J', 'P', 'G'):
/* Video in MJPEG format */
w32c_yuv_supported=1;
return "mcmjpg32.dll";
/* return "m3jpeg32.dll";
return "libavi_mjpeg.so"; */
}
printf("UNKNOWN video codec: %.4s (0x%0X)\n",(char*)&fccHandler,(int)fccHandler);
printf("If you know this video format and codec, you can edit codecs.c in the source!\n");
printf("Please contact the author, send this movie to be supported by future version.\n");
return NULL;
}
#define IMGFMT_YUY2 (('2'<<24)|('Y'<<16)|('U'<<8)|'Y')
int w32c_init_video (BITMAPINFOHEADER *bih_){
HRESULT ret;
int outfmt = IMGFMT_YUY2;
int video_step;
memcpy ( &w32c_bih, bih_, sizeof (BITMAPINFOHEADER));
video_step = w32c_bih.biSize; /* HACK */
w32c_bih.biSize = sizeof(BITMAPINFOHEADER);
memset(&w32c_o_bih, 0, sizeof(BITMAPINFOHEADER));
w32c_o_bih.biSize = sizeof(BITMAPINFOHEADER);
win32_codec_name = get_vids_codec_name (w32c_bih.biCompression, &w32c_bih);
w32c_hic = ICOpen( 0x63646976, w32c_bih.biCompression, ICMODE_FASTDECOMPRESS);
if(!w32c_hic){
printf("ICOpen failed! unknown codec / wrong parameters?\n");
return 0;
}
ret = ICDecompressGetFormat(w32c_hic, &w32c_bih, &w32c_o_bih);
if(ret){
printf("ICDecompressGetFormat failed: Error %ld\n", (long)ret);
return 0;
}
if(outfmt==IMGFMT_YUY2)
w32c_o_bih.biBitCount=16;
else
w32c_o_bih.biBitCount=outfmt&0xFF;// //24;
w32c_o_bih.biSizeImage = w32c_o_bih.biWidth*w32c_o_bih.biHeight*(w32c_o_bih.biBitCount/8);
/*
if(!flipped)
w32c_o_bih.biHeight=-bih.biHeight; */ /* flip image! */
w32c_o_bih.biHeight=-w32c_bih.biHeight;
if(outfmt==IMGFMT_YUY2 && !w32c_yuv_hack_needed)
w32c_o_bih.biCompression = mmioFOURCC('Y','U','Y','2');
ret = ICDecompressQuery(w32c_hic, &w32c_bih, &w32c_o_bih);
if(ret){
printf("ICDecompressQuery failed: Error %ld\n", (long)ret);
return 0;
}
ret = ICDecompressBegin(w32c_hic, &w32c_bih, &w32c_o_bih);
if(ret){
printf("ICDecompressBegin failed: Error %ld\n", (long)ret);
return 0;
}
if (w32c_yuv_hack_needed) {
w32c_o_bih.biCompression = mmioFOURCC('Y','U','Y','2');
}
w32c_size = 0;
if (!(gVideoDriver->get_capabilities () && VO_CAP_YUY2)) {
printf ("video output driver doesn't support YUY2 !!");
}
vo_set_image_format (w32c_bih.biWidth, w32c_bih.biHeight, 42, IMGFMT_YUY2, video_step);
our_out_buffer = malloc (w32c_o_bih.biSizeImage);
return 1;
}
int nFrame = 0;
void w32c_decode_video (unsigned char *data, uint32_t nSize, int bFrameEnd, uint32_t nPTS) {
HRESULT ret;
vo_image_buffer_t *img;
memcpy (&w32c_buf[w32c_size], data, nSize);
w32c_size += nSize;
if (bFrameEnd) {
w32c_bih.biSizeImage = w32c_size;
/*
printf ("Frame complete => decompressing [%d %d %d %d ... %d %d]size=%d\n",
w32c_buf[0],w32c_buf[1],w32c_buf[2],w32c_buf[3],
w32c_buf[w32c_size-2],w32c_buf[w32c_size-1], w32c_size);
*/
img = vo_alloc_image_buffer();
/* printf ("ICDecrompress %d\n",img); */
ret = ICDecompress(w32c_hic, ICDECOMPRESS_NOTKEYFRAME,
&w32c_bih, w32c_buf,
&w32c_o_bih, img->mem[0]);
/* memcpy(img->mem[0],our_out_buffer,w32c_bih.biWidth*w32c_bih.biHeight*2); */
/* memset(img->mem[1],128,w32c_o_bih.biWidth*w32c_o_bih.biHeight/4); */
/* memset(img->mem[2],128,w32c_o_bih.biWidth*w32c_o_bih.biHeight/4); */
img->PTS = nPTS;
if(ret) {
printf("Error decompressing frame, err=%ld\n", (long)ret);
img->bFrameBad = 1;
} else
img->bFrameBad = 0;
img->nID = nFrame;
nFrame++;
vo_queue_frame (img);
vo_free_image_buffer (img);
w32c_size = 0;
}
}
void w32c_close_video () {
}
char* get_auds_codec_name(int id){
switch (id){
case 0x160:/* DivX audio */
case 0x161:/* DivX audio */
return "divxa32.acm";
case 0x2: /* MS ADPCM */
return "msadp32.acm";
case 0x55: /* MPEG l3 */
return "l3codeca.acm";
case 0x11: /* IMA ADPCM */
return "imaadp32.acm";
case 0x31: /* MS GSM */
case 0x32: /* MS GSM */
return "msgsm32.acm";
}
printf("UNKNOWN audio codec: 0x%0X\n",id);
printf("If you know this audio format and codec, you can edit codecs.c in the source!\n");
printf("Please contact the author, send this movie to be supported by future version.\n");
return NULL;
}
int w32c_init_audio (WAVEFORMATEX *in_fmt_){
HRESULT ret;
static WAVEFORMATEX wf;
long in_size=in_fmt_->nBlockAlign;
unsigned long srcsize=0;
static WAVEFORMATEX *in_fmt;
in_fmt = (WAVEFORMATEX *) malloc (64);
memcpy (in_fmt, in_fmt_, sizeof (WAVEFORMATEX) + in_fmt_->cbSize);
if ( (in_fmt->wFormatTag == 0x01) || (in_fmt->wFormatTag == 0x2000)
|| (in_fmt->wFormatTag == 0x50) || (in_fmt->wFormatTag == 0x53) ) {
/* handled by other codecs in source code */
return 1;
}
w32c_srcstream=NULL;
gAudioOut->open (16, in_fmt->nSamplesPerSec, AO_MODE_STEREO);
wf.nChannels=in_fmt->nChannels;
wf.nSamplesPerSec=in_fmt->nSamplesPerSec;
wf.nAvgBytesPerSec=2*wf.nSamplesPerSec*wf.nChannels;
wf.wFormatTag=WAVE_FORMAT_PCM;
wf.nBlockAlign=2*in_fmt->nChannels;
wf.wBitsPerSample=16;
wf.cbSize=0;
win32_codec_name = get_auds_codec_name (in_fmt->wFormatTag);
ret=acmStreamOpen(&w32c_srcstream,(HACMDRIVER)NULL,
in_fmt,
&wf,
NULL,0,0,0);
if(ret){
if(ret==ACMERR_NOTPOSSIBLE)
printf("ACM_Decoder: Unappropriate audio format\n");
else
printf("ACM_Decoder: acmStreamOpen error %d", ret);
w32c_srcstream=NULL;
return 0;
}
/*
acmStreamSize(w32c_srcstream, in_size, &srcsize, ACM_STREAMSIZEF_SOURCE);
printf("Audio buffer min. size: %d\n",srcsize);
*/
acmStreamSize(w32c_srcstream, 16384, &w32c_rec_audio_src_size, ACM_STREAMSIZEF_DESTINATION);
/* printf("recommended source buffer size: %d\n", w32c_rec_audio_src_size); */
w32c_audio_size = 0;
return 1;
}
void w32c_decode_audio (unsigned char *data, uint32_t nSize, int bFrameEnd, uint32_t nPTS) {
static ACMSTREAMHEADER ash;
HRESULT hr;
DWORD srcsize=0;
memcpy (&w32c_audio_buf[w32c_audio_size], data, nSize);
w32c_audio_size += nSize;
while (w32c_audio_size >= w32c_rec_audio_src_size) {
memset(&ash, 0, sizeof(ash));
ash.cbStruct=sizeof(ash);
ash.fdwStatus=0;
ash.dwUser=0;
ash.pbSrc=w32c_audio_buf;
ash.cbSrcLength=w32c_rec_audio_src_size;
ash.pbDst=w32c_sample_buf;
ash.cbDstLength=20000;
hr=acmStreamPrepareHeader(w32c_srcstream,&ash,0);
if(hr){
printf("ACM_Decoder: acmStreamPrepareHeader error %d\n",hr);
return;
}
/*
printf ("decoding %d of %d bytes (%02x %02x %02x %02x ... %02x %02x)\n",
w32c_rec_audio_src_size, w32c_audio_size,
w32c_audio_buf[0], w32c_audio_buf[1], w32c_audio_buf[2], w32c_audio_buf[3],
w32c_audio_buf[w32c_rec_audio_src_size-2], w32c_audio_buf[w32c_rec_audio_src_size-1]);
*/
hr=acmStreamConvert(w32c_srcstream,&ash,0);
if(hr){
/* printf("acmStreamConvert error %d, used %d bytes\n",hr,ash.cbSrcLengthUsed); */
ash.cbSrcLengthUsed = w32c_rec_audio_src_size;
} else {
/*
printf ("acmStreamConvert worked, used %d bytes, generated %d bytes\n",
ash.cbSrcLengthUsed, ash.cbDstLengthUsed);
*/
if (ash.cbDstLengthUsed>0) {
/*
printf ("decoded : %02x %02x %02x %02x ... %02x %02x \n",
w32c_sample_buf[0], w32c_sample_buf[1], w32c_sample_buf[2], w32c_sample_buf[3],
w32c_sample_buf[ash.cbDstLengthUsed-2], w32c_sample_buf[ash.cbDstLengthUsed-1]);
*/
gAudioOut->write_audio_data (w32c_sample_buf, ash.cbDstLengthUsed / 4, nPTS);
}
}
if(ash.cbSrcLengthUsed>=w32c_audio_size){
w32c_audio_size=0;
} else {
unsigned char *pSrc, *pDst;
int i;
w32c_audio_size-=ash.cbSrcLengthUsed;
pSrc = &w32c_audio_buf [ash.cbSrcLengthUsed];
pDst = w32c_audio_buf;
for (i=0; i<w32c_audio_size; i++) {
*pDst = *pSrc;
pDst ++;
pSrc ++;
}
}
hr=acmStreamUnprepareHeader(w32c_srcstream,&ash,0);
if(hr){
printf("ACM_Decoder: acmStreamUnprepareHeader error %d\n",hr);
}
}
}
void w32c_close_audio () {
acmStreamClose(w32c_srcstream, 0);
}
|