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