]> git.sesse.net Git - x264/blob - common/common.h
remove the distinction between itex and ptex
[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 FIX8(f) ((int)(f*(1<<8)+.5))
38
39 #define CHECKED_MALLOC( var, size )\
40 {\
41     var = x264_malloc( size );\
42     if( !var )\
43     {\
44         x264_log( h, X264_LOG_ERROR, "malloc failed\n" );\
45         goto fail;\
46     }\
47 }
48
49 #define X264_BFRAME_MAX 16
50 #define X264_THREAD_MAX 128
51 #define X264_SLICE_MAX 4
52 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
53 #define X264_PCM_COST (386*8)
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 /****************************************************************************
60  * Includes
61  ****************************************************************************/
62 #include "osdep.h"
63 #include <stdarg.h>
64 #include <stddef.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <assert.h>
68 #include "x264.h"
69 #include "bs.h"
70 #include "set.h"
71 #include "predict.h"
72 #include "pixel.h"
73 #include "mc.h"
74 #include "frame.h"
75 #include "dct.h"
76 #include "cabac.h"
77 #include "quant.h"
78
79 /****************************************************************************
80  * Generals functions
81  ****************************************************************************/
82 /* x264_malloc : will do or emulate a memalign
83  * you have to use x264_free for buffers allocated with x264_malloc */
84 void *x264_malloc( int );
85 void *x264_realloc( void *p, int i_size );
86 void  x264_free( void * );
87
88 /* x264_slurp_file: malloc space for the whole file and read it */
89 char *x264_slurp_file( const char *filename );
90
91 /* mdate: return the current date in microsecond */
92 int64_t x264_mdate( void );
93
94 /* x264_param2string: return a (malloced) string containing most of
95  * the encoding options */
96 char *x264_param2string( x264_param_t *p, int b_res );
97
98 /* log */
99 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
100
101 void x264_reduce_fraction( int *n, int *d );
102
103 static inline uint8_t x264_clip_uint8( int x )
104 {
105     return x&(~255) ? (-x)>>31 : x;
106 }
107
108 static inline int x264_clip3( int v, int i_min, int i_max )
109 {
110     return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
111 }
112
113 static inline double x264_clip3f( double v, double f_min, double f_max )
114 {
115     return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
116 }
117
118 static inline int x264_median( int a, int b, int c )
119 {
120     int t = (a-b)&((a-b)>>31);
121     a -= t;
122     b += t;
123     b -= (b-c)&((b-c)>>31);
124     b += (a-b)&((a-b)>>31);
125     return b;
126 }
127
128 static inline void x264_median_mv( int16_t *dst, int16_t *a, int16_t *b, int16_t *c )
129 {
130     dst[0] = x264_median( a[0], b[0], c[0] );
131     dst[1] = x264_median( a[1], b[1], c[1] );
132 }
133
134 static inline int x264_predictor_difference( int16_t (*mvc)[2], intptr_t i_mvc )
135 {
136     int sum = 0, i;
137     for( i = 0; i < i_mvc-1; i++ )
138     {
139         sum += abs( mvc[i][0] - mvc[i+1][0] )
140              + abs( mvc[i][1] - mvc[i+1][1] );
141     }
142     return sum;
143 }
144
145 /****************************************************************************
146  *
147  ****************************************************************************/
148 enum slice_type_e
149 {
150     SLICE_TYPE_P  = 0,
151     SLICE_TYPE_B  = 1,
152     SLICE_TYPE_I  = 2,
153     SLICE_TYPE_SP = 3,
154     SLICE_TYPE_SI = 4
155 };
156
157 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
158
159 typedef struct
160 {
161     x264_sps_t *sps;
162     x264_pps_t *pps;
163
164     int i_type;
165     int i_first_mb;
166     int i_last_mb;
167
168     int i_pps_id;
169
170     int i_frame_num;
171
172     int b_mbaff;
173     int b_field_pic;
174     int b_bottom_field;
175
176     int i_idr_pic_id;   /* -1 if nal_type != 5 */
177
178     int i_poc_lsb;
179     int i_delta_poc_bottom;
180
181     int i_delta_poc[2];
182     int i_redundant_pic_cnt;
183
184     int b_direct_spatial_mv_pred;
185
186     int b_num_ref_idx_override;
187     int i_num_ref_idx_l0_active;
188     int i_num_ref_idx_l1_active;
189
190     int b_ref_pic_list_reordering_l0;
191     int b_ref_pic_list_reordering_l1;
192     struct {
193         int idc;
194         int arg;
195     } ref_pic_list_order[2][16];
196
197     int i_cabac_init_idc;
198
199     int i_qp;
200     int i_qp_delta;
201     int b_sp_for_swidth;
202     int i_qs_delta;
203
204     /* deblocking filter */
205     int i_disable_deblocking_filter_idc;
206     int i_alpha_c0_offset;
207     int i_beta_offset;
208
209 } x264_slice_header_t;
210
211 /* From ffmpeg
212  */
213 #define X264_SCAN8_SIZE (6*8)
214 #define X264_SCAN8_0 (4+1*8)
215
216 static const int x264_scan8[16+2*4] =
217 {
218     /* Luma */
219     4+1*8, 5+1*8, 4+2*8, 5+2*8,
220     6+1*8, 7+1*8, 6+2*8, 7+2*8,
221     4+3*8, 5+3*8, 4+4*8, 5+4*8,
222     6+3*8, 7+3*8, 6+4*8, 7+4*8,
223
224     /* Cb */
225     1+1*8, 2+1*8,
226     1+2*8, 2+2*8,
227
228     /* Cr */
229     1+4*8, 2+4*8,
230     1+5*8, 2+5*8,
231 };
232 /*
233    0 1 2 3 4 5 6 7
234  0
235  1   B B   L L L L
236  2   B B   L L L L
237  3         L L L L
238  4   R R   L L L L
239  5   R R
240 */
241
242 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
243
244 struct x264_t
245 {
246     /* encoder parameters */
247     x264_param_t    param;
248
249     x264_t          *thread[X264_THREAD_MAX];
250     x264_pthread_t  thread_handle;
251     int             b_thread_active;
252     int             i_thread_phase; /* which thread to use for the next frame */
253
254     /* bitstream output */
255     struct
256     {
257         int         i_nal;
258         x264_nal_t  nal[X264_NAL_MAX];
259         int         i_bitstream;    /* size of p_bitstream */
260         uint8_t     *p_bitstream;   /* will hold data for all nal */
261         bs_t        bs;
262         int         i_frame_size;
263     } out;
264
265     /**** thread synchronization starts here ****/
266
267     /* frame number/poc */
268     int             i_frame;
269
270     int             i_frame_offset; /* decoding only */
271     int             i_frame_num;    /* decoding only */
272     int             i_poc_msb;      /* decoding only */
273     int             i_poc_lsb;      /* decoding only */
274     int             i_poc;          /* decoding only */
275
276     int             i_thread_num;   /* threads only */
277     int             i_nal_type;     /* threads only */
278     int             i_nal_ref_idc;  /* threads only */
279
280     /* We use only one SPS and one PPS */
281     x264_sps_t      sps_array[1];
282     x264_sps_t      *sps;
283     x264_pps_t      pps_array[1];
284     x264_pps_t      *pps;
285     int             i_idr_pic_id;
286
287     /* quantization matrix for decoding, [cqm][qp%6][coef_y][coef_x] */
288     int             (*dequant4_mf[4])[4][4]; /* [4][6][4][4] */
289     int             (*dequant8_mf[2])[8][8]; /* [2][6][8][8] */
290     /* quantization matrix for trellis, [cqm][qp][coef] */
291     int             (*unquant4_mf[4])[16];   /* [4][52][16] */
292     int             (*unquant8_mf[2])[64];   /* [2][52][64] */
293     /* quantization matrix for deadzone */
294     uint16_t        (*quant4_mf[4])[16];     /* [4][52][16] */
295     uint16_t        (*quant8_mf[2])[64];     /* [2][52][64] */
296     uint16_t        (*quant4_bias[4])[16];   /* [4][52][16] */
297     uint16_t        (*quant8_bias[2])[64];   /* [2][52][64] */
298
299     const uint8_t   *chroma_qp_table; /* includes both the nonlinear luma->chroma mapping and chroma_qp_offset */
300
301     DECLARE_ALIGNED_16( uint32_t nr_residual_sum[2][64] );
302     DECLARE_ALIGNED_16( uint16_t nr_offset[2][64] );
303     uint32_t        nr_count[2];
304
305     /* Slice header */
306     x264_slice_header_t sh;
307
308     /* cabac context */
309     x264_cabac_t    cabac;
310
311     struct
312     {
313         /* Frames to be encoded (whose types have been decided) */
314         x264_frame_t *current[X264_BFRAME_MAX+3];
315         /* Temporary buffer (frames types not yet decided) */
316         x264_frame_t *next[X264_BFRAME_MAX+3];
317         /* Unused frames */
318         x264_frame_t *unused[X264_BFRAME_MAX + X264_THREAD_MAX*2 + 16+4];
319         /* For adaptive B decision */
320         x264_frame_t *last_nonb;
321
322         /* frames used for reference + sentinels */
323         x264_frame_t *reference[16+2];
324
325         int i_last_idr; /* Frame number of the last IDR */
326
327         int i_input;    /* Number of input frames already accepted */
328
329         int i_max_dpb;  /* Number of frames allocated in the decoded picture buffer */
330         int i_max_ref0;
331         int i_max_ref1;
332         int i_delay;    /* Number of frames buffered for B reordering */
333         int b_have_lowres;  /* Whether 1/2 resolution luma planes are being used */
334     } frames;
335
336     /* current frame being encoded */
337     x264_frame_t    *fenc;
338
339     /* frame being reconstructed */
340     x264_frame_t    *fdec;
341
342     /* references lists */
343     int             i_ref0;
344     x264_frame_t    *fref0[16+3];     /* ref list 0 */
345     int             i_ref1;
346     x264_frame_t    *fref1[16+3];     /* ref list 1 */
347     int             b_ref_reorder[2];
348
349
350
351     /* Current MB DCT coeffs */
352     struct
353     {
354         DECLARE_ALIGNED_16( int16_t luma16x16_dc[16] );
355         DECLARE_ALIGNED_16( int16_t chroma_dc[2][4] );
356         // FIXME share memory?
357         DECLARE_ALIGNED_16( int16_t luma8x8[4][64] );
358         DECLARE_ALIGNED_16( int16_t luma4x4[16+8][16] );
359     } dct;
360
361     /* MB table and cache for current frame/mb */
362     struct
363     {
364         int     i_mb_count;                 /* number of mbs in a frame */
365
366         /* Strides */
367         int     i_mb_stride;
368         int     i_b8_stride;
369         int     i_b4_stride;
370
371         /* Current index */
372         int     i_mb_x;
373         int     i_mb_y;
374         int     i_mb_xy;
375         int     i_b8_xy;
376         int     i_b4_xy;
377
378         /* Search parameters */
379         int     i_me_method;
380         int     i_subpel_refine;
381         int     b_chroma_me;
382         int     b_trellis;
383         int     b_noise_reduction;
384
385         int     b_interlaced;
386
387         /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
388         int     mv_min[2];
389         int     mv_max[2];
390         /* Subpel MV range for motion search.
391          * same mv_min/max but includes levels' i_mv_range. */
392         int     mv_min_spel[2];
393         int     mv_max_spel[2];
394         /* Fullpel MV range for motion search */
395         int     mv_min_fpel[2];
396         int     mv_max_fpel[2];
397
398         /* neighboring MBs */
399         unsigned int i_neighbour;
400         unsigned int i_neighbour8[4];       /* neighbours of each 8x8 or 4x4 block that are available */
401         unsigned int i_neighbour4[16];      /* at the time the block is coded */
402         int     i_mb_type_top;
403         int     i_mb_type_left;
404         int     i_mb_type_topleft;
405         int     i_mb_type_topright;
406         int     i_mb_prev_xy;
407         int     i_mb_top_xy;
408
409         /**** thread synchronization ends here ****/
410         /* subsequent variables are either thread-local or constant,
411          * and won't be copied from one thread to another */
412
413         /* mb table */
414         int8_t  *type;                      /* mb type */
415         int8_t  *qp;                        /* mb qp */
416         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
417         int8_t  (*intra4x4_pred_mode)[8];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
418                                             /* actually has only 7 entries; set to 8 for write-combining optimizations */
419         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
420         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
421         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
422         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
423         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
424         int16_t (*mvr[2][32])[2];           /* 16x16 mv for each possible ref */
425         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
426         int8_t  *mb_transform_size;         /* transform_size_8x8_flag of each mb */
427         uint8_t *intra_border_backup[2][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
428         uint8_t (*nnz_backup)[16];          /* when using cavlc + 8x8dct, the deblocker uses a modified nnz */
429
430         /* current value */
431         int     i_type;
432         int     i_partition;
433         int     i_sub_partition[4];
434         int     b_transform_8x8;
435
436         int     i_cbp_luma;
437         int     i_cbp_chroma;
438
439         int     i_intra16x16_pred_mode;
440         int     i_chroma_pred_mode;
441
442         /* skip flags for i4x4 and i8x8
443          * 0 = encode as normal.
444          * 1 (non-RD only) = the DCT is still in h->dct, restore fdec and skip reconstruction.
445          * 2 (RD only) = the DCT has since been overwritten by RD; restore that too. */
446         int i_skip_intra;
447         /* skip flag for motion compensation */
448         /* if we've already done MC, we don't need to do it again */
449         int b_skip_mc;
450
451         struct
452         {
453             /* space for p_fenc and p_fdec */
454 #define FENC_STRIDE 16
455 #define FDEC_STRIDE 32
456             DECLARE_ALIGNED_16( uint8_t fenc_buf[24*FENC_STRIDE] );
457             DECLARE_ALIGNED_16( uint8_t fdec_buf[27*FDEC_STRIDE] );
458
459             /* i4x4 and i8x8 backup data, for skipping the encode stage when possible */
460             DECLARE_ALIGNED_16( uint8_t i4x4_fdec_buf[16*16] );
461             DECLARE_ALIGNED_16( uint8_t i8x8_fdec_buf[16*16] );
462             DECLARE_ALIGNED_16( int16_t i8x8_dct_buf[3][64] );
463             DECLARE_ALIGNED_16( int16_t i4x4_dct_buf[15][16] );
464
465             /* pointer over mb of the frame to be compressed */
466             uint8_t *p_fenc[3];
467
468             /* pointer over mb of the frame to be reconstructed  */
469             uint8_t *p_fdec[3];
470
471             /* pointer over mb of the references */
472             int i_fref[2];
473             uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
474             uint16_t *p_integral[2][16];
475
476             /* fref stride */
477             int     i_stride[3];
478         } pic;
479
480         /* cache */
481         struct
482         {
483             /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
484             int8_t  intra4x4_pred_mode[X264_SCAN8_SIZE];
485
486             /* i_non_zero_count if available else 0x80 */
487             uint8_t non_zero_count[X264_SCAN8_SIZE];
488
489             /* -1 if unused, -2 if unavailable */
490             DECLARE_ALIGNED_4( int8_t ref[2][X264_SCAN8_SIZE] );
491
492             /* 0 if not available */
493             DECLARE_ALIGNED_16( int16_t mv[2][X264_SCAN8_SIZE][2] );
494             DECLARE_ALIGNED_8( int16_t mvd[2][X264_SCAN8_SIZE][2] );
495
496             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
497             DECLARE_ALIGNED_4( int8_t skip[X264_SCAN8_SIZE] );
498
499             DECLARE_ALIGNED_16( int16_t direct_mv[2][X264_SCAN8_SIZE][2] );
500             DECLARE_ALIGNED_4( int8_t  direct_ref[2][X264_SCAN8_SIZE] );
501             DECLARE_ALIGNED_4( int16_t pskip_mv[2] );
502
503             /* number of neighbors (top and left) that used 8x8 dct */
504             int     i_neighbour_transform_size;
505             int     i_neighbour_interlaced;
506         } cache;
507
508         /* */
509         int     i_qp;       /* current qp */
510         int     i_chroma_qp;
511         int     i_last_qp;  /* last qp */
512         int     i_last_dqp; /* last delta qp */
513         int     b_variable_qp; /* whether qp is allowed to vary per macroblock */
514         int     b_lossless;
515         int     b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
516         int     b_direct_auto_write; /* analyse direct modes, to use and/or save */
517
518         /* B_direct and weighted prediction */
519         int16_t dist_scale_factor[16][2];
520         int16_t bipred_weight[32][4];
521         /* maps fref1[0]'s ref indices into the current list0 */
522         int8_t  map_col_to_list0_buf[2]; // for negative indices
523         int8_t  map_col_to_list0[16];
524     } mb;
525
526     /* rate control encoding only */
527     x264_ratecontrol_t *rc;
528
529     /* stats */
530     struct
531     {
532         /* Current frame stats */
533         struct
534         {
535             /* MV bits (MV+Ref+Block Type) */
536             int i_mv_bits;
537             /* Texture bits (DCT coefs) */
538             int i_tex_bits;
539             /* ? */
540             int i_misc_bits;
541             /* MB type counts */
542             int i_mb_count[19];
543             int i_mb_count_i;
544             int i_mb_count_p;
545             int i_mb_count_skip;
546             int i_mb_count_8x8dct[2];
547             int i_mb_count_ref[2][32];
548             int i_mb_partition[17];
549             /* Estimated (SATD) cost as Intra/Predicted frame */
550             /* XXX: both omit the cost of MBs coded as P_SKIP */
551             int i_intra_cost;
552             int i_inter_cost;
553             int i_mbs_analysed;
554             /* Adaptive direct mv pred */
555             int i_direct_score[2];
556             /* Metrics */
557             int64_t i_ssd[3];
558             double f_ssim;
559         } frame;
560
561         /* Cumulated stats */
562
563         /* per slice info */
564         int     i_slice_count[5];
565         int64_t i_slice_size[5];
566         double  f_slice_qp[5];
567         int     i_consecutive_bframes[X264_BFRAME_MAX+1];
568         /* */
569         int64_t i_ssd_global[5];
570         double  f_psnr_average[5];
571         double  f_psnr_mean_y[5];
572         double  f_psnr_mean_u[5];
573         double  f_psnr_mean_v[5];
574         double  f_ssim_mean_y[5];
575         /* */
576         int64_t i_mb_count[5][19];
577         int64_t i_mb_partition[2][17];
578         int64_t i_mb_count_8x8dct[2];
579         int64_t i_mb_count_ref[2][2][32];
580         /* */
581         int     i_direct_score[2];
582         int     i_direct_frames[2];
583
584     } stat;
585
586     /* CPU functions dependents */
587     x264_predict_t      predict_16x16[4+3];
588     x264_predict_t      predict_8x8c[4+3];
589     x264_predict8x8_t   predict_8x8[9+3];
590     x264_predict_t      predict_4x4[9+3];
591
592     x264_pixel_function_t pixf;
593     x264_mc_functions_t   mc;
594     x264_dct_function_t   dctf;
595     x264_zigzag_function_t zigzagf;
596     x264_quant_function_t quantf;
597     x264_deblock_function_t loopf;
598
599 #if VISUALIZE
600     struct visualize_t *visualize;
601 #endif
602 };
603
604 // included at the end because it needs x264_t
605 #include "macroblock.h"
606
607 #ifdef HAVE_MMX
608 #include "x86/util.h"
609 #endif
610
611 #endif
612