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