]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
sync to x264 r591
[vlc] / modules / codec / x264.c
1 /*****************************************************************************
2  * x264.c: h264 video encoder
3  *****************************************************************************
4  * Copyright (C) 2004-2006 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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 /* Frame-type options */
43
44 #define KEYINT_TEXT N_("Maximum GOP size")
45 #define KEYINT_LONGTEXT N_( "Sets maximum interval between IDR-frames." \
46     "Larger values save bits, thus improving quality for a given bitrate at " \
47     "the cost of seeking precision." )
48
49 #define MIN_KEYINT_TEXT N_("Minimum GOP size")
50 #define MIN_KEYINT_LONGTEXT N_( "Sets minimum interval between IDR-frames. " \
51     "In H.264, I-frames do not necessarily bound a closed GOP because it is " \
52     "allowable for a P-frame to be predicted from more frames than just the " \
53     "one frame before it (also see reference frame option). Therefore, " \
54     "I-frames are not necessarily seekable. IDR-frames restrict subsequent " \
55     "P-frames from referring to any frame prior to the IDR-frame. \n" \
56     "If scenecuts appear within this interval, they are still encoded as " \
57     "I-frames, but do not start a new GOP." )
58
59 #define SCENE_TEXT N_("Extra I-frames aggressivity" )
60 #define SCENE_LONGTEXT N_( "Scene-cut detection. Controls how " \
61     "aggressively to insert extra I-frames. With small values of " \
62     "scenecut, the codec often has " \
63     "to force an I-frame when it would exceed keyint. " \
64     "Good values of scenecut may find a better location for the " \
65     "I-frame. Large values use more I-frames " \
66     "than necessary, thus wasting bits. -1 disables scene-cut detection, so " \
67     "I-frames are inserted only every other keyint frames, which probably " \
68     "leads to ugly encoding artifacts. Range 1 to 100." )
69
70 #define BFRAMES_TEXT N_("B-frames between I and P")
71 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-frames between I and " \
72     "P-frames. Range 1 to 16." )
73
74 #define B_ADAPT_TEXT N_("Adaptive B-frame decision")
75 #define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
76     "consecutive B-frames to be used, except possibly before an I-frame." )
77
78 #define B_BIAS_TEXT N_("B-frames usage")
79 #define B_BIAS_LONGTEXT N_( "Bias the choice to use B-frames. Positive values " \
80     "cause more B-frames, negative values cause less B-frames." )
81
82 #define BPYRAMID_TEXT N_("Keep some B-frames as references")
83 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
84     "predicting other frames. Keeps the middle of 2+ consecutive B-frames " \
85     "as a reference, and reorders frame appropriately." )
86
87 #define CABAC_TEXT N_("CABAC")
88 #define CABAC_LONGTEXT N_( "CABAC (Context-Adaptive Binary Arithmetic "\
89     "Coding). Slightly slows down encoding and decoding, but should save " \
90     "10 to 15% bitrate." )
91
92 #define REF_TEXT N_("Number of reference frames")
93 #define REF_LONGTEXT N_( "Number of previous frames used as predictors. " \
94     "This is effective in Anime, but seems to make little difference in " \
95     "live-action source material. Some decoders are unable to deal with " \
96     "large frameref values. Range 1 to 16." )
97
98 #define NF_TEXT N_("Skip loop filter")
99 #define NF_LONGTEXT N_( "Deactivate the deblocking loop filter (decreases quality).")
100
101 #define FILTER_TEXT N_("Loop filter AlphaC0 and Beta parameters alpha:beta")
102 #define FILTER_LONGTEXT N_( "Loop filter AlphaC0 and Beta parameters. " \
103     "Range -6 to 6 for both alpha and beta parameters. -6 means light " \
104     "filter, 6 means strong.")
105     
106 #define LEVEL_TEXT N_("H.264 level")
107 #define LEVEL_LONGTEXT N_( "Specify H.264 level (as defined by Annex A " \
108     "of the standard). Levels are not enforced; it's up to the user to select " \
109     "a level compatible with the rest of the encoding options. Range 1 to 5.1 " \
110     "(10 to 51 is also allowed).")
111
112 /* In order to play an interlaced output stream encoded by x264, a decoder needs
113    mbaff support. r570 is using the 'mb' part and not 'aff' yet; so it's really
114    'pure-interlaced' mode */
115 #if X264_BUILD >= 51 /* r570 */
116 #define INTERLACED_TEXT N_("Interlaced mode")
117 #define INTERLACED_LONGTEXT N_( "Pure-interlaced mode.")
118 #endif
119
120 /* Ratecontrol */
121
122 #define QP_TEXT N_("Set QP")
123 #define QP_LONGTEXT N_( "This selects the quantizer to use. " \
124     "Lower values result in better fidelity, but higher bitrates. 26 is a " \
125     "good default value. Range 0 (lossless) to 51." )
126
127 #define CRF_TEXT N_("Quality-based VBR")
128 #define CRF_LONGTEXT N_( "1-pass Quality-based VBR. Range 0 to 51." )
129
130 #define QPMIN_TEXT N_("Min QP")
131 #define QPMIN_LONGTEXT N_( "Minimum quantizer parameter. 15 to 35 seems to " \
132     "be a useful range." )
133
134 #define QPMAX_TEXT N_("Max QP")
135 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
136
137 #define QPSTEP_TEXT N_("Max QP step")
138 #define QPSTEP_LONGTEXT N_( "Max QP step between frames.")
139
140 #define RATETOL_TEXT N_("Average bitrate tolerance")
141 #define RATETOL_LONGTEXT N_( "Allowed variance in average " \
142     "bitrate (in kbits/s).")
143
144 #define VBV_MAXRATE_TEXT N_("Max local bitrate")
145 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate (in kbits/s).")
146
147 #define VBV_BUFSIZE_TEXT N_("VBV buffer")
148 #define VBV_BUFSIZE_LONGTEXT N_( "Averaging period for the maximum " \
149     "local bitrate (in kbits).")
150
151 #define VBV_INIT_TEXT N_("Initial VBV buffer occupancy")
152 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
153     "fraction of the buffer size. Range 0.0 to 1.0.")
154
155 /* IP Ratio < 1 is technically valid but should never improve quality */
156 #define IPRATIO_TEXT N_("QP factor between I and P")
157 #define IPRATIO_LONGTEXT N_( "QP factor between I and P. Range 1.0 to 2.0.")
158
159 /* PB ratio < 1 is not valid and breaks ratecontrol */
160 #define PBRATIO_TEXT N_("QP factor between P and B")
161 #define PBRATIO_LONGTEXT N_( "QP factor between P and B. Range 1.0 to 2.0.")
162
163 #define CHROMA_QP_OFFSET_TEXT N_("QP difference between chroma and luma")
164 #define CHROMA_QP_OFFSET_LONGTEXT N_( "QP difference between chroma and luma.")
165
166 #define QCOMP_TEXT N_("QP curve compression")
167 #define QCOMP_LONGTEXT N_( "QP curve compression. Range 0.0 (CBR) to 1.0 (QCP).")
168
169 #define CPLXBLUR_TEXT N_("Reduce fluctuations in QP")
170 #define CPLXBLUR_LONGTEXT N_( "This reduces the fluctuations in QP " \
171     "before curve compression. Temporally blurs complexity.")
172
173 #define QBLUR_TEXT N_("Reduce fluctuations in QP")
174 #define QBLUR_LONGTEXT N_( "This reduces the fluctations in QP " \
175     "after curve compression. Temporally blurs quants.")
176
177 /* Analysis */
178
179 #define ANALYSE_TEXT N_("Partitions to consider")
180 #define ANALYSE_LONGTEXT N_( "Partitions to consider in analyse mode: \n" \
181     " - none  : \n" \
182     " - fast  : i4x4\n" \
183     " - normal: i4x4,p8x8,(i8x8)\n" \
184     " - slow  : i4x4,p8x8,(i8x8),b8x8\n" \
185     " - all   : i4x4,p8x8,(i8x8),b8x8,p4x4\n" \
186     "(p4x4 requires p8x8. i8x8 requires 8x8dct).")
187
188 #define DIRECT_PRED_TEXT N_("Direct MV prediction mode")
189 #define DIRECT_PRED_LONGTEXT N_( "Direct MV prediction mode.")
190
191 #if X264_BUILD >= 52 /* r573 */
192 #define DIRECT_PRED_SIZE_TEXT N_("Direct prediction size")
193 #define DIRECT_PRED_SIZE_LONGTEXT N_( "Direct prediction size: "\
194     " -  0: 4x4\n" \
195     " -  1: 8x8\n" \
196     " - -1: smallest possible according to level\n" )
197 #endif
198
199 #define WEIGHTB_TEXT N_("Weighted prediction for B-frames")
200 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
201
202 #define ME_TEXT N_("Integer pixel motion estimation method")
203 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
204     " - dia: diamond search, radius 1 (fast)\n" \
205     " - hex: hexagonal search, radius 2\n" \
206     " - umh: uneven multi-hexagon search (better but slower)\n" \
207     " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
208
209 #define MERANGE_TEXT N_("Maximum motion vector search range")
210 #define MERANGE_LONGTEXT N_( "Maximum distance to search for " \
211     "motion estimation, measured from predicted position(s). " \
212     "Default of 16 is good for most footage, high motion sequences may " \
213     "benefit from settings between 24 and 32. Range 0 to 64." )
214
215 #define SUBME_TEXT N_("Subpixel motion estimation and partition decision " \
216     "quality")
217 #if X264_BUILD >= 46 /* r477 */
218 #define SUBME_MAX 7
219 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
220     "tradeoffs involved in the motion estimation decision process " \
221     "(lower = quicker and higher = better quality). Range 1 to 7." )
222 #elif X264_BUILD >= 30 /* r262 */
223 #define SUBME_MAX 6
224 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
225     "tradeoffs involved in the motion estimation decision process " \
226     "(lower = quicker and higher = better quality). Range 1 to 6." )
227 #else
228 #define SUBME_MAX 5
229 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
230     "tradeoffs involved in the motion estimation decision process " \
231     "(lower = quicker and higher = better quality). Range 1 to 5." )
232 #endif
233
234 #define B_RDO_TEXT N_("RD based mode decision for B-frames")
235 #define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. This " \
236     "requires subme 6 (or higher).")
237
238 #define MIXED_REFS_TEXT N_("Decide references on a per partition basis")
239 #define MIXED_REFS_LONGTEXT N_( "Allows each 8x8 or 16x8 partition to " \
240     "independently select a reference frame, as opposed to only one ref " \
241     "per macroblock." )
242
243 #define CHROMA_ME_TEXT N_("Ignore chroma in motion estimation")
244 #define CHROMA_ME_LONGTEXT N_( "Chroma ME for subpel and mode decision in " \
245     "P-frames.")
246
247 #define BIME_TEXT N_("Jointly optimize both MVs in B-frames")
248 #define BIME_LONGTEXT N_( "Joint bidirectional motion refinement.")
249
250 #define TRANSFORM_8X8DCT_TEXT N_("Adaptive spatial transform size")
251 #define TRANSFORM_8X8DCT_LONGTEXT N_( \
252     "SATD-based decision for 8x8 transform in inter-MBs.")
253
254 #define TRELLIS_TEXT N_("Trellis RD quantization" )
255 #define TRELLIS_LONGTEXT N_( "Trellis RD quantization: \n" \
256     " - 0: disabled\n" \
257     " - 1: enabled only on the final encode of a MB\n" \
258     " - 2: enabled on all mode decisions\n" \
259     "This requires CABAC." )
260
261 #define FAST_PSKIP_TEXT N_("Early SKIP detection on P-frames")
262 #define FAST_PSKIP_LONGTEXT N_( "Early SKIP detection on P-frames.")
263
264 #define DCT_DECIMATE_TEXT N_("Coefficient thresholding on P-frames")
265 #define DCT_DECIMATE_LONGTEXT N_( "Coefficient thresholding on P-frames." \
266     "Eliminate dct blocks containing only a small single coefficient.")
267
268 /* Noise reduction 1 is too weak to measure, suggest at least 10 */
269 #define NR_TEXT N_("Noise reduction")
270 #define NR_LONGTEXT N_( "Dct-domain noise reduction. Adaptive pseudo-deadzone. " \
271     "10 to 1000 seems to be a useful range." )
272
273 #if X264_BUILD >= 52 /* r573 */
274 #define DEADZONE_INTER_TEXT N_("Inter luma quantization deadzone")
275 #define DEADZONE_INTER_LONGTEXT N_( "Set the size of the inter luma quantization deadzone. " \
276     "Range 0 to 32." )
277
278 #define DEADZONE_INTRA_TEXT N_("Intra luma quantization deadzone")
279 #define DEADZONE_INTRA_LONGTEXT N_( "Set the size of the intra luma quantization deadzone. " \
280     "Range 0 to 32." )
281 #endif
282
283 /* Input/Output */
284
285 #define ASM_TEXT N_("CPU optimizations")
286 #define ASM_LONGTEXT N_( "Use assembler CPU optimizations.")
287
288 #define PSNR_TEXT N_("PSNR computation")
289 #define PSNR_LONGTEXT N_( "Compute and print PSNR stats. This has no effect on " \
290     "the actual encoding quality." )
291
292 #define SSIM_TEXT N_("SSIM computation")
293 #define SSIM_LONGTEXT N_( "Compute and print SSIM stats. This has no effect on " \
294     "the actual encoding quality." )
295
296 #define QUIET_TEXT N_("Quiet mode")
297 #define QUIET_LONGTEXT N_( "Quiet mode.")
298
299 #define VERBOSE_TEXT N_("Statistics")
300 #define VERBOSE_LONGTEXT N_( "Print stats for each frame.")
301
302 #if X264_BUILD >= 47 /* r518 */
303 #define SPS_ID_TEXT N_("SPS and PPS id numbers")
304 #define SPS_ID_LONGTEXT N_( "Set SPS and PPS id numbers to allow concatenating " \
305     "streams with different settings.")
306 #endif
307
308 #define AUD_TEXT N_("Access unit delimiters")
309 #define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")
310
311 #if X264_BUILD >= 24
312 static char *enc_me_list[] =
313   { "", "dia", "hex", "umh", "esa" };
314 static char *enc_me_list_text[] =
315   { N_("default"), N_("dia"), N_("hex"), N_("umh"), N_("esa") };
316 #endif
317
318 static char *enc_analyse_list[] =
319   { "", "none", "fast", "normal", "slow", "all" };
320 static char *enc_analyse_list_text[] =
321   { N_("default"), N_("none"), N_("fast"), N_("normal"),
322     N_("slow"), N_("all") };
323
324 #if X264_BUILD >= 45 /* r457 */
325 static char *direct_pred_list[] =
326   { "", "none", "spatial", "temporal", "auto" };
327 static char *direct_pred_list_text[] =
328   { N_("default"), N_("none"), N_("spatial"), N_("temporal"), N_("auto") };
329 #else
330 static char *direct_pred_list[] =
331   { "", "none", "spatial", "temporal" };
332 static char *direct_pred_list_text[] =
333   { N_("default"), N_("none"), N_("spatial"), N_("temporal") };
334 #endif
335
336 vlc_module_begin();
337     set_description( _("H.264/MPEG4 AVC encoder (using x264 library)"));
338     set_capability( "encoder", 200 );
339     set_callbacks( Open, Close );
340     set_category( CAT_INPUT );
341     set_subcategory( SUBCAT_INPUT_VCODEC );
342
343 /* Frame-type options */
344
345     add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
346                  KEYINT_LONGTEXT, VLC_FALSE );
347
348     add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
349                  MIN_KEYINT_LONGTEXT, VLC_FALSE );
350         add_deprecated( SOUT_CFG_PREFIX "keyint-min", VLC_FALSE ); /* Deprecated since 0.8.5 */
351
352     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
353                  SCENE_LONGTEXT, VLC_FALSE );
354         change_integer_range( -1, 100 );
355
356     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
357                  BFRAMES_LONGTEXT, VLC_FALSE );
358         change_integer_range( 0, 16 );
359
360 #if X264_BUILD >= 0x0013 /* r137 */
361     add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
362               B_ADAPT_LONGTEXT, VLC_FALSE );
363
364     add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
365                  B_BIAS_LONGTEXT, VLC_FALSE );
366         change_integer_range( -100, 100 );
367 #endif
368
369     add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
370               BPYRAMID_LONGTEXT, VLC_FALSE );
371
372     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
373               VLC_FALSE );
374
375     add_integer( SOUT_CFG_PREFIX "ref", 1, NULL, REF_TEXT,
376                  REF_LONGTEXT, VLC_FALSE );
377         add_deprecated( SOUT_CFG_PREFIX "frameref", VLC_FALSE ); /* Deprecated since 0.8.5 */
378         change_integer_range( 1, 16 );
379
380     add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
381               NF_LONGTEXT, VLC_FALSE );
382         add_deprecated( SOUT_CFG_PREFIX "loopfilter", VLC_FALSE ); /* Deprecated since 0.8.5 */
383
384     add_string( SOUT_CFG_PREFIX "deblock", "", NULL, FILTER_TEXT,
385                  FILTER_LONGTEXT, VLC_FALSE );
386         add_deprecated( SOUT_CFG_PREFIX "filter", VLC_FALSE ); /* Deprecated since 0.8.6 */
387
388     add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
389                LEVEL_LONGTEXT, VLC_FALSE );
390
391 #if X264_BUILD >= 51 /* r570 */
392     add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
393               VLC_FALSE );
394 #endif
395
396 /* Ratecontrol */
397
398     add_integer( SOUT_CFG_PREFIX "qp", 26, NULL, QP_TEXT, QP_LONGTEXT,
399                  VLC_FALSE );
400         change_integer_range( 0, 51 ); /* QP 0 -> lossless encoding */
401
402 #if X264_BUILD >= 37 /* r334 */
403     add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
404                  CRF_LONGTEXT, VLC_FALSE );
405         change_integer_range( 0, 51 );
406 #endif
407
408     add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
409                  QPMIN_LONGTEXT, VLC_FALSE );
410         add_deprecated( SOUT_CFG_PREFIX "qp-min", VLC_FALSE ); /* Deprecated since 0.8.5 */
411         change_integer_range( 0, 51 );
412
413     add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
414                  QPMAX_LONGTEXT, VLC_FALSE );
415         add_deprecated( SOUT_CFG_PREFIX "qp-max", VLC_FALSE ); /* Deprecated since 0.8.5 */
416         change_integer_range( 0, 51 );
417
418     add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
419                  QPSTEP_LONGTEXT, VLC_FALSE );
420         change_integer_range( 0, 51 );
421
422     add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
423                RATETOL_LONGTEXT, VLC_FALSE );
424         add_deprecated( SOUT_CFG_PREFIX "tolerance", VLC_FALSE ); /* Deprecated since 0.8.5 */
425         change_float_range( 0, 100 );
426
427     add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
428                  VBV_MAXRATE_LONGTEXT, VLC_FALSE );
429
430     add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
431                  VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
432
433     add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
434                VBV_INIT_LONGTEXT, VLC_FALSE );
435         change_float_range( 0, 1 );
436
437     add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
438                IPRATIO_LONGTEXT, VLC_FALSE );
439         change_float_range( 1, 2 );
440
441     add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
442                PBRATIO_LONGTEXT, VLC_FALSE );
443         change_float_range( 1, 2 );
444
445 #if X264_BUILD >= 23 /* r190 */
446     add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
447                  CHROMA_QP_OFFSET_LONGTEXT, VLC_FALSE );
448 #endif
449
450     add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
451                QCOMP_LONGTEXT, VLC_FALSE );
452         change_float_range( 0, 1 );
453
454     add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
455                CPLXBLUR_LONGTEXT, VLC_FALSE );
456
457     add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
458                QBLUR_LONGTEXT, VLC_FALSE );
459
460 /* Analysis */
461
462     /* x264 partitions = none (default). set at least "normal" mode. */
463     add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
464                 ANALYSE_LONGTEXT, VLC_FALSE );
465         add_deprecated( SOUT_CFG_PREFIX "analyse", VLC_FALSE ); /* Deprecated since 0.8.6 */
466         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
467
468     add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
469                 DIRECT_PRED_LONGTEXT, VLC_FALSE );
470         change_string_list( direct_pred_list, direct_pred_list_text, 0 );
471
472 #if X264_BUILD >= 52 /* r573 */
473     add_integer( SOUT_CFG_PREFIX "direct-8x8", 0, NULL, DIRECT_PRED_SIZE_TEXT,
474                  DIRECT_PRED_SIZE_LONGTEXT, VLC_FALSE );
475         change_integer_range( -1, 1 );
476 #endif
477
478 #if X264_BUILD >= 0x0012 /* r134 */
479     add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
480               WEIGHTB_LONGTEXT, VLC_FALSE );
481 #endif
482
483 #if X264_BUILD >= 24 /* r221 */
484     add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
485                 ME_LONGTEXT, VLC_FALSE );
486         change_string_list( enc_me_list, enc_me_list_text, 0 );
487
488     add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
489                  MERANGE_LONGTEXT, VLC_FALSE );
490         change_integer_range( 1, 64 );
491 #endif
492
493     add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
494                  SUBME_LONGTEXT, VLC_FALSE );
495         add_deprecated( SOUT_CFG_PREFIX "subpel", VLC_FALSE ); /* Deprecated since 0.8.5 */
496         change_integer_range( 1, SUBME_MAX );
497
498 #if X264_BUILD >= 41 /* r368 */
499     add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
500               B_RDO_LONGTEXT, VLC_FALSE );
501 #endif
502
503 #if X264_BUILD >= 36 /* r318 */
504     add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
505               MIXED_REFS_LONGTEXT, VLC_FALSE );
506 #endif
507
508 #if X264_BUILD >= 23 /* r171 */
509     add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
510               CHROMA_ME_LONGTEXT, VLC_FALSE );
511 #endif
512
513 #if X264_BUILD >= 43 /* r390 */
514     add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
515               BIME_LONGTEXT, VLC_FALSE );
516 #endif
517
518 #if X264_BUILD >= 30 /* r251 */
519     add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
520               TRANSFORM_8X8DCT_LONGTEXT, VLC_FALSE );
521 #endif
522
523 #if X264_BUILD >= 39 /* r360 */
524     add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
525                  TRELLIS_LONGTEXT, VLC_FALSE );
526         change_integer_range( 0, 2 );
527 #endif
528
529 #if X264_BUILD >= 42 /* r384 */
530     add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
531               FAST_PSKIP_LONGTEXT, VLC_FALSE );
532 #endif
533
534 #if X264_BUILD >= 46 /* r503 */
535     add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
536               DCT_DECIMATE_LONGTEXT, VLC_FALSE );
537 #endif
538
539 #if X264_BUILD >= 44 /* r398 */
540     add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
541                  NR_LONGTEXT, VLC_FALSE );
542         change_integer_range( 0, 1000 );
543 #endif
544
545 #if X264_BUILD >= 52 /* r573 */
546     add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
547                  DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
548         change_integer_range( 0, 32 );
549
550     add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
551                  DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
552         change_integer_range( 0, 32 );
553 #endif
554
555 /* Input/Output */
556
557 #if defined(__DARWIN__) && defined(__INTEL__)
558     add_bool( SOUT_CFG_PREFIX "asm", 0, NULL, ASM_TEXT,
559               ASM_LONGTEXT, VLC_FALSE );
560 #else
561     add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
562               ASM_LONGTEXT, VLC_FALSE );
563 #endif
564
565     /* x264 psnr = 1 (default). disable PSNR computation for speed. */
566     add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
567               PSNR_LONGTEXT, VLC_FALSE );
568
569 #if X264_BUILD >= 50 /* r554 */
570     /* x264 ssim = 1 (default). disable SSIM computation for speed. */
571     add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
572               SSIM_LONGTEXT, VLC_FALSE );
573 #endif
574
575     add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
576               QUIET_LONGTEXT, VLC_FALSE );
577
578 #if X264_BUILD >= 47 /* r518 */
579     add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
580                  SPS_ID_LONGTEXT, VLC_FALSE );
581 #endif
582
583     add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
584               AUD_LONGTEXT, VLC_FALSE );
585
586 #if X264_BUILD >= 0x000e /* r81 */
587     add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
588               VERBOSE_LONGTEXT, VLC_FALSE );
589 #endif
590
591 vlc_module_end();
592
593 /*****************************************************************************
594  * Local prototypes
595  *****************************************************************************/
596 static const char *ppsz_sout_options[] = {
597     "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
598     "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
599     "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
600     "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
601     "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
602     "me", "merange", "min-keyint", "mixed-refs", "nf", "nr", "partitions",
603     "pbratio", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax", "qpmin",
604     "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut", "sps-id",
605     "ssim", "subme", "subpel", "tolerance", "trellis", "verbose",
606     "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", NULL
607 };
608
609 static block_t *Encode( encoder_t *, picture_t * );
610
611 struct encoder_sys_t
612 {
613     x264_t          *h;
614     x264_param_t    param;
615
616     int             i_buffer;
617     uint8_t         *p_buffer;
618
619     mtime_t         i_last_ref_pts;
620 };
621
622 /*****************************************************************************
623  * Open: probe the encoder
624  *****************************************************************************/
625 static int  Open ( vlc_object_t *p_this )
626 {
627     encoder_t     *p_enc = (encoder_t *)p_this;
628     encoder_sys_t *p_sys;
629     vlc_value_t    val;
630     int i_qmin = 0, i_qmax = 0;
631     x264_nal_t    *nal;
632     int i, i_nal;
633
634     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
635         !p_enc->b_force )
636     {
637         return VLC_EGENERIC;
638     }
639
640 #if X264_BUILD < 37
641     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
642         p_enc->fmt_in.video.i_height % 16 != 0 )
643     {
644         msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
645                   p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
646
647         if( p_enc->fmt_in.video.i_width < 16 ||
648             p_enc->fmt_in.video.i_height < 16 )
649         {
650             msg_Err( p_enc, "video is too small to be cropped" );
651             return VLC_EGENERIC;
652         }
653
654         msg_Warn( p_enc, "cropping video to %ix%i",
655                   p_enc->fmt_in.video.i_width >> 4 << 4,
656                   p_enc->fmt_in.video.i_height >> 4 << 4 );
657     }
658 #endif
659
660     config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
661
662     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
663     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
664
665     p_enc->pf_encode_video = Encode;
666     p_enc->pf_encode_audio = NULL;
667     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
668     p_sys->i_last_ref_pts = 0;
669
670     x264_param_default( &p_sys->param );
671     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
672     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
673 #if X264_BUILD < 37
674     p_sys->param.i_width  = p_sys->param.i_width >> 4 << 4;
675     p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
676 #endif
677
678     /* average bitrate specified by transcode vb */
679     p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
680
681 #if X264_BUILD < 48
682     /* cbr = 1 overrides qp or crf and sets an average bitrate
683        but maxrate = average bitrate is needed for "real" CBR */
684     if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.b_cbr = 1;
685 #else
686     if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.i_rc_method = X264_RC_ABR;
687 #endif
688
689     var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
690     if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
691     var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
692     if( val.i_int >= 0 && val.i_int <= 51 ) i_qmin = val.i_int;
693     var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
694     if( val.i_int >= 0 && val.i_int <= 51 ) i_qmax = val.i_int;
695
696     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
697     if( val.i_int >= 0 && val.i_int <= 51 )
698     {
699         if( i_qmin > val.i_int ) i_qmin = val.i_int;
700         if( i_qmax < val.i_int ) i_qmax = val.i_int;
701
702 #if X264_BUILD >= 0x000a
703         p_sys->param.rc.i_qp_constant = val.i_int;
704         p_sys->param.rc.i_qp_min = i_qmin;
705         p_sys->param.rc.i_qp_max = i_qmax;
706 #else
707         p_sys->param.i_qp_constant = val.i_int;
708 #endif
709     }
710
711 #if X264_BUILD >= 24
712     var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
713     p_sys->param.rc.f_rate_tolerance = val.f_float;
714
715     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
716     p_sys->param.rc.f_vbv_buffer_init = val.f_float;
717
718     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
719     p_sys->param.rc.i_vbv_buffer_size = val.i_int;
720
721     /* x264 vbv-bufsize = 0 (default). if not provided set period
722        in seconds for local maximum bitrate (cache/bufsize) based
723        on average bitrate. */
724     if( !val.i_int )
725         p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate * 2;
726
727     /* max bitrate = average bitrate -> CBR */
728     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
729     p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
730
731 #else
732     p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
733     p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
734 #endif
735
736     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
737     p_sys->param.b_cabac = val.b_bool;
738
739     /* disable deblocking when nf (no loop filter) is enabled */
740     var_Get( p_enc, SOUT_CFG_PREFIX "nf", &val );
741     p_sys->param.b_deblocking_filter = !val.b_bool;
742
743     var_Get( p_enc, SOUT_CFG_PREFIX "deblock", &val );
744     if( val.psz_string )
745     {
746         char *p = strchr( val.psz_string, ':' );
747         p_sys->param.i_deblocking_filter_alphac0 = atoi( val.psz_string );
748         p_sys->param.i_deblocking_filter_beta = p ? atoi( p+1 ) : p_sys->param.i_deblocking_filter_alphac0;
749         free( val.psz_string );
750     }
751
752     var_Get( p_enc, SOUT_CFG_PREFIX "level", &val );
753     if( val.psz_string )
754     {
755         if( atof (val.psz_string) < 6 )
756             p_sys->param.i_level_idc = (int) ( 10 * atof (val.psz_string) + .5);
757         else
758             p_sys->param.i_level_idc = atoi (val.psz_string);
759         free( val.psz_string );
760     }
761
762 #if X264_BUILD >= 51 /* r570 */
763     var_Get( p_enc, SOUT_CFG_PREFIX "interlaced", &val );
764     p_sys->param.b_interlaced = val.b_bool;
765 #endif
766
767     var_Get( p_enc, SOUT_CFG_PREFIX "ipratio", &val );
768     p_sys->param.rc.f_ip_factor = val.f_float;
769
770     var_Get( p_enc, SOUT_CFG_PREFIX "pbratio", &val );
771     p_sys->param.rc.f_pb_factor = val.f_float;
772
773     var_Get( p_enc, SOUT_CFG_PREFIX "qcomp", &val );
774     p_sys->param.rc.f_qcompress = val.f_float;
775
776     var_Get( p_enc, SOUT_CFG_PREFIX "cplxblur", &val );
777     p_sys->param.rc.f_complexity_blur = val.f_float;
778
779     var_Get( p_enc, SOUT_CFG_PREFIX "qblur", &val );
780     p_sys->param.rc.f_qblur = val.f_float;
781
782 #if X264_BUILD >= 0x000e
783     var_Get( p_enc, SOUT_CFG_PREFIX "verbose", &val );
784     if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_DEBUG;
785 #endif
786
787     var_Get( p_enc, SOUT_CFG_PREFIX "quiet", &val );
788     if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_NONE;
789
790 #if X264_BUILD >= 47 /* r518 */
791     var_Get( p_enc, SOUT_CFG_PREFIX "sps-id", &val );
792     if( val.i_int >= 0 ) p_sys->param.i_sps_id = val.i_int;
793 #endif
794
795     var_Get( p_enc, SOUT_CFG_PREFIX "aud", &val );
796     if( val.b_bool ) p_sys->param.b_aud = val.b_bool;
797
798     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
799 #if X264_BUILD >= 0x000e
800     if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
801 #else
802     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
803 #endif
804
805     var_Get( p_enc, SOUT_CFG_PREFIX "min-keyint", &val );
806 #if X264_BUILD >= 0x000e
807     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
808 #else
809     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
810 #endif
811
812     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
813     if( val.i_int >= 0 && val.i_int <= 16 )
814         p_sys->param.i_bframe = val.i_int;
815
816 #if X264_BUILD >= 22
817     var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
818     p_sys->param.b_bframe_pyramid = val.b_bool;
819 #endif
820
821     var_Get( p_enc, SOUT_CFG_PREFIX "ref", &val );
822     if( val.i_int > 0 && val.i_int <= 15 )
823         p_sys->param.i_frame_reference = val.i_int;
824
825     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
826 #if X264_BUILD >= 0x000b
827     if( val.i_int >= -1 && val.i_int <= 100 )
828         p_sys->param.i_scenecut_threshold = val.i_int;
829 #endif
830
831     var_Get( p_enc, SOUT_CFG_PREFIX "subme", &val );
832     if( val.i_int >= 1 && val.i_int <= SUBME_MAX )
833         p_sys->param.analyse.i_subpel_refine = val.i_int;
834
835 #if X264_BUILD >= 24
836     var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
837     if( !strcmp( val.psz_string, "dia" ) )
838     {
839         p_sys->param.analyse.i_me_method = X264_ME_DIA;
840     }
841     else if( !strcmp( val.psz_string, "hex" ) )
842     {
843         p_sys->param.analyse.i_me_method = X264_ME_HEX;
844     }
845     else if( !strcmp( val.psz_string, "umh" ) )
846     {
847         p_sys->param.analyse.i_me_method = X264_ME_UMH;
848     }
849     else if( !strcmp( val.psz_string, "esa" ) )
850     {
851         p_sys->param.analyse.i_me_method = X264_ME_ESA;
852     }
853     if( val.psz_string ) free( val.psz_string );
854
855     var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
856     if( val.i_int >= 1 && val.i_int <= 64 )
857         p_sys->param.analyse.i_me_range = val.i_int;
858 #endif
859
860     var_Get( p_enc, SOUT_CFG_PREFIX "direct", &val );
861     if( !strcmp( val.psz_string, "none" ) )
862     {
863         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
864     }
865     else if( !strcmp( val.psz_string, "spatial" ) )
866     {
867         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
868     }
869     else if( !strcmp( val.psz_string, "temporal" ) )
870     {
871         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
872     }
873 #if X264_BUILD >= 45 /* r457 */
874     else if( !strcmp( val.psz_string, "auto" ) )
875     {
876         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
877     }
878 #endif
879     if( val.psz_string ) free( val.psz_string );
880
881     var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
882     p_sys->param.analyse.b_psnr = val.b_bool;
883
884 #if X264_BUILD >= 50 /* r554 */
885     var_Get( p_enc, SOUT_CFG_PREFIX "ssim", &val );
886     p_sys->param.analyse.b_ssim = val.b_bool;
887 #endif
888
889 #if X264_BUILD >= 0x0012
890     var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
891     p_sys->param.analyse.b_weighted_bipred = val.b_bool;
892 #endif
893
894 #if X264_BUILD >= 0x0013
895     var_Get( p_enc, SOUT_CFG_PREFIX "b-adapt", &val );
896     p_sys->param.b_bframe_adaptive = val.b_bool;
897
898     var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
899     if( val.i_int >= -100 && val.i_int <= 100 )
900         p_sys->param.i_bframe_bias = val.i_int;
901 #endif
902
903 #if X264_BUILD >= 23
904     var_Get( p_enc, SOUT_CFG_PREFIX "chroma-me", &val );
905     p_sys->param.analyse.b_chroma_me = val.b_bool;
906     var_Get( p_enc, SOUT_CFG_PREFIX "chroma-qp-offset", &val );
907     p_sys->param.analyse.i_chroma_qp_offset = val.i_int;
908 #endif
909
910 #if X264_BUILD >= 36
911     var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
912     p_sys->param.analyse.b_mixed_references = val.b_bool;
913 #endif
914
915 #if X264_BUILD >= 37
916     var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val ); 
917     if( val.i_int > 0 && val.i_int <= 51 )
918     {
919 #if X264_BUILD >= 54
920         p_sys->param.rc.f_rf_constant = val.i_int;
921 #else
922         p_sys->param.rc.i_rf_constant = val.i_int;
923 #endif
924 #if X264_BUILD >= 48
925         p_sys->param.rc.i_rc_method = X264_RC_CRF;
926 #endif
927     }
928 #endif
929
930 #if X264_BUILD >= 39
931     var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
932     if( val.i_int >= 0 && val.i_int <= 2 )
933         p_sys->param.analyse.i_trellis = val.i_int;
934 #endif
935
936 #if X264_BUILD >= 41
937     var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
938     p_sys->param.analyse.b_bframe_rdo = val.b_bool;
939 #endif
940
941 #if X264_BUILD >= 42
942     var_Get( p_enc, SOUT_CFG_PREFIX "fast-pskip", &val );
943     p_sys->param.analyse.b_fast_pskip = val.b_bool;
944 #endif
945
946 #if X264_BUILD >= 43
947     var_Get( p_enc, SOUT_CFG_PREFIX "bime", &val );
948     p_sys->param.analyse.b_bidir_me = val.b_bool;
949 #endif
950
951 #if X264_BUILD >= 44
952     var_Get( p_enc, SOUT_CFG_PREFIX "nr", &val );
953     if( val.i_int >= 0 && val.i_int <= 1000 )
954         p_sys->param.analyse.i_noise_reduction = val.i_int;
955 #endif
956
957 #if X264_BUILD >= 46
958     var_Get( p_enc, SOUT_CFG_PREFIX "dct-decimate", &val );
959     p_sys->param.analyse.b_dct_decimate = val.b_bool;
960 #endif
961
962 #if X264_BUILD >= 52
963     var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-inter", &val );
964     if( val.i_int >= 0 && val.i_int <= 32 )
965         p_sys->param.analyse.i_luma_deadzone[0] = val.i_int;
966
967     var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-intra", &val );
968     if( val.i_int >= 0 && val.i_int <= 32 )
969         p_sys->param.analyse.i_luma_deadzone[1] = val.i_int;   
970
971     var_Get( p_enc, SOUT_CFG_PREFIX "direct-8x8", &val );
972     if( val.i_int >= -1 && val.i_int <= 1 )
973         p_sys->param.analyse.i_direct_8x8_inference = val.i_int; 
974 #endif
975
976     var_Get( p_enc, SOUT_CFG_PREFIX "asm", &val );
977     if( !val.b_bool ) p_sys->param.cpu = 0;
978
979 #ifndef X264_ANALYSE_BSUB16x16
980 #   define X264_ANALYSE_BSUB16x16 0
981 #endif
982     var_Get( p_enc, SOUT_CFG_PREFIX "partitions", &val );
983     if( !strcmp( val.psz_string, "none" ) )
984     {
985         p_sys->param.analyse.inter = 0;
986     }
987     else if( !strcmp( val.psz_string, "fast" ) )
988     {
989         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
990     }
991     else if( !strcmp( val.psz_string, "normal" ) )
992     {
993         p_sys->param.analyse.inter =
994             X264_ANALYSE_I4x4 |
995             X264_ANALYSE_PSUB16x16;
996 #ifdef X264_ANALYSE_I8x8
997         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
998 #endif
999     }
1000     else if( !strcmp( val.psz_string, "slow" ) )
1001     {
1002         p_sys->param.analyse.inter =
1003             X264_ANALYSE_I4x4 |
1004             X264_ANALYSE_PSUB16x16 |
1005             X264_ANALYSE_BSUB16x16;
1006 #ifdef X264_ANALYSE_I8x8
1007         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1008 #endif
1009     }
1010     else if( !strcmp( val.psz_string, "all" ) )
1011     {
1012         p_sys->param.analyse.inter =
1013             X264_ANALYSE_I4x4 |
1014             X264_ANALYSE_PSUB16x16 |
1015             X264_ANALYSE_BSUB16x16 |
1016             X264_ANALYSE_PSUB8x8;
1017 #ifdef X264_ANALYSE_I8x8
1018         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1019 #endif
1020     }
1021     if( val.psz_string ) free( val.psz_string );
1022
1023 #if X264_BUILD >= 30
1024     var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
1025     p_sys->param.analyse.b_transform_8x8 = val.b_bool;
1026 #endif
1027
1028     if( p_enc->fmt_in.video.i_aspect > 0 )
1029     {
1030         int64_t i_num, i_den;
1031         unsigned int i_dst_num, i_dst_den;
1032
1033         i_num = p_enc->fmt_in.video.i_aspect *
1034             (int64_t)p_enc->fmt_in.video.i_height;
1035         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
1036         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1037
1038         p_sys->param.vui.i_sar_width = i_dst_num;
1039         p_sys->param.vui.i_sar_height = i_dst_den;
1040     }
1041     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
1042     {
1043         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
1044         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
1045     }
1046     if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMX) )
1047     {
1048         p_sys->param.cpu &= ~X264_CPU_MMX;
1049     }
1050     if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMXEXT) )
1051     {
1052         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
1053     }
1054     if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_SSE) )
1055     {
1056         p_sys->param.cpu &= ~X264_CPU_SSE;
1057     }
1058     if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_SSE2) )
1059     {
1060         p_sys->param.cpu &= ~X264_CPU_SSE2;
1061     }
1062
1063 #if X264_BUILD >= 29
1064     /* As of r543 x264 will autodetect the number of cpus and will set
1065        the number of threads accordingly unless ofcourse the number of
1066        threads is explicitly specified... */
1067     if( p_enc->i_threads >= 1 )
1068         p_sys->param.i_threads = p_enc->i_threads;
1069 #endif
1070
1071     /* Open the encoder */
1072     p_sys->h = x264_encoder_open( &p_sys->param );
1073
1074     /* alloc mem */
1075     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
1076         p_enc->fmt_in.video.i_height + 1000;
1077     p_sys->p_buffer = malloc( p_sys->i_buffer );
1078
1079     /* get the globals headers */
1080     p_enc->fmt_out.i_extra = 0;
1081     p_enc->fmt_out.p_extra = NULL;
1082
1083     x264_encoder_headers( p_sys->h, &nal, &i_nal );
1084     for( i = 0; i < i_nal; i++ )
1085     {
1086         int i_size = p_sys->i_buffer;
1087
1088         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
1089
1090         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
1091
1092         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
1093                 p_sys->p_buffer, i_size );
1094
1095         p_enc->fmt_out.i_extra += i_size;
1096     }
1097
1098     return VLC_SUCCESS;
1099 }
1100
1101 /****************************************************************************
1102  * Encode:
1103  ****************************************************************************/
1104 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
1105 {
1106     encoder_sys_t *p_sys = p_enc->p_sys;
1107     x264_picture_t pic;
1108     x264_nal_t *nal;
1109     block_t *p_block;
1110     int i_nal, i_out, i;
1111
1112     /* init pic */
1113     memset( &pic, 0, sizeof( x264_picture_t ) );
1114     pic.i_pts = p_pict->date;
1115     pic.img.i_csp = X264_CSP_I420;
1116     pic.img.i_plane = p_pict->i_planes;
1117     for( i = 0; i < p_pict->i_planes; i++ )
1118     {
1119         pic.img.plane[i] = p_pict->p[i].p_pixels;
1120         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
1121     }
1122
1123 #if X264_BUILD >= 0x0013
1124     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
1125 #else
1126     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
1127 #endif
1128
1129     if( !i_nal ) return NULL;
1130
1131     for( i = 0, i_out = 0; i < i_nal; i++ )
1132     {
1133         int i_size = p_sys->i_buffer - i_out;
1134         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
1135
1136         i_out += i_size;
1137     }
1138
1139     p_block = block_New( p_enc, i_out );
1140     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
1141
1142     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
1143         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1144     else if( pic.i_type == X264_TYPE_P )
1145         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1146     else if( pic.i_type == X264_TYPE_B )
1147         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1148
1149     /* This isn't really valid for streams with B-frames */
1150     p_block->i_length = I64C(1000000) *
1151         p_enc->fmt_in.video.i_frame_rate_base /
1152             p_enc->fmt_in.video.i_frame_rate;
1153
1154     p_block->i_dts = p_block->i_pts = pic.i_pts;
1155
1156     if( p_sys->param.i_bframe > 0 )
1157     {
1158         if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
1159         {
1160             p_block->i_dts = p_block->i_pts;
1161         }
1162         else
1163         {
1164             if( p_sys->i_last_ref_pts )
1165             {
1166                 p_block->i_dts = p_sys->i_last_ref_pts;
1167             }
1168             else
1169             {
1170                 /* Let's put something sensible */
1171                 p_block->i_dts = p_block->i_pts;
1172             }
1173
1174             p_sys->i_last_ref_pts = p_block->i_pts;
1175         }
1176     }
1177
1178     return p_block;
1179 }
1180
1181 /*****************************************************************************
1182  * CloseEncoder: x264 encoder destruction
1183  *****************************************************************************/
1184 static void Close( vlc_object_t *p_this )
1185 {
1186     encoder_t     *p_enc = (encoder_t *)p_this;
1187     encoder_sys_t *p_sys = p_enc->p_sys;
1188
1189     x264_encoder_close( p_sys->h );
1190     free( p_sys->p_buffer );
1191     free( p_sys );
1192 }