summaryrefslogtreecommitdiff
path: root/src/libw32dll/w32codec.c
blob: c49971680ac67009ec06783a87b28e850da68fcf (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/* 
 * 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.40 2001/11/10 13:48:03 guenter Exp $
 *
 * routines for using w32 codecs
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>

#include "wine/msacm.h"
#include "wine/driver.h"
#include "wine/avifmt.h"
#include "wine/vfw.h"
#include "wine/mmreg.h"
#include "wine/ldt_keeper.h"

#include "video_out.h"
#include "audio_out.h"
#include "buffer.h"
#include "monitor.h"
#include "xine_internal.h"
#include "memcpy.h"

extern char*   win32_codec_name; 
extern char*   win32_def_path;

typedef struct w32v_decoder_s {
  video_decoder_t   video_decoder;

  vo_instance_t    *video_out;
  int               video_step;
  int               decoder_ok;

  BITMAPINFOHEADER  bih, o_bih; 
  HIC               hic;
  int               yuv_supported ;
  int		    yuv_hack_needed ;
  int               flipped ;
  unsigned char     buf[128*1024];
  void             *img_buffer;
  int               size;
  long		    outfmt;

  /* profiler */
  int		   prof_rgb2yuv;
  
  LDT_FS *ldt_fs;
} w32v_decoder_t;

typedef struct w32a_decoder_s {
  audio_decoder_t   audio_decoder;

  ao_instance_t    *audio_out;
    int		    output_open;
  int               decoder_ok;

  unsigned char    *buf;
  int               size;   
  HACMSTREAM        srcstream;
  int               rec_audio_src_size;
  int               num_channels;
  
  LDT_FS *ldt_fs;
} w32a_decoder_t;


/*
 * RGB->YUY2 conversion, we need is for xine video-codec ->
 * video-output interface
 *
 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
 * normalized to the range 0..MAXSAMPLE rather than -0.5 .. 0.5.
 * The conversion equations to be implemented are therefore
 *      Y  =  0.29900 * R + 0.58700 * G + 0.11400 * B
 *      Cb = -0.16874 * R - 0.33126 * G + 0.50000 * B  + CENTERSAMPLE
 *      Cr =  0.50000 * R - 0.41869 * G - 0.08131 * B  + CENTERSAMPLE
 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
 *
 * To avoid floating-point arithmetic, we represent the fractional
 * constants as integers scaled up by 2^16 (about 4 digits precision);
 * we have to divide the products by 2^16, with appropriate rounding,
 * to get the correct answer.
 *
 * FIXME: For the XShm video-out driver, this conversion is a huge
 * waste of time (converting from RGB->YUY2 here and converting back
 * from YUY2->RGB in the XShm driver).
 */
#define	MAXSAMPLE	255
#define	CENTERSAMPLE	128

#define	SCALEBITS	16
#define	FIX(x)	 	( (int32_t) ( (x) * (1<<SCALEBITS) + 0.5 ) )
#define	ONE_HALF	( (int32_t) (1<< (SCALEBITS-1)) )
#define	CBCR_OFFSET	(CENTERSAMPLE << SCALEBITS)

#define R_Y_OFF         0                       /* offset to R => Y section */
#define G_Y_OFF         (1*(MAXSAMPLE+1))       /* offset to G => Y section */
#define B_Y_OFF         (2*(MAXSAMPLE+1))       /* etc. */
#define R_CB_OFF        (3*(MAXSAMPLE+1))
#define G_CB_OFF        (4*(MAXSAMPLE+1))
#define B_CB_OFF        (5*(MAXSAMPLE+1))
#define R_CR_OFF        B_CB_OFF                /* B=>Cb, R=>Cr are the same */
#define G_CR_OFF        (6*(MAXSAMPLE+1))
#define B_CR_OFF        (7*(MAXSAMPLE+1))
#define TABLE_SIZE      (8*(MAXSAMPLE+1))


/*
 * HAS_SLOW_MULT:
 * 0: use integer multiplication in inner loop of rgb2yuv conversion
 * 1: use precomputed tables (avoids slow integer multiplication)
 *
 * (On a P-II/Athlon, the version using the precomputed tables is
 * slightly faster)
 */
#define	HAS_SLOW_MULT	1

#if	HAS_SLOW_MULT
static int32_t *rgb_ycc_tab;
#endif

static void w32v_init_rgb_ycc(void)
{
#if	HAS_SLOW_MULT
  /*
   * System has slow integer multiplication, so we precompute
   * the YCbCr constants times R,G,B for all possible values.
   */
  int i;
    
  if (rgb_ycc_tab) return;

  rgb_ycc_tab = malloc(TABLE_SIZE * sizeof(int32_t));

  for (i = 0; i <= MAXSAMPLE; i++) {
    rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i;
    rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i;
    rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i     + ONE_HALF;
    rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i;
    rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i;
    /*
     * We use a rounding fudge-factor of 0.5-epsilon for Cb and Cr.
     * This ensures that the maximum output will round to MAXJSAMPLE
     * not MAXJSAMPLE+1, and thus that we don't have to range-limit.
     */
    rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
    /*
     * B=>Cb and R=>Cr tables are the same
    rgb_ycc_tab[i+R_CR_OFF] = FIX(0.50000) * i    + CBCR_OFFSET + ONE_HALF-1;
     */
    rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i;
    rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i;
  }
#endif
}

static char* get_vids_codec_name(w32v_decoder_t *this,
				 int buf_type) {

  this->yuv_supported=0;
  this->yuv_hack_needed=0;
  this->flipped=0;
  
  buf_type &= 0xffff0000;

  switch (buf_type) {
  case BUF_VIDEO_MSMPEG4_V12:
    /* Microsoft MPEG-4 v1/v2 */
    this->yuv_supported=1;
    this->yuv_hack_needed=1;
    this->flipped=1;
    return "mpg4c32.dll";

  case BUF_VIDEO_MSMPEG4_V3:
    /* Microsoft MPEG-4 v3 */
    this->yuv_supported=1;
    this->yuv_hack_needed=1;
    this->flipped=1;
    return "divxc32.dll";

  case BUF_VIDEO_IV50:
    /* Video in Indeo Video 5 format */
    this->yuv_supported=1;   /* YUV pic is upside-down :( */
    this->flipped=0;
    return "ir50_32.dll";

  case BUF_VIDEO_IV41:
    /* Video in Indeo Video 4.1 format */
    this->flipped=1;
    return "ir41_32.dll";
    
  case BUF_VIDEO_IV32:
    /* Video in Indeo Video 3.2 format */
    this->flipped=1;
    return "ir32_32.dll";
    
  case BUF_VIDEO_IV31:
    /* Video in Indeo Video 3.1 format */
    this->flipped=1;
    return "ir32_32.dll";

  case BUF_VIDEO_CINEPAK:
    /* Video in Cinepak format */
    this->flipped=1;
    this->yuv_supported=0;
    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 BUF_VIDEO_ATIVCR2:
    /* Video in ATI VCR2 format */
    this->yuv_supported=1;
    return "ativcr2.dll";
    
  case BUF_VIDEO_I263:
    /* Video in I263 format */
    this->flipped=1;
    this->yuv_supported=1;
    return "i263_32.drv";

  case BUF_VIDEO_MSVC:
    /* Video in Windows Video 1 */
    this->flipped=1;
    this->yuv_supported=0;
    return "msvidc32.dll";    
    
  case BUF_VIDEO_DV:
    /* MainConcept DV Codec */
    this->yuv_supported=1;
    return "mcdvd_32.dll";    

  }

  printf ("w32codec: this didn't happen: unknown video buf type %08x\n",
	  buf_type);
  

  return NULL;
}

#undef IMGFMT_YUY2
#undef IMGFMT_YV12
#define IMGFMT_YUY2  mmioFOURCC('Y','U','Y','2')
#define IMGFMT_YV12  mmioFOURCC('Y','V','1','2')
#define IMGFMT_32RGB mmioFOURCC( 32,'R','G','B')
#define IMGFMT_24RGB mmioFOURCC( 24,'R','G','B')
#define IMGFMT_16RGB mmioFOURCC( 16,'R','G','B')
#define IMGFMT_15RGB mmioFOURCC( 15,'R','G','B')

static int w32v_can_handle (video_decoder_t *this_gen, int buf_type) {
  buf_type &= 0xFFFF0000;

  return ( buf_type == BUF_VIDEO_MSMPEG4_V12 ||
	   buf_type == BUF_VIDEO_MSMPEG4_V3 ||
	   buf_type == BUF_VIDEO_IV50 ||
           buf_type == BUF_VIDEO_IV41 ||
           buf_type == BUF_VIDEO_IV32 ||
           buf_type == BUF_VIDEO_IV31 ||
           buf_type == BUF_VIDEO_CINEPAK ||
           /* buf_type == BUF_VIDEO_ATIVCR1 || */
           buf_type == BUF_VIDEO_ATIVCR2 ||
	   buf_type == BUF_VIDEO_I263 ||
	   buf_type == BUF_VIDEO_MSVC ||
	   buf_type == BUF_VIDEO_DV );
}

static void w32v_init (video_decoder_t *this_gen, vo_instance_t *video_out) {

  w32v_decoder_t *this = (w32v_decoder_t *) this_gen;

  this->video_out  = video_out;
  this->decoder_ok = 0;
}


static void w32v_init_codec (w32v_decoder_t *this, int buf_type) {

  HRESULT  ret;
  uint32_t vo_cap;
  int outfmt;

  w32v_init_rgb_ycc();

  printf ("w32codec: init codec...\n");

  memset(&this->o_bih, 0, sizeof(BITMAPINFOHEADER));
  this->o_bih.biSize = sizeof(BITMAPINFOHEADER);
  
  this->ldt_fs = Setup_LDT_Keeper();
  
  win32_codec_name = get_vids_codec_name (this, buf_type);

  outfmt = IMGFMT_15RGB;
  if (this->yuv_supported) {
    vo_cap = this->video_out->get_capabilities (this->video_out);
    if (vo_cap & VO_CAP_YUY2)
      outfmt = IMGFMT_YUY2;
  }

  this->hic = ICOpen (mmioFOURCC('v','i','d','c'), 
		      this->bih.biCompression, 
		      ICMODE_FASTDECOMPRESS);

  if(!this->hic){
    printf ("w32codec: ICOpen failed! unknown codec %08lx / wrong parameters?\n",
	    this->bih.biCompression);
    this->decoder_ok = 0;
    return;
  }

  ret = ICDecompressGetFormat(this->hic, &this->bih, &this->o_bih);
  if(ret){
    printf("w32codec: ICDecompressGetFormat (%.4s %08lx/%d) failed: Error %ld\n",
	   (char*)&this->o_bih.biCompression, 
	   this->bih.biCompression,
	   this->bih.biBitCount,
	   (long)ret);
    this->decoder_ok = 0;
    return;
  }

  printf ("w32codec: video output format: %.4s %08lx\n",
	  (char*)&this->o_bih.biCompression,
	  this->o_bih.biCompression);

  if(outfmt==IMGFMT_YUY2 || outfmt==IMGFMT_15RGB)
    this->o_bih.biBitCount=16;
  else
    this->o_bih.biBitCount=outfmt&0xFF;

  this->o_bih.biSizeImage = this->o_bih.biWidth * this->o_bih.biHeight
      * this->o_bih.biBitCount / 8;

  if (this->flipped)
    this->o_bih.biHeight=-this->bih.biHeight; 

  if(outfmt==IMGFMT_YUY2 && !this->yuv_hack_needed)
    this->o_bih.biCompression = mmioFOURCC('Y','U','Y','2');
  else 
    this->o_bih.biCompression = 0;
      
  ret = ICDecompressQuery(this->hic, &this->bih, &this->o_bih);
  
  if(ret){
    printf("w32codec: ICDecompressQuery failed: Error %ld\n", (long)ret);
    this->decoder_ok = 0;
    return;
  }
  
  ret = ICDecompressBegin(this->hic, &this->bih, &this->o_bih);
  if(ret){
    printf("w32codec: ICDecompressBegin failed: Error %ld\n", (long)ret);
    this->decoder_ok = 0;
    return;
  }

  if (outfmt==IMGFMT_YUY2 && this->yuv_hack_needed)
    this->o_bih.biCompression = mmioFOURCC('Y','U','Y','2'); 

  this->size = 0;

  this->img_buffer = malloc (this->o_bih.biSizeImage);

  this->video_out->open (this->video_out);

  this->outfmt = outfmt;
  this->decoder_ok = 1;
}

static void w32v_decode_data (video_decoder_t *this_gen, buf_element_t *buf) {
  w32v_decoder_t *this = (w32v_decoder_t *) this_gen;

  /*
  printf ("w32codec: processing packet type = %08x, buf : %d, 
          buf->decoder_info[0]=%d\n", 
	  buf->type, buf, buf->decoder_info[0]);
	  */
  if (buf->decoder_info[0] == 0) {
    if ( buf->type & 0xff )
      return;
    
    /* init package containing bih */
    memcpy ( &this->bih, buf->content, sizeof (BITMAPINFOHEADER));
    this->video_step = buf->decoder_info[1];

    w32v_init_codec (this, buf->type);
    
  } else if (this->decoder_ok) {

    /* printf ("w32codec: processing packet ...\n"); */

    fast_memcpy (&this->buf[this->size], buf->content, buf->size);

    this->size += buf->size;

    if (buf->decoder_info[0] == 2)  {

      HRESULT     ret;
      vo_frame_t *img;

      /* decoder video frame */

      this->bih.biSizeImage = this->size;

      img = this->video_out->get_frame (this->video_out,
					this->bih.biWidth, 
					this->bih.biHeight, 
					42, 
					IMGFMT_YUY2,
					this->video_step,
					VO_BOTH_FIELDS);

      /*
      ret = ICDecompress(this->hic, ICDECOMPRESS_NOTKEYFRAME, 
			 &this->bih, this->buf,
			 &this->o_bih, img->base[0]);
      */
      ret = ICDecompress(this->hic, ICDECOMPRESS_NOTKEYFRAME, 
			 &this->bih, this->buf,
			 &this->o_bih, this->img_buffer);

      if (this->outfmt==IMGFMT_YUY2) {
	/* already decoded into YUY2 format by DLL */
	fast_memcpy(img->base[0], this->img_buffer, this->bih.biHeight*this->bih.biWidth*2);
      } else {
	/* now, convert rgb to yuv */
	int row, col;
#if	HAS_SLOW_MULT
	int32_t *ctab = rgb_ycc_tab;
#endif

	profiler_start_count (this->prof_rgb2yuv);

	for (row=0; row<this->bih.biHeight; row++) {

	  uint16_t *pixel, *out;

	  pixel = (uint16_t *) ( (uint8_t *)this->img_buffer + 2 * row * this->o_bih.biWidth );
	  out = (uint16_t *) (img->base[0] + 2 * row * this->o_bih.biWidth );

	  for (col=0; col<this->o_bih.biWidth; col++, pixel++, out++) {
	    
	    uint8_t   r,g,b;
	    uint8_t   y,u,v;
	    
	    b = (*pixel & 0x001F) << 3;
	    g = (*pixel & 0x03E0) >> 5 << 3;
	    r = (*pixel & 0x7C00) >> 10 << 3;
	    
#if	HAS_SLOW_MULT
	    y = (ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) >> SCALEBITS;
	    if (!(col & 0x0001)) {
	      /* even pixel, do u */
	      u = (ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) >> SCALEBITS;
	      *out = ( (uint16_t) u << 8) | (uint16_t) y;
	    } else {
	      /* odd pixel, do v */
	      v = (ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) >> SCALEBITS;
	      *out = ( (uint16_t) v << 8) | (uint16_t) y;
	    }
#else
	    y = (FIX(0.299) * r + FIX(0.587) * g + FIX(0.114) * b + ONE_HALF) >> SCALEBITS;
	    if (!(col & 0x0001)) {
	      /* even pixel, do u */
	      u = (- FIX(0.16874) * r - FIX(0.33126) * g + FIX(0.5) * b + CBCR_OFFSET + ONE_HALF-1) >> SCALEBITS;
	      *out = ( (uint16_t) u << 8) | (uint16_t) y;
	    } else {
	      /* odd pixel, do v */
	      v = (FIX(0.5) * r - FIX(0.41869) * g - FIX(0.08131) * b + CBCR_OFFSET + ONE_HALF-1) >> SCALEBITS;
	      *out = ( (uint16_t) v << 8) | (uint16_t) y;
	    }
#endif
	    //printf("r %02x g %02x b %02x y %02x u %02x v %02x\n",r,g,b,y,u,v);
	  }
	}

	profiler_stop_count (this->prof_rgb2yuv);
      }

      img->PTS = buf->PTS;
      img->SCR = buf->SCR;
      if(ret) {
	printf("w32codec: Error decompressing frame, err=%ld\n", (long)ret); 
	img->bad_frame = 1;
      } else
	img->bad_frame = 0;
      
      if (img->copy) {
	int height = abs(this->o_bih.biHeight);
	int stride = this->o_bih.biWidth;
	uint8_t* src[3];
	  
	src[0] = img->base[0];
	  
	while ((height -= 16) >= 0) {
	  img->copy(img, src);
	  src[0] += 32 * stride;
	}
      }

      img->draw(img);
      img->free(img);

      this->size = 0;
    }

    /* printf ("w32codec: processing packet done\n"); */
  }
}

static void w32v_close (video_decoder_t *this_gen) {

  w32v_decoder_t *this = (w32v_decoder_t *) this_gen;

  free (this->img_buffer);

  this->video_out->close(this->video_out);
  
  Restore_LDT_Keeper( this->ldt_fs );
}

static char *w32v_get_id(void) {
  return "vfw (win32) video decoder";
}

/*
 * audio stuff
 */

static int w32a_can_handle (audio_decoder_t *this_gen, int buf_type) {

  int codec = buf_type & 0xFFFF0000;

  return ( (codec == BUF_AUDIO_DIVXA) ||
	   (codec == BUF_AUDIO_MSADPCM) ||
	   (codec == BUF_AUDIO_IMAADPCM) ||
	   (codec == BUF_AUDIO_MSGSM) ||
	   (codec == BUF_AUDIO_IMC) ||
	   (codec == BUF_AUDIO_LH) );
}

static char* get_auds_codec_name(w32a_decoder_t *this, int buf_type) {

  buf_type = buf_type & 0xFFFF0000;

  switch (buf_type) {
  case BUF_AUDIO_DIVXA:
    return "divxa32.acm";
  case BUF_AUDIO_MSADPCM:
    return "msadp32.acm";
  case BUF_AUDIO_IMAADPCM:
    return "imaadp32.acm";
  case BUF_AUDIO_MSGSM:
    return "msgsm32.acm";
  case BUF_AUDIO_IMC:
    return "imc32.acm";
  case BUF_AUDIO_LH:
    return "lhacm.acm";
  }
  printf ("w32codec: this didn't happen: unknown audio buf type %08x\n",
	  buf_type);
  return NULL;
}

static void w32a_init (audio_decoder_t *this_gen, ao_instance_t *audio_out) {

  w32a_decoder_t *this = (w32a_decoder_t *) this_gen;

  this->audio_out  = audio_out;
  this->output_open = 0;
  this->decoder_ok = 0;
  
  this->buf = NULL;
}

static int w32a_init_audio (w32a_decoder_t *this,
			    WAVEFORMATEX *in_fmt_,
			    int buf_type) {

  HRESULT ret;
  static WAVEFORMATEX wf;     
  long in_size=in_fmt_->nBlockAlign;
  static WAVEFORMATEX *in_fmt;
  unsigned long srcsize;

  in_fmt = (WAVEFORMATEX *) malloc (64);

  memcpy (in_fmt, in_fmt_, sizeof (WAVEFORMATEX) + in_fmt_->cbSize);

  this->srcstream = 0;
  this->num_channels  = in_fmt->nChannels;
  
  if (this->output_open)
    this->audio_out->close (this->audio_out);

  this->output_open = this->audio_out->open( this->audio_out, 
					      16, in_fmt->nSamplesPerSec, 
					      (in_fmt->nChannels == 2) ? AO_CAP_MODE_STEREO : AO_CAP_MODE_MONO);
  if (!this->output_open) {
    printf("w32codec: (ACM_Decoder) Cannot open audio output device\n");
    return 0;
  }

  this->ldt_fs = Setup_LDT_Keeper();
  
  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 (this, buf_type);
  ret=acmStreamOpen(&this->srcstream,(HACMDRIVER)NULL,
                    in_fmt,
		    &wf,
                    NULL,0,0,0);
  if(ret){
    if(ret==ACMERR_NOTPOSSIBLE)
      printf("w32codec: (ACM_Decoder) Unappropriate audio format\n");
    else
      printf("w32codec: (ACM_Decoder) acmStreamOpen error %d", (int) ret);
    this->srcstream = 0;
    return 0;
  }

  acmStreamSize(this->srcstream, in_size, &srcsize, ACM_STREAMSIZEF_SOURCE);
  printf("w32codec: Audio buffer min. size: %d\n",(int)srcsize);
  srcsize*=2;
  if(srcsize<16384) 
    srcsize=16384;
    
  acmStreamSize(this->srcstream, srcsize, (LPDWORD) &this->rec_audio_src_size, 
		ACM_STREAMSIZEF_DESTINATION);
  printf("w32codec: Recommended source buffer size: %d\n", this->rec_audio_src_size); 

  if( this->buf )
    free(this->buf);
  
  if( this->rec_audio_src_size < 2 * in_fmt->nBlockAlign ) {
    this->rec_audio_src_size = 2 * in_fmt->nBlockAlign;
    srcsize *= 2;
    printf("w32codec: adjusting source buffer size to %d\n", this->rec_audio_src_size); 
  }
  this->buf = malloc( 2 * this->rec_audio_src_size );
    
  this->size = 0;

  return 1;
}


static void w32a_decode_audio (w32a_decoder_t *this,
			       unsigned char *data, 
			       uint32_t size, 
			       int frame_end, 
			       uint32_t pts,
			       uint32_t scr) {

  static ACMSTREAMHEADER ash;
  HRESULT hr;
  /* DWORD srcsize=0; */

  fast_memcpy (&this->buf[this->size], data, size);

  this->size += size;
  if( this->size > 2 * this->rec_audio_src_size )
    printf("w32codec: buffer overflow on w32a_decode_audio\n");
  
  while (this->size >= this->rec_audio_src_size) {

    audio_buffer_t *audio_buffer = this->audio_out->get_buffer (this->audio_out);

    memset(&ash, 0, sizeof(ash));
    ash.cbStruct=sizeof(ash);
    ash.fdwStatus=0;
    ash.dwUser=0; 
    ash.pbSrc=this->buf;
    ash.cbSrcLength=this->rec_audio_src_size;
    ash.pbDst=(uint8_t *)audio_buffer->mem;
    ash.cbDstLength=audio_buffer->mem_size;
    hr=acmStreamPrepareHeader(this->srcstream,&ash,0);
    if(hr){
      printf("w32codec: (ACM_Decoder) acmStreamPrepareHeader error %d\n",(int)hr);
      
      audio_buffer->num_frames = 0;
      this->audio_out->put_buffer (this->audio_out, audio_buffer);

      return;
    }

    /*
    printf ("decoding %d of %d bytes (%02x %02x %02x %02x ... %02x %02x)\n", 
	    this->rec_audio_src_size, this->size,
	    this->buf[0], this->buf[1], this->buf[2], this->buf[3],
	    this->buf[this->rec_audio_src_size-2], this->buf[this->rec_audio_src_size-1]); 
    */

    hr=acmStreamConvert(this->srcstream,&ash,0);
    if(hr){
      printf("w32codec: acmStreamConvert error %d, used %d bytes\n",(int)hr,(int)ash.cbSrcLengthUsed);
      ash.cbSrcLengthUsed = this->rec_audio_src_size; 

      audio_buffer->num_frames = 0;
      this->audio_out->put_buffer (this->audio_out, audio_buffer);

    } else {
      /*
      printf ("acmStreamConvert worked, used %d bytes, generated %d bytes\n",
	      ash.cbSrcLengthUsed, ash.cbDstLengthUsed);
      */
      if (ash.cbDstLengthUsed>0) {

	audio_buffer->num_frames = ash.cbDstLengthUsed / (this->num_channels*2);
	audio_buffer->vpts       = pts;
	audio_buffer->scr        = scr;

	this->audio_out->put_buffer (this->audio_out, audio_buffer);

      }
    }
    if(ash.cbSrcLengthUsed>=this->size){
      this->size=0;
    } else {
      this->size-=ash.cbSrcLengthUsed;
      memmove( this->buf, &this->buf [ash.cbSrcLengthUsed], this->size);
    }

    hr=acmStreamUnprepareHeader(this->srcstream,&ash,0);
    if(hr){
      printf("w32codec: (ACM_Decoder) acmStreamUnprepareHeader error %d\n",(int)hr);
    }
  }
}

static void w32a_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {

  w32a_decoder_t *this = (w32a_decoder_t *) this_gen;

  if (buf->decoder_info[0] == 0) {
    /* init package containing bih */

    this->decoder_ok = w32a_init_audio (this, (WAVEFORMATEX *)buf->content, buf->type);
  } else if (this->decoder_ok) {

    w32a_decode_audio (this, buf->content, buf->size,
		       buf->decoder_info[0]==2, 
		       buf->PTS, buf->SCR);
  }
}



static void w32a_close (audio_decoder_t *this_gen) {

  w32a_decoder_t *this = (w32a_decoder_t *) this_gen;

  acmStreamClose(this->srcstream, 0);

  Restore_LDT_Keeper(this->ldt_fs);
  
  if (!this->output_open) {
    this->audio_out->close (this->audio_out);
    this->output_open = 0;
  }
  if( this->buf ) {
    free(this->buf);
    this->buf = NULL;
  }
}

static char *w32a_get_id(void) {
  return "vfw (win32) audio decoder";
}

video_decoder_t *init_video_decoder_plugin (int iface_version, config_values_t *cfg) {

  w32v_decoder_t *this ;

  if (iface_version != 2) {
    printf( "w32codec: plugin doesn't support plugin API version %d.\n"
	    "w32codec: this means there's a version mismatch between xine and this "
	    "w32codec: decoder plugin.\nInstalling current input plugins should help.\n",
	    iface_version);
    
    return NULL;
  }

  win32_def_path = cfg->lookup_str (cfg, "win32_path", "/usr/lib/win32");

  this = (w32v_decoder_t *) malloc (sizeof (w32v_decoder_t));

  this->video_decoder.interface_version   = 2;
  this->video_decoder.can_handle          = w32v_can_handle;
  this->video_decoder.init                = w32v_init;
  this->video_decoder.decode_data         = w32v_decode_data;
  this->video_decoder.close               = w32v_close;
  this->video_decoder.get_identifier      = w32v_get_id;
  this->video_decoder.priority            = 1;

  this->prof_rgb2yuv = profiler_allocate_slot ("w32codec rgb2yuv convert");


  return (video_decoder_t *) this;
}

audio_decoder_t *init_audio_decoder_plugin (int iface_version, config_values_t *cfg) {

  w32a_decoder_t *this ;

  
  if (iface_version != 2) {
    printf( "w32codec: plugin doesn't support plugin API version %d.\n"
	    "w32codec: this means there's a version mismatch between xine and this "
	    "w32codec: decoder plugin.\nInstalling current input plugins should help.\n",
	    iface_version);

    return NULL;
  }

  win32_def_path = cfg->lookup_str (cfg, "win32_path", "/usr/lib/win32");

  this = (w32a_decoder_t *) malloc (sizeof (w32a_decoder_t));

  this->audio_decoder.interface_version   = 2;
  this->audio_decoder.can_handle          = w32a_can_handle;
  this->audio_decoder.init                = w32a_init;
  this->audio_decoder.decode_data         = w32a_decode_data;
  this->audio_decoder.close               = w32a_close;
  this->audio_decoder.get_identifier      = w32a_get_id;
  this->audio_decoder.priority            = 1;
  
  return (audio_decoder_t *) this;
}