]> git.sesse.net Git - x264/blob - common/common.h
cosmetics: merge some duplicate tables
[x264] / common / common.h
1 /*****************************************************************************
2  * common.h: h264 encoder
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: common.h,v 1.1 2004/06/03 19:27:06 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifndef _COMMON_H
25 #define _COMMON_H 1
26
27 #ifdef HAVE_STDINT_H
28 #include <stdint.h>
29 #else
30 #include <inttypes.h>
31 #endif
32 #include <stdarg.h>
33
34 #ifdef _MSC_VER
35 #define snprintf _snprintf
36 #define X264_VERSION "" // no configure script for msvc
37 #endif
38
39 #include "x264.h"
40 #include "bs.h"
41 #include "set.h"
42 #include "predict.h"
43 #include "pixel.h"
44 #include "mc.h"
45 #include "frame.h"
46 #include "dct.h"
47 #include "cabac.h"
48 #include "csp.h"
49 #include "quant.h"
50
51 /****************************************************************************
52  * Macros
53  ****************************************************************************/
54 #define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
55 #define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
56 #define X264_ABS(a)   ( (a)< 0 ? -(a) : (a) )
57 #define X264_MIN3(a,b,c) X264_MIN((a),X264_MIN((b),(c)))
58 #define X264_MAX3(a,b,c) X264_MAX((a),X264_MAX((b),(c)))
59 #define X264_MIN4(a,b,c,d) X264_MIN((a),X264_MIN3((b),(c),(d)))
60 #define X264_MAX4(a,b,c,d) X264_MAX((a),X264_MAX3((b),(c),(d)))
61
62 /****************************************************************************
63  * Generals functions
64  ****************************************************************************/
65 /* x264_malloc : will do or emulate a memalign
66  * XXX you HAVE TO use x264_free for buffer allocated
67  * with x264_malloc
68  */
69 void *x264_malloc( int );
70 void *x264_realloc( void *p, int i_size );
71 void  x264_free( void * );
72
73 /* x264_slurp_file: malloc space for the whole file and read it */
74 char *x264_slurp_file( const char *filename );
75
76 /* mdate: return the current date in microsecond */
77 int64_t x264_mdate( void );
78
79 /* log */
80 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
81
82 static inline int x264_clip3( int v, int i_min, int i_max )
83 {
84     return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
85 }
86
87 static inline float x264_clip3f( float v, float f_min, float f_max )
88 {
89     return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
90 }
91
92 static inline int x264_median( int a, int b, int c )
93 {
94     int min = a, max =a;
95     if( b < min )
96         min = b;
97     else
98         max = b;    /* no need to do 'b > max' (more consuming than always doing affectation) */
99
100     if( c < min )
101         min = c;
102     else if( c > max )
103         max = c;
104
105     return a + b + c - min - max;
106 }
107
108
109 /****************************************************************************
110  *
111  ****************************************************************************/
112 enum slice_type_e
113 {
114     SLICE_TYPE_P  = 0,
115     SLICE_TYPE_B  = 1,
116     SLICE_TYPE_I  = 2,
117     SLICE_TYPE_SP = 3,
118     SLICE_TYPE_SI = 4
119 };
120
121 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
122
123 typedef struct
124 {
125     x264_sps_t *sps;
126     x264_pps_t *pps;
127
128     int i_type;
129     int i_first_mb;
130     int i_last_mb;
131
132     int i_pps_id;
133
134     int i_frame_num;
135
136     int b_field_pic;
137     int b_bottom_field;
138
139     int i_idr_pic_id;   /* -1 if nal_type != 5 */
140
141     int i_poc_lsb;
142     int i_delta_poc_bottom;
143
144     int i_delta_poc[2];
145     int i_redundant_pic_cnt;
146
147     int b_direct_spatial_mv_pred;
148
149     int b_num_ref_idx_override;
150     int i_num_ref_idx_l0_active;
151     int i_num_ref_idx_l1_active;
152
153     int b_ref_pic_list_reordering_l0;
154     int b_ref_pic_list_reordering_l1;
155     struct {
156         int idc;
157         int arg;
158     } ref_pic_list_order[2][16];
159
160     int i_cabac_init_idc;
161
162     int i_qp;
163     int i_qp_delta;
164     int b_sp_for_swidth;
165     int i_qs_delta;
166
167     /* deblocking filter */
168     int i_disable_deblocking_filter_idc;
169     int i_alpha_c0_offset;
170     int i_beta_offset;
171
172 } x264_slice_header_t;
173
174 /* From ffmpeg
175  */
176 #define X264_SCAN8_SIZE (6*8)
177 #define X264_SCAN8_0 (4+1*8)
178
179 static const int x264_scan8[16+2*4] =
180 {
181     /* Luma */
182     4+1*8, 5+1*8, 4+2*8, 5+2*8,
183     6+1*8, 7+1*8, 6+2*8, 7+2*8,
184     4+3*8, 5+3*8, 4+4*8, 5+4*8,
185     6+3*8, 7+3*8, 6+4*8, 7+4*8,
186
187     /* Cb */
188     1+1*8, 2+1*8,
189     1+2*8, 2+2*8,
190
191     /* Cr */
192     1+4*8, 2+4*8,
193     1+5*8, 2+5*8,
194 };
195 /*
196    0 1 2 3 4 5 6 7
197  0
198  1   B B   L L L L
199  2   B B   L L L L
200  3         L L L L
201  4   R R   L L L L
202  5   R R
203 */
204
205 #define X264_BFRAME_MAX 16
206 #define X264_SLICE_MAX 4
207 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
208
209 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
210 typedef struct x264_vlc_table_t     x264_vlc_table_t;
211
212 struct x264_t
213 {
214     /* encoder parameters */
215     x264_param_t    param;
216
217     x264_t *thread[X264_SLICE_MAX];
218
219     /* bitstream output */
220     struct
221     {
222         int         i_nal;
223         x264_nal_t  nal[X264_NAL_MAX];
224         int         i_bitstream;    /* size of p_bitstream */
225         uint8_t     *p_bitstream;   /* will hold data for all nal */
226         bs_t        bs;
227     } out;
228
229     /* frame number/poc */
230     int             i_frame;
231
232     int             i_frame_offset; /* decoding only */
233     int             i_frame_num;    /* decoding only */
234     int             i_poc_msb;      /* decoding only */
235     int             i_poc_lsb;      /* decoding only */
236     int             i_poc;          /* decoding only */
237
238     int             i_thread_num;   /* threads only */
239     int             i_nal_type;     /* threads only */
240     int             i_nal_ref_idc;  /* threads only */
241
242     /* We use only one SPS and one PPS */
243     x264_sps_t      sps_array[1];
244     x264_sps_t      *sps;
245     x264_pps_t      pps_array[1];
246     x264_pps_t      *pps;
247     int             i_idr_pic_id;
248
249     int             dequant4_mf[4][6][4][4];
250     int             dequant8_mf[2][6][8][8];
251     int             quant4_mf[4][6][4][4];
252     int             quant8_mf[2][6][8][8];
253
254     /* Slice header */
255     x264_slice_header_t sh;
256
257     /* cabac context */
258     x264_cabac_t    cabac;
259
260     struct
261     {
262         /* Frames to be encoded (whose types have been decided) */
263         x264_frame_t *current[X264_BFRAME_MAX+3];
264         /* Temporary buffer (frames types not yet decided) */
265         x264_frame_t *next[X264_BFRAME_MAX+3];
266         /* Unused frames */
267         x264_frame_t *unused[X264_BFRAME_MAX+3];
268         /* For adaptive B decision */
269         x264_frame_t *last_nonb;
270
271         /* frames used for reference +1 for decoding + sentinels */
272         x264_frame_t *reference[16+2+1+2];
273
274         int i_last_idr; /* Frame number of the last IDR */
275
276         int i_input;    /* Number of input frames already accepted */
277
278         int i_max_dpb;  /* Number of frames allocated in the decoded picture buffer */
279         int i_max_ref0;
280         int i_max_ref1;
281         int i_delay;    /* Number of frames buffered for B reordering */
282         int b_have_lowres;  /* Whether 1/2 resolution luma planes are being used */
283     } frames;
284
285     /* current frame being encoded */
286     x264_frame_t    *fenc;
287
288     /* frame being reconstructed */
289     x264_frame_t    *fdec;
290
291     /* references lists */
292     int             i_ref0;
293     x264_frame_t    *fref0[16+3];     /* ref list 0 */
294     int             i_ref1;
295     x264_frame_t    *fref1[16+3];     /* ref list 1 */
296     int             b_ref_reorder[2];
297
298
299
300     /* Current MB DCT coeffs */
301     struct
302     {
303         DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
304         DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
305         // FIXME merge with union
306         DECLARE_ALIGNED( int, luma8x8[4][64], 16 );
307         union
308         {
309             DECLARE_ALIGNED( int, residual_ac[15], 16 );
310             DECLARE_ALIGNED( int, luma4x4[16], 16 );
311         } block[16+8];
312     } dct;
313
314     /* MB table and cache for current frame/mb */
315     struct
316     {
317         int     i_mb_count;                 /* number of mbs in a frame */
318
319         /* Strides */
320         int     i_mb_stride;
321         int     i_b8_stride;
322         int     i_b4_stride;
323
324         /* Current index */
325         int     i_mb_x;
326         int     i_mb_y;
327         int     i_mb_xy;
328         int     i_b8_xy;
329         int     i_b4_xy;
330         
331         /* Search parameters */
332         int     i_me_method;
333         int     i_subpel_refine;
334         int     b_chroma_me;
335         /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
336         int     mv_min[2];
337         int     mv_max[2];
338         /* Fullpel MV range for motion search */
339         int     mv_min_fpel[2];
340         int     mv_max_fpel[2];
341
342         /* neighboring MBs */
343         unsigned int i_neighbour;
344         unsigned int i_neighbour8[4];       /* neighbours of each 8x8 or 4x4 block that are available */
345         unsigned int i_neighbour4[16];      /* at the time the block is coded */
346         int     i_mb_type_top; 
347         int     i_mb_type_left; 
348         int     i_mb_type_topleft; 
349         int     i_mb_type_topright; 
350
351         /* mb table */
352         int8_t  *type;                      /* mb type */
353         int8_t  *qp;                        /* mb qp */
354         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
355         int8_t  (*intra4x4_pred_mode)[7];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
356         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
357         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
358         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
359         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
360         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
361         int16_t (*mvr[2][16])[2];           /* 16x16 mv for each possible ref */
362         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
363         int8_t  *mb_transform_size;         /* transform_size_8x8_flag of each mb */
364
365         /* current value */
366         int     i_type;
367         int     i_partition;
368         int     i_sub_partition[4];
369         int     b_transform_8x8;
370
371         int     i_cbp_luma;
372         int     i_cbp_chroma;
373
374         int     i_intra16x16_pred_mode;
375         int     i_chroma_pred_mode;
376
377         struct
378         {
379             /* pointer over mb of the frame to be compressed */
380             uint8_t *p_fenc[3];
381
382             /* pointer over mb of the frame to be reconstrucated  */
383             uint8_t *p_fdec[3];
384
385             /* pointer over mb of the references */
386             uint8_t *p_fref[2][16][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
387
388             /* common stride */
389             int     i_stride[3];
390         } pic;
391
392         /* cache */
393         struct
394         {
395             /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
396             int     intra4x4_pred_mode[X264_SCAN8_SIZE];
397
398             /* i_non_zero_count if availble else 0x80 */
399             int     non_zero_count[X264_SCAN8_SIZE];
400
401             /* -1 if unused, -2 if unavaible */
402             int8_t  ref[2][X264_SCAN8_SIZE];
403
404             /* 0 if non avaible */
405             int16_t mv[2][X264_SCAN8_SIZE][2];
406             int16_t mvd[2][X264_SCAN8_SIZE][2];
407
408             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
409             int8_t  skip[X264_SCAN8_SIZE];
410
411             int16_t direct_mv[2][X264_SCAN8_SIZE][2];
412             int8_t  direct_ref[2][X264_SCAN8_SIZE];
413
414             /* number of neighbors (top and left) that used 8x8 dct */
415             int     i_neighbour_transform_size;
416             int     b_transform_8x8_allowed;
417         } cache;
418
419         /* */
420         int     i_qp;       /* current qp */
421         int     i_last_qp;  /* last qp */
422         int     i_last_dqp; /* last delta qp */
423         int     b_variable_qp; /* whether qp is allowed to vary per macroblock */
424         int     b_lossless;
425
426         /* B_direct and weighted prediction */
427         int     dist_scale_factor[16][16];
428         int     bipred_weight[16][16];
429         /* maps fref1[0]'s ref indices into the current list0 */
430         int     map_col_to_list0_buf[2]; // for negative indices
431         int     map_col_to_list0[16];
432     } mb;
433
434     /* rate control encoding only */
435     x264_ratecontrol_t *rc;
436
437     int i_last_inter_size;
438     int i_last_intra_size;
439     int i_last_intra_qp;
440
441     /* stats */
442     struct
443     {
444         /* Current frame stats */
445         struct
446         {
447             /* Headers bits (MV+Ref+MB Block Type */
448             int i_hdr_bits;
449             /* Texture bits (Intra/Predicted) */
450             int i_itex_bits;
451             int i_ptex_bits;
452             /* ? */
453             int i_misc_bits;
454             /* MB type counts */
455             int i_mb_count[19];
456             int i_mb_count_i;
457             int i_mb_count_p;
458             int i_mb_count_skip;
459             int i_mb_count_8x8dct[2];
460             int i_mb_count_size[7];
461             int i_mb_count_ref[16];
462             /* Estimated (SATD) cost as Intra/Predicted frame */
463             /* XXX: both omit the cost of MBs coded as P_SKIP */
464             int i_intra_cost;
465             int i_inter_cost;
466         } frame;
467
468         /* Cummulated stats */
469
470         /* per slice info */
471         int   i_slice_count[5];
472         int64_t i_slice_size[5];
473         int     i_slice_qp[5];
474         /* */
475         int64_t i_sqe_global[5];
476         float   f_psnr_average[5];
477         float   f_psnr_mean_y[5];
478         float   f_psnr_mean_u[5];
479         float   f_psnr_mean_v[5];
480         /* */
481         int64_t i_mb_count[5][19];
482         int64_t i_mb_count_8x8dct[2];
483         int64_t i_mb_count_size[2][7];
484         int64_t i_mb_count_ref[2][16];
485
486     } stat;
487
488     /* CPU functions dependants */
489     x264_predict_t      predict_16x16[4+3];
490     x264_predict_t      predict_8x8c[4+3];
491     x264_predict8x8_t   predict_8x8[9+3];
492     x264_predict_t      predict_4x4[9+3];
493
494     x264_pixel_function_t pixf;
495     x264_mc_functions_t   mc;
496     x264_dct_function_t   dctf;
497     x264_csp_function_t   csp;
498     x264_quant_function_t quantf;
499
500     /* vlc table for decoding purpose only */
501     x264_vlc_table_t *x264_coeff_token_lookup[5];
502     x264_vlc_table_t *x264_level_prefix_lookup;
503     x264_vlc_table_t *x264_total_zeros_lookup[15];
504     x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
505     x264_vlc_table_t *x264_run_before_lookup[7];
506
507 #if VISUALIZE
508     struct visualize_t *visualize;
509 #endif
510 };
511
512 // included at the end because it needs x264_t
513 #include "macroblock.h"
514
515 #endif
516