x264-dsp
common.h
1 /*****************************************************************************
2  * common.h: misc common functions
3  *****************************************************************************/
4 
5 #ifndef X264_COMMON_H
6 #define X264_COMMON_H
7 
8 #include "config.h"
9 
10 /****************************************************************************
11  * Macros
12  ****************************************************************************/
13 #define X264_MIN(a, b) ((a) < (b) ? (a) : (b))
14 #define X264_MAX(a, b) ((a) > (b) ? (a) : (b))
15 #define X264_MIN3(a, b, c) X264_MIN((a), X264_MIN((b), (c)))
16 #define X264_MAX3(a, b, c) X264_MAX((a), X264_MAX((b), (c)))
17 #define X264_MIN4(a, b, c, d) X264_MIN((a), X264_MIN3((b), (c), (d)))
18 #define X264_MAX4(a, b, c, d) X264_MAX((a), X264_MAX3((b), (c), (d)))
19 #define XCHG(type, a, b) \
20  do { \
21  type t = a; \
22  a = b; \
23  b = t; \
24  } while (0)
25 #define IS_DISPOSABLE(type) (type == X264_TYPE_B)
26 #define FIX8(f) ((int)(f * (1 << 8) + .5))
27 #define ALIGN(x, a) (((x) + ((a)-1)) & ~((a)-1))
28 
29 #define CHECKED_MALLOC(var, size) \
30  do { \
31  var = x264_malloc(size); \
32  if (!var) \
33  goto fail; \
34  } while (0)
35 #define CHECKED_MALLOCZERO(var, size) \
36  do { \
37  CHECKED_MALLOC(var, size); \
38  memset(var, 0, size); \
39  } while (0)
40 
41 #define X264_BFRAME_MAX 4 /**** default: 16 => 4 ****/
42 #define X264_REF_MAX 4 /**** default: 16 => 4 ****/
43 #define X264_THREAD_MAX 4 /**** default: 128 => 4 ****/
44 #define X264_LOOKAHEAD_THREAD_MAX 2 /**** default: 16 => 2 ****/
45 #define X264_LOOKAHEAD_MAX 5 /**** default: 250 => 5 ****/
46 #define QP_BD_OFFSET (6 * (BIT_DEPTH - 8))
47 #define QP_MAX_SPEC (51 + QP_BD_OFFSET)
48 #define QP_MAX (QP_MAX_SPEC + 18)
49 #define QP_MAX_MAX (51 + 2 * 6 + 18)
50 #define PIXEL_MAX ((1 << BIT_DEPTH) - 1)
51 // arbitrary, but low because SATD scores are 1/4 normal
52 #define X264_LOOKAHEAD_QP (12 + QP_BD_OFFSET)
53 #define SPEC_QP(x) X264_MIN((x), QP_MAX_SPEC)
54 
55 // number of pixels (per thread) in progress at any given time.
56 // 16 for the macroblock in progress + 3 for deblocking + 3 for motion compensation filter + 2 for extra safety
57 #define X264_THREAD_HEIGHT 24
58 
59 /* WEIGHTP_FAKE is set when mb_tree & psy are enabled, but normal weightp is disabled
60  * (such as in baseline). It checks for fades in lookahead and adjusts qp accordingly
61  * to increase quality. Defined as (-1) so that if(i_weighted_pred > 0) is true only when
62  * real weights are being used. */
63 
64 #define X264_WEIGHTP_FAKE (-1)
65 
66 #define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
67 #define FILLER_OVERHEAD (NALU_OVERHEAD + 1)
68 
69 /****************************************************************************
70  * Includes
71  ****************************************************************************/
72 #include "osdep.h"
73 #include <stdarg.h>
74 #include <stddef.h>
75 #include <stdlib.h>
76 #include <string.h>
77 /*#include <assert.h>*/
78 #include <limits.h>
79 
80 #if HAVE_INTERLACED
81 #define MB_INTERLACED h->mb.b_interlaced
82 #define SLICE_MBAFF h->sh.b_mbaff
83 #define PARAM_INTERLACED h->param.b_interlaced
84 #else
85 #define MB_INTERLACED 0
86 #define SLICE_MBAFF 0
87 #define PARAM_INTERLACED 0
88 #endif
89 
90 #ifdef CHROMA_FORMAT
91 #define CHROMA_H_SHIFT (CHROMA_FORMAT == CHROMA_420 || CHROMA_FORMAT == CHROMA_422)
92 #define CHROMA_V_SHIFT (CHROMA_FORMAT == CHROMA_420)
93 #else
94 #define CHROMA_FORMAT h->sps->i_chroma_format_idc
95 #define CHROMA_H_SHIFT h->mb.chroma_h_shift
96 #define CHROMA_V_SHIFT h->mb.chroma_v_shift
97 #endif
98 
99 #define CHROMA_SIZE(s) ((s) >> (CHROMA_H_SHIFT + CHROMA_V_SHIFT))
100 #define FRAME_SIZE(s) ((s) + 2 * CHROMA_SIZE(s))
101 
102 /* Unions for type-punning.
103  * Mn: load or store n bits, aligned, native-endian
104  * CPn: copy n bits, aligned, native-endian
105  * we don't use memcpy for CPn because memcpy's args aren't assumed to be aligned */
106 typedef union {
107  uint16_t i;
108  uint8_t c[2];
109 } MAY_ALIAS x264_union16_t;
110 typedef union {
111  uint32_t i;
112  uint16_t b[2];
113  uint8_t c[4];
114 } MAY_ALIAS x264_union32_t;
115 typedef union {
116  uint64_t i;
117  uint32_t a[2];
118  uint16_t b[4];
119  uint8_t c[8];
120 } MAY_ALIAS x264_union64_t;
121 typedef struct {
122  uint64_t i[2];
124 typedef union {
125  x264_uint128_t i;
126  uint64_t a[2];
127  uint32_t b[4];
128  uint16_t c[8];
129  uint8_t d[16];
130 } MAY_ALIAS x264_union128_t;
131 #define M16(src) (((x264_union16_t *)(src))->i)
132 #define M32(src) (((x264_union32_t *)(src))->i)
133 #define M64(src) (((x264_union64_t *)(src))->i)
134 #define M128(src) (((x264_union128_t *)(src))->i)
135 #define M128_ZERO ((x264_uint128_t){{0, 0}})
136 #define CP16(dst, src) M16(dst) = M16(src)
137 #define CP32(dst, src) M32(dst) = M32(src)
138 #define CP64(dst, src) M64(dst) = M64(src)
139 #define CP128(dst, src) M128(dst) = M128(src)
140 
141 #if HIGH_BIT_DEPTH
142 typedef uint16_t pixel;
143 typedef uint64_t pixel4;
144 typedef int32_t dctcoef;
145 typedef uint32_t udctcoef;
146 
147 #define PIXEL_SPLAT_X4(x) ((x)*0x0001000100010001ULL)
148 #define MPIXEL_X4(src) M64(src)
149 #else
150 typedef uint8_t pixel;
151 typedef uint32_t pixel4;
152 typedef int16_t dctcoef;
153 typedef uint16_t udctcoef;
154 
155 #define PIXEL_SPLAT_X4(x) ((x)*0x01010101U)
156 #define MPIXEL_X4(src) M32(src)
157 #endif
158 
159 #define BIT_DEPTH X264_BIT_DEPTH
160 
161 #define CPPIXEL_X4(dst, src) MPIXEL_X4(dst) = MPIXEL_X4(src)
162 
163 #define X264_SCAN8_LUMA_SIZE (5 * 8)
164 #define X264_SCAN8_SIZE (X264_SCAN8_LUMA_SIZE * 3)
165 #define X264_SCAN8_0 (4 + 1 * 8)
166 
167 /* Scan8 organization:
168  * 0 1 2 3 4 5 6 7
169  * 0 DY y y y y y
170  * 1 y Y Y Y Y
171  * 2 y Y Y Y Y
172  * 3 y Y Y Y Y
173  * 4 y Y Y Y Y
174  * 5 DU u u u u u
175  * 6 u U U U U
176  * 7 u U U U U
177  * 8 u U U U U
178  * 9 u U U U U
179  * 10 DV v v v v v
180  * 11 v V V V V
181  * 12 v V V V V
182  * 13 v V V V V
183  * 14 v V V V V
184  *
185  * Scan8 stores pixels of 4x4 sub-block.
186  * X264_SCAN8_SIZE = 3 * 5 * 8 = 120
187  * y/u/v stores left or top pixels
188  * Y/U/V stores current pixels
189  * DY/DU/DV are for luma/chroma DC.
190  */
191 
192 #define LUMA_DC 48
193 #define CHROMA_DC 49
194 
195 static const uint8_t x264_scan8[16 * 3 + 3] =
196  {
197  /* Y */
198  4 + 1 * 8, 5 + 1 * 8, 4 + 2 * 8, 5 + 2 * 8,
199  6 + 1 * 8, 7 + 1 * 8, 6 + 2 * 8, 7 + 2 * 8,
200  4 + 3 * 8, 5 + 3 * 8, 4 + 4 * 8, 5 + 4 * 8,
201  6 + 3 * 8, 7 + 3 * 8, 6 + 4 * 8, 7 + 4 * 8,
202  /* U */
203  4 + 6 * 8, 5 + 6 * 8, 4 + 7 * 8, 5 + 7 * 8,
204  6 + 6 * 8, 7 + 6 * 8, 6 + 7 * 8, 7 + 7 * 8,
205  4 + 8 * 8, 5 + 8 * 8, 4 + 9 * 8, 5 + 9 * 8,
206  6 + 8 * 8, 7 + 8 * 8, 6 + 9 * 8, 7 + 9 * 8,
207  /* V */
208  4 + 11 * 8, 5 + 11 * 8, 4 + 12 * 8, 5 + 12 * 8,
209  6 + 11 * 8, 7 + 11 * 8, 6 + 12 * 8, 7 + 12 * 8,
210  4 + 13 * 8, 5 + 13 * 8, 4 + 14 * 8, 5 + 14 * 8,
211  6 + 13 * 8, 7 + 13 * 8, 6 + 14 * 8, 7 + 14 * 8,
212  0 + 0 * 8, 0 + 5 * 8, 0 + 10 * 8};
213 
214 #include "x264.h"
215 #include "bitstream.h"
216 #include "set.h"
217 #include "predict.h"
218 #include "pixel.h"
219 #include "mc.h"
220 #include "frame.h"
221 #include "dct.h"
222 #include "cabac.h"
223 #include "quant.h"
224 
225 /****************************************************************************
226  * General functions
227  ****************************************************************************/
228 /* x264_malloc : will do or emulate a memalign
229  * you have to use x264_free for buffers allocated with x264_malloc */
230 void *x264_malloc(int);
231 void x264_free(void *);
232 
233 /* x264_param2string: return a (malloced) string containing most of
234  * the encoding options */
235 char *x264_param2string(x264_param_t *p, int b_res);
236 
237 /* log */
238 void x264_log(x264_t *h, int i_level, const char *psz_fmt, ...);
239 
240 void x264_cavlc_init(x264_t *h);
241 void x264_cabac_init(x264_t *h);
242 
243 static ALWAYS_INLINE pixel x264_clip_pixel(int x) {
244  return ((x & ~PIXEL_MAX) ? (-x) >> 31 & PIXEL_MAX : x);
245 }
246 
247 static ALWAYS_INLINE int x264_clip3(int v, int i_min, int i_max) {
248  return ((v < i_min) ? i_min : (v > i_max) ? i_max
249  : v);
250 }
251 
252 static ALWAYS_INLINE double x264_clip3f(double v, double f_min, double f_max) {
253  return ((v < f_min) ? f_min : (v > f_max) ? f_max
254  : v);
255 }
256 
257 #if defined(__TI_COMPILER_VERSION__) && HAVE_TIC6X
258 static ALWAYS_INLINE void x264_median_mv(int16_t *dst, int16_t *a, int16_t *b, int16_t *c) {
259  int mva = _mem4(a);
260  int mvb = _mem4(b);
261  int mvc = _mem4(c);
262  int mvd;
263 
264  mvd = _min2(mva, mvb);
265  mva = _max2(mva, mvb);
266  mvd = _max2(mvd, mvc);
267  mvd = _min2(mvd, mva);
268  _mem4(dst) = mvd;
269 }
270 #else
271 static ALWAYS_INLINE int x264_median(int a, int b, int c) {
272  int t = (a - b) & ((a - b) >> 31);
273  a -= t;
274  b += t;
275  b -= (b - c) & ((b - c) >> 31);
276  b += (a - b) & ((a - b) >> 31);
277  return b;
278 }
279 
280 static ALWAYS_INLINE void x264_median_mv(int16_t *dst, int16_t *a, int16_t *b, int16_t *c) {
281  dst[0] = x264_median(a[0], b[0], c[0]);
282  dst[1] = x264_median(a[1], b[1], c[1]);
283 }
284 #endif
285 
286 #if defined(__TI_COMPILER_VERSION__) && HAVE_TIC6X
287 static void ALWAYS_INLINE x264_predictor_roundclip(int16_t (*dst)[2], int16_t (*mvc)[2], int i_mvc, int mv_x_min, int mv_x_max, int mv_y_min, int mv_y_max) {
288  int mv_min = _spack2(mv_y_min, mv_x_min);
289  int mv_max = _spack2(mv_y_max, mv_x_max);
290  int k_2 = 0x00020002;
291  int mv, i;
292 
293  for (i = 0; i < i_mvc; i++) {
294  mv = _mem4(mvc++);
295  mv = _add2(mv, k_2);
296  mv = _shr2(mv, 2);
297  mv = _max2(mv, mv_min);
298  mv = _min2(mv, mv_max);
299  _mem4(dst++) = mv;
300  }
301 }
302 #else
303 static void ALWAYS_INLINE x264_predictor_roundclip(int16_t (*dst)[2], int16_t (*mvc)[2], int i_mvc, int mv_x_min, int mv_x_max, int mv_y_min, int mv_y_max) {
304  int i;
305  for (i = 0; i < i_mvc; i++) {
306  int mx = (mvc[i][0] + 2) >> 2;
307  int my = (mvc[i][1] + 2) >> 2;
308  dst[i][0] = x264_clip3(mx, mv_x_min, mv_x_max);
309  dst[i][1] = x264_clip3(my, mv_y_min, mv_y_max);
310  }
311 }
312 #endif
313 
314 extern const float x264_log2_lut[128];
315 extern const float x264_log2_lz_lut[32];
316 
317 static ALWAYS_INLINE float x264_log2(uint32_t x) {
318  int lz = x264_clz(x);
319  return x264_log2_lut[(x << lz >> 24) & 0x7f] + x264_log2_lz_lut[lz];
320 }
321 
322 enum slice_type_e {
323  SLICE_TYPE_P = 0,
324  SLICE_TYPE_B = 1,
325  SLICE_TYPE_I = 2,
326 };
327 
328 static const char slice_type_to_char[] = {'P', 'B', 'I'};
329 
330 /* Supplemental Enhancement Information (SEI) */
331 enum sei_payload_type_e {
332  SEI_BUFFERING_PERIOD = 0,
333  SEI_PIC_TIMING = 1,
334  SEI_PAN_SCAN_RECT = 2,
335  SEI_FILLER = 3,
336  SEI_USER_DATA_REGISTERED = 4,
337  SEI_USER_DATA_UNREGISTERED = 5,
338  SEI_RECOVERY_POINT = 6,
339  SEI_DEC_REF_PIC_MARKING = 7,
340  SEI_FRAME_PACKING = 45,
341 };
342 
343 typedef struct
344 {
345  /* sequence parameter set syntax */
346  x264_sps_t *sps;
347  /* picture parameter set syntax */
348  x264_pps_t *pps;
349 
350  /* slice type, 0: P, 1: B, 2: I */
351  int i_type;
352  /* first macro-block in slice. set 0 if only one slice per picture */
353  int i_first_mb;
354  /* last macro-block in slice. */
355  int i_last_mb;
356 
357  /* PPS id, range: [0-255] */
358  int i_pps_id;
359 
360  /* frame number indicating decode order */
361  int i_frame_num;
362 
363  /* macro-block adaptive field frame. 1: enable, 0: disable. default = h->param.b_interlaced */
364  int b_mbaff;
365  /* field coding flag. 1: field coding, 0: non-field coding */
366  int b_field_pic;
367  /* bottom field flag. valid only in field coding. */
368  int b_bottom_field;
369 
370  /* IDR picture flag */
371  int i_idr_pic_id; /* -1 if nal_type != 5 */
372 
373  /* picture-order-count */
374  int i_poc;
375  int i_delta_poc_bottom;
376 
377  int i_delta_poc[2];
378  int i_redundant_pic_cnt;
379 
380  /* direct predict mode for B-slice. 1: spatial, 0: temporal */
381  int b_direct_spatial_mv_pred;
382 
383  /* override flag of number of reference frames */
384  int b_num_ref_idx_override;
385  /* active reference frames of list 0 */
386  int i_num_ref_idx_l0_active;
387  /* active reference frames of list 1 */
388  int i_num_ref_idx_l1_active;
389 
390  /* list reordering flag. array[0] for list 0, array[1] for list 1. */
391  int b_ref_pic_list_reordering[2];
392  struct
393  {
394  int idc;
395  int arg;
396  } ref_pic_list_order[2][X264_REF_MAX];
397 
398  /* P-frame weighting */
399  int b_weighted_pred;
400  x264_weight_t weight[X264_REF_MAX * 2][3];
401 
402  /* memory management control operation */
403  int i_mmco_remove_from_end;
404  int i_mmco_command_count;
405  struct /* struct for future expansion */
406  {
407  int i_difference_of_pic_nums;
408  int i_poc;
409  } mmco[X264_REF_MAX];
410 
411  /* the index for determining the initialization table used in */
412  /* the initialization process for context variables. range: [0-2] */
413  int i_cabac_init_idc;
414 
415  int i_qp;
416  /************************************************************************
417  * specifies the initial value of QPY to be used for all the *
418  * macro-blocks in the slice until modified by the value of mb_qp_delta *
419  * in the macro-block layer. The initial QPY quantization parameter for *
420  * the slice is computed as: *
421  * QPY = 26 + pic_init_qp_minus26 + slice_qp_delta *
422  ************************************************************************/
423  int i_qp_delta;
424  /* SP flag, 1: SP-frame, 0: SI-frame or other */
425  int b_sp_for_swidth;
426  /************************************************************************
427  * specifies the value of QSY for all the macro-blocks in SP/SI slices. *
428  * The QSY quantization parameter for the slice is computed as: *
429  * QSY = 26 + pic_init_qs_minus26 + slice_qs_delta *
430  ************************************************************************/
431  int i_qs_delta;
432 
433  /* deblocking filter */
434  /*************************************************************************
435  * specifies whether the operation of the deblocking filter shall be *
436  * disabled across some block edges of the slice and specifies for which *
437  * edges the filtering is disabled. range: [0-2], default: 0. *
438  * *
439  * 0: all luma and chroma block edges of the slice are filtered. *
440  * 1: deblocking is disabled for all block edges of the slice. *
441  * 2: all luma and chroma block edges of the slice are filtered with *
442  * exception of the block edges that coincide with slice boundaries. *
443  *************************************************************************/
444  int i_disable_deblocking_filter_idc;
445  int i_alpha_c0_offset; /* alpha c0 offset for deblocking filter. default: 0 */
446  int i_beta_offset; /* beta offset for deblocking filter. default: 0 */
448 
449 typedef struct x264_lookahead_t {
450  volatile uint8_t b_exit_thread; /* notifying lookahead thread to exit */
451  uint8_t b_thread_active; /* lookahead thread active flag */
452  uint8_t b_analyse_keyframe; /* whether perform propagation analysis on I-frames. default: off */
453  int i_last_keyframe; /* last key frame number */
454  int i_slicetype_length; /* number of delay frames. 0 if b-frames disabled */
455  x264_frame_t *last_nonb; /* last Non-B frame pointer */
456  x264_pthread_t thread_handle; /* lookahead thread handle */
457  x264_sync_frame_list_t next; /* frames waiting for slice type decision */
458  x264_sync_frame_list_t ofbuf; /* frames which slice type decision is done */
460 
462 
463 typedef struct x264_left_table_t {
464  uint8_t intra[4];
465  uint8_t nnz[4];
466  uint8_t nnz_chroma[4];
467  uint8_t mv[4];
468  uint8_t ref[4];
470 
471 /* Current frame stats */
472 typedef struct
473 {
474  /* MV bits (MV+Ref+Block Type) */
475  int i_mv_bits;
476  /* Texture bits (DCT coefs) */
477  int i_tex_bits;
478  /* ? */
479  int i_misc_bits;
480  /* MB type counts */
481  int i_mb_count[19]; /* Accumulated mb count for 19 specific types of current frame */
482  int i_mb_count_i;
483  int i_mb_count_p;
484  int i_mb_count_skip;
485  int i_mb_count_8x8dct[2];
486  int i_mb_count_ref[2][X264_REF_MAX * 2];
487  int i_mb_partition[17]; /* Accumulated mb count for 17 partition types of current frame */
488  int i_mb_cbp[6];
489  int i_mb_pred_mode[4][13];
490  int i_mb_field[3];
491  /* Adaptive direct mv pred */
492  int i_direct_score[2];
493  /* Metrics */
494  int64_t i_ssd[3]; /* SSD of Luma(y) and Chroma(u,v) for PSNR */
495  double f_ssim; /* SSIM of Luma */
496  int i_ssim_cnt; /* SSIM count */
498 
499 struct x264_t {
500  /* encoder parameters */
501  x264_param_t param;
502 
503  x264_t *thread[X264_THREAD_MAX + 1];
504  x264_t *lookahead_thread[X264_LOOKAHEAD_THREAD_MAX];
505  int b_thread_active; /* thread active flag */
506  int i_thread_phase; /* which thread to use for the next frame */
507  int i_thread_idx; /* which thread this is */
508  int i_threadslice_start; /* first row in this thread slice. set 0 if only one slice per frame. */
509  int i_threadslice_end; /* row after the end of this thread slice. set h->mb.i_mb_height if only one slice per frame. */
510  int i_threadslice_pass; /* which pass of encoding we are on */
511 
512  /* bitstream output */
513  struct
514  {
515  int i_nal; /* current used nals */
516  int i_nals_allocated; /* allocated nals */
517  x264_nal_t *nal; /* nal unit list. output nals are guaranteed to be sequential in memory */
518  int i_bitstream; /* size of p_bitstream. at least 1MB */
519  uint8_t *p_bitstream; /* will hold raw data for all nal */
520  bs_t bs;
521  } out;
522 
523  uint8_t *nal_buffer; /* buffer for encapsulated nal unit. */
524  int nal_buffer_size; /* buffer size for encapsulated nal unit. at least 1.5 MB */
525 
526  /**** thread synchronization starts here ****/
527 
528  /* frame number/poc */
529  int i_frame; /* current frames */
530  int i_frame_num; /* total frames */
531 
532  /* Number of different frames being encoded by threads;
533  * 1 when sliced-threads is on, otherwise == x264_param_t.i_threads.
534  */
535  int i_thread_frames;
536  int i_nal_type; /* current nal unit type */
537  int i_nal_ref_idc; /* current nal unit reference indication */
538 
539  int64_t i_disp_fields; /* Number of displayed fields (both coded and implied via pic_struct) */
540  int i_disp_fields_last_frame;
541  int64_t i_prev_duration; /* Duration of previous frame */
542  int64_t i_coded_fields; /* Number of coded fields (both coded and implied via pic_struct) */
543  int64_t i_cpb_delay; /* Equal to number of fields preceding this field
544  * since last buffering_period SEI */
545  int64_t i_coded_fields_lookahead; /* Use separate counters for lookahead */
546  int64_t i_cpb_delay_lookahead;
547 
548  int64_t i_cpb_delay_pir_offset;
549  int64_t i_cpb_delay_pir_offset_next;
550 
551  int b_queued_intra_refresh;
552  int64_t i_last_idr_pts;
553 
554  /* IDR picture flag */
555  int i_idr_pic_id;
556 
557  /* quantization matrix for decoding, [cqm][qp%6][coef] */
558  int (*dequant4_mf[4])[16]; /* [4][6][16] */
559  /* quantization matrix for deadzone */
560  udctcoef (*quant4_mf[4])[16]; /* [4][52][16] */
561  udctcoef (*quant4_bias[4])[16]; /* [4][52][16] */
562 
563  /* mv/ref cost arrays. */
564  /* NOTE: x264_analyse_init_costs() mallocs 64KB for each integer between qp_min and qp_max.
565  * this may occupy space about: 64KB * 70 = 4.4MB
566  * cost_mv_fpel is used only when motion estimation is ESA or TESA.
567  */
568  uint16_t *cost_mv[QP_MAX + 1];
569 
570  /* includes both the nonlinear luma->chroma mapping and chroma_qp_offset.
571  * see definition of i_chroma_qp_table in common/macroblock.h
572  * h->chroma_qp_table = i_chroma_qp_table + 12 + h->pps->i_chroma_qp_index_offset;
573  */
574  const uint8_t *chroma_qp_table;
575 
576  /* Slice header */
578 
579  /* SPS / PPS */
580  x264_sps_t sps[1];
581  x264_pps_t pps[1];
582 
583  /* Slice header backup, for SEI_DEC_REF_PIC_MARKING */
584  int b_sh_backup;
585  x264_slice_header_t sh_backup;
586 
587  /* cabac context */
588  x264_cabac_t cabac;
589 
590  struct
591  {
592  /* Frames to be encoded (whose types have been decided) */
593  x264_frame_t **current;
594  /* Unused frames: 0 = fenc, 1 = fdec */
595  x264_frame_t **unused[2];
596 
597  /* Unused blank frames (for duplicates) */
598  x264_frame_t **blank_unused;
599 
600  /* frames used for reference + sentinels */
601  x264_frame_t *reference[X264_REF_MAX + 2];
602 
603  int i_last_keyframe; /* Frame number of the last keyframe */
604  int i_last_idr; /* Frame number of the last IDR (not RP)*/
605  int i_poc_last_open_gop; /* Poc of the I frame of the last open-gop. The value
606  * is only assigned during the period between that
607  * I frame and the next P or I frame, else -1 */
608 
609  int i_input; /* Number of input frames already accepted */
610 
611  int i_max_dpb; /* Number of frames allocated in the decoded picture buffer */
612  int i_max_ref0; /* Maximum frames of list 0 */
613  int i_max_ref1; /* Maximum frames of list 1 */
614  int i_delay; /* Number of frames buffered for B reordering */
615  int i_bframe_delay;
616  int64_t i_bframe_delay_time;
617  int64_t i_first_pts;
618  int64_t i_prev_reordered_pts[2];
619  int64_t i_largest_pts;
620  int64_t i_second_largest_pts;
621  /******************************************************
622  * Whether 1/2 resolution luma planes are being used. *
623  * It is activated when the encoder needs to generate *
624  * low resolution frames for the lookahead. *
625  ******************************************************/
626  int b_have_lowres;
627  int b_have_sub8x8_esa; /* Whether sub8x8 ESA is being used. default: off */
628  } frames;
629 
630  /* current frame being encoded */
631  x264_frame_t *fenc;
632 
633  /* frame being reconstructed */
634  x264_frame_t *fdec;
635 
636  /* references lists */
637  int i_ref[2]; /* frame number of l0/l1 */
638  x264_frame_t *fref[2][X264_REF_MAX + 3]; /* frames of l0/l1, ordered by distance */
639  x264_frame_t *fref_nearest[2]; /* nearest frame of l0/l1 */
640  int b_ref_reorder[2];
641 
642  /* hrd */
643  int initial_cpb_removal_delay;
644  int initial_cpb_removal_delay_offset;
645  int64_t i_reordered_pts_delay;
646 
647  /* Current MB DCT coeffs */
648  struct
649  {
650  /* Intra_16x16 LUMA DC for Hadamard transform */
651  ALIGNED_16(dctcoef luma16x16_dc[3][16]);
652  /* CHROMA DC for Hadamard transform */
653  ALIGNED_16(dctcoef chroma_dc[2][8]);
654  /* 4x4 DCT transform, including luma and chroma */
655  ALIGNED_16(dctcoef luma4x4[16 * 3][16]);
656  } dct;
657 
658  /* MB table and cache for current frame/mb */
659  struct
660  {
661  int i_mb_width; /* number of mbs in width */
662  int i_mb_height; /* number of mbs in height */
663  int i_mb_count; /* number of mbs in a frame */
664 
665  /* Chroma subsampling */
666  int chroma_h_shift; /* horizontal shift bits for Chroma, 1 for 4:2:0 format. */
667  int chroma_v_shift; /* vertical shift bits for Chroma, 1 for 4:2:0 format. */
668 
669  /* Strides */
670  int i_mb_stride; /* how many macro-blocks (16x16) in one line. */
671  int i_b8_stride; /* how many blocks (8x8) in one line. */
672  int i_b4_stride; /* how many blocks (4x4) in one line. */
673  int left_b8[2]; /* indices of two left blocks by 8x8 */
674  int left_b4[2]; /* indices of two left blocks by 4x4 */
675 
676  /* Current index */
677  int i_mb_x; /* x of current mb */
678  int i_mb_y; /* y of current mb */
679  int i_mb_xy; /* index of current mb */
680  int i_b8_xy; /* index of current mb counted by block 8x8 */
681  int i_b4_xy; /* index of current mb counted by block 4x4 */
682 
683  /* Search parameters */
684  int i_me_method; /* motion estimation method. see x264_param_t.i_me_method */
685  int i_subpel_refine; /* sub-pixel refine. see x264_param_t.i_subpel_refine */
686  /**********************************************************
687  * whether uses chroma in motion estimation. default: off *
688  * enabled only if x264_param_t.b_chroma_me == 1 and *
689  * (subme >= 5 if P or subme >= 9 if B) *
690  **********************************************************/
691  int b_chroma_me;
692  /*****************************************************************
693  * whether uses Trellis quantization. *
694  * This value depends on x264_param_t.analyse.i_trellis *
695  * *
696  * 1. for the analyse phase of a mb. *
697  * enabled only when i_trellis > 1 and i_mbrd > 0 *
698  * h->mb.b_trellis = h->param.analyse.i_trellis > 1 && a->i_mbrd *
699  * *
700  * 2. for the final encode of a mb. *
701  * enabled when i_trellis > 0 *
702  * h->mb.b_trellis = h->param.analyse.i_trellis *
703  *****************************************************************/
704  int b_trellis;
705  int b_noise_reduction; /* whether uses noise reduction. default: off, depends on h->param.analyse.i_noise_reduction */
706  int b_dct_decimate; /* whether uses dct decimate for P/B mb. default: on, depends on h->param.analyse.b_dct_decimate */
707  int i_psy_rd; /* Psy RD strength--fixed point value */
708  int i_psy_trellis; /* Psy trellis strength--fixed point value */
709 
710  int b_interlaced; /* interlaced mode. default: off */
711  int b_adaptive_mbaff; /* MBAFF+subme 0 requires non-adaptive MBAFF i.e. all field mbs. default: off */
712 
713  /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
714  int mv_min[2]; /* minimum motion vector in x/y */
715  int mv_max[2]; /* maximum motion vector in x/y */
716  int mv_miny_row[3]; /* 0 == top progressive, 1 == bot progressive, 2 == interlaced */
717  int mv_maxy_row[3];
718  /* Subpel MV range for motion search.
719  * same mv_min/max but includes levels' i_mv_range. */
720  int mv_min_spel[2]; /* min mv x/y of sub-pixel */
721  int mv_max_spel[2]; /* max mv x/y of sub-pixel */
722  int mv_miny_spel_row[3]; /* */
723  int mv_maxy_spel_row[3]; /* */
724  /* Fullpel MV range for motion search */
725  int mv_min_fpel[2]; /* min mv x/y of full-pixel */
726  int mv_max_fpel[2]; /* max mv x/y of full-pixel */
727  int mv_miny_fpel_row[3]; /* */
728  int mv_maxy_fpel_row[3]; /* */
729 
730  /* neighboring MBs */
731  unsigned int i_neighbour; /* position combination of neighbors that are available. eg: MB_LEFT|MB_TOP */
732  /*********************************************************
733  * neighbours of each 8x8 block that are available *
734  * at the time the block is coded. *
735  * *
736  * code order of 8x8 block in one macro-block: *
737  * 0 1 *
738  * 2 3 *
739  * *
740  * At the beginning of 8x8 block coding: *
741  * 1. available neighbours of block 3: *
742  * MB_LEFT | MB_TOP | MB_TOPLEFT *
743  * 2. available neighbours of other blocks is uncertain. *
744  *********************************************************/
745  unsigned int i_neighbour8[4];
746  /*********************************************************
747  * neighbours of each 4x4 block that are available *
748  * at the time the block is coded. *
749  * *
750  * code order of 4x4 block in one macro-block: *
751  * 0 1 4 5 *
752  * 2 3 6 7 *
753  * 8 9 12 13 *
754  * 10 11 14 15 *
755  * *
756  * At the beginning of 4x4 block coding: *
757  * 1. available neighbours of block 6, 9, 12, 14: *
758  * MB_LEFT | MB_TOP | MB_TOPLEFT | MB_TOPRIGHT *
759  * 2. available neighbours of block 3, 7, 11, 13, 15: *
760  * MB_LEFT | MB_TOP | MB_TOPLEFT *
761  * 3. available neighbours of other blocks is uncertain. *
762  *********************************************************/
763  unsigned int i_neighbour4[16];
764  unsigned int i_neighbour_intra; /* for constrained intra pred */
765  unsigned int i_neighbour_frame; /* ignoring slice boundaries */
766  int i_mb_type_top; /* type of top mb */
767  int i_mb_type_left[2]; /* type of two left mbs */
768  int i_mb_type_topleft; /* type of top-left mb */
769  int i_mb_type_topright; /* type of top-right mb */
770  int i_mb_prev_xy;
771  int i_mb_left_xy[2]; /* indices of two left mbs relative to h->mb.i_mb_xy */
772  int i_mb_top_xy; /* index of top mb relative to h->mb.i_mb_xy */
773  int i_mb_topleft_xy; /* index of top-left mb relative to h->mb.i_mb_xy */
774  int i_mb_topright_xy; /* index of top-right mb relative to h->mb.i_mb_xy */
775  int i_mb_top_y; /* coordinate-y of top mb relative to h->mb.i_mb_xy */
776  int i_mb_topleft_y; /* coordinate-y of top-left/top-right mb relative to h->mb.i_mb_xy */
777  int i_mb_topright_y; /* i_mb_top_y == i_mb_topleft_y == i_mb_topright_y if in progressive mode. */
778  const x264_left_table_t *left_index_table;
779  int i_mb_top_mbpair_xy;
780  int topleft_partition;
781  int b_allow_skip; /* whether allow P/B_SKIP mb. default: on */
782  int field_decoding_flag;
783 
784  /**** thread synchronization ends here ****/
785  /* subsequent variables are either thread-local or constant,
786  * and won't be copied from one thread to another */
787 
788  /* mb table of current frame */
789  int8_t *type; /* mb type. point to fdec: (h->mb.type = h->fdec->mb_type) */
790  uint8_t *partition; /* mb partition. point to fdec: (h->mb.partition = h->fdec->mb_partition) */
791  int8_t *qp; /* mb qp */
792  int16_t *cbp; /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc (all set for PCM)*/
793  int8_t (*intra4x4_pred_mode)[8]; /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
794  /* actually has only 7 entries; set to 8 for write-combining optimizations */
795  uint8_t (*non_zero_count)[16 * 3]; /* nzc. for I_PCM set to 16 */
796  int8_t *chroma_pred_mode; /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
797  int16_t (*mv[2])[2]; /* mb mv. set to 0 for intra mb. point to fdec: (h->mb.mv[0] = h->fdec->mv[0];h->mb.mv[1] = h->fdec->mv[1]) */
798  uint8_t (*mvd[2])[8][2]; /* absolute value of mb mv difference with predict, clipped to [0,33]. set to 0 if intra. cabac only */
799  int8_t *ref[2]; /* mb ref. set to -1 if non used (intra or Lx only). point to fdec: (h->mb.ref[0] = h->fdec->ref[0];h->mb.ref[1] = h->fdec->ref[1]) */
800  int16_t (*mvr[2][X264_REF_MAX * 2])[2]; /* 16x16 mv for each possible ref */
801  int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
802  int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */
803  uint16_t *slice_table; /* sh->first_mb of the slice that the indexed mb is part of
804  * NOTE: this will fail on resolutions above 2^16 MBs... */
805  uint8_t *field; /* point to fdec: (h->mb.field = h->fdec->field) */
806 
807  /* buffer for weighted versions of the reference frames */
808  pixel *p_weight_buf[X264_REF_MAX];
809 
810  /* current value */
811  /* current macro-block type.
812  * I_4x4,I_8x8,I_16x16,I_PCM,P_L0,P_8x8,P_SKIP,
813  * B_DIRECT,B_L0_L0,B_L0_L1,B_L0_BI,B_L1_L0,B_L1_L1,
814  * B_L1_BI,B_BI_L0,B_BI_L1,B_BI_BI,B_8x8,B_SKIP
815  */
816  int i_type;
817  /* current macro-block partition.
818  * D_16x16,D_8x16,D_16x8,D_8x8
819  */
820  int i_partition;
821  /* current macro-block sub-partition type. used only if (i_partition == D_8x8)
822  * for P_8x8:
823  * D_L0_4x4,D_L0_8x4,D_L0_4x8,D_L0_8x8
824  * for B_8x8:
825  * D_L0_4x4,D_L0_8x4,D_L0_4x8,D_L0_8x8,
826  * D_L1_4x4,D_L1_8x4,D_L1_4x8,D_L1_8x8,
827  * D_BI_4x4,D_BI_8x4,D_BI_4x8,D_BI_8x8,
828  * D_DIRECT_8x8
829  */
830  ALIGNED_4(uint8_t i_sub_partition[4]);
831  int b_transform_8x8; /* 8x8 transform flag for current mb */
832 
833  /**********************************************************************************
834  * Coded block pattern for luma. rage: [0-15] *
835  * If mb predict mode is Intra_16x16: *
836  * 0: all AC transform coefficient levels of the luma component of the *
837  * mb are equal to 0 for all 16 of the 4x4 blocks in the luma block *
838  * 15: at least one of the AC transform coefficient levels of the luma *
839  * component of the mb shall be non-zero, and the AC transform coefficient *
840  * levels are scanned for all 16 of the 4x4 blocks in the luma block *
841  * Otherwise (Not Intra_16x16), the following applies: *
842  * - If the corresponding bit of CodedBlockPatternLuma is equal to 0, *
843  * all transform coefficient levels of the luma transform blocks in the 8x8 *
844  * luma block are equal to zero. *
845  * - Otherwise (equal to 1), one or more transform coefficient levels of one *
846  * or more of the luma transform blocks in the 8x8 luma block shall be non-zero *
847  * valued and the transform coefficient levels of the corresponding transform *
848  * blocks are scanned. *
849  **********************************************************************************/
850  int i_cbp_luma;
851  /*********************************************************************************************
852  * Coded block pattern for chroma. range: [0-2] *
853  * 0 (none): All chroma transform coefficient levels are equal to 0. *
854  * 1 (dc only): One or more chroma DC transform coefficient levels shall be non-zero valued. *
855  * All chroma AC transform coefficient levels are equal to 0. *
856  * 2 (dc + ac): Zero or more chroma DC transform coefficient levels are non-zero valued. *
857  * One or more chroma AC transform coefficient levels shall be non-zero valued. *
858  *********************************************************************************************/
859  int i_cbp_chroma;
860 
861  int i_intra16x16_pred_mode; /* Intra_16x16 predict mode */
862  int i_chroma_pred_mode; /* Chroma predict mode */
863 
864  /***************************************************************************************
865  * skip flags for i4x4 and i8x8. default: 1 *
866  * 0 = encode as normal. *
867  * 1 (non-RD only) = the DCT is still in h->dct, restore fdec and skip reconstruction. *
868  * 2 (RD only) = the DCT has since been overwritten by RD; restore that too. *
869  ***************************************************************************************/
870  int i_skip_intra;
871  /* skip flag for motion compensation */
872  /* if we've already done MC, we don't need to do it again */
873  int b_skip_mc;
874  /* set to true if we are re-encoding a macroblock. */
875  int b_reencode_mb;
876  int ip_offset; /* Used by PIR to offset the quantizer of intra-refresh blocks. */
877  int b_deblock_rdo; /* Whether call x264_macroblock_deblock for each mb rd cost. default: off */
878  int b_overflow; /* If CAVLC had a level code overflow during bitstream writing. */
879 
880  struct
881  {
882  /* space for p_fenc and p_fdec */
883 #define FENC_STRIDE 16
884 #define FDEC_STRIDE 32
885  ALIGNED_16(pixel fenc_buf[48 * FENC_STRIDE]);
886  ALIGNED_16(pixel fdec_buf[52 * FDEC_STRIDE]);
887 
888  /* i4x4 backup data, for skipping the encode stage when possible */
889  ALIGNED_16(pixel i4x4_fdec_buf[16 * 16]);
890  ALIGNED_16(dctcoef i4x4_dct_buf[15][16]);
891  uint32_t i4x4_nnz_buf[4];
892  int i4x4_cbp;
893 
894  /* pointer of y/u/v over mb of the frame to be compressed */
895  pixel *p_fenc[3];
896  /* pointer to the actual source frame, not a block copy */
897  pixel *p_fenc_plane[3];
898 
899  /* pointer of y/u/v over mb of the frame to be reconstructed */
900  pixel *p_fdec[3];
901 
902  /* pointer over mb of the references of l0/l1 */
903  int i_fref[2];
904  /****************************************************************************
905  * p_fref stores pixel values based on 1/2 sub-pixel precision. *
906  * Index: yN, yH, yV, yHV, (NV12 ? uv : I444 ? (uN, uH, uV, uHV, vN, ...)) *
907  * [0] yN: full-pixel value of luma *
908  * [1] yH: horizontal half-pixel value of luma (right of full-pixel) *
909  * [2] yV: vertical half-pixel value of luma (down of full-pixel) *
910  * [3] yHV: hv half-pixel value of luma (diagonal down right of full-pixel) *
911  * *
912  * for NV12/I420: *
913  * [4] uv: full-pixel value of chroma *
914  * for I444: *
915  * [4] uN, [5] uH, [6] uV, [7] uHV, [8] vN, [9] vH, [10] vV, [11] vHV *
916  ****************************************************************************/
917  pixel *p_fref[2][X264_REF_MAX * 2][12];
918  pixel *p_fref_w[X264_REF_MAX * 2]; /* weighted fullpel luma */
919 
920  /* fref stride */
921  int i_stride[3];
922  } pic;
923 
924  /* cache */
925  struct
926  {
927  /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
928  ALIGNED_8(int8_t intra4x4_pred_mode[X264_SCAN8_LUMA_SIZE]);
929 
930  /* i_non_zero_count if available else 0x80 */
931  ALIGNED_16(uint8_t non_zero_count[X264_SCAN8_SIZE]);
932 
933  /* -1 if unused, -2 if unavailable */
934  ALIGNED_4(int8_t ref[2][X264_SCAN8_LUMA_SIZE]); /* reference frame number of last mb */
935 
936  /* 0 if not available */
937  ALIGNED_16(int16_t mv[2][X264_SCAN8_LUMA_SIZE][2]); /* motion vectors of last mb in x/y */
938  ALIGNED_8(uint8_t mvd[2][X264_SCAN8_LUMA_SIZE][2]); /* motion vectors delta of last mb in x/y */
939 
940  /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
941  ALIGNED_4(int8_t skip[X264_SCAN8_LUMA_SIZE]);
942 
943  ALIGNED_4(int16_t direct_mv[2][4][2]);
944  ALIGNED_4(int8_t direct_ref[2][4]);
945  int direct_partition;
946  ALIGNED_4(int16_t pskip_mv[2]);
947 
948  /* number of neighbors (top and left) that used 8x8 dct */
949  int i_neighbour_transform_size;
950  int i_neighbour_skip;
951 
952  /* neighbor CBPs */
953  int i_cbp_top; /* cbp of top mb */
954  int i_cbp_left; /* cbp of left mb */
955 
956  /* extra data required for mbaff in mv prediction */
957  int16_t topright_mv[2][3][2];
958  int8_t topright_ref[2][3];
959 
960  /* current mb deblock strength */
961  uint8_t (*deblock_strength)[8][4];
962  } cache;
963 
964  /* qp */
965  int i_qp; /* luma qp of current mb */
966  int i_chroma_qp; /* chroma qp of current mb */
967  int i_last_qp; /* luma qp of last mb */
968  int i_last_dqp; /* last delta qp. always 0 if qp is constant in slice. */
969  int b_variable_qp; /* whether qp is allowed to vary per macroblock. depends on whether VBV enabled or AQ enabled. */
970  int b_lossless; /* lossless flag. (rc.i_qp_constant == 0). default: off */
971  int b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
972  int b_direct_auto_write; /* analyse direct modes, to use and/or save */
973 
974  /* B_direct and weighted prediction */
975  int16_t dist_scale_factor_buf[2][2][X264_REF_MAX * 2][4];
976  int16_t (*dist_scale_factor)[4];
977  int8_t bipred_weight_buf[2][2][X264_REF_MAX * 2][4];
978  int8_t (*bipred_weight)[4];
979  /* maps fref1[0]'s ref indices into the current list0 */
980 #define map_col_to_list0(col) h->mb.map_col_to_list0[(col) + 2]
981  int8_t map_col_to_list0[X264_REF_MAX + 2];
982  int ref_blind_dupe; /* The index of the blind reference frame duplicate. */
983  int8_t deblock_ref_table[X264_REF_MAX * 2 + 2];
984 #define deblock_ref_table(x) h->mb.deblock_ref_table[(x) + 2]
985  } mb;
986 
987  /* rate control encoding only */
988  x264_ratecontrol_t *rc;
989 
990  /* stats */
991  struct
992  {
993  /* Current frame stats */
994  x264_frame_stat_t frame;
995 
996  /* Cumulated stats */
997 
998  /* per slice info */
999  int i_frame_count[3]; /* Accumulated frame count for P/B/I frames */
1000  int64_t i_frame_size[3]; /* Accumulated frame bytes for P/B/I frames */
1001  double f_frame_qp[3]; /* Accumulated frame QP for P/B/I frames */
1002  int i_consecutive_bframes[X264_BFRAME_MAX + 1];
1003 
1004  /* psnr and ssim */
1005 #ifdef _DEBUG
1006  double f_ssd_global[3]; /* Accumulated SSD for P/B/I frames */
1007  double f_psnr_average[3]; /* Average PSNR for P/B/I frames */
1008  double f_psnr_mean_y[3]; /* Mean LUMA PSNR for P/B/I frames */
1009  double f_psnr_mean_u[3]; /* Mean CHROMA (u) PSNR for P/B/I frames */
1010  double f_psnr_mean_v[3]; /* Mean CHROMA (v) PSNR for P/B/I frames */
1011  double f_ssim_mean_y[3]; /* Mean LUMA SSIM for P/B/I frames */
1012  double f_frame_duration[3]; /* Accumulated frame duration for P/B/I frames */
1013 #endif
1014 
1015  /* macro-block stat */
1016 #ifdef _DEBUG
1017  /**************************************************************
1018  * Accumulated mb count for 19 specific types of P/B/I frames *
1019  * *
1020  * I_4x4, I_8x8, I_16x16, I_PCM, P_L0, P_8x8, P_SKIP, *
1021  * B_DIRECT, B_L0_L0, B_L0_L1, B_L0_BI, B_L1_L0, B_L1_L1, *
1022  * B_L1_BI, B_BI_L0, B_BI_L1, B_BI_BI, B_8x8, B_SKIP *
1023  **************************************************************/
1024  int64_t i_mb_count[3][19];
1025  /**************************************************************
1026  * Accumulated mb count for 17 partition types of P/B frames *
1027  * *
1028  * sub partition type for P_8x8 and B_8x8 *
1029  * D_L0_4x4, D_L0_8x4, D_L0_4x8, D_L0_8x8, *
1030  * *
1031  * sub partition type for B_8x8 only *
1032  * D_L1_4x4, D_L1_8x4, D_L1_4x8, D_L1_8x8, *
1033  * D_BI_4x4, D_BI_8x4, D_BI_4x8, D_BI_8x8, *
1034  * D_DIRECT_8x8, *
1035  * *
1036  * partition *
1037  * D_8x8, D_16x8, D_8x16, D_16x16 *
1038  **************************************************************/
1039  int64_t i_mb_partition[2][17];
1040  int64_t i_mb_count_8x8dct[2];
1041  /**************************************************************
1042  * Accumulated mb count for referenced index of P/B frames *
1043  * [0][0]: L0 count of P-slices. *
1044  * [0][1]: Not used. *
1045  * [1][0]: L0 count of B-slices. *
1046  * [1][1]: L1 count of B-slices. *
1047  **************************************************************/
1048  int64_t i_mb_count_ref[2][2][X264_REF_MAX * 2];
1049  /**************************************************************
1050  * Accumulated CodeBlockPattern count for all frames *
1051  * [0]: Intra_y *
1052  * [1]: Inter_y *
1053  * [2]: Intra_uvDC/Intra_u *
1054  * [3]: Inter_uvDC/Inter_u *
1055  * [4]: Intra_uvAC/Intra_v *
1056  * [5]: Inter_uvAC/Inter_v *
1057  **************************************************************/
1058  int64_t i_mb_cbp[6];
1059  /**************************************************************
1060  * Accumulated mb count for 4 predict modes of all frames *
1061  * *
1062  * [0]: I16x16 by (V,H,DC,P,DC_LEFT,DC_TOP,DC_128) *
1063  * [1]: I8x8 by (V,H,DC,DDL,DDR,VR,HD,VL,HU, *
1064  * [2]: I4x4 DC_LEFT,DC_TOP,DC_128) *
1065  * [3]: I8x8c by (DC,H,V,P,DC_LEFT,DC_TOP,DC_128) *
1066  **************************************************************/
1067  int64_t i_mb_pred_mode[4][13];
1068  int64_t i_mb_field[3];
1069 #endif
1070 
1071  /* score of B-frames direct predict mode by temporal and spatial */
1072  int i_direct_score[2];
1073  /* num B-frames of direct predict mode by temporal and spatial */
1074  int i_direct_frames[2];
1075  /* num P-frames weighted for LUMA and CHROMA */
1076  int i_wpred[2];
1077  } stat;
1078 
1079  /* noise reduction */
1080  /* 0 = luma 4x4, 1 = luma 8x8, 2 = chroma 4x4, 3 = chroma 8x8 */
1081  udctcoef (*nr_offset)[64];
1082  uint32_t (*nr_residual_sum)[64];
1083  uint32_t *nr_count;
1084 
1085  ALIGNED_16(udctcoef nr_offset_denoise[4][64]);
1086  ALIGNED_16(uint32_t nr_residual_sum_buf[2][4][64]);
1087  uint32_t nr_count_buf[2][4];
1088 
1089  uint8_t luma2chroma_pixel[7]; /* Sub-sampled pixel size. for 4:2:0: 16x16 -> 8x8, 8x8 -> 4x4, 4x4 -> 2x2 */
1090 
1091  /* Buffers that are allocated per-thread even in sliced threads. */
1092  void *scratch_buffer; /* for any temporary storage that doesn't want repeated malloc */
1093  void *scratch_buffer2; /* used for output of frame cost, see x264_slicetype_frame_cost */
1094  pixel *intra_border_backup[5][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
1095  /* Deblock strength values are stored for each 4x4 partition. In MBAFF
1096  * there are four extra values that need to be stored, located in [4][i]. */
1097  uint8_t (*deblock_strength[2])[2][8][4];
1098 
1099  /* CPU functions dependents */
1100  x264_predict_t predict_16x16[4 + 3];
1101  x264_predict_t predict_4x4[9 + 3];
1102  x264_predict_t predict_chroma[4 + 3]; /* CHROMA predict, 8x8c or 8x16c */
1103  x264_predict_t predict_8x8c[4 + 3]; /* for CHROMA_420 */
1104  x264_pixel_function_t pixf;
1106  x264_dct_function_t dctf;
1107  x264_zigzag_function_t zigzagf;
1108  x264_quant_function_t quantf;
1111 
1112  x264_lookahead_t *lookahead; /* lookahead struct for each thread */
1113 };
1114 
1115 // included at the end because it needs x264_t
1116 #include "macroblock.h"
1117 
1118 #include "rectangle.h"
1119 
1120 #endif
Definition: me.h:11
Definition: bitstream.h:22
Definition: bitstream.h:46
Definition: cabac.h:9
Definition: dct.h:9
Definition: frame.h:208
Definition: common.h:473
Definition: frame.h:12
Definition: common.h:463
Definition: common.h:449
Definition: mc.h:31
Definition: x264.h:52
Definition: x264.h:187
Definition: pixel.h:70
Definition: set.h:186
Definition: quant.h:9
Definition: ratecontrol.c:83
Definition: common.h:344
Definition: set.h:71
Definition: frame.h:199
Definition: common.h:499
Definition: common.h:121
Definition: mc.h:10
Definition: dct.h:31
Definition: common.h:106