]> git.sesse.net Git - x264/blob - common/common.h
c60fdfdd5e4c2279519af63dd0db47870784263e
[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 #include "../x264.h"
35 #include "bs.h"
36 #include "set.h"
37 #include "predict.h"
38 #include "pixel.h"
39 #include "mc.h"
40 #include "frame.h"
41 #include "dct.h"
42 #include "cabac.h"
43 #include "csp.h"
44
45 /****************************************************************************
46  * Macros
47  ****************************************************************************/
48 #define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
49 #define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
50 #define X264_ABS(a)   ( (a)< 0 ? -(a) : (a) )
51
52 /****************************************************************************
53  * Generals functions
54  ****************************************************************************/
55 /* x264_malloc : will do or emulate a memalign
56  * XXX you HAVE TO use x264_free for buffer allocated
57  * with x264_malloc
58  */
59 void *x264_malloc( int );
60 void *x264_realloc( void *p, int i_size );
61 void  x264_free( void * );
62
63 /* mdate: return the current date in microsecond */
64 int64_t x264_mdate( void );
65
66 /* log */
67 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
68
69 static inline int x264_clip3( int v, int i_min, int i_max )
70 {
71     return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
72 }
73
74 static inline float x264_clip3f( float v, float f_min, float f_max )
75 {
76     return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
77 }
78
79 /****************************************************************************
80  *
81  ****************************************************************************/
82 enum slice_type_e
83 {
84     SLICE_TYPE_P  = 0,
85     SLICE_TYPE_B  = 1,
86     SLICE_TYPE_I  = 2,
87     SLICE_TYPE_SP = 3,
88     SLICE_TYPE_SI = 4
89 };
90
91 typedef struct
92 {
93     x264_sps_t *sps;
94     x264_pps_t *pps;
95
96     int i_type;
97     int i_first_mb;
98
99     int i_pps_id;
100
101     int i_frame_num;
102
103     int b_field_pic;
104     int b_bottom_field;
105
106     int i_idr_pic_id;   /* -1 if nal_type != 5 */
107
108     int i_poc_lsb;
109     int i_delta_poc_bottom;
110
111     int i_delta_poc[2];
112     int i_redundant_pic_cnt;
113
114     int b_direct_spatial_mv_pred;
115
116     int b_num_ref_idx_override;
117     int i_num_ref_idx_l0_active;
118     int i_num_ref_idx_l1_active;
119
120     int i_cabac_init_idc;
121
122     int i_qp_delta;
123     int b_sp_for_swidth;
124     int i_qs_delta;
125
126     /* deblocking filter */
127     int i_disable_deblocking_filter_idc;
128     int i_alpha_c0_offset;
129     int i_beta_offset;
130
131 } x264_slice_header_t;
132
133 /* From ffmpeg
134  */
135 #define X264_SCAN8_SIZE (6*8)
136 #define X264_SCAN8_0 (4+1*8)
137
138 static const int x264_scan8[16+2*4] =
139 {
140     /* Luma */
141     4+1*8, 5+1*8, 4+2*8, 5+2*8,
142     6+1*8, 7+1*8, 6+2*8, 7+2*8,
143     4+3*8, 5+3*8, 4+4*8, 5+4*8,
144     6+3*8, 7+3*8, 6+4*8, 7+4*8,
145
146     /* Cb */
147     1+1*8, 2+1*8,
148     1+2*8, 2+2*8,
149
150     /* Cr */
151     1+4*8, 2+4*8,
152     1+5*8, 2+5*8,
153 };
154 /*
155    0 1 2 3 4 5 6 7
156  0
157  1   B B   L L L L
158  2   B B   L L L L
159  3         L L L L
160  4   R R   L L L L
161  5   R R
162 */
163
164 #define X264_BFRAME_MAX 16
165
166 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
167 typedef struct x264_vlc_table_t     x264_vlc_table_t;
168
169 struct x264_t
170 {
171     /* encoder parameters */
172     x264_param_t    param;
173
174     /* bitstream output */
175     struct
176     {
177         int         i_nal;
178         x264_nal_t  nal[3];         /* for now 3 is enought */
179         int         i_bitstream;    /* size of p_bitstream */
180         uint8_t     *p_bitstream;   /* will hold data for all nal */
181         bs_t        bs;
182     } out;
183
184     /* frame number/poc */
185     int             i_frame;
186     int             i_poc;
187
188     int             i_frame_offset; /* decoding only */
189     int             i_frame_num;    /* decoding only */
190     int             i_poc_msb;      /* decoding only */
191     int             i_poc_lsb;      /* decoding only */
192
193     /* We use only one SPS and one PPS */
194     x264_sps_t      sps_array[32];
195     x264_sps_t      *sps;
196     x264_pps_t      pps_array[256];
197     x264_pps_t      *pps;
198     int             i_idr_pic_id;
199
200     /* Slice header */
201     x264_slice_header_t sh;
202
203     /* cabac context */
204     x264_cabac_t    cabac;
205
206     struct
207     {
208         /* Frames to be encoded */
209         x264_frame_t *current[X264_BFRAME_MAX+1];
210         /* Temporary buffer (eg B frames pending until we reach the I/P) */
211         x264_frame_t *next[X264_BFRAME_MAX+1];
212         /* Unused frames */
213         x264_frame_t *unused[X264_BFRAME_MAX+1];
214
215         /* frames used for reference +1 for decoding */
216         x264_frame_t *reference[16+1];
217
218         int i_last_idr; /* How many I non IDR frames from last IDR */
219         int i_last_i;   /* How many P/B frames from last I */
220
221         int i_input;    /* Number of input frames already accepted */
222     } frames;
223
224     /* current frame being encoded */
225     x264_frame_t    *fenc;
226
227     /* frame being reconstructed */
228     x264_frame_t    *fdec;
229
230     /* references lists */
231     int             i_ref0;
232     x264_frame_t    *fref0[16];       /* ref list 0 */
233     int             i_ref1;
234     x264_frame_t    *fref1[16];       /* ref list 1 */
235
236
237
238     /* Current MB DCT coeffs */
239     struct
240     {
241         DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
242         DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
243         union
244         {
245             DECLARE_ALIGNED( int, residual_ac[15], 16 );
246             DECLARE_ALIGNED( int, luma4x4[16], 16 );
247         } block[16+8];
248     } dct;
249
250     /* MB table and cache for current frame/mb */
251     struct
252     {
253         int     i_mb_count;                 /* number of mbs in a frame */
254
255         /* Strides */
256         int     i_mb_stride;
257
258         /* Current index */
259         int     i_mb_x;
260         int     i_mb_y;
261         int     i_mb_xy;
262
263         unsigned int i_neighbour;
264
265         /* mb table */
266         int8_t  *type;                      /* mb type */
267         int8_t  *qp;                        /* mb qp */
268         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
269         int8_t  (*intra4x4_pred_mode)[7];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
270         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
271         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
272         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
273         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
274         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
275         int16_t (*mvr[2][16])[2];           /* 16x16 mv for each possible ref */
276         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
277
278         /* for B_SKIP and B_DIRECT motion prediction */
279         struct
280         {
281             int16_t (*mv)[2];               /* keep only L0 */
282             int8_t   *ref;
283         } list1ref0;
284
285         /* current value */
286         int     i_type;
287         int     i_partition;
288         int     i_sub_partition[4];
289
290         int     i_cbp_luma;
291         int     i_cbp_chroma;
292
293         int     i_intra16x16_pred_mode;
294         int     i_chroma_pred_mode;
295
296         struct
297         {
298             /* pointer over mb of the frame to be compressed */
299             uint8_t *p_fenc[3];
300
301             /* pointer over mb of the frame to be reconstrucated  */
302             uint8_t *p_fdec[3];
303
304             /* pointer over mb of the references */
305             uint8_t *p_fref[2][16][3];
306
307             /* common stride */
308             int     i_stride[3];
309         } pic;
310
311         /* cache */
312         struct
313         {
314             /* real intra4x4_pred_mode if I_4X4, I_PRED_4x4_DC if mb available, -1 if not */
315             int     intra4x4_pred_mode[X264_SCAN8_SIZE];
316
317             /* i_non_zero_count if availble else 0x80 */
318             int     non_zero_count[X264_SCAN8_SIZE];
319
320             /* -1 if unused, -2 if unavaible */
321             int8_t  ref[2][X264_SCAN8_SIZE];
322
323             /* 0 if non avaible */
324             int16_t mv[2][X264_SCAN8_SIZE][2];
325             int16_t mvd[2][X264_SCAN8_SIZE][2];
326
327             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
328             int8_t  skip[X264_SCAN8_SIZE];
329
330             int16_t direct_mv[2][X264_SCAN8_SIZE][2];
331             int8_t  direct_ref[2][X264_SCAN8_SIZE];
332         } cache;
333
334         /* */
335         int     i_last_qp;  /* last qp */
336         int     i_last_dqp; /* last delta qp */
337
338     } mb;
339
340     /* rate control encoding only */
341     x264_ratecontrol_t *rc;
342
343     int i_last_inter_size;
344     int i_last_intra_size;
345     int i_last_intra_qp;
346
347     /* stats */
348     struct
349     {
350         /* Current frame stats */
351         struct
352         {
353             /* Headers bits (MV+Ref+MB Block Type */
354             int i_hdr_bits;
355             /* Texture bits (Intra/Predicted) */
356             int i_itex_bits;
357             int i_ptex_bits;
358             /* ? */
359             int i_misc_bits;
360             /* MB type counts */
361             int i_mb_count[18];
362             int i_mb_count_p;
363             int i_mb_count_skip;
364             /* Estimated (SATD) cost as Intra/Predicted frame */
365             /* XXX: both omit the cost of MBs coded as P_SKIP */
366             int i_intra_cost;
367             int i_inter_cost;
368         } frame;
369
370         /* Cummulated stats */
371
372         /* per slice info */
373         int   i_slice_count[5];
374         int64_t i_slice_size[5];
375         int     i_slice_qp[5];
376         /* */
377         int64_t i_sqe_global[5];
378         float   f_psnr_average[5];
379         float   f_psnr_mean_y[5];
380         float   f_psnr_mean_u[5];
381         float   f_psnr_mean_v[5];
382         /* */
383         int64_t i_mb_count[5][18];
384
385     } stat;
386
387     /* CPU functions dependants */
388     x264_predict_t      predict_16x16[4+3];
389     x264_predict_t      predict_8x8[4+3];
390     x264_predict_t      predict_4x4[9+3];
391
392     x264_pixel_function_t pixf;
393     x264_mc_function_t    mc[2];
394     x264_dct_function_t   dctf;
395     x264_csp_function_t   csp;
396
397     /* vlc table for decoding purpose only */
398     x264_vlc_table_t *x264_coeff_token_lookup[5];
399     x264_vlc_table_t *x264_level_prefix_lookup;
400     x264_vlc_table_t *x264_total_zeros_lookup[15];
401     x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
402     x264_vlc_table_t *x264_run_before_lookup[7];
403 };
404
405 #endif
406