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 *****************************************************************************/
30 #include <vlc_codec.h>
34 #define SOUT_CFG_PREFIX "sout-x264-"
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
42 /* Frame-type options */
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." )
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." )
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." )
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." )
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." )
78 #define B_BIAS_TEXT N_("Influence (bias) B-frames usage")
79 #define B_BIAS_LONGTEXT N_( "Bias the choice to use B-frames. Positive values " \
80 "cause more B-frames, negative values cause less B-frames." )
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." )
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." )
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." )
98 #define NF_TEXT N_("Skip loop filter")
99 #define NF_LONGTEXT N_( "Deactivate the deblocking loop filter (decreases quality).")
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.")
106 #define LEVEL_TEXT N_("H.264 level")
107 #define LEVEL_LONGTEXT N_( "Specify H.264 level (as defined by Annex A " \
108 "of the standard). Levels are not enforced; it's up to the user to select " \
109 "a level compatible with the rest of the encoding options. Range 1 to 5.1 " \
110 "(10 to 51 is also allowed).")
112 /* In order to play an interlaced output stream encoded by x264, a decoder needs
113 mbaff support. r570 is using the 'mb' part and not 'aff' yet; so it's really
114 'pure-interlaced' mode */
115 #if X264_BUILD >= 51 /* r570 */
116 #define INTERLACED_TEXT N_("Interlaced mode")
117 #define INTERLACED_LONGTEXT N_( "Pure-interlaced mode.")
122 #define QP_TEXT N_("Set QP")
123 #define QP_LONGTEXT N_( "This selects the quantizer to use. " \
124 "Lower values result in better fidelity, but higher bitrates. 26 is a " \
125 "good default value. Range 0 (lossless) to 51." )
127 #define CRF_TEXT N_("Quality-based VBR")
128 #define CRF_LONGTEXT N_( "1-pass Quality-based VBR. Range 0 to 51." )
130 #define QPMIN_TEXT N_("Min QP")
131 #define QPMIN_LONGTEXT N_( "Minimum quantizer parameter. 15 to 35 seems to " \
132 "be a useful range." )
134 #define QPMAX_TEXT N_("Max QP")
135 #define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )
137 #define QPSTEP_TEXT N_("Max QP step")
138 #define QPSTEP_LONGTEXT N_( "Max QP step between frames.")
140 #define RATETOL_TEXT N_("Average bitrate tolerance")
141 #define RATETOL_LONGTEXT N_( "Allowed variance in average " \
142 "bitrate (in kbits/s).")
144 #define VBV_MAXRATE_TEXT N_("Max local bitrate")
145 #define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate (in kbits/s).")
147 #define VBV_BUFSIZE_TEXT N_("VBV buffer")
148 #define VBV_BUFSIZE_LONGTEXT N_( "Averaging period for the maximum " \
149 "local bitrate (in kbits).")
151 #define VBV_INIT_TEXT N_("Initial VBV buffer occupancy")
152 #define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
153 "fraction of the buffer size. Range 0.0 to 1.0.")
155 /* IP Ratio < 1 is technically valid but should never improve quality */
156 #define IPRATIO_TEXT N_("QP factor between I and P")
157 #define IPRATIO_LONGTEXT N_( "QP factor between I and P. Range 1.0 to 2.0.")
159 /* PB ratio < 1 is not valid and breaks ratecontrol */
160 #define PBRATIO_TEXT N_("QP factor between P and B")
161 #define PBRATIO_LONGTEXT N_( "QP factor between P and B. Range 1.0 to 2.0.")
163 #define CHROMA_QP_OFFSET_TEXT N_("QP difference between chroma and luma")
164 #define CHROMA_QP_OFFSET_LONGTEXT N_( "QP difference between chroma and luma.")
166 #define QCOMP_TEXT N_("QP curve compression")
167 #define QCOMP_LONGTEXT N_( "QP curve compression. Range 0.0 (CBR) to 1.0 (QCP).")
169 #define CPLXBLUR_TEXT N_("Reduce fluctuations in QP")
170 #define CPLXBLUR_LONGTEXT N_( "This reduces the fluctuations in QP " \
171 "before curve compression. Temporally blurs complexity.")
173 #define QBLUR_TEXT N_("Reduce fluctuations in QP")
174 #define QBLUR_LONGTEXT N_( "This reduces the fluctations in QP " \
175 "after curve compression. Temporally blurs quants.")
179 #define ANALYSE_TEXT N_("Partitions to consider")
180 #define ANALYSE_LONGTEXT N_( "Partitions to consider in analyse mode: \n" \
183 " - normal: i4x4,p8x8,(i8x8)\n" \
184 " - slow : i4x4,p8x8,(i8x8),b8x8\n" \
185 " - all : i4x4,p8x8,(i8x8),b8x8,p4x4\n" \
186 "(p4x4 requires p8x8. i8x8 requires 8x8dct).")
188 #define DIRECT_PRED_TEXT N_("Direct MV prediction mode")
189 #define DIRECT_PRED_LONGTEXT N_( "Direct MV prediction mode.")
191 #if X264_BUILD >= 52 /* r573 */
192 #define DIRECT_PRED_SIZE_TEXT N_("Direct prediction size")
193 #define DIRECT_PRED_SIZE_LONGTEXT N_( "Direct prediction size: "\
196 " - -1: smallest possible according to level\n" )
199 #define WEIGHTB_TEXT N_("Weighted prediction for B-frames")
200 #define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")
202 #define ME_TEXT N_("Integer pixel motion estimation method")
203 #define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
204 " - dia: diamond search, radius 1 (fast)\n" \
205 " - hex: hexagonal search, radius 2\n" \
206 " - umh: uneven multi-hexagon search (better but slower)\n" \
207 " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
209 #define MERANGE_TEXT N_("Maximum motion vector search range")
210 #define MERANGE_LONGTEXT N_( "Maximum distance to search for " \
211 "motion estimation, measured from predicted position(s). " \
212 "Default of 16 is good for most footage, high motion sequences may " \
213 "benefit from settings between 24 and 32. Range 0 to 64." )
215 #define SUBME_TEXT N_("Subpixel motion estimation and partition decision " \
217 #if X264_BUILD >= 46 /* r477 */
219 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
220 "tradeoffs involved in the motion estimation decision process " \
221 "(lower = quicker and higher = better quality). Range 1 to 7." )
222 #elif X264_BUILD >= 30 /* r262 */
224 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
225 "tradeoffs involved in the motion estimation decision process " \
226 "(lower = quicker and higher = better quality). Range 1 to 6." )
229 #define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
230 "tradeoffs involved in the motion estimation decision process " \
231 "(lower = quicker and higher = better quality). Range 1 to 5." )
234 #define B_RDO_TEXT N_("RD based mode decision for B-frames")
235 #define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. This " \
236 "requires subme 6 (or higher).")
238 #define MIXED_REFS_TEXT N_("Decide references on a per partition basis")
239 #define MIXED_REFS_LONGTEXT N_( "Allows each 8x8 or 16x8 partition to " \
240 "independently select a reference frame, as opposed to only one ref " \
243 #define CHROMA_ME_TEXT N_("Chroma in motion estimation")
244 #define CHROMA_ME_LONGTEXT N_( "Chroma ME for subpel and mode decision in " \
247 #define BIME_TEXT N_("Jointly optimize both MVs in B-frames")
248 #define BIME_LONGTEXT N_( "Joint bidirectional motion refinement.")
250 #define TRANSFORM_8X8DCT_TEXT N_("Adaptive spatial transform size")
251 #define TRANSFORM_8X8DCT_LONGTEXT N_( \
252 "SATD-based decision for 8x8 transform in inter-MBs.")
254 #define TRELLIS_TEXT N_("Trellis RD quantization" )
255 #define TRELLIS_LONGTEXT N_( "Trellis RD quantization: \n" \
257 " - 1: enabled only on the final encode of a MB\n" \
258 " - 2: enabled on all mode decisions\n" \
259 "This requires CABAC." )
261 #define FAST_PSKIP_TEXT N_("Early SKIP detection on P-frames")
262 #define FAST_PSKIP_LONGTEXT N_( "Early SKIP detection on P-frames.")
264 #define DCT_DECIMATE_TEXT N_("Coefficient thresholding on P-frames")
265 #define DCT_DECIMATE_LONGTEXT N_( "Coefficient thresholding on P-frames." \
266 "Eliminate dct blocks containing only a small single coefficient.")
268 /* Noise reduction 1 is too weak to measure, suggest at least 10 */
269 #define NR_TEXT N_("Noise reduction")
270 #define NR_LONGTEXT N_( "Dct-domain noise reduction. Adaptive pseudo-deadzone. " \
271 "10 to 1000 seems to be a useful range." )
273 #if X264_BUILD >= 52 /* r573 */
274 #define DEADZONE_INTER_TEXT N_("Inter luma quantization deadzone")
275 #define DEADZONE_INTER_LONGTEXT N_( "Set the size of the inter luma quantization deadzone. " \
278 #define DEADZONE_INTRA_TEXT N_("Intra luma quantization deadzone")
279 #define DEADZONE_INTRA_LONGTEXT N_( "Set the size of the intra luma quantization deadzone. " \
285 #define ASM_TEXT N_("CPU optimizations")
286 #define ASM_LONGTEXT N_( "Use assembler CPU optimizations.")
288 #define PSNR_TEXT N_("PSNR computation")
289 #define PSNR_LONGTEXT N_( "Compute and print PSNR stats. This has no effect on " \
290 "the actual encoding quality." )
292 #define SSIM_TEXT N_("SSIM computation")
293 #define SSIM_LONGTEXT N_( "Compute and print SSIM stats. This has no effect on " \
294 "the actual encoding quality." )
296 #define QUIET_TEXT N_("Quiet mode")
297 #define QUIET_LONGTEXT N_( "Quiet mode.")
299 #define VERBOSE_TEXT N_("Statistics")
300 #define VERBOSE_LONGTEXT N_( "Print stats for each frame.")
302 #if X264_BUILD >= 47 /* r518 */
303 #define SPS_ID_TEXT N_("SPS and PPS id numbers")
304 #define SPS_ID_LONGTEXT N_( "Set SPS and PPS id numbers to allow concatenating " \
305 "streams with different settings.")
308 #define AUD_TEXT N_("Access unit delimiters")
309 #define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")
312 static char *enc_me_list[] =
313 { "dia", "hex", "umh", "esa" };
314 static char *enc_me_list_text[] =
315 { N_("dia"), N_("hex"), N_("umh"), N_("esa") };
318 static char *enc_analyse_list[] =
319 { "none", "fast", "normal", "slow", "all" };
320 static char *enc_analyse_list_text[] =
321 { N_("none"), N_("fast"), N_("normal"), N_("slow"), N_("all") };
323 #if X264_BUILD >= 45 /* r457 */
324 static char *direct_pred_list[] =
325 { "none", "spatial", "temporal", "auto" };
326 static char *direct_pred_list_text[] =
327 { N_("none"), N_("spatial"), N_("temporal"), N_("auto") };
329 static char *direct_pred_list[] =
330 { "none", "spatial", "temporal" };
331 static char *direct_pred_list_text[] =
332 { N_("none"), N_("spatial"), N_("temporal") };
336 set_description( _("H.264/MPEG4 AVC encoder (using x264 library)"));
337 set_capability( "encoder", 200 );
338 set_callbacks( Open, Close );
339 set_category( CAT_INPUT );
340 set_subcategory( SUBCAT_INPUT_VCODEC );
342 /* Frame-type options */
344 add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
345 KEYINT_LONGTEXT, VLC_FALSE );
347 add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
348 MIN_KEYINT_LONGTEXT, VLC_FALSE );
349 add_deprecated( SOUT_CFG_PREFIX "keyint-min", VLC_FALSE ); /* Deprecated since 0.8.5 */
351 add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
352 SCENE_LONGTEXT, VLC_FALSE );
353 change_integer_range( -1, 100 );
355 add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
356 BFRAMES_LONGTEXT, VLC_FALSE );
357 change_integer_range( 0, 16 );
359 #if X264_BUILD >= 0x0013 /* r137 */
360 add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
361 B_ADAPT_LONGTEXT, VLC_FALSE );
363 add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
364 B_BIAS_LONGTEXT, VLC_FALSE );
365 change_integer_range( -100, 100 );
368 add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
369 BPYRAMID_LONGTEXT, VLC_FALSE );
371 add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
374 add_integer( SOUT_CFG_PREFIX "ref", 1, NULL, REF_TEXT,
375 REF_LONGTEXT, VLC_FALSE );
376 change_integer_range( 1, 16 );
377 add_deprecated( SOUT_CFG_PREFIX "frameref", VLC_FALSE ); /* Deprecated since 0.8.5 */
379 add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
380 NF_LONGTEXT, VLC_FALSE );
381 add_deprecated( SOUT_CFG_PREFIX "loopfilter", VLC_FALSE ); /* Deprecated since 0.8.5 */
383 add_string( SOUT_CFG_PREFIX "deblock", "0:0", NULL, FILTER_TEXT,
384 FILTER_LONGTEXT, VLC_FALSE );
385 add_deprecated( SOUT_CFG_PREFIX "filter", VLC_FALSE ); /* Deprecated since 0.8.6 */
387 add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
388 LEVEL_LONGTEXT, VLC_FALSE );
390 #if X264_BUILD >= 51 /* r570 */
391 add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
397 add_integer( SOUT_CFG_PREFIX "qp", 26, NULL, QP_TEXT, QP_LONGTEXT,
399 change_integer_range( 0, 51 ); /* QP 0 -> lossless encoding */
401 #if X264_BUILD >= 37 /* r334 */
402 add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
403 CRF_LONGTEXT, VLC_FALSE );
404 change_integer_range( 0, 51 );
407 add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
408 QPMIN_LONGTEXT, VLC_FALSE );
409 change_integer_range( 0, 51 );
410 add_deprecated( SOUT_CFG_PREFIX "qp-min", VLC_FALSE ); /* Deprecated since 0.8.5 */
412 add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
413 QPMAX_LONGTEXT, VLC_FALSE );
414 change_integer_range( 0, 51 );
415 add_deprecated( SOUT_CFG_PREFIX "qp-max", VLC_FALSE ); /* Deprecated since 0.8.5 */
417 add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
418 QPSTEP_LONGTEXT, VLC_FALSE );
419 change_integer_range( 0, 51 );
421 add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
422 RATETOL_LONGTEXT, VLC_FALSE );
423 change_float_range( 0, 100 );
424 add_deprecated( SOUT_CFG_PREFIX "tolerance", VLC_FALSE ); /* Deprecated since 0.8.5 */
426 add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
427 VBV_MAXRATE_LONGTEXT, VLC_FALSE );
429 add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
430 VBV_BUFSIZE_LONGTEXT, VLC_FALSE );
432 add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
433 VBV_INIT_LONGTEXT, VLC_FALSE );
434 change_float_range( 0, 1 );
436 add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
437 IPRATIO_LONGTEXT, VLC_FALSE );
438 change_float_range( 1, 2 );
440 add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
441 PBRATIO_LONGTEXT, VLC_FALSE );
442 change_float_range( 1, 2 );
444 #if X264_BUILD >= 23 /* r190 */
445 add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
446 CHROMA_QP_OFFSET_LONGTEXT, VLC_FALSE );
449 add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
450 QCOMP_LONGTEXT, VLC_FALSE );
451 change_float_range( 0, 1 );
453 add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
454 CPLXBLUR_LONGTEXT, VLC_FALSE );
456 add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
457 QBLUR_LONGTEXT, VLC_FALSE );
461 /* x264 partitions = none (default). set at least "normal" mode. */
462 add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
463 ANALYSE_LONGTEXT, VLC_FALSE );
464 change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
465 add_deprecated( SOUT_CFG_PREFIX "analyse", VLC_FALSE ); /* Deprecated since 0.8.6 */
467 add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
468 DIRECT_PRED_LONGTEXT, VLC_FALSE );
469 change_string_list( direct_pred_list, direct_pred_list_text, 0 );
471 #if X264_BUILD >= 52 /* r573 */
472 add_integer( SOUT_CFG_PREFIX "direct-8x8", 0, NULL, DIRECT_PRED_SIZE_TEXT,
473 DIRECT_PRED_SIZE_LONGTEXT, VLC_FALSE );
474 change_integer_range( -1, 1 );
477 #if X264_BUILD >= 0x0012 /* r134 */
478 add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
479 WEIGHTB_LONGTEXT, VLC_FALSE );
482 #if X264_BUILD >= 24 /* r221 */
483 add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
484 ME_LONGTEXT, VLC_FALSE );
485 change_string_list( enc_me_list, enc_me_list_text, 0 );
487 add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
488 MERANGE_LONGTEXT, VLC_FALSE );
489 change_integer_range( 1, 64 );
492 add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
493 SUBME_LONGTEXT, VLC_FALSE );
494 change_integer_range( 1, SUBME_MAX );
495 add_deprecated( SOUT_CFG_PREFIX "subpel", VLC_FALSE ); /* Deprecated since 0.8.5 */
497 #if X264_BUILD >= 41 /* r368 */
498 add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
499 B_RDO_LONGTEXT, VLC_FALSE );
502 #if X264_BUILD >= 36 /* r318 */
503 add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
504 MIXED_REFS_LONGTEXT, VLC_FALSE );
507 #if X264_BUILD >= 23 /* r171 */
508 add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
509 CHROMA_ME_LONGTEXT, VLC_FALSE );
512 #if X264_BUILD >= 43 /* r390 */
513 add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
514 BIME_LONGTEXT, VLC_FALSE );
517 #if X264_BUILD >= 30 /* r251 */
518 add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
519 TRANSFORM_8X8DCT_LONGTEXT, VLC_FALSE );
522 #if X264_BUILD >= 39 /* r360 */
523 add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
524 TRELLIS_LONGTEXT, VLC_FALSE );
525 change_integer_range( 0, 2 );
528 #if X264_BUILD >= 42 /* r384 */
529 add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
530 FAST_PSKIP_LONGTEXT, VLC_FALSE );
533 #if X264_BUILD >= 46 /* r503 */
534 add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
535 DCT_DECIMATE_LONGTEXT, VLC_FALSE );
538 #if X264_BUILD >= 44 /* r398 */
539 add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
540 NR_LONGTEXT, VLC_FALSE );
541 change_integer_range( 0, 1000 );
544 #if X264_BUILD >= 52 /* r573 */
545 add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
546 DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
547 change_integer_range( 0, 32 );
549 add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
550 DEADZONE_INTRA_LONGTEXT, VLC_FALSE );
551 change_integer_range( 0, 32 );
556 #if defined(__DARWIN__) && defined(__INTEL__)
557 add_bool( SOUT_CFG_PREFIX "asm", 0, NULL, ASM_TEXT,
558 ASM_LONGTEXT, VLC_FALSE );
560 add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
561 ASM_LONGTEXT, VLC_FALSE );
564 /* x264 psnr = 1 (default). disable PSNR computation for speed. */
565 add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
566 PSNR_LONGTEXT, VLC_FALSE );
568 #if X264_BUILD >= 50 /* r554 */
569 /* x264 ssim = 1 (default). disable SSIM computation for speed. */
570 add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
571 SSIM_LONGTEXT, VLC_FALSE );
574 add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
575 QUIET_LONGTEXT, VLC_FALSE );
577 #if X264_BUILD >= 47 /* r518 */
578 add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
579 SPS_ID_LONGTEXT, VLC_FALSE );
582 add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
583 AUD_LONGTEXT, VLC_FALSE );
585 #if X264_BUILD >= 0x000e /* r81 */
586 add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
587 VERBOSE_LONGTEXT, VLC_FALSE );
592 /*****************************************************************************
594 *****************************************************************************/
595 static const char *ppsz_sout_options[] = {
596 "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
597 "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
598 "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
599 "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
600 "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
601 "me", "merange", "min-keyint", "mixed-refs", "nf", "nr", "partitions",
602 "pbratio", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax", "qpmin",
603 "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut", "sps-id",
604 "ssim", "subme", "subpel", "tolerance", "trellis", "verbose",
605 "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", NULL
608 static block_t *Encode( encoder_t *, picture_t * );
618 mtime_t i_last_ref_pts;
621 /*****************************************************************************
622 * Open: probe the encoder
623 *****************************************************************************/
624 static int Open ( vlc_object_t *p_this )
626 encoder_t *p_enc = (encoder_t *)p_this;
627 encoder_sys_t *p_sys;
629 int i_qmin = 0, i_qmax = 0;
633 if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
640 if( p_enc->fmt_in.video.i_width % 16 != 0 ||
641 p_enc->fmt_in.video.i_height % 16 != 0 )
643 msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
644 p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );
646 if( p_enc->fmt_in.video.i_width < 16 ||
647 p_enc->fmt_in.video.i_height < 16 )
649 msg_Err( p_enc, "video is too small to be cropped" );
653 msg_Warn( p_enc, "cropping video to %ix%i",
654 p_enc->fmt_in.video.i_width >> 4 << 4,
655 p_enc->fmt_in.video.i_height >> 4 << 4 );
659 config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
661 p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
662 p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
664 p_enc->pf_encode_video = Encode;
665 p_enc->pf_encode_audio = NULL;
666 p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
667 p_sys->i_last_ref_pts = 0;
669 x264_param_default( &p_sys->param );
670 p_sys->param.i_width = p_enc->fmt_in.video.i_width;
671 p_sys->param.i_height = p_enc->fmt_in.video.i_height;
673 p_sys->param.i_width = p_sys->param.i_width >> 4 << 4;
674 p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
677 /* average bitrate specified by transcode vb */
678 p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;
681 /* cbr = 1 overrides qp or crf and sets an average bitrate
682 but maxrate = average bitrate is needed for "real" CBR */
683 if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.b_cbr = 1;
685 if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.i_rc_method = X264_RC_ABR;
688 var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
689 if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
690 var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
691 if( val.i_int >= 0 && val.i_int <= 51 ) i_qmin = val.i_int;
692 var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
693 if( val.i_int >= 0 && val.i_int <= 51 ) i_qmax = val.i_int;
695 var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
696 if( val.i_int >= 0 && val.i_int <= 51 )
698 if( i_qmin > val.i_int ) i_qmin = val.i_int;
699 if( i_qmax < val.i_int ) i_qmax = val.i_int;
701 #if X264_BUILD >= 0x000a
702 p_sys->param.rc.i_qp_constant = val.i_int;
703 p_sys->param.rc.i_qp_min = i_qmin;
704 p_sys->param.rc.i_qp_max = i_qmax;
706 p_sys->param.i_qp_constant = val.i_int;
711 var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
712 p_sys->param.rc.f_rate_tolerance = val.f_float;
714 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
715 p_sys->param.rc.f_vbv_buffer_init = val.f_float;
717 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
718 p_sys->param.rc.i_vbv_buffer_size = val.i_int;
720 /* x264 vbv-bufsize = 0 (default). if not provided set period
721 in seconds for local maximum bitrate (cache/bufsize) based
722 on average bitrate. */
724 p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate * 2;
726 /* max bitrate = average bitrate -> CBR */
727 var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
728 p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
731 p_sys->param.rc.i_rc_buffer_size = p_sys->param.rc.i_bitrate;
732 p_sys->param.rc.i_rc_init_buffer = p_sys->param.rc.i_bitrate / 4;
735 var_Get( p_enc, SOUT_CFG_PREFIX "cabac", &val );
736 p_sys->param.b_cabac = val.b_bool;
738 /* disable deblocking when nf (no loop filter) is enabled */
739 var_Get( p_enc, SOUT_CFG_PREFIX "nf", &val );
740 p_sys->param.b_deblocking_filter = !val.b_bool;
742 var_Get( p_enc, SOUT_CFG_PREFIX "deblock", &val );
745 char *p = strchr( val.psz_string, ':' );
746 p_sys->param.i_deblocking_filter_alphac0 = atoi( val.psz_string );
747 p_sys->param.i_deblocking_filter_beta = p ? atoi( p+1 ) : p_sys->param.i_deblocking_filter_alphac0;
748 free( val.psz_string );
751 var_Get( p_enc, SOUT_CFG_PREFIX "level", &val );
754 if( atof (val.psz_string) < 6 )
755 p_sys->param.i_level_idc = (int) ( 10 * atof (val.psz_string) + .5);
757 p_sys->param.i_level_idc = atoi (val.psz_string);
758 free( val.psz_string );
761 #if X264_BUILD >= 51 /* r570 */
762 var_Get( p_enc, SOUT_CFG_PREFIX "interlaced", &val );
763 p_sys->param.b_interlaced = val.b_bool;
766 var_Get( p_enc, SOUT_CFG_PREFIX "ipratio", &val );
767 p_sys->param.rc.f_ip_factor = val.f_float;
769 var_Get( p_enc, SOUT_CFG_PREFIX "pbratio", &val );
770 p_sys->param.rc.f_pb_factor = val.f_float;
772 var_Get( p_enc, SOUT_CFG_PREFIX "qcomp", &val );
773 p_sys->param.rc.f_qcompress = val.f_float;
775 var_Get( p_enc, SOUT_CFG_PREFIX "cplxblur", &val );
776 p_sys->param.rc.f_complexity_blur = val.f_float;
778 var_Get( p_enc, SOUT_CFG_PREFIX "qblur", &val );
779 p_sys->param.rc.f_qblur = val.f_float;
781 #if X264_BUILD >= 0x000e
782 var_Get( p_enc, SOUT_CFG_PREFIX "verbose", &val );
783 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_DEBUG;
786 var_Get( p_enc, SOUT_CFG_PREFIX "quiet", &val );
787 if( val.b_bool ) p_sys->param.i_log_level = X264_LOG_NONE;
789 #if X264_BUILD >= 47 /* r518 */
790 var_Get( p_enc, SOUT_CFG_PREFIX "sps-id", &val );
791 if( val.i_int >= 0 ) p_sys->param.i_sps_id = val.i_int;
794 var_Get( p_enc, SOUT_CFG_PREFIX "aud", &val );
795 if( val.b_bool ) p_sys->param.b_aud = val.b_bool;
797 var_Get( p_enc, SOUT_CFG_PREFIX "keyint", &val );
798 #if X264_BUILD >= 0x000e
799 if( val.i_int > 0 ) p_sys->param.i_keyint_max = val.i_int;
801 if( val.i_int > 0 ) p_sys->param.i_iframe = val.i_int;
804 var_Get( p_enc, SOUT_CFG_PREFIX "min-keyint", &val );
805 #if X264_BUILD >= 0x000e
806 if( val.i_int > 0 ) p_sys->param.i_keyint_min = val.i_int;
808 if( val.i_int > 0 ) p_sys->param.i_idrframe = val.i_int;
811 var_Get( p_enc, SOUT_CFG_PREFIX "bframes", &val );
812 if( val.i_int >= 0 && val.i_int <= 16 )
813 p_sys->param.i_bframe = val.i_int;
816 var_Get( p_enc, SOUT_CFG_PREFIX "bpyramid", &val );
817 p_sys->param.b_bframe_pyramid = val.b_bool;
820 var_Get( p_enc, SOUT_CFG_PREFIX "ref", &val );
821 if( val.i_int > 0 && val.i_int <= 15 )
822 p_sys->param.i_frame_reference = val.i_int;
824 var_Get( p_enc, SOUT_CFG_PREFIX "scenecut", &val );
825 #if X264_BUILD >= 0x000b
826 if( val.i_int >= -1 && val.i_int <= 100 )
827 p_sys->param.i_scenecut_threshold = val.i_int;
830 var_Get( p_enc, SOUT_CFG_PREFIX "subme", &val );
831 if( val.i_int >= 1 && val.i_int <= SUBME_MAX )
832 p_sys->param.analyse.i_subpel_refine = val.i_int;
835 var_Get( p_enc, SOUT_CFG_PREFIX "me", &val );
836 if( !strcmp( val.psz_string, "dia" ) )
838 p_sys->param.analyse.i_me_method = X264_ME_DIA;
840 else if( !strcmp( val.psz_string, "hex" ) )
842 p_sys->param.analyse.i_me_method = X264_ME_HEX;
844 else if( !strcmp( val.psz_string, "umh" ) )
846 p_sys->param.analyse.i_me_method = X264_ME_UMH;
848 else if( !strcmp( val.psz_string, "esa" ) )
850 p_sys->param.analyse.i_me_method = X264_ME_ESA;
852 if( val.psz_string ) free( val.psz_string );
854 var_Get( p_enc, SOUT_CFG_PREFIX "merange", &val );
855 if( val.i_int >= 1 && val.i_int <= 64 )
856 p_sys->param.analyse.i_me_range = val.i_int;
859 var_Get( p_enc, SOUT_CFG_PREFIX "direct", &val );
860 if( !strcmp( val.psz_string, "none" ) )
862 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_NONE;
864 else if( !strcmp( val.psz_string, "spatial" ) )
866 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_SPATIAL;
868 else if( !strcmp( val.psz_string, "temporal" ) )
870 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_TEMPORAL;
872 #if X264_BUILD >= 45 /* r457 */
873 else if( !strcmp( val.psz_string, "auto" ) )
875 p_sys->param.analyse.i_direct_mv_pred = X264_DIRECT_PRED_AUTO;
878 if( val.psz_string ) free( val.psz_string );
880 var_Get( p_enc, SOUT_CFG_PREFIX "psnr", &val );
881 p_sys->param.analyse.b_psnr = val.b_bool;
883 #if X264_BUILD >= 50 /* r554 */
884 var_Get( p_enc, SOUT_CFG_PREFIX "ssim", &val );
885 p_sys->param.analyse.b_ssim = val.b_bool;
888 #if X264_BUILD >= 0x0012
889 var_Get( p_enc, SOUT_CFG_PREFIX "weightb", &val );
890 p_sys->param.analyse.b_weighted_bipred = val.b_bool;
893 #if X264_BUILD >= 0x0013
894 var_Get( p_enc, SOUT_CFG_PREFIX "b-adapt", &val );
895 p_sys->param.b_bframe_adaptive = val.b_bool;
897 var_Get( p_enc, SOUT_CFG_PREFIX "b-bias", &val );
898 if( val.i_int >= -100 && val.i_int <= 100 )
899 p_sys->param.i_bframe_bias = val.i_int;
903 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-me", &val );
904 p_sys->param.analyse.b_chroma_me = val.b_bool;
905 var_Get( p_enc, SOUT_CFG_PREFIX "chroma-qp-offset", &val );
906 p_sys->param.analyse.i_chroma_qp_offset = val.i_int;
910 var_Get( p_enc, SOUT_CFG_PREFIX "mixed-refs", &val );
911 p_sys->param.analyse.b_mixed_references = val.b_bool;
915 var_Get( p_enc, SOUT_CFG_PREFIX "crf", &val );
916 if( val.i_int > 0 && val.i_int <= 51 )
919 p_sys->param.rc.f_rf_constant = val.i_int;
921 p_sys->param.rc.i_rf_constant = val.i_int;
924 p_sys->param.rc.i_rc_method = X264_RC_CRF;
930 var_Get( p_enc, SOUT_CFG_PREFIX "trellis", &val );
931 if( val.i_int >= 0 && val.i_int <= 2 )
932 p_sys->param.analyse.i_trellis = val.i_int;
936 var_Get( p_enc, SOUT_CFG_PREFIX "b-rdo", &val );
937 p_sys->param.analyse.b_bframe_rdo = val.b_bool;
941 var_Get( p_enc, SOUT_CFG_PREFIX "fast-pskip", &val );
942 p_sys->param.analyse.b_fast_pskip = val.b_bool;
946 var_Get( p_enc, SOUT_CFG_PREFIX "bime", &val );
947 p_sys->param.analyse.b_bidir_me = val.b_bool;
951 var_Get( p_enc, SOUT_CFG_PREFIX "nr", &val );
952 if( val.i_int >= 0 && val.i_int <= 1000 )
953 p_sys->param.analyse.i_noise_reduction = val.i_int;
957 var_Get( p_enc, SOUT_CFG_PREFIX "dct-decimate", &val );
958 p_sys->param.analyse.b_dct_decimate = val.b_bool;
962 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-inter", &val );
963 if( val.i_int >= 0 && val.i_int <= 32 )
964 p_sys->param.analyse.i_luma_deadzone[0] = val.i_int;
966 var_Get( p_enc, SOUT_CFG_PREFIX "deadzone-intra", &val );
967 if( val.i_int >= 0 && val.i_int <= 32 )
968 p_sys->param.analyse.i_luma_deadzone[1] = val.i_int;
970 var_Get( p_enc, SOUT_CFG_PREFIX "direct-8x8", &val );
971 if( val.i_int >= -1 && val.i_int <= 1 )
972 p_sys->param.analyse.i_direct_8x8_inference = val.i_int;
975 var_Get( p_enc, SOUT_CFG_PREFIX "asm", &val );
976 if( !val.b_bool ) p_sys->param.cpu = 0;
978 #ifndef X264_ANALYSE_BSUB16x16
979 # define X264_ANALYSE_BSUB16x16 0
981 var_Get( p_enc, SOUT_CFG_PREFIX "partitions", &val );
982 if( !strcmp( val.psz_string, "none" ) )
984 p_sys->param.analyse.inter = 0;
986 else if( !strcmp( val.psz_string, "fast" ) )
988 p_sys->param.analyse.inter = X264_ANALYSE_I4x4;
990 else if( !strcmp( val.psz_string, "normal" ) )
992 p_sys->param.analyse.inter =
994 X264_ANALYSE_PSUB16x16;
995 #ifdef X264_ANALYSE_I8x8
996 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
999 else if( !strcmp( val.psz_string, "slow" ) )
1001 p_sys->param.analyse.inter =
1003 X264_ANALYSE_PSUB16x16 |
1004 X264_ANALYSE_BSUB16x16;
1005 #ifdef X264_ANALYSE_I8x8
1006 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1009 else if( !strcmp( val.psz_string, "all" ) )
1011 p_sys->param.analyse.inter =
1013 X264_ANALYSE_PSUB16x16 |
1014 X264_ANALYSE_BSUB16x16 |
1015 X264_ANALYSE_PSUB8x8;
1016 #ifdef X264_ANALYSE_I8x8
1017 p_sys->param.analyse.inter |= X264_ANALYSE_I8x8;
1020 if( val.psz_string ) free( val.psz_string );
1022 #if X264_BUILD >= 30
1023 var_Get( p_enc, SOUT_CFG_PREFIX "8x8dct", &val );
1024 p_sys->param.analyse.b_transform_8x8 = val.b_bool;
1027 if( p_enc->fmt_in.video.i_aspect > 0 )
1029 int64_t i_num, i_den;
1030 unsigned int i_dst_num, i_dst_den;
1032 i_num = p_enc->fmt_in.video.i_aspect *
1033 (int64_t)p_enc->fmt_in.video.i_height;
1034 i_den = VOUT_ASPECT_FACTOR * p_enc->fmt_in.video.i_width;
1035 vlc_ureduce( &i_dst_num, &i_dst_den, i_num, i_den, 0 );
1037 p_sys->param.vui.i_sar_width = i_dst_num;
1038 p_sys->param.vui.i_sar_height = i_dst_den;
1040 if( p_enc->fmt_in.video.i_frame_rate_base > 0 )
1042 p_sys->param.i_fps_num = p_enc->fmt_in.video.i_frame_rate;
1043 p_sys->param.i_fps_den = p_enc->fmt_in.video.i_frame_rate_base;
1045 if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMX) )
1047 p_sys->param.cpu &= ~X264_CPU_MMX;
1049 if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_MMXEXT) )
1051 p_sys->param.cpu &= ~X264_CPU_MMXEXT;
1053 if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_SSE) )
1055 p_sys->param.cpu &= ~X264_CPU_SSE;
1057 if( !(p_enc->p_libvlc_global->i_cpu & CPU_CAPABILITY_SSE2) )
1059 p_sys->param.cpu &= ~X264_CPU_SSE2;
1062 #if X264_BUILD >= 29
1063 /* As of r543 x264 will autodetect the number of cpus and will set
1064 the number of threads accordingly unless ofcourse the number of
1065 threads is explicitly specified... */
1066 if( p_enc->i_threads >= 1 )
1067 p_sys->param.i_threads = p_enc->i_threads;
1070 /* Open the encoder */
1071 p_sys->h = x264_encoder_open( &p_sys->param );
1074 p_sys->i_buffer = 4 * p_enc->fmt_in.video.i_width *
1075 p_enc->fmt_in.video.i_height + 1000;
1076 p_sys->p_buffer = malloc( p_sys->i_buffer );
1078 /* get the globals headers */
1079 p_enc->fmt_out.i_extra = 0;
1080 p_enc->fmt_out.p_extra = NULL;
1082 x264_encoder_headers( p_sys->h, &nal, &i_nal );
1083 for( i = 0; i < i_nal; i++ )
1085 int i_size = p_sys->i_buffer;
1087 x264_nal_encode( p_sys->p_buffer, &i_size, 1, &nal[i] );
1089 p_enc->fmt_out.p_extra = realloc( p_enc->fmt_out.p_extra, p_enc->fmt_out.i_extra + i_size );
1091 memcpy( p_enc->fmt_out.p_extra + p_enc->fmt_out.i_extra,
1092 p_sys->p_buffer, i_size );
1094 p_enc->fmt_out.i_extra += i_size;
1100 /****************************************************************************
1102 ****************************************************************************/
1103 static block_t *Encode( encoder_t *p_enc, picture_t *p_pict )
1105 encoder_sys_t *p_sys = p_enc->p_sys;
1109 int i_nal, i_out, i;
1112 memset( &pic, 0, sizeof( x264_picture_t ) );
1113 pic.i_pts = p_pict->date;
1114 pic.img.i_csp = X264_CSP_I420;
1115 pic.img.i_plane = p_pict->i_planes;
1116 for( i = 0; i < p_pict->i_planes; i++ )
1118 pic.img.plane[i] = p_pict->p[i].p_pixels;
1119 pic.img.i_stride[i] = p_pict->p[i].i_pitch;
1122 #if X264_BUILD >= 0x0013
1123 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic, &pic );
1125 x264_encoder_encode( p_sys->h, &nal, &i_nal, &pic );
1128 if( !i_nal ) return NULL;
1130 for( i = 0, i_out = 0; i < i_nal; i++ )
1132 int i_size = p_sys->i_buffer - i_out;
1133 x264_nal_encode( p_sys->p_buffer + i_out, &i_size, 1, &nal[i] );
1138 p_block = block_New( p_enc, i_out );
1139 memcpy( p_block->p_buffer, p_sys->p_buffer, i_out );
1141 if( pic.i_type == X264_TYPE_IDR || pic.i_type == X264_TYPE_I )
1142 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1143 else if( pic.i_type == X264_TYPE_P )
1144 p_block->i_flags |= BLOCK_FLAG_TYPE_P;
1145 else if( pic.i_type == X264_TYPE_B )
1146 p_block->i_flags |= BLOCK_FLAG_TYPE_B;
1148 /* This isn't really valid for streams with B-frames */
1149 p_block->i_length = I64C(1000000) *
1150 p_enc->fmt_in.video.i_frame_rate_base /
1151 p_enc->fmt_in.video.i_frame_rate;
1153 p_block->i_dts = p_block->i_pts = pic.i_pts;
1155 if( p_sys->param.i_bframe > 0 )
1157 if( p_block->i_flags & BLOCK_FLAG_TYPE_B )
1159 p_block->i_dts = p_block->i_pts;
1163 if( p_sys->i_last_ref_pts )
1165 p_block->i_dts = p_sys->i_last_ref_pts;
1169 /* Let's put something sensible */
1170 p_block->i_dts = p_block->i_pts;
1173 p_sys->i_last_ref_pts = p_block->i_pts;
1180 /*****************************************************************************
1181 * CloseEncoder: x264 encoder destruction
1182 *****************************************************************************/
1183 static void Close( vlc_object_t *p_this )
1185 encoder_t *p_enc = (encoder_t *)p_this;
1186 encoder_sys_t *p_sys = p_enc->p_sys;
1188 x264_encoder_close( p_sys->h );
1189 free( p_sys->p_buffer );