]> git.sesse.net Git - vlc/blob - modules/codec/x264.c
input options whitelisting, step 2 (refs #1371)
[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         change_safe();
380
381     add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
382                  MIN_KEYINT_LONGTEXT, VLC_FALSE );
383         change_safe();
384         add_deprecated_alias( SOUT_CFG_PREFIX "keyint-min" ); /* Deprecated since 0.8.5 */
385
386     add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
387                  SCENE_LONGTEXT, VLC_FALSE );
388         change_safe();
389         change_integer_range( -1, 100 );
390
391 #if X264_BUILD >= 55 /* r607 */
392     add_bool( SOUT_CFG_PREFIX "pre-scenecut", 0, NULL, PRESCENE_TEXT,
393               PRESCENE_LONGTEXT, VLC_FALSE );
394         change_safe();
395 #endif
396
397     add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
398                  BFRAMES_LONGTEXT, VLC_FALSE );
399         change_safe();
400         change_integer_range( 0, 16 );
401
402 #if X264_BUILD >= 0x0013 /* r137 */
403     add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
404               B_ADAPT_LONGTEXT, VLC_FALSE );
405         change_safe();
406
407     add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
408                  B_BIAS_LONGTEXT, VLC_FALSE );
409         change_safe();
410         change_integer_range( -100, 100 );
411 #endif
412
413     add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
414               BPYRAMID_LONGTEXT, VLC_FALSE );
415         change_safe();
416
417     add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
418               VLC_FALSE );
419         change_safe();
420
421     add_integer( SOUT_CFG_PREFIX "ref", 1, NULL, REF_TEXT,
422                  REF_LONGTEXT, VLC_FALSE );
423         change_safe();
424         change_integer_range( 1, 16 );
425         add_deprecated_alias( SOUT_CFG_PREFIX "frameref" ); /* Deprecated since 0.8.5 */
426
427     add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
428               NF_LONGTEXT, VLC_FALSE );
429         change_safe();
430         add_deprecated_alias( SOUT_CFG_PREFIX "loopfilter" ); /* Deprecated since 0.8.5 */
431
432     add_string( SOUT_CFG_PREFIX "deblock", "0:0", NULL, FILTER_TEXT,
433                  FILTER_LONGTEXT, VLC_FALSE );
434         change_safe();
435         add_deprecated_alias( SOUT_CFG_PREFIX "filter" ); /* Deprecated since 0.8.6 */
436
437     add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
438                LEVEL_LONGTEXT, VLC_FALSE );
439         change_safe();
440
441 #if X264_BUILD >= 51 /* r570 */
442     add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
443               VLC_FALSE );
444         change_safe();
445 #endif
446
447 /* Ratecontrol */
448
449     add_integer( SOUT_CFG_PREFIX "qp", 26, NULL, QP_TEXT, QP_LONGTEXT,
450                  VLC_FALSE );
451         change_safe();
452         change_integer_range( 0, 51 ); /* QP 0 -> lossless encoding */
453
454 #if X264_BUILD >= 37 /* r334 */
455     add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
456                  CRF_LONGTEXT, VLC_FALSE );
457         change_safe();
458         change_integer_range( 0, 51 );
459 #endif
460
461     add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
462                  QPMIN_LONGTEXT, VLC_FALSE );
463         change_safe();
464         change_integer_range( 0, 51 );
465         add_deprecated_alias( SOUT_CFG_PREFIX "qp-min" ); /* Deprecated since 0.8.5 */
466
467     add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
468                  QPMAX_LONGTEXT, VLC_FALSE );
469         change_safe();
470         change_integer_range( 0, 51 );
471         add_deprecated_alias( SOUT_CFG_PREFIX "qp-max" ); /* Deprecated since 0.8.5 */
472
473     add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
474                  QPSTEP_LONGTEXT, VLC_FALSE );
475         change_safe();
476         change_integer_range( 0, 51 );
477
478     add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
479                RATETOL_LONGTEXT, VLC_FALSE );
480         change_safe();
481         change_float_range( 0, 100 );
482         add_deprecated_alias( SOUT_CFG_PREFIX "tolerance" ); /* Deprecated since 0.8.5 */
483
484     add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
485                  VBV_MAXRATE_LONGTEXT, VLC_FALSE );
486         change_safe();
487
488     add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
489                  VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
490         change_safe();
491
492     add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
493                VBV_INIT_LONGTEXT, VLC_FALSE );
494         change_safe();
495         change_float_range( 0, 1 );
496
497     add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
498                IPRATIO_LONGTEXT, VLC_FALSE );
499         change_safe();
500         change_float_range( 1, 2 );
501
502     add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
503                PBRATIO_LONGTEXT, VLC_FALSE );
504         change_safe();
505         change_float_range( 1, 2 );
506
507 #if X264_BUILD >= 23 /* r190 */
508     add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
509                  CHROMA_QP_OFFSET_LONGTEXT, VLC_FALSE );
510         change_safe();
511 #endif
512
513     add_integer( SOUT_CFG_PREFIX "pass", 0, NULL, PASS_TEXT,
514                  PASS_LONGTEXT, VLC_FALSE );
515         change_safe();
516         change_integer_range( 0, 3 );
517
518     add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
519                QCOMP_LONGTEXT, VLC_FALSE );
520         change_safe();
521         change_float_range( 0, 1 );
522
523     add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
524                CPLXBLUR_LONGTEXT, VLC_FALSE );
525         change_safe();
526
527     add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
528                QBLUR_LONGTEXT, VLC_FALSE );
529         change_safe();
530
531 /* Analysis */
532
533     /* x264 partitions = none (default). set at least "normal" mode. */
534     add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
535                 ANALYSE_LONGTEXT, VLC_FALSE );
536         change_safe();
537         change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
538         add_deprecated_alias( SOUT_CFG_PREFIX "analyse" ); /* Deprecated since 0.8.6 */
539
540     add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
541                 DIRECT_PRED_LONGTEXT, VLC_FALSE );
542         change_safe();
543         change_string_list( direct_pred_list, direct_pred_list_text, 0 );
544
545 #if X264_BUILD >= 52 /* r573 */
546     add_integer( SOUT_CFG_PREFIX "direct-8x8", 0, NULL, DIRECT_PRED_SIZE_TEXT,
547                  DIRECT_PRED_SIZE_LONGTEXT, VLC_FALSE );
548         change_safe();
549         change_integer_range( -1, 1 );
550 #endif
551
552 #if X264_BUILD >= 0x0012 /* r134 */
553     add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
554               WEIGHTB_LONGTEXT, VLC_FALSE );
555         change_safe();
556 #endif
557
558 #if X264_BUILD >= 24 /* r221 */
559     add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
560                 ME_LONGTEXT, VLC_FALSE );
561         change_safe();
562         change_string_list( enc_me_list, enc_me_list_text, 0 );
563
564     add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
565                  MERANGE_LONGTEXT, VLC_FALSE );
566         change_safe();
567         change_integer_range( 1, 64 );
568 #endif
569
570     add_integer( SOUT_CFG_PREFIX "mvrange", -1, NULL, MVRANGE_TEXT,
571                  MVRANGE_LONGTEXT, VLC_FALSE );
572         change_safe();
573
574 #if X264_BUILD >= 55 /* r607 */
575     add_integer( SOUT_CFG_PREFIX "mvrange-thread", -1, NULL, MVRANGE_THREAD_TEXT,
576                  MVRANGE_THREAD_LONGTEXT, VLC_FALSE );
577         change_safe();
578 #endif
579
580     add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
581                  SUBME_LONGTEXT, VLC_FALSE );
582         change_safe();
583         change_integer_range( 1, SUBME_MAX );
584         add_deprecated_alias( SOUT_CFG_PREFIX "subpel" ); /* Deprecated since 0.8.5 */
585
586 #if X264_BUILD >= 41 /* r368 */
587     add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
588               B_RDO_LONGTEXT, VLC_FALSE );
589         change_safe();
590 #endif
591
592 #if X264_BUILD >= 36 /* r318 */
593     add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
594               MIXED_REFS_LONGTEXT, VLC_FALSE );
595         change_safe();
596 #endif
597
598 #if X264_BUILD >= 23 /* r171 */
599     add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
600               CHROMA_ME_LONGTEXT, VLC_FALSE );
601         change_safe();
602 #endif
603
604 #if X264_BUILD >= 43 /* r390 */
605     add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
606               BIME_LONGTEXT, VLC_FALSE );
607         change_safe();
608 #endif
609
610 #if X264_BUILD >= 30 /* r251 */
611     add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
612               TRANSFORM_8X8DCT_LONGTEXT, VLC_FALSE );
613         change_safe();
614 #endif
615
616 #if X264_BUILD >= 39 /* r360 */
617     add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
618                  TRELLIS_LONGTEXT, VLC_FALSE );
619         change_safe();
620         change_integer_range( 0, 2 );
621 #endif
622
623 #if X264_BUILD >= 42 /* r384 */
624     add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
625               FAST_PSKIP_LONGTEXT, VLC_FALSE );
626         change_safe();
627 #endif
628
629 #if X264_BUILD >= 46 /* r503 */
630     add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
631               DCT_DECIMATE_LONGTEXT, VLC_FALSE );
632         change_safe();
633 #endif
634
635 #if X264_BUILD >= 44 /* r398 */
636     add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
637                  NR_LONGTEXT, VLC_FALSE );
638         change_safe();
639         change_integer_range( 0, 1000 );
640 #endif
641
642 #if X264_BUILD >= 52 /* r573 */
643     add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
644                  DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
645         change_safe();
646         change_integer_range( 0, 32 );
647
648     add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
649                  DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
650         change_safe();
651         change_integer_range( 0, 32 );
652 #endif
653
654 /* Input/Output */
655
656 #if X264_BUILD >= 55 /* r607 */
657     add_bool( SOUT_CFG_PREFIX "non-deterministic", 0, NULL, NON_DETERMINISTIC_TEXT,
658               NON_DETERMINISTIC_LONGTEXT, VLC_FALSE );
659         change_safe();
660 #endif
661
662     add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
663               ASM_LONGTEXT, VLC_FALSE );
664         change_safe();
665
666     /* x264 psnr = 1 (default). disable PSNR computation for speed. */
667     add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
668               PSNR_LONGTEXT, VLC_FALSE );
669         change_safe();
670
671 #if X264_BUILD >= 50 /* r554 */
672     /* x264 ssim = 1 (default). disable SSIM computation for speed. */
673     add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
674               SSIM_LONGTEXT, VLC_FALSE );
675         change_safe();
676 #endif
677
678     add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
679               QUIET_LONGTEXT, VLC_FALSE );
680         change_safe();
681
682 #if X264_BUILD >= 47 /* r518 */
683     add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
684                  SPS_ID_LONGTEXT, VLC_FALSE );
685         change_safe();
686 #endif
687
688     add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
689               AUD_LONGTEXT, VLC_FALSE );
690         change_safe();
691
692 #if X264_BUILD >= 0x000e /* r81 */
693     add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
694               VERBOSE_LONGTEXT, VLC_FALSE );
695         change_safe();
696 #endif
697
698     add_string( SOUT_CFG_PREFIX "stats", "x264_2pass.log", NULL, STATS_TEXT,
699                 STATS_LONGTEXT, VLC_FALSE );
700
701 vlc_module_end();
702
703 /*****************************************************************************
704  * Local prototypes
705  *****************************************************************************/
706 static const char *ppsz_sout_options[] = {
707     "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
708     "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
709     "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
710     "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
711     "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
712     "me", "merange", "min-keyint", "mixed-refs", "mvrange", "mvrange-thread",
713     "nf", "non-deterministic", "nr", "partitions", "pass", "pbratio",
714     "pre-scenecut", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax",
715     "qpmin", "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut",
716     "sps-id", "ssim", "stats", "subme", "subpel", "tolerance", "trellis",
717     "verbose", "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", NULL
718 };
719
720 static block_t *Encode( encoder_t *, picture_t * );
721
722 struct encoder_sys_t
723 {
724     x264_t          *h;
725     x264_param_t    param;
726
727     int             i_buffer;
728     uint8_t         *p_buffer;
729
730     mtime_t         i_interpolated_dts;
731
732     char *psz_stat_name;
733 };
734
735 /*****************************************************************************
736  * Open: probe the encoder
737  *****************************************************************************/
738 static int  Open ( vlc_object_t *p_this )
739 {
740     encoder_t     *p_enc = (encoder_t *)p_this;
741     encoder_sys_t *p_sys;
742     vlc_value_t    val;
743     int i_qmin = 0, i_qmax = 0;
744     x264_nal_t    *nal;
745     int i, i_nal;
746
747     if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
748         !p_enc->b_force )
749     {
750         return VLC_EGENERIC;
751     }
752
753 #if X264_BUILD < 37
754     if( p_enc->fmt_in.video.i_width % 16 != 0 ||
755         p_enc->fmt_in.video.i_height % 16 != 0 )
756     {
757         msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
758                   p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
759
760         if( p_enc->fmt_in.video.i_width < 16 ||
761             p_enc->fmt_in.video.i_height < 16 )
762         {
763             msg_Err( p_enc, "video is too small to be cropped" );
764             return VLC_EGENERIC;
765         }
766
767         msg_Warn( p_enc, "cropping video to %ix%i",
768                   p_enc->fmt_in.video.i_width >> 4 << 4,
769                   p_enc->fmt_in.video.i_height >> 4 << 4 );
770     }
771 #endif
772
773     config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
774
775     p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
776     p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
777
778     p_enc->pf_encode_video = Encode;
779     p_enc->pf_encode_audio = NULL;
780     p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
781     p_sys->i_interpolated_dts = 0;
782     p_sys->psz_stat_name = NULL;
783
784     x264_param_default( &p_sys->param );
785     p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
786     p_sys->param.i_height = p_enc->fmt_in.video.i_height;
787 #if X264_BUILD < 37
788     p_sys->param.i_width  = p_sys->param.i_width >> 4 << 4;
789     p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
790 #endif
791
792     /* average bitrate specified by transcode vb */
793     p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
794
795 #if X264_BUILD < 48
796     /* cbr = 1 overrides qp or crf and sets an average bitrate
797        but maxrate = average bitrate is needed for "real" CBR */
798     if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.b_cbr = 1;
799 #else
800     if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.i_rc_method = X264_RC_ABR;
801 #endif
802
803     var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
804     if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
805     var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
806     if( val.i_int >= 0 && val.i_int <= 51 ) i_qmin = val.i_int;
807     var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
808     if( val.i_int >= 0 && val.i_int <= 51 ) i_qmax = val.i_int;
809
810     var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
811     if( val.i_int >= 0 && val.i_int <= 51 )
812     {
813         if( i_qmin > val.i_int ) i_qmin = val.i_int;
814         if( i_qmax < val.i_int ) i_qmax = val.i_int;
815
816 #if X264_BUILD >= 0x000a
817         p_sys->param.rc.i_qp_constant = val.i_int;
818         p_sys->param.rc.i_qp_min = i_qmin;
819         p_sys->param.rc.i_qp_max = i_qmax;
820 #else
821         p_sys->param.i_qp_constant = val.i_int;
822 #endif
823     }
824
825 #if X264_BUILD >= 24
826     var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
827     p_sys->param.rc.f_rate_tolerance = val.f_float;
828
829     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
830     p_sys->param.rc.f_vbv_buffer_init = val.f_float;
831
832     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
833     p_sys->param.rc.i_vbv_buffer_size = val.i_int;
834
835     /* x264 vbv-bufsize = 0 (default). if not provided set period
836        in seconds for local maximum bitrate (cache/bufsize) based
837        on average bitrate. */
838     if( !val.i_int )
839         p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate * 2;
840
841     /* max bitrate = average bitrate -> CBR */
842     var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
843     p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
844
845 #else
846     p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
847     p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
848 #endif
849
850     var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
851     p_sys->param.b_cabac = val.b_bool;
852
853     /* disable deblocking when nf (no loop filter) is enabled */
854     var_Get( p_enc, SOUT_CFG_PREFIX "nf", &val );
855     p_sys->param.b_deblocking_filter = !val.b_bool;
856
857     var_Get( p_enc, SOUT_CFG_PREFIX "deblock", &val );
858     if( val.psz_string )
859     {
860         char *p = strchr( val.psz_string, ':' );
861         p_sys->param.i_deblocking_filter_alphac0 = atoi( val.psz_string );
862         p_sys->param.i_deblocking_filter_beta = p ? atoi( p+1 ) : p_sys->param.i_deblocking_filter_alphac0;
863         free( val.psz_string );
864     }
865
866     var_Get( p_enc, SOUT_CFG_PREFIX "level", &val );
867     if( val.psz_string )
868     {
869         if( atof (val.psz_string) < 6 )
870             p_sys->param.i_level_idc = (int) ( 10 * atof (val.psz_string) + .5);
871         else
872             p_sys->param.i_level_idc = atoi (val.psz_string);
873         free( val.psz_string );
874     }
875
876 #if X264_BUILD >= 51 /* r570 */
877     var_Get( p_enc, SOUT_CFG_PREFIX "interlaced", &val );
878     p_sys->param.b_interlaced = val.b_bool;
879 #endif
880
881     var_Get( p_enc, SOUT_CFG_PREFIX "ipratio", &val );
882     p_sys->param.rc.f_ip_factor = val.f_float;
883
884     var_Get( p_enc, SOUT_CFG_PREFIX "pbratio", &val );
885     p_sys->param.rc.f_pb_factor = val.f_float;
886
887     var_Get( p_enc, SOUT_CFG_PREFIX "qcomp", &val );
888     p_sys->param.rc.f_qcompress = val.f_float;
889
890     var_Get( p_enc, SOUT_CFG_PREFIX "cplxblur", &val );
891     p_sys->param.rc.f_complexity_blur = val.f_float;
892
893     var_Get( p_enc, SOUT_CFG_PREFIX "qblur", &val );
894     p_sys->param.rc.f_qblur = val.f_float;
895
896 #if X264_BUILD >= 0x000e
897     var_Get( p_enc, SOUT_CFG_PREFIX "verbose", &val );
898     if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_DEBUG;
899 #endif
900
901     var_Get( p_enc, SOUT_CFG_PREFIX "quiet", &val );
902     if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_NONE;
903
904 #if X264_BUILD >= 47 /* r518 */
905     var_Get( p_enc, SOUT_CFG_PREFIX "sps-id", &val );
906     if( val.i_int >= 0 ) p_sys->param.i_sps_id = val.i_int;
907 #endif
908
909     var_Get( p_enc, SOUT_CFG_PREFIX "aud", &val );
910     if( val.b_bool ) p_sys->param.b_aud = val.b_bool;
911
912     var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
913 #if X264_BUILD >= 0x000e
914     if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
915 #else
916     if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
917 #endif
918
919     var_Get( p_enc, SOUT_CFG_PREFIX "min-keyint", &val );
920 #if X264_BUILD >= 0x000e
921     if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
922 #else
923     if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
924 #endif
925
926     var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
927     if( val.i_int >= 0 && val.i_int <= 16 )
928         p_sys->param.i_bframe = val.i_int;
929
930 #if X264_BUILD >= 22
931     var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
932     p_sys->param.b_bframe_pyramid = val.b_bool;
933 #endif
934
935     var_Get( p_enc, SOUT_CFG_PREFIX "ref", &val );
936     if( val.i_int > 0 && val.i_int <= 15 )
937         p_sys->param.i_frame_reference = val.i_int;
938
939     var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
940 #if X264_BUILD >= 0x000b
941     if( val.i_int >= -1 && val.i_int <= 100 )
942         p_sys->param.i_scenecut_threshold = val.i_int;
943 #endif
944
945 #if X264_BUILD >= 55 /* r607 */
946     var_Get( p_enc, SOUT_CFG_PREFIX "pre-scenecut", &val );
947     p_sys->param.b_pre_scenecut = val.b_bool;
948     var_Get( p_enc, SOUT_CFG_PREFIX "non-deterministic", &val );
949     p_sys->param.b_deterministic = val.b_bool;
950 #endif
951
952     var_Get( p_enc, SOUT_CFG_PREFIX "subme", &val );
953     if( val.i_int >= 1 && val.i_int <= SUBME_MAX )
954         p_sys->param.analyse.i_subpel_refine = val.i_int;
955
956 #if X264_BUILD >= 24
957     var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
958     if( !strcmp( val.psz_string, "dia" ) )
959     {
960         p_sys->param.analyse.i_me_method = X264_ME_DIA;
961     }
962     else if( !strcmp( val.psz_string, "hex" ) )
963     {
964         p_sys->param.analyse.i_me_method = X264_ME_HEX;
965     }
966     else if( !strcmp( val.psz_string, "umh" ) )
967     {
968         p_sys->param.analyse.i_me_method = X264_ME_UMH;
969     }
970     else if( !strcmp( val.psz_string, "esa" ) )
971     {
972         p_sys->param.analyse.i_me_method = X264_ME_ESA;
973     }
974     if( val.psz_string ) free( val.psz_string );
975
976     var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
977     if( val.i_int >= 0 && val.i_int <= 64 )
978         p_sys->param.analyse.i_me_range = val.i_int;
979
980     var_Get( p_enc, SOUT_CFG_PREFIX "mvrange", &val );
981         p_sys->param.analyse.i_mv_range = val.i_int;
982 #endif
983
984 #if X264_BUILD >= 55 /* r607 */
985     var_Get( p_enc, SOUT_CFG_PREFIX "mvrange-thread", &val );
986         p_sys->param.analyse.i_mv_range_thread = val.i_int;
987 #endif
988
989     var_Get( p_enc, SOUT_CFG_PREFIX "direct", &val );
990     if( !strcmp( val.psz_string, "none" ) )
991     {
992         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
993     }
994     else if( !strcmp( val.psz_string, "spatial" ) )
995     {
996         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
997     }
998     else if( !strcmp( val.psz_string, "temporal" ) )
999     {
1000         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
1001     }
1002 #if X264_BUILD >= 45 /* r457 */
1003     else if( !strcmp( val.psz_string, "auto" ) )
1004     {
1005         p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
1006     }
1007 #endif
1008     if( val.psz_string ) free( val.psz_string );
1009
1010     var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
1011     p_sys->param.analyse.b_psnr = val.b_bool;
1012
1013 #if X264_BUILD >= 50 /* r554 */
1014     var_Get( p_enc, SOUT_CFG_PREFIX "ssim", &val );
1015     p_sys->param.analyse.b_ssim = val.b_bool;
1016 #endif
1017
1018 #if X264_BUILD >= 0x0012
1019     var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
1020     p_sys->param.analyse.b_weighted_bipred = val.b_bool;
1021 #endif
1022
1023 #if X264_BUILD >= 0x0013
1024     var_Get( p_enc, SOUT_CFG_PREFIX "b-adapt", &val );
1025     p_sys->param.b_bframe_adaptive = val.b_bool;
1026
1027     var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
1028     if( val.i_int >= -100 && val.i_int <= 100 )
1029         p_sys->param.i_bframe_bias = val.i_int;
1030 #endif
1031
1032 #if X264_BUILD >= 23
1033     var_Get( p_enc, SOUT_CFG_PREFIX "chroma-me", &val );
1034     p_sys->param.analyse.b_chroma_me = val.b_bool;
1035     var_Get( p_enc, SOUT_CFG_PREFIX "chroma-qp-offset", &val );
1036     p_sys->param.analyse.i_chroma_qp_offset = val.i_int;
1037 #endif
1038
1039 #if X264_BUILD >= 36
1040     var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
1041     p_sys->param.analyse.b_mixed_references = val.b_bool;
1042 #endif
1043
1044 #if X264_BUILD >= 37
1045     var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val );
1046     if( val.i_int > 0 && val.i_int <= 51 )
1047     {
1048 #if X264_BUILD >= 54
1049         p_sys->param.rc.f_rf_constant = val.i_int;
1050 #else
1051         p_sys->param.rc.i_rf_constant = val.i_int;
1052 #endif
1053 #if X264_BUILD >= 48
1054         p_sys->param.rc.i_rc_method = X264_RC_CRF;
1055 #endif
1056     }
1057 #endif
1058
1059 #if X264_BUILD >= 39
1060     var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
1061     if( val.i_int >= 0 && val.i_int <= 2 )
1062         p_sys->param.analyse.i_trellis = val.i_int;
1063 #endif
1064
1065 #if X264_BUILD >= 41
1066     var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
1067     p_sys->param.analyse.b_bframe_rdo = val.b_bool;
1068 #endif
1069
1070 #if X264_BUILD >= 42
1071     var_Get( p_enc, SOUT_CFG_PREFIX "fast-pskip", &val );
1072     p_sys->param.analyse.b_fast_pskip = val.b_bool;
1073 #endif
1074
1075 #if X264_BUILD >= 43
1076     var_Get( p_enc, SOUT_CFG_PREFIX "bime", &val );
1077     p_sys->param.analyse.b_bidir_me = val.b_bool;
1078 #endif
1079
1080 #if X264_BUILD >= 44
1081     var_Get( p_enc, SOUT_CFG_PREFIX "nr", &val );
1082     if( val.i_int >= 0 && val.i_int <= 1000 )
1083         p_sys->param.analyse.i_noise_reduction = val.i_int;
1084 #endif
1085
1086 #if X264_BUILD >= 46
1087     var_Get( p_enc, SOUT_CFG_PREFIX "dct-decimate", &val );
1088     p_sys->param.analyse.b_dct_decimate = val.b_bool;
1089 #endif
1090
1091 #if X264_BUILD >= 52
1092     var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-inter", &val );
1093     if( val.i_int >= 0 && val.i_int <= 32 )
1094         p_sys->param.analyse.i_luma_deadzone[0] = val.i_int;
1095
1096     var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-intra", &val );
1097     if( val.i_int >= 0 && val.i_int <= 32 )
1098         p_sys->param.analyse.i_luma_deadzone[1] = val.i_int;
1099
1100     var_Get( p_enc, SOUT_CFG_PREFIX "direct-8x8", &val );
1101     if( val.i_int >= -1 && val.i_int <= 1 )
1102         p_sys->param.analyse.i_direct_8x8_inference = val.i_int;
1103 #endif
1104
1105     var_Get( p_enc, SOUT_CFG_PREFIX "asm", &val );
1106     if( !val.b_bool ) p_sys->param.cpu = 0;
1107
1108 #ifndef X264_ANALYSE_BSUB16x16
1109 #   define X264_ANALYSE_BSUB16x16 0
1110 #endif
1111     var_Get( p_enc, SOUT_CFG_PREFIX "partitions", &val );
1112     if( !strcmp( val.psz_string, "none" ) )
1113     {
1114         p_sys->param.analyse.inter = 0;
1115     }
1116     else if( !strcmp( val.psz_string, "fast" ) )
1117     {
1118         p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
1119     }
1120     else if( !strcmp( val.psz_string, "normal" ) )
1121     {
1122         p_sys->param.analyse.inter =
1123             X264_ANALYSE_I4x4 |
1124             X264_ANALYSE_PSUB16x16;
1125 #ifdef X264_ANALYSE_I8x8
1126         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1127 #endif
1128     }
1129     else if( !strcmp( val.psz_string, "slow" ) )
1130     {
1131         p_sys->param.analyse.inter =
1132             X264_ANALYSE_I4x4 |
1133             X264_ANALYSE_PSUB16x16 |
1134             X264_ANALYSE_BSUB16x16;
1135 #ifdef X264_ANALYSE_I8x8
1136         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1137 #endif
1138     }
1139     else if( !strcmp( val.psz_string, "all" ) )
1140     {
1141         p_sys->param.analyse.inter =
1142             X264_ANALYSE_I4x4 |
1143             X264_ANALYSE_PSUB16x16 |
1144             X264_ANALYSE_BSUB16x16 |
1145             X264_ANALYSE_PSUB8x8;
1146 #ifdef X264_ANALYSE_I8x8
1147         p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1148 #endif
1149     }
1150     if( val.psz_string ) free( val.psz_string );
1151
1152 #if X264_BUILD >= 30
1153     var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
1154     p_sys->param.analyse.b_transform_8x8 = val.b_bool;
1155 #endif
1156
1157     if( p_enc->fmt_in.video.i_aspect > 0 )
1158     {
1159         int64_t i_num, i_den;
1160         unsigned int i_dst_num, i_dst_den;
1161
1162         i_num = p_enc->fmt_in.video.i_aspect *
1163             (int64_t)p_enc->fmt_in.video.i_height;
1164         i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
1165         vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1166
1167         p_sys->param.vui.i_sar_width = i_dst_num;
1168         p_sys->param.vui.i_sar_height = i_dst_den;
1169     }
1170     if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
1171     {
1172         p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
1173         p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
1174     }
1175
1176     unsigned i_cpu = vlc_CPU();
1177     if( !(i_cpu & CPU_CAPABILITY_MMX) )
1178     {
1179         p_sys->param.cpu &= ~X264_CPU_MMX;
1180     }
1181     if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
1182     {
1183         p_sys->param.cpu &= ~X264_CPU_MMXEXT;
1184     }
1185     if( !(i_cpu & CPU_CAPABILITY_SSE) )
1186     {
1187         p_sys->param.cpu &= ~X264_CPU_SSE;
1188     }
1189     if( !(i_cpu & CPU_CAPABILITY_SSE2) )
1190     {
1191         p_sys->param.cpu &= ~X264_CPU_SSE2;
1192     }
1193
1194 #if X264_BUILD >= 29
1195     /* As of r543 x264 will autodetect the number of cpus and will set
1196        the number of threads accordingly unless ofcourse the number of
1197        threads is explicitly specified... */
1198     if( p_enc->i_threads >= 1 )
1199         p_sys->param.i_threads = p_enc->i_threads;
1200 #endif
1201
1202     var_Get( p_enc, SOUT_CFG_PREFIX "stats", &val );
1203     if( val.psz_string )
1204     {
1205         p_sys->param.rc.psz_stat_in  =
1206         p_sys->param.rc.psz_stat_out =
1207         p_sys->psz_stat_name         = val.psz_string;
1208     }
1209
1210     var_Get( p_enc, SOUT_CFG_PREFIX "pass", &val );
1211     if( val.i_int > 0 && val.i_int <= 3 )
1212     {
1213         p_sys->param.rc.b_stat_write = val.i_int & 1;
1214         p_sys->param.rc.b_stat_read = val.i_int & 2;
1215     }
1216
1217     /* Open the encoder */
1218     p_sys->h = x264_encoder_open( &p_sys->param );
1219
1220     /* alloc mem */
1221     p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
1222         p_enc->fmt_in.video.i_height + 1000;
1223     p_sys->p_buffer = malloc( p_sys->i_buffer );
1224
1225     /* get the globals headers */
1226     p_enc->fmt_out.i_extra = 0;
1227     p_enc->fmt_out.p_extra = NULL;
1228
1229     x264_encoder_headers( p_sys->h, &nal, &i_nal );
1230     for( i = 0; i < i_nal; i++ )
1231     {
1232         int i_size = p_sys->i_buffer;
1233
1234         x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
1235
1236         p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
1237
1238         memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
1239                 p_sys->p_buffer, i_size );
1240
1241         p_enc->fmt_out.i_extra += i_size;
1242     }
1243
1244     return VLC_SUCCESS;
1245 }
1246
1247 /****************************************************************************
1248  * Encode:
1249  ****************************************************************************/
1250 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
1251 {
1252     encoder_sys_t *p_sys = p_enc->p_sys;
1253     x264_picture_t pic;
1254     x264_nal_t *nal;
1255     block_t *p_block;
1256     int i_nal, i_out, i;
1257
1258     /* init pic */
1259     memset( &pic, 0, sizeof( x264_picture_t ) );
1260     pic.i_pts = p_pict->date;
1261     pic.img.i_csp = X264_CSP_I420;
1262     pic.img.i_plane = p_pict->i_planes;
1263     for( i = 0; i < p_pict->i_planes; i++ )
1264     {
1265         pic.img.plane[i] = p_pict->p[i].p_pixels;
1266         pic.img.i_stride[i] = p_pict->p[i].i_pitch;
1267     }
1268
1269 #if X264_BUILD >= 0x0013
1270     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
1271 #else
1272     x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
1273 #endif
1274
1275     if( !i_nal ) return NULL;
1276
1277     for( i = 0, i_out = 0; i < i_nal; i++ )
1278     {
1279         int i_size = p_sys->i_buffer - i_out;
1280         x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
1281
1282         i_out += i_size;
1283     }
1284
1285     p_block = block_New( p_enc, i_out );
1286     memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
1287
1288     if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
1289         p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1290     else if( pic.i_type == X264_TYPE_P )
1291         p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1292     else if( pic.i_type == X264_TYPE_B )
1293         p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1294
1295     /* This isn't really valid for streams with B-frames */
1296     p_block->i_length = I64C(1000000) *
1297         p_enc->fmt_in.video.i_frame_rate_base /
1298             p_enc->fmt_in.video.i_frame_rate;
1299
1300     p_block->i_pts = pic.i_pts;
1301
1302     if( p_sys->param.i_bframe > 0 )
1303     {
1304         if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
1305         {
1306             /* FIXME : this is wrong if bpyramid is set */
1307             p_block->i_dts = p_block->i_pts;
1308             p_sys->i_interpolated_dts = p_block->i_dts;
1309         }
1310         else
1311         {
1312             if( p_sys->i_interpolated_dts )
1313             {
1314                 p_block->i_dts = p_sys->i_interpolated_dts;
1315             }
1316             else
1317             {
1318                 /* Let's put something sensible */
1319                 p_block->i_dts = p_block->i_pts;
1320             }
1321
1322             p_sys->i_interpolated_dts += p_block->i_length;
1323         }
1324     }
1325     else
1326     {
1327         p_block->i_dts = p_block->i_pts;
1328     }
1329
1330     return p_block;
1331 }
1332
1333 /*****************************************************************************
1334  * CloseEncoder: x264 encoder destruction
1335  *****************************************************************************/
1336 static void Close( vlc_object_t *p_this )
1337 {
1338     encoder_t     *p_enc = (encoder_t *)p_this;
1339     encoder_sys_t *p_sys = p_enc->p_sys;
1340  
1341     if( p_sys->psz_stat_name )
1342         free( p_sys->psz_stat_name );
1343
1344     x264_encoder_close( p_sys->h );
1345     free( p_sys->p_buffer );
1346     free( p_sys );
1347 }