1 /*****************************************************************************
2 * xvid.c: an encoder for libxvidcore, the Xvid video codec
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: xvid.c,v 1.3 2003/02/22 16:10:31 fenrir Exp $
7 * Authors: Laurent Aimar
9 * This program 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.
14 * This program 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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
39 /*****************************************************************************
41 *****************************************************************************/
42 static int OpenEncoder ( vlc_object_t * );
43 static void CloseEncoder ( vlc_object_t * );
45 static int Init ( video_encoder_t *p_encoder );
46 static int Encode ( video_encoder_t *p_encoder,
47 picture_t *p_pic, void *p_data, size_t *pi_data );
48 static void End ( video_encoder_t *p_encoder );
51 /*****************************************************************************
53 *****************************************************************************/
54 static char * ppsz_xvid_quant_algo[] = { "MPEG", "H263",NULL };
55 static char * ppsz_xvid_me[] = { "", "zero", "logarithmic", "fullsearch", "pmvfast", "epzs", NULL };
58 set_description( _("XviD video encoder (MPEG-4)") );
59 set_capability( "video encoder", 50 );
60 set_callbacks( OpenEncoder, CloseEncoder );
62 add_shortcut( "xvid" );
63 add_category_hint( "general setting", NULL, VLC_TRUE );
64 add_integer( "encoder-xvid-bitrate", 1000, NULL, "bitrate (kb/s)", "bitrate (kb/s)", VLC_TRUE );
65 add_integer( "encoder-xvid-min-quantizer", 2, NULL, "min quantizer", "range 1-31", VLC_TRUE );
66 add_integer( "encoder-xvid-max-quantizer", 31, NULL, "max quantizer", "1-31", VLC_TRUE );
67 add_integer( "encoder-xvid-max-key-interval", -1, NULL, "max key interval", "maximum value of frames between two keyframes", VLC_TRUE );
68 add_category_hint( "advanced setting", NULL, VLC_TRUE );
69 add_integer( "encoder-xvid-reaction-delay-factor", -1, NULL, "rc reaction delay factor", "rate controler parameters", VLC_TRUE);
70 add_integer( "encoder-xvid-averaging-period", -1, NULL, "rc averaging period", "rate controler parameters", VLC_TRUE );
71 add_integer( "encoder-xvid-buffer", -1, NULL, "rc buffer", "rate controler parameters", VLC_TRUE );
72 add_category_hint( "advanced frame setting", NULL, VLC_TRUE );
73 add_string_from_list( "encoder-xvid-quantization", "MPEG", ppsz_xvid_quant_algo, NULL, "quantization algorithm", "", VLC_TRUE );
74 add_bool( "encoder-xvid-halfpel", 1, NULL, "half pixel motion estimation.", "", VLC_TRUE );
75 add_bool( "encoder-xvid-4mv", 0, NULL, "fourc vector per macroblock(need halfpel)", "", VLC_TRUE );
76 add_bool( "encoder-xvid-lumi-mask", 0, NULL, "use a lumimasking algorithm", "", VLC_TRUE );
77 add_bool( "encoder-xvid-adaptive-quant", 0, NULL, "perform an adaptative quantization", "", VLC_TRUE );
78 add_bool( "encoder-xvid-interlacing", 0, NULL, "use MPEG4 interlaced mode", "", VLC_TRUE );
79 add_string_from_list( "encoder-xvid-me", "", ppsz_xvid_me, NULL, "motion estimation", "", VLC_TRUE );
80 add_bool( "encoder-xvid-motion-advanceddiamond", 1, NULL, "motion advanceddiamond", "", VLC_TRUE );
81 add_bool( "encoder-xvid-motion-halfpeldiamond", 1, NULL, "motion halfpel diamond", "", VLC_TRUE );
82 add_bool( "encoder-xvid-motion-halfpelrefine", 1, NULL, "motion halfpelrefine", "", VLC_TRUE );
83 add_bool( "encoder-xvid-motion-extsearch", 1, NULL, "motion extsearch", "", VLC_TRUE );
84 add_bool( "encoder-xvid-motion-earlystop", 1, NULL, "motion earlystop", "", VLC_TRUE );
85 add_bool( "encoder-xvid-motion-quickstop", 1, NULL, "motion quickstop", "", VLC_TRUE );
86 add_bool( "encoder-xvid-motion-usesquares", 0, NULL, "use a square search", "", VLC_TRUE );
93 XVID_ENC_FRAME xframe;
96 /*****************************************************************************
98 *****************************************************************************
99 * Check the library and init it
100 * see if it can encode to the requested codec
101 *****************************************************************************/
102 static int OpenEncoder ( vlc_object_t *p_this )
104 video_encoder_t *p_encoder = (video_encoder_t*)p_this;
106 XVID_INIT_PARAM xinit;
110 if( p_encoder->i_codec != VLC_FOURCC( 'm', 'p', '4', 'v' ) )
112 /* unsupported codec */
116 msg_Dbg( p_encoder, "XviD encoder compiled for API_VERSION:0x%x", API_VERSION );
118 /* init the library */
120 if( ( i_err = xvid_init( NULL, 0, &xinit, NULL ) ) != XVID_ERR_OK )
122 msg_Err( p_encoder, "cannot init xvid library (err:0x%x)", i_err );
125 /* check API_VERSION */
126 if( xinit.api_version != API_VERSION )
128 msg_Err( p_encoder, "API_VERSION version mismatch (library:0x%x)", xinit.api_version );
132 msg_Dbg( p_encoder, "XviD api_version:0x%x core_build:0x%x cpu_flags:0x%x",
137 /* force some parameters */
138 if( p_encoder->i_chroma != VLC_FOURCC( 'Y', 'V', '1', '2' )&&
139 p_encoder->i_chroma != VLC_FOURCC( 'I', '4', '2', '0' ) )
141 p_encoder->i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
144 /* set exported functions */
145 p_encoder->pf_init = Init;
146 p_encoder->pf_encode = Encode;
147 p_encoder->pf_end = End;
152 static void CloseEncoder ( vlc_object_t *p_this )
157 /*****************************************************************************
159 *****************************************************************************
161 *****************************************************************************/
162 static int Init ( video_encoder_t *p_encoder )
164 encoder_sys_t *p_sys;
168 XVID_ENC_PARAM xparam;
170 if( !( p_encoder->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) ) ) )
172 msg_Err( p_encoder, "out of memory" );
175 memset( p_sys, 0, sizeof( encoder_sys_t ) );
177 /* *** open a instance of the codec *** */
178 xparam.width = p_encoder->i_width;
179 xparam.height = p_encoder->i_height;
181 /* framerat = xparam.fbase / xparam.fincr */
185 /* desired bitrate */
186 xparam.rc_bitrate = config_GetInt( p_encoder, "encoder-xvid-bitrate" ) * 1000;
187 /* setting to success in achive xparam.rc_bitrate */
188 xparam.rc_reaction_delay_factor = config_GetInt( p_encoder, "encoder-xvid-reaction-delay-factor" );
189 xparam.rc_averaging_period = config_GetInt( p_encoder, "encoder-xvid-averaging-period" );
190 xparam.rc_buffer = config_GetInt( p_encoder, "encoder-xvid-buffer" );
192 /* limit the range of allowed quantizers [1..31] */
193 xparam.min_quantizer = config_GetInt( p_encoder, "encoder-xvid-min-quantizer" );;
194 xparam.min_quantizer = __MAX( __MIN( xparam.min_quantizer, 31 ), 1 );
196 xparam.max_quantizer = config_GetInt( p_encoder, "encoder-xvid-max-quantizer" );
197 xparam.max_quantizer = __MAX( __MIN( xparam.max_quantizer, 31 ), 1 );
199 /* maximum value of frames between two keyframes */
200 xparam.max_key_interval = config_GetInt( p_encoder, "encoder-xvid-max-key-interval" );
202 switch( xvid_encore( NULL, XVID_ENC_CREATE, &xparam, NULL ) )
205 msg_Dbg( p_encoder, "encoder creation successed" );
208 case XVID_ERR_MEMORY:
209 msg_Err( p_encoder, "encoder creation failed, out of memory" );
211 case XVID_ERR_FORMAT:
212 msg_Err( p_encoder, "encoder creation failed, bad format" );
217 msg_Err( p_encoder, "encoder creation failed" );
221 p_sys->handle = xparam.handle;
223 /* *** set xframe parameters *** */
224 #define xframe p_sys->xframe
225 /* set xframe.general options */
227 psz = config_GetPsz( p_encoder, "encoder-xvid-quantization" );
230 if( !strcmp( psz, "H263" ) )
232 xframe.general |= XVID_H263QUANT;
236 xframe.general |= XVID_MPEGQUANT;
240 if( config_GetInt( p_encoder, "encoder-xvid-halfpel" ) )
241 xframe.general |= XVID_HALFPEL;
242 if( config_GetInt( p_encoder, "encoder-xvid-4mv" ) )
243 xframe.general |= XVID_INTER4V;
244 if( config_GetInt( p_encoder, "encoder-xvid-lumi-mask" ) )
245 xframe.general |= XVID_LUMIMASKING;
246 if( config_GetInt( p_encoder, "encoder-xvid-adaptive-quant" ) )
247 xframe.general |= XVID_ADAPTIVEQUANT;
248 if( config_GetInt( p_encoder, "encoder-xvid-interlacing" ) )
249 xframe.general |= XVID_INTERLACING;
251 psz = config_GetPsz( p_encoder, "encoder-xvid-me" );
254 if( !strcmp( psz, "zero" ) )
256 xframe.general |= XVID_ME_ZERO;
258 else if( !strcmp( psz, "logarithmic" ) )
260 xframe.general |= XVID_ME_LOGARITHMIC;
262 else if( !strcmp( psz, "fullsearch" ) )
264 xframe.general |= XVID_ME_FULLSEARCH;
266 else if( !strcmp( psz, "pmvfast" ) )
268 xframe.general |= XVID_ME_PMVFAST;
270 else if( !strcmp( psz, "epzs" ) )
272 xframe.general |= XVID_ME_EPZS;
278 if( config_GetInt( p_encoder, "encoder-xvid-motion-advanceddiamond" ) )
279 xframe.motion |= PMV_ADVANCEDDIAMOND16 | PMV_ADVANCEDDIAMOND8;
280 if( config_GetInt( p_encoder, "encoder-xvid-motion-halfpeldiamond" ) )
281 xframe.motion |= PMV_HALFPELDIAMOND16 | PMV_HALFPELDIAMOND8;
282 if( config_GetInt( p_encoder, "encoder-xvid-motion-halfpelrefine") )
283 xframe.motion |= PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8;
284 if( config_GetInt( p_encoder, "encoder-xvid-motion-extsearch" ) )
285 xframe.motion |= PMV_EXTSEARCH16 | PMV_EXTSEARCH8;
286 if( config_GetInt( p_encoder, "encoder-xvid-motion-earlystop") )
287 xframe.motion |= PMV_EARLYSTOP16 | PMV_EARLYSTOP8;
288 if( config_GetInt( p_encoder, "encoder-xvid-motion-quickstop" ) )
289 xframe.motion |= PMV_QUICKSTOP16 | PMV_QUICKSTOP8;
290 if( config_GetInt( p_encoder, "encoder-xvid-motion-usesquares" ) )
291 xframe.motion |= PMV_USESQUARES16 | PMV_USESQUARES8;
293 /* no user quant matrix */
294 xframe.quant_intra_matrix = NULL;
295 xframe.quant_inter_matrix = NULL;
297 switch( p_encoder->i_chroma )
299 case VLC_FOURCC( 'Y', 'V', '1', '2' ):
300 xframe.colorspace = XVID_CSP_YV12;
302 case VLC_FOURCC( 'I', '4', '2', '0' ):
303 xframe.colorspace = XVID_CSP_I420;
305 /* != 0 -> force quant */
312 /*****************************************************************************
313 * Encode: encode a single frame
314 *****************************************************************************
316 *****************************************************************************/
317 static int Encode ( video_encoder_t *p_encoder,
318 picture_t *p_pic, void *p_data, size_t *pi_data )
321 #define xframe p_encoder->p_sys->xframe
323 xframe.image = p_pic->p->p_pixels;
325 xframe.bitstream = p_data;
328 /* let codec decided between I-frame and P-frame */
331 i_err = xvid_encore( p_encoder->p_sys->handle, XVID_ENC_ENCODE, &xframe, NULL );
333 *pi_data = xframe.length;
338 /*****************************************************************************
340 *****************************************************************************
342 *****************************************************************************/
343 static void End ( video_encoder_t *p_encoder )
347 /* *** close the codec */
348 i_err = xvid_encore(p_encoder->p_sys->handle, XVID_ENC_DESTROY, NULL, NULL);
350 /* *** free memory */
351 free( p_encoder->p_sys );
353 msg_Dbg( p_encoder, "closing encoder (err:0x%x)", i_err );