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