Debian Codegraph @

drivers/vidix/vidixlib.c

raw
1 /*
2 * vidixlib.c
3 * VIDIXLib - Library for VIDeo Interface for *niX
4 * This interface is introduced as universal one to MPEG decoder,
5 * BES == Back End Scaler and YUV2RGB hw accelerators.
6 * In the future it may be expanded up to capturing and audio things.
7 * Main goal of this this interface imlpementation is providing DGA
8 * everywhere where it's possible (unlike X11 and other).
9 * Copyright 2002 Nick Kurshev
10 * Licence: GPL
11 * This interface is based on v4l2, fbvid.h, mga_vid.h projects
12 * and personally my ideas.
13 * NOTE: This interface is introduces as APP interface.
14 * Don't use it for driver.
15 * It provides multistreaming. This mean that APP can handle
16 * several streams simultaneously. (Example: Video capturing and video
17 * playback or capturing, video playback, audio encoding and so on).
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <string.h>
23
24 #include <dlfcn.h> /* GLIBC specific. Exists under cygwin too! */
25 #include <dirent.h>
26
27 #include "vidixlib.h"
28 #include "../bswap.h"
29
30 #define t_vdl(p) (((vdl_stream_t *)p))
31
32 typedef struct vdl_stream_s
33 {
34 void * handle;
35 int (*get_caps)(vidix_capability_t *);
36 int (*query_fourcc)(vidix_fourcc_t *);
37 int (*config_playback)(vidix_playback_t *);
38 int (*playback_on)( void );
39 int (*playback_off)( void );
40 /* Functions below can be missed in driver ;) */
41 int (*init)(const char *);
42 void (*destroy)(void);
43 int (*frame_sel)( unsigned frame_idx );
44 int (*get_eq)( vidix_video_eq_t * );
45 int (*set_eq)( const vidix_video_eq_t * );
46 int (*get_deint)( vidix_deinterlace_t * );
47 int (*set_deint)( const vidix_deinterlace_t * );
48 int (*copy_frame)( const vidix_dma_t * );
49 int (*query_dma)( void );
50 int (*get_gkey)( vidix_grkey_t * );
51 int (*set_gkey)( const vidix_grkey_t * );
52 int (*get_num_fx)( unsigned * );
53 int (*get_fx)( vidix_oem_fx_t * );
54 int (*set_fx)( const vidix_oem_fx_t * );
55 }vdl_stream_t;
56
57 static char drv_name[FILENAME_MAX];
58 static int dl_idx = -1;
59 /* currently available driver for static linking */
60 static const char* const drv_snames[] = {
61 #ifdef VIDIX_BUILD_STATIC
62 "genfb_",
63 "mach64_",
64 "mga_crtc2_",
65 "mga_",
66 "nvidia_",
67 "pm2_",
68 "pm3_",
69 "radeo_",
70 "rage128_",
71 #endif
72 NULL
73 };
74
75 extern unsigned vdlGetVersion( void )
76 {
77 return VIDIX_VERSION;
78 }
79
80 static void* dlsymm(void* handle, const char* fce)
81 {
82 char b[100];
83 #if defined(__OpenBSD__) && !defined(__ELF__)
84 b[0] = '_';
85 b[1] = 0;
86 #else
87 b[0] = 0;
88 #endif
89 if (dl_idx >= 0) strcat(b, drv_snames[dl_idx]);
90 strcat(b, fce);
91 //printf("Handle %p %s\n", handle, b);
92 return dlsym(handle, b);
93 }
94
95 static int vdl_fill_driver(VDL_HANDLE stream)
96 {
97 t_vdl(stream)->init = dlsymm(t_vdl(stream)->handle,"vixInit");
98 t_vdl(stream)->destroy = dlsymm(t_vdl(stream)->handle,"vixDestroy");
99 t_vdl(stream)->get_caps = dlsymm(t_vdl(stream)->handle,"vixGetCapability");
100 t_vdl(stream)->query_fourcc = dlsymm(t_vdl(stream)->handle,"vixQueryFourcc");
101 t_vdl(stream)->config_playback= dlsymm(t_vdl(stream)->handle,"vixConfigPlayback");
102 t_vdl(stream)->playback_on = dlsymm(t_vdl(stream)->handle,"vixPlaybackOn");
103 t_vdl(stream)->playback_off = dlsymm(t_vdl(stream)->handle,"vixPlaybackOff");
104 t_vdl(stream)->frame_sel = dlsymm(t_vdl(stream)->handle,"vixPlaybackFrameSelect");
105 t_vdl(stream)->get_eq = dlsymm(t_vdl(stream)->handle,"vixPlaybackGetEq");
106 t_vdl(stream)->set_eq = dlsymm(t_vdl(stream)->handle,"vixPlaybackSetEq");
107 t_vdl(stream)->get_gkey = dlsymm(t_vdl(stream)->handle,"vixGetGrKeys");
108 t_vdl(stream)->set_gkey = dlsymm(t_vdl(stream)->handle,"vixSetGrKeys");
109 t_vdl(stream)->get_deint = dlsymm(t_vdl(stream)->handle,"vixPlaybackGetDeint");
110 t_vdl(stream)->set_deint = dlsymm(t_vdl(stream)->handle,"vixPlaybackSetDeint");
111 t_vdl(stream)->copy_frame = dlsymm(t_vdl(stream)->handle,"vixPlaybackCopyFrame");
112 t_vdl(stream)->query_dma = dlsymm(t_vdl(stream)->handle,"vixQueryDMAStatus");
113 t_vdl(stream)->get_num_fx = dlsymm(t_vdl(stream)->handle,"vixQueryNumOemEffects");
114 t_vdl(stream)->get_fx = dlsymm(t_vdl(stream)->handle,"vixGetOemEffect");
115 t_vdl(stream)->set_fx = dlsymm(t_vdl(stream)->handle,"vixSetOemEffect");
116 /* check driver viability */
117 if(!( t_vdl(stream)->get_caps && t_vdl(stream)->query_fourcc &&
118 t_vdl(stream)->config_playback && t_vdl(stream)->playback_on &&
119 t_vdl(stream)->playback_off))
120 {
121 printf("vidixlib: Incomplete driver: some of essential features are missed in it.\n");
122 return 0;
123 }
124 return 1;
125 }
126
127 #ifndef RTLD_GLOBAL
128 #define RTLD_GLOBAL RTLD_LAZY
129 #endif
130 #ifndef RTLD_NOW
131 #define RTLD_NOW RTLD_LAZY
132 #endif
133
134 static int vdl_probe_driver(VDL_HANDLE stream,const char *path,const char *name,unsigned cap,int verbose)
135 {
136 vidix_capability_t vid_cap;
137 unsigned (*_ver)(void);
138 int (*_probe)(int,int);
139 int (*_cap)(vidix_capability_t*);
140 strcpy(drv_name,path);
141 strcat(drv_name,name);
142 if(verbose) printf("vidixlib: PROBING: %s\n",drv_name);
143
144 {
145 const char* slash = strrchr(drv_name, '/');
146 if (slash) {
147 for (dl_idx = 0; drv_snames[dl_idx]; dl_idx++) {
148 if (!strncmp(slash + 1, drv_snames[dl_idx], strlen(drv_snames[dl_idx])))
149 break; // locate the name
150 }
151 if (!drv_snames[dl_idx]) dl_idx = -1;
152 }
153 }
154 if (dl_idx < 0)
155 if(!(t_vdl(stream)->handle = dlopen(drv_name,RTLD_LAZY|RTLD_GLOBAL))) {
156 if(verbose) printf("vidixlib: %s not driver: %s\n",drv_name,dlerror());
157 return 0;
158 }
159 _ver = dlsymm(t_vdl(stream)->handle,"vixGetVersion");
160 _probe = dlsymm(t_vdl(stream)->handle,"vixProbe");
161 _cap = dlsymm(t_vdl(stream)->handle,"vixGetCapability");
162 if(_ver)
163 {
164 if((*_ver)() != VIDIX_VERSION)
165 {
166 if(verbose) printf("vidixlib: %s has wrong version\n",drv_name);
167 err:
168 dlclose(t_vdl(stream)->handle);
169 t_vdl(stream)->handle = 0;
170 dl_idx = -1;
171 return 0;
172 }
173 }
174 else
175 {
176 fatal_err:
177 if(verbose) printf("vidixlib: %s has no function definition\n",drv_name);
178 goto err;
179 }
180 if(_probe) { if((*_probe)(verbose,PROBE_NORMAL) != 0) goto err; }
181 else goto fatal_err;
182 if(_cap) { if((*_cap)(&vid_cap) != 0) goto err; }
183 else goto fatal_err;
184 if((vid_cap.type & cap) != cap)
185 {
186 if(verbose) printf("vidixlib: Found %s but has no required capability\n",drv_name);
187 goto err;
188 }
189 if(verbose) printf("vidixlib: %s probed o'k\n",drv_name);
190 return 1;
191 }
192
193 static int vdl_find_driver(VDL_HANDLE stream,const char *path,unsigned cap,int verbose)
194 {
195 DIR *dstream;
196 struct dirent *name;
197 int done = 0;
198 if(!(dstream = opendir(path))) return 0;
199 while(!done)
200 {
201 name = readdir(dstream);
202 if(name)
203 {
204 if(name->d_name[0] != '.')
205 if(vdl_probe_driver(stream,path,name->d_name,cap,verbose)) break;
206 }
207 else done = 1;
208 }
209 closedir(dstream);
210 return done?0:1;
211 }
212
213 VDL_HANDLE vdlOpen(const char *path,const char *name,unsigned cap,int verbose)
214 {
215 vdl_stream_t *stream;
216 const char *drv_args=NULL;
217 int errcode;
218 if(!(stream = malloc(sizeof(vdl_stream_t)))) return NULL;
219 memset(stream,0,sizeof(vdl_stream_t));
220 if(name)
221 {
222 unsigned (*ver)(void);
223 int (*probe)(int,int);
224 unsigned version = 0;
225 unsigned char *arg_sep;
226 arg_sep = strchr(name,':');
227 if(arg_sep) { *arg_sep='\0'; drv_args = &arg_sep[1]; }
228 strcpy(drv_name,path);
229 strcat(drv_name,name);
230 {
231 const char* slash = strrchr(drv_name, '/');
232 if (slash) {
233 for (dl_idx = 0; drv_snames[dl_idx]; dl_idx++) {
234 if (!strncmp(slash + 1, drv_snames[dl_idx], strlen(drv_snames[dl_idx])))
235 break; // locate the name
236 }
237 if (!drv_snames[dl_idx]) dl_idx = -1;
238 }
239 }
240 if (dl_idx < 0)
241 if(!(t_vdl(stream)->handle = dlopen(drv_name,RTLD_NOW|RTLD_GLOBAL)))
242 {
243 if (verbose)
244 printf("vidixlib: dlopen error: %s\n", dlerror());
245 err:
246 vdlClose(stream);
247 return NULL;
248 }
249 ver = dlsymm(t_vdl(stream)->handle,"vixGetVersion");
250 if(ver) version = (*ver)();
251 if(version != VIDIX_VERSION)
252 goto err;
253 probe = dlsymm(t_vdl(stream)->handle,"vixProbe");
254 if(probe) { if((*probe)(verbose,PROBE_FORCE)!=0) goto err; }
255 else goto err;
256 fill:
257 if(!vdl_fill_driver(stream)) goto err;
258 goto ok;
259 }
260 else
261 if(vdl_find_driver(stream,path,cap,verbose))
262 {
263 if(verbose) printf("vidixlib: will use %s driver\n",drv_name);
264 goto fill;
265 }
266 else goto err;
267 ok:
268 if(t_vdl(stream)->init)
269 {
270 if(verbose) printf("vidixlib: Attempt to initialize driver at: %p\n",t_vdl(stream)->init);
271 if((errcode=t_vdl(stream)->init(drv_args))!=0)
272 {
273 if(verbose) printf("vidixlib: Can't init driver: %s\n",strerror(errcode));
274 goto err;
275 }
276 }
277 if(verbose) printf("vidixlib: '%s'successfully loaded\n",drv_name);
278 return stream;
279 }
280
281 void vdlClose(VDL_HANDLE stream)
282 {
283 if(t_vdl(stream)->destroy) t_vdl(stream)->destroy();
284 if(t_vdl(stream)->handle) dlclose(t_vdl(stream)->handle);
285 memset(stream,0,sizeof(vdl_stream_t)); /* <- it's not stupid */
286 free(stream);
287 dl_idx = -1;
288 }
289
290 int vdlGetCapability(VDL_HANDLE handle, vidix_capability_t *cap)
291 {
292 return t_vdl(handle)->get_caps(cap);
293 }
294
295 #define MPLAYER_IMGFMT_RGB (('R'<<24)|('G'<<16)|('B'<<8))
296 #define MPLAYER_IMGFMT_BGR (('B'<<24)|('G'<<16)|('R'<<8))
297 #define MPLAYER_IMGFMT_RGB_MASK 0xFFFFFF00
298
299 static uint32_t normalize_fourcc(uint32_t fourcc)
300 {
301 if((fourcc & MPLAYER_IMGFMT_RGB_MASK) == (MPLAYER_IMGFMT_RGB|0) ||
302 (fourcc & MPLAYER_IMGFMT_RGB_MASK) == (MPLAYER_IMGFMT_BGR|0))
303 return bswap_32(fourcc);
304 else return fourcc;
305 }
306
307 int vdlQueryFourcc(VDL_HANDLE handle,vidix_fourcc_t *f)
308 {
309 f->fourcc = normalize_fourcc(f->fourcc);
310 return t_vdl(handle)->query_fourcc(f);
311 }
312
313 int vdlConfigPlayback(VDL_HANDLE handle,vidix_playback_t *p)
314 {
315 p->fourcc = normalize_fourcc(p->fourcc);
316 return t_vdl(handle)->config_playback(p);
317 }
318
319 int vdlPlaybackOn(VDL_HANDLE handle)
320 {
321 return t_vdl(handle)->playback_on();
322 }
323
324 int vdlPlaybackOff(VDL_HANDLE handle)
325 {
326 return t_vdl(handle)->playback_off();
327 }
328
329 int vdlPlaybackFrameSelect(VDL_HANDLE handle, unsigned frame_idx )
330 {
331 return t_vdl(handle)->frame_sel ? t_vdl(handle)->frame_sel(frame_idx) : ENOSYS;
332 }
333
334 int vdlPlaybackGetEq(VDL_HANDLE handle, vidix_video_eq_t * e)
335 {
336 return t_vdl(handle)->get_eq ? t_vdl(handle)->get_eq(e) : ENOSYS;
337 }
338
339 int vdlPlaybackSetEq(VDL_HANDLE handle, const vidix_video_eq_t * e)
340 {
341 return t_vdl(handle)->set_eq ? t_vdl(handle)->set_eq(e) : ENOSYS;
342 }
343
344 int vdlPlaybackCopyFrame(VDL_HANDLE handle, vidix_dma_t * f)
345 {
346 return t_vdl(handle)->copy_frame ? t_vdl(handle)->copy_frame(f) : ENOSYS;
347 }
348
349 int vdlQueryDMAStatus(VDL_HANDLE handle )
350 {
351 return t_vdl(handle)->query_dma ? t_vdl(handle)->query_dma() : ENOSYS;
352 }
353
354 int vdlGetGrKeys(VDL_HANDLE handle, vidix_grkey_t * k)
355 {
356 return t_vdl(handle)->get_gkey ? t_vdl(handle)->get_gkey(k) : ENOSYS;
357 }
358
359 int vdlSetGrKeys(VDL_HANDLE handle, const vidix_grkey_t * k)
360 {
361 return t_vdl(handle)->set_gkey ? t_vdl(handle)->set_gkey(k) : ENOSYS;
362 }
363
364 int vdlPlaybackGetDeint(VDL_HANDLE handle, vidix_deinterlace_t * d)
365 {
366 return t_vdl(handle)->get_deint ? t_vdl(handle)->get_deint(d) : ENOSYS;
367 }
368
369 int vdlPlaybackSetDeint(VDL_HANDLE handle, const vidix_deinterlace_t * d)
370 {
371 return t_vdl(handle)->set_deint ? t_vdl(handle)->set_deint(d) : ENOSYS;
372 }
373
374 int vdlQueryNumOemEffects(VDL_HANDLE handle, unsigned * number )
375 {
376 return t_vdl(handle)->get_num_fx ? t_vdl(handle)->get_num_fx(number) : ENOSYS;
377 }
378
379 int vdlGetOemEffect(VDL_HANDLE handle, vidix_oem_fx_t * f)
380 {
381 return t_vdl(handle)->get_fx ? t_vdl(handle)->get_fx(f) : ENOSYS;
382 }
383
384 int vdlSetOemEffect(VDL_HANDLE handle, const vidix_oem_fx_t * f)
385 {
386 return t_vdl(handle)->set_fx ? t_vdl(handle)->set_fx(f) : ENOSYS;
387 }
388
389 /* ABI related extensions */
390 vidix_capability_t * vdlAllocCapabilityS( void )
391 {
392 vidix_capability_t *retval;
393 retval=malloc(sizeof(vidix_capability_t));
394 if(retval) memset(retval,0,sizeof(vidix_capability_t));
395 return retval;
396 }
397
398 vidix_fourcc_t * vdlAllocFourccS( void )
399 {
400 vidix_fourcc_t *retval;
401 retval=malloc(sizeof(vidix_fourcc_t));
402 if(retval) memset(retval,0,sizeof(vidix_fourcc_t));
403 return retval;
404 }
405
406 vidix_yuv_t * vdlAllocYUVS( void )
407 {
408 vidix_yuv_t *retval;
409 retval=malloc(sizeof(vidix_yuv_t));
410 if(retval) memset(retval,0,sizeof(vidix_yuv_t));
411 return retval;
412 }
413
414 vidix_rect_t * vdlAllocRectS( void )
415 {
416 vidix_rect_t *retval;
417 retval=malloc(sizeof(vidix_rect_t));
418 if(retval) memset(retval,0,sizeof(vidix_rect_t));
419 return retval;
420 }
421
422 vidix_playback_t * vdlAllocPlaybackS( void )
423 {
424 vidix_playback_t *retval;
425 retval=malloc(sizeof(vidix_playback_t));
426 if(retval) memset(retval,0,sizeof(vidix_playback_t));
427 return retval;
428 }
429
430 vidix_grkey_t * vdlAllocGrKeyS( void )
431 {
432 vidix_grkey_t *retval;
433 retval=malloc(sizeof(vidix_grkey_t));
434 if(retval) memset(retval,0,sizeof(vidix_grkey_t));
435 return retval;
436 }
437
438 vidix_video_eq_t * vdlAllocVideoEqS( void )
439 {
440 vidix_video_eq_t *retval;
441 retval=malloc(sizeof(vidix_video_eq_t));
442 if(retval) memset(retval,0,sizeof(vidix_video_eq_t));
443 return retval;
444 }
445
446 vidix_deinterlace_t * vdlAllocDeinterlaceS( void )
447 {
448 vidix_deinterlace_t *retval;
449 retval=malloc(sizeof(vidix_deinterlace_t));
450 if(retval) memset(retval,0,sizeof(vidix_deinterlace_t));
451 return retval;
452 }
453
454 vidix_dma_t * vdlAllocDmaS( void )
455 {
456 vidix_dma_t *retval;
457 retval=malloc(sizeof(vidix_dma_t));
458 if(retval) memset(retval,0,sizeof(vidix_dma_t));
459 return retval;
460 }
461
462 vidix_oem_fx_t * vdlAllocOemFxS( void )
463 {
464 vidix_oem_fx_t *retval;
465 retval=malloc(sizeof(vidix_oem_fx_t));
466 if(retval) memset(retval,0,sizeof(vidix_oem_fx_t));
467 return retval;
468 }
469
470 void vdlFreeCapabilityS(vidix_capability_t * _this) { free(_this); }
471 void vdlFreeFourccS( vidix_fourcc_t * _this ) { free(_this); }
472 void vdlFreePlaybackS( vidix_playback_t * _this ) { free(_this); }
473 void vdlFreeYUVS( vidix_yuv_t * _this) { free(_this); }
474 void vdlFreeRectS( vidix_rect_t * _this) { free(_this); }
475 void vdlFreeGrKeyS( vidix_grkey_t * _this) { free(_this); }
476 void vdlFreeVideoEqS( vidix_video_eq_t * _this) { free(_this); }
477 void vdlFreeDeinterlaceS( vidix_deinterlace_t * _this) { free(_this); }
478 void vdlFreeDmaS( vidix_dma_t * _this) { free(_this); }
479 void vdlFreeOemFxS( vidix_oem_fx_t * _this) { free(_this); }