summaryrefslogtreecommitdiffstats
path: root/dll_init.c
blob: dac4e12d9d8abf55e1c82b293fa0175189996d88 (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
// ACM audio and VfW video codecs initialization
// based on the avifile library [http://divx.euro.ru]

int init_audio_codec(){
    HRESULT ret;
    WAVEFORMATEX *in_fmt=(WAVEFORMATEX*)&avi_header.wf_ext;
    unsigned long srcsize=0;

  if(verbose) printf("======= Win32 (ACM) AUDIO Codec init =======\n");

    avi_header.srcstream=NULL;

//    if(in_fmt->nSamplesPerSec==0){  printf("Bad WAVE header!\n");exit(1);  }
//    MSACM_RegisterAllDrivers();

    avi_header.wf.nChannels=in_fmt->nChannels;
    avi_header.wf.nSamplesPerSec=in_fmt->nSamplesPerSec;
    avi_header.wf.nAvgBytesPerSec=2*avi_header.wf.nSamplesPerSec*avi_header.wf.nChannels;
    avi_header.wf.wFormatTag=WAVE_FORMAT_PCM;
    avi_header.wf.nBlockAlign=2*in_fmt->nChannels;
    avi_header.wf.wBitsPerSample=16;
    avi_header.wf.cbSize=0;

    win32_codec_name = avi_header.audio_codec;
    ret=acmStreamOpen(&avi_header.srcstream,(HACMDRIVER)NULL,
                    in_fmt,&avi_header.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);
        avi_header.srcstream=NULL;
        return 0;
    }
    if(verbose) printf("Audio codec opened OK! ;-)\n");

    srcsize=in_fmt->nBlockAlign;
    acmStreamSize(avi_header.srcstream, srcsize, &srcsize, ACM_STREAMSIZEF_SOURCE);
    if(srcsize<OUTBURST) srcsize=OUTBURST;
    avi_header.audio_out_minsize=srcsize; // audio output min. size
    if(verbose) printf("Audio ACM output buffer min. size: %d\n",srcsize);

    acmStreamSize(avi_header.srcstream, srcsize, &srcsize, ACM_STREAMSIZEF_DESTINATION);
    avi_header.audio_in_minsize=srcsize; // audio input min. size
    if(verbose) printf("Audio ACM input buffer min. size: %d\n",srcsize);

    return 1;
}


int init_video_codec(int outfmt){
  HRESULT ret;

  if(verbose) printf("======= Win32 (VFW) VIDEO Codec init =======\n");

  memset(&avi_header.o_bih, 0, sizeof(BITMAPINFOHEADER));
  avi_header.o_bih.biSize = sizeof(BITMAPINFOHEADER);

  win32_codec_name = avi_header.video_codec;
  avi_header.hic = ICOpen( 0x63646976, avi_header.bih.biCompression, ICMODE_FASTDECOMPRESS);
//  avi_header.hic = ICOpen( 0x63646976, avi_header.bih.biCompression, ICMODE_DECOMPRESS);
  if(!avi_header.hic){
    printf("ICOpen failed! unknown codec / wrong parameters?\n");
    return 0;
  }

//  avi_header.bih.biBitCount=32;

  ret = ICDecompressGetFormat(avi_header.hic, &avi_header.bih, &avi_header.o_bih);
  if(ret){
    printf("ICDecompressGetFormat failed: Error %d\n", ret);
    return 0;
  }
  if(verbose) printf("ICDecompressGetFormat OK\n");
  
//  printf("ICM_DECOMPRESS_QUERY=0x%X",ICM_DECOMPRESS_QUERY);

//  avi_header.o_bih.biWidth=avi_header.bih.biWidth;
//  avi_header.o_bih.biCompression = 0x32315659; //  mmioFOURCC('U','Y','V','Y');
//  ret=ICDecompressGetFormatSize(avi_header.hic,&avi_header.o_bih);
//  avi_header.o_bih.biCompression = 3; //0x32315659;
//  avi_header.o_bih.biCompression = mmioFOURCC('U','Y','V','Y');
//  avi_header.o_bih.biCompression = mmioFOURCC('U','Y','V','Y');
//  avi_header.o_bih.biCompression = mmioFOURCC('Y','U','Y','2');
//  avi_header.o_bih.biPlanes=3;
//  avi_header.o_bih.biBitCount=16;

  if(outfmt==IMGFMT_YUY2)
    avi_header.o_bih.biBitCount=16;
  else
    avi_header.o_bih.biBitCount=outfmt&0xFF;//   //24;

  avi_header.o_bih.biSizeImage=avi_header.o_bih.biWidth*avi_header.o_bih.biHeight*(avi_header.o_bih.biBitCount/8);

  if(!avi_header.flipped)
    avi_header.o_bih.biHeight=-avi_header.bih.biHeight; // flip image!

  if(outfmt==IMGFMT_YUY2 && !avi_header.yuv_hack_needed)
    avi_header.o_bih.biCompression = mmioFOURCC('Y','U','Y','2');

//  avi_header.o_bih.biCompression = mmioFOURCC('U','Y','V','Y');


  if(verbose) {
    printf("Starting decompression, format:\n");
	printf("  biSize %d\n", avi_header.bih.biSize);
	printf("  biWidth %d\n", avi_header.bih.biWidth);
	printf("  biHeight %d\n", avi_header.bih.biHeight);
	printf("  biPlanes %d\n", avi_header.bih.biPlanes);
	printf("  biBitCount %d\n", avi_header.bih.biBitCount);
	printf("  biCompression %d='%.4s'\n", avi_header.bih.biCompression, &avi_header.bih.biCompression);
	printf("  biSizeImage %d\n", avi_header.bih.biSizeImage);
    printf("Dest fmt:\n");
	printf("  biSize %d\n", avi_header.o_bih.biSize);
	printf("  biWidth %d\n", avi_header.o_bih.biWidth);
	printf("  biHeight %d\n", avi_header.o_bih.biHeight);
	printf("  biPlanes %d\n", avi_header.o_bih.biPlanes);
	printf("  biBitCount %d\n", avi_header.o_bih.biBitCount);
	printf("  biCompression %d='%.4s'\n", avi_header.o_bih.biCompression, &avi_header.o_bih.biCompression);
	printf("  biSizeImage %d\n", avi_header.o_bih.biSizeImage);
  }

  ret = ICDecompressQuery(avi_header.hic, &avi_header.bih, &avi_header.o_bih);
  if(ret){
    printf("ICDecompressQuery failed: Error %d\n", ret);
    return 0;
  }
  if(verbose) printf("ICDecompressQuery OK\n");

  
  ret = ICDecompressBegin(avi_header.hic, &avi_header.bih, &avi_header.o_bih);
  if(ret){
    printf("ICDecompressBegin failed: Error %d\n", ret);
    return 0;
  }

#if 0

//avi_header.hic
//ICSendMessage(HIC hic,unsigned int msg,long lParam1,long lParam2)
{ int i;
  for(i=73;i<256;i++){
    printf("Calling ICM_USER+%d function...",i);fflush(stdout);
    ret = ICSendMessage(avi_header.hic,ICM_USER+i,NULL,NULL);
    printf(" ret=%d\n",ret);
  }
}
#endif

  avi_header.our_out_buffer = malloc(avi_header.o_bih.biSizeImage);
  if(!avi_header.our_out_buffer){
    printf("not enough memory for decoded picture buffer (%d bytes)\n", avi_header.o_bih.biSizeImage);
    return 0;
  }

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

//  avi_header.our_in_buffer=malloc(avi_header.video.dwSuggestedBufferSize); // FIXME!!!!
  
  if(verbose) printf("VIDEO CODEC Init OK!!! ;-)\n");
  return 1;
}