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