typo fixes
[mplayer/greg.git] / libmpcodecs / vd_xvid4.c
blob57b07cb401734c84ff99374356d7b5c96d4d439b
1 /*****************************************************************************
3 * - XviD 1.x decoder module for mplayer/mencoder -
5 * Copyright(C) 2003 Marco Belli <elcabesa@inwind.it>
6 * 2003-2004 Edouard Gomez <ed.gomez@free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *****************************************************************************/
24 /*****************************************************************************
25 * Includes
26 ****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>
31 #include "config.h"
32 #include "mp_msg.h"
34 #ifdef HAVE_XVID4
36 #include "vd_internal.h"
37 #include "m_option.h"
39 #include <xvid.h>
41 /*****************************************************************************
42 * Configuration options
43 ****************************************************************************/
45 static int do_dr2 = 1;
46 static int filmeffect = 0;
47 static int lumadeblock = 0;
48 static int chromadeblock = 0;
49 static int lumadering = 0;
50 static int chromadering = 0;
52 m_option_t xvid_dec_opts[] = {
53 { "dr2", &do_dr2, CONF_TYPE_FLAG, 0, 0, 1, NULL},
54 { "nodr2", &do_dr2, CONF_TYPE_FLAG, 0, 1, 0, NULL},
55 { "filmeffect", &filmeffect, CONF_TYPE_FLAG, 0, 0, 1, NULL},
56 { "deblock-luma", &lumadeblock, CONF_TYPE_FLAG, 0, 0, 1, NULL},
57 { "deblock-chroma", &chromadeblock, CONF_TYPE_FLAG, 0, 0, 1, NULL},
58 { "dering-luma", &lumadering, CONF_TYPE_FLAG, 0, 0, 1, NULL},
59 { "dering-chroma", &chromadering, CONF_TYPE_FLAG, 0, 0, 1, NULL},
60 {NULL, NULL, 0, 0, 0, 0, NULL}
63 /*****************************************************************************
64 * Module private data
65 ****************************************************************************/
67 typedef struct {
68 int cs;
69 unsigned char img_type;
70 void* hdl;
71 mp_image_t* mpi;
72 int vo_initialized;
73 } priv_t;
75 /*****************************************************************************
76 * Module function helpers
77 ****************************************************************************/
79 static float stats2aspect(xvid_dec_stats_t *stats);
81 /*****************************************************************************
82 * Video decoder API function definitions
83 ****************************************************************************/
85 /*============================================================================
86 * control - to set/get/query special features/parameters
87 *==========================================================================*/
89 static int control(sh_video_t *sh,int cmd,void* arg,...)
91 return(CONTROL_UNKNOWN);
94 /*============================================================================
95 * init - initialize the codec
96 *==========================================================================*/
98 static int init(sh_video_t *sh)
100 xvid_gbl_info_t xvid_gbl_info;
101 xvid_gbl_init_t xvid_ini;
102 xvid_dec_create_t dec_p;
103 priv_t* p;
104 int cs;
106 memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info_t));
107 xvid_gbl_info.version = XVID_VERSION;
109 memset(&xvid_ini, 0, sizeof(xvid_gbl_init_t));
110 xvid_ini.version = XVID_VERSION;
112 memset(&dec_p, 0, sizeof(xvid_dec_create_t));
113 dec_p.version = XVID_VERSION;
116 switch(sh->codec->outfmt[sh->outfmtidx]){
117 case IMGFMT_YV12:
118 /* We will use our own buffers, this speeds decoding avoiding
119 * frame memcpy's overhead */
120 cs = (do_dr2)?XVID_CSP_INTERNAL:XVID_CSP_USER;
121 break;
122 case IMGFMT_YUY2:
123 cs = XVID_CSP_YUY2;
124 break;
125 case IMGFMT_UYVY:
126 cs = XVID_CSP_UYVY;
127 break;
128 case IMGFMT_I420:
129 case IMGFMT_IYUV:
130 /* We will use our own buffers, this speeds decoding avoiding
131 * frame memcpy's overhead */
132 cs = (do_dr2)?XVID_CSP_INTERNAL:XVID_CSP_USER;
133 break;
134 case IMGFMT_BGR15:
135 cs = XVID_CSP_RGB555;
136 break;
137 case IMGFMT_BGR16:
138 cs = XVID_CSP_RGB565;
139 break;
140 case IMGFMT_BGR32:
141 cs = XVID_CSP_BGRA;
142 break;
143 case IMGFMT_YVYU:
144 cs = XVID_CSP_YVYU;
145 break;
146 default:
147 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Unsupported out_fmt: 0x%X\n",
148 sh->codec->outfmt[sh->outfmtidx]);
149 return(0);
152 /* Gather some information about the host library */
153 if(xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL) < 0) {
154 mp_msg(MSGT_MENCODER,MSGL_INFO, "xvid: could not get information about the library\n");
155 } else {
156 mp_msg(MSGT_MENCODER,MSGL_INFO, "xvid: using library version %d.%d.%d (build %s)\n",
157 XVID_VERSION_MAJOR(xvid_gbl_info.actual_version),
158 XVID_VERSION_MINOR(xvid_gbl_info.actual_version),
159 XVID_VERSION_PATCH(xvid_gbl_info.actual_version),
160 xvid_gbl_info.build);
163 /* Initialize the xvidcore library */
164 if(xvid_global(NULL, XVID_GBL_INIT, &xvid_ini, NULL))
165 return(0);
167 /* We use 0 width and height so xvidcore will resize its buffers
168 * if required. That allows this vd plugin to do resize on first
169 * VOL encountered (don't trust containers' width and height) */
170 dec_p.width = 0;
171 dec_p.height = 0;
173 /* Get a decoder instance */
174 if(xvid_decore(0, XVID_DEC_CREATE, &dec_p, NULL)<0) {
175 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "XviD init failed\n");
176 return(0);
179 p = (priv_t*)malloc(sizeof(priv_t));
180 p->cs = cs;
181 p->hdl = dec_p.handle;
182 p->vo_initialized = 0;
183 sh->context = p;
185 switch(cs) {
186 case XVID_CSP_INTERNAL:
187 p->img_type = MP_IMGTYPE_EXPORT;
188 break;
189 case XVID_CSP_USER:
190 p->img_type = MP_IMGTYPE_STATIC;
191 break;
192 default:
193 p->img_type = MP_IMGTYPE_TEMP;
194 break;
197 return(1);
200 /*============================================================================
201 * uninit - close the codec
202 *==========================================================================*/
204 static void uninit(sh_video_t *sh){
205 priv_t* p = sh->context;
206 if(!p)
207 return;
208 xvid_decore(p->hdl,XVID_DEC_DESTROY, NULL, NULL);
209 free(p);
212 /*============================================================================
213 * decode - decode a frame from stream
214 *==========================================================================*/
216 static mp_image_t* decode(sh_video_t *sh, void* data, int len, int flags)
218 xvid_dec_frame_t dec;
219 xvid_dec_stats_t stats;
220 mp_image_t* mpi = NULL;
222 priv_t* p = sh->context;
225 if(!data || len <= 0)
226 return(NULL);
228 memset(&dec,0,sizeof(xvid_dec_frame_t));
229 memset(&stats, 0, sizeof(xvid_dec_stats_t));
230 dec.version = XVID_VERSION;
231 stats.version = XVID_VERSION;
233 dec.bitstream = data;
234 dec.length = len;
236 dec.general |= XVID_LOWDELAY
237 /* XXX: if lowdelay is unset, and xvidcore internal buffers are
238 * used => crash. MUST FIX */
239 | (filmeffect ? XVID_FILMEFFECT : 0 )
240 | (lumadeblock ? XVID_DEBLOCKY : 0 )
241 | (chromadeblock ? XVID_DEBLOCKUV : 0 );
242 #if XVID_API >= XVID_MAKE_API(4,1)
243 dec.general |= (lumadering ? XVID_DEBLOCKY|XVID_DERINGY : 0 );
244 dec.general |= (chromadering ? XVID_DEBLOCKUV|XVID_DERINGUV : 0 );
245 #endif
246 dec.output.csp = p->cs;
248 /* Decoding loop because xvidcore may return VOL information for
249 * on the fly buffer resizing. In that case we must decode VOL,
250 * init VO, then decode the frame */
251 do {
252 int consumed;
254 /* If we don't know frame size yet, don't even try to request
255 * a buffer, we must loop until we find a VOL, so VO plugin
256 * is initialized and we can obviously output something */
257 if (p->vo_initialized) {
258 mpi = mpcodecs_get_image(sh, p->img_type,
259 MP_IMGFLAG_ACCEPT_STRIDE,
260 sh->disp_w, sh->disp_h);
262 if(p->cs != XVID_CSP_INTERNAL) {
263 dec.output.plane[0] = mpi->planes[0];
264 dec.output.plane[1] = mpi->planes[1];
265 dec.output.plane[2] = mpi->planes[2];
267 dec.output.stride[0] = mpi->stride[0];
268 dec.output.stride[1] = mpi->stride[1];
269 dec.output.stride[2] = mpi->stride[2];
273 /* Decode data */
274 consumed = xvid_decore(p->hdl, XVID_DEC_DECODE, &dec, &stats);
275 if (consumed < 0) {
276 mp_msg(MSGT_DECVIDEO, MSGL_ERR, "Decoding error\n");
277 return(NULL);
280 /* Found a VOL information stats, if VO plugin is not initialized
281 * yet then do it now */
282 if (stats.type == XVID_TYPE_VOL && !p->vo_initialized) {
283 sh->aspect = stats2aspect(&stats);
284 if(!mpcodecs_config_vo(sh, stats.data.vol.width, stats.data.vol.height, IMGFMT_YV12))
285 return(NULL);
287 /* Don't take this path twice */
288 p->vo_initialized = !p->vo_initialized;
291 /* Don't forget to update buffer position and buffer length */
292 dec.bitstream += consumed;
293 dec.length -= consumed;
294 } while ((stats.type == XVID_TYPE_VOL || stats.type == XVID_TYPE_NOTHING) && dec.length > 0);
296 /* There are two ways to get out of the decoding loop:
297 * - a frame has been returned
298 * - no more data in buffer and no frames returned */
300 /* If mpi is NULL, it proves nothing has been returned by the decoder
301 * so don't try to display internal buffers. */
302 if (mpi != NULL && p->cs == XVID_CSP_INTERNAL) {
303 mpi->planes[0] = dec.output.plane[0];
304 mpi->planes[1] = dec.output.plane[1];
305 mpi->planes[2] = dec.output.plane[2];
307 mpi->stride[0] = dec.output.stride[0];
308 mpi->stride[1] = dec.output.stride[1];
309 mpi->stride[2] = dec.output.stride[2];
312 /* If we got out the decoding loop because the buffer was empty and there was nothing
313 * to output yet, then just return NULL */
314 return((stats.type == XVID_TYPE_NOTHING)? NULL: mpi);
317 /*****************************************************************************
318 * Helper functions
319 ****************************************************************************/
321 /* Returns DAR value according to VOL's informations contained in stats
322 * param */
323 static float stats2aspect(xvid_dec_stats_t *stats)
325 if (stats->type == XVID_TYPE_VOL) {
326 float wpar;
327 float hpar;
328 float dar;
330 /* MPEG4 strem stores PAR (Pixel Aspect Ratio), mplayer uses
331 * DAR (Display Aspect Ratio)
333 * Both are related thanks to the equation:
334 * width
335 * DAR = ----- x PAR
336 * height
338 * As MPEG4 is so well designed (*cough*), VOL header carries
339 * both informations together -- lucky eh ? */
341 switch (stats->data.vol.par) {
342 case XVID_PAR_11_VGA: /* 1:1 vga (square), default if supplied PAR is not a valid value */
343 wpar = hpar = 1.0f;
344 break;
345 case XVID_PAR_43_PAL: /* 4:3 pal (12:11 625-line) */
346 wpar = 12;
347 hpar = 11;
348 break;
349 case XVID_PAR_43_NTSC: /* 4:3 ntsc (10:11 525-line) */
350 wpar = 10;
351 hpar = 11;
352 break;
353 case XVID_PAR_169_PAL: /* 16:9 pal (16:11 625-line) */
354 wpar = 16;
355 hpar = 11;
356 break;
357 case XVID_PAR_169_NTSC: /* 16:9 ntsc (40:33 525-line) */
358 wpar = 40;
359 hpar = 33;
360 break;
361 case XVID_PAR_EXT: /* extended par; use par_width, par_height */
362 wpar = stats->data.vol.par_width;
363 hpar = stats->data.vol.par_height;
364 break;
365 default:
366 wpar = hpar = 1.0f;
367 break;
370 dar = ((float)stats->data.vol.width*wpar);
371 dar /= ((float)stats->data.vol.height*hpar);
373 return(dar);
376 return(0.0f);
379 /*****************************************************************************
380 * Module structure definition
381 ****************************************************************************/
383 static vd_info_t info =
385 "XviD 1.0 decoder",
386 "xvid",
387 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
388 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
389 "No Comment"
392 LIBVD_EXTERN(xvid)
394 #endif /* HAVE_XVID4 */
396 /* Please do not change that tag comment.
397 * arch-tag: b7d654a5-76ea-4768-9713-2c791567fe7d mplayer xvid decoder module */