]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
Patch by Dennis van Amerongen: Unify x264 commandline options and VLC's x264 options.
[vlc] / modules / codec / x264.c
1 /*****************************************************************************
2  * x264.c: h264 video encoder
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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/sout.h>
30 #include <vlc/decoder.h>
31
32 #include <x264.h>
33
34 #define SOUT_CFG_PREFIX "sout-x264-"
35
36 /*****************************************************************************
37  * Module descriptor
38  *****************************************************************************/
39 static int  Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
41
42 #define QP_TEXT N_("Quantizer parameter")
43 #define QP_LONGTEXT N_( \
44     "This selects the quantizer to use (1 to 51). Lower values result in " \
45     "better fidelity, but higher bitrates. 26 is a good default value." )
46
47 #define QPMIN_TEXT N_("Minimum quantizer parameter")
48 #define QPMIN_LONGTEXT N_( "Minimum quantizer, 15/35 seems to be a useful " \
49     "range." )
50
51 #define QPMAX_TEXT N_("Maximum quantizer parameter")
52 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
53
54 #define CABAC_TEXT N_("Enable CABAC")
55 #define CABAC_LONGTEXT N_( "Enable CABAC (Context-Adaptive Binary Arithmetic "\
56     "Coding). Slightly slows down encoding and decoding, but should save " \
57     "10-15% bitrate." )
58
59 #define LOOPF_TEXT N_("Enable loop filter")
60 #define LOOPF_LONGTEXT N_( "Use deblocking loop filter (increases quality).")
61
62 #define ANALYSE_TEXT N_("Analyse mode")
63 #define ANALYSE_LONGTEXT N_( "This selects the analysing mode.")
64
65 #define TOLERANCE_TEXT N_("Bitrate tolerance")
66 #define TOLERANCE_LONGTEXT N_( "Sets the allowed variance in average " \
67     "bitrate.")
68
69 #define VBV_MAXRATE_TEXT N_("Maximum local bitrate")
70 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate in kbits/s.")
71
72 #define VBV_BUFSIZE_TEXT N_("Averaging period for the maximum local bitrate")
73 #define VBV_BUFSIZE_LONGTEXT N_( "Sets an averaging period for the maximum " \
74     "local bitrate, in kbits/s.")
75
76 #define VBV_INIT_TEXT N_("Initial buffer occupancy")
77 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
78     "fraction of the buffer size.")
79
80 #define KEYINT_TEXT N_("Sets maximum interval between IDR-frames")
81 #define KEYINT_LONGTEXT N_( "Larger values save bits, thus improve quality "\
82     "for a given bitrate, at the cost of seeking precision." )
83
84 #define KEYINT_MIN_TEXT N_("Sets minimum interval between IDR-frames")
85 #define KEYINT_MIN_LONGTEXT N_("In H.264, I-Frames do not necessarily bound " \
86     "a closed GOP because it is allowable for a P-frame to be predicted from "\
87     "more frames than just the one frame before it (also see frameref). " \
88     "Therefore, I-frames are not necessarily seekable. " \
89     "IDR-Frames restrict subsequent P-frames from referring to any frame " \
90     "prior to the IDR-Frame. \n" \
91     "If scenecuts appear within this interval, they are still encoded as " \
92     "I-frames, but do not start a new GOP. Default value is keyint * 0.4." )
93
94 #define BFRAMES_TEXT N_("B frames")
95 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-Frames between I and " \
96     "P-frames." )
97
98 #define BPYRAMID_TEXT N_("B pyramid")
99 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
100     "predicting other frames." )
101
102 #define FRAMEREF_TEXT N_("Number of previous frames used as predictors.")
103 #define FRAMEREF_LONGTEXT N_( "This is effective in Anime, but seems to " \
104     "make little difference in live-action source material. Some decoders " \
105     "are unable to deal with large frameref values." )
106
107 #define SCENE_TEXT N_("Scene-cut detection.")
108 #define SCENE_LONGTEXT N_( "Controls how aggressively to insert extra " \
109     "I-frames. With small values of scenecut, the codec often has to force " \
110     "an I-frame when it would exceed keyint. " \
111     "Good values of scenecut may find a better location for the I-frame. " \
112     "Large values use more I-frames than necessary, thus wasting bits. " \
113     "-1 disables scene-cut detection, so I-frames are be inserted only every "\
114     "other keyint frames, which probably leads to ugly encoding artifacts." )
115
116 #define SUBPEL_TEXT N_("Sub-pixel refinement quality.")
117 #define SUBPEL_LONGTEXT N_( "This parameter controls quality versus speed " \
118     "tradeoffs involved in the motion estimation decision process " \
119     "(lower = quicker and higher = better quality)." )
120
121 #define ME_TEXT N_("Motion estimation algorithm.")
122 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
123     " - dia: diamond search, radius 1 (fast)\n" \
124     " - hex: hexagonal search, radius 2\n" \
125     " - umh: uneven multi-hexagon search (better but slower)\n" \
126     " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
127
128 #define MERANGE_TEXT N_("Motion estimation search range.")
129 #define MERANGE_LONGTEXT N_( "Maximum distance to search for motion estimation, "\
130     "measured from predicted position(s). Default of 16 is good for most footage, "\
131     "high motion sequences may benefit from settings between 24-32." )
132
133 #define NO_PSNR_TEXT N_("Disable PSNR calculation.")
134 #define NO_PSNR_LONGTEXT N_( "This has no effect on actual encoding quality, "\
135     "it just prevents the stats from being calculated (for speed)." )
136
137 #define NO_B_ADAPT_TEXT N_("Disable adaptive B-frames.")
138 #define NO_B_ADAPT_LONGTEXT N_( "If this is on, the specified number of consecutive B-frames "\
139     "will always be used, except possibly before an I-frame. " )
140
141 #define B_BIAS_TEXT N_("Bias the choice to use B-frames.")
142 #define B_BIAS_LONGTEXT N_( "Positive values cause more B-frames, negative values cause less B-frames. " )
143
144 #define CRF_TEXT N_("CRF (1-pass Quality-based VBR (nominal QP)).")
145 #define CRF_LONGTEXT N_( "CRF (1-pass Quality-based VBR (nominal QP))." )
146
147 #define TRELLIS_TEXT N_("Trellis RD quantization.")
148 #define TRELLIS_LONGTEXT N_( "Trellis RD quantization. Requires CABAC. \n" \
149      " - 0: disabled\n" \
150      " - 1: enabled only on the final encode of a MB\n" \
151      " - 2: enabled on all mode decisions\n" )
152
153 #define MIXED_REFS_TEXT N_("Decide references on a per partition basis.")
154 #define MIXED_REFS_LONGTEXT N_( "Decide references on a per partition basis." )
155
156 #define B_RDO_TEXT N_("RD based mode decision for B-frames.")
157 #define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. Requires subme 6." )
158
159 #define NO_FAST_PSKIP_TEXT N_("Disable early SKIP detection on P-frames.")
160 #define NO_FAST_PSKIP_LONGTEXT N_( "Disable early SKIP detection on P-frames.")
161
162 #define WEIGHTB_TEXT N_("Weighted prediction for B-frames.")
163 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
164
165 #if X264_BUILD >= 23
166 static char *enc_me_list[] =
167   { "", "dia", "hex", "umh", "esa" };
168 static char *enc_me_list_text[] =
169   { N_("default"), N_("dia"), N_("hex"), N_("umh"), N_("esa") };
170 #endif
171
172 static char *enc_analyse_list[] =
173   { "", "all", "normal", "fast", "none" };
174 static char *enc_analyse_list_text[] =
175   { N_("default"), N_("all"), N_("slow"), N_("normal"),
176     N_("fast"), N_("none") };
177
178 vlc_module_begin();
179     set_description( _("H264 encoder (using x264 library)"));
180     set_capability( "encoder", 200 );
181     set_callbacks( Open, Close );
182     set_category( CAT_INPUT );
183     set_subcategory( SUBCAT_INPUT_VCODEC );
184
185     add_integer( SOUT_CFG_PREFIX "qp", 0, NULL, QP_TEXT, QP_LONGTEXT,
186                  VLC_FALSE );
187         change_integer_range( 0, 51 );
188     add_integer( SOUT_CFG_PREFIX "qp-min", 10, NULL, QPMIN_TEXT,
189                  QPMIN_LONGTEXT, VLC_FALSE );
190         change_integer_range( 0, 51 );
191     add_integer( SOUT_CFG_PREFIX "qp-max", 51, NULL, QPMAX_TEXT,
192                  QPMAX_LONGTEXT, VLC_FALSE );
193         change_integer_range( 0, 51 );
194
195     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
196               VLC_FALSE );
197
198     add_bool( SOUT_CFG_PREFIX "loopfilter", 1, NULL, LOOPF_TEXT,
199               LOOPF_LONGTEXT, VLC_FALSE );
200
201     add_string( SOUT_CFG_PREFIX "analyse", "", NULL, ANALYSE_TEXT,
202                 ANALYSE_LONGTEXT, VLC_FALSE );
203         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
204
205     add_float( SOUT_CFG_PREFIX "tolerance", 1.0, NULL, TOLERANCE_TEXT,
206                TOLERANCE_LONGTEXT, VLC_FALSE );
207         change_float_range( 0, 100 );
208
209     add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
210                  VBV_MAXRATE_LONGTEXT, VLC_FALSE );
211
212     add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
213                  VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
214
215     add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
216                VBV_INIT_LONGTEXT, VLC_FALSE );
217         change_float_range( 0, 1 );
218
219     add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
220                  KEYINT_LONGTEXT, VLC_FALSE );
221
222     add_integer( SOUT_CFG_PREFIX "keyint-min", 0, NULL, KEYINT_MIN_TEXT,
223                  KEYINT_MIN_LONGTEXT, VLC_FALSE );
224
225     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
226                  BFRAMES_LONGTEXT, VLC_FALSE );
227         change_integer_range( 0, 16 );
228
229     add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
230               BPYRAMID_LONGTEXT, VLC_FALSE );
231
232     add_integer( SOUT_CFG_PREFIX "frameref", 1, NULL, FRAMEREF_TEXT,
233                  FRAMEREF_LONGTEXT, VLC_FALSE );
234         change_integer_range( 1, 15 );
235
236     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
237                  SCENE_LONGTEXT, VLC_FALSE );
238         change_integer_range( -1, 100 );
239
240 #if X264_BUILD >= 30
241     add_integer( SOUT_CFG_PREFIX "subpel", 6, NULL, SUBPEL_TEXT,
242                  SUBPEL_LONGTEXT, VLC_FALSE );
243         change_integer_range( 1, 6 );
244 #else
245     add_integer( SOUT_CFG_PREFIX "subpel", 5, NULL, SUBPEL_TEXT,
246                  SUBPEL_LONGTEXT, VLC_FALSE );
247         change_integer_range( 1, 5 );
248 #endif
249
250 #if X264_BUILD >= 23 /* r221 */
251     add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
252                 ME_LONGTEXT, VLC_FALSE );
253         change_string_list( enc_me_list, enc_me_list_text, 0 );
254
255     add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
256                  MERANGE_LONGTEXT, VLC_FALSE );
257         change_integer_range( 1, 64 );
258 #endif
259
260 #if X264_BUILD >= 0x000a /* r44 */
261     add_bool( SOUT_CFG_PREFIX "no-psnr", 0, NULL, NO_PSNR_TEXT,
262               NO_PSNR_LONGTEXT, VLC_FALSE );
263 #endif
264
265 #if X264_BUILD >= 0x0012 /* r134 */
266     add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
267               WEIGHTB_LONGTEXT, VLC_FALSE );
268 #endif
269
270 #if X264_BUILD >= 0x0013 /* r137 */
271     add_bool( SOUT_CFG_PREFIX "no-b-adapt", 0, NULL, NO_B_ADAPT_TEXT,
272               NO_B_ADAPT_LONGTEXT, VLC_FALSE );
273
274     add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
275                  B_BIAS_LONGTEXT, VLC_FALSE );
276         change_integer_range( -100, 100 );
277 #endif
278
279 #if X264_BUILD >= 36 /* r318 */
280     add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
281               MIXED_REFS_LONGTEXT, VLC_FALSE );
282 #endif
283
284 #if X264_BUILD >= 37 /* r334 */
285     add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
286                  CRF_LONGTEXT, VLC_FALSE );
287         change_integer_range( 0, 51 );
288 #endif
289
290 #if X264_BUILD >= 39 /* r360 */
291     add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
292                  TRELLIS_LONGTEXT, VLC_FALSE );
293         change_integer_range( 0, 2 );
294 #endif
295
296 #if X264_BUILD >= 40 /* r368 */
297     add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
298               B_RDO_LONGTEXT, VLC_FALSE );
299 #endif
300
301 #if X264_BUILD >= 42 /* r384 */
302     add_bool( SOUT_CFG_PREFIX "no-fast-pskip", 0, NULL, NO_FAST_PSKIP_TEXT,
303               NO_FAST_PSKIP_LONGTEXT, VLC_FALSE );
304 #endif
305
306 vlc_module_end();
307
308 /*****************************************************************************
309  * Local prototypes
310  *****************************************************************************/
311 static const char *ppsz_sout_options[] = {
312     "analyse", "bframes", "bpyramid", "b-bias", "b-rdo", "cabac",
313     "crf", "frameref", "keyint", "keyint-min", "loopfilter", "me",
314     "merange", "mixed-refs", "no-b-adapt", "no-fast-pskip", "no-psnr",
315     "qp", "qp-min", "qp-max", "scenecut", "subpel", "tolerance",
316     "trellis", "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb" , NULL
317 };
318
319 static block_t *Encode( encoder_t *, picture_t * );
320
321 struct encoder_sys_t
322 {
323     x264_t          *h;
324     x264_param_t    param;
325
326     int             i_buffer;
327     uint8_t         *p_buffer;
328
329     mtime_t         i_last_ref_pts;
330 };
331
332 /*****************************************************************************
333  * Open: probe the encoder
334  *****************************************************************************/
335 static int  Open ( vlc_object_t *p_this )
336 {
337     encoder_t     *p_enc = (encoder_t *)p_this;
338     encoder_sys_t *p_sys;
339     vlc_value_t    val;
340     int i_qmin = 0, i_qmax = 0;
341
342     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
343         !p_enc->b_force )
344     {
345         return VLC_EGENERIC;
346     }
347
348 #if X264_BUILD < 37
349     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
350         p_enc->fmt_in.video.i_height % 16!= 0 )
351     {
352         msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
353                   p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
354
355         if( p_enc->fmt_in.video.i_width < 16 ||
356             p_enc->fmt_in.video.i_height < 16 )
357         {
358             msg_Err( p_enc, "video is too small to be cropped" );
359             return VLC_EGENERIC;
360         }
361
362         msg_Warn( p_enc, "cropping video to %ix%i",
363                   p_enc->fmt_in.video.i_width >> 4 << 4,
364                   p_enc->fmt_in.video.i_height >> 4 << 4 );
365     }
366 #endif
367
368     sout_CfgParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
369
370     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
371     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
372
373     p_enc->pf_encode_video = Encode;
374     p_enc->pf_encode_audio = NULL;
375     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
376     p_sys->i_last_ref_pts = 0;
377
378     x264_param_default( &p_sys->param );
379     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
380     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
381 #if X264_BUILD < 37
382     p_sys->param.i_width  = p_sys->param.i_width >> 4 << 4;
383     p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
384 #endif
385
386     var_Get( p_enc, SOUT_CFG_PREFIX "qp-min", &val );
387     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmin = val.i_int;
388     var_Get( p_enc, SOUT_CFG_PREFIX "qp-max", &val );
389     if( val.i_int >= 1 && val.i_int <= 51 ) i_qmax = val.i_int;
390
391     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
392     if( val.i_int >= 1 && val.i_int <= 51 )
393     {
394         if( i_qmin > val.i_int ) i_qmin = val.i_int;
395         if( i_qmax < val.i_int ) i_qmax = val.i_int;
396
397 #if X264_BUILD >= 0x000a
398         p_sys->param.rc.i_qp_constant = val.i_int;
399         p_sys->param.rc.i_qp_min = i_qmin;
400         p_sys->param.rc.i_qp_max = i_qmax;
401 #else
402         p_sys->param.i_qp_constant = val.i_int;
403 #endif
404     }
405     else
406     {
407         /* No QP -> constant bitrate */
408 #if X264_BUILD >= 0x000a
409         p_sys->param.rc.b_cbr = 1;
410         p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
411
412 #if X264_BUILD >= 24
413         var_Get( p_enc, SOUT_CFG_PREFIX "tolerance", &val );
414         p_sys->param.rc.f_rate_tolerance = val.f_float;
415
416         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
417         p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
418
419         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
420         p_sys->param.rc.i_vbv_buffer_size = val.i_int;
421         if( !val.i_int )
422             p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate;
423
424         var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
425         p_sys->param.rc.f_vbv_buffer_init = val.f_float;
426 #else
427         p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
428         p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
429 #endif
430 #endif
431     }
432
433     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
434     p_sys->param.b_cabac = val.b_bool;
435
436     var_Get( p_enc, SOUT_CFG_PREFIX "loopfilter", &val );
437     p_sys->param.b_deblocking_filter = val.b_bool;
438
439     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
440 #if X264_BUILD >= 0x000e
441     if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
442     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int * 0.4;
443 #else
444     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
445 #endif
446
447     var_Get( p_enc, SOUT_CFG_PREFIX "keyint-min", &val );
448 #if X264_BUILD >= 0x000e
449     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
450 #else
451     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
452 #endif
453
454     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
455     if( val.i_int >= 0 && val.i_int <= 16 ) p_sys->param.i_bframe = val.i_int;
456
457 #if X264_BUILD >= 22
458     var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
459     p_sys->param.b_bframe_pyramid = val.b_bool;
460 #endif
461
462     var_Get( p_enc, SOUT_CFG_PREFIX "frameref", &val );
463     if( val.i_int > 0 && val.i_int <= 15 )
464         p_sys->param.i_frame_reference = val.i_int;
465
466     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
467 #if X264_BUILD >= 0x000b
468     if( val.i_int >= -1 && val.i_int <= 100 )
469         p_sys->param.i_scenecut_threshold = val.i_int;
470 #endif
471
472 #if X264_BUILD >= 22
473     var_Get( p_enc, SOUT_CFG_PREFIX "subpel", &val );
474 #if X264_BUILD >= 30
475     if( val.i_int >= 1 && val.i_int <= 6 )
476 #else
477     if( val.i_int >= 1 && val.i_int <= 5 )
478 #endif
479         p_sys->param.analyse.i_subpel_refine = val.i_int;
480 #endif
481
482 #if X264_BUILD >= 23
483     var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
484     if( !strcmp( val.psz_string, "dia" ) )
485     {
486         p_sys->param.analyse.i_me_method = X264_ME_DIA;
487     }
488     else if( !strcmp( val.psz_string, "hex" ) )
489     {
490         p_sys->param.analyse.i_me_method = X264_ME_HEX;
491     }
492     else if( !strcmp( val.psz_string, "umh" ) )
493     {
494         p_sys->param.analyse.i_me_method = X264_ME_UMH;
495     }
496     else if( !strcmp( val.psz_string, "esa" ) )
497     {
498         p_sys->param.analyse.i_me_method = X264_ME_ESA;
499     }
500     if( val.psz_string ) free( val.psz_string );
501
502     var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
503     if( val.i_int >= 1 && val.i_int <= 64 ) p_sys->param.analyse.i_me_range = val.i_int;
504 #endif
505
506     var_Get( p_enc, SOUT_CFG_PREFIX "no-psnr", &val );
507     p_sys->param.analyse.b_psnr = ! val.b_bool;
508
509 #if X264_BUILD >= 0x0012
510     var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
511     p_sys->param.analyse.b_weighted_bipred = val.b_bool;
512 #endif
513
514 #if X264_BUILD >= 0x0013
515     var_Get( p_enc, SOUT_CFG_PREFIX "no-b-adapt", &val );
516     p_sys->param.b_bframe_adaptive = ! val.b_bool;
517
518     var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
519     if( val.i_int >= -100 && val.i_int <= 100 )
520         p_sys->param.i_bframe_bias = val.i_int;
521 #endif
522
523 #if X264_BUILD >= 36
524     var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
525     p_sys->param.analyse.b_mixed_references = val.b_bool;
526 #endif
527
528 #if X264_BUILD >= 37
529     var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val );
530     if( val.i_int >= 0 && val.i_int <= 51 )
531         p_sys->param.rc.i_rf_constant = val.i_int;
532 #endif
533
534 #if X264_BUILD >= 39
535     var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
536     if( val.i_int >= 0 && val.i_int <= 2 )
537         p_sys->param.analyse.i_trellis = val.i_int;
538 #endif  
539
540 #if X264_BUILD >= 40
541     var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
542     p_sys->param.analyse.b_bframe_rdo = val.b_bool;
543 #endif
544
545 #if X264_BUILD >= 42
546     var_Get( p_enc, SOUT_CFG_PREFIX "no-fast-pskip", &val );
547     p_sys->param.analyse.b_fast_pskip = ! val.b_bool;
548 #endif
549
550 #ifndef X264_ANALYSE_BSUB16x16
551 #   define X264_ANALYSE_BSUB16x16 0
552 #endif
553     var_Get( p_enc, SOUT_CFG_PREFIX "analyse", &val );
554     if( !strcmp( val.psz_string, "none" ) )
555     {
556         p_sys->param.analyse.inter = 0;
557     }
558     else if( !strcmp( val.psz_string, "fast" ) )
559     {
560         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
561     }
562     else if( !strcmp( val.psz_string, "normal" ) )
563     {
564         p_sys->param.analyse.inter =
565             X264_ANALYSE_I4x4 | X264_ANALYSE_PSUB16x16;
566     }
567     else if( !strcmp( val.psz_string, "slow" ) )
568     {
569         p_sys->param.analyse.inter =
570             X264_ANALYSE_I4x4 |
571             X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8 |
572             X264_ANALYSE_BSUB16x16;
573     }
574     else if( !strcmp( val.psz_string, "all" ) )
575     {
576         p_sys->param.analyse.inter =
577             X264_ANALYSE_I4x4 |
578             X264_ANALYSE_PSUB16x16 | X264_ANALYSE_PSUB8x8 |
579             X264_ANALYSE_BSUB16x16;
580 #ifdef X264_ANALYSE_I8x8
581         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
582         p_sys->param.analyse.b_transform_8x8 = 1;
583 #endif
584     }
585     if( val.psz_string ) free( val.psz_string );
586
587     if( p_enc->fmt_in.video.i_aspect > 0 )
588     {
589         int64_t i_num, i_den;
590         int i_dst_num, i_dst_den;
591
592         i_num = p_enc->fmt_in.video.i_aspect *
593             (int64_t)p_enc->fmt_in.video.i_height;
594         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
595         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
596
597         p_sys->param.vui.i_sar_width = i_dst_num;
598         p_sys->param.vui.i_sar_height = i_dst_den;
599     }
600     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
601     {
602         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
603         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
604     }
605     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMX) )
606     {
607         p_sys->param.cpu &= ~X264_CPU_MMX;
608     }
609     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_MMXEXT) )
610     {
611         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
612     }
613     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE) )
614     {
615         p_sys->param.cpu &= ~X264_CPU_SSE;
616     }
617     if( !(p_enc->p_libvlc->i_cpu & CPU_CAPABILITY_SSE2) )
618     {
619         p_sys->param.cpu &= ~X264_CPU_SSE2;
620     }
621
622 #if X264_BUILD >= 29
623     if( p_enc->i_threads >= 1 )
624         p_sys->param.i_threads = p_enc->i_threads;
625 #endif
626
627     /* Open the encoder */
628     p_sys->h = x264_encoder_open( &p_sys->param );
629
630     /* alloc mem */
631     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
632         p_enc->fmt_in.video.i_height + 1000;
633     p_sys->p_buffer = malloc( p_sys->i_buffer );
634
635     /* get the globals headers */
636     p_enc->fmt_out.i_extra = 0;
637     p_enc->fmt_out.p_extra = NULL;
638
639 #if 0
640     x264_encoder_headers( p_sys->h, &nal, &i_nal );
641     for( i = 0; i < i_nal; i++ )
642     {
643         int i_size = p_sys->i_buffer;
644
645         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
646
647         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
648
649         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
650                 p_sys->p_buffer, i_size );
651
652         p_enc->fmt_out.i_extra += i_size;
653     }
654 #endif
655
656     return VLC_SUCCESS;
657 }
658
659 /****************************************************************************
660  * Encode:
661  ****************************************************************************/
662 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
663 {
664     encoder_sys_t *p_sys = p_enc->p_sys;
665     x264_picture_t pic;
666     x264_nal_t *nal;
667     block_t *p_block;
668     int i_nal, i_out, i;
669
670     /* init pic */
671     memset( &pic, 0, sizeof( x264_picture_t ) );
672     pic.i_pts = p_pict->date;
673     pic.img.i_csp = X264_CSP_I420;
674     pic.img.i_plane = p_pict->i_planes;
675     for( i = 0; i < p_pict->i_planes; i++ )
676     {
677         pic.img.plane[i] = p_pict->p[i].p_pixels;
678         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
679     }
680
681 #if X264_BUILD >= 0x0013
682     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
683 #else
684     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
685 #endif
686
687     if( !i_nal ) return NULL;
688
689     for( i = 0, i_out = 0; i < i_nal; i++ )
690     {
691         int i_size = p_sys->i_buffer - i_out;
692         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
693
694         i_out += i_size;
695     }
696
697     p_block = block_New( p_enc, i_out );
698     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
699
700     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
701         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
702     else if( pic.i_type == X264_TYPE_P )
703         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
704     else if( pic.i_type == X264_TYPE_B )
705         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
706
707     /* This isn't really valid for streams with B-frames */
708     p_block->i_length = I64C(1000000) *
709         p_enc->fmt_in.video.i_frame_rate_base /
710             p_enc->fmt_in.video.i_frame_rate;
711
712     p_block->i_dts = p_block->i_pts = pic.i_pts;
713
714     if( p_sys->param.i_bframe > 0 )
715     {
716         if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
717         {
718             p_block->i_dts = p_block->i_pts;
719         }
720         else
721         {
722             if( p_sys->i_last_ref_pts )
723             {
724                 p_block->i_dts = p_sys->i_last_ref_pts;
725             }
726             else
727             {
728                 /* Let's put something sensible */
729                 p_block->i_dts = p_block->i_pts;
730             }
731
732             p_sys->i_last_ref_pts = p_block->i_pts;
733         }
734     }
735
736     return p_block;
737 }
738
739 /*****************************************************************************
740  * CloseEncoder: x264 encoder destruction
741  *****************************************************************************/
742 static void Close( vlc_object_t *p_this )
743 {
744     encoder_t     *p_enc = (encoder_t *)p_this;
745     encoder_sys_t *p_sys = p_enc->p_sys;
746
747     x264_encoder_close( p_sys->h );
748     free( p_sys->p_buffer );
749     free( p_sys );
750 }