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