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