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