x264-dsp
x264.h
1 /*****************************************************************************
2  * x264.h: x264 public header
3  *****************************************************************************/
4 
5 #ifndef X264_X264_H
6 #define X264_X264_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif /* __cplusplus */
11 
12 #include <stdarg.h>
13 #include <stdint.h>
14 
15 #define X264_BUILD 128
16 
17 /* x264_t:
18  * opaque handler for encoder */
19 typedef struct x264_t x264_t;
20 
21 /****************************************************************************
22  * NAL structure and functions
23  ****************************************************************************/
24 
25 enum nal_unit_type_e {
26  NAL_UNKNOWN = 0,
27  NAL_SLICE = 1, /* I/P/B slice */
28  NAL_SLICE_DPA = 2, /* data partition a, only for Extended profile */
29  NAL_SLICE_DPB = 3, /* data partition b, only for Extended profile */
30  NAL_SLICE_DPC = 4, /* data partition c, only for Extended profile */
31  NAL_SLICE_IDR = 5, /* ref_idc != 0 */
32  NAL_SEI = 6, /* ref_idc == 0 */
33  NAL_SPS = 7, /* sequence parameter set */
34  NAL_PPS = 8, /* picture parameter set */
35  NAL_AUD = 9, /* access unit delimiter, only for special scenario */
36  NAL_FILLER = 12 /* filler, only for constant bitrate */
37  /* ref_idc == 0 for 6,9,10,11,12 */
38 };
39 enum nal_priority_e {
40  NAL_PRIORITY_DISPOSABLE = 0,
41  NAL_PRIORITY_LOW = 1,
42  NAL_PRIORITY_HIGH = 2,
43  NAL_PRIORITY_HIGHEST = 3
44 };
45 
46 /* The data within the payload is already NAL-encapsulated; the ref_idc and type
47  * are merely in the struct for easy access by the calling application.
48  * All data returned in an x264_nal_t, including the data in p_payload, is no longer
49  * valid after the next call to x264_encoder_encode. Thus it must be used or copied
50  * before calling x264_encoder_encode or x264_encoder_headers again. */
51 typedef struct
52 {
53  int i_ref_idc; /* nal_priority_e */
54  int i_type; /* nal_unit_type_e */
55  int b_long_startcode; /* long start-code flag. used only in annexb mode. */
56  int i_first_mb; /* If this NAL is a slice, the index of the first MB in the slice. */
57  int i_last_mb; /* If this NAL is a slice, the index of the last MB in the slice. */
58 
59  /* Size of payload in bytes. */
60  int i_payload;
61  /* If param->b_annexb is set, Annex-B bytestream with startcode.
62  * Otherwise, startcode is replaced with a 4-byte size.
63  * This size is the size used in mp4/similar muxing; it is equal to i_payload-4 */
64  uint8_t *p_payload;
65 } x264_nal_t;
66 
67 /****************************************************************************
68  * Encoder parameters
69  ****************************************************************************/
70 /* CPU flags
71  */
72 #define X264_CPU_CACHELINE_32 0x0000001 /* avoid memory loads that span the border between two cachelines */
73 #define X264_CPU_CACHELINE_64 0x0000002 /* 32/64 is the size of a cacheline in bytes */
74 #define X264_CPU_ALTIVEC 0x0000004
75 #define X264_CPU_MMX 0x0000008
76 #define X264_CPU_MMX2 0x0000010 /* MMX2 aka MMXEXT aka ISSE */
77 #define X264_CPU_MMXEXT X264_CPU_MMX2
78 #define X264_CPU_SSE 0x0000020
79 #define X264_CPU_SSE2 0x0000040
80 #define X264_CPU_SSE2_IS_SLOW 0x0000080 /* avoid most SSE2 functions on Athlon64 */
81 #define X264_CPU_SSE2_IS_FAST 0x0000100 /* a few functions are only faster on Core2 and Phenom */
82 #define X264_CPU_SSE3 0x0000200
83 #define X264_CPU_SSSE3 0x0000400
84 #define X264_CPU_SHUFFLE_IS_FAST 0x0000800 /* Penryn, Nehalem, and Phenom have fast shuffle units */
85 #define X264_CPU_STACK_MOD4 0x0001000 /* if stack is only mod4 and not mod16 */
86 #define X264_CPU_SSE4 0x0002000 /* SSE4.1 */
87 #define X264_CPU_SSE42 0x0004000 /* SSE4.2 */
88 #define X264_CPU_SSE_MISALIGN 0x0008000 /* Phenom support for misaligned SSE instruction arguments */
89 #define X264_CPU_LZCNT 0x0010000 /* Phenom support for "leading zero count" instruction. */
90 #define X264_CPU_ARMV6 0x0020000
91 #define X264_CPU_NEON 0x0040000 /* ARM NEON */
92 #define X264_CPU_FAST_NEON_MRC 0x0080000 /* Transfer from NEON to ARM register is fast (Cortex-A9) */
93 #define X264_CPU_SLOW_CTZ 0x0100000 /* BSR/BSF x86 instructions are really slow on some CPUs */
94 #define X264_CPU_SLOW_ATOM 0x0200000 /* The Atom just sucks */
95 #define X264_CPU_AVX 0x0400000 /* AVX support: requires OS support even if YMM registers \
96  * aren't used. */
97 #define X264_CPU_XOP 0x0800000 /* AMD XOP */
98 #define X264_CPU_FMA4 0x1000000 /* AMD FMA4 */
99 #define X264_CPU_AVX2 0x2000000 /* AVX2 */
100 #define X264_CPU_FMA3 0x4000000 /* Intel FMA3 */
101 #define X264_CPU_BMI1 0x8000000 /* BMI1 */
102 #define X264_CPU_BMI2 0x10000000 /* BMI2 */
103 #define X264_CPU_TBM 0x20000000 /* AMD TBM */
104 
105 /* Analyse flags
106  */
107 #define X264_ANALYSE_I4x4 0x0001 /* Analyse i4x4 */
108 #define X264_ANALYSE_I8x8 0x0002 /* Analyse i8x8 (requires 8x8 transform) */
109 #define X264_ANALYSE_PSUB16x16 0x0010 /* Analyse p16x8, p8x16 and p8x8 */
110 #define X264_ANALYSE_PSUB8x8 0x0020 /* Analyse p8x4, p4x8, p4x4 */
111 #define X264_ANALYSE_BSUB16x16 0x0100 /* Analyse b16x8, b8x16 and b8x8 */
112 #define X264_DIRECT_PRED_NONE 0 /* B-frame direct predict mode: none (turn off) */
113 #define X264_DIRECT_PRED_SPATIAL 1 /* B-frame direct predict mode: spatial */
114 #define X264_DIRECT_PRED_TEMPORAL 2 /* B-frame direct predict mode: temporal */
115 #define X264_DIRECT_PRED_AUTO 3 /* B-frame direct predict mode: auto (spatial or temporal) */
116 #define X264_ME_DIA 0 /* Motion estimation mode: DIA */
117 #define X264_ME_HEX 1 /* Motion estimation mode: HEX. default */
118 #define X264_ME_UMH 2 /* Motion estimation mode: UMH */
119 #define X264_ME_ESA 3 /* Motion estimation mode: ESA */
120 #define X264_ME_TESA 4 /* Motion estimation mode: TESA */
121 #define X264_CQM_FLAT 0 /* CQM mode: Flat. default */
122 #define X264_CQM_JVT 1 /* CQM mode: JVT definition */
123 #define X264_CQM_CUSTOM 2 /* CQM mode: customized by caller */
124 #define X264_RC_CQP 0 /* Rate control mode: Constant QP */
125 #define X264_RC_CRF 1 /* Rate control mode: Constant Rate Factor (or Constant Quality). default */
126 #define X264_RC_ABR 2 /* Rate control mode: Average Bit Rate */
127 #define X264_QP_AUTO 0
128 #define X264_AQ_NONE 0 /* Adaptive quantization mode: none */
129 #define X264_AQ_VARIANCE 1 /* Adaptive quantization mode: variance. default */
130 #define X264_AQ_AUTOVARIANCE 2 /* Adaptive quantization mode: auto-variance */
131 #define X264_B_ADAPT_NONE 0 /* B-frame adaptive mode: none */
132 #define X264_B_ADAPT_FAST 1 /* B-frame adaptive mode: fast. default */
133 #define X264_B_ADAPT_TRELLIS 2 /* B-frame adaptive mode: trellis */
134 #define X264_WEIGHTP_NONE 0 /* Weighted P-frame predict mode: none */
135 #define X264_WEIGHTP_SIMPLE 1 /* Weighted P-frame predict mode: simple. */
136 #define X264_WEIGHTP_SMART 2 /* Weighted P-frame predict mode: smart. default */
137 #define X264_B_PYRAMID_NONE 0 /* B-frame pyramid mode: none */
138 #define X264_B_PYRAMID_STRICT 1 /* B-frame pyramid mode: strict */
139 #define X264_B_PYRAMID_NORMAL 2 /* B-frame pyramid mode: normal. default */
140 #define X264_KEYINT_MIN_AUTO 0
141 #define X264_KEYINT_MAX_INFINITE (1 << 30)
142 
143 /* Colorspace type */
144 #define X264_CSP_MASK 0x00ff /* */
145 #define X264_CSP_NONE 0x0000 /* Invalid mode */
146 #define X264_CSP_I420 0x0001 /* yuv 4:2:0 planar. default */
147 #define X264_CSP_YV12 0x0002 /* yvu 4:2:0 planar */
148 #define X264_CSP_NV12 0x0003 /* yuv 4:2:0, with one y plane and one packed u+v */
149 #define X264_CSP_I422 0x0004 /* yuv 4:2:2 planar. supported only in High422 profile */
150 #define X264_CSP_YV16 0x0005 /* yvu 4:2:2 planar */
151 #define X264_CSP_NV16 0x0006 /* yuv 4:2:2, with one y plane and one packed u+v */
152 #define X264_CSP_I444 0x0007 /* yuv 4:4:4 planar. supported only in High444 profile */
153 #define X264_CSP_YV24 0x0008 /* yvu 4:4:4 planar */
154 #define X264_CSP_BGR 0x0009 /* packed bgr 24bits */
155 #define X264_CSP_BGRA 0x000a /* packed bgr 32bits */
156 #define X264_CSP_RGB 0x000b /* packed rgb 24bits */
157 #define X264_CSP_MAX 0x000c /* end of list */
158 #define X264_CSP_VFLIP 0x1000 /* the csp is vertically flipped */
159 #define X264_CSP_HIGH_DEPTH 0x2000 /* the csp has a depth of 16 bits per pixel component. supported only in High10 profile */
160 
161 /* Slice type */
162 #define X264_TYPE_AUTO 0x0000 /* Let x264 choose the right type */
163 #define X264_TYPE_IDR 0x0001
164 #define X264_TYPE_I 0x0002
165 #define X264_TYPE_P 0x0003
166 #define X264_TYPE_BREF 0x0004 /* Non-disposable B-frame */
167 #define X264_TYPE_B 0x0005
168 #define X264_TYPE_KEYFRAME 0x0006 /* IDR or I depending on b_open_gop option */
169 #define IS_X264_TYPE_I(x) ((x) == X264_TYPE_I || (x) == X264_TYPE_IDR)
170 #define IS_X264_TYPE_B(x) ((x) == X264_TYPE_B || (x) == X264_TYPE_BREF)
171 
172 /* Log level */
173 #define X264_LOG_NONE (-1)
174 #define X264_LOG_ERROR 0
175 #define X264_LOG_WARNING 1
176 #define X264_LOG_INFO 2
177 #define X264_LOG_DEBUG 3
178 
179 /* Threading */
180 #define X264_THREADS_AUTO 0 /* Automatically select optimal number of threads */
181 #define X264_SYNC_LOOKAHEAD_AUTO (-1) /* Automatically select optimal lookahead thread buffer size */
182 
183 /* HRD */
184 #define X264_NAL_HRD_NONE 0
185 #define X264_NAL_HRD_VBR 1
186 #define X264_NAL_HRD_CBR 2
187 
188 typedef struct x264_param_t {
189  /* CPU flags */
190  unsigned int cpu; /* cpu feature set. default: 0 */
191  int i_threads; /* encode multiple frames in parallel. Set 1 if no thread support */
192  int i_lookahead_threads; /* multiple threads for lookahead analysis. Set 1 if no thread support */
193  int b_sliced_threads; /* Whether to use slice-based threading. default: off */
194  int b_deterministic; /* whether to allow non-deterministic optimizations when threaded. Set 0 if no thread support */
195  int b_cpu_independent; /* force canonical behavior rather than cpu-dependent optimal algorithms. default: off */
196  int i_sync_lookahead; /* threaded lookahead buffer. Set 0 if no thread support */
197 
198  /* Video Properties */
199  int i_width; /* width in pixels */
200  int i_height; /* height in pixels */
201  int i_csp; /* CSP of encoded bitstream */
202  int i_level_idc; /* H.264 level indication */
203  int i_frame_total; /* number of frames to encode if known, else 0 */
204 
205  /* NAL HRD
206  * Uses Buffering and Picture Timing SEIs to signal HRD
207  * The HRD in H.264 was not designed with VFR in mind.
208  * It is therefore not recommended to use NAL HRD with VFR.
209  * Furthermore, reconfiguring the VBV (via x264_encoder_reconfig)
210  * will currently generate invalid HRD.
211  *
212  * Default: 0
213  * Signal HRD information. Required for Blue-ray streams, television broadcast and a few other specialist areas.
214  * Acceptable values are:
215  * 0: none. Specify no HRD information
216  * 1: vbr. Specify HRD information
217  * 2: cbr. Specify HRD information and pack the bitstream to the bitrate specified by -bitrate. Requires bitrate mode ratecontrol.
218  * Recommendation: none, unless you need to signal this information.
219  */
220  int i_nal_hrd;
221 
222  struct
223  {
224  /* they will be reduced to be 0 < x <= 65535 and prime */
225  int i_sar_height;
226  int i_sar_width;
227 
228  int i_overscan; /* 0=undef, 1=no overscan, 2=overscan */
229 
230  /* see h264 annex E for the values of the following */
231  int i_vidformat;
232  int b_fullrange;
233  int i_colorprim;
234  int i_transfer;
235  int i_colmatrix;
236  int i_chroma_loc; /* both top & bottom */
237  } vui;
238 
239  /* Bitstream parameters */
240  int i_frame_reference; /* Maximum number of reference frames */
241  int i_dpb_size; /* Force a DPB size larger than that implied by B-frames and reference frames.
242  * Useful in combination with interactive error resilience. */
243  int i_keyint_max; /* Force an IDR keyframe at this interval. default: 50 */
244  int i_keyint_min; /* Scenecuts closer together than this are coded as I, not IDR. default: 5 */
245  int i_scenecut_threshold; /* how aggressively to insert extra I frames */
246  int b_intra_refresh; /* Whether or not to use periodic intra refresh instead of IDR frames. default: off */
247 
248  int i_bframe; /* how many b-frame between 2 references pictures */
249  /*************************************************************************************************
250  * Set the adaptive B-frame placement decision algorithm. This setting controls *
251  * how x264 decides between placing a P- or B-frame. *
252  * range: [0, 2], default: 1 *
253  * *
254  * 0. Disabled. Pick B-frames always. This is the same as what the older no-b-adapt setting did. *
255  * 1. 'Fast' algorithm, faster, speed slightly increases with higher --b-frames setting. *
256  * When using this mode, you basically always want to use --bframes 16. *
257  * 2. 'Optimal' algorithm, slower, speed significantly decreases with higher --b-frames setting. *
258  *************************************************************************************************/
259  int i_bframe_adaptive;
260  /*******************************************************************************
261  * Controls the likelihood of B-frames being used instead of P-frames. *
262  * range: [-100, 100], default: 0 *
263  * Values greater than 0 increase the weighting towards B-frames, *
264  * while values less than 0 do the opposite. This number is an arbitrary *
265  * metric. The range is from -100 to 100. A value of 100/-100 does not *
266  * guarantee every/no P-frame will be converted (use --b-adapt 0 for that). *
267  * Only use this if you think you make better ratecontrol decisions than x264. *
268  *******************************************************************************/
269  int i_bframe_bias;
270  /***********************************************************************************
271  * Allow the use of B-frames as references for other frames. Without this setting, *
272  * frames can only reference I- or P-frames. Although I/P-frames are more valued *
273  * as references because of their higher quality, B-frames can also be useful. *
274  * B-frames designated as references will get a quantizer halfway between P-frames *
275  * and normal B-frames. Need to use at least two B-frames before B-pyramid work. *
276  * *
277  * range: [0, 2], default: 2 (normal) *
278  * *
279  * - 0 (none): do not allow B-frames to be used as references. *
280  * - 1 (strict): allow one B-frame per minigop to be used as reference; *
281  * - 2 (normal): allow numerous B-frames per minigop to be used as references. *
282  ***********************************************************************************/
283  int i_bframe_pyramid;
284  int b_open_gop; /* Whether enables Open-GOP. default: off */
285  /*************************************************************
286  * Whether enables compatible with Blu-Ray players. *
287  * This feature enables a bunch of 'Blu-Ray hacks': *
288  * - min-CR + level 4.1 hack *
289  * - Special b-pyramid SEI *
290  * - B-frames cannot reference frames outside their minigop *
291  * - Open-GOP keyframe interval hack *
292  *************************************************************/
293  int b_bluray_compat;
294 
295  int b_deblocking_filter; /* deblocking filter. default: on */
296  int i_deblocking_filter_alphac0; /* [-6, 6] -6 light filter, 6 strong. default: 0 */
297  int i_deblocking_filter_beta; /* [-6, 6] idem. default: 0 */
298 
299  int b_cabac; /* CABAC entropy coding flag. default: on */
300  /* The index for determining the initialization table used in
301  * the initialization process for context variables.
302  * range: [0-2], default: 0
303  */
304  int i_cabac_init_idc;
305 
306  int b_interlaced; /* interlaced mode. default: off */
307  int b_constrained_intra; /* Enable constrained intra prediction, which is required for the base layer of SVC encodes. default: off */
308 
309  int i_cqm_preset; /* X264_CQM_*, value: FLAT/JVT/CUSTOM, default: FLAT */
310  /*char *psz_cqm_file;*/ /* JM format */
311  /* used only if i_cqm_preset == X264_CQM_CUSTOM */
312  /*uint8_t cqm_4iy[16];*/ /* Intra_4x4_LUMA */
313  /*uint8_t cqm_4py[16];*/ /* Inter_4x4_LUMA */
314  /*uint8_t cqm_4ic[16];*/ /* Intra_4x4_CHROMA */
315  /*uint8_t cqm_4pc[16];*/ /* Inter_4x4_CHROMA */
316  /*uint8_t cqm_8iy[64];*/ /* Intra_8x8_LUMA */
317  /*uint8_t cqm_8py[64];*/ /* Inter_8x8_LUMA */
318  /*uint8_t cqm_8ic[64];*/ /* Intra_8x8_CHROMA */
319  /*uint8_t cqm_8pc[64];*/ /* Inter_8x8_CHROMA */
320 
321  /* Log */
322  void (*pf_log)(void *, int i_level, const char *psz, va_list);
323  void *p_log_private;
324  int i_log_level;
325  /*int b_full_recon;*/ /* fully reconstruct frames, even when not necessary for encoding. default: off */
326  /*char *psz_dump_yuv;*/ /* filename for reconstructed frames. default: NULL */
327 
328  /* Encoder analyser parameters */
329  struct
330  {
331  unsigned int intra; /* intra partitions. default: I4x4|I8x8 */
332  unsigned int inter; /* inter partitions. default: I4x4|I8x8|PSUB16x16|BSUB16*16 */
333 
334  int b_transform_8x8; /* 8x8 transform mode flag. default: on */
335  /*************************************************************************
336  * weighting for P-frames *
337  * Enables use of explicit weighted prediction to improve compression *
338  * in P-frames. Also improves quality in fades. Higher modes are slower. *
339  * *
340  * NOTE: When encoding for Adobe Flash set this to 1 - its decoder *
341  * generates artifacts otherwise. Flash 10.1 fixes this bug. *
342  * *
343  * 0. Disabled. *
344  * 1. Simple: fade analysis, but no reference duplication. *
345  * 2. Smart: fade analysis and reference duplication. default *
346  *************************************************************************/
347  int i_weighted_pred;
348  int b_weighted_bipred; /* implicit weighting for B-frames */
349  int i_direct_mv_pred; /* spatial vs temporal mv prediction */
350  /*********************************************************************
351  * Add an offset to the quantizer of chroma planes when encoding. *
352  * The offset can be negative. default: 0. *
353  * *
354  * When using psy options are enabled (psy-rd, psy-trellis), *
355  * x264 automatically subtracts 2 from this value to compensate *
356  * for these optimisations overly favouring luma detail by default. *
357  * *
358  * Note: x264 only encodes the luma and chroma planes at the same *
359  * quantizer up to quantizer 29. After this, chroma is progressively *
360  * quantized by a lower amount than luma until you end with luma at *
361  * q51 and chroma at q39. This is required by the H.264 standard. *
362  *********************************************************************/
363  int i_chroma_qp_offset;
364 
365  /***********************************************************************************
366  * motion estimation algorithm to use (X264_ME_*). default: HEX *
367  * *
368  * dia (diamond) is the simplest search, consisting of starting at the best *
369  * predictor, checking the motion vectors at one pixel upwards, left, down, *
370  * and to the right, picking the best, and repeating the process until it *
371  * no longer finds any better motion vector. *
372  * *
373  * hex (hexagon) consists of a similar strategy, except it uses a range-2 *
374  * search of 6 surrounding points, thus the name. It is considerably more *
375  * efficient than dia and hardly any slower, and therefore makes a good *
376  * choice for general-use encoding. *
377  * *
378  * umh (uneven multi-hex) is considerably slower than hex, but searches a *
379  * complex multi-hexagon pattern in order to avoid missing harder-to-find *
380  * motion vectors. Unlike hex and dia, the merange parameter directly controls *
381  * umh's search radius, allowing one to increase or decrease the size of the *
382  * wide search. *
383  * *
384  * esa (exhaustive) is a highly optimized intelligent search of the entire motion *
385  * search space within merange of the best predictor. It is mathematically *
386  * equivalent to the bruteforce method of searching every single motion vector in *
387  * that area, though faster. However, it is still considerably slower than UMH, *
388  * with not too much benefit, so is not particularly useful for everyday encoding. *
389  ***********************************************************************************/
390  int i_me_method;
391  /*********************************************************************************
392  * integer pixel motion estimation search range (from predicted mv). default: 16 *
393  * merange controls the max range of the motion search in pixels. *
394  * For hex and dia, the range is clamped to 4-16, with a default of 16. *
395  * For umh and esa, it can be increased beyond the default 16 to allow for a *
396  * wider-range motion search, which is useful on HD footage and for high-motion *
397  * footage. Note that for umh, esa, and tesa, increasing merange will *
398  * significantly slow down encoding. *
399  *********************************************************************************/
400  int i_me_range; /* */
401  /************************************************************************
402  * maximum length of mv (pixels). default: -1(auto), range:[32-512]. *
403  * if mv_range is not set by user, use value defined by H.264 level. *
404  * level > 30, mv_range: 512 *
405  * level > 20, mv_range: 256 *
406  * level > 10, mv_range: 128 *
407  * level <= 10, mv_range: 64 *
408  ************************************************************************/
409  int i_mv_range;
410  int i_mv_range_thread; /* minimum space between threads. -1 = auto, based on number of threads. */
411  /**************************************************************************************
412  * sub-pixel motion estimation quality, range: [0-11], default: 7 *
413  * Set the subpixel estimation complexity. Higher numbers are better. Levels 1-5 *
414  * simply control the subpixel refinement strength. Level 6 enables RDO for mode *
415  * decision, and level 8 enables RDO for motion vectors and intra prediction modes. *
416  * Level 10 enables QPRD (use rate distortion optimization for qp selection). *
417  * *
418  * Using a value less than 2 will enable a faster, and lower quality lookahead mode, *
419  * as well as cause poorer --scenecut decisions to be made, and thus not recommended. *
420  * *
421  * Possible Values: *
422  * 0. fullpel only *
423  * 1. QPel SAD 1 iteration *
424  * 2. QPel SATD 2 iterations *
425  * 3. HPel on MB then QPel *
426  * 4. Always QPel *
427  * 5. Multi QPel + bi-directional motion estimation *
428  * 6. RD on I/P frames *
429  * 7. RD on all frames. [default] *
430  * 8. RD refinement on I/P frames *
431  * 9. RD refinement on all frames *
432  * 10. QP-RD (requires --trellis=2, --aq-mode > 0) *
433  * 11. Full RD *
434  **************************************************************************************/
435  int i_subpel_refine;
436  int b_chroma_me; /* chroma ME for subpel and mode decision in P-frames. default: on */
437  int b_mixed_references; /* allow each mb partition to have its own reference frame. default: on (need i_frame_reference > 1) */
438  /****************************************************************************
439  * Trellis RD quantization. range: [0-2], default: 1. *
440  * - 0: disabled. *
441  * - 1: enabled only on the final encode of a MB. [default] *
442  * - 2: enabled on all mode decisions of a MB. *
443  * *
444  * Trellis quantization is an algorithm that can improve data compression *
445  * in DCT-based encoding methods. It is used to optimize residual DCT *
446  * coefficients after motion estimation in lossy video compression. *
447  * *
448  * Trellis quantization reduces the size of some DCT coefficients while *
449  * recovering others to take their place. This process can increase quality *
450  * because coefficients chosen by Trellis have the lowest rate-distortion *
451  * ratio. Trellis quantization effectively finds the optimal quantization *
452  * for each block to maximize the PSNR relative to bitrate. *
453  * *
454  * Note that Trellis is still considered 'experimental', and almost *
455  * certainly is a Bad Thing for at least cartoons. *
456  ****************************************************************************/
457  int i_trellis;
458  int b_fast_pskip; /* early SKIP detection on P-frames. default: on */
459  /*****************************************************
460  * DCT Decimation. default: on *
461  * transform coefficient thresholding on P-frames. *
462  * *
463  * This will drop DCT blocks it deems "unnecessary", *
464  * and improve coding efficiency, with a usually *
465  * negligible loss in quality. *
466  *****************************************************/
467  int b_dct_decimate;
468  /********************************************************
469  * adaptive pseudo-deadzone. default: 0 *
470  * Performs fast noise reduction. Estimates film noise *
471  * based on this value and attempts to remove it by *
472  * dropping small details before quantization. This may *
473  * not match the quality of a good external noise *
474  * reduction filter, but it performs very fast. *
475  ********************************************************/
476  int i_noise_reduction;
477 
478  /********************************************************************************
479  * Psy RD: Psychovisually optimized rate-distortion optimization *
480  * How it works (simply): the human eye doesn't just want the image to look *
481  * similar to the original, it wants the image to have similar complexity. *
482  * Therefore, we would rather see a somewhat distorted but still detailed block *
483  * than a non-distorted but completely blurred block. The result is a bias *
484  * towards a detailed and/or grainy output image, a bit like xvid except that *
485  * its actual detail rather than ugly blocking. *
486  ********************************************************************************/
487  float f_psy_rd; /* Psy RD strength. (requires subme>=6 to activate) */
488  float f_psy_trellis; /* Psy trellis strength. (requires trellis>=1 to activate) */
489  int b_psy; /* Toggle all psy optimizations. default: on */
490 
491  int b_mb_info; /* Use input mb_info data in x264_picture_t. see x264_image_properties_t.mb_info. default: no */
492  int b_mb_info_update; /* Update the values in mb_info according to the results of encoding. default: no */
493  /******************************************************************
494  * Set the deadzone size that will be used in luma quantization. *
495  * The deadzone value sets the level of fine detail that x264 *
496  * will arbitrarily drop without attempting to preserve. Very *
497  * fine detail is both hard to see and expensive to encode, *
498  * dropping this detail without attempting to preserve it stops *
499  * wasting bits on such a low-return section of the video. *
500  * Deadzone is incompatible with Trellis. *
501  * default: [0]: 21 (inter), [1]: 11 (intra), range: [0 - 32] *
502  ******************************************************************/
503  int i_luma_deadzone[2];
504 
505  int b_psnr; /* compute and print PSNR stats. default: off */
506  int b_ssim; /* compute and print SSIM stats. default: off */
507  } analyse;
508 
509  /* Rate control parameters */
510  struct
511  {
512  int i_rc_method; /* X264_RC_*, value: CQP/CRF/ABR, default: CRF */
513 
514  int i_qp_constant; /* 0 to (51 + 6*(x264_bit_depth-8)). 0=lossless. default: 23 */
515  int i_qp_min; /* min allowed QP value */
516  int i_qp_max; /* max allowed QP value */
517  int i_qp_step; /* max QP step between frames. default: 4 */
518 
519  int i_bitrate; /* In ABR mode, average bitrate (kbps) */
520  float f_rf_constant; /* 1pass VBR, nominal QP. default: 23 */
521  float f_rf_constant_max; /* In CRF mode, maximum CRF as caused by VBV */
522  /*********************************************************************************************
523  * rate tolerance. default: 1.0 *
524  * *
525  * This is a dual purpose parameter: *
526  * In 1-pass bitrate encodes, this settings controls the percentage that x264 can miss the *
527  * target average bitrate by. You can set this to 'inf' to disable this overflow detection *
528  * completely. The lowest you can set this is to 0.01. The higher you set this to the better *
529  * x264 can react to complex scenes near the end of the movie. The unit of measure for this *
530  * purpose is percent (eg, 1.0 = 1% bitrate deviation allowed). *
531  * Many movies (any action movie, for instance) are most complex at the climatic finale. As *
532  * a 1pass encode doesn't know this, the number of bits required for the end is usually *
533  * underestimated. A ratetol of inf can mitigate this by allowing the encode to function *
534  * more like a --crf encode, but the filesize will blow out. *
535  * *
536  * When VBV is activated (ie, you're specified --vbv-* options), this setting also affects *
537  * VBV aggressiveness. Setting this higher allows VBV to fluctuate more at the risk of *
538  * possibly violating the VBV settings. For this purpose, the unit of measure is arbitrary. *
539  *********************************************************************************************/
540  float f_rate_tolerance;
541  /******************************************************************************************
542  * The VBV (Video Buffer Verifier) system in x264 is used to constrain the output bitrate *
543  * so it is suitable for streaming in a bandwidth constrained environment. The h.264 VBV *
544  * model is based around the idea of a "VBV buffer" on the decoder side. As the h.264 *
545  * stream is downloaded by the client it's stored in the VBV buffer. A frame must be *
546  * fully downloaded into the VBV buffer before it can be decoded. *
547  * *
548  * x264 has three options that control the VBV buffer: *
549  * 1. The buffer's size is specified in kbit with --vbv-bufsize, *
550  * 2. The rate at which the buffer fills is specified in kbit/sec by --vbv-maxrate, *
551  * 3. The proportion of the buffer that must be filled before playback can start is *
552  * specified by --vbv-init. *
553  * *
554  * When using VBV, you always need to set the first two options and should never need to *
555  * set the last one. *
556  ******************************************************************************************/
557  int i_vbv_max_bitrate; /* set by --vbv-maxrate. default: 0 */
558  int i_vbv_buffer_size; /* set by --vbv-bufsize. default: 0 */
559  float f_vbv_buffer_init; /* <=1: fraction of buffer_size. >1: kbit. set by --vbv-init. default: 0.9 */
560  float f_ip_factor; /* qp ratio of I-frames / P-frames. default: 1.4 */
561  float f_pb_factor; /* qp ratio of P-frames / B-frames. default: 1.3 */
562 
563  /********************************************************************************************
564  * Adaptive Quantization Mode *
565  * *
566  * Without AQ, x264 tends to underallocate bits to less-detailed sections. *
567  * AQ is used to better distribute the available bits between all macroblocks in the video. *
568  * This setting changes what scope AQ re-arranges bits in: *
569  * 0: Do not use AQ at all. *
570  * 1: Allow AQ to redistribute bits across the whole video and within frames. default. *
571  * 2: Auto-variance AQ (experimental) which attempts to adapt strength per-frame. *
572  ********************************************************************************************/
573  int i_aq_mode; /* psy adaptive QP. (X264_AQ_*) */
574  float f_aq_strength; /* strength of AQ bias towards low detail ('flat') macroblocks. range: [0.0 - 2.0], default: 1.0 */
575  /*********************************************************************************
576  * Macroblock-tree ratecontrol. default: on *
577  * Using macroblock tree ratecontrol overall improves the compression by keeping *
578  * track of temporal propagation across frames and weighting accordingly. *
579  *********************************************************************************/
580  int b_mb_tree;
581  int i_lookahead; /* lookahead value. default: 40. Set 0 if no thread support */
582 
583  /* 2pass mode only */
584  int b_stat_write; /* Enable stat writing in psz_stat_out. default: off */
585  char *psz_stat_out; /* stat out file name. default: null */
586  int b_stat_read; /* Read stat from psz_stat_in and use it. default: off */
587  char *psz_stat_in; /* stat in file name. default: null */
588 
589  /* 2pass params (same as ffmpeg ones) */
590  float f_qcompress; /* 0.0 => cbr, 1.0 => constant qp. default: 0.6 */
591  float f_qblur; /* temporally blur quants */
592  float f_complexity_blur; /* temporally blur complexity */
593  } rc;
594 
595  /* Cropping Rectangle parameters: added to those implicitly defined by
596  non-mod16 video resolutions. */
597  struct
598  {
599  unsigned int i_left;
600  unsigned int i_top;
601  unsigned int i_right;
602  unsigned int i_bottom;
603  } crop_rect;
604 
605  /**************************************************************
606  * For stereoscopic (3D) videos define frame arrangement. *
607  * range: [-1 - 5], default: -1 (not set) *
608  * - 0: checkerboard - pixels are alternatively from L and R *
609  * - 1: column alternation - L and R are interlaced by column *
610  * - 2: row alternation - L and R are interlaced by row *
611  * - 3: side by side - L is on the left, R on the right *
612  * - 4: top bottom - L is on top, R on bottom *
613  * - 5: frame alternation - one view per frame *
614  **************************************************************/
615  int i_frame_packing;
616 
617  /* Muxing parameters */
618  int b_aud; /* generate access unit delimiters. default: off */
619  int b_repeat_headers; /* put SPS/PPS before each keyframe. default: on. set 0 if output is mp4/mkv/flv */
620  int b_annexb; /* if set, place start codes (4 bytes) before NAL units,
621  * otherwise place size (4 bytes) before NAL units. default: on. set 0 if output is mp4/mkv/flv */
622  int i_sps_id; /* SPS and PPS id number. range:[0-31], default: 0 */
623  int b_vfr_input; /* VFR input. If 1, use timebase and timestamps for ratecontrol purposes.
624  * If 0, use fps only. */
625  int b_pulldown; /* use explicit set timebase for CFR */
626  uint32_t i_fps_num; /* FPS numerator */
627  uint32_t i_fps_den; /* FPS denominator */
628  uint32_t i_timebase_num; /* Timebase numerator */
629  uint32_t i_timebase_den; /* Timebase denominator */
630 
631  /* Top field first flag, used only in interlaced mode.
632  * 1: top field followed by bottom
633  * 0: bottom field followed by top
634  */
635  int b_tff;
636 
637  /* Pulldown:
638  * The correct pic_struct must be passed with each input frame.
639  * The input timebase should be the timebase corresponding to the output framerate. This should be constant.
640  * e.g. for 3:2 pulldown timebase should be 1001/30000
641  * The PTS passed with each frame must be the PTS of the frame after pulldown is applied.
642  * Frame doubling and tripling require b_vfr_input set to zero (see H.264 Table D-1)
643  *
644  * Pulldown changes are not clearly defined in H.264. Therefore, it is the calling app's responsibility to manage this.
645  */
646 
647  int b_pic_struct;
648 
649  /* Fake Interlaced.
650  *
651  * Used only when b_interlaced=0. Setting this flag makes it possible to flag the stream as PAFF interlaced yet
652  * encode all frames progessively. It is useful for encoding 25p and 30p Blu-Ray streams.
653  */
654 
655  int b_fake_interlaced;
656 
657  /* Slicing parameters */
658  int i_slice_max_size; /* Max size per slice in bytes; includes estimated NAL overhead. default: 0 (unlimited) */
659  int i_slice_max_mbs; /* Max number of MBs per slice; overrides i_slice_count. default: 0 (unlimited) */
660  int i_slice_count; /* Number of slices per frame: forces rectangular slices. default: 0 */
661 
662  /* Optional callback for freeing this x264_param_t when it is done being used.
663  * Only used when the x264_param_t sits in memory for an indefinite period of time,
664  * i.e. when an x264_param_t is passed to x264_t in an x264_picture_t or in zones.
665  * Not used when x264_encoder_reconfig is called directly. */
666  void (*param_free)(void *);
667 
668  /* Optional low-level callback for low-latency encoding. Called for each output NAL unit
669  * immediately after the NAL unit is finished encoding. This allows the calling application
670  * to begin processing video data (e.g. by sending packets over a network) before the frame
671  * is done encoding.
672  *
673  * This callback MUST do the following in order to work correctly:
674  * 1) Have available an output buffer of at least size nal->i_payload*3/2 + 5 + 16.
675  * 2) Call x264_nal_encode( h, dst, nal ), where dst is the output buffer.
676  * After these steps, the content of nal is valid and can be used in the same way as if
677  * the NAL unit were output by x264_encoder_encode.
678  *
679  * This does not need to be synchronous with the encoding process: the data pointed to
680  * by nal (both before and after x264_nal_encode) will remain valid until the next
681  * x264_encoder_encode call. The callback must be re-entrant.
682  *
683  * This callback does not work with frame-based threads; threads must be disabled
684  * or sliced-threads enabled. This callback also does not work as one would expect
685  * with HRD -- since the buffering period SEI cannot be calculated until the frame
686  * is finished encoding, it will not be sent via this callback.
687  *
688  * Note also that the NALs are not necessarily returned in order when sliced threads is
689  * enabled. Accordingly, the variable i_first_mb and i_last_mb are available in
690  * x264_nal_t to help the calling application reorder the slices if necessary.
691  *
692  * When this callback is enabled, x264_encoder_encode does not return valid NALs;
693  * the calling application is expected to acquire all output NALs through the callback.
694  *
695  * It is generally sensible to combine this callback with a use of slice-max-mbs or
696  * slice-max-size.
697  *
698  * The opaque pointer is the opaque pointer from the input frame associated with this
699  * NAL unit. This helps distinguish between nalu_process calls from different sources,
700  * e.g. if doing multiple encodes in one process.
701  */
702  void (*nalu_process)(x264_t *h, x264_nal_t *nal, void *opaque);
703 } x264_param_t;
704 
705 void x264_nal_encode(x264_t *h, uint8_t *dst, x264_nal_t *nal);
706 
707 /****************************************************************************
708  * H.264 level restriction information
709  ****************************************************************************/
710 
711 typedef struct
712 {
713  int level_idc; /* level indication */
714  int mbps; /* max macroblock processing rate (macroblocks/sec) */
715  int frame_size; /* max frame size (macroblocks) */
716  int dpb; /* max decoded picture buffer (bytes) */
717  int bitrate; /* max bitrate (kbit/sec) */
718  int cpb; /* max vbv buffer (kbit) */
719  int mv_range; /* max vertical mv component range (pixels) */
720  int mvs_per_2mb; /* max mvs per 2 consecutive mbs. */
721  int slice_rate; /* ?? */
722  int mincr; /* min compression ratio */
723  int bipred8x8; /* limit bipred to >=8x8 */
724  int direct8x8; /* limit b_direct to >=8x8 */
725  int frame_only; /* forbid interlacing */
726 } x264_level_t;
727 
728 /* all of the levels defined in the standard, terminated by .level_idc=0 */
729 extern const x264_level_t x264_levels[];
730 
731 /****************************************************************************
732  * Basic parameter handling functions
733  ****************************************************************************/
734 
735 /* x264_param_default:
736  * fill x264_param_t with default values and do CPU detection */
737 void x264_param_default(x264_param_t *);
738 
739 /****************************************************************************
740  * Picture structures and functions
741  ****************************************************************************/
742 
743 /* x264_bit_depth:
744  * Specifies the number of bits per pixel that x264 uses. This is also the
745  * bit depth that x264 encodes in. If this value is > 8, x264 will read
746  * two bytes of input data for each pixel sample, and expect the upper
747  * (16-x264_bit_depth) bits to be zero.
748  * Note: The flag X264_CSP_HIGH_DEPTH must be used to specify the
749  * colorspace depth as well. */
750 extern const int x264_bit_depth;
751 
752 /* x264_chroma_format:
753  * Specifies the chroma formats that x264 supports encoding. When this
754  * value is non-zero, then it represents a X264_CSP_* that is the only
755  * chroma format that x264 supports encoding. If the value is 0 then
756  * there are no restrictions. */
757 extern const int x264_chroma_format;
758 
759 enum pic_struct_e {
760  PIC_STRUCT_AUTO = 0, // automatically decide (default)
761  PIC_STRUCT_PROGRESSIVE = 1, // progressive frame
762  // "TOP" and "BOTTOM" are not supported in x264 (PAFF only)
763  PIC_STRUCT_TOP_BOTTOM = 4, // top field followed by bottom
764  PIC_STRUCT_BOTTOM_TOP = 5, // bottom field followed by top
765  PIC_STRUCT_TOP_BOTTOM_TOP = 6, // top field, bottom field, top field repeated
766  PIC_STRUCT_BOTTOM_TOP_BOTTOM = 7, // bottom field, top field, bottom field repeated
767  PIC_STRUCT_DOUBLE = 8, // double frame
768  PIC_STRUCT_TRIPLE = 9 // triple frame
769 };
770 
771 typedef struct
772 {
773  double cpb_initial_arrival_time;
774  double cpb_final_arrival_time;
775  double cpb_removal_time;
776 
777  double dpb_output_time;
778 } x264_hrd_t;
779 
780 typedef struct
781 {
782  int i_csp; /* Colorspace */
783  int i_plane; /* Number of image planes */
784  int i_stride[4]; /* Strides for each plane */
785  uint8_t *plane[4]; /* Pointers to each plane */
786 } x264_image_t;
787 
788 typedef struct
789 {
790  /* All arrays of data here are ordered as follows:
791  * each array contains one offset per macroblock, in raster scan order. In interlaced
792  * mode, top-field MBs and bottom-field MBs are interleaved at the row level.
793  * Macroblocks are 16x16 blocks of pixels (with respect to the luma plane). For the
794  * purposes of calculating the number of macroblocks, width and height are rounded up to
795  * the nearest 16. If in interlaced mode, height is rounded up to the nearest 32 instead. */
796 
797  /* In: an array of quantizer offsets to be applied to this image during encoding.
798  * These are added on top of the decisions made by x264.
799  * Offsets can be fractional; they are added before QPs are rounded to integer.
800  * Adaptive quantization must be enabled to use this feature. Behavior if quant
801  * offsets differ between encoding passes is undefined. */
802  float *quant_offsets;
803  /* In: optional callback to free quant_offsets when used.
804  * Useful if one wants to use a different quant_offset array for each frame. */
805  void (*quant_offsets_free)(void *);
806 
807  /* In: optional array of flags for each macroblock.
808  * Allows specifying additional information for the encoder such as which macroblocks
809  * remain unchanged. Usable flags are listed below.
810  * x264_param_t.analyse.b_mb_info must be set to use this, since x264 needs to track
811  * extra data internally to make full use of this information.
812  *
813  * Out: if b_mb_info_update is set, x264 will update this array as a result of encoding.
814  *
815  * For "MBINFO_CONSTANT", it will remove this flag on any macroblock whose decoded
816  * pixels have changed. This can be useful for e.g. noting which areas of the
817  * frame need to actually be blitted. Note: this intentionally ignores the effects
818  * of deblocking for the current frame, which should be fine unless one needs exact
819  * pixel-perfect accuracy.
820  *
821  * Results for MBINFO_CONSTANT are currently only set for P-frames, and are not
822  * guaranteed to enumerate all blocks which haven't changed. (There may be false
823  * negatives, but no false positives.)
824  */
825  uint8_t *mb_info;
826  /* In: optional callback to free mb_info when used. */
827  void (*mb_info_free)(void *);
828 
829 /* The macroblock is constant and remains unchanged from the previous frame. */
830 #define X264_MBINFO_CONSTANT (1 << 0)
831  /* More flags may be added in the future. */
832 
833  /* Out: SSIM of the the frame luma (if x264_param_t.b_ssim is set) */
834  double f_ssim;
835  /* Out: Average PSNR of the frame (if x264_param_t.b_psnr is set) */
836  double f_psnr_avg;
837  /* Out: PSNR of Y, U, and V (if x264_param_t.b_psnr is set) */
838  double f_psnr[3];
839 
840  /* Out: Average effective CRF of the encoded frame */
841  double f_crf_avg;
843 
844 typedef struct
845 {
846  /* In: force picture type (if not auto)
847  * If x264 encoding parameters are violated in the forcing of picture types,
848  * x264 will correct the input picture type and log a warning.
849  * The quality of frametype decisions may suffer if a great deal of fine-grained
850  * mixing of auto and forced frametypes is done.
851  * Out: type of the picture encoded */
852  int i_type;
853  /* In: force quantizer for != X264_QP_AUTO */
854  int i_qpplus1;
855  /* In: pic_struct, for pulldown/doubling/etc...used only if b_pic_struct=1.
856  * use pic_struct_e for pic_struct inputs
857  * Out: pic_struct element associated with frame */
858  int i_pic_struct;
859  /* Out: whether this frame is a keyframe. Important when using modes that result in
860  * SEI recovery points being used instead of IDR frames. */
861  int b_keyframe;
862  /* In: user pts, Out: pts of encoded picture (user)*/
863  int64_t i_pts;
864  /* Out: frame dts. When the pts of the first frame is close to zero,
865  * initial frames may have a negative dts which must be dealt with by any muxer */
866  int64_t i_dts;
867  /* In: custom encoding parameters to be set from this frame forwards
868  (in coded order, not display order). If NULL, continue using
869  parameters from the previous frame. Some parameters, such as
870  aspect ratio, can only be changed per-GOP due to the limitations
871  of H.264 itself; in this case, the caller must force an IDR frame
872  if it needs the changed parameter to apply immediately. */
873  x264_param_t *param;
874  /* In: raw image data */
875  /* Out: reconstructed image data. x264 may skip part of the reconstruction process,
876  e.g. deblocking, in frames where it isn't necessary. To force complete
877  reconstruction, at a small speed cost, set b_full_recon. */
878  x264_image_t img;
879  /* In: optional information to modify encoder decisions for this frame
880  * Out: information about the encoded frame */
882  /* Out: HRD timing information. Output only when i_nal_hrd is set. */
883  x264_hrd_t hrd_timing;
884  /* private user data. copied from input to output frames. */
885  void *opaque;
887 
888 /* x264_picture_init:
889  * initialize an x264_picture_t. Needs to be done if the calling application
890  * allocates its own x264_picture_t as opposed to using x264_picture_alloc. */
891 void x264_picture_init(x264_picture_t *pic);
892 
893 /* x264_picture_alloc:
894  * alloc data for a picture. You must call x264_picture_clean on it.
895  * returns 0 on success, or -1 on malloc failure or invalid colorspace. */
896 int x264_picture_alloc(x264_picture_t *pic, int i_csp, int i_width, int i_height);
897 
898 /* x264_picture_clean:
899  * free associated resource for a x264_picture_t allocated with
900  * x264_picture_alloc ONLY */
901 void x264_picture_clean(x264_picture_t *pic);
902 
903 /****************************************************************************
904  * Encoder functions
905  ****************************************************************************/
906 
907 /* x264_encoder_open:
908  * create a new encoder handler, all parameters from x264_param_t are copied */
909 x264_t *x264_encoder_open(x264_param_t *);
910 /* x264_encoder_parameters:
911  * copies the current internal set of parameters to the pointer provided
912  * by the caller. useful when the calling application needs to know
913  * how x264_encoder_open has changed the parameters, or the current state
914  * of the encoder after multiple x264_encoder_reconfig calls.
915  * note that the data accessible through pointers in the returned param struct
916  * (e.g. filenames) should not be modified by the calling application. */
917 void x264_encoder_parameters(x264_t *, x264_param_t *);
918 /* x264_encoder_headers:
919  * return the SPS and PPS that will be used for the whole stream.
920  * *pi_nal is the number of NAL units outputted in pp_nal.
921  * returns negative on error.
922  * the payloads of all output NALs are guaranteed to be sequential in memory. */
923 int x264_encoder_headers(x264_t *, x264_nal_t **pp_nal, int *pi_nal);
924 /* x264_encoder_encode:
925  * encode one picture.
926  * *pi_nal is the number of NAL units outputted in pp_nal.
927  * returns negative on error, zero if no NAL units returned.
928  * the payloads of all output NALs are guaranteed to be sequential in memory. */
929 int x264_encoder_encode(x264_t *, x264_nal_t **pp_nal, int *pi_nal, x264_picture_t *pic_in, x264_picture_t *pic_out);
930 /* x264_encoder_close:
931  * close an encoder handler */
932 void x264_encoder_close(x264_t *);
933 
934 #ifdef __cplusplus
935 }
936 #endif /* __cplusplus */
937 
938 #endif
Definition: x264.h:771
Definition: x264.h:788
Definition: x264.h:780
Definition: x264.h:711
Definition: x264.h:52
Definition: x264.h:187
Definition: x264.h:844
Definition: common.h:499