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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
34 #include <vlc_codec.h>
36 #ifdef PTW32_STATIC_LIB
41 #define SOUT_CFG_PREFIX "sout-x264-"
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
49 /* Frame-type options */
51 #define KEYINT_TEXT N_("Maximum GOP size")
52 #define KEYINT_LONGTEXT N_( "Sets maximum interval between IDR-frames." \
53 "Larger values save bits, thus improving quality for a given bitrate at " \
54 "the cost of seeking precision." )
56 #define MIN_KEYINT_TEXT N_("Minimum GOP size")
57 #define MIN_KEYINT_LONGTEXT N_( "Sets minimum interval between IDR-frames. " \
58 "In H.264, I-frames do not necessarily bound a closed GOP because it is " \
59 "allowable for a P-frame to be predicted from more frames than just the " \
60 "one frame before it (also see reference frame option). Therefore, " \
61 "I-frames are not necessarily seekable. IDR-frames restrict subsequent " \
62 "P-frames from referring to any frame prior to the IDR-frame. \n" \
63 "If scenecuts appear within this interval, they are still encoded as " \
64 "I-frames, but do not start a new GOP." )
66 #define SCENE_TEXT N_("Extra I-frames aggressivity" )
67 #define SCENE_LONGTEXT N_( "Scene-cut detection. Controls how " \
68 "aggressively to insert extra I-frames. With small values of " \
69 "scenecut, the codec often has " \
70 "to force an I-frame when it would exceed keyint. " \
71 "Good values of scenecut may find a better location for the " \
72 "I-frame. Large values use more I-frames " \
73 "than necessary, thus wasting bits. -1 disables scene-cut detection, so " \
74 "I-frames are inserted only every other keyint frames, which probably " \
75 "leads to ugly encoding artifacts. Range 1 to 100." )
77 #if X264_BUILD >= 55 /* r607 */ && X264_BUILD < 67 /* r1117 */
78 #define PRESCENE_TEXT N_("Faster, less precise scenecut detection" )
79 #define PRESCENE_LONGTEXT N_( "Faster, less precise scenecut detection. " \
80 "Required and implied by multi-threading." )
83 #define BFRAMES_TEXT N_("B-frames between I and P")
84 #define BFRAMES_LONGTEXT N_( "Number of consecutive B-frames between I and " \
85 "P-frames. Range 1 to 16." )
87 #define B_ADAPT_TEXT N_("Adaptive B-frame decision")
89 #define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
90 "consecutive B-frames to be used, except possibly before an I-frame." \
93 #define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
94 "consecutive B-frames to be used, except possibly before an I-frame." )
97 #define B_BIAS_TEXT N_("Influence (bias) B-frames usage")
98 #define B_BIAS_LONGTEXT N_( "Bias the choice to use B-frames. Positive values " \
99 "cause more B-frames, negative values cause less B-frames." )
101 #define BPYRAMID_TEXT N_("Keep some B-frames as references")
102 #define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
103 "predicting other frames. Keeps the middle of 2+ consecutive B-frames " \
104 "as a reference, and reorders frame appropriately." )
106 #define CABAC_TEXT N_("CABAC")
107 #define CABAC_LONGTEXT N_( "CABAC (Context-Adaptive Binary Arithmetic "\
108 "Coding). Slightly slows down encoding and decoding, but should save " \
109 "10 to 15% bitrate." )
111 #define REF_TEXT N_("Number of reference frames")
112 #define REF_LONGTEXT N_( "Number of previous frames used as predictors. " \
113 "This is effective in Anime, but seems to make little difference in " \
114 "live-action source material. Some decoders are unable to deal with " \
115 "large frameref values. Range 1 to 16." )
117 #define NF_TEXT N_("Skip loop filter")
118 #define NF_LONGTEXT N_( "Deactivate the deblocking loop filter (decreases quality).")
120 #define FILTER_TEXT N_("Loop filter AlphaC0 and Beta parameters alpha:beta")
121 #define FILTER_LONGTEXT N_( "Loop filter AlphaC0 and Beta parameters. " \
122 "Range -6 to 6 for both alpha and beta parameters. -6 means light " \
123 "filter, 6 means strong.")
125 #define LEVEL_TEXT N_("H.264 level")
126 #define LEVEL_LONGTEXT N_( "Specify H.264 level (as defined by Annex A " \
127 "of the standard). Levels are not enforced; it's up to the user to select " \
128 "a level compatible with the rest of the encoding options. Range 1 to 5.1 " \
129 "(10 to 51 is also allowed).")
131 /* In order to play an interlaced output stream encoded by x264, a decoder needs
132 mbaff support. r570 is using the 'mb' part and not 'aff' yet; so it's really
133 'pure-interlaced' mode */
134 #if X264_BUILD >= 51 /* r570 */
135 #define INTERLACED_TEXT N_("Interlaced mode")
136 #define INTERLACED_LONGTEXT N_( "Pure-interlaced mode.")
141 #define QP_TEXT N_("Set QP")
142 #define QP_LONGTEXT N_( "This selects the quantizer to use. " \
143 "Lower values result in better fidelity, but higher bitrates. 26 is a " \
144 "good default value. Range 0 (lossless) to 51." )
146 #define CRF_TEXT N_("Quality-based VBR")
147 #define CRF_LONGTEXT N_( "1-pass Quality-based VBR. Range 0 to 51." )
149 #define QPMIN_TEXT N_("Min QP")
150 #define QPMIN_LONGTEXT N_( "Minimum quantizer parameter. 15 to 35 seems to " \
151 "be a useful range." )
153 #define QPMAX_TEXT N_("Max QP")
154 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
156 #define QPSTEP_TEXT N_("Max QP step")
157 #define QPSTEP_LONGTEXT N_( "Max QP step between frames.")
159 #define RATETOL_TEXT N_("Average bitrate tolerance")
160 #define RATETOL_LONGTEXT N_( "Allowed variance in average " \
161 "bitrate (in kbits/s).")
163 #define VBV_MAXRATE_TEXT N_("Max local bitrate")
164 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate (in kbits/s).")
166 #define VBV_BUFSIZE_TEXT N_("VBV buffer")
167 #define VBV_BUFSIZE_LONGTEXT N_( "Averaging period for the maximum " \
168 "local bitrate (in kbits).")
170 #define VBV_INIT_TEXT N_("Initial VBV buffer occupancy")
171 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
172 "fraction of the buffer size. Range 0.0 to 1.0.")
175 #define AQ_MODE_TEXT N_("How AQ distributes bits")
176 #define AQ_MODE_LONGTEXT N_("Defines bitdistribution mode for AQ, default 1\n" \
178 " - 1: Current x264 default mode\n"\
179 " - 2: uses log(var)^2 instead of log(var) and attempts to adapt strength per frame")
181 #define AQ_STRENGTH_TEXT N_("Strength of AQ")
182 #define AQ_STRENGTH_LONGTEXT N_("Strength to reduce blocking and blurring in flat\n"\
183 "and textured areas, default 1.0 recommented to be between 0..2\n"\
188 /* IP Ratio < 1 is technically valid but should never improve quality */
189 #define IPRATIO_TEXT N_("QP factor between I and P")
190 #define IPRATIO_LONGTEXT N_( "QP factor between I and P. Range 1.0 to 2.0.")
192 /* PB ratio < 1 is not valid and breaks ratecontrol */
193 #define PBRATIO_TEXT N_("QP factor between P and B")
194 #define PBRATIO_LONGTEXT N_( "QP factor between P and B. Range 1.0 to 2.0.")
196 #define CHROMA_QP_OFFSET_TEXT N_("QP difference between chroma and luma")
197 #define CHROMA_QP_OFFSET_LONGTEXT N_( "QP difference between chroma and luma.")
199 #define PASS_TEXT N_("Multipass ratecontrol")
200 #define PASS_LONGTEXT N_( "Multipass ratecontrol:\n" \
201 " - 1: First pass, creates stats file\n" \
202 " - 2: Last pass, does not overwrite stats file\n" \
203 " - 3: Nth pass, overwrites stats file\n" )
205 #define QCOMP_TEXT N_("QP curve compression")
206 #define QCOMP_LONGTEXT N_( "QP curve compression. Range 0.0 (CBR) to 1.0 (QCP).")
208 #define CPLXBLUR_TEXT N_("Reduce fluctuations in QP")
209 #define CPLXBLUR_LONGTEXT N_( "This reduces the fluctuations in QP " \
210 "before curve compression. Temporally blurs complexity.")
212 #define QBLUR_TEXT N_("Reduce fluctuations in QP")
213 #define QBLUR_LONGTEXT N_( "This reduces the fluctations in QP " \
214 "after curve compression. Temporally blurs quants.")
218 #define ANALYSE_TEXT N_("Partitions to consider")
219 #define ANALYSE_LONGTEXT N_( "Partitions to consider in analyse mode: \n" \
222 " - normal: i4x4,p8x8,(i8x8)\n" \
223 " - slow : i4x4,p8x8,(i8x8),b8x8\n" \
224 " - all : i4x4,p8x8,(i8x8),b8x8,p4x4\n" \
225 "(p4x4 requires p8x8. i8x8 requires 8x8dct).")
227 #define DIRECT_PRED_TEXT N_("Direct MV prediction mode")
228 #define DIRECT_PRED_LONGTEXT N_( "Direct MV prediction mode.")
230 #if X264_BUILD >= 52 /* r573 */
231 #define DIRECT_PRED_SIZE_TEXT N_("Direct prediction size")
232 #define DIRECT_PRED_SIZE_LONGTEXT N_( "Direct prediction size: "\
235 " - -1: smallest possible according to level\n" )
238 #define WEIGHTB_TEXT N_("Weighted prediction for B-frames")
239 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
241 #define ME_TEXT N_("Integer pixel motion estimation method")
242 #if X264_BUILD >= 58 /* r728 */
243 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
244 " - dia: diamond search, radius 1 (fast)\n" \
245 " - hex: hexagonal search, radius 2\n" \
246 " - umh: uneven multi-hexagon search (better but slower)\n" \
247 " - esa: exhaustive search (extremely slow, primarily for testing)\n" \
248 " - tesa: hadamard exhaustive search (extremely slow, primarily for testing)\n" )
250 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
251 " - dia: diamond search, radius 1 (fast)\n" \
252 " - hex: hexagonal search, radius 2\n" \
253 " - umh: uneven multi-hexagon search (better but slower)\n" \
254 " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
258 #define MERANGE_TEXT N_("Maximum motion vector search range")
259 #define MERANGE_LONGTEXT N_( "Maximum distance to search for " \
260 "motion estimation, measured from predicted position(s). " \
261 "Default of 16 is good for most footage, high motion sequences may " \
262 "benefit from settings between 24 and 32. Range 0 to 64." )
264 #define MVRANGE_TEXT N_("Maximum motion vector length")
265 #define MVRANGE_LONGTEXT N_( "Maximum motion vector length in pixels. " \
266 "-1 is automatic, based on level." )
269 #if X264_BUILD >= 55 /* r607 */
270 #define MVRANGE_THREAD_TEXT N_("Minimum buffer space between threads")
271 #define MVRANGE_THREAD_LONGTEXT N_( "Minimum buffer space between threads. " \
272 "-1 is automatic, based on number of threads." )
276 #define PSY_RD_TEXT N_( "Strength of psychovisual optimization, default is \"1.0:0.0\"")
277 #define PSY_RD_LONGTEXT N_( "First parameter controls if RD is on (subme>=6) or off"\
278 "second parameter controls if Trellis is used on psychovisual optimization," \
282 #define SUBME_TEXT N_("Subpixel motion estimation and partition decision " \
286 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
287 "tradeoffs involved in the motion estimation decision process " \
288 "(lower = quicker and higher = better quality). Range 1 to 9." )
289 #elif X264_BUILD >= 46 /* r477 */
291 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
292 "tradeoffs involved in the motion estimation decision process " \
293 "(lower = quicker and higher = better quality). Range 1 to 7." )
294 #elif X264_BUILD >= 30 /* r262 */
296 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
297 "tradeoffs involved in the motion estimation decision process " \
298 "(lower = quicker and higher = better quality). Range 1 to 6." )
301 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
302 "tradeoffs involved in the motion estimation decision process " \
303 "(lower = quicker and higher = better quality). Range 1 to 5." )
306 #define B_RDO_TEXT N_("RD based mode decision for B-frames")
307 #define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. This " \
308 "requires subme 6 (or higher).")
310 #define MIXED_REFS_TEXT N_("Decide references on a per partition basis")
311 #define MIXED_REFS_LONGTEXT N_( "Allows each 8x8 or 16x8 partition to " \
312 "independently select a reference frame, as opposed to only one ref " \
315 #define CHROMA_ME_TEXT N_("Chroma in motion estimation")
316 #define CHROMA_ME_LONGTEXT N_( "Chroma ME for subpel and mode decision in " \
319 #define BIME_TEXT N_("Jointly optimize both MVs in B-frames")
320 #define BIME_LONGTEXT N_( "Joint bidirectional motion refinement.")
322 #define TRANSFORM_8X8DCT_TEXT N_("Adaptive spatial transform size")
323 #define TRANSFORM_8X8DCT_LONGTEXT N_( \
324 "SATD-based decision for 8x8 transform in inter-MBs.")
326 #define TRELLIS_TEXT N_("Trellis RD quantization" )
327 #define TRELLIS_LONGTEXT N_( "Trellis RD quantization: \n" \
329 " - 1: enabled only on the final encode of a MB\n" \
330 " - 2: enabled on all mode decisions\n" \
331 "This requires CABAC." )
333 #define FAST_PSKIP_TEXT N_("Early SKIP detection on P-frames")
334 #define FAST_PSKIP_LONGTEXT N_( "Early SKIP detection on P-frames.")
336 #define DCT_DECIMATE_TEXT N_("Coefficient thresholding on P-frames")
337 #define DCT_DECIMATE_LONGTEXT N_( "Coefficient thresholding on P-frames." \
338 "Eliminate dct blocks containing only a small single coefficient.")
340 /* Noise reduction 1 is too weak to measure, suggest at least 10 */
341 #define NR_TEXT N_("Noise reduction")
342 #define NR_LONGTEXT N_( "Dct-domain noise reduction. Adaptive pseudo-deadzone. " \
343 "10 to 1000 seems to be a useful range." )
345 #if X264_BUILD >= 52 /* r573 */
346 #define DEADZONE_INTER_TEXT N_("Inter luma quantization deadzone")
347 #define DEADZONE_INTER_LONGTEXT N_( "Set the size of the inter luma quantization deadzone. " \
350 #define DEADZONE_INTRA_TEXT N_("Intra luma quantization deadzone")
351 #define DEADZONE_INTRA_LONGTEXT N_( "Set the size of the intra luma quantization deadzone. " \
357 #if X264_BUILD >= 55 /* r607 */
358 #define NON_DETERMINISTIC_TEXT N_("Non-deterministic optimizations when threaded")
359 #define NON_DETERMINISTIC_LONGTEXT N_( "Slightly improve quality of SMP, " \
360 "at the cost of repeatability.")
363 #define ASM_TEXT N_("CPU optimizations")
364 #define ASM_LONGTEXT N_( "Use assembler CPU optimizations.")
366 #define STATS_TEXT N_("Filename for 2 pass stats file")
367 #define STATS_LONGTEXT N_( "Filename for 2 pass stats file for multi-pass encoding.")
369 #define PSNR_TEXT N_("PSNR computation")
370 #define PSNR_LONGTEXT N_( "Compute and print PSNR stats. This has no effect on " \
371 "the actual encoding quality." )
373 #define SSIM_TEXT N_("SSIM computation")
374 #define SSIM_LONGTEXT N_( "Compute and print SSIM stats. This has no effect on " \
375 "the actual encoding quality." )
377 #define QUIET_TEXT N_("Quiet mode")
378 #define QUIET_LONGTEXT N_( "Quiet mode.")
380 #define VERBOSE_TEXT N_("Statistics")
381 #define VERBOSE_LONGTEXT N_( "Print stats for each frame.")
383 #if X264_BUILD >= 47 /* r518 */
384 #define SPS_ID_TEXT N_("SPS and PPS id numbers")
385 #define SPS_ID_LONGTEXT N_( "Set SPS and PPS id numbers to allow concatenating " \
386 "streams with different settings.")
389 #define AUD_TEXT N_("Access unit delimiters")
390 #define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")
392 #if X264_BUILD >= 24 && X264_BUILD < 58
393 static const char *const enc_me_list[] =
394 { "dia", "hex", "umh", "esa" };
395 static const char *const enc_me_list_text[] =
396 { N_("dia"), N_("hex"), N_("umh"), N_("esa") };
399 #if X264_BUILD >= 58 /* r728 */
400 static const char *const enc_me_list[] =
401 { "dia", "hex", "umh", "esa", "tesa" };
402 static const char *const enc_me_list_text[] =
403 { N_("dia"), N_("hex"), N_("umh"), N_("esa"), N_("tesa") };
406 static const char *const enc_analyse_list[] =
407 { "none", "fast", "normal", "slow", "all" };
408 static const char *const enc_analyse_list_text[] =
409 { N_("none"), N_("fast"), N_("normal"), N_("slow"), N_("all") };
411 #if X264_BUILD >= 45 /* r457 */
412 static const char *const direct_pred_list[] =
413 { "none", "spatial", "temporal", "auto" };
414 static const char *const direct_pred_list_text[] =
415 { N_("none"), N_("spatial"), N_("temporal"), N_("auto") };
417 static const char *const direct_pred_list[] =
418 { "none", "spatial", "temporal" };
419 static const char *const direct_pred_list_text[] =
420 { N_("none"), N_("spatial"), N_("temporal") };
424 set_description( N_("H.264/MPEG4 AVC encoder (x264)"))
425 set_capability( "encoder", 200 )
426 set_callbacks( Open, Close )
427 set_category( CAT_INPUT )
428 set_subcategory( SUBCAT_INPUT_VCODEC )
430 /* Frame-type options */
432 add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
433 KEYINT_LONGTEXT, false )
435 add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
436 MIN_KEYINT_LONGTEXT, false )
437 add_deprecated_alias( SOUT_CFG_PREFIX "keyint-min" ) /* Deprecated since 0.8.5 */
439 add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
440 SCENE_LONGTEXT, false )
441 change_integer_range( -1, 100 )
443 #if X264_BUILD >= 55 /* r607 */
444 # if X264_BUILD < 67 /* r1117 */
445 add_bool( SOUT_CFG_PREFIX "pre-scenecut", 0, NULL, PRESCENE_TEXT,
446 PRESCENE_LONGTEXT, false )
448 add_obsolete_bool( SOUT_CFG_PREFIX "pre-scenecut" )
452 add_integer( SOUT_CFG_PREFIX "bframes", 3, NULL, BFRAMES_TEXT,
453 BFRAMES_LONGTEXT, false )
454 change_integer_range( 0, 16 )
457 add_integer( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
458 B_ADAPT_LONGTEXT, false )
459 change_integer_range( 0, 2 )
460 #elif X264_BUILD >= 0x0013 /* r137 */
461 add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
462 B_ADAPT_LONGTEXT, false )
465 #if X264_BUILD >= 0x0013 /* r137 */
466 add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
467 B_BIAS_LONGTEXT, false )
468 change_integer_range( -100, 100 )
471 add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
472 BPYRAMID_LONGTEXT, false )
474 add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
477 add_integer( SOUT_CFG_PREFIX "ref", 3, NULL, REF_TEXT,
478 REF_LONGTEXT, false )
479 change_integer_range( 1, 16 )
480 add_deprecated_alias( SOUT_CFG_PREFIX "frameref" ) /* Deprecated since 0.8.5 */
482 add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
484 add_deprecated_alias( SOUT_CFG_PREFIX "loopfilter" ) /* Deprecated since 0.8.5 */
486 add_string( SOUT_CFG_PREFIX "deblock", "0:0", NULL, FILTER_TEXT,
487 FILTER_LONGTEXT, false )
488 add_deprecated_alias( SOUT_CFG_PREFIX "filter" ) /* Deprecated since 0.8.6 */
491 add_string( SOUT_CFG_PREFIX "psy-rd", "1.0:0.0", NULL, PSY_RD_TEXT,
492 PSY_RD_LONGTEXT, false )
495 add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
496 LEVEL_LONGTEXT, false )
498 #if X264_BUILD >= 51 /* r570 */
499 add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
505 add_integer( SOUT_CFG_PREFIX "qp", -1, NULL, QP_TEXT, QP_LONGTEXT,
507 change_integer_range( -1, 51 ) /* QP 0 -> lossless encoding */
509 #if X264_BUILD >= 37 /* r334 */
510 add_integer( SOUT_CFG_PREFIX "crf", 23, NULL, CRF_TEXT,
511 CRF_LONGTEXT, false )
512 change_integer_range( 0, 51 )
515 add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
516 QPMIN_LONGTEXT, false )
517 change_integer_range( 0, 51 )
518 add_deprecated_alias( SOUT_CFG_PREFIX "qp-min" ) /* Deprecated since 0.8.5 */
520 add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
521 QPMAX_LONGTEXT, false )
522 change_integer_range( 0, 51 )
523 add_deprecated_alias( SOUT_CFG_PREFIX "qp-max" ) /* Deprecated since 0.8.5 */
525 add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
526 QPSTEP_LONGTEXT, false )
527 change_integer_range( 0, 51 )
529 add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
530 RATETOL_LONGTEXT, false )
531 change_float_range( 0, 100 )
532 add_deprecated_alias( SOUT_CFG_PREFIX "tolerance" ) /* Deprecated since 0.8.5 */
534 add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
535 VBV_MAXRATE_LONGTEXT, false )
537 add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
538 VBV_BUFSIZE_LONGTEXT, false )
540 add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
541 VBV_INIT_LONGTEXT, false )
542 change_float_range( 0, 1 )
544 add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
545 IPRATIO_LONGTEXT, false )
546 change_float_range( 1, 2 )
548 add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
549 PBRATIO_LONGTEXT, false )
550 change_float_range( 1, 2 )
552 #if X264_BUILD >= 23 /* r190 */
553 add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
554 CHROMA_QP_OFFSET_LONGTEXT, false )
557 add_integer( SOUT_CFG_PREFIX "pass", 0, NULL, PASS_TEXT,
558 PASS_LONGTEXT, false )
559 change_integer_range( 0, 3 )
561 add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
562 QCOMP_LONGTEXT, false )
563 change_float_range( 0, 1 )
565 add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
566 CPLXBLUR_LONGTEXT, false )
568 add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
569 QBLUR_LONGTEXT, false )
571 add_integer( SOUT_CFG_PREFIX "aq-mode", X264_AQ_VARIANCE, NULL, AQ_MODE_TEXT,
572 AQ_MODE_LONGTEXT, false )
573 change_integer_range( 0, 2 )
574 add_float( SOUT_CFG_PREFIX "aq-strength", 1.0, NULL, AQ_STRENGTH_TEXT,
575 AQ_STRENGTH_LONGTEXT, false )
580 /* x264 partitions = none (default). set at least "normal" mode. */
581 add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
582 ANALYSE_LONGTEXT, false )
583 change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
584 add_deprecated_alias( SOUT_CFG_PREFIX "analyse" ) /* Deprecated since 0.8.6 */
586 add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
587 DIRECT_PRED_LONGTEXT, false )
588 change_string_list( direct_pred_list, direct_pred_list_text, 0 );
590 #if X264_BUILD >= 52 /* r573 */
591 add_integer( SOUT_CFG_PREFIX "direct-8x8", 1, NULL, DIRECT_PRED_SIZE_TEXT,
592 DIRECT_PRED_SIZE_LONGTEXT, false )
593 change_integer_range( -1, 1 )
596 #if X264_BUILD >= 0x0012 /* r134 */
597 add_bool( SOUT_CFG_PREFIX "weightb", 1, NULL, WEIGHTB_TEXT,
598 WEIGHTB_LONGTEXT, false )
601 #if X264_BUILD >= 24 /* r221 */
602 add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
604 change_string_list( enc_me_list, enc_me_list_text, 0 );
606 add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
607 MERANGE_LONGTEXT, false )
608 change_integer_range( 1, 64 )
611 add_integer( SOUT_CFG_PREFIX "mvrange", -1, NULL, MVRANGE_TEXT,
612 MVRANGE_LONGTEXT, false )
614 #if X264_BUILD >= 55 /* r607 */
615 add_integer( SOUT_CFG_PREFIX "mvrange-thread", -1, NULL, MVRANGE_THREAD_TEXT,
616 MVRANGE_THREAD_LONGTEXT, false )
619 add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
620 SUBME_LONGTEXT, false )
621 change_integer_range( 1, SUBME_MAX )
622 add_deprecated_alias( SOUT_CFG_PREFIX "subpel" ) /* Deprecated since 0.8.5 */
625 # if X264_BUILD < 65 /* r368 */
626 add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
627 B_RDO_LONGTEXT, false )
629 add_obsolete_bool( SOUT_CFG_PREFIX "b-rdo" )
633 #if X264_BUILD >= 36 /* r318 */
634 add_bool( SOUT_CFG_PREFIX "mixed-refs", 1, NULL, MIXED_REFS_TEXT,
635 MIXED_REFS_LONGTEXT, false )
638 #if X264_BUILD >= 23 /* r171 */
639 add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
640 CHROMA_ME_LONGTEXT, false )
644 # if X264_BUILD < 65 /* r390 */
645 add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
646 BIME_LONGTEXT, false )
648 add_obsolete_bool( SOUT_CFG_PREFIX "bime" )
652 #if X264_BUILD >= 30 /* r251 */
653 add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
654 TRANSFORM_8X8DCT_LONGTEXT, false )
657 #if X264_BUILD >= 39 /* r360 */
658 add_integer( SOUT_CFG_PREFIX "trellis", 1, NULL, TRELLIS_TEXT,
659 TRELLIS_LONGTEXT, false )
660 change_integer_range( 0, 2 )
663 #if X264_BUILD >= 42 /* r384 */
664 add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
665 FAST_PSKIP_LONGTEXT, false )
668 #if X264_BUILD >= 46 /* r503 */
669 add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
670 DCT_DECIMATE_LONGTEXT, false )
673 #if X264_BUILD >= 44 /* r398 */
674 add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
676 change_integer_range( 0, 1000 )
679 #if X264_BUILD >= 52 /* r573 */
680 add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
681 DEADZONE_INTRA_LONGTEXT, false )
682 change_integer_range( 0, 32 )
684 add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
685 DEADZONE_INTRA_LONGTEXT, false )
686 change_integer_range( 0, 32 )
691 #if X264_BUILD >= 55 /* r607 */
692 add_bool( SOUT_CFG_PREFIX "non-deterministic", 0, NULL, NON_DETERMINISTIC_TEXT,
693 NON_DETERMINISTIC_LONGTEXT, false )
696 add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
697 ASM_LONGTEXT, false )
699 /* x264 psnr = 1 (default). disable PSNR computation for speed. */
700 add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
701 PSNR_LONGTEXT, false )
703 #if X264_BUILD >= 50 /* r554 */
704 /* x264 ssim = 1 (default). disable SSIM computation for speed. */
705 add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
706 SSIM_LONGTEXT, false )
709 add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
710 QUIET_LONGTEXT, false )
712 #if X264_BUILD >= 47 /* r518 */
713 add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
714 SPS_ID_LONGTEXT, false )
717 add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
718 AUD_LONGTEXT, false )
720 #if X264_BUILD >= 0x000e /* r81 */
721 add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
722 VERBOSE_LONGTEXT, false )
725 add_string( SOUT_CFG_PREFIX "stats", "x264_2pass.log", NULL, STATS_TEXT,
726 STATS_LONGTEXT, false )
730 /*****************************************************************************
732 *****************************************************************************/
733 static const char *const ppsz_sout_options[] = {
734 "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
735 "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
736 "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
737 "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
738 "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
739 "me", "merange", "min-keyint", "mixed-refs", "mvrange", "mvrange-thread",
740 "nf", "non-deterministic", "nr", "partitions", "pass", "pbratio",
741 "pre-scenecut", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax",
742 "qpmin", "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut",
743 "sps-id", "ssim", "stats", "subme", "subpel", "tolerance", "trellis",
744 "verbose", "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", "aq-mode",
745 "aq-strength", "psy-rd", NULL
748 static block_t *Encode( encoder_t *, picture_t * );
758 mtime_t i_interpolated_dts;
763 /*****************************************************************************
764 * Open: probe the encoder
765 *****************************************************************************/
766 static int Open ( vlc_object_t *p_this )
768 encoder_t *p_enc = (encoder_t *)p_this;
769 encoder_sys_t *p_sys;
771 int i_qmin = 0, i_qmax = 0;
775 if( p_enc->fmt_out.i_codec != VLC_CODEC_H264 &&
781 /* X264_POINTVER or X264_VERSION are not available */
782 msg_Dbg ( p_enc, "version x264 0.%d.X", X264_BUILD );
785 if( p_enc->fmt_in.video.i_width % 16 != 0 ||
786 p_enc->fmt_in.video.i_height % 16 != 0 )
788 msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
789 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
791 if( p_enc->fmt_in.video.i_width < 16 ||
792 p_enc->fmt_in.video.i_height < 16 )
794 msg_Err( p_enc, "video is too small to be cropped" );
798 msg_Warn( p_enc, "cropping video to %ix%i",
799 p_enc->fmt_in.video.i_width >> 4 << 4,
800 p_enc->fmt_in.video.i_height >> 4 << 4 );
804 config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
806 p_enc->fmt_out.i_cat = VIDEO_ES;
807 p_enc->fmt_out.i_codec = VLC_CODEC_H264;
808 p_enc->fmt_in.i_codec = VLC_CODEC_I420;
810 p_enc->pf_encode_video = Encode;
811 p_enc->pf_encode_audio = NULL;
812 p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
815 p_sys->i_interpolated_dts = 0;
816 p_sys->psz_stat_name = NULL;
817 p_sys->p_buffer = NULL;
819 x264_param_default( &p_sys->param );
820 p_sys->param.i_width = p_enc->fmt_in.video.i_width;
821 p_sys->param.i_height = p_enc->fmt_in.video.i_height;
823 p_sys->param.i_width = p_sys->param.i_width >> 4 << 4;
824 p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
828 var_Get( p_enc, SOUT_CFG_PREFIX "qcomp", &val );
829 p_sys->param.rc.f_qcompress = val.f_float;
831 /* transcode-default bitrate is 0,
832 * set more to ABR if user specifies bitrate */
833 if( p_enc->fmt_out.i_bitrate > 0 )
835 p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
837 /* cbr = 1 overrides qp or crf and sets an average bitrate
838 but maxrate = average bitrate is needed for "real" CBR */
839 p_sys->param.rc.b_cbr=1;
841 p_sys->param.rc.i_rc_method = X264_RC_ABR;
843 else /* Set default to CRF */
846 var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val );
847 if( val.i_int > 0 && val.i_int <= 51 )
849 # if X264_BUILD >= 54
850 p_sys->param.rc.f_rf_constant = val.i_int;
852 p_sys->param.rc.i_rf_constant = val.i_int;
854 # if X264_BUILD >= 48
855 p_sys->param.rc.i_rc_method = X264_RC_CRF;
861 var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
862 if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
863 var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
864 if( val.i_int >= 0 && val.i_int <= 51 )
867 p_sys->param.rc.i_qp_min = i_qmin;
869 var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
870 if( val.i_int >= 0 && val.i_int <= 51 )
873 p_sys->param.rc.i_qp_max = i_qmax;
876 var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
877 if( val.i_int >= 0 && val.i_int <= 51 )
879 if( i_qmin > val.i_int ) i_qmin = val.i_int;
880 if( i_qmax < val.i_int ) i_qmax = val.i_int;
882 /* User defined QP-value, so change ratecontrol method */
883 p_sys->param.rc.i_rc_method = X264_RC_CQP;
884 #if X264_BUILD >= 0x000a
885 p_sys->param.rc.i_qp_constant = val.i_int;
886 p_sys->param.rc.i_qp_min = i_qmin;
887 p_sys->param.rc.i_qp_max = i_qmax;
889 p_sys->param.i_qp_constant = val.i_int;
895 var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
896 p_sys->param.rc.f_rate_tolerance = val.f_float;
898 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
899 p_sys->param.rc.f_vbv_buffer_init = val.f_float;
901 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
902 p_sys->param.rc.i_vbv_buffer_size = val.i_int;
904 /* max bitrate = average bitrate -> CBR */
905 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
907 if( !val.i_int && p_sys->param.rc.i_rc_method == X264_RC_ABR )
908 p_sys->param.rc.i_vbv_max_bitrate = p_sys->param.rc.i_bitrate;
909 else if ( val.i_int )
911 p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
914 p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
915 p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
918 var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
919 p_sys->param.b_cabac = val.b_bool;
921 /* disable deblocking when nf (no loop filter) is enabled */
922 var_Get( p_enc, SOUT_CFG_PREFIX "nf", &val );
923 p_sys->param.b_deblocking_filter = !val.b_bool;
925 var_Get( p_enc, SOUT_CFG_PREFIX "deblock", &val );
928 char *p = strchr( val.psz_string, ':' );
929 p_sys->param.i_deblocking_filter_alphac0 = atoi( val.psz_string );
930 p_sys->param.i_deblocking_filter_beta = p ? atoi( p+1 ) : p_sys->param.i_deblocking_filter_alphac0;
931 free( val.psz_string );
935 var_Get( p_enc, SOUT_CFG_PREFIX "psy-rd", &val );
938 char *p = strchr( val.psz_string, ':' );
939 p_sys->param.analyse.f_psy_rd = atof( val.psz_string );
940 p_sys->param.analyse.f_psy_trellis = p ? atof( p+1 ) : 0;
941 free( val.psz_string );
946 var_Get( p_enc, SOUT_CFG_PREFIX "level", &val );
949 if( atof (val.psz_string) < 6 )
950 p_sys->param.i_level_idc = (int) ( 10 * atof (val.psz_string) + .5);
952 p_sys->param.i_level_idc = atoi (val.psz_string);
953 free( val.psz_string );
956 #if X264_BUILD >= 51 /* r570 */
957 var_Get( p_enc, SOUT_CFG_PREFIX "interlaced", &val );
958 p_sys->param.b_interlaced = val.b_bool;
961 var_Get( p_enc, SOUT_CFG_PREFIX "ipratio", &val );
962 p_sys->param.rc.f_ip_factor = val.f_float;
964 var_Get( p_enc, SOUT_CFG_PREFIX "pbratio", &val );
965 p_sys->param.rc.f_pb_factor = val.f_float;
968 var_Get( p_enc, SOUT_CFG_PREFIX "cplxblur", &val );
969 p_sys->param.rc.f_complexity_blur = val.f_float;
971 var_Get( p_enc, SOUT_CFG_PREFIX "qblur", &val );
972 p_sys->param.rc.f_qblur = val.f_float;
975 var_Get( p_enc, SOUT_CFG_PREFIX "aq-mode", &val );
976 p_sys->param.rc.i_aq_mode = val.i_int;
978 var_Get( p_enc, SOUT_CFG_PREFIX "aq-strength", &val );
979 p_sys->param.rc.f_aq_strength = val.f_float;
982 #if X264_BUILD >= 0x000e
983 var_Get( p_enc, SOUT_CFG_PREFIX "verbose", &val );
984 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_DEBUG;
987 var_Get( p_enc, SOUT_CFG_PREFIX "quiet", &val );
988 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_NONE;
990 #if X264_BUILD >= 47 /* r518 */
991 var_Get( p_enc, SOUT_CFG_PREFIX "sps-id", &val );
992 if( val.i_int >= 0 ) p_sys->param.i_sps_id = val.i_int;
995 var_Get( p_enc, SOUT_CFG_PREFIX "aud", &val );
996 if( val.b_bool ) p_sys->param.b_aud = val.b_bool;
998 var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
999 #if X264_BUILD >= 0x000e
1000 if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
1002 if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
1005 var_Get( p_enc, SOUT_CFG_PREFIX "min-keyint", &val );
1006 #if X264_BUILD >= 0x000e
1007 if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
1009 if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
1012 var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
1013 if( val.i_int >= 0 && val.i_int <= 16 )
1014 p_sys->param.i_bframe = val.i_int;
1016 #if X264_BUILD >= 22
1017 var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
1018 p_sys->param.b_bframe_pyramid = val.b_bool;
1021 var_Get( p_enc, SOUT_CFG_PREFIX "ref", &val );
1022 if( val.i_int > 0 && val.i_int <= 15 )
1023 p_sys->param.i_frame_reference = val.i_int;
1025 var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
1026 #if X264_BUILD >= 0x000b
1027 if( val.i_int >= -1 && val.i_int <= 100 )
1028 p_sys->param.i_scenecut_threshold = val.i_int;
1031 #if X264_BUILD >= 55 /* r607 */ && X264_BUILD < 67 /* r1117 */
1032 var_Get( p_enc, SOUT_CFG_PREFIX "pre-scenecut", &val );
1033 p_sys->param.b_pre_scenecut = val.b_bool;
1036 #if X264_BUILD >= 55 /* r607 */
1037 var_Get( p_enc, SOUT_CFG_PREFIX "non-deterministic", &val );
1038 p_sys->param.b_deterministic = val.b_bool;
1041 var_Get( p_enc, SOUT_CFG_PREFIX "subme", &val );
1042 if( val.i_int >= 1 && val.i_int <= SUBME_MAX )
1043 p_sys->param.analyse.i_subpel_refine = val.i_int;
1045 #if X264_BUILD >= 24
1046 var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
1047 if( !strcmp( val.psz_string, "dia" ) )
1049 p_sys->param.analyse.i_me_method = X264_ME_DIA;
1051 else if( !strcmp( val.psz_string, "hex" ) )
1053 p_sys->param.analyse.i_me_method = X264_ME_HEX;
1055 else if( !strcmp( val.psz_string, "umh" ) )
1057 p_sys->param.analyse.i_me_method = X264_ME_UMH;
1059 else if( !strcmp( val.psz_string, "esa" ) )
1061 p_sys->param.analyse.i_me_method = X264_ME_ESA;
1063 # if X264_BUILD >= 58 /* r728 */
1064 else if( !strcmp( val.psz_string, "tesa" ) )
1066 p_sys->param.analyse.i_me_method = X264_ME_TESA;
1069 free( val.psz_string );
1071 var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
1072 if( val.i_int >= 0 && val.i_int <= 64 )
1073 p_sys->param.analyse.i_me_range = val.i_int;
1075 var_Get( p_enc, SOUT_CFG_PREFIX "mvrange", &val );
1076 p_sys->param.analyse.i_mv_range = val.i_int;
1079 #if X264_BUILD >= 55 /* r607 */
1080 var_Get( p_enc, SOUT_CFG_PREFIX "mvrange-thread", &val );
1081 p_sys->param.analyse.i_mv_range_thread = val.i_int;
1084 var_Get( p_enc, SOUT_CFG_PREFIX "direct", &val );
1085 if( !strcmp( val.psz_string, "none" ) )
1087 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
1089 else if( !strcmp( val.psz_string, "spatial" ) )
1091 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
1093 else if( !strcmp( val.psz_string, "temporal" ) )
1095 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
1097 #if X264_BUILD >= 45 /* r457 */
1098 else if( !strcmp( val.psz_string, "auto" ) )
1100 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
1103 free( val.psz_string );
1105 var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
1106 p_sys->param.analyse.b_psnr = val.b_bool;
1108 #if X264_BUILD >= 50 /* r554 */
1109 var_Get( p_enc, SOUT_CFG_PREFIX "ssim", &val );
1110 p_sys->param.analyse.b_ssim = val.b_bool;
1113 #if X264_BUILD >= 0x0012
1114 var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
1115 p_sys->param.analyse.b_weighted_bipred = val.b_bool;
1118 #if X264_BUILD >= 0x0013
1119 var_Get( p_enc, SOUT_CFG_PREFIX "b-adapt", &val );
1120 #if X264_BUILD >= 63
1121 p_sys->param.i_bframe_adaptive = val.i_int;
1123 p_sys->param.b_bframe_adaptive = val.b_bool;
1126 var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
1127 if( val.i_int >= -100 && val.i_int <= 100 )
1128 p_sys->param.i_bframe_bias = val.i_int;
1131 #if X264_BUILD >= 23
1132 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-me", &val );
1133 p_sys->param.analyse.b_chroma_me = val.b_bool;
1134 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-qp-offset", &val );
1135 p_sys->param.analyse.i_chroma_qp_offset = val.i_int;
1138 #if X264_BUILD >= 36
1139 var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
1140 p_sys->param.analyse.b_mixed_references = val.b_bool;
1144 #if X264_BUILD >= 39
1145 var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
1146 if( val.i_int >= 0 && val.i_int <= 2 )
1147 p_sys->param.analyse.i_trellis = val.i_int;
1150 #if X264_BUILD >= 41 && X264_BUILD < 65
1151 var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
1152 p_sys->param.analyse.b_bframe_rdo = val.b_bool;
1155 #if X264_BUILD >= 42
1156 var_Get( p_enc, SOUT_CFG_PREFIX "fast-pskip", &val );
1157 p_sys->param.analyse.b_fast_pskip = val.b_bool;
1160 #if X264_BUILD >= 43 && X264_BUILD < 65
1161 var_Get( p_enc, SOUT_CFG_PREFIX "bime", &val );
1162 p_sys->param.analyse.b_bidir_me = val.b_bool;
1165 #if X264_BUILD >= 44
1166 var_Get( p_enc, SOUT_CFG_PREFIX "nr", &val );
1167 if( val.i_int >= 0 && val.i_int <= 1000 )
1168 p_sys->param.analyse.i_noise_reduction = val.i_int;
1171 #if X264_BUILD >= 46
1172 var_Get( p_enc, SOUT_CFG_PREFIX "dct-decimate", &val );
1173 p_sys->param.analyse.b_dct_decimate = val.b_bool;
1176 #if X264_BUILD >= 52
1177 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-inter", &val );
1178 if( val.i_int >= 0 && val.i_int <= 32 )
1179 p_sys->param.analyse.i_luma_deadzone[0] = val.i_int;
1181 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-intra", &val );
1182 if( val.i_int >= 0 && val.i_int <= 32 )
1183 p_sys->param.analyse.i_luma_deadzone[1] = val.i_int;
1185 #if X264_BUILD <= 65
1186 var_Get( p_enc, SOUT_CFG_PREFIX "direct-8x8", &val );
1187 if( val.i_int >= -1 && val.i_int <= 1 )
1188 p_sys->param.analyse.i_direct_8x8_inference = val.i_int;
1192 var_Get( p_enc, SOUT_CFG_PREFIX "asm", &val );
1193 if( !val.b_bool ) p_sys->param.cpu = 0;
1195 #ifndef X264_ANALYSE_BSUB16x16
1196 # define X264_ANALYSE_BSUB16x16 0
1198 var_Get( p_enc, SOUT_CFG_PREFIX "partitions", &val );
1199 if( !strcmp( val.psz_string, "none" ) )
1201 p_sys->param.analyse.inter = 0;
1203 else if( !strcmp( val.psz_string, "fast" ) )
1205 p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
1207 else if( !strcmp( val.psz_string, "normal" ) )
1209 p_sys->param.analyse.inter =
1211 X264_ANALYSE_PSUB16x16;
1212 #ifdef X264_ANALYSE_I8x8
1213 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1216 else if( !strcmp( val.psz_string, "slow" ) )
1218 p_sys->param.analyse.inter =
1220 X264_ANALYSE_PSUB16x16 |
1221 X264_ANALYSE_BSUB16x16;
1222 #ifdef X264_ANALYSE_I8x8
1223 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1226 else if( !strcmp( val.psz_string, "all" ) )
1228 p_sys->param.analyse.inter = ~0;
1230 free( val.psz_string );
1232 #if X264_BUILD >= 30
1233 var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
1234 p_sys->param.analyse.b_transform_8x8 = val.b_bool;
1237 if( p_enc->fmt_in.video.i_aspect > 0 )
1239 int64_t i_num, i_den;
1240 unsigned int i_dst_num, i_dst_den;
1242 i_num = p_enc->fmt_in.video.i_aspect *
1243 (int64_t)p_enc->fmt_in.video.i_height;
1244 i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
1245 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1247 p_sys->param.vui.i_sar_width = i_dst_num;
1248 p_sys->param.vui.i_sar_height = i_dst_den;
1251 if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
1253 p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
1254 p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
1257 /* x264 vbv-bufsize = 0 (default). if not provided set period
1258 in seconds for local maximum bitrate (cache/bufsize) based
1259 on average bitrate when use has told bitrate.
1260 vbv-buffer size is set to bitrate * secods between keyframes */
1261 if( !p_sys->param.rc.i_vbv_buffer_size &&
1262 p_sys->param.rc.i_rc_method == X264_RC_ABR &&
1263 p_sys->param.i_fps_num )
1265 p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate *
1266 p_sys->param.i_fps_den;
1267 #if X264_BUILD >= 0x000e
1268 p_sys->param.rc.i_vbv_buffer_size *= p_sys->param.i_keyint_max;
1270 p_sys->param.rc.i_vbv_buffer_size *= p_sys->param.i_idrframe;
1272 p_sys->param.rc.i_vbv_buffer_size /= p_sys->param.i_fps_num;
1276 unsigned i_cpu = vlc_CPU();
1277 if( !(i_cpu & CPU_CAPABILITY_MMX) )
1279 p_sys->param.cpu &= ~X264_CPU_MMX;
1281 if( !(i_cpu & CPU_CAPABILITY_MMXEXT) )
1283 p_sys->param.cpu &= ~X264_CPU_MMXEXT;
1285 if( !(i_cpu & CPU_CAPABILITY_SSE) )
1287 p_sys->param.cpu &= ~X264_CPU_SSE;
1289 if( !(i_cpu & CPU_CAPABILITY_SSE2) )
1291 p_sys->param.cpu &= ~X264_CPU_SSE2;
1294 /* BUILD 29 adds support for multi-threaded encoding while BUILD 49 (r543)
1295 also adds support for threads = 0 for automatically selecting an optimal
1296 value (cores * 1.5) based on detected CPUs. Default behavior for x264 is
1297 threads = 1, however VLC usage differs and uses threads = 0 (auto) by
1298 default unless ofcourse transcode threads is explicitly specified.. */
1299 #if X264_BUILD >= 29
1300 p_sys->param.i_threads = p_enc->i_threads;
1303 var_Get( p_enc, SOUT_CFG_PREFIX "stats", &val );
1304 if( val.psz_string )
1306 p_sys->param.rc.psz_stat_in =
1307 p_sys->param.rc.psz_stat_out =
1308 p_sys->psz_stat_name = val.psz_string;
1311 var_Get( p_enc, SOUT_CFG_PREFIX "pass", &val );
1312 if( val.i_int > 0 && val.i_int <= 3 )
1314 p_sys->param.rc.b_stat_write = val.i_int & 1;
1315 p_sys->param.rc.b_stat_read = val.i_int & 2;
1318 /* We need to initialize pthreadw32 before we open the encoder,
1319 but only once for the whole application. Since pthreadw32
1320 doesn't keep a refcount, do it ourselves. */
1321 #ifdef PTW32_STATIC_LIB
1322 vlc_value_t lock, count;
1324 var_Create( p_enc->p_libvlc, "pthread_win32_mutex", VLC_VAR_MUTEX );
1325 var_Get( p_enc->p_libvlc, "pthread_win32_mutex", &lock );
1326 vlc_mutex_lock( lock.p_address );
1328 var_Create( p_enc->p_libvlc, "pthread_win32_count", VLC_VAR_INTEGER );
1329 var_Get( p_enc->p_libvlc, "pthread_win32_count", &count );
1331 if( count.i_int == 0 )
1333 msg_Dbg( p_enc, "initializing pthread-win32" );
1334 if( !pthread_win32_process_attach_np() || !pthread_win32_thread_attach_np() )
1336 msg_Warn( p_enc, "pthread Win32 Initialization failed" );
1337 vlc_mutex_unlock( lock.p_address );
1338 return VLC_EGENERIC;
1343 var_Set( p_enc->p_libvlc, "pthread_win32_count", count );
1344 vlc_mutex_unlock( lock.p_address );
1348 /* Open the encoder */
1349 p_sys->h = x264_encoder_open( &p_sys->param );
1351 if( p_sys->h == NULL )
1353 msg_Err( p_enc, "cannot open x264 encoder" );
1354 Close( VLC_OBJECT(p_enc) );
1355 return VLC_EGENERIC;
1359 p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
1360 p_enc->fmt_in.video.i_height + 1000;
1361 p_sys->p_buffer = malloc( p_sys->i_buffer );
1362 if( !p_sys->p_buffer )
1364 Close( VLC_OBJECT(p_enc) );
1368 /* get the globals headers */
1369 p_enc->fmt_out.i_extra = 0;
1370 p_enc->fmt_out.p_extra = NULL;
1372 x264_encoder_headers( p_sys->h, &nal, &i_nal );
1373 for( i = 0; i < i_nal; i++ )
1376 int i_size = p_sys->i_buffer;
1378 x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
1380 p_tmp = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
1383 Close( VLC_OBJECT(p_enc) );
1386 p_enc->fmt_out.p_extra = p_tmp;
1388 memcpy( (uint8_t*)p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
1389 p_sys->p_buffer, i_size );
1391 p_enc->fmt_out.i_extra += i_size;
1397 /****************************************************************************
1399 ****************************************************************************/
1400 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
1402 encoder_sys_t *p_sys = p_enc->p_sys;
1406 int i_nal, i_out, i;
1409 memset( &pic, 0, sizeof( x264_picture_t ) );
1410 pic.i_pts = p_pict->date;
1411 pic.img.i_csp = X264_CSP_I420;
1412 pic.img.i_plane = p_pict->i_planes;
1413 for( i = 0; i < p_pict->i_planes; i++ )
1415 pic.img.plane[i] = p_pict->p[i].p_pixels;
1416 pic.img.i_stride[i] = p_pict->p[i].i_pitch;
1419 #if X264_BUILD >= 0x0013
1420 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
1422 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
1425 if( !i_nal ) return NULL;
1427 for( i = 0, i_out = 0; i < i_nal; i++ )
1429 int i_size = p_sys->i_buffer - i_out;
1430 x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
1435 p_block = block_New( p_enc, i_out );
1436 if( !p_block ) return NULL;
1437 memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
1439 if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
1440 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1441 else if( pic.i_type == X264_TYPE_P )
1442 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1443 else if( pic.i_type == X264_TYPE_B )
1444 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1446 /* This isn't really valid for streams with B-frames */
1447 p_block->i_length = INT64_C(1000000) *
1448 p_enc->fmt_in.video.i_frame_rate_base /
1449 p_enc->fmt_in.video.i_frame_rate;
1451 p_block->i_pts = pic.i_pts;
1453 if( p_sys->param.i_bframe > 0 )
1455 if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
1457 /* FIXME : this is wrong if bpyramid is set */
1458 p_block->i_dts = p_block->i_pts;
1459 p_sys->i_interpolated_dts = p_block->i_dts;
1463 if( p_sys->i_interpolated_dts )
1465 p_block->i_dts = p_sys->i_interpolated_dts;
1469 /* Let's put something sensible */
1470 p_block->i_dts = p_block->i_pts;
1473 p_sys->i_interpolated_dts += p_block->i_length;
1478 p_block->i_dts = p_block->i_pts;
1484 /*****************************************************************************
1485 * CloseEncoder: x264 encoder destruction
1486 *****************************************************************************/
1487 static void Close( vlc_object_t *p_this )
1489 encoder_t *p_enc = (encoder_t *)p_this;
1490 encoder_sys_t *p_sys = p_enc->p_sys;
1492 free( p_sys->psz_stat_name );
1495 x264_encoder_close( p_sys->h );
1497 #ifdef PTW32_STATIC_LIB
1498 vlc_value_t lock, count;
1500 var_Get( p_enc->p_libvlc, "pthread_win32_mutex", &lock );
1501 vlc_mutex_lock( lock.p_address );
1503 var_Get( p_enc->p_libvlc, "pthread_win32_count", &count );
1505 var_Set( p_enc->p_libvlc, "pthread_win32_count", count );
1507 if( count.i_int == 0 )
1509 pthread_win32_thread_detach_np();
1510 pthread_win32_process_detach_np();
1511 msg_Dbg( p_enc, "pthread-win32 deinitialized" );
1513 vlc_mutex_unlock( lock.p_address );
1516 free( p_sys->p_buffer );