]> git.sesse.net Git - x264/blob - common/common.h
lossless mode enabled at qp=0
[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     int i_last_mb;
125
126     int i_pps_id;
127
128     int i_frame_num;
129
130     int b_field_pic;
131     int b_bottom_field;
132
133     int i_idr_pic_id;   /* -1 if nal_type != 5 */
134
135     int i_poc_lsb;
136     int i_delta_poc_bottom;
137
138     int i_delta_poc[2];
139     int i_redundant_pic_cnt;
140
141     int b_direct_spatial_mv_pred;
142
143     int b_num_ref_idx_override;
144     int i_num_ref_idx_l0_active;
145     int i_num_ref_idx_l1_active;
146
147     int b_ref_pic_list_reordering_l0;
148     int b_ref_pic_list_reordering_l1;
149     struct {
150         int idc;
151         int arg;
152     } ref_pic_list_order[2][16];
153
154     int i_cabac_init_idc;
155
156     int i_qp_delta;
157     int b_sp_for_swidth;
158     int i_qs_delta;
159
160     /* deblocking filter */
161     int i_disable_deblocking_filter_idc;
162     int i_alpha_c0_offset;
163     int i_beta_offset;
164
165 } x264_slice_header_t;
166
167 /* From ffmpeg
168  */
169 #define X264_SCAN8_SIZE (6*8)
170 #define X264_SCAN8_0 (4+1*8)
171
172 static const int x264_scan8[16+2*4] =
173 {
174     /* Luma */
175     4+1*8, 5+1*8, 4+2*8, 5+2*8,
176     6+1*8, 7+1*8, 6+2*8, 7+2*8,
177     4+3*8, 5+3*8, 4+4*8, 5+4*8,
178     6+3*8, 7+3*8, 6+4*8, 7+4*8,
179
180     /* Cb */
181     1+1*8, 2+1*8,
182     1+2*8, 2+2*8,
183
184     /* Cr */
185     1+4*8, 2+4*8,
186     1+5*8, 2+5*8,
187 };
188 /*
189    0 1 2 3 4 5 6 7
190  0
191  1   B B   L L L L
192  2   B B   L L L L
193  3         L L L L
194  4   R R   L L L L
195  5   R R
196 */
197
198 #define X264_BFRAME_MAX 16
199 #define X264_SLICE_MAX 4
200 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
201
202 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
203 typedef struct x264_vlc_table_t     x264_vlc_table_t;
204
205 struct x264_t
206 {
207     /* encoder parameters */
208     x264_param_t    param;
209
210     x264_t *thread[X264_SLICE_MAX];
211
212     /* bitstream output */
213     struct
214     {
215         int         i_nal;
216         x264_nal_t  nal[X264_NAL_MAX];
217         int         i_bitstream;    /* size of p_bitstream */
218         uint8_t     *p_bitstream;   /* will hold data for all nal */
219         bs_t        bs;
220     } out;
221
222     /* frame number/poc */
223     int             i_frame;
224
225     int             i_frame_offset; /* decoding only */
226     int             i_frame_num;    /* decoding only */
227     int             i_poc_msb;      /* decoding only */
228     int             i_poc_lsb;      /* decoding only */
229     int             i_poc;          /* decoding only */
230
231     int             i_thread_num;   /* threads only */
232     int             i_nal_type;     /* threads only */
233     int             i_nal_ref_idc;  /* threads only */
234
235     /* We use only one SPS and one PPS */
236     x264_sps_t      sps_array[1];
237     x264_sps_t      *sps;
238     x264_pps_t      pps_array[1];
239     x264_pps_t      *pps;
240     int             i_idr_pic_id;
241
242     /* Slice header */
243     x264_slice_header_t sh;
244
245     /* cabac context */
246     x264_cabac_t    cabac;
247
248     struct
249     {
250         /* Frames to be encoded (whose types have been decided) */
251         x264_frame_t *current[X264_BFRAME_MAX+3];
252         /* Temporary buffer (frames types not yet decided) */
253         x264_frame_t *next[X264_BFRAME_MAX+3];
254         /* Unused frames */
255         x264_frame_t *unused[X264_BFRAME_MAX+3];
256         /* For adaptive B decision */
257         x264_frame_t *last_nonb;
258
259         /* frames used for reference +1 for decoding + sentinels */
260         x264_frame_t *reference[16+2+1+2];
261
262         int i_last_idr; /* Frame number of the last IDR */
263
264         int i_input;    /* Number of input frames already accepted */
265
266         int i_max_dpb;  /* Number of frames allocated in the decoded picture buffer */
267         int i_max_ref0;
268         int i_max_ref1;
269         int i_delay;    /* Number of frames buffered for B reordering */
270     } frames;
271
272     /* current frame being encoded */
273     x264_frame_t    *fenc;
274
275     /* frame being reconstructed */
276     x264_frame_t    *fdec;
277
278     /* references lists */
279     int             i_ref0;
280     x264_frame_t    *fref0[16+3];     /* ref list 0 */
281     int             i_ref1;
282     x264_frame_t    *fref1[16+3];     /* ref list 1 */
283     int             b_ref_reorder[2];
284
285
286
287     /* Current MB DCT coeffs */
288     struct
289     {
290         DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
291         DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
292         // FIXME merge with union
293         DECLARE_ALIGNED( int, luma8x8[4][64], 16 );
294         union
295         {
296             DECLARE_ALIGNED( int, residual_ac[15], 16 );
297             DECLARE_ALIGNED( int, luma4x4[16], 16 );
298         } block[16+8];
299     } dct;
300
301     /* MB table and cache for current frame/mb */
302     struct
303     {
304         int     i_mb_count;                 /* number of mbs in a frame */
305
306         /* Strides */
307         int     i_mb_stride;
308         int     i_b8_stride;
309         int     i_b4_stride;
310
311         /* Current index */
312         int     i_mb_x;
313         int     i_mb_y;
314         int     i_mb_xy;
315         int     i_b8_xy;
316         int     i_b4_xy;
317         
318         /* Search parameters */
319         int     i_me_method;
320         int     i_subpel_refine;
321         int     b_chroma_me;
322         /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
323         int     mv_min[2];
324         int     mv_max[2];
325         /* Fullpel MV range for motion search */
326         int     mv_min_fpel[2];
327         int     mv_max_fpel[2];
328
329         /* neighboring MBs */
330         unsigned int i_neighbour;
331         unsigned int i_neighbour8[4];       /* neighbours of each 8x8 or 4x4 block that are available */
332         unsigned int i_neighbour4[16];      /* at the time the block is coded */
333         int     i_mb_type_top; 
334         int     i_mb_type_left; 
335         int     i_mb_type_topleft; 
336         int     i_mb_type_topright; 
337
338         /* mb table */
339         int8_t  *type;                      /* mb type */
340         int8_t  *qp;                        /* mb qp */
341         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
342         int8_t  (*intra4x4_pred_mode)[7];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
343         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
344         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
345         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
346         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
347         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
348         int16_t (*mvr[2][16])[2];           /* 16x16 mv for each possible ref */
349         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
350         int8_t  *mb_transform_size;         /* transform_size_8x8_flag of each mb */
351
352         /* current value */
353         int     i_type;
354         int     i_partition;
355         int     i_sub_partition[4];
356         int     b_transform_8x8;
357
358         int     i_cbp_luma;
359         int     i_cbp_chroma;
360
361         int     i_intra16x16_pred_mode;
362         int     i_chroma_pred_mode;
363
364         struct
365         {
366             /* pointer over mb of the frame to be compressed */
367             uint8_t *p_fenc[3];
368
369             /* pointer over mb of the frame to be reconstrucated  */
370             uint8_t *p_fdec[3];
371
372             /* pointer over mb of the references */
373             uint8_t *p_fref[2][16][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
374
375             /* common stride */
376             int     i_stride[3];
377         } pic;
378
379         /* cache */
380         struct
381         {
382             /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
383             int     intra4x4_pred_mode[X264_SCAN8_SIZE];
384
385             /* i_non_zero_count if availble else 0x80 */
386             int     non_zero_count[X264_SCAN8_SIZE];
387
388             /* -1 if unused, -2 if unavaible */
389             int8_t  ref[2][X264_SCAN8_SIZE];
390
391             /* 0 if non avaible */
392             int16_t mv[2][X264_SCAN8_SIZE][2];
393             int16_t mvd[2][X264_SCAN8_SIZE][2];
394
395             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
396             int8_t  skip[X264_SCAN8_SIZE];
397
398             int16_t direct_mv[2][X264_SCAN8_SIZE][2];
399             int8_t  direct_ref[2][X264_SCAN8_SIZE];
400
401             /* number of neighbors (top and left) that used 8x8 dct */
402             int     i_neighbour_transform_size;
403             int     b_transform_8x8_allowed;
404         } cache;
405
406         /* */
407         int     i_qp;       /* current qp */
408         int     i_last_qp;  /* last qp */
409         int     i_last_dqp; /* last delta qp */
410         int     b_variable_qp; /* whether qp is allowed to vary per macroblock */
411         int     b_lossless;
412
413         /* B_direct and weighted prediction */
414         int     dist_scale_factor[16][16];
415         int     bipred_weight[16][16];
416         /* maps fref1[0]'s ref indices into the current list0 */
417         int     map_col_to_list0_buf[2]; // for negative indices
418         int     map_col_to_list0[16];
419     } mb;
420
421     /* rate control encoding only */
422     x264_ratecontrol_t *rc;
423
424     int i_last_inter_size;
425     int i_last_intra_size;
426     int i_last_intra_qp;
427
428     /* stats */
429     struct
430     {
431         /* Current frame stats */
432         struct
433         {
434             /* Headers bits (MV+Ref+MB Block Type */
435             int i_hdr_bits;
436             /* Texture bits (Intra/Predicted) */
437             int i_itex_bits;
438             int i_ptex_bits;
439             /* ? */
440             int i_misc_bits;
441             /* MB type counts */
442             int i_mb_count[19];
443             int i_mb_count_i;
444             int i_mb_count_p;
445             int i_mb_count_skip;
446             int i_mb_count_8x8dct[2];
447             /* Estimated (SATD) cost as Intra/Predicted frame */
448             /* XXX: both omit the cost of MBs coded as P_SKIP */
449             int i_intra_cost;
450             int i_inter_cost;
451         } frame;
452
453         /* Cummulated stats */
454
455         /* per slice info */
456         int   i_slice_count[5];
457         int64_t i_slice_size[5];
458         int     i_slice_qp[5];
459         /* */
460         int64_t i_sqe_global[5];
461         float   f_psnr_average[5];
462         float   f_psnr_mean_y[5];
463         float   f_psnr_mean_u[5];
464         float   f_psnr_mean_v[5];
465         /* */
466         int64_t i_mb_count[5][19];
467         int64_t i_mb_count_8x8dct[2];
468
469     } stat;
470
471     /* CPU functions dependants */
472     x264_predict_t      predict_16x16[4+3];
473     x264_predict_t      predict_8x8c[4+3];
474     x264_predict8x8_t   predict_8x8[9+3];
475     x264_predict_t      predict_4x4[9+3];
476
477     x264_pixel_function_t pixf;
478     x264_mc_functions_t   mc;
479     x264_dct_function_t   dctf;
480     x264_csp_function_t   csp;
481
482     /* vlc table for decoding purpose only */
483     x264_vlc_table_t *x264_coeff_token_lookup[5];
484     x264_vlc_table_t *x264_level_prefix_lookup;
485     x264_vlc_table_t *x264_total_zeros_lookup[15];
486     x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
487     x264_vlc_table_t *x264_run_before_lookup[7];
488
489 #if VISUALIZE
490     struct visualize_t *visualize;
491 #endif
492 };
493
494 #endif
495