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