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 /*****************************************************************************
26 ****************************************************************************/
36 #include "vd_internal.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 /*****************************************************************************
65 ****************************************************************************/
69 unsigned char img_type
;
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
;
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
]){
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
;
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
;
135 cs
= XVID_CSP_RGB555
;
138 cs
= XVID_CSP_RGB565
;
147 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unsupported out_fmt: 0x%X\n",
148 sh
->codec
->outfmt
[sh
->outfmtidx
]);
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");
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
))
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) */
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");
179 p
= (priv_t
*)malloc(sizeof(priv_t
));
181 p
->hdl
= dec_p
.handle
;
182 p
->vo_initialized
= 0;
186 case XVID_CSP_INTERNAL
:
187 p
->img_type
= MP_IMGTYPE_EXPORT
;
190 p
->img_type
= MP_IMGTYPE_STATIC
;
193 p
->img_type
= MP_IMGTYPE_TEMP
;
200 /*============================================================================
201 * uninit - close the codec
202 *==========================================================================*/
204 static void uninit(sh_video_t
*sh
){
205 priv_t
* p
= sh
->context
;
208 xvid_decore(p
->hdl
,XVID_DEC_DESTROY
, NULL
, NULL
);
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)
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
;
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 );
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 */
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];
274 consumed
= xvid_decore(p
->hdl
, XVID_DEC_DECODE
, &dec
, &stats
);
276 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Decoding error\n");
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
))
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 /*****************************************************************************
319 ****************************************************************************/
321 /* Returns DAR value according to VOL's informations contained in stats
323 static float stats2aspect(xvid_dec_stats_t
*stats
)
325 if (stats
->type
== XVID_TYPE_VOL
) {
330 /* MPEG4 strem stores PAR (Pixel Aspect Ratio), mplayer uses
331 * DAR (Display Aspect Ratio)
333 * Both are related thanks to the equation:
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 */
345 case XVID_PAR_43_PAL
: /* 4:3 pal (12:11 625-line) */
349 case XVID_PAR_43_NTSC
: /* 4:3 ntsc (10:11 525-line) */
353 case XVID_PAR_169_PAL
: /* 16:9 pal (16:11 625-line) */
357 case XVID_PAR_169_NTSC
: /* 16:9 ntsc (40:33 525-line) */
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
;
370 dar
= ((float)stats
->data
.vol
.width
*wpar
);
371 dar
/= ((float)stats
->data
.vol
.height
*hpar
);
379 /*****************************************************************************
380 * Module structure definition
381 ****************************************************************************/
383 static vd_info_t info
=
387 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
388 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
394 #endif /* HAVE_XVID4 */
396 /* Please do not change that tag comment.
397 * arch-tag: b7d654a5-76ea-4768-9713-2c791567fe7d mplayer xvid decoder module */