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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *****************************************************************************/
24 /*****************************************************************************
26 ****************************************************************************/
34 #include "vd_internal.h"
39 /*****************************************************************************
40 * Configuration options
41 ****************************************************************************/
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;
50 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
}
61 /*****************************************************************************
63 ****************************************************************************/
67 unsigned char img_type
;
73 /*****************************************************************************
74 * Module function helpers
75 ****************************************************************************/
77 static float stats2aspect(xvid_dec_stats_t
*stats
);
79 /*****************************************************************************
80 * Video decoder API function definitions
81 ****************************************************************************/
83 /*============================================================================
84 * control - to set/get/query special features/parameters
85 *==========================================================================*/
87 static int control(sh_video_t
*sh
,int cmd
,void* arg
,...)
89 return(CONTROL_UNKNOWN
);
92 /*============================================================================
93 * init - initialize the codec
94 *==========================================================================*/
96 static int init(sh_video_t
*sh
)
98 xvid_gbl_info_t xvid_gbl_info
;
99 xvid_gbl_init_t xvid_ini
;
100 xvid_dec_create_t dec_p
;
104 memset(&xvid_gbl_info
, 0, sizeof(xvid_gbl_info_t
));
105 xvid_gbl_info
.version
= XVID_VERSION
;
107 memset(&xvid_ini
, 0, sizeof(xvid_gbl_init_t
));
108 xvid_ini
.version
= XVID_VERSION
;
110 memset(&dec_p
, 0, sizeof(xvid_dec_create_t
));
111 dec_p
.version
= XVID_VERSION
;
114 switch(sh
->codec
->outfmt
[sh
->outfmtidx
]){
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
;
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
;
133 cs
= XVID_CSP_RGB555
;
136 cs
= XVID_CSP_RGB565
;
145 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Unsupported out_fmt: 0x%X\n",
146 sh
->codec
->outfmt
[sh
->outfmtidx
]);
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");
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
);
161 /* Initialize the xvidcore library */
162 if(xvid_global(NULL
, XVID_GBL_INIT
, &xvid_ini
, NULL
))
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) */
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");
177 p
= malloc(sizeof(priv_t
));
179 p
->hdl
= dec_p
.handle
;
180 p
->vo_initialized
= 0;
184 case XVID_CSP_INTERNAL
:
185 p
->img_type
= MP_IMGTYPE_EXPORT
;
188 p
->img_type
= MP_IMGTYPE_STATIC
;
191 p
->img_type
= MP_IMGTYPE_TEMP
;
198 /*============================================================================
199 * uninit - close the codec
200 *==========================================================================*/
202 static void uninit(sh_video_t
*sh
){
203 priv_t
* p
= sh
->context
;
206 xvid_decore(p
->hdl
,XVID_DEC_DESTROY
, NULL
, NULL
);
210 /*============================================================================
211 * decode - decode a frame from stream
212 *==========================================================================*/
214 static mp_image_t
* decode(sh_video_t
*sh
, void* data
, int len
, int flags
)
216 xvid_dec_frame_t dec
;
217 xvid_dec_stats_t stats
;
218 mp_image_t
* mpi
= NULL
;
220 priv_t
* p
= sh
->context
;
223 if(!data
|| len
<= 0)
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
;
231 dec
.bitstream
= data
;
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 );
244 dec
.output
.csp
= p
->cs
;
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 */
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
);
260 if(p
->cs
!= XVID_CSP_INTERNAL
) {
261 dec
.output
.plane
[0] = mpi
->planes
[0];
262 dec
.output
.plane
[1] = mpi
->planes
[1];
263 dec
.output
.plane
[2] = mpi
->planes
[2];
265 dec
.output
.stride
[0] = mpi
->stride
[0];
266 dec
.output
.stride
[1] = mpi
->stride
[1];
267 dec
.output
.stride
[2] = mpi
->stride
[2];
272 consumed
= xvid_decore(p
->hdl
, XVID_DEC_DECODE
, &dec
, &stats
);
274 mp_msg(MSGT_DECVIDEO
, MSGL_ERR
, "Decoding error\n");
278 /* Found a VOL information stats, if VO plugin is not initialized
279 * yet then do it now */
280 if (stats
.type
== XVID_TYPE_VOL
&& !p
->vo_initialized
) {
281 sh
->aspect
= stats2aspect(&stats
);
282 if(!mpcodecs_config_vo(sh
, stats
.data
.vol
.width
, stats
.data
.vol
.height
, IMGFMT_YV12
))
285 /* Don't take this path twice */
286 p
->vo_initialized
= !p
->vo_initialized
;
289 /* Don't forget to update buffer position and buffer length */
290 dec
.bitstream
+= consumed
;
291 dec
.length
-= consumed
;
292 } while ((stats
.type
== XVID_TYPE_VOL
|| stats
.type
== XVID_TYPE_NOTHING
) && dec
.length
> 0);
294 /* There are two ways to get out of the decoding loop:
295 * - a frame has been returned
296 * - no more data in buffer and no frames returned */
298 /* If mpi is NULL, it proves nothing has been returned by the decoder
299 * so don't try to display internal buffers. */
300 if (mpi
!= NULL
&& p
->cs
== XVID_CSP_INTERNAL
) {
301 mpi
->planes
[0] = dec
.output
.plane
[0];
302 mpi
->planes
[1] = dec
.output
.plane
[1];
303 mpi
->planes
[2] = dec
.output
.plane
[2];
305 mpi
->stride
[0] = dec
.output
.stride
[0];
306 mpi
->stride
[1] = dec
.output
.stride
[1];
307 mpi
->stride
[2] = dec
.output
.stride
[2];
310 /* If we got out the decoding loop because the buffer was empty and there was nothing
311 * to output yet, then just return NULL */
312 return((stats
.type
== XVID_TYPE_NOTHING
)? NULL
: mpi
);
315 /*****************************************************************************
317 ****************************************************************************/
319 /* Returns DAR value according to VOL's informations contained in stats
321 static float stats2aspect(xvid_dec_stats_t
*stats
)
323 if (stats
->type
== XVID_TYPE_VOL
) {
328 /* MPEG4 strem stores PAR (Pixel Aspect Ratio), mplayer uses
329 * DAR (Display Aspect Ratio)
331 * Both are related thanks to the equation:
336 * As MPEG4 is so well designed (*cough*), VOL header carries
337 * both informations together -- lucky eh ? */
339 switch (stats
->data
.vol
.par
) {
340 case XVID_PAR_11_VGA
: /* 1:1 vga (square), default if supplied PAR is not a valid value */
343 case XVID_PAR_43_PAL
: /* 4:3 pal (12:11 625-line) */
347 case XVID_PAR_43_NTSC
: /* 4:3 ntsc (10:11 525-line) */
351 case XVID_PAR_169_PAL
: /* 16:9 pal (16:11 625-line) */
355 case XVID_PAR_169_NTSC
: /* 16:9 ntsc (40:33 525-line) */
359 case XVID_PAR_EXT
: /* extended par; use par_width, par_height */
360 wpar
= stats
->data
.vol
.par_width
;
361 hpar
= stats
->data
.vol
.par_height
;
368 dar
= ((float)stats
->data
.vol
.width
*wpar
);
369 dar
/= ((float)stats
->data
.vol
.height
*hpar
);
377 /*****************************************************************************
378 * Module structure definition
379 ****************************************************************************/
381 static vd_info_t info
=
385 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
386 "Marco Belli <elcabesa@inwind.it>, Edouard Gomez <ed.gomez@free.fr>",
392 /* Please do not change that tag comment.
393 * arch-tag: b7d654a5-76ea-4768-9713-2c791567fe7d mplayer xvid decoder module */