summaryrefslogtreecommitdiff
path: root/libs/winloader/driver.c
blob: 3dfd0ffa8eacd56bcfe7c6e77b5017da804a642d (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
#include <stdio.h>
#include <malloc.h>
#include <wine/driver.h>
#include <wine/pe_image.h>
#include <wine/winreg.h>
#include <wine/vfw.h>
#include <registry.h>

#include "config.h"

#define STORE_ALL \
    __asm__ ( \
    "push %%ebx\n\t" \
    "push %%ecx\n\t" \
    "push %%edx\n\t" \
    "push %%esi\n\t" \
    "push %%edi\n\t"::)

#define REST_ALL \
    __asm__ ( \
    "pop %%edi\n\t" \
    "pop %%esi\n\t" \
    "pop %%edx\n\t" \
    "pop %%ecx\n\t" \
    "pop %%ebx\n\t"::)


#define WIN32_PATH GST_WIN32_LIBDIR

typedef struct {
    UINT             uDriverSignature;
    HINSTANCE        hDriverModule;
    DRIVERPROC       DriverProc;
    DWORD            dwDriverID;
} DRVR;

typedef DRVR  *PDRVR;
typedef DRVR  *NPDRVR;
typedef DRVR  *LPDRVR;
    
static DWORD dwDrvID = 0;

LRESULT WINAPI SendDriverMessage( HDRVR hDriver, UINT message,
                                    LPARAM lParam1, LPARAM lParam2 )
{
    DRVR* module=(DRVR*)hDriver;
    int result;
#ifdef DETAILED_OUT    
    printf("SendDriverMessage: driver %X, message %X, arg1 %X, arg2 %X\n", hDriver, message, lParam1, lParam2);
#endif
    if(module==0)return -1;
    if(module->hDriverModule==0)return -1;
    if(module->DriverProc==0)return -1;
    STORE_ALL;
    result=module->DriverProc(module->dwDriverID,1,message,lParam1,lParam2);
    REST_ALL;
#ifdef DETAILED_OUT    
    printf("\t\tResult: %X\n", result);
#endif    
    return result;
}				    

static NPDRVR DrvAlloc(HDRVR*lpDriver, LPUINT lpDrvResult)
{
    NPDRVR npDriver;
    /* allocate and lock handle */
    if (lpDriver)
    {
        if (*lpDriver = (HDRVR) malloc(sizeof(DRVR)) )
        {
            if (npDriver = (NPDRVR) *lpDriver)
            {
                    *lpDrvResult = MMSYSERR_NOERROR;
                    return (npDriver);
            }
            free((NPDRVR)*lpDriver);
        }
        return (*lpDrvResult = MMSYSERR_NOMEM, (NPDRVR) 0);
    }
    return (*lpDrvResult = MMSYSERR_INVALPARAM, (NPDRVR) 0);
}

typedef struct
{
    HMODULE handle;
    char name[64];
    int usage;
}codec_t;

static codec_t avi_codecs[]={
 {0, WIN32_PATH"/divxc32.dll", 0},	//0
 {0, WIN32_PATH"/ir50_32.dll", 0},
 {0, WIN32_PATH"/ir41_32.dll", 0},
 {0, WIN32_PATH"/ir32_32.dll", 0},    
 {0, WIN32_PATH"/mpg4c32.dll", 0},
 {0, WIN32_PATH"/iccvid.dll", 0},	//5
 {0, WIN32_PATH"/libvideodll.so", 0},
 {0, WIN32_PATH"/divxa32.acm", 0},	
 {0, WIN32_PATH"/msadp32.acm", 0},
 {0, WIN32_PATH"/ativcr1.dll", 0},
 {0, WIN32_PATH"/ativcr2.dll", 0},	//10
 {0, WIN32_PATH"/i263_32.drv", 0},
 {0, WIN32_PATH"/l3codeca.acm", 0},
// {0, WIN32_PATH"/atiyvu9.dll", 0},
};

                                                                                                                    
static void DrvFree(HDRVR hDriver)
{
    int i;
    if(hDriver)
    	if(((DRVR*)hDriver)->hDriverModule)
    	if(((DRVR*)hDriver)->DriverProc)
	(((DRVR*)hDriver)->DriverProc)(((DRVR*)hDriver)->dwDriverID, hDriver, DRV_CLOSE, 0, 0);
    if(hDriver)
    for(i=0; i<sizeof(avi_codecs)/sizeof(avi_codecs[0]); i++)
	if(avi_codecs[i].handle==((DRVR*)hDriver)->hDriverModule)
	{
	    avi_codecs[i].usage--;	
	    if(avi_codecs[i].usage==0)
	    { 
    		avi_codecs[i].handle=0;	    
                 if(((DRVR*)hDriver)->hDriverModule)
    		if(((DRVR*)hDriver)->DriverProc)
			(((DRVR*)hDriver)->DriverProc)(0, hDriver, DRV_FREE, 0, 0);
		FreeLibrary(((DRVR*)hDriver)->hDriverModule);
    		if (hDriver)
        	    		free((NPDRVR)hDriver);
		return;	
	    }	
	}      
}       

void DrvClose(HDRVR hdrvr)
{
    DrvFree(hdrvr);
}
    
HDRVR
//DrvOpen(LPCSTR lpszDriverName, LPCSTR lpszSectionName, LPARAM lParam2)
DrvOpen(LPARAM lParam2)
{
    int drv_id;
    char filename[MAX_PATH], *f;
    UINT uDrvResult;
    HDRVR hDriver;
    NPDRVR npDriver;
     char unknown[0x24];
    int seg;
    int qwe;
    int regs[10];

    int fccHandler=*((int*)lParam2+2);
    int fccType=*((int*)lParam2+1);
    if(fccType==0x63646976)//vidc
	switch(fccHandler)
	{
	case mmioFOURCC('D', 'I', 'V', '3'):
	case mmioFOURCC('D', 'I', 'V', '4'):
	case mmioFOURCC('d', 'i', 'v', '3'):
        case mmioFOURCC('d', 'i', 'v', '4'):
	case mmioFOURCC('M', 'P', '4', '1'):
	case mmioFOURCC('M', 'P', '4', '2'):
	case mmioFOURCC('M', 'P', '4', '3'):
	    printf("Video in DivX ;-) format\n");
	    drv_id=0;
	    break;
	case mmioFOURCC('I', 'V', '5', '0'):	    
	case mmioFOURCC('i', 'v', '5', '0'):	 
	    printf("Video in Indeo Video 5 format\n");   
	    drv_id=1;
	    break;
	case mmioFOURCC('I', 'V', '4', '1'):	    
	case mmioFOURCC('i', 'v', '4', '1'):	    
	    printf("Video in Indeo Video 4.1 format\n");   
	    drv_id=2;
	    break;
	case mmioFOURCC('I', 'V', '3', '2'):	    
	case mmioFOURCC('i', 'v', '3', '2'):
	    printf("Video in Indeo Video 3.2 format\n");   
	    drv_id=3;
	    break;
	
	case mmioFOURCC('m', 'p', '4', '1'):
	case mmioFOURCC('m', 'p', '4', '2'):
        case mmioFOURCC('m', 'p', '4', '3'):
	case mmioFOURCC('M', 'P', 'G', '4'):
	    printf("Video in Microsoft MPEG-4 format\n");   
	    drv_id=4;
	    break;
	case mmioFOURCC('c', 'v', 'i', 'd'):
	    printf("Video in Cinepak format\n");   
	    drv_id=5;
	    break;	
	case mmioFOURCC('V', 'C', 'R', '1'):
	    drv_id=9;
	    break;
	case mmioFOURCC('V', 'C', 'R', '2'):
	    drv_id=10;
	    break;
	case mmioFOURCC('i', '2', '6', '3'):
	case mmioFOURCC('I', '2', '6', '3'):
	    drv_id=11;
	    break;	    	  
//	case mmioFOURCC('Y', 'V', 'U', '9'):
//	    drv_id=12;
//	    break;  
	default:
	    printf("Unknown codec %X='%c%c%c%c'\n", fccHandler, 
	    fccHandler&0xFF, (fccHandler&0xFF00)>>8,
    	    (fccHandler&0xFF0000)>>16, (fccHandler&0xFF000000)>>24);
    	    return (HDRVR)0;	
        }
    else
	switch(fccHandler)
	{
    	case 0x160://DivX audio
    	case 0x161://DivX audio
	    drv_id=7; 
	    break;
	case 0x2://MS ADPCM
	    drv_id=8;
	    break;
	case 0x55://MPEG Layer 3
	printf("MPEG Layer 3 ( 0x55 )\n");
	    drv_id=12;
	    break;
	default:
	    printf("Unknown ACM codec 0x%X\n", fccHandler);
	    return (HDRVR)0;
	}
	
    if (!(npDriver = DrvAlloc(&hDriver, &uDrvResult)))
	return ((HDRVR) 0);

    if(avi_codecs[drv_id].handle==0)
    {
     if (!(avi_codecs[drv_id].handle=npDriver->hDriverModule = LoadLibraryA(avi_codecs[drv_id].name)))
     {
     	printf("Can't open library %s\n", avi_codecs[drv_id].name);
        DrvFree(hDriver);
        return ((HDRVR) 0);
     }
     else avi_codecs[drv_id].usage=1;
    }
    else
    {
	npDriver->hDriverModule=avi_codecs[drv_id].handle;
	avi_codecs[drv_id].usage++;
    }	
    
//    14c0 
    if(drv_id==0)
    {
	int newkey;
	int bitrate;	
	int count=4;
	if(RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\LinuxLoader\\Divx", 0, 0, &newkey)!=0)
	    goto no_reg;
	if(RegQueryValueExA(newkey, "BitRate", 0, 0, &bitrate, &count)!=0)
	{
	    RegCloseKey(newkey);
	    goto no_reg;
	}    
//	printf("Setting default bitrate from %f to %d\n",
//		*(double*)((char*)npDriver->hDriverModule+0x14c0), bitrate);
 
	*(double*)((char*)npDriver->hDriverModule+0x14c0)=bitrate;
	RegCloseKey(newkey);
    no_reg:
	;	
    }	
   
     if (!(npDriver->DriverProc = (DRIVERPROC)
             GetProcAddress(npDriver->hDriverModule, "DriverProc")))
         {
         printf("Library %s is not a valid codec\n", avi_codecs[drv_id].name);
         FreeLibrary(npDriver->hDriverModule);
         DrvFree(hDriver);
         return ((HDRVR) 0);
     }
     
    TRACE("DriverProc == %X\n", npDriver->DriverProc);
     npDriver->dwDriverID = ++dwDrvID;

     if (avi_codecs[drv_id].usage==1)
     {
	STORE_ALL;
        (npDriver->DriverProc)(0, hDriver, DRV_LOAD, 0, 0);
	REST_ALL;
	TRACE("DRV_LOAD Ok!\n");
	STORE_ALL;
	(npDriver->DriverProc)(0, hDriver, DRV_ENABLE, 0, 0);
	REST_ALL;
	TRACE("DRV_ENABLE Ok!\n");
     }

     // open driver 
    STORE_ALL;
     npDriver->dwDriverID=(npDriver->DriverProc)(npDriver->dwDriverID, hDriver, DRV_OPEN,
         (LPARAM) (LPSTR) unknown, lParam2);
    REST_ALL;

    TRACE("DRV_OPEN Ok!(%X)\n", npDriver->dwDriverID);

    if (uDrvResult)
    {
         DrvFree(hDriver);
         hDriver = (HDRVR) 0;
     }
     return (hDriver);
}