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