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