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