x264.c 50.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * x264.c: h264 video encoder
 *****************************************************************************
4
 * Copyright (C) 2004-2006 the VideoLAN team
5
 * $Id$
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22
23
24
25
26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
33
34
35
#include <vlc_vout.h>
#include <vlc_sout.h>
#include <vlc_codec.h>
36

bigben's avatar
   
bigben committed
37
38
39
#ifdef PTW32_STATIC_LIB
#include <pthread.h>
#endif
40
41
#include <x264.h>

42
43
#define SOUT_CFG_PREFIX "sout-x264-"

44
45
46
47
48
49
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

50
/* Frame-type options */
51

52
53
#define KEYINT_TEXT N_("Maximum GOP size")
#define KEYINT_LONGTEXT N_( "Sets maximum interval between IDR-frames." \
54
55
    "Larger values save bits, thus improving quality for a given bitrate at " \
    "the cost of seeking precision." )
56

57
58
59
60
#define MIN_KEYINT_TEXT N_("Minimum GOP size")
#define MIN_KEYINT_LONGTEXT N_( "Sets minimum interval between IDR-frames. " \
    "In H.264, I-frames do not necessarily bound a closed GOP because it is " \
    "allowable for a P-frame to be predicted from more frames than just the " \
61
62
63
    "one frame before it (also see reference frame option). Therefore, " \
    "I-frames are not necessarily seekable. IDR-frames restrict subsequent " \
    "P-frames from referring to any frame prior to the IDR-frame. \n" \
64
65
66
    "If scenecuts appear within this interval, they are still encoded as " \
    "I-frames, but do not start a new GOP." )

67
#define SCENE_TEXT N_("Extra I-frames aggressivity" )
zorglub's avatar
zorglub committed
68
69
70
71
72
73
#define SCENE_LONGTEXT N_( "Scene-cut detection. Controls how " \
    "aggressively to insert extra I-frames. With small values of " \
    "scenecut, the codec often has " \
    "to force an I-frame when it would exceed keyint. " \
    "Good values of scenecut may find a better location for the " \
    "I-frame. Large values use more I-frames " \
74
    "than necessary, thus wasting bits. -1 disables scene-cut detection, so " \
75
76
    "I-frames are inserted only every other keyint frames, which probably " \
    "leads to ugly encoding artifacts. Range 1 to 100." )
77

78
79
80
81
82
83
#if X264_BUILD >= 55 /* r607 */
#define PRESCENE_TEXT N_("Faster, less precise scenecut detection" )
#define PRESCENE_LONGTEXT N_( "Faster, less precise scenecut detection. " \
    "Required and implied by multi-threading." )
#endif

zorglub's avatar
zorglub committed
84
#define BFRAMES_TEXT N_("B-frames between I and P")
85
#define BFRAMES_LONGTEXT N_( "Number of consecutive B-frames between I and " \
86
    "P-frames. Range 1 to 16." )
87
88

#define B_ADAPT_TEXT N_("Adaptive B-frame decision")
89
90
91
92
93
#if X264_BUILD >= 63
#define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
    "consecutive B-frames to be used, except possibly before an I-frame." \
    "Range 0 to 2." )
#else
94
#define B_ADAPT_LONGTEXT N_( "Force the specified number of " \
95
    "consecutive B-frames to be used, except possibly before an I-frame." )
96
#endif
97

98
#define B_BIAS_TEXT N_("Influence (bias) B-frames usage")
99
#define B_BIAS_LONGTEXT N_( "Bias the choice to use B-frames. Positive values " \
100
    "cause more B-frames, negative values cause less B-frames." )
101
102
103
104
105
106
107
108

#define BPYRAMID_TEXT N_("Keep some B-frames as references")
#define BPYRAMID_LONGTEXT N_( "Allows B-frames to be used as references for " \
    "predicting other frames. Keeps the middle of 2+ consecutive B-frames " \
    "as a reference, and reorders frame appropriately." )

#define CABAC_TEXT N_("CABAC")
#define CABAC_LONGTEXT N_( "CABAC (Context-Adaptive Binary Arithmetic "\
109
    "Coding). Slightly slows down encoding and decoding, but should save " \
110
111
    "10 to 15% bitrate." )

zorglub's avatar
zorglub committed
112
#define REF_TEXT N_("Number of reference frames")
113
114
115
#define REF_LONGTEXT N_( "Number of previous frames used as predictors. " \
    "This is effective in Anime, but seems to make little difference in " \
    "live-action source material. Some decoders are unable to deal with " \
116
    "large frameref values. Range 1 to 16." )
117

118
119
#define NF_TEXT N_("Skip loop filter")
#define NF_LONGTEXT N_( "Deactivate the deblocking loop filter (decreases quality).")
120

121
122
123
124
#define FILTER_TEXT N_("Loop filter AlphaC0 and Beta parameters alpha:beta")
#define FILTER_LONGTEXT N_( "Loop filter AlphaC0 and Beta parameters. " \
    "Range -6 to 6 for both alpha and beta parameters. -6 means light " \
    "filter, 6 means strong.")
125
 
126
127
128
129
130
#define LEVEL_TEXT N_("H.264 level")
#define LEVEL_LONGTEXT N_( "Specify H.264 level (as defined by Annex A " \
    "of the standard). Levels are not enforced; it's up to the user to select " \
    "a level compatible with the rest of the encoding options. Range 1 to 5.1 " \
    "(10 to 51 is also allowed).")
131

132
133
134
135
136
137
138
139
/* In order to play an interlaced output stream encoded by x264, a decoder needs
   mbaff support. r570 is using the 'mb' part and not 'aff' yet; so it's really
   'pure-interlaced' mode */
#if X264_BUILD >= 51 /* r570 */
#define INTERLACED_TEXT N_("Interlaced mode")
#define INTERLACED_LONGTEXT N_( "Pure-interlaced mode.")
#endif

140
141
/* Ratecontrol */

zorglub's avatar
zorglub committed
142
#define QP_TEXT N_("Set QP")
143
144
#define QP_LONGTEXT N_( "This selects the quantizer to use. " \
    "Lower values result in better fidelity, but higher bitrates. 26 is a " \
145
    "good default value. Range 0 (lossless) to 51." )
146

zorglub's avatar
zorglub committed
147
#define CRF_TEXT N_("Quality-based VBR")
148
#define CRF_LONGTEXT N_( "1-pass Quality-based VBR. Range 0 to 51." )
149

zorglub's avatar
zorglub committed
150
#define QPMIN_TEXT N_("Min QP")
151
152
#define QPMIN_LONGTEXT N_( "Minimum quantizer parameter. 15 to 35 seems to " \
    "be a useful range." )
153

Christophe Mutricy's avatar
typo    
Christophe Mutricy committed
154
#define QPMAX_TEXT N_("Max QP")
155
156
#define QPMAX_LONGTEXT N_( "Maximum quantizer parameter." )

Christophe Mutricy's avatar
typo    
Christophe Mutricy committed
157
#define QPSTEP_TEXT N_("Max QP step")
158
159
#define QPSTEP_LONGTEXT N_( "Max QP step between frames.")

160
#define RATETOL_TEXT N_("Average bitrate tolerance")
161
#define RATETOL_LONGTEXT N_( "Allowed variance in average " \
162
    "bitrate (in kbits/s).")
163

164
#define VBV_MAXRATE_TEXT N_("Max local bitrate")
165
#define VBV_MAXRATE_LONGTEXT N_( "Sets a maximum local bitrate (in kbits/s).")
166

167
168
#define VBV_BUFSIZE_TEXT N_("VBV buffer")
#define VBV_BUFSIZE_LONGTEXT N_( "Averaging period for the maximum " \
169
    "local bitrate (in kbits).")
170

171
#define VBV_INIT_TEXT N_("Initial VBV buffer occupancy")
172
#define VBV_INIT_LONGTEXT N_( "Sets the initial buffer occupancy as a " \
173
    "fraction of the buffer size. Range 0.0 to 1.0.")
174

175
176
177
178
179
180
181
182
183
184
185
186
187
188
#if X264_BUILD >= 59
#define AQ_MODE_TEXT N_("How AQ distributes bits")
#define AQ_MODE_LONGTEXT N_("Defines bitdistribution mode for AQ, default 2\n" \
        " - 0: Disabled\n"\
        " - 1: Avoid moving bits between frames\n"\
        " - 2: Move bits between frames")

#define AQ_STRENGTH_TEXT N_("Strength of AQ")
#define AQ_STRENGTH_LONGTEXT N_("Strength to reduce blocking and blurring in flat\n"\
        "and textured areas, default 1.0 recommented to be between 0..2\n"\
        " - 0.5: weak AQ\n"\
        " - 1.5: strong AQ")
#endif

189
/* IP Ratio < 1 is technically valid but should never improve quality */
190
#define IPRATIO_TEXT N_("QP factor between I and P")
191
#define IPRATIO_LONGTEXT N_( "QP factor between I and P. Range 1.0 to 2.0.")
192

193
/* PB ratio < 1 is not valid and breaks ratecontrol */
194
#define PBRATIO_TEXT N_("QP factor between P and B")
195
#define PBRATIO_LONGTEXT N_( "QP factor between P and B. Range 1.0 to 2.0.")
196

197
198
199
#define CHROMA_QP_OFFSET_TEXT N_("QP difference between chroma and luma")
#define CHROMA_QP_OFFSET_LONGTEXT N_( "QP difference between chroma and luma.")

200
201
202
203
204
205
#define PASS_TEXT N_("Multipass ratecontrol")
#define PASS_LONGTEXT N_( "Multipass ratecontrol:\n" \
    " - 1: First pass, creates stats file\n" \
    " - 2: Last pass, does not overwrite stats file\n" \
    " - 3: Nth pass, overwrites stats file\n" )

206
#define QCOMP_TEXT N_("QP curve compression")
207
#define QCOMP_LONGTEXT N_( "QP curve compression. Range 0.0 (CBR) to 1.0 (QCP).")
208

zorglub's avatar
zorglub committed
209
210
#define CPLXBLUR_TEXT N_("Reduce fluctuations in QP")
#define CPLXBLUR_LONGTEXT N_( "This reduces the fluctuations in QP " \
211
    "before curve compression. Temporally blurs complexity.")
212

zorglub's avatar
zorglub committed
213
214
#define QBLUR_TEXT N_("Reduce fluctuations in QP")
#define QBLUR_LONGTEXT N_( "This reduces the fluctations in QP " \
215
    "after curve compression. Temporally blurs quants.")
216
217
218
219
220
221
222
223
224
225
226
227
228

/* Analysis */

#define ANALYSE_TEXT N_("Partitions to consider")
#define ANALYSE_LONGTEXT N_( "Partitions to consider in analyse mode: \n" \
    " - none  : \n" \
    " - fast  : i4x4\n" \
    " - normal: i4x4,p8x8,(i8x8)\n" \
    " - slow  : i4x4,p8x8,(i8x8),b8x8\n" \
    " - all   : i4x4,p8x8,(i8x8),b8x8,p4x4\n" \
    "(p4x4 requires p8x8. i8x8 requires 8x8dct).")

#define DIRECT_PRED_TEXT N_("Direct MV prediction mode")
229
#define DIRECT_PRED_LONGTEXT N_( "Direct MV prediction mode.")
zorglub's avatar
zorglub committed
230

231
232
233
234
235
236
237
238
#if X264_BUILD >= 52 /* r573 */
#define DIRECT_PRED_SIZE_TEXT N_("Direct prediction size")
#define DIRECT_PRED_SIZE_LONGTEXT N_( "Direct prediction size: "\
    " -  0: 4x4\n" \
    " -  1: 8x8\n" \
    " - -1: smallest possible according to level\n" )
#endif

239
240
241
242
#define WEIGHTB_TEXT N_("Weighted prediction for B-frames")
#define WEIGHTB_LONGTEXT N_( "Weighted prediction for B-frames.")

#define ME_TEXT N_("Integer pixel motion estimation method")
243
244
245
246
247
248
249
250
#if X264_BUILD >= 58 /* r728 */
#define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
    " - dia: diamond search, radius 1 (fast)\n" \
    " - hex: hexagonal search, radius 2\n" \
    " - umh: uneven multi-hexagon search (better but slower)\n" \
    " - esa: exhaustive search (extremely slow, primarily for testing)\n" \
    " - tesa: hadamard exhaustive search (extremely slow, primarily for testing)\n" )
#else
251
#define ME_LONGTEXT N_( "Selects the motion estimation algorithm: "\
252
253
254
255
    " - dia: diamond search, radius 1 (fast)\n" \
    " - hex: hexagonal search, radius 2\n" \
    " - umh: uneven multi-hexagon search (better but slower)\n" \
    " - esa: exhaustive search (extremely slow, primarily for testing)\n" )
256
#endif
257

258
#if X264_BUILD >= 24
zorglub's avatar
zorglub committed
259
260
261
#define MERANGE_TEXT N_("Maximum motion vector search range")
#define MERANGE_LONGTEXT N_( "Maximum distance to search for " \
    "motion estimation, measured from predicted position(s). " \
262
263
    "Default of 16 is good for most footage, high motion sequences may " \
    "benefit from settings between 24 and 32. Range 0 to 64." )
264

265
266
267
#define MVRANGE_TEXT N_("Maximum motion vector length")
#define MVRANGE_LONGTEXT N_( "Maximum motion vector length in pixels. " \
    "-1 is automatic, based on level." )
268
269
270
271
272
273
274
#endif

#if X264_BUILD >= 55 /* r607 */
#define MVRANGE_THREAD_TEXT N_("Minimum buffer space between threads")
#define MVRANGE_THREAD_LONGTEXT N_( "Minimum buffer space between threads. " \
    "-1 is automatic, based on number of threads." )
#endif
275

276
#define SUBME_TEXT N_("Subpixel motion estimation and partition decision " \
zorglub's avatar
zorglub committed
277
    "quality")
aballier's avatar
aballier committed
278
279
280
281
282
283
#if X264_BUILD >= 65 
#define SUBME_MAX 9
#define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
    "tradeoffs involved in the motion estimation decision process " \
    "(lower = quicker and higher = better quality). Range 1 to 9." )
#elif X264_BUILD >= 46 /* r477 */
284
285
286
287
288
289
#define SUBME_MAX 7
#define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
    "tradeoffs involved in the motion estimation decision process " \
    "(lower = quicker and higher = better quality). Range 1 to 7." )
#elif X264_BUILD >= 30 /* r262 */
#define SUBME_MAX 6
290
291
#define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
    "tradeoffs involved in the motion estimation decision process " \
292
293
294
295
296
297
298
    "(lower = quicker and higher = better quality). Range 1 to 6." )
#else
#define SUBME_MAX 5
#define SUBME_LONGTEXT N_( "This parameter controls quality versus speed " \
    "tradeoffs involved in the motion estimation decision process " \
    "(lower = quicker and higher = better quality). Range 1 to 5." )
#endif
299

300
#define B_RDO_TEXT N_("RD based mode decision for B-frames")
zorglub's avatar
zorglub committed
301
#define B_RDO_LONGTEXT N_( "RD based mode decision for B-frames. This " \
302
    "requires subme 6 (or higher).")
303

304
#define MIXED_REFS_TEXT N_("Decide references on a per partition basis")
zorglub's avatar
zorglub committed
305
#define MIXED_REFS_LONGTEXT N_( "Allows each 8x8 or 16x8 partition to " \
306
307
    "independently select a reference frame, as opposed to only one ref " \
    "per macroblock." )
308

309
#define CHROMA_ME_TEXT N_("Chroma in motion estimation")
zorglub's avatar
zorglub committed
310
#define CHROMA_ME_LONGTEXT N_( "Chroma ME for subpel and mode decision in " \
311
    "P-frames.")
312

313
314
315
316
#define BIME_TEXT N_("Jointly optimize both MVs in B-frames")
#define BIME_LONGTEXT N_( "Joint bidirectional motion refinement.")

#define TRANSFORM_8X8DCT_TEXT N_("Adaptive spatial transform size")
zorglub's avatar
zorglub committed
317
#define TRANSFORM_8X8DCT_LONGTEXT N_( \
318
    "SATD-based decision for 8x8 transform in inter-MBs.")
319

zorglub's avatar
zorglub committed
320
#define TRELLIS_TEXT N_("Trellis RD quantization" )
321
#define TRELLIS_LONGTEXT N_( "Trellis RD quantization: \n" \
322
323
324
325
    " - 0: disabled\n" \
    " - 1: enabled only on the final encode of a MB\n" \
    " - 2: enabled on all mode decisions\n" \
    "This requires CABAC." )
326

327
328
#define FAST_PSKIP_TEXT N_("Early SKIP detection on P-frames")
#define FAST_PSKIP_LONGTEXT N_( "Early SKIP detection on P-frames.")
329

330
331
332
333
#define DCT_DECIMATE_TEXT N_("Coefficient thresholding on P-frames")
#define DCT_DECIMATE_LONGTEXT N_( "Coefficient thresholding on P-frames." \
    "Eliminate dct blocks containing only a small single coefficient.")

334
/* Noise reduction 1 is too weak to measure, suggest at least 10 */
335
#define NR_TEXT N_("Noise reduction")
336
337
#define NR_LONGTEXT N_( "Dct-domain noise reduction. Adaptive pseudo-deadzone. " \
    "10 to 1000 seems to be a useful range." )
338

339
340
341
342
343
344
345
346
347
348
#if X264_BUILD >= 52 /* r573 */
#define DEADZONE_INTER_TEXT N_("Inter luma quantization deadzone")
#define DEADZONE_INTER_LONGTEXT N_( "Set the size of the inter luma quantization deadzone. " \
    "Range 0 to 32." )

#define DEADZONE_INTRA_TEXT N_("Intra luma quantization deadzone")
#define DEADZONE_INTRA_LONGTEXT N_( "Set the size of the intra luma quantization deadzone. " \
    "Range 0 to 32." )
#endif

349
/* Input/Output */
350

351
352
353
354
355
356
#if X264_BUILD >= 55 /* r607 */
#define NON_DETERMINISTIC_TEXT N_("Non-deterministic optimizations when threaded")
#define NON_DETERMINISTIC_LONGTEXT N_( "Slightly improve quality of SMP, " \
    "at the cost of repeatability.")
#endif

357
#define ASM_TEXT N_("CPU optimizations")
358
#define ASM_LONGTEXT N_( "Use assembler CPU optimizations.")
359

360
361
#define STATS_TEXT N_("Filename for 2 pass stats file")
#define STATS_LONGTEXT N_( "Filename for 2 pass stats file for multi-pass encoding.")
362

363
364
365
366
367
368
369
#define PSNR_TEXT N_("PSNR computation")
#define PSNR_LONGTEXT N_( "Compute and print PSNR stats. This has no effect on " \
    "the actual encoding quality." )

#define SSIM_TEXT N_("SSIM computation")
#define SSIM_LONGTEXT N_( "Compute and print SSIM stats. This has no effect on " \
    "the actual encoding quality." )
370

371
372
373
#define QUIET_TEXT N_("Quiet mode")
#define QUIET_LONGTEXT N_( "Quiet mode.")

zorglub's avatar
zorglub committed
374
#define VERBOSE_TEXT N_("Statistics")
375
376
#define VERBOSE_LONGTEXT N_( "Print stats for each frame.")

377
378
379
380
381
382
383
384
385
#if X264_BUILD >= 47 /* r518 */
#define SPS_ID_TEXT N_("SPS and PPS id numbers")
#define SPS_ID_LONGTEXT N_( "Set SPS and PPS id numbers to allow concatenating " \
    "streams with different settings.")
#endif

#define AUD_TEXT N_("Access unit delimiters")
#define AUD_LONGTEXT N_( "Generate access unit delimiter NAL units.")

386
#if X264_BUILD >= 24 && X264_BUILD < 58
387
static const char *const enc_me_list[] =
388
  { "dia", "hex", "umh", "esa" };
389
static const char *const enc_me_list_text[] =
390
  { N_("dia"), N_("hex"), N_("umh"), N_("esa") };
391
392
#endif

393
#if X264_BUILD >= 58 /* r728 */
394
static const char *const enc_me_list[] =
395
  { "dia", "hex", "umh", "esa", "tesa" };
396
static const char *const enc_me_list_text[] =
397
398
399
  { N_("dia"), N_("hex"), N_("umh"), N_("esa"), N_("tesa") };
#endif

400
static const char *const enc_analyse_list[] =
401
  { "none", "fast", "normal", "slow", "all" };
402
static const char *const enc_analyse_list_text[] =
403
  { N_("none"), N_("fast"), N_("normal"), N_("slow"), N_("all") };
404

405
#if X264_BUILD >= 45 /* r457 */
406
static const char *const direct_pred_list[] =
407
  { "none", "spatial", "temporal", "auto" };
408
static const char *const direct_pred_list_text[] =
409
  { N_("none"), N_("spatial"), N_("temporal"), N_("auto") };
410
#else
411
static const char *const direct_pred_list[] =
412
  { "none", "spatial", "temporal" };
413
static const char *const direct_pred_list_text[] =
414
  { N_("none"), N_("spatial"), N_("temporal") };
415
#endif
Laurent Aimar's avatar
Laurent Aimar committed
416

417
vlc_module_begin();
418
    set_description( N_("H.264/MPEG4 AVC encoder (using x264 library)"));
419
    set_capability( "encoder", 200 );
420
    set_callbacks( Open, Close );
zorglub's avatar
zorglub committed
421
422
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_VCODEC );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
423

424
425
426
/* Frame-type options */

    add_integer( SOUT_CFG_PREFIX "keyint", 250, NULL, KEYINT_TEXT,
427
                 KEYINT_LONGTEXT, false );
428
429

    add_integer( SOUT_CFG_PREFIX "min-keyint", 25, NULL, MIN_KEYINT_TEXT,
430
                 MIN_KEYINT_LONGTEXT, false );
431
        add_deprecated_alias( SOUT_CFG_PREFIX "keyint-min" ); /* Deprecated since 0.8.5 */
432
433

    add_integer( SOUT_CFG_PREFIX "scenecut", 40, NULL, SCENE_TEXT,
434
                 SCENE_LONGTEXT, false );
435
436
        change_integer_range( -1, 100 );

437
438
#if X264_BUILD >= 55 /* r607 */
    add_bool( SOUT_CFG_PREFIX "pre-scenecut", 0, NULL, PRESCENE_TEXT,
439
              PRESCENE_LONGTEXT, false );
440
441
#endif

442
    add_integer( SOUT_CFG_PREFIX "bframes", 0, NULL, BFRAMES_TEXT,
443
                 BFRAMES_LONGTEXT, false );
444
445
        change_integer_range( 0, 16 );

446
447
448
#if X264_BUILD >= 63
    add_integer( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
                 B_ADAPT_LONGTEXT, false );
449
450
        change_integer_range( 0, 2 );
#elif  X264_BUILD >= 0x0013 /* r137 */
451
    add_bool( SOUT_CFG_PREFIX "b-adapt", 1, NULL, B_ADAPT_TEXT,
452
              B_ADAPT_LONGTEXT, false );
453
#endif
454
455

#if  X264_BUILD >= 0x0013 /* r137 */
456
    add_integer( SOUT_CFG_PREFIX "b-bias", 0, NULL, B_BIAS_TEXT,
457
                 B_BIAS_LONGTEXT, false );
458
459
460
461
        change_integer_range( -100, 100 );
#endif

    add_bool( SOUT_CFG_PREFIX "bpyramid", 0, NULL, BPYRAMID_TEXT,
462
              BPYRAMID_LONGTEXT, false );
463
464

    add_bool( SOUT_CFG_PREFIX "cabac", 1, NULL, CABAC_TEXT, CABAC_LONGTEXT,
465
              false );
466
467

    add_integer( SOUT_CFG_PREFIX "ref", 1, NULL, REF_TEXT,
468
                 REF_LONGTEXT, false );
469
        change_integer_range( 1, 16 );
470
        add_deprecated_alias( SOUT_CFG_PREFIX "frameref" ); /* Deprecated since 0.8.5 */
471
472

    add_bool( SOUT_CFG_PREFIX "nf", 0, NULL, NF_TEXT,
473
              NF_LONGTEXT, false );
474
        add_deprecated_alias( SOUT_CFG_PREFIX "loopfilter" ); /* Deprecated since 0.8.5 */
475

476
    add_string( SOUT_CFG_PREFIX "deblock", "0:0", NULL, FILTER_TEXT,
477
                 FILTER_LONGTEXT, false );
478
        add_deprecated_alias( SOUT_CFG_PREFIX "filter" ); /* Deprecated since 0.8.6 */
479
480

    add_string( SOUT_CFG_PREFIX "level", "5.1", NULL, LEVEL_TEXT,
481
               LEVEL_LONGTEXT, false );
482

483
484
#if X264_BUILD >= 51 /* r570 */
    add_bool( SOUT_CFG_PREFIX "interlaced", 0, NULL, INTERLACED_TEXT, INTERLACED_LONGTEXT,
485
              false );
486
487
#endif

488
489
490
/* Ratecontrol */

    add_integer( SOUT_CFG_PREFIX "qp", 26, NULL, QP_TEXT, QP_LONGTEXT,
491
                 false );
492
493
494
495
        change_integer_range( 0, 51 ); /* QP 0 -> lossless encoding */

#if X264_BUILD >= 37 /* r334 */
    add_integer( SOUT_CFG_PREFIX "crf", 0, NULL, CRF_TEXT,
496
                 CRF_LONGTEXT, false );
497
        change_integer_range( 0, 51 );
498
499
500
#endif

    add_integer( SOUT_CFG_PREFIX "qpmin", 10, NULL, QPMIN_TEXT,
501
                 QPMIN_LONGTEXT, false );
502
        change_integer_range( 0, 51 );
503
        add_deprecated_alias( SOUT_CFG_PREFIX "qp-min" ); /* Deprecated since 0.8.5 */
504
505

    add_integer( SOUT_CFG_PREFIX "qpmax", 51, NULL, QPMAX_TEXT,
506
                 QPMAX_LONGTEXT, false );
507
        change_integer_range( 0, 51 );
508
        add_deprecated_alias( SOUT_CFG_PREFIX "qp-max" ); /* Deprecated since 0.8.5 */
Laurent Aimar's avatar
Laurent Aimar committed
509

510
    add_integer( SOUT_CFG_PREFIX "qpstep", 4, NULL, QPSTEP_TEXT,
511
                 QPSTEP_LONGTEXT, false );
512
        change_integer_range( 0, 51 );
513

514
    add_float( SOUT_CFG_PREFIX "ratetol", 1.0, NULL, RATETOL_TEXT,
515
               RATETOL_LONGTEXT, false );
516
        change_float_range( 0, 100 );
517
        add_deprecated_alias( SOUT_CFG_PREFIX "tolerance" ); /* Deprecated since 0.8.5 */
518
519

    add_integer( SOUT_CFG_PREFIX "vbv-maxrate", 0, NULL, VBV_MAXRATE_TEXT,
520
                 VBV_MAXRATE_LONGTEXT, false );
521
522

    add_integer( SOUT_CFG_PREFIX "vbv-bufsize", 0, NULL, VBV_BUFSIZE_TEXT,
523
                 VBV_BUFSIZE_LONGTEXT, false );
524
525

    add_float( SOUT_CFG_PREFIX "vbv-init", 0.9, NULL, VBV_INIT_TEXT,
526
               VBV_INIT_LONGTEXT, false );
527
528
        change_float_range( 0, 1 );

529
    add_float( SOUT_CFG_PREFIX "ipratio", 1.40, NULL, IPRATIO_TEXT,
530
               IPRATIO_LONGTEXT, false );
531
        change_float_range( 1, 2 );
532

533
    add_float( SOUT_CFG_PREFIX "pbratio", 1.30, NULL, PBRATIO_TEXT,
534
               PBRATIO_LONGTEXT, false );
535
        change_float_range( 1, 2 );
536

537
538
#if X264_BUILD >= 23 /* r190 */
    add_integer( SOUT_CFG_PREFIX "chroma-qp-offset", 0, NULL, CHROMA_QP_OFFSET_TEXT,
539
                 CHROMA_QP_OFFSET_LONGTEXT, false );
540
#endif
541

542
    add_integer( SOUT_CFG_PREFIX "pass", 0, NULL, PASS_TEXT,
543
                 PASS_LONGTEXT, false );
544
545
        change_integer_range( 0, 3 );

546
    add_float( SOUT_CFG_PREFIX "qcomp", 0.60, NULL, QCOMP_TEXT,
547
               QCOMP_LONGTEXT, false );
548
        change_float_range( 0, 1 );
549

550
    add_float( SOUT_CFG_PREFIX "cplxblur", 20.0, NULL, CPLXBLUR_TEXT,
551
               CPLXBLUR_LONGTEXT, false );
552

553
    add_float( SOUT_CFG_PREFIX "qblur", 0.5, NULL, QBLUR_TEXT,
554
               QBLUR_LONGTEXT, false );
555
556
#if X264_BUILD >= 59
    add_integer( SOUT_CFG_PREFIX "aq-mode", 2, NULL, AQ_MODE_TEXT,
557
                 AQ_MODE_LONGTEXT, false );
558
559
         change_integer_range( 0, 2 );
    add_float( SOUT_CFG_PREFIX "aq-strength", 1.0, NULL, AQ_STRENGTH_TEXT,
560
               AQ_STRENGTH_LONGTEXT, false );
561
#endif
562

563
564
/* Analysis */

565
566
    /* x264 partitions = none (default). set at least "normal" mode. */
    add_string( SOUT_CFG_PREFIX "partitions", "normal", NULL, ANALYSE_TEXT,
567
                ANALYSE_LONGTEXT, false );
568
        change_string_list( enc_analyse_list, enc_analyse_list_text, 0 );
569
        add_deprecated_alias( SOUT_CFG_PREFIX "analyse" ); /* Deprecated since 0.8.6 */
570

571
    add_string( SOUT_CFG_PREFIX "direct", "spatial", NULL, DIRECT_PRED_TEXT,
572
                DIRECT_PRED_LONGTEXT, false );
573
574
        change_string_list( direct_pred_list, direct_pred_list_text, 0 );

575
#if X264_BUILD >= 52 /* r573 */
576
    add_integer( SOUT_CFG_PREFIX "direct-8x8", -1, NULL, DIRECT_PRED_SIZE_TEXT,
577
                 DIRECT_PRED_SIZE_LONGTEXT, false );
578
579
580
        change_integer_range( -1, 1 );
#endif

581
582
#if X264_BUILD >= 0x0012 /* r134 */
    add_bool( SOUT_CFG_PREFIX "weightb", 0, NULL, WEIGHTB_TEXT,
583
              WEIGHTB_LONGTEXT, false );
584
#endif
585

586
#if X264_BUILD >= 24 /* r221 */
587
    add_string( SOUT_CFG_PREFIX "me", "hex", NULL, ME_TEXT,
588
                ME_LONGTEXT, false );
589
590
        change_string_list( enc_me_list, enc_me_list_text, 0 );

591
    add_integer( SOUT_CFG_PREFIX "merange", 16, NULL, MERANGE_TEXT,
592
                 MERANGE_LONGTEXT, false );
593
594
595
        change_integer_range( 1, 64 );
#endif

596
    add_integer( SOUT_CFG_PREFIX "mvrange", -1, NULL, MVRANGE_TEXT,
597
                 MVRANGE_LONGTEXT, false );
598

599
600
#if X264_BUILD >= 55 /* r607 */
    add_integer( SOUT_CFG_PREFIX "mvrange-thread", -1, NULL, MVRANGE_THREAD_TEXT,
601
                 MVRANGE_THREAD_LONGTEXT, false );
602
603
#endif

604
    add_integer( SOUT_CFG_PREFIX "subme", 5, NULL, SUBME_TEXT,
605
                 SUBME_LONGTEXT, false );
trax's avatar
trax committed
606
        change_integer_range( 1, SUBME_MAX );
607
        add_deprecated_alias( SOUT_CFG_PREFIX "subpel" ); /* Deprecated since 0.8.5 */
608

aballier's avatar
aballier committed
609
#if X264_BUILD >= 41 && X264_BUILD < 65 /* r368 */
610
    add_bool( SOUT_CFG_PREFIX "b-rdo", 0, NULL, B_RDO_TEXT,
611
              B_RDO_LONGTEXT, false );
612
613
#endif

614
615
#if X264_BUILD >= 36 /* r318 */
    add_bool( SOUT_CFG_PREFIX "mixed-refs", 0, NULL, MIXED_REFS_TEXT,
616
              MIXED_REFS_LONGTEXT, false );
617
618
#endif

619
620
#if X264_BUILD >= 23 /* r171 */
    add_bool( SOUT_CFG_PREFIX "chroma-me", 1, NULL, CHROMA_ME_TEXT,
621
              CHROMA_ME_LONGTEXT, false );
622
623
#endif

aballier's avatar
aballier committed
624
#if X264_BUILD >= 43 && X264_BUILD < 65 /* r390 */
625
    add_bool( SOUT_CFG_PREFIX "bime", 0, NULL, BIME_TEXT,
626
              BIME_LONGTEXT, false );
627
628
629
630
#endif

#if X264_BUILD >= 30 /* r251 */
    add_bool( SOUT_CFG_PREFIX "8x8dct", 0, NULL, TRANSFORM_8X8DCT_TEXT,
631
              TRANSFORM_8X8DCT_LONGTEXT, false );
632
633
634
635
#endif

#if X264_BUILD >= 39 /* r360 */
    add_integer( SOUT_CFG_PREFIX "trellis", 0, NULL, TRELLIS_TEXT,
636
                 TRELLIS_LONGTEXT, false );
637
638
639
        change_integer_range( 0, 2 );
#endif

640
641
#if X264_BUILD >= 42 /* r384 */
    add_bool( SOUT_CFG_PREFIX "fast-pskip", 1, NULL, FAST_PSKIP_TEXT,
642
              FAST_PSKIP_LONGTEXT, false );
643
644
#endif

645
646
#if X264_BUILD >= 46 /* r503 */
    add_bool( SOUT_CFG_PREFIX "dct-decimate", 1, NULL, DCT_DECIMATE_TEXT,
647
              DCT_DECIMATE_LONGTEXT, false );
648
649
#endif

650
651
#if X264_BUILD >= 44 /* r398 */
    add_integer( SOUT_CFG_PREFIX "nr", 0, NULL, NR_TEXT,
652
                 NR_LONGTEXT, false );
653
        change_integer_range( 0, 1000 );
654
655
#endif

656
657
#if X264_BUILD >= 52 /* r573 */
    add_integer( SOUT_CFG_PREFIX "deadzone-inter", 21, NULL, DEADZONE_INTER_TEXT,
658
                 DEADZONE_INTRA_LONGTEXT, false );
659
660
661
        change_integer_range( 0, 32 );

    add_integer( SOUT_CFG_PREFIX "deadzone-intra", 11, NULL, DEADZONE_INTRA_TEXT,
662
                 DEADZONE_INTRA_LONGTEXT, false );
663
664
665
        change_integer_range( 0, 32 );
#endif

666
667
/* Input/Output */

668
669
#if X264_BUILD >= 55 /* r607 */
    add_bool( SOUT_CFG_PREFIX "non-deterministic", 0, NULL, NON_DETERMINISTIC_TEXT,
670
              NON_DETERMINISTIC_LONGTEXT, false );
671
672
#endif

673
    add_bool( SOUT_CFG_PREFIX "asm", 1, NULL, ASM_TEXT,
674
              ASM_LONGTEXT, false );
675

676
    /* x264 psnr = 1 (default). disable PSNR computation for speed. */
677
    add_bool( SOUT_CFG_PREFIX "psnr", 0, NULL, PSNR_TEXT,
678
              PSNR_LONGTEXT, false );
679

680
681
682
#if X264_BUILD >= 50 /* r554 */
    /* x264 ssim = 1 (default). disable SSIM computation for speed. */
    add_bool( SOUT_CFG_PREFIX "ssim", 0, NULL, SSIM_TEXT,
683
              SSIM_LONGTEXT, false );
684
685
#endif

686
    add_bool( SOUT_CFG_PREFIX "quiet", 0, NULL, QUIET_TEXT,
687
              QUIET_LONGTEXT, false );
688

689
690
#if X264_BUILD >= 47 /* r518 */
    add_integer( SOUT_CFG_PREFIX "sps-id", 0, NULL, SPS_ID_TEXT,
691
                 SPS_ID_LONGTEXT, false );
692
693
694
#endif

    add_bool( SOUT_CFG_PREFIX "aud", 0, NULL, AUD_TEXT,
695
              AUD_LONGTEXT, false );
696

697
698
#if X264_BUILD >= 0x000e /* r81 */
    add_bool( SOUT_CFG_PREFIX "verbose", 0, NULL, VERBOSE_TEXT,
699
              VERBOSE_LONGTEXT, false );
700
#endif
701

702
    add_string( SOUT_CFG_PREFIX "stats", "x264_2pass.log", NULL, STATS_TEXT,
703
                STATS_LONGTEXT, false );
704

705
vlc_module_end();
706
707
708
709

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
710
static const char *const ppsz_sout_options[] = {
711
712
713
714
715
    "8x8dct", "analyse", "asm", "aud", "bframes", "bime", "bpyramid",
    "b-adapt", "b-bias", "b-rdo", "cabac", "chroma-me", "chroma-qp-offset",
    "cplxblur", "crf", "dct-decimate", "deadzone-inter", "deadzone-intra",
    "deblock", "direct", "direct-8x8", "filter", "fast-pskip", "frameref",
    "interlaced", "ipratio", "keyint", "keyint-min", "level", "loopfilter",
716
717
718
719
720
    "me", "merange", "min-keyint", "mixed-refs", "mvrange", "mvrange-thread",
    "nf", "non-deterministic", "nr", "partitions", "pass", "pbratio",
    "pre-scenecut", "psnr", "qblur", "qp", "qcomp", "qpstep", "qpmax",
    "qpmin", "qp-max", "qp-min", "quiet", "ratetol", "ref", "scenecut",
    "sps-id", "ssim", "stats", "subme", "subpel", "tolerance", "trellis",
721
722
    "verbose", "vbv-bufsize", "vbv-init", "vbv-maxrate", "weightb", "aq-mode",
    "aq-strength",NULL
Laurent Aimar's avatar
Laurent Aimar committed
723
724
};

725
726
727
728
729
730
731
732
733
static block_t *Encode( encoder_t *, picture_t * );

struct encoder_sys_t
{
    x264_t          *h;
    x264_param_t    param;

    int             i_buffer;
    uint8_t         *p_buffer;
734

735
    mtime_t         i_interpolated_dts;
Laurent Aimar's avatar
Laurent Aimar committed
736
737

    char *psz_stat_name;
738
739
740
741
742
743
744
745
746
};

/*****************************************************************************
 * Open: probe the encoder
 *****************************************************************************/
static int  Open ( vlc_object_t *p_this )
{
    encoder_t     *p_enc = (encoder_t *)p_this;
    encoder_sys_t *p_sys;
Laurent Aimar's avatar
Laurent Aimar committed
747
    vlc_value_t    val;
748
    int i_qmin = 0, i_qmax = 0;
hartman's avatar
hartman committed
749
750
    x264_nal_t    *nal;
    int i, i_nal;
751

752
753
    if( p_enc->fmt_out.i_codec != VLC_FOURCC( 'h', '2', '6', '4' ) &&
        !p_enc->b_force )
754
755
756
    {
        return VLC_EGENERIC;
    }
757

758
759
    /* X264_POINTVER or X264_VERSION are not available */
    msg_Dbg ( p_enc, "version x264 0.%d.X", X264_BUILD );
760

761
#if X264_BUILD < 37
762
    if( p_enc->fmt_in.video.i_width % 16 != 0 ||
763
        p_enc->fmt_in.video.i_height % 16 != 0 )
764
    {
765
766
767
768
769
770
771
772
773
774
775
776
777
        msg_Warn( p_enc, "size is not a multiple of 16 (%ix%i)",
                  p_enc->fmt_in.video.i_width, p_enc->fmt_in.video.i_height );

        if( p_enc->fmt_in.video.i_width < 16 ||
            p_enc->fmt_in.video.i_height < 16 )
        {
            msg_Err( p_enc, "video is too small to be cropped" );
            return VLC_EGENERIC;
        }

        msg_Warn( p_enc, "cropping video to %ix%i",
                  p_enc->fmt_in.video.i_width >> 4 << 4,
                  p_enc->fmt_in.video.i_height >> 4 << 4 );
778
    }
779
#endif
780

781
    config_ChainParse( p_enc, SOUT_CFG_PREFIX, ppsz_sout_options, p_enc->p_cfg );
Laurent Aimar's avatar
Laurent Aimar committed
782
783

    p_enc->fmt_out.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
784
    p_enc->fmt_in.i_codec = VLC_FOURCC('I','4','2','0');
Laurent Aimar's avatar
Laurent Aimar committed
785

786
787
788
    p_enc->pf_encode_video = Encode;
    p_enc->pf_encode_audio = NULL;
    p_enc->p_sys = p_sys = malloc( sizeof( encoder_sys_t ) );
789
790
    if( !p_sys )
        return VLC_ENOMEM;
791
    p_sys->i_interpolated_dts = 0;
Laurent Aimar's avatar
Laurent Aimar committed
792
    p_sys->psz_stat_name = NULL;
793
794

    x264_param_default( &p_sys->param );
795
796
797
798
799
800
    p_sys->param.i_width  = p_enc->fmt_in.video.i_width;
    p_sys->param.i_height = p_enc->fmt_in.video.i_height;
#if X264_BUILD < 37
    p_sys->param.i_width  = p_sys->param.i_width >> 4 << 4;
    p_sys->param.i_height = p_sys->param.i_height >> 4 << 4;
#endif
801

802
803
804
805
806
807
    /* average bitrate specified by transcode vb */
    p_sys->param.rc.i_bitrate = p_enc->fmt_out.i_bitrate / 1000;

    var_Get( p_enc, SOUT_CFG_PREFIX "qpstep", &val );
    if( val.i_int >= 0 && val.i_int <= 51 ) p_sys->param.rc.i_qp_step = val.i_int;
    var_Get( p_enc, SOUT_CFG_PREFIX "qpmin", &val );
808
809
810
811
812
    if( val.i_int >= 0 && val.i_int <= 51 )
    {
        i_qmin = val.i_int;
        p_sys->param.rc.i_qp_min = i_qmin;
    }
813
    var_Get( p_enc, SOUT_CFG_PREFIX "qpmax", &val );
814
815
816
817
818
    if( val.i_int >= 0 && val.i_int <= 51 )
    {
        i_qmax = val.i_int;
        p_sys->param.rc.i_qp_max = i_qmax;
    }
819

Laurent Aimar's avatar
Laurent Aimar committed
820
    var_Get( p_enc, SOUT_CFG_PREFIX "qp", &val );
821
    if( val.i_int >= 0 && val.i_int <= 51 )
Laurent Aimar's avatar
Laurent Aimar committed
822
    {
823
824
        if( i_qmin > val.i_int ) i_qmin = val.i_int;
        if( i_qmax < val.i_int ) i_qmax = val.i_int;
825

826
        p_sys->param.rc.i_rc_method = X264_RC_CQP;
827
828
#if X264_BUILD >= 0x000a
        p_sys->param.rc.i_qp_constant = val.i_int;
829
830
        p_sys->param.rc.i_qp_min = i_qmin;
        p_sys->param.rc.i_qp_max = i_qmax;
831
#else
Laurent Aimar's avatar
Laurent Aimar committed
832
        p_sys->param.i_qp_constant = val.i_int;
833
#endif
Laurent Aimar's avatar
Laurent Aimar committed
834
    }
835

836
837
838
839
840
841
842
843
#if X264_BUILD < 48
    /* cbr = 1 overrides qp or crf and sets an average bitrate
       but maxrate = average bitrate is needed for "real" CBR */
    if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.b_cbr = 1;
#else
    if( p_sys->param.rc.i_bitrate > 0 ) p_sys->param.rc.i_rc_method = X264_RC_ABR;
#endif

844
#if X264_BUILD >= 24
845
846
    var_Get( p_enc, SOUT_CFG_PREFIX "ratetol", &val );
    p_sys->param.rc.f_rate_tolerance = val.f_float;
847

848
849
    var_Get( p_enc, SOUT_CFG_PREFIX "vbv-init", &val );
    p_sys->param.rc.f_vbv_buffer_init = val.f_float;
850

851
852
853
854
855
856
857
858
    var_Get( p_enc, SOUT_CFG_PREFIX "vbv-bufsize", &val );
    p_sys->param.rc.i_vbv_buffer_size = val.i_int;

    /* x264 vbv-bufsize = 0 (default). if not provided set period
       in seconds for local maximum bitrate (cache/bufsize) based
       on average bitrate. */
    if( !val.i_int )
        p_sys->param.rc.i_vbv_buffer_size = p_sys->param.rc.i_bitrate * 2;
859

860
861
862
    /* max bitrate = average bitrate -> CBR */
    var_Get( p_enc, SOUT_CFG_PREFIX "vbv-maxrate", &val );
    p_sys->param.rc.i_vbv_max_bitrate = val.i_int;
863