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