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