1 /*****************************************************************************
2 * x264.c: h264 video encoder
3 *****************************************************************************
4 * Copyright (C) 2004-2006 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_codec.h>
38 #define SOUT_CFG_PREFIX "sout-x264-"
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 /* Frame-type options */
48 #define KEYINT_TEXT N_("Maximum GOP size")
49 #define KEYINT_LONGTEXT N_( "Sets maximum interval between IDR-frames." \
50 "Larger values save bits, thus improving quality for a given bitrate at " \
51 "the cost of seeking precision." )
53 #define MIN_KEYINT_TEXT N_("Minimum GOP size")
54 #define MIN_KEYINT_LONGTEXT N_( "Sets minimum interval between IDR-frames. " \
55 "In H.264, I-frames do not necessarily bound a closed GOP because it is " \
56 "allowable for a P-frame to be predicted from more frames than just the " \
57 "one frame before it (also see reference frame option). Therefore, " \
58 "I-frames are not necessarily seekable. IDR-frames restrict subsequent " \
59 "P-frames from referring to any frame prior to the IDR-frame. \n" \
60 "If scenecuts appear within this interval, they are still encoded as " \
61 "I-frames, but do not start a new GOP." )
63 #define SCENE_TEXT N_("Extra I-frames aggressivity" )
64 #define SCENE_LONGTEXT N_( "Scene-cut detection. Controls how " \
65 "aggressively to insert extra I-frames. With small values of " \
66 "scenecut, the codec often has " \
67 "to force an I-frame when it would exceed keyint. " \
68 "Good values of scenecut may find a better location for the " \
69 "I-frame. Large values use more I-frames " \
70 "than necessary, thus wasting bits. -1 disables scene-cut detection, so " \
71 "I-frames are inserted only every other keyint frames, which probably " \
72 "leads to ugly encoding artifacts. Range 1 to 100." )
74 #if X264_BUILD >= 55 /* r607 */
75 #define PRESCENE_TEXT N_("Faster, less precise scenecut detection" )
76 #define PRESCENE_LONGTEXT N_( "Faster, less precise scenecut detection. " \
77 "Required and implied by multi-threading." )
80 #define BFRAMES_TEXT N_("B-frames between I and P")
81 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-frames between I and " \
82 "P-frames. Range 1 to 16." )
84 #define B_ADAPT_TEXT N_("Adaptive B-frame decision")
85 #define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
86 "consecutive B-frames to be used, except possibly before an I-frame." )
88 #define B_BIAS_TEXT N_("Influence (bias) B-frames usage")
89 #define B_BIAS_LONGTEXT N_( "Bias the choice to use B-frames. Positive values " \
90 "cause more B-frames, negative values cause less B-frames." )
92 #define BPYRAMID_TEXT N_("Keep some B-frames as references")
93 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
94 "predicting other frames. Keeps the middle of 2+ consecutive B-frames " \
95 "as a reference, and reorders frame appropriately." )
97 #define CABAC_TEXT N_("CABAC")
98 #define CABAC_LONGTEXT N_( "CABAC (Context-Adaptive Binary Arithmetic "\
99 "Coding). Slightly slows down encoding and decoding, but should save " \
100 "10 to 15% bitrate." )
102 #define REF_TEXT N_("Number of reference frames")
103 #define REF_LONGTEXT N_( "Number of previous frames used as predictors. " \
104 "This is effective in Anime, but seems to make little difference in " \
105 "live-action source material. Some decoders are unable to deal with " \
106 "large frameref values. Range 1 to 16." )
108 #define NF_TEXT N_("Skip loop filter")
109 #define NF_LONGTEXT N_( "Deactivate the deblocking loop filter (decreases quality).")
111 #define FILTER_TEXT N_("Loop filter AlphaC0 and Beta parameters alpha:beta")
112 #define FILTER_LONGTEXT N_( "Loop filter AlphaC0 and Beta parameters. " \
113 "Range -6 to 6 for both alpha and beta parameters. -6 means light " \
114 "filter, 6 means strong.")
116 #define LEVEL_TEXT N_("H.264 level")
117 #define LEVEL_LONGTEXT N_( "Specify H.264 level (as defined by Annex A " \
118 "of the standard). Levels are not enforced; it's up to the user to select " \
119 "a level compatible with the rest of the encoding options. Range 1 to 5.1 " \
120 "(10 to 51 is also allowed).")
122 /* In order to play an interlaced output stream encoded by x264, a decoder needs
123 mbaff support. r570 is using the 'mb' part and not 'aff' yet; so it's really
124 'pure-interlaced' mode */
125 #if X264_BUILD >= 51 /* r570 */
126 #define INTERLACED_TEXT N_("Interlaced mode")
127 #define INTERLACED_LONGTEXT N_( "Pure-interlaced mode.")
132 #define QP_TEXT N_("Set QP")
133 #define QP_LONGTEXT N_( "This selects the quantizer to use. " \
134 "Lower values result in better fidelity, but higher bitrates. 26 is a " \
135 "good default value. Range 0 (lossless) to 51." )
137 #define CRF_TEXT N_("Quality-based VBR")
138 #define CRF_LONGTEXT N_( "1-pass Quality-based VBR. Range 0 to 51." )
140 #define QPMIN_TEXT N_("Min QP")
141 #define QPMIN_LONGTEXT N_( "Minimum quantizer parameter. 15 to 35 seems to " \
142 "be a useful range." )
144 #define QPMAX_TEXT N_("Max QP")
145 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
147 #define QPSTEP_TEXT N_("Max QP step")
148 #define QPSTEP_LONGTEXT N_( "Max QP step between frames.")
150 #define RATETOL_TEXT N_("Average bitrate tolerance")
151 #define RATETOL_LONGTEXT N_( "Allowed variance in average " \
152 "bitrate (in kbits/s).")
154 #define VBV_MAXRATE_TEXT N_("Max local bitrate")
155 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate (in kbits/s).")
157 #define VBV_BUFSIZE_TEXT N_("VBV buffer")
158 #define VBV_BUFSIZE_LONGTEXT N_( "Averaging period for the maximum " \
159 "local bitrate (in kbits).")
161 #define VBV_INIT_TEXT N_("Initial VBV buffer occupancy")
162 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
163 "fraction of the buffer size. Range 0.0 to 1.0.")
165 /* IP Ratio < 1 is technically valid but should never improve quality */
166 #define IPRATIO_TEXT N_("QP factor between I and P")
167 #define IPRATIO_LONGTEXT N_( "QP factor between I and P. Range 1.0 to 2.0.")
169 /* PB ratio < 1 is not valid and breaks ratecontrol */
170 #define PBRATIO_TEXT N_("QP factor between P and B")
171 #define PBRATIO_LONGTEXT N_( "QP factor between P and B. Range 1.0 to 2.0.")
173 #define CHROMA_QP_OFFSET_TEXT N_("QP difference between chroma and luma")
174 #define CHROMA_QP_OFFSET_LONGTEXT N_( "QP difference between chroma and luma.")
176 #define PASS_TEXT N_("Multipass ratecontrol")
177 #define PASS_LONGTEXT N_( "Multipass ratecontrol:\n" \
178 " - 1: First pass, creates stats file\n" \
179 " - 2: Last pass, does not overwrite stats file\n" \
180 " - 3: Nth pass, overwrites stats file\n" )
182 #define QCOMP_TEXT N_("QP curve compression")
183 #define QCOMP_LONGTEXT N_( "QP curve compression. Range 0.0 (CBR) to 1.0 (QCP).")
185 #define CPLXBLUR_TEXT N_("Reduce fluctuations in QP")
186 #define CPLXBLUR_LONGTEXT N_( "This reduces the fluctuations in QP " \
187 "before curve compression. Temporally blurs complexity.")
189 #define QBLUR_TEXT N_("Reduce fluctuations in QP")
190 #define QBLUR_LONGTEXT N_( "This reduces the fluctations in QP " \
191 "after curve compression. Temporally blurs quants.")
195 #define ANALYSE_TEXT N_("Partitions to consider")
196 #define ANALYSE_LONGTEXT N_( "Partitions to consider in analyse mode: \n" \
199 " - normal: i4x4,p8x8,(i8x8)\n" \
200 " - slow : i4x4,p8x8,(i8x8),b8x8\n" \
201 " - all : i4x4,p8x8,(i8x8),b8x8,p4x4\n" \
202 "(p4x4 requires p8x8. i8x8 requires 8x8dct).")
204 #define DIRECT_PRED_TEXT N_("Direct MV prediction mode")
205 #define DIRECT_PRED_LONGTEXT N_( "Direct MV prediction mode.")
207 #if X264_BUILD >= 52 /* r573 */
208 #define DIRECT_PRED_SIZE_TEXT N_("Direct prediction size")
209 #define DIRECT_PRED_SIZE_LONGTEXT N_( "Direct prediction size: "\
212 " - -1: smallest possible according to level\n" )
215 #define WEIGHTB_TEXT N_("Weighted prediction for B-frames")
216 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
218 #define ME_TEXT N_("Integer pixel motion estimation method")
219 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
220 " - dia: diamond search, radius 1 (fast)\n" \
221 " - hex: hexagonal search, radius 2\n" \
222 " - umh: uneven multi-hexagon search (better but slower)\n" \
223 " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
226 #define MERANGE_TEXT N_("Maximum motion vector search range")
227 #define MERANGE_LONGTEXT N_( "Maximum distance to search for " \
228 "motion estimation, measured from predicted position(s). " \
229 "Default of 16 is good for most footage, high motion sequences may " \
230 "benefit from settings between 24 and 32. Range 0 to 64." )
232 #define MVRANGE_TEXT N_("Maximum motion vector length")
233 #define MVRANGE_LONGTEXT N_( "Maximum motion vector length in pixels. " \
234 "-1 is automatic, based on level." )
237 #if X264_BUILD >= 55 /* r607 */
238 #define MVRANGE_THREAD_TEXT N_("Minimum buffer space between threads")
239 #define MVRANGE_THREAD_LONGTEXT N_( "Minimum buffer space between threads. " \
240 "-1 is automatic, based on number of threads." )
243 #define SUBME_TEXT N_("Subpixel motion estimation and partition decision " \
245 #if X264_BUILD >= 46 /* r477 */
247 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
248 "tradeoffs involved in the motion estimation decision process " \
249 "(lower = quicker and higher = better quality). Range 1 to 7." )
250 #elif X264_BUILD >= 30 /* r262 */
252 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
253 "tradeoffs involved in the motion estimation decision process " \
254 "(lower = quicker and higher = better quality). Range 1 to 6." )
257 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
258 "tradeoffs involved in the motion estimation decision process " \
259 "(lower = quicker and higher = better quality). Range 1 to 5." )
262 #define B_RDO_TEXT N_("RD based mode decision for B-frames")
263 #define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. This " \
264 "requires subme 6 (or higher).")
266 #define MIXED_REFS_TEXT N_("Decide references on a per partition basis")
267 #define MIXED_REFS_LONGTEXT N_( "Allows each 8x8 or 16x8 partition to " \
268 "independently select a reference frame, as opposed to only one ref " \
271 #define CHROMA_ME_TEXT N_("Chroma in motion estimation")
272 #define CHROMA_ME_LONGTEXT N_( "Chroma ME for subpel and mode decision in " \
275 #define BIME_TEXT N_("Jointly optimize both MVs in B-frames")
276 #define BIME_LONGTEXT N_( "Joint bidirectional motion refinement.")
278 #define TRANSFORM_8X8DCT_TEXT N_("Adaptive spatial transform size")
279 #define TRANSFORM_8X8DCT_LONGTEXT N_( \
280 "SATD-based decision for 8x8 transform in inter-MBs.")
282 #define TRELLIS_TEXT N_("Trellis RD quantization" )
283 #define TRELLIS_LONGTEXT N_( "Trellis RD quantization: \n" \
285 " - 1: enabled only on the final encode of a MB\n" \
286 " - 2: enabled on all mode decisions\n" \
287 "This requires CABAC." )
289 #define FAST_PSKIP_TEXT N_("Early SKIP detection on P-frames")
290 #define FAST_PSKIP_LONGTEXT N_( "Early SKIP detection on P-frames.")
292 #define DCT_DECIMATE_TEXT N_("Coefficient thresholding on P-frames")
293 #define DCT_DECIMATE_LONGTEXT N_( "Coefficient thresholding on P-frames." \
294 "Eliminate dct blocks containing only a small single coefficient.")
296 /* Noise reduction 1 is too weak to measure, suggest at least 10 */
297 #define NR_TEXT N_("Noise reduction")
298 #define NR_LONGTEXT N_( "Dct-domain noise reduction. Adaptive pseudo-deadzone. " \
299 "10 to 1000 seems to be a useful range." )
301 #if X264_BUILD >= 52 /* r573 */
302 #define DEADZONE_INTER_TEXT N_("Inter luma quantization deadzone")
303 #define DEADZONE_INTER_LONGTEXT N_( "Set the size of the inter luma quantization deadzone. " \
306 #define DEADZONE_INTRA_TEXT N_("Intra luma quantization deadzone")
307 #define DEADZONE_INTRA_LONGTEXT N_( "Set the size of the intra luma quantization deadzone. " \
313 #if X264_BUILD >= 55 /* r607 */
314 #define NON_DETERMINISTIC_TEXT N_("Non-deterministic optimizations when threaded")
315 #define NON_DETERMINISTIC_LONGTEXT N_( "Slightly improve quality of SMP, " \
316 "at the cost of repeatability.")
319 #define ASM_TEXT N_("CPU optimizations")
320 #define ASM_LONGTEXT N_( "Use assembler CPU optimizations.")
322 #define STATS_TEXT N_("Filename for 2 pass stats file")
323 #define STATS_LONGTEXT N_( "Filename for 2 pass stats file for multi-pass encoding.")
325 #define PSNR_TEXT N_("PSNR computation")
326 #define PSNR_LONGTEXT N_( "Compute and print PSNR stats. This has no effect on " \
327 "the actual encoding quality." )
329 #define SSIM_TEXT N_("SSIM computation")
330 #define SSIM_LONGTEXT N_( "Compute and print SSIM stats. This has no effect on " \
331 "the actual encoding quality." )
333 #define QUIET_TEXT N_("Quiet mode")
334 #define QUIET_LONGTEXT N_( "Quiet mode.")
336 #define VERBOSE_TEXT N_("Statistics")
337 #define VERBOSE_LONGTEXT N_( "Print stats for each frame.")
339 #if X264_BUILD >= 47 /* r518 */
340 #define SPS_ID_TEXT N_("SPS and PPS id numbers")
341 #define SPS_ID_LONGTEXT N_( "Set SPS and PPS id numbers to allow concatenating " \
342 "streams with different settings.")
345 #define AUD_TEXT N_("Access unit delimiters")
346 #define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")
349 static const char *enc_me_list[] =
350 { "dia", "hex", "umh", "esa" };
351 static const char *enc_me_list_text[] =
352 { N_("dia"), N_("hex"), N_("umh"), N_("esa") };
355 static const char *enc_analyse_list[] =
356 { "none", "fast", "normal", "slow", "all" };
357 static const char *enc_analyse_list_text[] =
358 { N_("none"), N_("fast"), N_("normal"), N_("slow"), N_("all") };
360 #if X264_BUILD >= 45 /* r457 */
361 static const char *direct_pred_list[] =
362 { "none", "spatial", "temporal", "auto" };
363 static const char *direct_pred_list_text[] =
364 { N_("none"), N_("spatial"), N_("temporal"), N_("auto") };
366 static const char *direct_pred_list[] =
367 { "none", "spatial", "temporal" };
368 static const char *direct_pred_list_text[] =
369 { N_("none"), N_("spatial"), N_("temporal") };
373 set_description( _("H.264/MPEG4 AVC encoder (using x264 library)"));
374 set_capability( "encoder", 200 );
375 set_callbacks( Open, Close );
376 set_category( CAT_INPUT );
377 set_subcategory( SUBCAT_INPUT_VCODEC );
379 /* Frame-type options */
381 add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
382 KEYINT_LONGTEXT, VLC_FALSE );
384 add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
385 MIN_KEYINT_LONGTEXT, VLC_FALSE );
386 add_deprecated_alias( SOUT_CFG_PREFIX "keyint-min" ); /* Deprecated since 0.8.5 */
388 add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
389 SCENE_LONGTEXT, VLC_FALSE );
390 change_integer_range( -1, 100 );
392 #if X264_BUILD >= 55 /* r607 */
393 add_bool( SOUT_CFG_PREFIX "pre-scenecut", 0, NULL, PRESCENE_TEXT,
394 PRESCENE_LONGTEXT, VLC_FALSE );
397 add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
398 BFRAMES_LONGTEXT, VLC_FALSE );
399 change_integer_range( 0, 16 );
401 #if X264_BUILD >= 0x0013 /* r137 */
402 add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
403 B_ADAPT_LONGTEXT, VLC_FALSE );
405 add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
406 B_BIAS_LONGTEXT, VLC_FALSE );
407 change_integer_range( -100, 100 );
410 add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
411 BPYRAMID_LONGTEXT, VLC_FALSE );
413 add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
416 add_integer( SOUT_CFG_PREFIX "ref", 1, NULL, REF_TEXT,
417 REF_LONGTEXT, VLC_FALSE );
418 change_integer_range( 1, 16 );
419 add_deprecated_alias( SOUT_CFG_PREFIX "frameref" ); /* Deprecated since 0.8.5 */
421 add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
422 NF_LONGTEXT, VLC_FALSE );
423 add_deprecated_alias( SOUT_CFG_PREFIX "loopfilter" ); /* Deprecated since 0.8.5 */
425 add_string( SOUT_CFG_PREFIX "deblock", "0:0", NULL, FILTER_TEXT,
426 FILTER_LONGTEXT, VLC_FALSE );
427 add_deprecated_alias( SOUT_CFG_PREFIX "filter" ); /* Deprecated since 0.8.6 */
429 add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
430 LEVEL_LONGTEXT, VLC_FALSE );
432 #if X264_BUILD >= 51 /* r570 */
433 add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
439 add_integer( SOUT_CFG_PREFIX "qp", 26, NULL, QP_TEXT, QP_LONGTEXT,
441 change_integer_range( 0, 51 ); /* QP 0 -> lossless encoding */
443 #if X264_BUILD >= 37 /* r334 */
444 add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
445 CRF_LONGTEXT, VLC_FALSE );
446 change_integer_range( 0, 51 );
449 add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
450 QPMIN_LONGTEXT, VLC_FALSE );
451 change_integer_range( 0, 51 );
452 add_deprecated_alias( SOUT_CFG_PREFIX "qp-min" ); /* Deprecated since 0.8.5 */
454 add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
455 QPMAX_LONGTEXT, VLC_FALSE );
456 change_integer_range( 0, 51 );
457 add_deprecated_alias( SOUT_CFG_PREFIX "qp-max" ); /* Deprecated since 0.8.5 */
459 add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
460 QPSTEP_LONGTEXT, VLC_FALSE );
461 change_integer_range( 0, 51 );
463 add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
464 RATETOL_LONGTEXT, VLC_FALSE );
465 change_float_range( 0, 100 );
466 add_deprecated_alias( SOUT_CFG_PREFIX "tolerance" ); /* Deprecated since 0.8.5 */
468 add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
469 VBV_MAXRATE_LONGTEXT, VLC_FALSE );
471 add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
472 VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
474 add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
475 VBV_INIT_LONGTEXT, VLC_FALSE );
476 change_float_range( 0, 1 );
478 add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
479 IPRATIO_LONGTEXT, VLC_FALSE );
480 change_float_range( 1, 2 );
482 add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
483 PBRATIO_LONGTEXT, VLC_FALSE );
484 change_float_range( 1, 2 );
486 #if X264_BUILD >= 23 /* r190 */
487 add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
488 CHROMA_QP_OFFSET_LONGTEXT, VLC_FALSE );
491 add_integer( SOUT_CFG_PREFIX "pass", 0, NULL, PASS_TEXT,
492 PASS_LONGTEXT, VLC_FALSE );
493 change_integer_range( 0, 3 );
495 add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
496 QCOMP_LONGTEXT, VLC_FALSE );
497 change_float_range( 0, 1 );
499 add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
500 CPLXBLUR_LONGTEXT, VLC_FALSE );
502 add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
503 QBLUR_LONGTEXT, VLC_FALSE );
507 /* x264 partitions = none (default). set at least "normal" mode. */
508 add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
509 ANALYSE_LONGTEXT, VLC_FALSE );
510 change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
511 add_deprecated_alias( SOUT_CFG_PREFIX "analyse" ); /* Deprecated since 0.8.6 */
513 add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
514 DIRECT_PRED_LONGTEXT, VLC_FALSE );
515 change_string_list( direct_pred_list, direct_pred_list_text, 0 );
517 #if X264_BUILD >= 52 /* r573 */
518 add_integer( SOUT_CFG_PREFIX "direct-8x8", 0, NULL, DIRECT_PRED_SIZE_TEXT,
519 DIRECT_PRED_SIZE_LONGTEXT, VLC_FALSE );
520 change_integer_range( -1, 1 );
523 #if X264_BUILD >= 0x0012 /* r134 */
524 add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
525 WEIGHTB_LONGTEXT, VLC_FALSE );
528 #if X264_BUILD >= 24 /* r221 */
529 add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
530 ME_LONGTEXT, VLC_FALSE );
531 change_string_list( enc_me_list, enc_me_list_text, 0 );
533 add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
534 MERANGE_LONGTEXT, VLC_FALSE );
535 change_integer_range( 1, 64 );
538 add_integer( SOUT_CFG_PREFIX "mvrange", -1, NULL, MVRANGE_TEXT,
539 MVRANGE_LONGTEXT, VLC_FALSE );
541 #if X264_BUILD >= 55 /* r607 */
542 add_integer( SOUT_CFG_PREFIX "mvrange-thread", -1, NULL, MVRANGE_THREAD_TEXT,
543 MVRANGE_THREAD_LONGTEXT, VLC_FALSE );
546 add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
547 SUBME_LONGTEXT, VLC_FALSE );
548 change_integer_range( 1, SUBME_MAX );
549 add_deprecated_alias( SOUT_CFG_PREFIX "subpel" ); /* Deprecated since 0.8.5 */
551 #if X264_BUILD >= 41 /* r368 */
552 add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
553 B_RDO_LONGTEXT, VLC_FALSE );
556 #if X264_BUILD >= 36 /* r318 */
557 add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
558 MIXED_REFS_LONGTEXT, VLC_FALSE );
561 #if X264_BUILD >= 23 /* r171 */
562 add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
563 CHROMA_ME_LONGTEXT, VLC_FALSE );
566 #if X264_BUILD >= 43 /* r390 */
567 add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
568 BIME_LONGTEXT, VLC_FALSE );
571 #if X264_BUILD >= 30 /* r251 */
572 add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
573 TRANSFORM_8X8DCT_LONGTEXT, VLC_FALSE );
576 #if X264_BUILD >= 39 /* r360 */
577 add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
578 TRELLIS_LONGTEXT, VLC_FALSE );
579 change_integer_range( 0, 2 );
582 #if X264_BUILD >= 42 /* r384 */
583 add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
584 FAST_PSKIP_LONGTEXT, VLC_FALSE );
587 #if X264_BUILD >= 46 /* r503 */
588 add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
589 DCT_DECIMATE_LONGTEXT, VLC_FALSE );
592 #if X264_BUILD >= 44 /* r398 */
593 add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
594 NR_LONGTEXT, VLC_FALSE );
595 change_integer_range( 0, 1000 );
598 #if X264_BUILD >= 52 /* r573 */
599 add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
600 DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
601 change_integer_range( 0, 32 );
603 add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
604 DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
605 change_integer_range( 0, 32 );
610 #if X264_BUILD >= 55 /* r607 */
611 add_bool( SOUT_CFG_PREFIX "non-deterministic", 0, NULL, NON_DETERMINISTIC_TEXT,
612 NON_DETERMINISTIC_LONGTEXT, VLC_FALSE );
615 add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
616 ASM_LONGTEXT, VLC_FALSE );
618 /* x264 psnr = 1 (default). disable PSNR computation for speed. */
619 add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
620 PSNR_LONGTEXT, VLC_FALSE );
622 #if X264_BUILD >= 50 /* r554 */
623 /* x264 ssim = 1 (default). disable SSIM computation for speed. */
624 add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
625 SSIM_LONGTEXT, VLC_FALSE );
628 add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
629 QUIET_LONGTEXT, VLC_FALSE );
631 #if X264_BUILD >= 47 /* r518 */
632 add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
633 SPS_ID_LONGTEXT, VLC_FALSE );
636 add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
637 AUD_LONGTEXT, VLC_FALSE );
639 #if X264_BUILD >= 0x000e /* r81 */
640 add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
641 VERBOSE_LONGTEXT, VLC_FALSE );
644 add_string( SOUT_CFG_PREFIX "stats", "x264_2pass.log", NULL, STATS_TEXT,
645 STATS_LONGTEXT, VLC_FALSE );
649 /*****************************************************************************
651 *****************************************************************************/
652 static const char *ppsz_sout_options[] = {
653 "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
654 "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
655 "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
656 "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
657 "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
658 "me", "merange", "min-keyint", "mixed-refs", "mvrange", "mvrange-thread",
659 "nf", "non-deterministic", "nr", "partitions", "pass", "pbratio",
660 "pre-scenecut", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax",
661 "qpmin", "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut",
662 "sps-id", "ssim", "stats", "subme", "subpel", "tolerance", "trellis",
663 "verbose", "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", NULL
666 static block_t *Encode( encoder_t *, picture_t * );
676 mtime_t i_interpolated_dts;
681 /*****************************************************************************
682 * Open: probe the encoder
683 *****************************************************************************/
684 static int Open ( vlc_object_t *p_this )
686 encoder_t *p_enc = (encoder_t *)p_this;
687 encoder_sys_t *p_sys;
689 int i_qmin = 0, i_qmax = 0;
693 if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
700 if( p_enc->fmt_in.video.i_width % 16 != 0 ||
701 p_enc->fmt_in.video.i_height % 16 != 0 )
703 msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
704 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
706 if( p_enc->fmt_in.video.i_width < 16 ||
707 p_enc->fmt_in.video.i_height < 16 )
709 msg_Err( p_enc, "video is too small to be cropped" );
713 msg_Warn( p_enc, "cropping video to %ix%i",
714 p_enc->fmt_in.video.i_width >> 4 << 4,
715 p_enc->fmt_in.video.i_height >> 4 << 4 );
719 config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
721 p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
722 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
724 p_enc->pf_encode_video = Encode;
725 p_enc->pf_encode_audio = NULL;
726 p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
727 p_sys->i_interpolated_dts = 0;
728 p_sys->psz_stat_name = NULL;
730 x264_param_default( &p_sys->param );
731 p_sys->param.i_width = p_enc->fmt_in.video.i_width;
732 p_sys->param.i_height = p_enc->fmt_in.video.i_height;
734 p_sys->param.i_width = p_sys->param.i_width >> 4 << 4;
735 p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
738 /* average bitrate specified by transcode vb */
739 p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
742 /* cbr = 1 overrides qp or crf and sets an average bitrate
743 but maxrate = average bitrate is needed for "real" CBR */
744 if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.b_cbr = 1;
746 if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.i_rc_method = X264_RC_ABR;
749 var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
750 if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
751 var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
752 if( val.i_int >= 0 && val.i_int <= 51 ) i_qmin = val.i_int;
753 var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
754 if( val.i_int >= 0 && val.i_int <= 51 ) i_qmax = val.i_int;
756 var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
757 if( val.i_int >= 0 && val.i_int <= 51 )
759 if( i_qmin > val.i_int ) i_qmin = val.i_int;
760 if( i_qmax < val.i_int ) i_qmax = val.i_int;
762 #if X264_BUILD >= 0x000a
763 p_sys->param.rc.i_qp_constant = val.i_int;
764 p_sys->param.rc.i_qp_min = i_qmin;
765 p_sys->param.rc.i_qp_max = i_qmax;
767 p_sys->param.i_qp_constant = val.i_int;
772 var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
773 p_sys->param.rc.f_rate_tolerance = val.f_float;
775 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
776 p_sys->param.rc.f_vbv_buffer_init = val.f_float;
778 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
779 p_sys->param.rc.i_vbv_buffer_size = val.i_int;
781 /* x264 vbv-bufsize = 0 (default). if not provided set period
782 in seconds for local maximum bitrate (cache/bufsize) based
783 on average bitrate. */
785 p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate * 2;
787 /* max bitrate = average bitrate -> CBR */
788 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
789 p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
792 p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
793 p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
796 var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
797 p_sys->param.b_cabac = val.b_bool;
799 /* disable deblocking when nf (no loop filter) is enabled */
800 var_Get( p_enc, SOUT_CFG_PREFIX "nf", &val );
801 p_sys->param.b_deblocking_filter = !val.b_bool;
803 var_Get( p_enc, SOUT_CFG_PREFIX "deblock", &val );
806 char *p = strchr( val.psz_string, ':' );
807 p_sys->param.i_deblocking_filter_alphac0 = atoi( val.psz_string );
808 p_sys->param.i_deblocking_filter_beta = p ? atoi( p+1 ) : p_sys->param.i_deblocking_filter_alphac0;
809 free( val.psz_string );
812 var_Get( p_enc, SOUT_CFG_PREFIX "level", &val );
815 if( atof (val.psz_string) < 6 )
816 p_sys->param.i_level_idc = (int) ( 10 * atof (val.psz_string) + .5);
818 p_sys->param.i_level_idc = atoi (val.psz_string);
819 free( val.psz_string );
822 #if X264_BUILD >= 51 /* r570 */
823 var_Get( p_enc, SOUT_CFG_PREFIX "interlaced", &val );
824 p_sys->param.b_interlaced = val.b_bool;
827 var_Get( p_enc, SOUT_CFG_PREFIX "ipratio", &val );
828 p_sys->param.rc.f_ip_factor = val.f_float;
830 var_Get( p_enc, SOUT_CFG_PREFIX "pbratio", &val );
831 p_sys->param.rc.f_pb_factor = val.f_float;
833 var_Get( p_enc, SOUT_CFG_PREFIX "qcomp", &val );
834 p_sys->param.rc.f_qcompress = val.f_float;
836 var_Get( p_enc, SOUT_CFG_PREFIX "cplxblur", &val );
837 p_sys->param.rc.f_complexity_blur = val.f_float;
839 var_Get( p_enc, SOUT_CFG_PREFIX "qblur", &val );
840 p_sys->param.rc.f_qblur = val.f_float;
842 #if X264_BUILD >= 0x000e
843 var_Get( p_enc, SOUT_CFG_PREFIX "verbose", &val );
844 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_DEBUG;
847 var_Get( p_enc, SOUT_CFG_PREFIX "quiet", &val );
848 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_NONE;
850 #if X264_BUILD >= 47 /* r518 */
851 var_Get( p_enc, SOUT_CFG_PREFIX "sps-id", &val );
852 if( val.i_int >= 0 ) p_sys->param.i_sps_id = val.i_int;
855 var_Get( p_enc, SOUT_CFG_PREFIX "aud", &val );
856 if( val.b_bool ) p_sys->param.b_aud = val.b_bool;
858 var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
859 #if X264_BUILD >= 0x000e
860 if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
862 if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
865 var_Get( p_enc, SOUT_CFG_PREFIX "min-keyint", &val );
866 #if X264_BUILD >= 0x000e
867 if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
869 if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
872 var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
873 if( val.i_int >= 0 && val.i_int <= 16 )
874 p_sys->param.i_bframe = val.i_int;
877 var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
878 p_sys->param.b_bframe_pyramid = val.b_bool;
881 var_Get( p_enc, SOUT_CFG_PREFIX "ref", &val );
882 if( val.i_int > 0 && val.i_int <= 15 )
883 p_sys->param.i_frame_reference = val.i_int;
885 var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
886 #if X264_BUILD >= 0x000b
887 if( val.i_int >= -1 && val.i_int <= 100 )
888 p_sys->param.i_scenecut_threshold = val.i_int;
891 #if X264_BUILD >= 55 /* r607 */
892 var_Get( p_enc, SOUT_CFG_PREFIX "pre-scenecut", &val );
893 p_sys->param.b_pre_scenecut = val.b_bool;
894 var_Get( p_enc, SOUT_CFG_PREFIX "non-deterministic", &val );
895 p_sys->param.b_deterministic = val.b_bool;
898 var_Get( p_enc, SOUT_CFG_PREFIX "subme", &val );
899 if( val.i_int >= 1 && val.i_int <= SUBME_MAX )
900 p_sys->param.analyse.i_subpel_refine = val.i_int;
903 var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
904 if( !strcmp( val.psz_string, "dia" ) )
906 p_sys->param.analyse.i_me_method = X264_ME_DIA;
908 else if( !strcmp( val.psz_string, "hex" ) )
910 p_sys->param.analyse.i_me_method = X264_ME_HEX;
912 else if( !strcmp( val.psz_string, "umh" ) )
914 p_sys->param.analyse.i_me_method = X264_ME_UMH;
916 else if( !strcmp( val.psz_string, "esa" ) )
918 p_sys->param.analyse.i_me_method = X264_ME_ESA;
920 if( val.psz_string ) free( val.psz_string );
922 var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
923 if( val.i_int >= 0 && val.i_int <= 64 )
924 p_sys->param.analyse.i_me_range = val.i_int;
926 var_Get( p_enc, SOUT_CFG_PREFIX "mvrange", &val );
927 p_sys->param.analyse.i_mv_range = val.i_int;
930 #if X264_BUILD >= 55 /* r607 */
931 var_Get( p_enc, SOUT_CFG_PREFIX "mvrange-thread", &val );
932 p_sys->param.analyse.i_mv_range_thread = val.i_int;
935 var_Get( p_enc, SOUT_CFG_PREFIX "direct", &val );
936 if( !strcmp( val.psz_string, "none" ) )
938 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
940 else if( !strcmp( val.psz_string, "spatial" ) )
942 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
944 else if( !strcmp( val.psz_string, "temporal" ) )
946 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
948 #if X264_BUILD >= 45 /* r457 */
949 else if( !strcmp( val.psz_string, "auto" ) )
951 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
954 if( val.psz_string ) free( val.psz_string );
956 var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
957 p_sys->param.analyse.b_psnr = val.b_bool;
959 #if X264_BUILD >= 50 /* r554 */
960 var_Get( p_enc, SOUT_CFG_PREFIX "ssim", &val );
961 p_sys->param.analyse.b_ssim = val.b_bool;
964 #if X264_BUILD >= 0x0012
965 var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
966 p_sys->param.analyse.b_weighted_bipred = val.b_bool;
969 #if X264_BUILD >= 0x0013
970 var_Get( p_enc, SOUT_CFG_PREFIX "b-adapt", &val );
971 p_sys->param.b_bframe_adaptive = val.b_bool;
973 var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
974 if( val.i_int >= -100 && val.i_int <= 100 )
975 p_sys->param.i_bframe_bias = val.i_int;
979 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-me", &val );
980 p_sys->param.analyse.b_chroma_me = val.b_bool;
981 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-qp-offset", &val );
982 p_sys->param.analyse.i_chroma_qp_offset = val.i_int;
986 var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
987 p_sys->param.analyse.b_mixed_references = val.b_bool;
991 var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val );
992 if( val.i_int > 0 && val.i_int <= 51 )
995 p_sys->param.rc.f_rf_constant = val.i_int;
997 p_sys->param.rc.i_rf_constant = val.i_int;
1000 p_sys->param.rc.i_rc_method = X264_RC_CRF;
1005 #if X264_BUILD >= 39
1006 var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
1007 if( val.i_int >= 0 && val.i_int <= 2 )
1008 p_sys->param.analyse.i_trellis = val.i_int;
1011 #if X264_BUILD >= 41
1012 var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
1013 p_sys->param.analyse.b_bframe_rdo = val.b_bool;
1016 #if X264_BUILD >= 42
1017 var_Get( p_enc, SOUT_CFG_PREFIX "fast-pskip", &val );
1018 p_sys->param.analyse.b_fast_pskip = val.b_bool;
1021 #if X264_BUILD >= 43
1022 var_Get( p_enc, SOUT_CFG_PREFIX "bime", &val );
1023 p_sys->param.analyse.b_bidir_me = val.b_bool;
1026 #if X264_BUILD >= 44
1027 var_Get( p_enc, SOUT_CFG_PREFIX "nr", &val );
1028 if( val.i_int >= 0 && val.i_int <= 1000 )
1029 p_sys->param.analyse.i_noise_reduction = val.i_int;
1032 #if X264_BUILD >= 46
1033 var_Get( p_enc, SOUT_CFG_PREFIX "dct-decimate", &val );
1034 p_sys->param.analyse.b_dct_decimate = val.b_bool;
1037 #if X264_BUILD >= 52
1038 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-inter", &val );
1039 if( val.i_int >= 0 && val.i_int <= 32 )
1040 p_sys->param.analyse.i_luma_deadzone[0] = val.i_int;
1042 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-intra", &val );
1043 if( val.i_int >= 0 && val.i_int <= 32 )
1044 p_sys->param.analyse.i_luma_deadzone[1] = val.i_int;
1046 var_Get( p_enc, SOUT_CFG_PREFIX "direct-8x8", &val );
1047 if( val.i_int >= -1 && val.i_int <= 1 )
1048 p_sys->param.analyse.i_direct_8x8_inference = val.i_int;
1051 var_Get( p_enc, SOUT_CFG_PREFIX "asm", &val );
1052 if( !val.b_bool ) p_sys->param.cpu = 0;
1054 #ifndef X264_ANALYSE_BSUB16x16
1055 # define X264_ANALYSE_BSUB16x16 0
1057 var_Get( p_enc, SOUT_CFG_PREFIX "partitions", &val );
1058 if( !strcmp( val.psz_string, "none" ) )
1060 p_sys->param.analyse.inter = 0;
1062 else if( !strcmp( val.psz_string, "fast" ) )
1064 p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
1066 else if( !strcmp( val.psz_string, "normal" ) )
1068 p_sys->param.analyse.inter =
1070 X264_ANALYSE_PSUB16x16;
1071 #ifdef X264_ANALYSE_I8x8
1072 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1075 else if( !strcmp( val.psz_string, "slow" ) )
1077 p_sys->param.analyse.inter =
1079 X264_ANALYSE_PSUB16x16 |
1080 X264_ANALYSE_BSUB16x16;
1081 #ifdef X264_ANALYSE_I8x8
1082 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1085 else if( !strcmp( val.psz_string, "all" ) )
1087 p_sys->param.analyse.inter =
1089 X264_ANALYSE_PSUB16x16 |
1090 X264_ANALYSE_BSUB16x16 |
1091 X264_ANALYSE_PSUB8x8;
1092 #ifdef X264_ANALYSE_I8x8
1093 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1096 if( val.psz_string ) free( val.psz_string );
1098 #if X264_BUILD >= 30
1099 var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
1100 p_sys->param.analyse.b_transform_8x8 = val.b_bool;
1103 if( p_enc->fmt_in.video.i_aspect > 0 )
1105 int64_t i_num, i_den;
1106 unsigned int i_dst_num, i_dst_den;
1108 i_num = p_enc->fmt_in.video.i_aspect *
1109 (int64_t)p_enc->fmt_in.video.i_height;
1110 i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
1111 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1113 p_sys->param.vui.i_sar_width = i_dst_num;
1114 p_sys->param.vui.i_sar_height = i_dst_den;
1116 if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
1118 p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
1119 p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
1122 unsigned i_cpu = vlc_CPU();
1123 if( !(i_cpu & CPU_CAPABILITY_MMX) )
1125 p_sys->param.cpu &= ~X264_CPU_MMX;
1127 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
1129 p_sys->param.cpu &= ~X264_CPU_MMXEXT;
1131 if( !(i_cpu & CPU_CAPABILITY_SSE) )
1133 p_sys->param.cpu &= ~X264_CPU_SSE;
1135 if( !(i_cpu & CPU_CAPABILITY_SSE2) )
1137 p_sys->param.cpu &= ~X264_CPU_SSE2;
1140 #if X264_BUILD >= 29
1141 /* As of r543 x264 will autodetect the number of cpus and will set
1142 the number of threads accordingly unless ofcourse the number of
1143 threads is explicitly specified... */
1144 if( p_enc->i_threads >= 1 )
1145 p_sys->param.i_threads = p_enc->i_threads;
1148 var_Get( p_enc, SOUT_CFG_PREFIX "stats", &val );
1149 if( val.psz_string )
1151 p_sys->param.rc.psz_stat_in =
1152 p_sys->param.rc.psz_stat_out =
1153 p_sys->psz_stat_name = val.psz_string;
1156 var_Get( p_enc, SOUT_CFG_PREFIX "pass", &val );
1157 if( val.i_int > 0 && val.i_int <= 3 )
1159 p_sys->param.rc.b_stat_write = val.i_int & 1;
1160 p_sys->param.rc.b_stat_read = val.i_int & 2;
1163 /* Open the encoder */
1164 p_sys->h = x264_encoder_open( &p_sys->param );
1167 p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
1168 p_enc->fmt_in.video.i_height + 1000;
1169 p_sys->p_buffer = malloc( p_sys->i_buffer );
1171 /* get the globals headers */
1172 p_enc->fmt_out.i_extra = 0;
1173 p_enc->fmt_out.p_extra = NULL;
1175 x264_encoder_headers( p_sys->h, &nal, &i_nal );
1176 for( i = 0; i < i_nal; i++ )
1178 int i_size = p_sys->i_buffer;
1180 x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
1182 p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
1184 memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
1185 p_sys->p_buffer, i_size );
1187 p_enc->fmt_out.i_extra += i_size;
1193 /****************************************************************************
1195 ****************************************************************************/
1196 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
1198 encoder_sys_t *p_sys = p_enc->p_sys;
1202 int i_nal, i_out, i;
1205 memset( &pic, 0, sizeof( x264_picture_t ) );
1206 pic.i_pts = p_pict->date;
1207 pic.img.i_csp = X264_CSP_I420;
1208 pic.img.i_plane = p_pict->i_planes;
1209 for( i = 0; i < p_pict->i_planes; i++ )
1211 pic.img.plane[i] = p_pict->p[i].p_pixels;
1212 pic.img.i_stride[i] = p_pict->p[i].i_pitch;
1215 #if X264_BUILD >= 0x0013
1216 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
1218 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
1221 if( !i_nal ) return NULL;
1223 for( i = 0, i_out = 0; i < i_nal; i++ )
1225 int i_size = p_sys->i_buffer - i_out;
1226 x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
1231 p_block = block_New( p_enc, i_out );
1232 memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
1234 if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
1235 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1236 else if( pic.i_type == X264_TYPE_P )
1237 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1238 else if( pic.i_type == X264_TYPE_B )
1239 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1241 /* This isn't really valid for streams with B-frames */
1242 p_block->i_length = I64C(1000000) *
1243 p_enc->fmt_in.video.i_frame_rate_base /
1244 p_enc->fmt_in.video.i_frame_rate;
1246 p_block->i_pts = pic.i_pts;
1248 if( p_sys->param.i_bframe > 0 )
1250 if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
1252 /* FIXME : this is wrong if bpyramid is set */
1253 p_block->i_dts = p_block->i_pts;
1254 p_sys->i_interpolated_dts = p_block->i_dts;
1258 if( p_sys->i_interpolated_dts )
1260 p_block->i_dts = p_sys->i_interpolated_dts;
1264 /* Let's put something sensible */
1265 p_block->i_dts = p_block->i_pts;
1268 p_sys->i_interpolated_dts += p_block->i_length;
1273 p_block->i_dts = p_block->i_pts;
1279 /*****************************************************************************
1280 * CloseEncoder: x264 encoder destruction
1281 *****************************************************************************/
1282 static void Close( vlc_object_t *p_this )
1284 encoder_t *p_enc = (encoder_t *)p_this;
1285 encoder_sys_t *p_sys = p_enc->p_sys;
1287 if( p_sys->psz_stat_name )
1288 free( p_sys->psz_stat_name );
1290 x264_encoder_close( p_sys->h );
1291 free( p_sys->p_buffer );