Add support for VDPAU video out, including hardware decoding.
[mplayer/glamo.git] / libmpcodecs / vd_xanim.c
blob05f8945bbb0a437a8eb5fa78e4af56f22b558314
1 /*
2 XAnim Video Codec DLL support
4 It partly emulates the Xanim codebase.
5 You need the -rdynamic flag to use this with gcc.
7 (C) 2001-2002 Alex Beregszaszi
8 and Arpad Gereoffy <arpi@thot.banki.hu>
9 */
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h> /* strerror */
15 #include "config.h"
17 #include "mp_msg.h"
19 #include "vd_internal.h"
21 static vd_info_t info = {
22 "XAnim codecs",
23 "xanim",
24 "A'rpi & Alex",
25 "Xanim (http://xanim.va.pubnix.com/)",
26 "binary codec plugins"
29 LIBVD_EXTERN(xanim)
31 #ifdef __FreeBSD__
32 #include <unistd.h>
33 #endif
35 #include <dlfcn.h> /* dlsym, dlopen, dlclose */
36 #include <stdarg.h> /* va_alist, va_start, va_end */
37 #include <errno.h> /* strerror, errno */
39 #include "mp_msg.h"
40 #include "mpbswap.h"
42 #include "osdep/timer.h"
44 #if 0
45 /* this should be removed */
46 #ifndef RTLD_NOW
47 #define RLTD_NOW 2
48 #endif
49 #ifndef RTLD_LAZY
50 #define RLTD_LAZY 1
51 #endif
52 #ifndef RTLD_GLOBAL
53 #define RLTD_GLOBAL 256
54 #endif
55 #endif
57 typedef struct
59 unsigned int what;
60 unsigned int id;
61 int (*iq_func)(); /* init/query function */
62 unsigned int (*dec_func)(); /* opt decode function */
63 } XAVID_FUNC_HDR;
65 #define XAVID_WHAT_NO_MORE 0x0000
66 #define XAVID_AVI_QUERY 0x0001
67 #define XAVID_QT_QUERY 0x0002
68 #define XAVID_DEC_FUNC 0x0100
70 #define XAVID_API_REV 0x0003
72 typedef struct
74 unsigned int api_rev;
75 char *desc;
76 char *rev;
77 char *copyright;
78 char *mod_author;
79 char *authors;
80 unsigned int num_funcs;
81 XAVID_FUNC_HDR *funcs;
82 } XAVID_MOD_HDR;
84 /* XA CODEC .. */
85 typedef struct
87 void *anim_hdr;
88 unsigned int compression;
89 unsigned int x, y;
90 unsigned int depth;
91 void *extra;
92 unsigned int xapi_rev;
93 unsigned int (*decoder)();
94 char *description;
95 unsigned int avi_ctab_flag;
96 unsigned int (*avi_read_ext)();
97 } XA_CODEC_HDR;
99 #define CODEC_SUPPORTED 1
100 #define CODEC_UNKNOWN 0
101 #define CODEC_UNSUPPORTED -1
103 /* fuckin colormap structures for xanim */
104 typedef struct
106 unsigned short red;
107 unsigned short green;
108 unsigned short blue;
109 unsigned short gray;
110 } ColorReg;
112 typedef struct XA_ACTION_STRUCT
114 int type;
115 int cmap_rev;
116 unsigned char *data;
117 struct XA_ACTION_STRUCT *next;
118 struct XA_CHDR_STRUCT *chdr;
119 ColorReg *h_cmap;
120 unsigned int *map;
121 struct XA_ACTION_STRUCT *next_same_chdr;
122 } XA_ACTION;
124 typedef struct XA_CHDR_STRUCT
126 unsigned int rev;
127 ColorReg *cmap;
128 unsigned int csize, coff;
129 unsigned int *map;
130 unsigned int msize, moff;
131 struct XA_CHDR_STRUCT *next;
132 XA_ACTION *acts;
133 struct XA_CHDR_STRUCT *new_chdr;
134 } XA_CHDR;
136 typedef struct
138 unsigned int cmd;
139 unsigned int skip_flag;
140 unsigned int imagex, imagey; /* image buffer size */
141 unsigned int imaged; /* image depth */
142 XA_CHDR *chdr; /* color map header */
143 unsigned int map_flag;
144 unsigned int *map;
145 unsigned int xs, ys;
146 unsigned int xe, ye;
147 unsigned int special;
148 void *extra;
149 } XA_DEC_INFO;
151 typedef struct
153 unsigned int file_num;
154 unsigned int anim_type;
155 unsigned int imagex;
156 unsigned int imagey;
157 unsigned int imagec;
158 unsigned int imaged;
159 } XA_ANIM_HDR;
161 typedef struct {
162 XA_DEC_INFO *decinfo;
163 void *file_handler;
164 long (*iq_func)(XA_CODEC_HDR *codec_hdr);
165 unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
166 unsigned int dsize, XA_DEC_INFO *dec_info);
167 mp_image_t *mpi;
168 } vd_xanim_ctx;
170 #if 0
171 typedef char xaBYTE;
172 typedef short xaSHORT;
173 typedef int xaLONG;
175 typedef unsigned char xaUBYTE;
176 typedef unsigned short xaUSHORT;
177 typedef unsigned int xaULONG;
178 #endif
180 #define xaFALSE 0
181 #define xaTRUE 1
183 #define ACT_DLTA_NORM 0x00000000
184 #define ACT_DLTA_BODY 0x00000001
185 #define ACT_DLTA_XOR 0x00000002
186 #define ACT_DLTA_NOP 0x00000004
187 #define ACT_DLTA_MAPD 0x00000008
188 #define ACT_DLTA_DROP 0x00000010
189 #define ACT_DLTA_BAD 0x80000000
191 #define XA_CLOSE_FUNCS 5
192 int xa_close_funcs = 0;
193 void *xa_close_func[XA_CLOSE_FUNCS];
195 /* load, init and query */
196 static int xacodec_load(sh_video_t *sh, char *filename)
198 vd_xanim_ctx *priv = sh->context;
199 void *(*what_the)();
200 char *error;
201 XAVID_MOD_HDR *mod_hdr;
202 XAVID_FUNC_HDR *func;
203 int i;
205 // priv->file_handler = dlopen(filename, RTLD_NOW|RTLD_GLOBAL);
206 priv->file_handler = dlopen(filename, RTLD_LAZY);
207 if (!priv->file_handler)
209 error = dlerror();
210 if (error)
211 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s while %s\n", filename, error);
212 else
213 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s\n", filename);
214 return 0;
217 what_the = dlsym(priv->file_handler, "What_The");
218 if ((error = dlerror()) != NULL)
220 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to init %s while %s\n", filename, error);
221 dlclose(priv->file_handler);
222 return 0;
225 mod_hdr = what_the();
226 if (!mod_hdr)
228 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: initializer function failed in %s\n", filename);
229 dlclose(priv->file_handler);
230 return 0;
233 mp_msg(MSGT_DECVIDEO, MSGL_V, "=== XAnim Codec ===\n");
234 mp_msg(MSGT_DECVIDEO, MSGL_V, " Filename: %s (API revision: %x)\n", filename, mod_hdr->api_rev);
235 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec: %s. Rev: %s\n", mod_hdr->desc, mod_hdr->rev);
236 if (mod_hdr->copyright)
237 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s\n", mod_hdr->copyright);
238 if (mod_hdr->mod_author)
239 mp_msg(MSGT_DECVIDEO, MSGL_V, " Module Author(s): %s\n", mod_hdr->mod_author);
240 if (mod_hdr->authors)
241 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec Author(s): %s\n", mod_hdr->authors);
243 if (mod_hdr->api_rev > XAVID_API_REV)
245 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported api revision (%d) in %s\n",
246 mod_hdr->api_rev, filename);
247 dlclose(priv->file_handler);
248 return 0;
251 func = mod_hdr->funcs;
252 if (!func)
254 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: function table error in %s\n", filename);
255 dlclose(priv->file_handler);
256 return 0;
259 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Exported functions by codec: [functable: %p entries: %d]\n",
260 mod_hdr->funcs, mod_hdr->num_funcs);
261 for (i = 0; i < (int)mod_hdr->num_funcs; i++)
263 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %d: %d %d [iq:%p d:%p]\n",
264 i, func[i].what, func[i].id, func[i].iq_func, func[i].dec_func);
265 if (func[i].what & XAVID_AVI_QUERY)
267 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: avi init/query func (id: %d)\n",
268 func[i].iq_func, func[i].id);
269 priv->iq_func = (void *)func[i].iq_func;
271 if (func[i].what & XAVID_QT_QUERY)
273 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: qt init/query func (id: %d)\n",
274 func[i].iq_func, func[i].id);
275 priv->iq_func = (void *)func[i].iq_func;
277 if (func[i].what & XAVID_DEC_FUNC)
279 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: decoder func (init/query: %p) (id: %d)\n",
280 func[i].dec_func, func[i].iq_func, func[i].id);
281 priv->dec_func = (void *)func[i].dec_func;
284 return 1;
287 static int xacodec_query(sh_video_t *sh, XA_CODEC_HDR *codec_hdr)
289 vd_xanim_ctx *priv = sh->context;
290 long ret;
292 #if 0
293 /* the brute one */
294 if (priv->dec_func)
296 codec_hdr->decoder = priv->dec_func;
297 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "We got decoder's address at init! %p\n", codec_hdr->decoder);
298 return 1;
300 #endif
302 ret = priv->iq_func(codec_hdr);
303 switch(ret)
305 case CODEC_SUPPORTED:
306 priv->dec_func = (void *)codec_hdr->decoder;
307 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Codec is supported: found decoder for %s at %p\n",
308 codec_hdr->description, codec_hdr->decoder);
309 return 1;
310 case CODEC_UNSUPPORTED:
311 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unsupported by dll\n",
312 codec_hdr->description);
313 return 0;
314 case CODEC_UNKNOWN:
315 default:
316 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unknown by dll\n",
317 codec_hdr->description);
318 return 0;
322 void XA_Print(char *fmt, ...)
324 va_list vallist;
325 char buf[1024];
327 va_start(vallist, fmt);
328 vsnprintf(buf, 1024, fmt, vallist);
329 mp_msg(MSGT_XACODEC, MSGL_DBG2, "[xacodec] %s\n", buf);
330 va_end(vallist);
332 return;
335 /* 0 is no debug (needed by 3ivX) */
336 long xa_debug = 0;
338 void TheEnd1(char *err_mess)
340 XA_Print("error: %s - exiting\n", err_mess);
341 /* we should exit here... */
343 return;
346 void XA_Add_Func_To_Free_Chain(XA_ANIM_HDR *anim_hdr, void (*function)())
348 // XA_Print("XA_Add_Func_To_Free_Chain('anim_hdr: %08x', 'function: %08x')",
349 // anim_hdr, function);
350 xa_close_func[xa_close_funcs] = function;
351 if (xa_close_funcs+1 < XA_CLOSE_FUNCS)
352 xa_close_funcs++;
354 return;
358 unsigned long XA_Time_Read(void)
360 return GetTimer(); //(GetRelativeTime());
363 void XA_dummy(void)
365 XA_Print("dummy() called");
368 void XA_Gen_YUV_Tabs(XA_ANIM_HDR *anim_hdr)
370 XA_Print("XA_Gen_YUV_Tabs('anim_hdr: %08x')", anim_hdr);
371 return;
374 void JPG_Setup_Samp_Limit_Table(XA_ANIM_HDR *anim_hdr)
376 XA_Print("JPG_Setup_Samp_Limit_Table('anim_hdr: %08x')", anim_hdr);
377 return;
380 void JPG_Alloc_MCU_Bufs(XA_ANIM_HDR *anim_hdr, unsigned int width,
381 unsigned int height, unsigned int full_flag)
383 XA_Print("JPG_Alloc_MCU_Bufs('anim_hdr: %08x', 'width: %d', 'height: %d', 'full_flag: %d')",
384 anim_hdr, width, height, full_flag);
385 return;
388 /* --------------- 4x4 pixel YUV block fillers [CVID] ----------------- */
390 typedef struct
392 unsigned char r0, g0, b0;
393 unsigned char r1, g1, b1;
394 unsigned char r2, g2, b2;
395 unsigned char r3, g3, b3;
396 unsigned int clr0_0, clr0_1, clr0_2, clr0_3;
397 unsigned int clr1_0, clr1_1, clr1_2, clr1_3;
398 unsigned int clr2_0, clr2_1, clr2_2, clr2_3;
399 unsigned int clr3_0, clr3_1, clr3_2, clr3_3;
400 } XA_2x2_Color;
402 #define SET_4_YUV_PIXELS(image,x,y,cmap2x2) \
403 image->planes[0][((x)+0)+((y)+0)*image->stride[0]]=cmap2x2->clr0_0;\
404 image->planes[0][((x)+1)+((y)+0)*image->stride[0]]=cmap2x2->clr0_1;\
405 image->planes[0][((x)+0)+((y)+1)*image->stride[0]]=cmap2x2->clr0_2;\
406 image->planes[0][((x)+1)+((y)+1)*image->stride[0]]=cmap2x2->clr0_3;\
407 image->planes[1][((x)>>1)+((y)>>1)*image->stride[1]]=cmap2x2->clr1_0;\
408 image->planes[2][((x)>>1)+((y)>>1)*image->stride[2]]=cmap2x2->clr1_1;
410 void XA_2x2_OUT_1BLK_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
411 unsigned int imagex, XA_2x2_Color *cmap2x2)
413 mp_image_t *mpi = (mp_image_t *)image_p;
415 #if 0
416 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
417 #else
418 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
419 SET_4_YUV_PIXELS(mpi,x+2,y,cmap2x2)
420 SET_4_YUV_PIXELS(mpi,x,y+2,cmap2x2)
421 SET_4_YUV_PIXELS(mpi,x+2,y+2,cmap2x2)
422 #endif
424 return;
427 void XA_2x2_OUT_4BLKS_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
428 unsigned int imagex, XA_2x2_Color *cm0, XA_2x2_Color *cm1, XA_2x2_Color *cm2,
429 XA_2x2_Color *cm3)
431 mp_image_t *mpi = (mp_image_t *)image_p;
433 SET_4_YUV_PIXELS(mpi,x,y,cm0)
434 SET_4_YUV_PIXELS(mpi,x+2,y,cm1)
435 SET_4_YUV_PIXELS(mpi,x,y+2,cm2)
436 SET_4_YUV_PIXELS(mpi,x+2,y+2,cm3)
437 return;
440 void *YUV2x2_Blk_Func(unsigned int image_type, int blks, unsigned int dith_flag)
442 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Blk_Func(image_type=%d, blks=%d, dith_flag=%d)\n",
443 image_type, blks, dith_flag);
444 switch(blks){
445 case 1:
446 return (void*) XA_2x2_OUT_1BLK_Convert;
447 case 4:
448 return (void*) XA_2x2_OUT_4BLKS_Convert;
451 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Unimplemented: YUV2x2_Blk_Func(image_type=%d blks=%d dith=%d)\n",image_type,blks,dith_flag);
452 return (void*) XA_dummy;
455 // Take Four Y's and UV and put them into a 2x2 Color structure.
457 void XA_YUV_2x2_clr(XA_2x2_Color *cmap2x2, unsigned int Y0, unsigned int Y1,
458 unsigned int Y2, unsigned int Y3, unsigned int U, unsigned int V,
459 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
462 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV_2x2_clr(%p [%d,%d,%d,%d][%d][%d] %d %p %p)\n",
463 cmap2x2,Y0,Y1,Y2,Y3,U,V,map_flag,map,chdr);
465 cmap2x2->clr0_0=Y0;
466 cmap2x2->clr0_1=Y1;
467 cmap2x2->clr0_2=Y2;
468 cmap2x2->clr0_3=Y3;
469 cmap2x2->clr1_0=U;
470 cmap2x2->clr1_1=V;
471 return;
474 void *YUV2x2_Map_Func(unsigned int image_type, unsigned int dith_type)
476 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Map_Func('image_type: %d', 'dith_type: %d')",
477 image_type, dith_type);
478 return (void*)XA_YUV_2x2_clr;
481 /* -------------------- whole YUV frame converters ------------------------- */
483 typedef struct
485 unsigned char *Ybuf;
486 unsigned char *Ubuf;
487 unsigned char *Vbuf;
488 unsigned char *the_buf;
489 unsigned int the_buf_size;
490 unsigned short y_w, y_h;
491 unsigned short uv_w, uv_h;
492 } YUVBufs;
494 typedef struct
496 unsigned long Uskip_mask;
497 long *YUV_Y_tab;
498 long *YUV_UB_tab;
499 long *YUV_VR_tab;
500 long *YUV_UG_tab;
501 long *YUV_VG_tab;
502 } YUVTabs;
504 YUVBufs jpg_YUVBufs;
505 YUVTabs def_yuv_tabs;
507 /* -------------- YUV 4x4 1x1 1x1 (4:1:0 aka YVU9) [Indeo 3,4,5] ------------------ */
509 void XA_YUV1611_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
510 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs,
511 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
513 sh_video_t *sh = (sh_video_t*)image_p;
514 vd_xanim_ctx *priv = sh->context;
515 mp_image_t *mpi;
516 int y;
517 int ystride=(yuv->y_w)?yuv->y_w:imagex;
518 int uvstride=(yuv->uv_w)?yuv->uv_w:(imagex/4);
520 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUVTabs: %d %p %p %p %p %p\n",yuv_tabs->Uskip_mask,
521 yuv_tabs->YUV_Y_tab,
522 yuv_tabs->YUV_UB_tab,
523 yuv_tabs->YUV_VR_tab,
524 yuv_tabs->YUV_UG_tab,
525 yuv_tabs->YUV_VG_tab );
527 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV1611_Convert('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')",
528 image_p, imagex, imagey, i_x, i_y, yuv, yuv_tabs, map_flag, map, chdr);
530 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%d) %dx%d %dx%d\n",
531 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
532 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
534 if(!yuv_tabs->YUV_Y_tab){
535 // standard YVU9 - simply export it!
536 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0,
537 sh->disp_w, sh->disp_h);
538 priv->mpi=mpi; if(!mpi) return; // ERROR!
539 mpi->planes[0]=yuv->Ybuf;
540 mpi->planes[1]=yuv->Ubuf;
541 mpi->planes[2]=yuv->Vbuf;
542 mpi->width=imagex;
543 mpi->stride[0]=ystride; //i_x; // yuv->y_w
544 mpi->stride[1]=mpi->stride[2]=uvstride; //i_x/4; // yuv->uv_w
545 return;
548 // allocate TEMP buffer and convert the image:
549 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
550 sh->disp_w, sh->disp_h);
551 priv->mpi=mpi; if(!mpi) return; // ERROR!
553 // convert the Y plane:
554 for(y=0;y<(int)imagey;y++){
555 unsigned int x;
556 unsigned char* s=yuv->Ybuf+ystride*y;
557 unsigned char* d=mpi->planes[0]+mpi->stride[0]*y;
558 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
561 imagex>>=2;
562 imagey>>=2;
564 // convert the U plane:
565 for(y=0;y<(int)imagey;y++){
566 unsigned int x;
567 unsigned char* s=yuv->Ubuf+uvstride*y;
568 unsigned char* d=mpi->planes[1]+mpi->stride[1]*y;
569 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
572 // convert the V plane:
573 for(y=0;y<(int)imagey;y++){
574 unsigned int x;
575 unsigned char* s=yuv->Vbuf+uvstride*y;
576 unsigned char* d=mpi->planes[2]+mpi->stride[2]*y;
577 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
581 void *XA_YUV1611_Func(unsigned int image_type)
583 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV1611_Func('image_type: %d')", image_type);
584 return (void *)XA_YUV1611_Convert;
587 /* --------------- YUV 2x2 1x1 1x1 (4:2:0 aka YV12) [3ivX,H263] ------------ */
589 void XA_YUV221111_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
590 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs, unsigned int map_flag,
591 unsigned int *map, XA_CHDR *chdr)
593 sh_video_t *sh = (sh_video_t*)image_p;
594 vd_xanim_ctx *priv = sh->context;
595 mp_image_t *mpi;
596 // note: 3ivX codec doesn't set y_w, uv_w, they are random junk :(
597 int ystride=imagex; //(yuv->y_w)?yuv->y_w:imagex;
598 int uvstride=imagex/2; //(yuv->uv_w)?yuv->uv_w:(imagex/2);
600 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV221111_Convert(%p %dx%d %d;%d [%dx%d] %p %p %d %p %p)\n",
601 image_p,imagex,imagey,i_x,i_y, sh->disp_w, sh->disp_h,
602 yuv,yuv_tabs,map_flag,map,chdr);
604 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%X) %Xx%X %Xx%X\n",
605 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
606 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
608 // standard YV12 - simply export it!
609 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, sh->disp_w, sh->disp_h);
610 priv->mpi=mpi; if(!mpi) return; // ERROR!
611 mpi->planes[0]=yuv->Ybuf;
612 mpi->planes[1]=yuv->Ubuf;
613 mpi->planes[2]=yuv->Vbuf;
614 mpi->width=imagex;
615 mpi->stride[0]=ystride; //i_x; // yuv->y_w
616 mpi->stride[1]=mpi->stride[2]=uvstride; //=i_x/4; // yuv->uv_w
619 void *XA_YUV221111_Func(unsigned int image_type)
621 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV221111_Func('image_type: %d')\n",image_type);
622 return (void *)XA_YUV221111_Convert;
625 /* *** EOF XANIM *** */
627 // to set/get/query special features/parameters
628 static int control(sh_video_t *sh,int cmd,void* arg,...){
629 return CONTROL_UNKNOWN;
632 // init driver
633 static int init(sh_video_t *sh)
635 vd_xanim_ctx *priv;
636 char *def_path = XACODEC_PATH;
637 char dll[1024];
638 XA_CODEC_HDR codec_hdr;
639 int i;
641 priv = malloc(sizeof(vd_xanim_ctx));
642 if (!priv)
643 return 0;
644 sh->context = priv;
645 memset(priv, 0, sizeof(vd_xanim_ctx));
647 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12)) return 0;
649 priv->iq_func = NULL;
650 priv->dec_func = NULL;
652 for (i=0; i < XA_CLOSE_FUNCS; i++)
653 xa_close_func[i] = NULL;
655 if (getenv("XANIM_MOD_DIR"))
656 def_path = getenv("XANIM_MOD_DIR");
658 snprintf(dll, 1024, "%s/%s", def_path, sh->codec->dll);
659 if (xacodec_load(sh, dll) == 0)
660 return 0;
662 codec_hdr.xapi_rev = XAVID_API_REV;
663 codec_hdr.anim_hdr = malloc(4096);
664 codec_hdr.description = sh->codec->info;
665 codec_hdr.compression = bswap_32(sh->bih->biCompression);
666 codec_hdr.decoder = NULL;
667 codec_hdr.x = sh->bih->biWidth; /* ->disp_w */
668 codec_hdr.y = sh->bih->biHeight; /* ->disp_h */
669 /* extra fields to store palette */
670 codec_hdr.avi_ctab_flag = 0;
671 codec_hdr.avi_read_ext = NULL;
672 codec_hdr.extra = NULL;
674 switch(sh->codec->outfmt[sh->outfmtidx])
676 case IMGFMT_BGR32:
677 codec_hdr.depth = 32;
678 break;
679 case IMGFMT_BGR24:
680 codec_hdr.depth = 24;
681 break;
682 case IMGFMT_IYUV:
683 case IMGFMT_I420:
684 case IMGFMT_YV12:
685 codec_hdr.depth = 12;
686 break;
687 case IMGFMT_YVU9:
688 codec_hdr.depth = 9;
689 break;
690 default:
691 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported image out format (%s)\n",
692 vo_format_name(sh->codec->outfmt[sh->outfmtidx]));
693 return 0;
695 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "xacodec: querying for input %dx%d %dbit [fourcc: %4x] (%s)...\n",
696 codec_hdr.x, codec_hdr.y, codec_hdr.depth, codec_hdr.compression, codec_hdr.description);
698 if (xacodec_query(sh, &codec_hdr) == 0)
699 return 0;
701 // free(codec_hdr.anim_hdr);
703 priv->decinfo = malloc(sizeof(XA_DEC_INFO));
704 if (priv->decinfo == NULL)
706 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: memory allocation error: %s\n",
707 strerror(errno));
708 return 0;
710 priv->decinfo->cmd = 0;
711 priv->decinfo->skip_flag = 0;
712 priv->decinfo->imagex = priv->decinfo->xe = codec_hdr.x;
713 priv->decinfo->imagey = priv->decinfo->ye = codec_hdr.y;
714 priv->decinfo->imaged = codec_hdr.depth;
715 priv->decinfo->chdr = NULL;
716 priv->decinfo->map_flag = 0; /* xaFALSE */
717 priv->decinfo->map = NULL;
718 priv->decinfo->xs = priv->decinfo->ys = 0;
719 priv->decinfo->special = 0;
720 priv->decinfo->extra = codec_hdr.extra;
721 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "decinfo->extra, filled by codec: %p [%s]\n",
722 &priv->decinfo->extra, (char *)priv->decinfo->extra);
724 return 1;
727 // uninit driver
728 static void uninit(sh_video_t *sh)
730 vd_xanim_ctx *priv = sh->context;
731 int i;
732 void (*close_func)();
734 for (i=0; i < XA_CLOSE_FUNCS; i++)
735 if (xa_close_func[i])
737 close_func = xa_close_func[i];
738 close_func();
740 dlclose(priv->file_handler);
741 if (priv->decinfo != NULL)
742 free(priv->decinfo);
743 free(priv);
746 // unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
747 // unsigned int dsize, XA_DEC_INFO *dec_info);
749 // decode a frame
750 static mp_image_t* decode(sh_video_t *sh, void *data, int len, int flags)
752 vd_xanim_ctx *priv = sh->context;
753 unsigned int ret;
755 if (len <= 0)
756 return NULL; // skipped frame
758 priv->decinfo->skip_flag = (flags&3)?1:0;
760 if(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_STATIC){
761 // allocate static buffer for cvid-like codecs:
762 priv->mpi = mpcodecs_get_image(sh, MP_IMGTYPE_STATIC,
763 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
764 (sh->disp_w+3)&(~3), (sh->disp_h+3)&(~3));
765 if (!priv->mpi) return NULL;
766 ret = priv->dec_func((uint8_t*)priv->mpi, data, len, priv->decinfo);
767 } else {
768 // left the buffer allocation to the codecs, pass sh_video && priv
769 priv->mpi=NULL;
770 ret = priv->dec_func((uint8_t*)sh, data, len, priv->decinfo);
773 if (ret == ACT_DLTA_NORM)
774 return priv->mpi;
776 if (ret & ACT_DLTA_MAPD)
777 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "mapd\n");
779 if (!(ret & ACT_DLT_MAPD))
780 xacodec_driver->decinfo->map_flag = 0;
781 else
783 xacodec_driver->decinfo->map_flag = 1;
784 xacodec_driver->decinfo->map = ...
788 if (ret & ACT_DLTA_XOR)
790 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "xor\n");
791 return priv->mpi;
794 /* nothing changed */
795 if (ret & ACT_DLTA_NOP)
797 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "nop\n");
798 return NULL;
801 /* frame dropped (also display latest frame) */
802 if (ret & ACT_DLTA_DROP)
804 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "drop\n");
805 return NULL;
808 if (ret & ACT_DLTA_BAD)
810 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "bad\n");
811 return NULL;
814 /* used for double buffer */
815 if (ret & ACT_DLTA_BODY)
817 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "body\n");
818 return NULL;
821 return priv->mpi;