Merge svn changes up to r30907
[mplayer/glamo.git] / libmpcodecs / vd_xanim.c
blobc756c8b8b781b47fe1b94b0783241a1318ebe13a
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 * Copyright (C) 2001-2002 Alex Beregszaszi
8 * Arpad Gereoffy <arpi@thot.banki.hu>
10 * This file is part of MPlayer.
12 * MPlayer is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * MPlayer is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h> /* strerror */
31 #include "config.h"
33 #include "mp_msg.h"
35 #include "vd_internal.h"
37 static const vd_info_t info = {
38 "XAnim codecs",
39 "xanim",
40 "A'rpi & Alex",
41 "Xanim (http://xanim.va.pubnix.com/)",
42 "binary codec plugins"
45 LIBVD_EXTERN(xanim)
47 #ifdef __FreeBSD__
48 #include <unistd.h>
49 #endif
51 #include <dlfcn.h> /* dlsym, dlopen, dlclose */
52 #include <stdarg.h> /* va_alist, va_start, va_end */
53 #include <errno.h> /* strerror, errno */
55 #include "mp_msg.h"
56 #include "mpbswap.h"
58 #include "osdep/timer.h"
60 #if 0
61 /* this should be removed */
62 #ifndef RTLD_NOW
63 #define RLTD_NOW 2
64 #endif
65 #ifndef RTLD_LAZY
66 #define RLTD_LAZY 1
67 #endif
68 #ifndef RTLD_GLOBAL
69 #define RLTD_GLOBAL 256
70 #endif
71 #endif
73 typedef struct
75 unsigned int what;
76 unsigned int id;
77 int (*iq_func)(); /* init/query function */
78 unsigned int (*dec_func)(); /* opt decode function */
79 } XAVID_FUNC_HDR;
81 #define XAVID_WHAT_NO_MORE 0x0000
82 #define XAVID_AVI_QUERY 0x0001
83 #define XAVID_QT_QUERY 0x0002
84 #define XAVID_DEC_FUNC 0x0100
86 #define XAVID_API_REV 0x0003
88 typedef struct
90 unsigned int api_rev;
91 char *desc;
92 char *rev;
93 char *copyright;
94 char *mod_author;
95 char *authors;
96 unsigned int num_funcs;
97 XAVID_FUNC_HDR *funcs;
98 } XAVID_MOD_HDR;
100 /* XA CODEC .. */
101 typedef struct
103 void *anim_hdr;
104 unsigned int compression;
105 unsigned int x, y;
106 unsigned int depth;
107 void *extra;
108 unsigned int xapi_rev;
109 unsigned int (*decoder)();
110 char *description;
111 unsigned int avi_ctab_flag;
112 unsigned int (*avi_read_ext)();
113 } XA_CODEC_HDR;
115 #define CODEC_SUPPORTED 1
116 #define CODEC_UNKNOWN 0
117 #define CODEC_UNSUPPORTED -1
119 /* fuckin colormap structures for xanim */
120 typedef struct
122 unsigned short red;
123 unsigned short green;
124 unsigned short blue;
125 unsigned short gray;
126 } ColorReg;
128 typedef struct XA_ACTION_STRUCT
130 int type;
131 int cmap_rev;
132 unsigned char *data;
133 struct XA_ACTION_STRUCT *next;
134 struct XA_CHDR_STRUCT *chdr;
135 ColorReg *h_cmap;
136 unsigned int *map;
137 struct XA_ACTION_STRUCT *next_same_chdr;
138 } XA_ACTION;
140 typedef struct XA_CHDR_STRUCT
142 unsigned int rev;
143 ColorReg *cmap;
144 unsigned int csize, coff;
145 unsigned int *map;
146 unsigned int msize, moff;
147 struct XA_CHDR_STRUCT *next;
148 XA_ACTION *acts;
149 struct XA_CHDR_STRUCT *new_chdr;
150 } XA_CHDR;
152 typedef struct
154 unsigned int cmd;
155 unsigned int skip_flag;
156 unsigned int imagex, imagey; /* image buffer size */
157 unsigned int imaged; /* image depth */
158 XA_CHDR *chdr; /* color map header */
159 unsigned int map_flag;
160 unsigned int *map;
161 unsigned int xs, ys;
162 unsigned int xe, ye;
163 unsigned int special;
164 void *extra;
165 } XA_DEC_INFO;
167 typedef struct
169 unsigned int file_num;
170 unsigned int anim_type;
171 unsigned int imagex;
172 unsigned int imagey;
173 unsigned int imagec;
174 unsigned int imaged;
175 } XA_ANIM_HDR;
177 typedef struct {
178 XA_DEC_INFO *decinfo;
179 void *file_handler;
180 long (*iq_func)(XA_CODEC_HDR *codec_hdr);
181 unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
182 unsigned int dsize, XA_DEC_INFO *dec_info);
183 mp_image_t *mpi;
184 } vd_xanim_ctx;
186 #if 0
187 typedef char xaBYTE;
188 typedef short xaSHORT;
189 typedef int xaLONG;
191 typedef unsigned char xaUBYTE;
192 typedef unsigned short xaUSHORT;
193 typedef unsigned int xaULONG;
194 #endif
196 #define xaFALSE 0
197 #define xaTRUE 1
199 #define ACT_DLTA_NORM 0x00000000
200 #define ACT_DLTA_BODY 0x00000001
201 #define ACT_DLTA_XOR 0x00000002
202 #define ACT_DLTA_NOP 0x00000004
203 #define ACT_DLTA_MAPD 0x00000008
204 #define ACT_DLTA_DROP 0x00000010
205 #define ACT_DLTA_BAD 0x80000000
207 #define XA_CLOSE_FUNCS 5
208 int xa_close_funcs = 0;
209 void *xa_close_func[XA_CLOSE_FUNCS];
211 /* load, init and query */
212 static int xacodec_load(sh_video_t *sh, char *filename)
214 vd_xanim_ctx *priv = sh->context;
215 void *(*what_the)();
216 char *error;
217 XAVID_MOD_HDR *mod_hdr;
218 XAVID_FUNC_HDR *func;
219 int i;
221 // priv->file_handler = dlopen(filename, RTLD_NOW|RTLD_GLOBAL);
222 priv->file_handler = dlopen(filename, RTLD_LAZY);
223 if (!priv->file_handler)
225 error = dlerror();
226 if (error)
227 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s while %s\n", filename, error);
228 else
229 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s\n", filename);
230 return 0;
233 what_the = dlsym(priv->file_handler, "What_The");
234 if ((error = dlerror()) != NULL)
236 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to init %s while %s\n", filename, error);
237 dlclose(priv->file_handler);
238 return 0;
241 mod_hdr = what_the();
242 if (!mod_hdr)
244 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: initializer function failed in %s\n", filename);
245 dlclose(priv->file_handler);
246 return 0;
249 mp_msg(MSGT_DECVIDEO, MSGL_V, "=== XAnim Codec ===\n");
250 mp_msg(MSGT_DECVIDEO, MSGL_V, " Filename: %s (API revision: %x)\n", filename, mod_hdr->api_rev);
251 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec: %s. Rev: %s\n", mod_hdr->desc, mod_hdr->rev);
252 if (mod_hdr->copyright)
253 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s\n", mod_hdr->copyright);
254 if (mod_hdr->mod_author)
255 mp_msg(MSGT_DECVIDEO, MSGL_V, " Module Author(s): %s\n", mod_hdr->mod_author);
256 if (mod_hdr->authors)
257 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec Author(s): %s\n", mod_hdr->authors);
259 if (mod_hdr->api_rev > XAVID_API_REV)
261 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported api revision (%d) in %s\n",
262 mod_hdr->api_rev, filename);
263 dlclose(priv->file_handler);
264 return 0;
267 func = mod_hdr->funcs;
268 if (!func)
270 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: function table error in %s\n", filename);
271 dlclose(priv->file_handler);
272 return 0;
275 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Exported functions by codec: [functable: %p entries: %d]\n",
276 mod_hdr->funcs, mod_hdr->num_funcs);
277 for (i = 0; i < (int)mod_hdr->num_funcs; i++)
279 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %d: %d %d [iq:%p d:%p]\n",
280 i, func[i].what, func[i].id, func[i].iq_func, func[i].dec_func);
281 if (func[i].what & XAVID_AVI_QUERY)
283 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: avi init/query func (id: %d)\n",
284 func[i].iq_func, func[i].id);
285 priv->iq_func = (void *)func[i].iq_func;
287 if (func[i].what & XAVID_QT_QUERY)
289 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: qt init/query func (id: %d)\n",
290 func[i].iq_func, func[i].id);
291 priv->iq_func = (void *)func[i].iq_func;
293 if (func[i].what & XAVID_DEC_FUNC)
295 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: decoder func (init/query: %p) (id: %d)\n",
296 func[i].dec_func, func[i].iq_func, func[i].id);
297 priv->dec_func = (void *)func[i].dec_func;
300 return 1;
303 static int xacodec_query(sh_video_t *sh, XA_CODEC_HDR *codec_hdr)
305 vd_xanim_ctx *priv = sh->context;
306 long ret;
308 #if 0
309 /* the brute one */
310 if (priv->dec_func)
312 codec_hdr->decoder = priv->dec_func;
313 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "We got decoder's address at init! %p\n", codec_hdr->decoder);
314 return 1;
316 #endif
318 ret = priv->iq_func(codec_hdr);
319 switch(ret)
321 case CODEC_SUPPORTED:
322 priv->dec_func = (void *)codec_hdr->decoder;
323 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Codec is supported: found decoder for %s at %p\n",
324 codec_hdr->description, codec_hdr->decoder);
325 return 1;
326 case CODEC_UNSUPPORTED:
327 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unsupported by dll\n",
328 codec_hdr->description);
329 return 0;
330 case CODEC_UNKNOWN:
331 default:
332 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unknown by dll\n",
333 codec_hdr->description);
334 return 0;
338 void XA_Print(char *fmt, ...)
340 va_list vallist;
341 char buf[1024];
343 va_start(vallist, fmt);
344 vsnprintf(buf, 1024, fmt, vallist);
345 mp_msg(MSGT_XACODEC, MSGL_DBG2, "[xacodec] %s\n", buf);
346 va_end(vallist);
348 return;
351 /* 0 is no debug (needed by 3ivX) */
352 long xa_debug = 0;
354 void TheEnd1(char *err_mess)
356 XA_Print("error: %s - exiting\n", err_mess);
357 /* we should exit here... */
359 return;
362 void XA_Add_Func_To_Free_Chain(XA_ANIM_HDR *anim_hdr, void (*function)())
364 // XA_Print("XA_Add_Func_To_Free_Chain('anim_hdr: %08x', 'function: %08x')",
365 // anim_hdr, function);
366 xa_close_func[xa_close_funcs] = function;
367 if (xa_close_funcs+1 < XA_CLOSE_FUNCS)
368 xa_close_funcs++;
370 return;
374 unsigned long XA_Time_Read(void)
376 return GetTimer(); //(GetRelativeTime());
379 void XA_dummy(void)
381 XA_Print("dummy() called");
384 void XA_Gen_YUV_Tabs(XA_ANIM_HDR *anim_hdr)
386 XA_Print("XA_Gen_YUV_Tabs('anim_hdr: %08x')", anim_hdr);
387 return;
390 void JPG_Setup_Samp_Limit_Table(XA_ANIM_HDR *anim_hdr)
392 XA_Print("JPG_Setup_Samp_Limit_Table('anim_hdr: %08x')", anim_hdr);
393 return;
396 void JPG_Alloc_MCU_Bufs(XA_ANIM_HDR *anim_hdr, unsigned int width,
397 unsigned int height, unsigned int full_flag)
399 XA_Print("JPG_Alloc_MCU_Bufs('anim_hdr: %08x', 'width: %d', 'height: %d', 'full_flag: %d')",
400 anim_hdr, width, height, full_flag);
401 return;
404 /* --------------- 4x4 pixel YUV block fillers [CVID] ----------------- */
406 typedef struct
408 unsigned char r0, g0, b0;
409 unsigned char r1, g1, b1;
410 unsigned char r2, g2, b2;
411 unsigned char r3, g3, b3;
412 unsigned int clr0_0, clr0_1, clr0_2, clr0_3;
413 unsigned int clr1_0, clr1_1, clr1_2, clr1_3;
414 unsigned int clr2_0, clr2_1, clr2_2, clr2_3;
415 unsigned int clr3_0, clr3_1, clr3_2, clr3_3;
416 } XA_2x2_Color;
418 #define SET_4_YUV_PIXELS(image,x,y,cmap2x2) \
419 image->planes[0][((x)+0)+((y)+0)*image->stride[0]]=cmap2x2->clr0_0;\
420 image->planes[0][((x)+1)+((y)+0)*image->stride[0]]=cmap2x2->clr0_1;\
421 image->planes[0][((x)+0)+((y)+1)*image->stride[0]]=cmap2x2->clr0_2;\
422 image->planes[0][((x)+1)+((y)+1)*image->stride[0]]=cmap2x2->clr0_3;\
423 image->planes[1][((x)>>1)+((y)>>1)*image->stride[1]]=cmap2x2->clr1_0;\
424 image->planes[2][((x)>>1)+((y)>>1)*image->stride[2]]=cmap2x2->clr1_1;
426 void XA_2x2_OUT_1BLK_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
427 unsigned int imagex, XA_2x2_Color *cmap2x2)
429 mp_image_t *mpi = (mp_image_t *)image_p;
431 #if 0
432 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
433 #else
434 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
435 SET_4_YUV_PIXELS(mpi,x+2,y,cmap2x2)
436 SET_4_YUV_PIXELS(mpi,x,y+2,cmap2x2)
437 SET_4_YUV_PIXELS(mpi,x+2,y+2,cmap2x2)
438 #endif
440 return;
443 void XA_2x2_OUT_4BLKS_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
444 unsigned int imagex, XA_2x2_Color *cm0, XA_2x2_Color *cm1, XA_2x2_Color *cm2,
445 XA_2x2_Color *cm3)
447 mp_image_t *mpi = (mp_image_t *)image_p;
449 SET_4_YUV_PIXELS(mpi,x,y,cm0)
450 SET_4_YUV_PIXELS(mpi,x+2,y,cm1)
451 SET_4_YUV_PIXELS(mpi,x,y+2,cm2)
452 SET_4_YUV_PIXELS(mpi,x+2,y+2,cm3)
453 return;
456 void *YUV2x2_Blk_Func(unsigned int image_type, int blks, unsigned int dith_flag)
458 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Blk_Func(image_type=%d, blks=%d, dith_flag=%d)\n",
459 image_type, blks, dith_flag);
460 switch(blks){
461 case 1:
462 return (void*) XA_2x2_OUT_1BLK_Convert;
463 case 4:
464 return (void*) XA_2x2_OUT_4BLKS_Convert;
467 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Unimplemented: YUV2x2_Blk_Func(image_type=%d blks=%d dith=%d)\n",image_type,blks,dith_flag);
468 return (void*) XA_dummy;
471 // Take Four Y's and UV and put them into a 2x2 Color structure.
473 void XA_YUV_2x2_clr(XA_2x2_Color *cmap2x2, unsigned int Y0, unsigned int Y1,
474 unsigned int Y2, unsigned int Y3, unsigned int U, unsigned int V,
475 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
478 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV_2x2_clr(%p [%d,%d,%d,%d][%d][%d] %d %p %p)\n",
479 cmap2x2,Y0,Y1,Y2,Y3,U,V,map_flag,map,chdr);
481 cmap2x2->clr0_0=Y0;
482 cmap2x2->clr0_1=Y1;
483 cmap2x2->clr0_2=Y2;
484 cmap2x2->clr0_3=Y3;
485 cmap2x2->clr1_0=U;
486 cmap2x2->clr1_1=V;
487 return;
490 void *YUV2x2_Map_Func(unsigned int image_type, unsigned int dith_type)
492 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Map_Func('image_type: %d', 'dith_type: %d')",
493 image_type, dith_type);
494 return (void*)XA_YUV_2x2_clr;
497 /* -------------------- whole YUV frame converters ------------------------- */
499 typedef struct
501 unsigned char *Ybuf;
502 unsigned char *Ubuf;
503 unsigned char *Vbuf;
504 unsigned char *the_buf;
505 unsigned int the_buf_size;
506 unsigned short y_w, y_h;
507 unsigned short uv_w, uv_h;
508 } YUVBufs;
510 typedef struct
512 unsigned long Uskip_mask;
513 long *YUV_Y_tab;
514 long *YUV_UB_tab;
515 long *YUV_VR_tab;
516 long *YUV_UG_tab;
517 long *YUV_VG_tab;
518 } YUVTabs;
520 YUVBufs jpg_YUVBufs;
521 YUVTabs def_yuv_tabs;
523 /* -------------- YUV 4x4 1x1 1x1 (4:1:0 aka YVU9) [Indeo 3,4,5] ------------------ */
525 void XA_YUV1611_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
526 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs,
527 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
529 sh_video_t *sh = (sh_video_t*)image_p;
530 vd_xanim_ctx *priv = sh->context;
531 mp_image_t *mpi;
532 int y;
533 int ystride=(yuv->y_w)?yuv->y_w:imagex;
534 int uvstride=(yuv->uv_w)?yuv->uv_w:(imagex/4);
536 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUVTabs: %d %p %p %p %p %p\n",yuv_tabs->Uskip_mask,
537 yuv_tabs->YUV_Y_tab,
538 yuv_tabs->YUV_UB_tab,
539 yuv_tabs->YUV_VR_tab,
540 yuv_tabs->YUV_UG_tab,
541 yuv_tabs->YUV_VG_tab );
543 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')",
544 image_p, imagex, imagey, i_x, i_y, yuv, yuv_tabs, map_flag, map, chdr);
546 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%d) %dx%d %dx%d\n",
547 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
548 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
550 if(!yuv_tabs->YUV_Y_tab){
551 // standard YVU9 - simply export it!
552 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0,
553 sh->disp_w, sh->disp_h);
554 priv->mpi=mpi; if(!mpi) return; // ERROR!
555 mpi->planes[0]=yuv->Ybuf;
556 mpi->planes[1]=yuv->Ubuf;
557 mpi->planes[2]=yuv->Vbuf;
558 mpi->width=imagex;
559 mpi->stride[0]=ystride; //i_x; // yuv->y_w
560 mpi->stride[1]=mpi->stride[2]=uvstride; //i_x/4; // yuv->uv_w
561 return;
564 // allocate TEMP buffer and convert the image:
565 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
566 sh->disp_w, sh->disp_h);
567 priv->mpi=mpi; if(!mpi) return; // ERROR!
569 // convert the Y plane:
570 for(y=0;y<(int)imagey;y++){
571 unsigned int x;
572 unsigned char* s=yuv->Ybuf+ystride*y;
573 unsigned char* d=mpi->planes[0]+mpi->stride[0]*y;
574 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
577 imagex>>=2;
578 imagey>>=2;
580 // convert the U plane:
581 for(y=0;y<(int)imagey;y++){
582 unsigned int x;
583 unsigned char* s=yuv->Ubuf+uvstride*y;
584 unsigned char* d=mpi->planes[1]+mpi->stride[1]*y;
585 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
588 // convert the V plane:
589 for(y=0;y<(int)imagey;y++){
590 unsigned int x;
591 unsigned char* s=yuv->Vbuf+uvstride*y;
592 unsigned char* d=mpi->planes[2]+mpi->stride[2]*y;
593 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
597 void *XA_YUV1611_Func(unsigned int image_type)
599 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV1611_Func('image_type: %d')", image_type);
600 return (void *)XA_YUV1611_Convert;
603 /* --------------- YUV 2x2 1x1 1x1 (4:2:0 aka YV12) [3ivX,H263] ------------ */
605 void XA_YUV221111_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
606 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs, unsigned int map_flag,
607 unsigned int *map, XA_CHDR *chdr)
609 sh_video_t *sh = (sh_video_t*)image_p;
610 vd_xanim_ctx *priv = sh->context;
611 mp_image_t *mpi;
612 // note: 3ivX codec doesn't set y_w, uv_w, they are random junk :(
613 int ystride=imagex; //(yuv->y_w)?yuv->y_w:imagex;
614 int uvstride=imagex/2; //(yuv->uv_w)?yuv->uv_w:(imagex/2);
616 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV221111_Convert(%p %dx%d %d;%d [%dx%d] %p %p %d %p %p)\n",
617 image_p,imagex,imagey,i_x,i_y, sh->disp_w, sh->disp_h,
618 yuv,yuv_tabs,map_flag,map,chdr);
620 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%X) %Xx%X %Xx%X\n",
621 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
622 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
624 // standard YV12 - simply export it!
625 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, sh->disp_w, sh->disp_h);
626 priv->mpi=mpi; if(!mpi) return; // ERROR!
627 mpi->planes[0]=yuv->Ybuf;
628 mpi->planes[1]=yuv->Ubuf;
629 mpi->planes[2]=yuv->Vbuf;
630 mpi->width=imagex;
631 mpi->stride[0]=ystride; //i_x; // yuv->y_w
632 mpi->stride[1]=mpi->stride[2]=uvstride; //=i_x/4; // yuv->uv_w
635 void *XA_YUV221111_Func(unsigned int image_type)
637 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV221111_Func('image_type: %d')\n",image_type);
638 return (void *)XA_YUV221111_Convert;
641 /* *** EOF XANIM *** */
643 // to set/get/query special features/parameters
644 static int control(sh_video_t *sh,int cmd,void* arg,...){
645 return CONTROL_UNKNOWN;
648 // init driver
649 static int init(sh_video_t *sh)
651 vd_xanim_ctx *priv;
652 char *def_path = BINARY_CODECS_PATH;
653 char dll[1024];
654 XA_CODEC_HDR codec_hdr;
655 int i;
657 priv = malloc(sizeof(vd_xanim_ctx));
658 if (!priv)
659 return 0;
660 sh->context = priv;
661 memset(priv, 0, sizeof(vd_xanim_ctx));
663 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12)) return 0;
665 priv->iq_func = NULL;
666 priv->dec_func = NULL;
668 for (i=0; i < XA_CLOSE_FUNCS; i++)
669 xa_close_func[i] = NULL;
671 if (getenv("XANIM_MOD_DIR"))
672 def_path = getenv("XANIM_MOD_DIR");
674 snprintf(dll, 1024, "%s/%s", def_path, sh->codec->dll);
675 if (xacodec_load(sh, dll) == 0)
676 return 0;
678 codec_hdr.xapi_rev = XAVID_API_REV;
679 codec_hdr.anim_hdr = malloc(4096);
680 codec_hdr.description = sh->codec->info;
681 codec_hdr.compression = bswap_32(sh->bih->biCompression);
682 codec_hdr.decoder = NULL;
683 codec_hdr.x = sh->bih->biWidth; /* ->disp_w */
684 codec_hdr.y = sh->bih->biHeight; /* ->disp_h */
685 /* extra fields to store palette */
686 codec_hdr.avi_ctab_flag = 0;
687 codec_hdr.avi_read_ext = NULL;
688 codec_hdr.extra = NULL;
690 switch(sh->codec->outfmt[sh->outfmtidx])
692 case IMGFMT_BGR32:
693 codec_hdr.depth = 32;
694 break;
695 case IMGFMT_BGR24:
696 codec_hdr.depth = 24;
697 break;
698 case IMGFMT_IYUV:
699 case IMGFMT_I420:
700 case IMGFMT_YV12:
701 codec_hdr.depth = 12;
702 break;
703 case IMGFMT_YVU9:
704 codec_hdr.depth = 9;
705 break;
706 default:
707 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported image out format (%s)\n",
708 vo_format_name(sh->codec->outfmt[sh->outfmtidx]));
709 return 0;
711 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "xacodec: querying for input %dx%d %dbit [fourcc: %4x] (%s)...\n",
712 codec_hdr.x, codec_hdr.y, codec_hdr.depth, codec_hdr.compression, codec_hdr.description);
714 if (xacodec_query(sh, &codec_hdr) == 0)
715 return 0;
717 // free(codec_hdr.anim_hdr);
719 priv->decinfo = malloc(sizeof(XA_DEC_INFO));
720 if (priv->decinfo == NULL)
722 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: memory allocation error: %s\n",
723 strerror(errno));
724 return 0;
726 priv->decinfo->cmd = 0;
727 priv->decinfo->skip_flag = 0;
728 priv->decinfo->imagex = priv->decinfo->xe = codec_hdr.x;
729 priv->decinfo->imagey = priv->decinfo->ye = codec_hdr.y;
730 priv->decinfo->imaged = codec_hdr.depth;
731 priv->decinfo->chdr = NULL;
732 priv->decinfo->map_flag = 0; /* xaFALSE */
733 priv->decinfo->map = NULL;
734 priv->decinfo->xs = priv->decinfo->ys = 0;
735 priv->decinfo->special = 0;
736 priv->decinfo->extra = codec_hdr.extra;
737 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "decinfo->extra, filled by codec: %p [%s]\n",
738 &priv->decinfo->extra, (char *)priv->decinfo->extra);
740 return 1;
743 // uninit driver
744 static void uninit(sh_video_t *sh)
746 vd_xanim_ctx *priv = sh->context;
747 int i;
748 void (*close_func)();
750 for (i=0; i < XA_CLOSE_FUNCS; i++)
751 if (xa_close_func[i])
753 close_func = xa_close_func[i];
754 close_func();
756 dlclose(priv->file_handler);
757 if (priv->decinfo != NULL)
758 free(priv->decinfo);
759 free(priv);
762 // unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
763 // unsigned int dsize, XA_DEC_INFO *dec_info);
765 // decode a frame
766 static mp_image_t* decode(sh_video_t *sh, void *data, int len, int flags)
768 vd_xanim_ctx *priv = sh->context;
769 unsigned int ret;
771 if (len <= 0)
772 return NULL; // skipped frame
774 priv->decinfo->skip_flag = (flags&3)?1:0;
776 if(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_STATIC){
777 // allocate static buffer for cvid-like codecs:
778 priv->mpi = mpcodecs_get_image(sh, MP_IMGTYPE_STATIC,
779 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
780 (sh->disp_w+3)&(~3), (sh->disp_h+3)&(~3));
781 if (!priv->mpi) return NULL;
782 ret = priv->dec_func((uint8_t*)priv->mpi, data, len, priv->decinfo);
783 } else {
784 // left the buffer allocation to the codecs, pass sh_video && priv
785 priv->mpi=NULL;
786 ret = priv->dec_func((uint8_t*)sh, data, len, priv->decinfo);
789 if (ret == ACT_DLTA_NORM)
790 return priv->mpi;
792 if (ret & ACT_DLTA_MAPD)
793 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "mapd\n");
795 if (!(ret & ACT_DLT_MAPD))
796 xacodec_driver->decinfo->map_flag = 0;
797 else
799 xacodec_driver->decinfo->map_flag = 1;
800 xacodec_driver->decinfo->map = ...
804 if (ret & ACT_DLTA_XOR)
806 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "xor\n");
807 return priv->mpi;
810 /* nothing changed */
811 if (ret & ACT_DLTA_NOP)
813 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "nop\n");
814 return NULL;
817 /* frame dropped (also display latest frame) */
818 if (ret & ACT_DLTA_DROP)
820 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "drop\n");
821 return NULL;
824 if (ret & ACT_DLTA_BAD)
826 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "bad\n");
827 return NULL;
830 /* used for double buffer */
831 if (ret & ACT_DLTA_BODY)
833 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "body\n");
834 return NULL;
837 return priv->mpi;