comparison xacodec.c @ 2372:0959af64c098

needs to be hacked
author alex
date Mon, 22 Oct 2001 16:54:13 +0000
parents
children 463da8544d23
comparison
equal deleted inserted replaced
2371:0f2cad867121 2372:0959af64c098
1 /*
2 xacodec.c
3 XAnim Video Codec DLL support
4
5 (C) 2001 Alex Beregszaszi <alex@naxine.org>
6 */
7
8 #include <stdio.h>
9 #include <dlfcn.h> /* dlsym, dlopen, dlclose */
10 #include <stdarg.h> /* va_alist, va_start, va_end */
11 #include <errno.h> /* strerror, errno */
12
13 #include "odeon.h"
14 #include "aclib/byteswap.h"
15
16 #include "stream.h"
17 #include "demuxer.h"
18 #include "codec-cfg.h"
19 #include "wine/avifmt.h"
20 #include "wine/vfw.h"
21 #include "wine/mmreg.h"
22 #include "stheader.h"
23 #include "loader/DirectShow/default.h"
24 #include "libvo/img_format.h"
25 #include "xacodec.h"
26
27 #ifndef RTLD_NOW
28 #define RLTD_NOW 2
29 #endif
30 #ifndef RTLD_LAZY
31 #define RLTD_LAZY 1
32 #endif
33 #ifndef RTLD_GLOBAL
34 #define RLTD_GLOBAL 256
35 #endif
36
37 extern int verbose;
38
39 typedef struct xacodec_driver
40 {
41 XA_DEC_INFO *decinfo;
42 void *file_handler;
43 long (*iq_func)(XA_CODEC_HDR *codec_hdr);
44 unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
45 unsigned int dsize, XA_DEC_INFO *dec_info);
46 void (*close_func)();
47 } xacodec_driver_t;
48
49 xacodec_driver_t *xacodec_driver = NULL;
50
51 /* *** XANIM SHIT *** */
52
53 typedef struct
54 {
55 unsigned char r0, g0, b0;
56 unsigned char r1, g1, b1;
57 unsigned char r2, g2, b2;
58 unsigned char r3, g3, b3;
59 unsigned int clr0_0, clr0_1, clr0_2, clr0_3;
60 unsigned int clr1_0, clr1_1, clr1_2, clr1_3;
61 unsigned int clr2_0, clr2_1, clr2_2, clr2_3;
62 unsigned int clr3_0, clr3_1, clr3_2, clr3_3;
63 } XA_2x2_Color;
64
65 #define ip_OUT_2x2_1BLK(ip, CAST, cmap2x2, rinc) { register CAST d0, d1; \
66 *ip++ = d0 = (CAST)(cmap2x2->clr0_0); *ip++ = d0; \
67 *ip++ = d1 = (CAST)(cmap2x2->clr1_0); *ip = d1; \
68 ip += rinc; \
69 *ip++ = d0; *ip++ = d0; *ip++ = d1; *ip = d1; ip += rinc; \
70 *ip++ = d0 = (CAST)(cmap2x2->clr2_0); *ip++ = d0; \
71 *ip++ = d1 = (CAST)(cmap2x2->clr3_0); *ip = d1; \
72 ip += rinc; *ip++ = d0; *ip++ = d0; *ip++ = d1; *ip++ = d1; }
73
74 #define ip_OUT_2x2_2BLKS(ip, CAST, c2x2map0, c2x2map1, rinc) { \
75 *ip++ = (CAST)(c2x2map0->clr0_0); \
76 *ip++ = (CAST)(c2x2map0->clr1_0); \
77 *ip++ = (CAST)(c2x2map1->clr0_0); \
78 *ip = (CAST)(c2x2map1->clr1_0); ip += rinc; \
79 *ip++ = (CAST)(c2x2map0->clr2_0); \
80 *ip++ = (CAST)(c2x2map0->clr3_0); \
81 *ip++ = (CAST)(c2x2map1->clr2_0); \
82 *ip = (CAST)(c2x2map1->clr3_0); }
83
84 void XA_2x2_OUT_1BLK_clr8(unsigned char *image, unsigned int x, unsigned int y,
85 unsigned int imagex, XA_2x2_Color *cmap2x2)
86 {
87 unsigned int row_inc = imagex - 3;
88 unsigned char *ip = (unsigned char *)(image + y * imagex + x);
89
90 XA_Print("XA_2x2_OUT_1BLK_clr8('image: %08x', 'x: %d', 'y: %d', 'imagex: %d', 'cmap2x2: %08x')",
91 image, x, y, imagex, cmap2x2);
92 ip_OUT_2x2_1BLK(ip, unsigned char, cmap2x2, row_inc);
93 }
94
95 void XA_2x2_OUT_4BLKS_clr8(unsigned char *image, unsigned int x, unsigned int y,
96 unsigned int imagex, XA_2x2_Color *cm0, XA_2x2_Color *cm1, XA_2x2_Color *cm2,
97 XA_2x2_Color *cm3)
98 {
99 unsigned int row_inc = imagex - 3;
100 unsigned char *ip = (unsigned char *)(image + y * imagex + x);
101
102 XA_Print("XA_2x2_OUT_1BLK_clr8('image: %08x', 'x: %d', 'y: %d', 'imagex: %d', 'cm0: %08x', 'cm1: %08x', 'cm2: %08x', 'cm3: %08x')",
103 image, x, y, imagex, cm0, cm1, cm2, cm3);
104 ip_OUT_2x2_2BLKS(ip, unsigned char, cm0, cm1, row_inc);
105 ip += row_inc;
106 ip_OUT_2x2_2BLKS(ip, unsigned char, cm2, cm3, row_inc);
107 }
108
109 void *YUV2x2_Blk_Func(unsigned int image_type, int blks, unsigned int dith_flag)
110 {
111 void (*color_func)();
112
113 XA_Print("YUV2x2_Blk_Func('image_type: %d', 'blks: %d', 'dith_flag: %d')",
114 image_type, blks, dith_flag);
115
116 if (blks == 1)
117 {
118 switch(image_type)
119 {
120 default:
121 color_func = XA_2x2_OUT_1BLK_clr8;
122 break;
123 }
124 }
125 else /* blks == 4 */
126 {
127 switch(image_type)
128 {
129 default:
130 color_func = XA_2x2_OUT_4BLKS_clr8;
131 break;
132 }
133 }
134
135 XA_Print("YUV2x2_Blk_Func -> %08x", color_func);
136
137 return((void *)color_func);
138 }
139
140 void *YUV2x2_Map_Func(unsigned int image_type, unsigned int dith_type)
141 {
142 XA_Print("YUV2x2_Map_Func('image_type: %d', 'dith_type: %d')",
143 image_type, dith_type);
144 }
145
146 int XA_Add_Func_To_Free_Chain(XA_ANIM_HDR *anim_hdr, void (*function)())
147 {
148 XA_Print("XA_Add_Func_To_Free_Chain('anim_hdr: %08x', 'function: %08x')",
149 anim_hdr, function);
150 xacodec_driver->close_func = function;
151 }
152
153 int XA_Gen_YUV_Tabs(XA_ANIM_HDR *anim_hdr)
154 {
155 XA_Print("XA_Gen_YUV_Tabs('anim_hdr: %08x')", anim_hdr);
156
157 // XA_Print("anim type: %d - img[x: %d, y: %d, c: %d, d: %d]",
158 // anim_hdr->anim_type, anim_hdr->imagex, anim_hdr->imagey,
159 // anim_hdr->imagec, anim_hdr->imaged);
160 }
161
162 void JPG_Setup_Samp_Limit_Table(XA_ANIM_HDR *anim_hdr)
163 {
164 XA_Print("JPG_Setup_Samp_Limit_Table('anim_hdr: %08x')", anim_hdr);
165 }
166
167 void JPG_Alloc_MCU_Bufs(XA_ANIM_HDR *anim_hdr, unsigned int width,
168 unsigned int height, unsigned int full_flag)
169 {
170 XA_Print("JPG_Alloc_MCU_Bufs('anim_hdr: %08x', 'width: %d', 'height: %d', 'full_flag: %d')",
171 anim_hdr, width, height, full_flag);
172 }
173
174 typedef struct
175 {
176 unsigned char *Ybuf;
177 unsigned char *Ubuf;
178 unsigned char *Vbuf;
179 unsigned char *the_buf;
180 unsigned int the_buf_size;
181 unsigned short y_w, y_h;
182 unsigned short uv_w, uv_h;
183 } YUVBufs;
184
185 typedef struct
186 {
187 unsigned long Uskip_mask;
188 long *YUV_Y_tab;
189 long *YUV_UB_tab;
190 long *YUV_VR_tab;
191 long *YUV_UG_tab;
192 long *YUV_VG_tab;
193 } YUVTabs;
194
195 #define XA_IMTYPE_RGB 0x0001
196
197 void XA_YUV1611_To_RGB(unsigned char *image, unsigned int imagex, unsigned int imagey,
198 unsigned int i_x, unsigned int i_y, YUVBufs *yuv_bufs, YUVTabs *yuv_tabs,
199 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
200 {
201 XA_Print("XA_YUV1611_To_RGB('image: %08x', 'imagex: %d', 'imagey: %d', 'i_x: %d', 'i_y: %d', 'yuv_bufs: %08x', 'yuv_tabs: %08x', 'map_flag: %d', 'map: %08x', 'chdr: %08x')",
202 image, imagex, imagey, i_x, i_y, yuv_bufs, yuv_tabs, map_flag, map, chdr);
203 }
204
205 void XA_YUV1611_To_CLR8(unsigned char *image, unsigned int imagex, unsigned int imagey,
206 unsigned int i_x, unsigned int i_y, YUVBufs *yuv_bufs, YUVTabs *yuv_tabs,
207 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
208 {
209 XA_Print("XA_YUV1611_To_CLR8('image: %08x', 'imagex: %d', 'imagey: %d', 'i_x: %d', 'i_y: %d', 'yuv_bufs: %08x', 'yuv_tabs: %08x', 'map_flag: %d', 'map: %08x', 'chdr: %08x')",
210 image, imagex, imagey, i_x, i_y, yuv_bufs, yuv_tabs, map_flag, map, chdr);
211 }
212
213 void *XA_YUV1611_Func(unsigned int image_type)
214 {
215 void (*color_func)();
216
217 XA_Print("XA_YUV1611_Func('image_type: %d')", image_type);
218
219 switch(image_type)
220 {
221 case XA_IMTYPE_RGB:
222 color_func = XA_YUV1611_To_RGB;
223 break;
224 default:
225 color_func = XA_YUV1611_To_CLR8;
226 break;
227 }
228
229 return((void *)color_func);
230 }
231
232 unsigned long XA_Time_Read()
233 {
234 return(GetRelativeTime());
235 }
236
237 /* YUV 41 11 11 routines */
238 void XA_YUV411111_To_RGB(unsigned char *image, unsigned int imagex, unsigned int imagey,
239 unsigned int i_x, unsigned int i_y, YUVBufs *yuv_bufs, YUVTabs *yuv_tabs,
240 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
241 {
242 XA_Print("XA_YUV411111_To_RGB('image: %d', 'imagex: %d', 'imagey: %d', 'i_x: %d', 'i_y: %d', 'yuv_bufs: %08x', 'yuv_tabs: %08x', 'map_flag: %d', 'map: %08x', 'chdr: %08x')",
243 image, imagex, imagey, i_x, i_y, yuv_bufs, yuv_tabs, map_flag, map, chdr);
244 }
245
246
247 void *XA_YUV411111_Func(unsigned int image_type)
248 {
249 void (*color_func)();
250
251 XA_Print("XA_YUV411111_Func('image_type: %d')", image_type);
252
253 switch(image_type)
254 {
255 case XA_IMTYPE_RGB: color_func = XA_YUV411111_To_RGB; break;
256 }
257
258 return((void *)color_func);
259 }
260
261
262 YUVBufs jpg_YUVBufs;
263 YUVTabs def_yuv_tabs;
264
265 /* *** EOF XANIM SHIT *** */
266
267 /* Needed by XAnim DLLs */
268 int XA_Print(char *fmt, ...)
269 {
270 va_list vallist;
271 char buf[1024];
272
273 va_start(vallist, fmt);
274 vsnprintf(buf, 1024, fmt, vallist);
275 odprintf(LOG_NORM, "[xacodec] %s", buf);
276 va_end(vallist);
277 }
278
279 int TheEnd1(char *err_mess)
280 {
281 XA_Print("error: %s - exiting\n", err_mess);
282 xacodec_exit();
283 }
284 /* end of crap */
285
286 /* load, init and query */
287 int xacodec_init(char *filename, xacodec_driver_t *codec_driver)
288 {
289 void *(*what_the)();
290 char *error;
291 XAVID_MOD_HDR *mod_hdr;
292 XAVID_FUNC_HDR *func;
293 int i;
294
295 codec_driver->file_handler = dlopen(filename, RTLD_NOW|RTLD_GLOBAL);
296 if (!codec_driver->file_handler)
297 {
298 error = dlerror();
299 if (error)
300 printf("xacodec: fialed to init %s while %s\n", filename, error);
301 else
302 printf("xacodec: failed ot init (dlopen) %s\n", filename);
303 return(0);
304 }
305
306 what_the = dlsym(codec_driver->file_handler, "What_The");
307 if ((error = dlerror()) != NULL)
308 {
309 printf("xacodec: failed to init %s while %s\n", filename, error);
310 dlclose(codec_driver->file_handler);
311 return(0);
312 }
313
314 mod_hdr = what_the();
315 if (!mod_hdr)
316 {
317 printf("xacodec: 'What_The' function failed in %s\n", filename);
318 dlclose(codec_driver->file_handler);
319 return(0);
320 }
321
322 printf("=== XAnim Codec ===\n");
323 printf(" Filename: %s (API revision: %x)\n", filename, mod_hdr->api_rev);
324 printf(" Codec: %s. Rev: %s\n", mod_hdr->desc, mod_hdr->rev);
325 if (mod_hdr->copyright)
326 printf(" %s\n", mod_hdr->copyright);
327 if (mod_hdr->mod_author)
328 printf(" Module Author(s): %s\n", mod_hdr->mod_author);
329 if (mod_hdr->authors)
330 printf(" Codec Author(s): %s\n", mod_hdr->authors);
331
332 if (mod_hdr->api_rev > XAVID_API_REV)
333 {
334 printf("xacodec: not supported api revision in %s\n", filename);
335 dlclose(codec_driver->file_handler);
336 return(0);
337 }
338
339 func = mod_hdr->funcs;
340 if (!func)
341 {
342 printf("xacodec: function table error in %s\n", filename);
343 dlclose(codec_driver->file_handler);
344 return(0);
345 }
346
347 if (verbose > 2)
348 printf("dump: funcs: 0x%08x (num %d)\n", mod_hdr->funcs, mod_hdr->num_funcs);
349 for (i = 0; i < mod_hdr->num_funcs; i++)
350 {
351 if (verbose > 2)
352 printf("dump(%d): %d %d [iq:0x%08x d:0x%08x]\n", i,
353 func[i].what, func[i].id, func[i].iq_func, func[i].dec_func);
354 if (func[i].what & XAVID_AVI_QUERY)
355 {
356 printf("0x%08x: avi init/query func (id: %d)\n",
357 func[i].iq_func, func[i].id);
358 codec_driver->iq_func = func[i].iq_func;
359 }
360 if (func[i].what & XAVID_QT_QUERY)
361 {
362 printf("0x%08x: qt init/query func (id: %d)\n",
363 func[i].iq_func, func[i].id);
364 codec_driver->iq_func = func[i].iq_func;
365 }
366 if (func[i].what & XAVID_DEC_FUNC)
367 {
368 printf("0x%08x: decoder func (init/query: 0x%08x) (id: %d)\n",
369 func[i].dec_func, func[i].iq_func, func[i].id);
370 codec_driver->dec_func = func[i].dec_func;
371 }
372 }
373 return(1);
374 }
375
376 int xacodec_query(xacodec_driver_t *codec_driver, XA_CODEC_HDR *codec_hdr)
377 {
378 long codec_ret;
379
380 codec_ret = codec_driver->iq_func(codec_hdr);
381 switch(codec_ret)
382 {
383 case CODEC_SUPPORTED:
384 codec_driver->dec_func = codec_hdr->decoder;
385 printf("Codec is supported: found decoder for %s at 0x%08x\n",
386 codec_hdr->description, codec_hdr->decoder);
387 return(1);
388 case CODEC_UNSUPPORTED:
389 printf("Codec is unsupported\n");
390 return(0);
391 case CODEC_UNKNOWN:
392 default:
393 printf("Codec is unknown\n");
394 return(0);
395 }
396 }
397
398 char *xacodec_def_path = XACODEC_PATH;
399
400 int xacodec_init_video(sh_video_t *vidinfo, int out_format)
401 {
402 char dll[1024];
403 XA_CODEC_HDR codec_hdr;
404
405 xacodec_driver = realloc(xacodec_driver, sizeof(struct xacodec_driver));
406 if (xacodec_driver == NULL)
407 return(0);
408
409 xacodec_driver->iq_func = NULL;
410 xacodec_driver->dec_func = NULL;
411 xacodec_driver->close_func = NULL;
412
413 snprintf(dll, 1024, "%s/%s", xacodec_def_path, vidinfo->codec->dll);
414 if (xacodec_init(dll, xacodec_driver) == 0)
415 return(0);
416
417 codec_hdr.anim_hdr = malloc(4096);
418 codec_hdr.description = vidinfo->codec->info;
419 codec_hdr.compression = bswap_32(vidinfo->format);
420 codec_hdr.decoder = NULL;
421 codec_hdr.avi_ctab_flag = 0;
422 codec_hdr.avi_read_ext = NULL;
423 codec_hdr.xapi_rev = 0x0001;
424 codec_hdr.extra = NULL;
425 codec_hdr.x = vidinfo->disp_w;
426 codec_hdr.y = vidinfo->disp_h;
427
428 switch(out_format)
429 {
430 case IMGFMT_RGB8:
431 codec_hdr.depth = 8;
432 break;
433 case IMGFMT_RGB15:
434 codec_hdr.depth = 15;
435 break;
436 case IMGFMT_RGB16:
437 codec_hdr.depth = 16;
438 break;
439 case IMGFMT_RGB24:
440 codec_hdr.depth = 24;
441 break;
442 case IMGFMT_RGB32:
443 codec_hdr.depth = 32;
444 break;
445 case IMGFMT_BGR8:
446 codec_hdr.depth = 8;
447 break;
448 case IMGFMT_BGR15:
449 codec_hdr.depth = 15;
450 break;
451 case IMGFMT_BGR16:
452 codec_hdr.depth = 16;
453 break;
454 case IMGFMT_BGR24:
455 codec_hdr.depth = 24;
456 break;
457 case IMGFMT_BGR32:
458 codec_hdr.depth = 32;
459 break;
460 default:
461 printf("xacodec: not supported image format (%s)\n",
462 vo_format_name(out_format));
463 return(0);
464 }
465 odprintf(LOG_INFO, "xacodec: querying for %dx%d %dbit [fourcc: %4x] (%s)...\n",
466 codec_hdr.x, codec_hdr.y, codec_hdr.depth, codec_hdr.compression,
467 codec_hdr.description);
468
469 if (xacodec_query(xacodec_driver, &codec_hdr) == 0)
470 return(0);
471
472 // free(codec_hdr.anim_hdr);
473
474 xacodec_driver->decinfo = malloc(sizeof(XA_DEC_INFO));
475 xacodec_driver->decinfo->cmd = 0;
476 xacodec_driver->decinfo->skip_flag = 0;
477 xacodec_driver->decinfo->imagex = xacodec_driver->decinfo->xe = codec_hdr.x;
478 xacodec_driver->decinfo->imagey = xacodec_driver->decinfo->ye = codec_hdr.y;
479 xacodec_driver->decinfo->imaged = codec_hdr.depth;
480 xacodec_driver->decinfo->chdr = NULL;
481 xacodec_driver->decinfo->map_flag = 0; /* xaFALSE */
482 xacodec_driver->decinfo->map = NULL;
483 xacodec_driver->decinfo->xs = xacodec_driver->decinfo->ys = 0;
484 xacodec_driver->decinfo->special = 0;
485 xacodec_driver->decinfo->extra = codec_hdr.extra;
486
487 vidinfo->our_out_buffer = malloc(codec_hdr.y * codec_hdr.x * codec_hdr.depth);
488
489 printf("out_buf size: %d\n", codec_hdr.y * codec_hdr.x * codec_hdr.depth);
490
491 if (vidinfo->our_out_buffer == NULL)
492 {
493 odprintf(LOG_ERROR, "cannot allocate memory for output: %s",
494 strerror(errno));
495 return(0);
496 }
497
498 printf("extra: %08x - %dx%d %dbit\n", codec_hdr.extra,
499 codec_hdr.x, codec_hdr.y, codec_hdr.depth);
500 return(1);
501 }
502
503 #define ACT_DLT_NORM 0x00000000
504 #define ACT_DLT_BODY 0x00000001
505 #define ACT_DLT_XOR 0x00000002
506 #define ACT_DLT_NOP 0x00000004
507 #define ACT_DLT_MAPD 0x00000008
508 #define ACT_DLT_DROP 0x00000010
509 #define ACT_DLT_BAD 0x80000000
510
511 // unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
512 // unsigned int dsize, XA_DEC_INFO *dec_info);
513
514 int xacodec_decode_frame(uint8_t *frame, int frame_size,
515 uint8_t *dest, int skip_flag)
516 {
517 unsigned int ret;
518 int i;
519
520 if (skip_flag > 0)
521 printf("frame will be dropped..\n");
522
523 xacodec_driver->decinfo->skip_flag = skip_flag;
524
525 printf("frame: %08x (size: %d) - dest: %08x\n", frame, frame_size, dest);
526
527 ret = xacodec_driver->dec_func(dest, frame, frame_size, xacodec_driver->decinfo);
528
529 printf("ret: %lu : ", ret);
530
531 for (i = 0; i < 10; i++)
532 {
533 if (frame[i] != dest[i])
534 printf("%d: [%02x] != [%02x] ", i, frame[i], dest[i]);
535 else
536 printf("%d: [%02x] ", frame[i]);
537 }
538
539 if (ret == ACT_DLT_NORM)
540 {
541 printf("norm\n");
542 return(TRUE);
543 }
544
545 /*
546 if (!(ret & ACT_DLT_MAPD))
547 xacodec_driver->decinfo->map_flag = 0;
548 else
549 {
550 xacodec_driver->decinfo->map_flag = 1;
551 xacodec_driver->decinfo->map = ...
552 }
553 */
554
555 if (ret & ACT_DLT_NOP)
556 {
557 printf("nop\n");
558 return(FALSE); /* dst = 0 */
559 }
560
561 if (ret & ACT_DLT_DROP) /* by skip frames and errors */
562 {
563 printf("drop\n");
564 return(FALSE);
565 }
566
567
568 if (ret & ACT_DLT_BAD)
569 {
570 printf("bad\n");
571 return(FALSE);
572 }
573
574 if (ret & ACT_DLT_BODY)
575 {
576 printf("body\n");
577 return(TRUE);
578 }
579
580 if (ret & ACT_DLT_XOR)
581 {
582 printf("xor\n");
583 return(TRUE);
584 }
585
586 return(FALSE);
587 }
588
589 int xacodec_exit()
590 {
591 if (xacodec_driver->close_func)
592 xacodec_driver->close_func();
593 dlclose(xacodec_driver->file_handler);
594 if (xacodec_driver->decinfo != NULL)
595 free(xacodec_driver->decinfo);
596 if (xacodec_driver != NULL)
597 free(xacodec_driver);
598 return(TRUE);
599 }