]> git.sesse.net Git - vlc/blob - modules/encoder/xvid.c
* modules/access/http.c :
[vlc] / modules / encoder / xvid.c
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 $
6  *
7  * Authors: Laurent Aimar
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/input.h>
30 #include <vlc/decoder.h>
31
32 #include <stdlib.h>
33
34 #include "codecs.h"
35 #include "encoder.h"
36
37 #include <xvid.h>
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static int  OpenEncoder ( vlc_object_t * );
43 static void CloseEncoder ( vlc_object_t * );
44
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 );
49
50
51 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 static char * ppsz_xvid_quant_algo[] = { "MPEG", "H263",NULL };
55 static char * ppsz_xvid_me[] = { "", "zero", "logarithmic", "fullsearch", "pmvfast", "epzs", NULL };
56
57 vlc_module_begin();
58     set_description( _("XviD video encoder (MPEG-4)") );
59     set_capability( "video encoder", 50 );
60     set_callbacks( OpenEncoder, CloseEncoder );
61
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 );
87 vlc_module_end();
88
89
90 struct encoder_sys_t
91 {
92     void *handle;
93     XVID_ENC_FRAME xframe;
94 };
95
96 /*****************************************************************************
97  * OpenEncoder:
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 )
103 {
104     video_encoder_t *p_encoder = (video_encoder_t*)p_this;
105
106     XVID_INIT_PARAM xinit;
107     int i_err;
108
109
110     if( p_encoder->i_codec != VLC_FOURCC( 'm', 'p', '4', 'v' ) )
111     {
112         /* unsupported codec */
113         return VLC_EGENERIC;
114     }
115
116     msg_Dbg( p_encoder, "XviD encoder compiled for API_VERSION:0x%x", API_VERSION );
117
118     /* init the library */
119     xinit.cpu_flags = 0;
120     if( ( i_err = xvid_init( NULL, 0, &xinit, NULL ) ) != XVID_ERR_OK )
121     {
122         msg_Err( p_encoder, "cannot init xvid library (err:0x%x)", i_err );
123         return VLC_EGENERIC;
124     }
125     /* check API_VERSION  */
126     if( xinit.api_version != API_VERSION )
127     {
128         msg_Err( p_encoder, "API_VERSION version mismatch (library:0x%x)", xinit.api_version );
129         return VLC_EGENERIC;
130     }
131
132     msg_Dbg( p_encoder, "XviD api_version:0x%x core_build:0x%x cpu_flags:0x%x",
133             xinit.api_version,
134             xinit.core_build,
135             xinit.cpu_flags );
136
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' ) )
140     {
141         p_encoder->i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
142     }
143
144     /* set exported functions */
145     p_encoder->pf_init = Init;
146     p_encoder->pf_encode = Encode;
147     p_encoder->pf_end = End;
148
149     return VLC_SUCCESS;
150 }
151
152 static void CloseEncoder ( vlc_object_t *p_this )
153 {
154     ;
155 }
156
157 /*****************************************************************************
158  * Init:
159  *****************************************************************************
160  *
161  *****************************************************************************/
162 static int Init     ( video_encoder_t *p_encoder )
163 {
164     encoder_sys_t *p_sys;
165
166     char *psz;
167     int i_err;
168     XVID_ENC_PARAM xparam;
169
170     if( !( p_encoder->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) ) ) )
171     {
172         msg_Err( p_encoder, "out of memory" );
173         return VLC_EGENERIC;
174     }
175     memset( p_sys, 0, sizeof( encoder_sys_t ) );
176
177     /* *** open a instance of the codec *** */
178     xparam.width    = p_encoder->i_width;
179     xparam.height   = p_encoder->i_height;
180
181     /* framerat = xparam.fbase / xparam.fincr */
182     xparam.fincr    = 1;
183     xparam.fbase    = 25;
184
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" );
191
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 );
195
196     xparam.max_quantizer = config_GetInt( p_encoder, "encoder-xvid-max-quantizer" );
197     xparam.max_quantizer = __MAX( __MIN( xparam.max_quantizer, 31 ), 1 );
198
199     /* maximum   value  of   frames  between   two  keyframes */
200     xparam.max_key_interval = config_GetInt( p_encoder, "encoder-xvid-max-key-interval" );
201
202     switch( xvid_encore( NULL, XVID_ENC_CREATE, &xparam, NULL ) )
203     {
204         case XVID_ERR_OK:
205             msg_Dbg( p_encoder, "encoder creation successed" );
206             break;
207
208         case XVID_ERR_MEMORY:
209             msg_Err( p_encoder, "encoder creation failed, out of memory" );
210             return VLC_EGENERIC;
211         case XVID_ERR_FORMAT:
212             msg_Err( p_encoder, "encoder creation failed, bad format" );
213             return VLC_EGENERIC;
214
215         case XVID_ERR_FAIL:
216         default:
217             msg_Err( p_encoder, "encoder creation failed" );
218             return VLC_EGENERIC;
219     }
220     /* get the handle */
221     p_sys->handle = xparam.handle;
222
223     /* *** set xframe parameters *** */
224 #define xframe p_sys->xframe
225     /* set xframe.general options  */
226     xframe.general = 0;
227     psz = config_GetPsz( p_encoder, "encoder-xvid-quantization" );
228     if( psz )
229     {
230         if( !strcmp( psz, "H263" ) )
231         {
232             xframe.general |= XVID_H263QUANT;
233         }
234         else
235         {
236             xframe.general |= XVID_MPEGQUANT;
237         }
238         free( psz );
239     }
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;
250
251     psz = config_GetPsz( p_encoder, "encoder-xvid-me" );
252     if( psz )
253     {
254         if( !strcmp( psz, "zero" ) )
255         {
256             xframe.general |= XVID_ME_ZERO;
257         }
258         else if( !strcmp( psz, "logarithmic" ) )
259         {
260             xframe.general |= XVID_ME_LOGARITHMIC;
261         }
262         else if( !strcmp( psz, "fullsearch" ) )
263         {
264             xframe.general |= XVID_ME_FULLSEARCH;
265         }
266         else if( !strcmp( psz, "pmvfast" ) )
267         {
268             xframe.general |= XVID_ME_PMVFAST;
269         }
270         else if( !strcmp( psz, "epzs" ) )
271         {
272             xframe.general |= XVID_ME_EPZS;
273         }
274         free( psz );
275     }
276
277     xframe.motion = 0;
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;
292
293     /* no user quant matrix */
294     xframe.quant_intra_matrix = NULL;
295     xframe.quant_inter_matrix = NULL;
296
297     switch( p_encoder->i_chroma )
298     {
299         case VLC_FOURCC( 'Y', 'V', '1', '2' ):
300             xframe.colorspace = XVID_CSP_YV12;
301
302         case VLC_FOURCC( 'I', '4', '2', '0' ):
303             xframe.colorspace = XVID_CSP_I420;
304     }
305     /* != 0 -> force quant */
306     xframe.quant = 0;
307 #undef  xframe
308
309     return VLC_SUCCESS;
310 }
311
312 /*****************************************************************************
313  * Encode: encode a single frame
314  *****************************************************************************
315  *
316  *****************************************************************************/
317 static int Encode   ( video_encoder_t *p_encoder,
318                       picture_t *p_pic, void *p_data, size_t *pi_data )
319 {
320     int i_err;
321 #define xframe p_encoder->p_sys->xframe
322
323     xframe.image = p_pic->p->p_pixels;
324
325     xframe.bitstream = p_data;
326     xframe.length = -1;
327
328     /* let codec decided between I-frame and P-frame */
329     xframe.intra = -1;
330
331     i_err = xvid_encore( p_encoder->p_sys->handle, XVID_ENC_ENCODE, &xframe, NULL );
332
333     *pi_data = xframe.length;
334 #undef  xframe
335     return VLC_SUCCESS;
336 }
337
338 /*****************************************************************************
339  * End
340  *****************************************************************************
341  *
342  *****************************************************************************/
343 static void End      ( video_encoder_t *p_encoder )
344 {
345     int i_err;
346
347     /* *** close the codec */
348     i_err = xvid_encore(p_encoder->p_sys->handle, XVID_ENC_DESTROY, NULL, NULL);
349
350     /* *** free memory */
351     free( p_encoder->p_sys );
352
353     msg_Dbg( p_encoder, "closing encoder (err:0x%x)", i_err );
354     return;
355 }
356
357