]> git.sesse.net Git - x264/blob - common/common.h
move zigzag scan functions to dsp function pointers.
[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 #include <stdlib.h>
34 #include <assert.h>
35
36 #ifdef _MSC_VER
37 #define snprintf _snprintf
38 #define X264_VERSION "" // no configure script for msvc
39 #endif
40
41 /* alloca: force 16byte alignment */
42 #ifdef _MSC_VER
43 #define x264_alloca(x) (void*)(((intptr_t)_alloca((x)+15)+15)&~15)
44 #else
45 #define x264_alloca(x) (void*)(((intptr_t) alloca((x)+15)+15)&~15)
46 #endif
47
48 /* threads */
49 #ifdef __WIN32__
50 #include <windows.h>
51 #define pthread_t               HANDLE
52 #define pthread_create(t,u,f,d) *(t)=CreateThread(NULL,0,f,d,0,NULL)
53 #define pthread_join(t,s)       { WaitForSingleObject(t,INFINITE); \
54                                   CloseHandle(t); } 
55 #define HAVE_PTHREAD 1
56
57 #elif defined(SYS_BEOS)
58 #include <kernel/OS.h>
59 #define pthread_t               thread_id
60 #define pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
61                                   resume_thread(*(t)); }
62 #define pthread_join(t,s)       { long tmp; \
63                                   wait_for_thread(t,(s)?(long*)(s):&tmp); }
64 #define HAVE_PTHREAD 1
65
66 #elif defined(HAVE_PTHREAD)
67 #include <pthread.h>
68 #endif
69
70 /****************************************************************************
71  * Macros
72  ****************************************************************************/
73 #define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
74 #define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
75 #define X264_MIN3(a,b,c) X264_MIN((a),X264_MIN((b),(c)))
76 #define X264_MAX3(a,b,c) X264_MAX((a),X264_MAX((b),(c)))
77 #define X264_MIN4(a,b,c,d) X264_MIN((a),X264_MIN3((b),(c),(d)))
78 #define X264_MAX4(a,b,c,d) X264_MAX((a),X264_MAX3((b),(c),(d)))
79 #define XCHG(type,a,b) { type t = a; a = b; b = t; }
80 #define FIX8(f) ((int)(f*(1<<8)+.5))
81
82 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
83 #define UNUSED __attribute__((unused))
84 #else
85 #define UNUSED
86 #endif
87
88 #define CHECKED_MALLOC( var, size )\
89 {\
90     var = x264_malloc( size );\
91     if( !var )\
92     {\
93         x264_log( h, X264_LOG_ERROR, "malloc failed\n" );\
94         goto fail;\
95     }\
96 }
97
98 #define X264_BFRAME_MAX 16
99 #define X264_SLICE_MAX 4
100 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
101
102 /****************************************************************************
103  * Includes
104  ****************************************************************************/
105 #include "x264.h"
106 #include "bs.h"
107 #include "set.h"
108 #include "predict.h"
109 #include "pixel.h"
110 #include "mc.h"
111 #include "frame.h"
112 #include "dct.h"
113 #include "cabac.h"
114 #include "csp.h"
115 #include "quant.h"
116
117 /****************************************************************************
118  * Generals functions
119  ****************************************************************************/
120 /* x264_malloc : will do or emulate a memalign
121  * XXX you HAVE TO use x264_free for buffer allocated
122  * with x264_malloc
123  */
124 void *x264_malloc( int );
125 void *x264_realloc( void *p, int i_size );
126 void  x264_free( void * );
127
128 /* x264_slurp_file: malloc space for the whole file and read it */
129 char *x264_slurp_file( const char *filename );
130
131 /* mdate: return the current date in microsecond */
132 int64_t x264_mdate( void );
133
134 /* x264_param2string: return a (malloced) string containing most of
135  * the encoding options */
136 char *x264_param2string( x264_param_t *p, int b_res );
137
138 /* log */
139 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
140
141 void x264_reduce_fraction( int *n, int *d );
142
143 static inline int x264_clip3( int v, int i_min, int i_max )
144 {
145     return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
146 }
147
148 static inline float x264_clip3f( float v, float f_min, float f_max )
149 {
150     return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
151 }
152
153 static inline int x264_median( int a, int b, int c )
154 {
155     int min = a, max =a;
156     if( b < min )
157         min = b;
158     else
159         max = b;    /* no need to do 'b > max' (more consuming than always doing affectation) */
160
161     if( c < min )
162         min = c;
163     else if( c > max )
164         max = c;
165
166     return a + b + c - min - max;
167 }
168
169
170 /****************************************************************************
171  *
172  ****************************************************************************/
173 enum slice_type_e
174 {
175     SLICE_TYPE_P  = 0,
176     SLICE_TYPE_B  = 1,
177     SLICE_TYPE_I  = 2,
178     SLICE_TYPE_SP = 3,
179     SLICE_TYPE_SI = 4
180 };
181
182 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
183
184 typedef struct
185 {
186     x264_sps_t *sps;
187     x264_pps_t *pps;
188
189     int i_type;
190     int i_first_mb;
191     int i_last_mb;
192
193     int i_pps_id;
194
195     int i_frame_num;
196
197     int b_mbaff;
198     int b_field_pic;
199     int b_bottom_field;
200
201     int i_idr_pic_id;   /* -1 if nal_type != 5 */
202
203     int i_poc_lsb;
204     int i_delta_poc_bottom;
205
206     int i_delta_poc[2];
207     int i_redundant_pic_cnt;
208
209     int b_direct_spatial_mv_pred;
210
211     int b_num_ref_idx_override;
212     int i_num_ref_idx_l0_active;
213     int i_num_ref_idx_l1_active;
214
215     int b_ref_pic_list_reordering_l0;
216     int b_ref_pic_list_reordering_l1;
217     struct {
218         int idc;
219         int arg;
220     } ref_pic_list_order[2][16];
221
222     int i_cabac_init_idc;
223
224     int i_qp;
225     int i_qp_delta;
226     int b_sp_for_swidth;
227     int i_qs_delta;
228
229     /* deblocking filter */
230     int i_disable_deblocking_filter_idc;
231     int i_alpha_c0_offset;
232     int i_beta_offset;
233
234 } x264_slice_header_t;
235
236 /* From ffmpeg
237  */
238 #define X264_SCAN8_SIZE (6*8)
239 #define X264_SCAN8_0 (4+1*8)
240
241 static const int x264_scan8[16+2*4] =
242 {
243     /* Luma */
244     4+1*8, 5+1*8, 4+2*8, 5+2*8,
245     6+1*8, 7+1*8, 6+2*8, 7+2*8,
246     4+3*8, 5+3*8, 4+4*8, 5+4*8,
247     6+3*8, 7+3*8, 6+4*8, 7+4*8,
248
249     /* Cb */
250     1+1*8, 2+1*8,
251     1+2*8, 2+2*8,
252
253     /* Cr */
254     1+4*8, 2+4*8,
255     1+5*8, 2+5*8,
256 };
257 /*
258    0 1 2 3 4 5 6 7
259  0
260  1   B B   L L L L
261  2   B B   L L L L
262  3         L L L L
263  4   R R   L L L L
264  5   R R
265 */
266
267 typedef struct x264_ratecontrol_t   x264_ratecontrol_t;
268 typedef struct x264_vlc_table_t     x264_vlc_table_t;
269
270 struct x264_t
271 {
272     /* encoder parameters */
273     x264_param_t    param;
274
275     x264_t *thread[X264_SLICE_MAX];
276
277     /* bitstream output */
278     struct
279     {
280         int         i_nal;
281         x264_nal_t  nal[X264_NAL_MAX];
282         int         i_bitstream;    /* size of p_bitstream */
283         uint8_t     *p_bitstream;   /* will hold data for all nal */
284         bs_t        bs;
285     } out;
286
287     /* frame number/poc */
288     int             i_frame;
289
290     int             i_frame_offset; /* decoding only */
291     int             i_frame_num;    /* decoding only */
292     int             i_poc_msb;      /* decoding only */
293     int             i_poc_lsb;      /* decoding only */
294     int             i_poc;          /* decoding only */
295
296     int             i_thread_num;   /* threads only */
297     int             i_nal_type;     /* threads only */
298     int             i_nal_ref_idc;  /* threads only */
299
300     /* We use only one SPS and one PPS */
301     x264_sps_t      sps_array[1];
302     x264_sps_t      *sps;
303     x264_pps_t      pps_array[1];
304     x264_pps_t      *pps;
305     int             i_idr_pic_id;
306
307     int             (*dequant4_mf[4])[4][4]; /* [4][6][4][4] */
308     int             (*dequant8_mf[2])[8][8]; /* [2][6][8][8] */
309     int             (*quant4_mf[4])[4][4];   /* [4][6][4][4] */
310     int             (*quant8_mf[2])[8][8];   /* [2][6][8][8] */
311     int             (*unquant4_mf[4])[16];   /* [4][52][16] */
312     int             (*unquant8_mf[2])[64];   /* [2][52][64] */
313
314     uint32_t        nr_residual_sum[2][64];
315     uint32_t        nr_offset[2][64];
316     uint32_t        nr_count[2];
317
318     /* Slice header */
319     x264_slice_header_t sh;
320
321     /* cabac context */
322     x264_cabac_t    cabac;
323
324     struct
325     {
326         /* Frames to be encoded (whose types have been decided) */
327         x264_frame_t *current[X264_BFRAME_MAX+3];
328         /* Temporary buffer (frames types not yet decided) */
329         x264_frame_t *next[X264_BFRAME_MAX+3];
330         /* Unused frames */
331         x264_frame_t *unused[X264_BFRAME_MAX+3];
332         /* For adaptive B decision */
333         x264_frame_t *last_nonb;
334
335         /* frames used for reference +1 for decoding + sentinels */
336         x264_frame_t *reference[16+2+1+2];
337
338         int i_last_idr; /* Frame number of the last IDR */
339
340         int i_input;    /* Number of input frames already accepted */
341
342         int i_max_dpb;  /* Number of frames allocated in the decoded picture buffer */
343         int i_max_ref0;
344         int i_max_ref1;
345         int i_delay;    /* Number of frames buffered for B reordering */
346         int b_have_lowres;  /* Whether 1/2 resolution luma planes are being used */
347     } frames;
348
349     /* current frame being encoded */
350     x264_frame_t    *fenc;
351
352     /* frame being reconstructed */
353     x264_frame_t    *fdec;
354
355     /* references lists */
356     int             i_ref0;
357     x264_frame_t    *fref0[16+3];     /* ref list 0 */
358     int             i_ref1;
359     x264_frame_t    *fref1[16+3];     /* ref list 1 */
360     int             b_ref_reorder[2];
361
362
363
364     /* Current MB DCT coeffs */
365     struct
366     {
367         DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
368         DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
369         // FIXME merge with union
370         DECLARE_ALIGNED( int, luma8x8[4][64], 16 );
371         union
372         {
373             DECLARE_ALIGNED( int, residual_ac[15], 16 );
374             DECLARE_ALIGNED( int, luma4x4[16], 16 );
375         } block[16+8];
376     } dct;
377
378     /* MB table and cache for current frame/mb */
379     struct
380     {
381         int     i_mb_count;                 /* number of mbs in a frame */
382
383         /* Strides */
384         int     i_mb_stride;
385         int     i_b8_stride;
386         int     i_b4_stride;
387
388         /* Current index */
389         int     i_mb_x;
390         int     i_mb_y;
391         int     i_mb_xy;
392         int     i_b8_xy;
393         int     i_b4_xy;
394         
395         /* Search parameters */
396         int     i_me_method;
397         int     i_subpel_refine;
398         int     b_chroma_me;
399         int     b_trellis;
400         int     b_noise_reduction;
401
402         int     b_interlaced;
403
404         /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
405         int     mv_min[2];
406         int     mv_max[2];
407         /* Subpel MV range for motion search.
408          * same mv_min/max but includes levels' i_mv_range. */
409         int     mv_min_spel[2];
410         int     mv_max_spel[2];
411         /* Fullpel MV range for motion search */
412         int     mv_min_fpel[2];
413         int     mv_max_fpel[2];
414
415         /* neighboring MBs */
416         unsigned int i_neighbour;
417         unsigned int i_neighbour8[4];       /* neighbours of each 8x8 or 4x4 block that are available */
418         unsigned int i_neighbour4[16];      /* at the time the block is coded */
419         int     i_mb_type_top; 
420         int     i_mb_type_left; 
421         int     i_mb_type_topleft; 
422         int     i_mb_type_topright; 
423         int     i_mb_prev_xy;
424         int     i_mb_top_xy;
425
426         /* mb table */
427         int8_t  *type;                      /* mb type */
428         int8_t  *qp;                        /* mb qp */
429         int16_t *cbp;                       /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc  (all set for PCM)*/
430         int8_t  (*intra4x4_pred_mode)[7];   /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
431         uint8_t (*non_zero_count)[16+4+4];  /* nzc. for I_PCM set to 16 */
432         int8_t  *chroma_pred_mode;          /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
433         int16_t (*mv[2])[2];                /* mb mv. set to 0 for intra mb */
434         int16_t (*mvd[2])[2];               /* mb mv difference with predict. set to 0 if intra. cabac only */
435         int8_t   *ref[2];                   /* mb ref. set to -1 if non used (intra or Lx only) */
436         int16_t (*mvr[2][32])[2];           /* 16x16 mv for each possible ref */
437         int8_t  *skipbp;                    /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
438         int8_t  *mb_transform_size;         /* transform_size_8x8_flag of each mb */
439
440         /* current value */
441         int     i_type;
442         int     i_partition;
443         int     i_sub_partition[4];
444         int     b_transform_8x8;
445
446         int     i_cbp_luma;
447         int     i_cbp_chroma;
448
449         int     i_intra16x16_pred_mode;
450         int     i_chroma_pred_mode;
451
452         struct
453         {
454             /* space for p_fenc and p_fdec */
455 #define FENC_STRIDE 16
456 #define FDEC_STRIDE 32
457             DECLARE_ALIGNED( uint8_t, fenc_buf[24*FENC_STRIDE], 16 );
458             DECLARE_ALIGNED( uint8_t, fdec_buf[27*FDEC_STRIDE], 16 );
459
460             /* pointer over mb of the frame to be compressed */
461             uint8_t *p_fenc[3];
462
463             /* pointer over mb of the frame to be reconstructed  */
464             uint8_t *p_fdec[3];
465
466             /* pointer over mb of the references */
467             int i_fref[2];
468             uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
469             uint16_t *p_integral[2][16];
470
471             /* fref stride */
472             int     i_stride[3];
473         } pic;
474
475         /* cache */
476         struct
477         {
478             /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
479             int     intra4x4_pred_mode[X264_SCAN8_SIZE];
480
481             /* i_non_zero_count if available else 0x80 */
482             int     non_zero_count[X264_SCAN8_SIZE];
483
484             /* -1 if unused, -2 if unavailable */
485             int8_t  ref[2][X264_SCAN8_SIZE];
486
487             /* 0 if not available */
488             int16_t mv[2][X264_SCAN8_SIZE][2];
489             int16_t mvd[2][X264_SCAN8_SIZE][2];
490
491             /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
492             int8_t  skip[X264_SCAN8_SIZE];
493
494             int16_t direct_mv[2][X264_SCAN8_SIZE][2];
495             int8_t  direct_ref[2][X264_SCAN8_SIZE];
496
497             /* number of neighbors (top and left) that used 8x8 dct */
498             int     i_neighbour_transform_size;
499             int     b_transform_8x8_allowed;
500             int     i_neighbour_interlaced;
501         } cache;
502
503         /* */
504         int     i_qp;       /* current qp */
505         int     i_last_qp;  /* last qp */
506         int     i_last_dqp; /* last delta qp */
507         int     b_variable_qp; /* whether qp is allowed to vary per macroblock */
508         int     b_lossless;
509         int     b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
510         int     b_direct_auto_write; /* analyse direct modes, to use and/or save */
511
512         /* B_direct and weighted prediction */
513         int     dist_scale_factor[16][2];
514         int     bipred_weight[32][4];
515         /* maps fref1[0]'s ref indices into the current list0 */
516         int     map_col_to_list0_buf[2]; // for negative indices
517         int     map_col_to_list0[16];
518     } mb;
519
520     /* rate control encoding only */
521     x264_ratecontrol_t *rc;
522
523     /* stats */
524     struct
525     {
526         /* Current frame stats */
527         struct
528         {
529             /* Headers bits (MV+Ref+MB Block Type */
530             int i_hdr_bits;
531             /* Texture bits (Intra/Predicted) */
532             int i_itex_bits;
533             int i_ptex_bits;
534             /* ? */
535             int i_misc_bits;
536             /* MB type counts */
537             int i_mb_count[19];
538             int i_mb_count_i;
539             int i_mb_count_p;
540             int i_mb_count_skip;
541             int i_mb_count_8x8dct[2];
542             int i_mb_count_size[7];
543             int i_mb_count_ref[32];
544             /* Estimated (SATD) cost as Intra/Predicted frame */
545             /* XXX: both omit the cost of MBs coded as P_SKIP */
546             int i_intra_cost;
547             int i_inter_cost;
548             /* Adaptive direct mv pred */
549             int i_direct_score[2];
550         } frame;
551
552         /* Cumulated stats */
553
554         /* per slice info */
555         int     i_slice_count[5];
556         int64_t i_slice_size[5];
557         int     i_slice_qp[5];
558         /* */
559         int64_t i_sqe_global[5];
560         float   f_psnr_average[5];
561         float   f_psnr_mean_y[5];
562         float   f_psnr_mean_u[5];
563         float   f_psnr_mean_v[5];
564         float   f_ssim_mean_y[5];
565         /* */
566         int64_t i_mb_count[5][19];
567         int64_t i_mb_count_8x8dct[2];
568         int64_t i_mb_count_size[2][7];
569         int64_t i_mb_count_ref[2][32];
570         /* */
571         int     i_direct_score[2];
572         int     i_direct_frames[2];
573
574     } stat;
575
576     /* CPU functions dependents */
577     x264_predict_t      predict_16x16[4+3];
578     x264_predict_t      predict_8x8c[4+3];
579     x264_predict8x8_t   predict_8x8[9+3];
580     x264_predict_t      predict_4x4[9+3];
581
582     x264_pixel_function_t pixf;
583     x264_mc_functions_t   mc;
584     x264_dct_function_t   dctf;
585     x264_zigzag_function_t zigzagf;
586     x264_csp_function_t   csp;
587     x264_quant_function_t quantf;
588     x264_deblock_function_t loopf;
589
590     /* vlc table for decoding purpose only */
591     x264_vlc_table_t *x264_coeff_token_lookup[5];
592     x264_vlc_table_t *x264_level_prefix_lookup;
593     x264_vlc_table_t *x264_total_zeros_lookup[15];
594     x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
595     x264_vlc_table_t *x264_run_before_lookup[7];
596
597 #if VISUALIZE
598     struct visualize_t *visualize;
599 #endif
600 };
601
602 // included at the end because it needs x264_t
603 #include "macroblock.h"
604
605 #endif
606