]> git.sesse.net Git - x264/blob - common/common.h
Simplify decimate checks in macroblock_encode
[x264] / common / common.h
1 /*****************************************************************************
2  * common.h: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2003-2008 x264 project
5  *
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *          Loren Merritt <lorenm@u.washington.edu>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef X264_COMMON_H
25 #define X264_COMMON_H
26
27 /****************************************************************************
28  * Macros
29  ****************************************************************************/
30 #define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
31 #define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
32 #define X264_MIN3(a,b,c) X264_MIN((a),X264_MIN((b),(c)))
33 #define X264_MAX3(a,b,c) X264_MAX((a),X264_MAX((b),(c)))
34 #define X264_MIN4(a,b,c,d) X264_MIN((a),X264_MIN3((b),(c),(d)))
35 #define X264_MAX4(a,b,c,d) X264_MAX((a),X264_MAX3((b),(c),(d)))
36 #define XCHG(type,a,b) do{ type t = a; a = b; b = t; } while(0)
37 #define IS_DISPOSABLE(type) ( type == X264_TYPE_B )
38 #define FIX8(f) ((int)(f*(1<<8)+.5))
39
40 #define CHECKED_MALLOC( var, size )\
41 do {\
42     var = x264_malloc( size );\
43     if( !var )\
44         goto fail;\
45 } while( 0 )
46 #define CHECKED_MALLOCZERO( var, size )\
47 do {\
48     CHECKED_MALLOC( var, size );\
49     memset( var, 0, size );\
50 } while( 0 )
51
52 #define X264_BFRAME_MAX 16
53 #define X264_THREAD_MAX 128
54 #define X264_PCM_COST (386*8)
55 #define X264_LOOKAHEAD_MAX 250
56 // arbitrary, but low because SATD scores are 1/4 normal
57 #define X264_LOOKAHEAD_QP 12
58
59 // number of pixels (per thread) in progress at any given time.
60 // 16 for the macroblock in progress + 3 for deblocking + 3 for motion compensation filter + 2 for extra safety
61 #define X264_THREAD_HEIGHT 24
62
63 /* WEIGHTP_FAKE is set when mb_tree & psy are enabled, but normal weightp is disabled
64  * (such as in baseline). It checks for fades in lookahead and adjusts qp accordingly
65  * to increase quality. Defined as (-1) so that if(i_weighted_pred > 0) is true only when
66  * real weights are being used. */
67
68 #define X264_WEIGHTP_FAKE (-1)
69
70 /****************************************************************************
71  * Includes
72  ****************************************************************************/
73 #include "osdep.h"
74 #include <stdarg.h>
75 #include <stddef.h>
76 #include <stdlib.h>
77 #include <string.h>
78 #include <assert.h>
79 #include <limits.h>
80
81 /* Unions for type-punning.
82  * Mn: load or store n bits, aligned, native-endian
83  * CPn: copy n bits, aligned, native-endian
84  * we don't use memcpy for CPn because memcpy's args aren't assumed to be aligned */
85 typedef union { uint16_t i; uint8_t  c[2]; } MAY_ALIAS x264_union16_t;
86 typedef union { uint32_t i; uint16_t b[2]; uint8_t  c[4]; } MAY_ALIAS x264_union32_t;
87 typedef union { uint64_t i; uint32_t a[2]; uint16_t b[4]; uint8_t c[8]; } MAY_ALIAS x264_union64_t;
88 #define M16(src) (((x264_union16_t*)(src))->i)
89 #define M32(src) (((x264_union32_t*)(src))->i)
90 #define M64(src) (((x264_union64_t*)(src))->i)
91 #define CP16(dst,src) M16(dst) = M16(src)
92 #define CP32(dst,src) M32(dst) = M32(src)
93 #define CP64(dst,src) M64(dst) = M64(src)
94
95 #include "x264.h"
96 #include "bs.h"
97 #include "set.h"
98 #include "predict.h"
99 #include "pixel.h"
100 #include "mc.h"
101 #include "frame.h"
102 #include "dct.h"
103 #include "cabac.h"
104 #include "quant.h"
105
106 /****************************************************************************
107  * General functions
108  ****************************************************************************/
109 /* x264_malloc : will do or emulate a memalign
110  * you have to use x264_free for buffers allocated with x264_malloc */
111 void *x264_malloc( int );
112 void  x264_free( void * );
113
114 /* x264_slurp_file: malloc space for the whole file and read it */
115 char *x264_slurp_file( const char *filename );
116
117 /* mdate: return the current date in microsecond */
118 int64_t x264_mdate( void );
119
120 /* x264_param2string: return a (malloced) string containing most of
121  * the encoding options */
122 char *x264_param2string( x264_param_t *p, int b_res );
123
124 int x264_nal_encode( uint8_t *dst, int b_annexb, x264_nal_t *nal );
125
126 /* log */
127 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
128
129 void x264_reduce_fraction( int *n, int *d );
130 void x264_init_vlc_tables();
131
132 static inline uint8_t x264_clip_uint8( int x )
133 {
134     return x&(~255) ? (-x)>>31 : x;
135 }
136
137 static inline int x264_clip3( int v, int i_min, int i_max )
138 {
139     return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
140 }
141
142 static inline double x264_clip3f( double v, double f_min, double f_max )
143 {
144     return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
145 }
146
147 static inline int x264_median( int a, int b, int c )
148 {
149     int t = (a-b)&((a-b)>>31);
150     a -= t;
151     b += t;
152     b -= (b-c)&((b-c)>>31);
153     b += (a-b)&((a-b)>>31);
154     return b;
155 }
156
157 static inline void x264_median_mv( int16_t *dst, int16_t *a, int16_t *b, int16_t *c )
158 {
159     dst[0] = x264_median( a[0], b[0], c[0] );
160     dst[1] = x264_median( a[1], b[1], c[1] );
161 }
162
163 static inline int x264_predictor_difference( int16_t (*mvc)[2], intptr_t i_mvc )
164 {
165     int sum = 0, i;
166     for( i = 0; i < i_mvc-1; i++ )
167     {
168         sum += abs( mvc[i][0] - mvc[i+1][0] )
169              + abs( mvc[i][1] - mvc[i+1][1] );
170     }
171     return sum;
172 }
173
174 static inline uint32_t x264_cabac_amvd_sum( int16_t *mvdleft, int16_t *mvdtop )
175 {
176     int amvd0 = abs(mvdleft[0]) + abs(mvdtop[0]);
177     int amvd1 = abs(mvdleft[1]) + abs(mvdtop[1]);
178     amvd0 = (amvd0 > 2) + (amvd0 > 32);
179     amvd1 = (amvd1 > 2) + (amvd1 > 32);
180     return amvd0 + (amvd1<<16);
181 }
182
183 extern const uint8_t x264_exp2_lut[64];
184 extern const float x264_log2_lut[128];
185 extern const float x264_log2_lz_lut[32];
186
187 /* Not a general-purpose function; multiplies input by -1/6 to convert
188  * qp to qscale. */
189 static ALWAYS_INLINE int x264_exp2fix8( float x )
190 {
191     int i = x*(-64.f/6.f) + 512.5f;
192     if( i < 0 ) return 0;
193     if( i > 1023 ) return 0xffff;
194     return (x264_exp2_lut[i&63]+256) << (i>>6) >> 8;
195 }
196
197 static ALWAYS_INLINE float x264_log2( uint32_t x )
198 {
199     int lz = x264_clz( x );
200     return x264_log2_lut[(x<<lz>>24)&0x7f] + x264_log2_lz_lut[lz];
201 }
202
203 /****************************************************************************
204  *
205  ****************************************************************************/
206 enum slice_type_e
207 {
208     SLICE_TYPE_P  = 0,
209     SLICE_TYPE_B  = 1,
210     SLICE_TYPE_I  = 2,
211     SLICE_TYPE_SP = 3,
212     SLICE_TYPE_SI = 4
213 };
214
215 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
216
217 typedef struct
218 {
219     x264_sps_t *sps;
220     x264_pps_t *pps;
221
222     int i_type;
223     int i_first_mb;
224     int i_last_mb;
225
226     int i_pps_id;
227
228     int i_frame_num;
229
230     int b_mbaff;
231     int b_field_pic;
232     int b_bottom_field;
233
234     int i_idr_pic_id;   /* -1 if nal_type != 5 */
235
236     int i_poc_lsb;
237     int i_delta_poc_bottom;
238
239     int i_delta_poc[2];
240     int i_redundant_pic_cnt;
241
242     int b_direct_spatial_mv_pred;
243
244     int b_num_ref_idx_override;
245     int i_num_ref_idx_l0_active;
246     int i_num_ref_idx_l1_active;
247
248     int b_ref_pic_list_reordering_l0;
249     int b_ref_pic_list_reordering_l1;
250     struct
251     {
252         int idc;
253         int arg;
254     } ref_pic_list_order[2][16];
255
256     /* P-frame weighting */
257     x264_weight_t weight[32][3];
258
259     int i_mmco_remove_from_end;
260     int i_mmco_command_count;
261     struct /* struct for future expansion */
262     {
263         int i_difference_of_pic_nums;
264         int i_poc;
265     } mmco[16];
266
267     int i_cabac_init_idc;
268
269     int i_qp;
270     int i_qp_delta;
271     int b_sp_for_swidth;
272     int i_qs_delta;
273
274     /* deblocking filter */
275     int i_disable_deblocking_filter_idc;
276     int i_alpha_c0_offset;
277     int i_beta_offset;
278
279 } x264_slice_header_t;
280
281 typedef struct x264_lookahead_t
282 {
283     volatile uint8_t              b_exit_thread;
284     uint8_t                       b_thread_active;
285     uint8_t                       b_analyse_keyframe;
286     int                           i_last_keyframe;
287     int                           i_slicetype_length;
288     x264_frame_t                  *last_nonb;
289     x264_synch_frame_list_t       ifbuf;
290     x264_synch_frame_list_t       next;
291     x264_synch_frame_list_t       ofbuf;
292 } x264_lookahead_t;
293
294 /* From ffmpeg
295  */
296 #define X264_SCAN8_SIZE (6*8)
297 #define X264_SCAN8_0 (4+1*8)
298
299 static const int x264_scan8[16+2*4+3] =
300 {
301     /* Luma */
302     4+1*8, 5+1*8, 4+2*8, 5+2*8,
303     6+1*8, 7+1*8, 6+2*8, 7+2*8,
304     4+3*8, 5+3*8, 4+4*8, 5+4*8,
305     6+3*8, 7+3*8, 6+4*8, 7+4*8,
306
307     /* Cb */
308     1+1*8, 2+1*8,
309     1+2*8, 2+2*8,
310
311     /* Cr */
312     1+4*8, 2+4*8,
313     1+5*8, 2+5*8,
314
315     /* Luma DC */
316     4+5*8,
317
318     /* Chroma DC */
319     5+5*8, 6+5*8
320 };
321 /*
322    0 1 2 3 4 5 6 7
323  0
324  1   B B   L L L L
325  2   B B   L L L L
326  3         L L L L
327  4   R R   L L L L
328  5   R R   DyDuDv
329 */
330
331 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
332
333 struct x264_t
334 {
335     /* encoder parameters */
336     x264_param_t    param;
337
338     x264_t          *thread[X264_THREAD_MAX+1];
339     x264_pthread_t  thread_handle;
340     int             b_thread_active;
341     int             i_thread_phase; /* which thread to use for the next frame */
342     int             i_threadslice_start; /* first row in this thread slice */
343     int             i_threadslice_end; /* row after the end of this thread slice */
344
345     /* bitstream output */
346     struct
347     {
348         int         i_nal;
349         int         i_nals_allocated;
350         x264_nal_t  *nal;
351         int         i_bitstream;    /* size of p_bitstream */
352         uint8_t     *p_bitstream;   /* will hold data for all nal */
353         bs_t        bs;
354     } out;
355
356     uint8_t *nal_buffer;
357     int      nal_buffer_size;
358
359     /**** thread synchronization starts here ****/
360
361     /* frame number/poc */
362     int             i_frame;
363     int             i_frame_num;
364
365     int             i_thread_frames; /* Number of different frames being encoded by threads;
366                                       * 1 when sliced-threads is on. */
367     int             i_nal_type;
368     int             i_nal_ref_idc;
369
370     /* We use only one SPS and one PPS */
371     x264_sps_t      sps_array[1];
372     x264_sps_t      *sps;
373     x264_pps_t      pps_array[1];
374     x264_pps_t      *pps;
375     int             i_idr_pic_id;
376
377     /* Timebase multiplier for DTS compression */
378     int             i_dts_compress_multiplier;
379
380     /* quantization matrix for decoding, [cqm][qp%6][coef] */
381     int             (*dequant4_mf[4])[16];   /* [4][6][16] */
382     int             (*dequant8_mf[2])[64];   /* [2][6][64] */
383     /* quantization matrix for trellis, [cqm][qp][coef] */
384     int             (*unquant4_mf[4])[16];   /* [4][52][16] */
385     int             (*unquant8_mf[2])[64];   /* [2][52][64] */
386     /* quantization matrix for deadzone */
387     uint16_t        (*quant4_mf[4])[16];     /* [4][52][16] */
388     uint16_t        (*quant8_mf[2])[64];     /* [2][52][64] */
389     uint16_t        (*quant4_bias[4])[16];   /* [4][52][16] */
390     uint16_t        (*quant8_bias[2])[64];   /* [2][52][64] */
391
392     /* mv/ref cost arrays.  Indexed by lambda instead of
393      * qp because, due to rounding, some quantizers share
394      * lambdas.  This saves memory. */
395     uint16_t *cost_mv[92];
396     uint16_t *cost_mv_fpel[92][4];
397
398     const uint8_t   *chroma_qp_table; /* includes both the nonlinear luma->chroma mapping and chroma_qp_offset */
399
400     ALIGNED_16( uint32_t nr_residual_sum[2][64] );
401     ALIGNED_16( uint16_t nr_offset[2][64] );
402     uint32_t        nr_count[2];
403
404     /* Slice header */
405     x264_slice_header_t sh;
406
407     /* cabac context */
408     x264_cabac_t    cabac;
409
410     struct
411     {
412         /* Frames to be encoded (whose types have been decided) */
413         x264_frame_t **current;
414         /* Unused frames: 0 = fenc, 1 = fdec */
415         x264_frame_t **unused[2];
416
417         /* Unused blank frames (for duplicates) */
418         x264_frame_t **blank_unused;
419
420         /* frames used for reference + sentinels */
421         x264_frame_t *reference[16+2];
422
423         int i_last_keyframe; /* Frame number of the last keyframe */
424
425         int i_input;    /* Number of input frames already accepted */
426
427         int i_max_dpb;  /* Number of frames allocated in the decoded picture buffer */
428         int i_max_ref0;
429         int i_max_ref1;
430         int i_delay;    /* Number of frames buffered for B reordering */
431         int     i_bframe_delay;
432         int64_t i_bframe_delay_time;
433         int64_t i_init_delta;
434         int64_t i_prev_dts[2];
435         int b_have_lowres;  /* Whether 1/2 resolution luma planes are being used */
436         int b_have_sub8x8_esa;
437     } frames;
438
439     /* current frame being encoded */
440     x264_frame_t    *fenc;
441
442     /* frame being reconstructed */
443     x264_frame_t    *fdec;
444
445     /* references lists */
446     int             i_ref0;
447     x264_frame_t    *fref0[16+3];     /* ref list 0 */
448     int             i_ref1;
449     x264_frame_t    *fref1[16+3];     /* ref list 1 */
450     int             b_ref_reorder[2];
451
452
453
454     /* Current MB DCT coeffs */
455     struct
456     {
457         ALIGNED_16( int16_t luma16x16_dc[16] );
458         ALIGNED_16( int16_t chroma_dc[2][4] );
459         // FIXME share memory?
460         ALIGNED_16( int16_t luma8x8[4][64] );
461         ALIGNED_16( int16_t luma4x4[16+8][16] );
462     } dct;
463
464     /* MB table and cache for current frame/mb */
465     struct
466     {
467         int     i_mb_count;                 /* number of mbs in a frame */
468
469         /* Strides */
470         int     i_mb_stride;
471         int     i_b8_stride;
472         int     i_b4_stride;
473
474         /* Current index */
475         int     i_mb_x;
476         int     i_mb_y;
477         int     i_mb_xy;
478         int     i_b8_xy;
479         int     i_b4_xy;
480
481         /* Search parameters */
482         int     i_me_method;
483         int     i_subpel_refine;
484         int     b_chroma_me;
485         int     b_trellis;
486         int     b_noise_reduction;
487         int     b_dct_decimate;
488         int     i_psy_rd; /* Psy RD strength--fixed point value*/
489         int     i_psy_trellis; /* Psy trellis strength--fixed point value*/
490
491         int     b_interlaced;
492
493         /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
494         int     mv_min[2];
495         int     mv_max[2];
496         /* Subpel MV range for motion search.
497          * same mv_min/max but includes levels' i_mv_range. */
498         int     mv_min_spel[2];
499         int     mv_max_spel[2];
500         /* Fullpel MV range for motion search */
501         int     mv_min_fpel[2];
502         int     mv_max_fpel[2];
503
504         /* neighboring MBs */
505         unsigned int i_neighbour;
506         unsigned int i_neighbour8[4];       /* neighbours of each 8x8 or 4x4 block that are available */
507         unsigned int i_neighbour4[16];      /* at the time the block is coded */
508         unsigned int i_neighbour_intra;     /* for constrained intra pred */
509         int     i_mb_type_top;
510         int     i_mb_type_left;
511         int     i_mb_type_topleft;
512         int     i_mb_type_topright;
513         int     i_mb_prev_xy;
514         int     i_mb_top_xy;
515
516         /**** thread synchronization ends here ****/
517         /* subsequent variables are either thread-local or constant,
518          * and won't be copied from one thread to another */
519
520         /* mb table */
521         int8_t  *type;                      /* mb type */
522         int8_t  *qp;                        /* mb qp */
523         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
524         int8_t  (*intra4x4_pred_mode)[8];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
525                                             /* actually has only 7 entries; set to 8 for write-combining optimizations */
526         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
527         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
528         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
529         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
530         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
531         int16_t (*mvr[2][32])[2];           /* 16x16 mv for each possible ref */
532         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
533         int8_t  *mb_transform_size;         /* transform_size_8x8_flag of each mb */
534         uint8_t *intra_border_backup[2][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
535
536          /* buffer for weighted versions of the reference frames */
537         uint8_t *p_weight_buf[16];
538
539         /* current value */
540         int     i_type;
541         int     i_partition;
542         ALIGNED_4( uint8_t i_sub_partition[4] );
543         int     b_transform_8x8;
544
545         int     i_cbp_luma;
546         int     i_cbp_chroma;
547
548         int     i_intra16x16_pred_mode;
549         int     i_chroma_pred_mode;
550
551         /* skip flags for i4x4 and i8x8
552          * 0 = encode as normal.
553          * 1 (non-RD only) = the DCT is still in h->dct, restore fdec and skip reconstruction.
554          * 2 (RD only) = the DCT has since been overwritten by RD; restore that too. */
555         int i_skip_intra;
556         /* skip flag for motion compensation */
557         /* if we've already done MC, we don't need to do it again */
558         int b_skip_mc;
559         /* set to true if we are re-encoding a macroblock. */
560         int b_reencode_mb;
561         int ip_offset; /* Used by PIR to offset the quantizer of intra-refresh blocks. */
562
563         struct
564         {
565             /* space for p_fenc and p_fdec */
566 #define FENC_STRIDE 16
567 #define FDEC_STRIDE 32
568             ALIGNED_16( uint8_t fenc_buf[24*FENC_STRIDE] );
569             ALIGNED_16( uint8_t fdec_buf[27*FDEC_STRIDE] );
570
571             /* i4x4 and i8x8 backup data, for skipping the encode stage when possible */
572             ALIGNED_16( uint8_t i4x4_fdec_buf[16*16] );
573             ALIGNED_16( uint8_t i8x8_fdec_buf[16*16] );
574             ALIGNED_16( int16_t i8x8_dct_buf[3][64] );
575             ALIGNED_16( int16_t i4x4_dct_buf[15][16] );
576             uint32_t i4x4_nnz_buf[4];
577             uint32_t i8x8_nnz_buf[4];
578             int i4x4_cbp;
579             int i8x8_cbp;
580
581             /* Psy trellis DCT data */
582             ALIGNED_16( int16_t fenc_dct8[4][64] );
583             ALIGNED_16( int16_t fenc_dct4[16][16] );
584
585             /* Psy RD SATD scores */
586             int fenc_satd[4][4];
587             int fenc_satd_sum;
588             int fenc_sa8d[2][2];
589             int fenc_sa8d_sum;
590
591             /* pointer over mb of the frame to be compressed */
592             uint8_t *p_fenc[3];
593             /* pointer to the actual source frame, not a block copy */
594             uint8_t *p_fenc_plane[3];
595
596             /* pointer over mb of the frame to be reconstructed  */
597             uint8_t *p_fdec[3];
598
599             /* pointer over mb of the references */
600             int i_fref[2];
601             uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
602             uint8_t *p_fref_w[32];  /* weighted fullpel luma */
603             uint16_t *p_integral[2][16];
604
605             /* fref stride */
606             int     i_stride[3];
607         } pic;
608
609         /* cache */
610         struct
611         {
612             /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
613             ALIGNED_8( int8_t intra4x4_pred_mode[X264_SCAN8_SIZE] );
614
615             /* i_non_zero_count if available else 0x80 */
616             ALIGNED_4( uint8_t non_zero_count[X264_SCAN8_SIZE] );
617
618             /* -1 if unused, -2 if unavailable */
619             ALIGNED_4( int8_t ref[2][X264_SCAN8_SIZE] );
620
621             /* 0 if not available */
622             ALIGNED_16( int16_t mv[2][X264_SCAN8_SIZE][2] );
623             ALIGNED_8( int16_t mvd[2][X264_SCAN8_SIZE][2] );
624
625             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
626             ALIGNED_4( int8_t skip[X264_SCAN8_SIZE] );
627
628             ALIGNED_4( int16_t direct_mv[2][4][2] );
629             ALIGNED_4( int8_t  direct_ref[2][4] );
630             ALIGNED_4( int16_t pskip_mv[2] );
631
632             /* number of neighbors (top and left) that used 8x8 dct */
633             int     i_neighbour_transform_size;
634             int     i_neighbour_interlaced;
635
636             /* neighbor CBPs */
637             int     i_cbp_top;
638             int     i_cbp_left;
639         } cache;
640
641         /* */
642         int     i_qp;       /* current qp */
643         int     i_chroma_qp;
644         int     i_last_qp;  /* last qp */
645         int     i_last_dqp; /* last delta qp */
646         int     b_variable_qp; /* whether qp is allowed to vary per macroblock */
647         int     b_lossless;
648         int     b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
649         int     b_direct_auto_write; /* analyse direct modes, to use and/or save */
650
651         /* lambda values */
652         int     i_trellis_lambda2[2][2]; /* [luma,chroma][inter,intra] */
653         int     i_psy_rd_lambda;
654         int     i_chroma_lambda2_offset;
655
656         /* B_direct and weighted prediction */
657         int16_t dist_scale_factor[16][2];
658         int8_t bipred_weight_buf[2][32][4];
659         int8_t (*bipred_weight)[4];
660         /* maps fref1[0]'s ref indices into the current list0 */
661 #define map_col_to_list0(col) h->mb.map_col_to_list0[col+2]
662         int8_t  map_col_to_list0[18];
663         int ref_blind_dupe; /* The index of the blind reference frame duplicate. */
664     } mb;
665
666     /* rate control encoding only */
667     x264_ratecontrol_t *rc;
668
669     /* stats */
670     struct
671     {
672         /* Current frame stats */
673         struct
674         {
675             /* MV bits (MV+Ref+Block Type) */
676             int i_mv_bits;
677             /* Texture bits (DCT coefs) */
678             int i_tex_bits;
679             /* ? */
680             int i_misc_bits;
681             /* MB type counts */
682             int i_mb_count[19];
683             int i_mb_count_i;
684             int i_mb_count_p;
685             int i_mb_count_skip;
686             int i_mb_count_8x8dct[2];
687             int i_mb_count_ref[2][32];
688             int i_mb_partition[17];
689             int i_mb_cbp[6];
690             int i_mb_pred_mode[3][13];
691             /* Adaptive direct mv pred */
692             int i_direct_score[2];
693             /* Metrics */
694             int64_t i_ssd[3];
695             double f_ssim;
696         } frame;
697
698         /* Cumulated stats */
699
700         /* per slice info */
701         int     i_frame_count[5];
702         int64_t i_frame_size[5];
703         double  f_frame_qp[5];
704         int     i_consecutive_bframes[X264_BFRAME_MAX+1];
705         /* */
706         int64_t i_ssd_global[5];
707         double  f_psnr_average[5];
708         double  f_psnr_mean_y[5];
709         double  f_psnr_mean_u[5];
710         double  f_psnr_mean_v[5];
711         double  f_ssim_mean_y[5];
712         /* */
713         int64_t i_mb_count[5][19];
714         int64_t i_mb_partition[2][17];
715         int64_t i_mb_count_8x8dct[2];
716         int64_t i_mb_count_ref[2][2][32];
717         int64_t i_mb_cbp[6];
718         int64_t i_mb_pred_mode[3][13];
719         /* */
720         int     i_direct_score[2];
721         int     i_direct_frames[2];
722         /* num p-frames weighted */
723         int     i_wpred[3];
724
725     } stat;
726
727     void *scratch_buffer; /* for any temporary storage that doesn't want repeated malloc */
728
729     /* CPU functions dependents */
730     x264_predict_t      predict_16x16[4+3];
731     x264_predict_t      predict_8x8c[4+3];
732     x264_predict8x8_t   predict_8x8[9+3];
733     x264_predict_t      predict_4x4[9+3];
734     x264_predict_8x8_filter_t predict_8x8_filter;
735
736     x264_pixel_function_t pixf;
737     x264_mc_functions_t   mc;
738     x264_dct_function_t   dctf;
739     x264_zigzag_function_t zigzagf;
740     x264_quant_function_t quantf;
741     x264_deblock_function_t loopf;
742
743 #ifdef HAVE_VISUALIZE
744     struct visualize_t *visualize;
745 #endif
746     x264_lookahead_t *lookahead;
747 };
748
749 // included at the end because it needs x264_t
750 #include "macroblock.h"
751
752 #ifdef HAVE_MMX
753 #include "x86/util.h"
754 #endif
755
756 #endif
757