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 $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
37 #define inline __inline
38 #define strncasecmp(s1, s2, n) strnicmp(s1, s2, n)
39 #define snprintf _snprintf
40 #define X264_VERSION "" // no configure script for msvc
43 /* alloca: force 16byte alignment */
45 #define x264_alloca(x) (void*)(((intptr_t)_alloca((x)+15)+15)&~15)
47 #define x264_alloca(x) (void*)(((intptr_t) alloca((x)+15)+15)&~15)
51 #define DECLARE_ALIGNED( type, var, n ) __declspec(align(n)) type var
53 #define DECLARE_ALIGNED( type, var, n ) type var __attribute__((aligned(n)))
57 #if defined(__WIN32__) && defined(HAVE_PTHREAD)
59 #define USE_CONDITION_VAR
61 #elif defined(SYS_BEOS)
62 #include <kernel/OS.h>
63 #define pthread_t thread_id
64 #define pthread_create(t,u,f,d) { *(t)=spawn_thread(f,"",10,d); \
65 resume_thread(*(t)); }
66 #define pthread_join(t,s) { long tmp; \
67 wait_for_thread(t,(s)?(long*)(s):&tmp); }
69 #define usleep(t) snooze(t)
71 #define HAVE_PTHREAD 1
73 #elif defined(HAVE_PTHREAD)
75 #define USE_CONDITION_VAR
78 #define pthread_create(t,u,f,d)
79 #define pthread_join(t,s)
82 #ifndef USE_CONDITION_VAR
83 #define pthread_mutex_t int
84 #define pthread_mutex_init(m,f)
85 #define pthread_mutex_destroy(m)
86 #define pthread_mutex_lock(m)
87 #define pthread_mutex_unlock(m)
88 #define pthread_cond_t int
89 #define pthread_cond_init(c,f)
90 #define pthread_cond_destroy(c)
91 #define pthread_cond_broadcast(c)
92 #define pthread_cond_wait(c,m) usleep(100)
95 /****************************************************************************
97 ****************************************************************************/
98 #define X264_MIN(a,b) ( (a)<(b) ? (a) : (b) )
99 #define X264_MAX(a,b) ( (a)>(b) ? (a) : (b) )
100 #define X264_MIN3(a,b,c) X264_MIN((a),X264_MIN((b),(c)))
101 #define X264_MAX3(a,b,c) X264_MAX((a),X264_MAX((b),(c)))
102 #define X264_MIN4(a,b,c,d) X264_MIN((a),X264_MIN3((b),(c),(d)))
103 #define X264_MAX4(a,b,c,d) X264_MAX((a),X264_MAX3((b),(c),(d)))
104 #define XCHG(type,a,b) { type t = a; a = b; b = t; }
105 #define FIX8(f) ((int)(f*(1<<8)+.5))
108 #define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
111 #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
112 #define UNUSED __attribute__((unused))
117 #define CHECKED_MALLOC( var, size )\
119 var = x264_malloc( size );\
122 x264_log( h, X264_LOG_ERROR, "malloc failed\n" );\
127 #define X264_BFRAME_MAX 16
128 #define X264_THREAD_MAX 128
129 #define X264_SLICE_MAX 4
130 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
131 #define X264_THREAD_HEIGHT 24 // number of pixels (per thread) in progress at any given time. could theoretically be as low as 22
133 /****************************************************************************
135 ****************************************************************************/
148 /****************************************************************************
150 ****************************************************************************/
151 /* x264_malloc : will do or emulate a memalign
152 * XXX you HAVE TO use x264_free for buffer allocated
155 void *x264_malloc( int );
156 void *x264_realloc( void *p, int i_size );
157 void x264_free( void * );
159 /* x264_slurp_file: malloc space for the whole file and read it */
160 char *x264_slurp_file( const char *filename );
162 /* mdate: return the current date in microsecond */
163 int64_t x264_mdate( void );
165 /* x264_param2string: return a (malloced) string containing most of
166 * the encoding options */
167 char *x264_param2string( x264_param_t *p, int b_res );
170 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
172 void x264_reduce_fraction( int *n, int *d );
174 static inline int x264_clip3( int v, int i_min, int i_max )
176 return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
179 static inline float x264_clip3f( float v, float f_min, float f_max )
181 return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
184 static inline int x264_median( int a, int b, int c )
190 max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */
197 return a + b + c - min - max;
201 /****************************************************************************
203 ****************************************************************************/
213 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
232 int i_idr_pic_id; /* -1 if nal_type != 5 */
235 int i_delta_poc_bottom;
238 int i_redundant_pic_cnt;
240 int b_direct_spatial_mv_pred;
242 int b_num_ref_idx_override;
243 int i_num_ref_idx_l0_active;
244 int i_num_ref_idx_l1_active;
246 int b_ref_pic_list_reordering_l0;
247 int b_ref_pic_list_reordering_l1;
251 } ref_pic_list_order[2][16];
253 int i_cabac_init_idc;
260 /* deblocking filter */
261 int i_disable_deblocking_filter_idc;
262 int i_alpha_c0_offset;
265 } x264_slice_header_t;
269 #define X264_SCAN8_SIZE (6*8)
270 #define X264_SCAN8_0 (4+1*8)
272 static const int x264_scan8[16+2*4] =
275 4+1*8, 5+1*8, 4+2*8, 5+2*8,
276 6+1*8, 7+1*8, 6+2*8, 7+2*8,
277 4+3*8, 5+3*8, 4+4*8, 5+4*8,
278 6+3*8, 7+3*8, 6+4*8, 7+4*8,
298 typedef struct x264_ratecontrol_t x264_ratecontrol_t;
299 typedef struct x264_vlc_table_t x264_vlc_table_t;
303 /* encoder parameters */
306 x264_t *thread[X264_THREAD_MAX];
307 pthread_t thread_handle;
309 int i_thread_phase; /* which thread to use for the next frame */
311 /* bitstream output */
315 x264_nal_t nal[X264_NAL_MAX];
316 int i_bitstream; /* size of p_bitstream */
317 uint8_t *p_bitstream; /* will hold data for all nal */
322 /* frame number/poc */
325 int i_frame_offset; /* decoding only */
326 int i_frame_num; /* decoding only */
327 int i_poc_msb; /* decoding only */
328 int i_poc_lsb; /* decoding only */
329 int i_poc; /* decoding only */
331 int i_thread_num; /* threads only */
332 int i_nal_type; /* threads only */
333 int i_nal_ref_idc; /* threads only */
335 /* We use only one SPS and one PPS */
336 x264_sps_t sps_array[1];
338 x264_pps_t pps_array[1];
342 int (*dequant4_mf[4])[4][4]; /* [4][6][4][4] */
343 int (*dequant8_mf[2])[8][8]; /* [2][6][8][8] */
344 int (*unquant4_mf[4])[16]; /* [4][52][16] */
345 int (*unquant8_mf[2])[64]; /* [2][52][64] */
346 uint16_t (*quant4_mf[4])[16]; /* [4][52][16] */
347 uint16_t (*quant8_mf[2])[64]; /* [2][52][64] */
348 uint16_t (*quant4_bias[4])[16]; /* [4][52][16] */
349 uint16_t (*quant8_bias[2])[64]; /* [2][52][64] */
351 uint32_t nr_residual_sum[2][64];
352 uint32_t nr_offset[2][64];
353 uint32_t nr_count[2];
356 x264_slice_header_t sh;
363 /* Frames to be encoded (whose types have been decided) */
364 x264_frame_t *current[X264_BFRAME_MAX+3];
365 /* Temporary buffer (frames types not yet decided) */
366 x264_frame_t *next[X264_BFRAME_MAX+3];
368 x264_frame_t *unused[X264_BFRAME_MAX + X264_THREAD_MAX*2 + 16+4];
369 /* For adaptive B decision */
370 x264_frame_t *last_nonb;
372 /* frames used for reference + sentinels */
373 x264_frame_t *reference[16+2];
375 int i_last_idr; /* Frame number of the last IDR */
377 int i_input; /* Number of input frames already accepted */
379 int i_max_dpb; /* Number of frames allocated in the decoded picture buffer */
382 int i_delay; /* Number of frames buffered for B reordering */
383 int b_have_lowres; /* Whether 1/2 resolution luma planes are being used */
386 /* current frame being encoded */
389 /* frame being reconstructed */
392 /* references lists */
394 x264_frame_t *fref0[16+3]; /* ref list 0 */
396 x264_frame_t *fref1[16+3]; /* ref list 1 */
397 int b_ref_reorder[2];
401 /* Current MB DCT coeffs */
404 DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
405 DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
406 // FIXME merge with union
407 DECLARE_ALIGNED( int, luma8x8[4][64], 16 );
410 DECLARE_ALIGNED( int, residual_ac[15], 16 );
411 DECLARE_ALIGNED( int, luma4x4[16], 16 );
415 /* MB table and cache for current frame/mb */
418 int i_mb_count; /* number of mbs in a frame */
432 /* Search parameters */
437 int b_noise_reduction;
441 /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
444 /* Subpel MV range for motion search.
445 * same mv_min/max but includes levels' i_mv_range. */
448 /* Fullpel MV range for motion search */
452 /* neighboring MBs */
453 unsigned int i_neighbour;
454 unsigned int i_neighbour8[4]; /* neighbours of each 8x8 or 4x4 block that are available */
455 unsigned int i_neighbour4[16]; /* at the time the block is coded */
458 int i_mb_type_topleft;
459 int i_mb_type_topright;
464 int8_t *type; /* mb type */
465 int8_t *qp; /* mb qp */
466 int16_t *cbp; /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc (all set for PCM)*/
467 int8_t (*intra4x4_pred_mode)[7]; /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
468 uint8_t (*non_zero_count)[16+4+4]; /* nzc. for I_PCM set to 16 */
469 int8_t *chroma_pred_mode; /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
470 int16_t (*mv[2])[2]; /* mb mv. set to 0 for intra mb */
471 int16_t (*mvd[2])[2]; /* mb mv difference with predict. set to 0 if intra. cabac only */
472 int8_t *ref[2]; /* mb ref. set to -1 if non used (intra or Lx only) */
473 int16_t (*mvr[2][32])[2]; /* 16x16 mv for each possible ref */
474 int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
475 int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */
476 uint8_t *intra_border_backup[2][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
481 int i_sub_partition[4];
487 int i_intra16x16_pred_mode;
488 int i_chroma_pred_mode;
492 /* space for p_fenc and p_fdec */
493 #define FENC_STRIDE 16
494 #define FDEC_STRIDE 32
495 DECLARE_ALIGNED( uint8_t, fenc_buf[24*FENC_STRIDE], 16 );
496 DECLARE_ALIGNED( uint8_t, fdec_buf[27*FDEC_STRIDE], 16 );
498 /* pointer over mb of the frame to be compressed */
501 /* pointer over mb of the frame to be reconstructed */
504 /* pointer over mb of the references */
506 uint8_t *p_fref[2][32][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
507 uint16_t *p_integral[2][16];
516 /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
517 int intra4x4_pred_mode[X264_SCAN8_SIZE];
519 /* i_non_zero_count if available else 0x80 */
520 int non_zero_count[X264_SCAN8_SIZE];
522 /* -1 if unused, -2 if unavailable */
523 int8_t ref[2][X264_SCAN8_SIZE];
525 /* 0 if not available */
526 int16_t mv[2][X264_SCAN8_SIZE][2];
527 int16_t mvd[2][X264_SCAN8_SIZE][2];
529 /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
530 int8_t skip[X264_SCAN8_SIZE];
532 int16_t direct_mv[2][X264_SCAN8_SIZE][2];
533 int8_t direct_ref[2][X264_SCAN8_SIZE];
536 /* number of neighbors (top and left) that used 8x8 dct */
537 int i_neighbour_transform_size;
538 int b_transform_8x8_allowed;
539 int i_neighbour_interlaced;
543 int i_qp; /* current qp */
545 int i_last_qp; /* last qp */
546 int i_last_dqp; /* last delta qp */
547 int b_variable_qp; /* whether qp is allowed to vary per macroblock */
549 int b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
550 int b_direct_auto_write; /* analyse direct modes, to use and/or save */
552 /* B_direct and weighted prediction */
553 int dist_scale_factor[16][2];
554 int bipred_weight[32][4];
555 /* maps fref1[0]'s ref indices into the current list0 */
556 int map_col_to_list0_buf[2]; // for negative indices
557 int map_col_to_list0[16];
560 /* rate control encoding only */
561 x264_ratecontrol_t *rc;
566 /* Current frame stats */
569 /* Headers bits (MV+Ref+MB Block Type */
571 /* Texture bits (Intra/Predicted) */
581 int i_mb_count_8x8dct[2];
582 int i_mb_count_size[7];
583 int i_mb_count_ref[32];
584 /* Estimated (SATD) cost as Intra/Predicted frame */
585 /* XXX: both omit the cost of MBs coded as P_SKIP */
589 /* Adaptive direct mv pred */
590 int i_direct_score[2];
593 /* Cumulated stats */
596 int i_slice_count[5];
597 int64_t i_slice_size[5];
600 int64_t i_sqe_global[5];
601 float f_psnr_average[5];
602 float f_psnr_mean_y[5];
603 float f_psnr_mean_u[5];
604 float f_psnr_mean_v[5];
605 float f_ssim_mean_y[5];
607 int64_t i_mb_count[5][19];
608 int64_t i_mb_count_8x8dct[2];
609 int64_t i_mb_count_size[2][7];
610 int64_t i_mb_count_ref[2][32];
612 int i_direct_score[2];
613 int i_direct_frames[2];
617 /* CPU functions dependents */
618 x264_predict_t predict_16x16[4+3];
619 x264_predict_t predict_8x8c[4+3];
620 x264_predict8x8_t predict_8x8[9+3];
621 x264_predict_t predict_4x4[9+3];
623 x264_pixel_function_t pixf;
624 x264_mc_functions_t mc;
625 x264_dct_function_t dctf;
626 x264_zigzag_function_t zigzagf;
627 x264_csp_function_t csp;
628 x264_quant_function_t quantf;
629 x264_deblock_function_t loopf;
631 /* vlc table for decoding purpose only */
632 x264_vlc_table_t *x264_coeff_token_lookup[5];
633 x264_vlc_table_t *x264_level_prefix_lookup;
634 x264_vlc_table_t *x264_total_zeros_lookup[15];
635 x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
636 x264_vlc_table_t *x264_run_before_lookup[7];
639 struct visualize_t *visualize;
643 // included at the end because it needs x264_t
644 #include "macroblock.h"