sync with en/mplayer.1 rev. 30936
[mplayer/glamo.git] / libmpcodecs / vd_xanim.c
blob6067e5add6573f6e4c36fb3410cdbeae113e99df
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"
36 #include "loader/codecpath.h"
38 static const vd_info_t info = {
39 "XAnim codecs",
40 "xanim",
41 "A'rpi & Alex",
42 "Xanim (http://xanim.va.pubnix.com/)",
43 "binary codec plugins"
46 LIBVD_EXTERN(xanim)
48 #ifdef __FreeBSD__
49 #include <unistd.h>
50 #endif
52 #include <dlfcn.h> /* dlsym, dlopen, dlclose */
53 #include <stdarg.h> /* va_alist, va_start, va_end */
54 #include <errno.h> /* strerror, errno */
56 #include "mp_msg.h"
57 #include "mpbswap.h"
59 #include "osdep/timer.h"
61 #if 0
62 /* this should be removed */
63 #ifndef RTLD_NOW
64 #define RLTD_NOW 2
65 #endif
66 #ifndef RTLD_LAZY
67 #define RLTD_LAZY 1
68 #endif
69 #ifndef RTLD_GLOBAL
70 #define RLTD_GLOBAL 256
71 #endif
72 #endif
74 typedef struct
76 unsigned int what;
77 unsigned int id;
78 int (*iq_func)(); /* init/query function */
79 unsigned int (*dec_func)(); /* opt decode function */
80 } XAVID_FUNC_HDR;
82 #define XAVID_WHAT_NO_MORE 0x0000
83 #define XAVID_AVI_QUERY 0x0001
84 #define XAVID_QT_QUERY 0x0002
85 #define XAVID_DEC_FUNC 0x0100
87 #define XAVID_API_REV 0x0003
89 typedef struct
91 unsigned int api_rev;
92 char *desc;
93 char *rev;
94 char *copyright;
95 char *mod_author;
96 char *authors;
97 unsigned int num_funcs;
98 XAVID_FUNC_HDR *funcs;
99 } XAVID_MOD_HDR;
101 /* XA CODEC .. */
102 typedef struct
104 void *anim_hdr;
105 unsigned int compression;
106 unsigned int x, y;
107 unsigned int depth;
108 void *extra;
109 unsigned int xapi_rev;
110 unsigned int (*decoder)();
111 char *description;
112 unsigned int avi_ctab_flag;
113 unsigned int (*avi_read_ext)();
114 } XA_CODEC_HDR;
116 #define CODEC_SUPPORTED 1
117 #define CODEC_UNKNOWN 0
118 #define CODEC_UNSUPPORTED -1
120 /* fuckin colormap structures for xanim */
121 typedef struct
123 unsigned short red;
124 unsigned short green;
125 unsigned short blue;
126 unsigned short gray;
127 } ColorReg;
129 typedef struct XA_ACTION_STRUCT
131 int type;
132 int cmap_rev;
133 unsigned char *data;
134 struct XA_ACTION_STRUCT *next;
135 struct XA_CHDR_STRUCT *chdr;
136 ColorReg *h_cmap;
137 unsigned int *map;
138 struct XA_ACTION_STRUCT *next_same_chdr;
139 } XA_ACTION;
141 typedef struct XA_CHDR_STRUCT
143 unsigned int rev;
144 ColorReg *cmap;
145 unsigned int csize, coff;
146 unsigned int *map;
147 unsigned int msize, moff;
148 struct XA_CHDR_STRUCT *next;
149 XA_ACTION *acts;
150 struct XA_CHDR_STRUCT *new_chdr;
151 } XA_CHDR;
153 typedef struct
155 unsigned int cmd;
156 unsigned int skip_flag;
157 unsigned int imagex, imagey; /* image buffer size */
158 unsigned int imaged; /* image depth */
159 XA_CHDR *chdr; /* color map header */
160 unsigned int map_flag;
161 unsigned int *map;
162 unsigned int xs, ys;
163 unsigned int xe, ye;
164 unsigned int special;
165 void *extra;
166 } XA_DEC_INFO;
168 typedef struct
170 unsigned int file_num;
171 unsigned int anim_type;
172 unsigned int imagex;
173 unsigned int imagey;
174 unsigned int imagec;
175 unsigned int imaged;
176 } XA_ANIM_HDR;
178 typedef struct {
179 XA_DEC_INFO *decinfo;
180 void *file_handler;
181 long (*iq_func)(XA_CODEC_HDR *codec_hdr);
182 unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
183 unsigned int dsize, XA_DEC_INFO *dec_info);
184 mp_image_t *mpi;
185 } vd_xanim_ctx;
187 #if 0
188 typedef char xaBYTE;
189 typedef short xaSHORT;
190 typedef int xaLONG;
192 typedef unsigned char xaUBYTE;
193 typedef unsigned short xaUSHORT;
194 typedef unsigned int xaULONG;
195 #endif
197 #define xaFALSE 0
198 #define xaTRUE 1
200 #define ACT_DLTA_NORM 0x00000000
201 #define ACT_DLTA_BODY 0x00000001
202 #define ACT_DLTA_XOR 0x00000002
203 #define ACT_DLTA_NOP 0x00000004
204 #define ACT_DLTA_MAPD 0x00000008
205 #define ACT_DLTA_DROP 0x00000010
206 #define ACT_DLTA_BAD 0x80000000
208 #define XA_CLOSE_FUNCS 5
209 int xa_close_funcs = 0;
210 void *xa_close_func[XA_CLOSE_FUNCS];
212 /* load, init and query */
213 static int xacodec_load(sh_video_t *sh, char *filename)
215 vd_xanim_ctx *priv = sh->context;
216 void *(*what_the)();
217 char *error;
218 XAVID_MOD_HDR *mod_hdr;
219 XAVID_FUNC_HDR *func;
220 int i;
222 // priv->file_handler = dlopen(filename, RTLD_NOW|RTLD_GLOBAL);
223 priv->file_handler = dlopen(filename, RTLD_LAZY);
224 if (!priv->file_handler)
226 error = dlerror();
227 if (error)
228 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s while %s\n", filename, error);
229 else
230 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to dlopen %s\n", filename);
231 return 0;
234 what_the = dlsym(priv->file_handler, "What_The");
235 if ((error = dlerror()) != NULL)
237 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: failed to init %s while %s\n", filename, error);
238 dlclose(priv->file_handler);
239 return 0;
242 mod_hdr = what_the();
243 if (!mod_hdr)
245 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: initializer function failed in %s\n", filename);
246 dlclose(priv->file_handler);
247 return 0;
250 mp_msg(MSGT_DECVIDEO, MSGL_V, "=== XAnim Codec ===\n");
251 mp_msg(MSGT_DECVIDEO, MSGL_V, " Filename: %s (API revision: %x)\n", filename, mod_hdr->api_rev);
252 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec: %s. Rev: %s\n", mod_hdr->desc, mod_hdr->rev);
253 if (mod_hdr->copyright)
254 mp_msg(MSGT_DECVIDEO, MSGL_V, " %s\n", mod_hdr->copyright);
255 if (mod_hdr->mod_author)
256 mp_msg(MSGT_DECVIDEO, MSGL_V, " Module Author(s): %s\n", mod_hdr->mod_author);
257 if (mod_hdr->authors)
258 mp_msg(MSGT_DECVIDEO, MSGL_V, " Codec Author(s): %s\n", mod_hdr->authors);
260 if (mod_hdr->api_rev > XAVID_API_REV)
262 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported api revision (%d) in %s\n",
263 mod_hdr->api_rev, filename);
264 dlclose(priv->file_handler);
265 return 0;
268 func = mod_hdr->funcs;
269 if (!func)
271 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: function table error in %s\n", filename);
272 dlclose(priv->file_handler);
273 return 0;
276 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Exported functions by codec: [functable: %p entries: %d]\n",
277 mod_hdr->funcs, mod_hdr->num_funcs);
278 for (i = 0; i < (int)mod_hdr->num_funcs; i++)
280 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %d: %d %d [iq:%p d:%p]\n",
281 i, func[i].what, func[i].id, func[i].iq_func, func[i].dec_func);
282 if (func[i].what & XAVID_AVI_QUERY)
284 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: avi init/query func (id: %d)\n",
285 func[i].iq_func, func[i].id);
286 priv->iq_func = (void *)func[i].iq_func;
288 if (func[i].what & XAVID_QT_QUERY)
290 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: qt init/query func (id: %d)\n",
291 func[i].iq_func, func[i].id);
292 priv->iq_func = (void *)func[i].iq_func;
294 if (func[i].what & XAVID_DEC_FUNC)
296 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, " %p: decoder func (init/query: %p) (id: %d)\n",
297 func[i].dec_func, func[i].iq_func, func[i].id);
298 priv->dec_func = (void *)func[i].dec_func;
301 return 1;
304 static int xacodec_query(sh_video_t *sh, XA_CODEC_HDR *codec_hdr)
306 vd_xanim_ctx *priv = sh->context;
307 long ret;
309 #if 0
310 /* the brute one */
311 if (priv->dec_func)
313 codec_hdr->decoder = priv->dec_func;
314 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "We got decoder's address at init! %p\n", codec_hdr->decoder);
315 return 1;
317 #endif
319 ret = priv->iq_func(codec_hdr);
320 switch(ret)
322 case CODEC_SUPPORTED:
323 priv->dec_func = (void *)codec_hdr->decoder;
324 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "Codec is supported: found decoder for %s at %p\n",
325 codec_hdr->description, codec_hdr->decoder);
326 return 1;
327 case CODEC_UNSUPPORTED:
328 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unsupported by dll\n",
329 codec_hdr->description);
330 return 0;
331 case CODEC_UNKNOWN:
332 default:
333 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "Codec (%s) is unknown by dll\n",
334 codec_hdr->description);
335 return 0;
339 /* These functions are required for loading XAnim binary libs.
340 * Add forward declarations to avoid warnings with -Wmissing-prototypes. */
341 void XA_Print(char *fmt, ...);
342 void TheEnd1(char *err_mess);
343 void XA_Add_Func_To_Free_Chain(XA_ANIM_HDR *anim_hdr, void (*function)());
344 unsigned long XA_Time_Read(void);
345 void XA_Gen_YUV_Tabs(XA_ANIM_HDR *anim_hdr);
346 void JPG_Setup_Samp_Limit_Table(XA_ANIM_HDR *anim_hdr);
347 void JPG_Alloc_MCU_Bufs(XA_ANIM_HDR *anim_hdr, unsigned int width,
348 unsigned int height, unsigned int full_flag);
349 void *YUV2x2_Blk_Func(unsigned int image_type, int blks,
350 unsigned int dith_flag);
351 void *YUV2x2_Map_Func(unsigned int image_type, unsigned int dith_type);
352 void *XA_YUV1611_Func(unsigned int image_type);
353 void *XA_YUV221111_Func(unsigned int image_type);
355 void XA_Print(char *fmt, ...)
357 va_list vallist;
358 char buf[1024];
360 va_start(vallist, fmt);
361 vsnprintf(buf, 1024, fmt, vallist);
362 mp_msg(MSGT_XACODEC, MSGL_DBG2, "[xacodec] %s\n", buf);
363 va_end(vallist);
365 return;
368 /* 0 is no debug (needed by 3ivX) */
369 long xa_debug = 0;
371 void TheEnd1(char *err_mess)
373 XA_Print("error: %s - exiting\n", err_mess);
374 /* we should exit here... */
376 return;
379 void XA_Add_Func_To_Free_Chain(XA_ANIM_HDR *anim_hdr, void (*function)())
381 // XA_Print("XA_Add_Func_To_Free_Chain('anim_hdr: %08x', 'function: %08x')",
382 // anim_hdr, function);
383 xa_close_func[xa_close_funcs] = function;
384 if (xa_close_funcs+1 < XA_CLOSE_FUNCS)
385 xa_close_funcs++;
387 return;
390 unsigned long XA_Time_Read(void)
392 return GetTimer(); //(GetRelativeTime());
395 static void XA_dummy(void)
397 XA_Print("dummy() called");
400 void XA_Gen_YUV_Tabs(XA_ANIM_HDR *anim_hdr)
402 XA_Print("XA_Gen_YUV_Tabs('anim_hdr: %08x')", anim_hdr);
403 return;
406 void JPG_Setup_Samp_Limit_Table(XA_ANIM_HDR *anim_hdr)
408 XA_Print("JPG_Setup_Samp_Limit_Table('anim_hdr: %08x')", anim_hdr);
409 return;
412 void JPG_Alloc_MCU_Bufs(XA_ANIM_HDR *anim_hdr, unsigned int width,
413 unsigned int height, unsigned int full_flag)
415 XA_Print("JPG_Alloc_MCU_Bufs('anim_hdr: %08x', 'width: %d', 'height: %d', 'full_flag: %d')",
416 anim_hdr, width, height, full_flag);
417 return;
420 /* --------------- 4x4 pixel YUV block fillers [CVID] ----------------- */
422 typedef struct
424 unsigned char r0, g0, b0;
425 unsigned char r1, g1, b1;
426 unsigned char r2, g2, b2;
427 unsigned char r3, g3, b3;
428 unsigned int clr0_0, clr0_1, clr0_2, clr0_3;
429 unsigned int clr1_0, clr1_1, clr1_2, clr1_3;
430 unsigned int clr2_0, clr2_1, clr2_2, clr2_3;
431 unsigned int clr3_0, clr3_1, clr3_2, clr3_3;
432 } XA_2x2_Color;
434 #define SET_4_YUV_PIXELS(image,x,y,cmap2x2) \
435 image->planes[0][((x)+0)+((y)+0)*image->stride[0]]=cmap2x2->clr0_0;\
436 image->planes[0][((x)+1)+((y)+0)*image->stride[0]]=cmap2x2->clr0_1;\
437 image->planes[0][((x)+0)+((y)+1)*image->stride[0]]=cmap2x2->clr0_2;\
438 image->planes[0][((x)+1)+((y)+1)*image->stride[0]]=cmap2x2->clr0_3;\
439 image->planes[1][((x)>>1)+((y)>>1)*image->stride[1]]=cmap2x2->clr1_0;\
440 image->planes[2][((x)>>1)+((y)>>1)*image->stride[2]]=cmap2x2->clr1_1;
442 static void XA_2x2_OUT_1BLK_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
443 unsigned int imagex, XA_2x2_Color *cmap2x2)
445 mp_image_t *mpi = (mp_image_t *)image_p;
447 #if 0
448 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
449 #else
450 SET_4_YUV_PIXELS(mpi,x,y,cmap2x2)
451 SET_4_YUV_PIXELS(mpi,x+2,y,cmap2x2)
452 SET_4_YUV_PIXELS(mpi,x,y+2,cmap2x2)
453 SET_4_YUV_PIXELS(mpi,x+2,y+2,cmap2x2)
454 #endif
456 return;
459 static void XA_2x2_OUT_4BLKS_Convert(unsigned char *image_p, unsigned int x, unsigned int y,
460 unsigned int imagex, XA_2x2_Color *cm0, XA_2x2_Color *cm1, XA_2x2_Color *cm2,
461 XA_2x2_Color *cm3)
463 mp_image_t *mpi = (mp_image_t *)image_p;
465 SET_4_YUV_PIXELS(mpi,x,y,cm0)
466 SET_4_YUV_PIXELS(mpi,x+2,y,cm1)
467 SET_4_YUV_PIXELS(mpi,x,y+2,cm2)
468 SET_4_YUV_PIXELS(mpi,x+2,y+2,cm3)
469 return;
472 void *YUV2x2_Blk_Func(unsigned int image_type, int blks, unsigned int dith_flag)
474 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Blk_Func(image_type=%d, blks=%d, dith_flag=%d)\n",
475 image_type, blks, dith_flag);
476 switch(blks){
477 case 1:
478 return (void*) XA_2x2_OUT_1BLK_Convert;
479 case 4:
480 return (void*) XA_2x2_OUT_4BLKS_Convert;
483 mp_msg(MSGT_DECVIDEO,MSGL_WARN,"Unimplemented: YUV2x2_Blk_Func(image_type=%d blks=%d dith=%d)\n",image_type,blks,dith_flag);
484 return (void*) XA_dummy;
487 // Take Four Y's and UV and put them into a 2x2 Color structure.
489 static void XA_YUV_2x2_clr(XA_2x2_Color *cmap2x2, unsigned int Y0, unsigned int Y1,
490 unsigned int Y2, unsigned int Y3, unsigned int U, unsigned int V,
491 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
494 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV_2x2_clr(%p [%d,%d,%d,%d][%d][%d] %d %p %p)\n",
495 cmap2x2,Y0,Y1,Y2,Y3,U,V,map_flag,map,chdr);
497 cmap2x2->clr0_0=Y0;
498 cmap2x2->clr0_1=Y1;
499 cmap2x2->clr0_2=Y2;
500 cmap2x2->clr0_3=Y3;
501 cmap2x2->clr1_0=U;
502 cmap2x2->clr1_1=V;
503 return;
506 void *YUV2x2_Map_Func(unsigned int image_type, unsigned int dith_type)
508 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "YUV2x2_Map_Func('image_type: %d', 'dith_type: %d')",
509 image_type, dith_type);
510 return (void*)XA_YUV_2x2_clr;
513 /* -------------------- whole YUV frame converters ------------------------- */
515 typedef struct
517 unsigned char *Ybuf;
518 unsigned char *Ubuf;
519 unsigned char *Vbuf;
520 unsigned char *the_buf;
521 unsigned int the_buf_size;
522 unsigned short y_w, y_h;
523 unsigned short uv_w, uv_h;
524 } YUVBufs;
526 typedef struct
528 unsigned long Uskip_mask;
529 long *YUV_Y_tab;
530 long *YUV_UB_tab;
531 long *YUV_VR_tab;
532 long *YUV_UG_tab;
533 long *YUV_VG_tab;
534 } YUVTabs;
536 YUVBufs jpg_YUVBufs;
537 YUVTabs def_yuv_tabs;
539 /* -------------- YUV 4x4 1x1 1x1 (4:1:0 aka YVU9) [Indeo 3,4,5] ------------------ */
541 static void XA_YUV1611_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
542 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs,
543 unsigned int map_flag, unsigned int *map, XA_CHDR *chdr)
545 sh_video_t *sh = (sh_video_t*)image_p;
546 vd_xanim_ctx *priv = sh->context;
547 mp_image_t *mpi;
548 int y;
549 int ystride=(yuv->y_w)?yuv->y_w:imagex;
550 int uvstride=(yuv->uv_w)?yuv->uv_w:(imagex/4);
552 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUVTabs: %d %p %p %p %p %p\n",yuv_tabs->Uskip_mask,
553 yuv_tabs->YUV_Y_tab,
554 yuv_tabs->YUV_UB_tab,
555 yuv_tabs->YUV_VR_tab,
556 yuv_tabs->YUV_UG_tab,
557 yuv_tabs->YUV_VG_tab );
559 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')",
560 image_p, imagex, imagey, i_x, i_y, yuv, yuv_tabs, map_flag, map, chdr);
562 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%d) %dx%d %dx%d\n",
563 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
564 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
566 if(!yuv_tabs->YUV_Y_tab){
567 // standard YVU9 - simply export it!
568 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0,
569 sh->disp_w, sh->disp_h);
570 priv->mpi=mpi; if(!mpi) return; // ERROR!
571 mpi->planes[0]=yuv->Ybuf;
572 mpi->planes[1]=yuv->Ubuf;
573 mpi->planes[2]=yuv->Vbuf;
574 mpi->width=imagex;
575 mpi->stride[0]=ystride; //i_x; // yuv->y_w
576 mpi->stride[1]=mpi->stride[2]=uvstride; //i_x/4; // yuv->uv_w
577 return;
580 // allocate TEMP buffer and convert the image:
581 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
582 sh->disp_w, sh->disp_h);
583 priv->mpi=mpi; if(!mpi) return; // ERROR!
585 // convert the Y plane:
586 for(y=0;y<(int)imagey;y++){
587 unsigned int x;
588 unsigned char* s=yuv->Ybuf+ystride*y;
589 unsigned char* d=mpi->planes[0]+mpi->stride[0]*y;
590 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
593 imagex>>=2;
594 imagey>>=2;
596 // convert the U plane:
597 for(y=0;y<(int)imagey;y++){
598 unsigned int x;
599 unsigned char* s=yuv->Ubuf+uvstride*y;
600 unsigned char* d=mpi->planes[1]+mpi->stride[1]*y;
601 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
604 // convert the V plane:
605 for(y=0;y<(int)imagey;y++){
606 unsigned int x;
607 unsigned char* s=yuv->Vbuf+uvstride*y;
608 unsigned char* d=mpi->planes[2]+mpi->stride[2]*y;
609 for(x=0;x<imagex;x++) d[x]=s[x]<<1;
613 void *XA_YUV1611_Func(unsigned int image_type)
615 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV1611_Func('image_type: %d')", image_type);
616 return (void *)XA_YUV1611_Convert;
619 /* --------------- YUV 2x2 1x1 1x1 (4:2:0 aka YV12) [3ivX,H263] ------------ */
621 static void XA_YUV221111_Convert(unsigned char *image_p, unsigned int imagex, unsigned int imagey,
622 unsigned int i_x, unsigned int i_y, YUVBufs *yuv, YUVTabs *yuv_tabs, unsigned int map_flag,
623 unsigned int *map, XA_CHDR *chdr)
625 sh_video_t *sh = (sh_video_t*)image_p;
626 vd_xanim_ctx *priv = sh->context;
627 mp_image_t *mpi;
628 // note: 3ivX codec doesn't set y_w, uv_w, they are random junk :(
629 int ystride=imagex; //(yuv->y_w)?yuv->y_w:imagex;
630 int uvstride=imagex/2; //(yuv->uv_w)?yuv->uv_w:(imagex/2);
632 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "XA_YUV221111_Convert(%p %dx%d %d;%d [%dx%d] %p %p %d %p %p)\n",
633 image_p,imagex,imagey,i_x,i_y, sh->disp_w, sh->disp_h,
634 yuv,yuv_tabs,map_flag,map,chdr);
636 mp_dbg(MSGT_DECVIDEO,MSGL_DBG3, "YUV: %p %p %p %X (%X) %Xx%X %Xx%X\n",
637 yuv->Ybuf,yuv->Ubuf,yuv->Vbuf,yuv->the_buf,yuv->the_buf_size,
638 yuv->y_w,yuv->y_h,yuv->uv_w,yuv->uv_h);
640 // standard YV12 - simply export it!
641 mpi = mpcodecs_get_image(sh, MP_IMGTYPE_EXPORT, 0, sh->disp_w, sh->disp_h);
642 priv->mpi=mpi; if(!mpi) return; // ERROR!
643 mpi->planes[0]=yuv->Ybuf;
644 mpi->planes[1]=yuv->Ubuf;
645 mpi->planes[2]=yuv->Vbuf;
646 mpi->width=imagex;
647 mpi->stride[0]=ystride; //i_x; // yuv->y_w
648 mpi->stride[1]=mpi->stride[2]=uvstride; //=i_x/4; // yuv->uv_w
651 void *XA_YUV221111_Func(unsigned int image_type)
653 mp_dbg(MSGT_DECVIDEO,MSGL_DBG2, "XA_YUV221111_Func('image_type: %d')\n",image_type);
654 return (void *)XA_YUV221111_Convert;
657 /* *** EOF XANIM *** */
659 // to set/get/query special features/parameters
660 static int control(sh_video_t *sh,int cmd,void* arg,...){
661 return CONTROL_UNKNOWN;
664 // init driver
665 static int init(sh_video_t *sh)
667 vd_xanim_ctx *priv;
668 char dll[1024];
669 XA_CODEC_HDR codec_hdr;
670 int i;
672 priv = malloc(sizeof(vd_xanim_ctx));
673 if (!priv)
674 return 0;
675 sh->context = priv;
676 memset(priv, 0, sizeof(vd_xanim_ctx));
678 if(!mpcodecs_config_vo(sh,sh->disp_w,sh->disp_h,IMGFMT_YV12)) return 0;
680 priv->iq_func = NULL;
681 priv->dec_func = NULL;
683 for (i=0; i < XA_CLOSE_FUNCS; i++)
684 xa_close_func[i] = NULL;
686 snprintf(dll, 1024, "%s/%s", def_path, sh->codec->dll);
687 if (xacodec_load(sh, dll) == 0)
688 return 0;
690 codec_hdr.xapi_rev = XAVID_API_REV;
691 codec_hdr.anim_hdr = malloc(4096);
692 codec_hdr.description = sh->codec->info;
693 codec_hdr.compression = bswap_32(sh->bih->biCompression);
694 codec_hdr.decoder = NULL;
695 codec_hdr.x = sh->bih->biWidth; /* ->disp_w */
696 codec_hdr.y = sh->bih->biHeight; /* ->disp_h */
697 /* extra fields to store palette */
698 codec_hdr.avi_ctab_flag = 0;
699 codec_hdr.avi_read_ext = NULL;
700 codec_hdr.extra = NULL;
702 switch(sh->codec->outfmt[sh->outfmtidx])
704 case IMGFMT_BGR32:
705 codec_hdr.depth = 32;
706 break;
707 case IMGFMT_BGR24:
708 codec_hdr.depth = 24;
709 break;
710 case IMGFMT_IYUV:
711 case IMGFMT_I420:
712 case IMGFMT_YV12:
713 codec_hdr.depth = 12;
714 break;
715 case IMGFMT_YVU9:
716 codec_hdr.depth = 9;
717 break;
718 default:
719 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: not supported image out format (%s)\n",
720 vo_format_name(sh->codec->outfmt[sh->outfmtidx]));
721 return 0;
723 mp_msg(MSGT_DECVIDEO, MSGL_INFO, "xacodec: querying for input %dx%d %dbit [fourcc: %4x] (%s)...\n",
724 codec_hdr.x, codec_hdr.y, codec_hdr.depth, codec_hdr.compression, codec_hdr.description);
726 if (xacodec_query(sh, &codec_hdr) == 0)
727 return 0;
729 // free(codec_hdr.anim_hdr);
731 priv->decinfo = malloc(sizeof(XA_DEC_INFO));
732 if (priv->decinfo == NULL)
734 mp_msg(MSGT_DECVIDEO, MSGL_FATAL, "xacodec: memory allocation error: %s\n",
735 strerror(errno));
736 return 0;
738 priv->decinfo->cmd = 0;
739 priv->decinfo->skip_flag = 0;
740 priv->decinfo->imagex = priv->decinfo->xe = codec_hdr.x;
741 priv->decinfo->imagey = priv->decinfo->ye = codec_hdr.y;
742 priv->decinfo->imaged = codec_hdr.depth;
743 priv->decinfo->chdr = NULL;
744 priv->decinfo->map_flag = 0; /* xaFALSE */
745 priv->decinfo->map = NULL;
746 priv->decinfo->xs = priv->decinfo->ys = 0;
747 priv->decinfo->special = 0;
748 priv->decinfo->extra = codec_hdr.extra;
749 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "decinfo->extra, filled by codec: %p [%s]\n",
750 &priv->decinfo->extra, (char *)priv->decinfo->extra);
752 return 1;
755 // uninit driver
756 static void uninit(sh_video_t *sh)
758 vd_xanim_ctx *priv = sh->context;
759 int i;
760 void (*close_func)();
762 for (i=0; i < XA_CLOSE_FUNCS; i++)
763 if (xa_close_func[i])
765 close_func = xa_close_func[i];
766 close_func();
768 dlclose(priv->file_handler);
769 if (priv->decinfo != NULL)
770 free(priv->decinfo);
771 free(priv);
774 // unsigned int (*dec_func)(unsigned char *image, unsigned char *delta,
775 // unsigned int dsize, XA_DEC_INFO *dec_info);
777 // decode a frame
778 static mp_image_t* decode(sh_video_t *sh, void *data, int len, int flags)
780 vd_xanim_ctx *priv = sh->context;
781 unsigned int ret;
783 if (len <= 0)
784 return NULL; // skipped frame
786 priv->decinfo->skip_flag = (flags&3)?1:0;
788 if(sh->codec->outflags[sh->outfmtidx] & CODECS_FLAG_STATIC){
789 // allocate static buffer for cvid-like codecs:
790 priv->mpi = mpcodecs_get_image(sh, MP_IMGTYPE_STATIC,
791 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
792 (sh->disp_w+3)&(~3), (sh->disp_h+3)&(~3));
793 if (!priv->mpi) return NULL;
794 ret = priv->dec_func((uint8_t*)priv->mpi, data, len, priv->decinfo);
795 } else {
796 // left the buffer allocation to the codecs, pass sh_video && priv
797 priv->mpi=NULL;
798 ret = priv->dec_func((uint8_t*)sh, data, len, priv->decinfo);
801 if (ret == ACT_DLTA_NORM)
802 return priv->mpi;
804 if (ret & ACT_DLTA_MAPD)
805 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "mapd\n");
807 if (!(ret & ACT_DLT_MAPD))
808 xacodec_driver->decinfo->map_flag = 0;
809 else
811 xacodec_driver->decinfo->map_flag = 1;
812 xacodec_driver->decinfo->map = ...
816 if (ret & ACT_DLTA_XOR)
818 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "xor\n");
819 return priv->mpi;
822 /* nothing changed */
823 if (ret & ACT_DLTA_NOP)
825 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "nop\n");
826 return NULL;
829 /* frame dropped (also display latest frame) */
830 if (ret & ACT_DLTA_DROP)
832 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "drop\n");
833 return NULL;
836 if (ret & ACT_DLTA_BAD)
838 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "bad\n");
839 return NULL;
842 /* used for double buffer */
843 if (ret & ACT_DLTA_BODY)
845 mp_msg(MSGT_DECVIDEO, MSGL_DBG2, "body\n");
846 return NULL;
849 return priv->mpi;