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