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 *****************************************************************************/
35 #define snprintf _snprintf
36 #define X264_VERSION "" // no configure script for msvc
50 /****************************************************************************
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)))
59 /****************************************************************************
61 ****************************************************************************/
62 /* x264_malloc : will do or emulate a memalign
63 * XXX you HAVE TO use x264_free for buffer allocated
66 void *x264_malloc( int );
67 void *x264_realloc( void *p, int i_size );
68 void x264_free( void * );
70 /* x264_slurp_file: malloc space for the whole file and read it */
71 char *x264_slurp_file( const char *filename );
73 /* mdate: return the current date in microsecond */
74 int64_t x264_mdate( void );
77 void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
79 static inline int x264_clip3( int v, int i_min, int i_max )
81 return ( (v < i_min) ? i_min : (v > i_max) ? i_max : v );
84 static inline float x264_clip3f( float v, float f_min, float f_max )
86 return ( (v < f_min) ? f_min : (v > f_max) ? f_max : v );
89 static inline int x264_median( int a, int b, int c )
95 max = b; /* no need to do 'b > max' (more consuming than always doing affectation) */
102 return a + b + c - min - max;
106 /****************************************************************************
108 ****************************************************************************/
118 static const char slice_type_to_char[] = { 'P', 'B', 'I', 'S', 'S' };
136 int i_idr_pic_id; /* -1 if nal_type != 5 */
139 int i_delta_poc_bottom;
142 int i_redundant_pic_cnt;
144 int b_direct_spatial_mv_pred;
146 int b_num_ref_idx_override;
147 int i_num_ref_idx_l0_active;
148 int i_num_ref_idx_l1_active;
150 int b_ref_pic_list_reordering_l0;
151 int b_ref_pic_list_reordering_l1;
155 } ref_pic_list_order[2][16];
157 int i_cabac_init_idc;
163 /* deblocking filter */
164 int i_disable_deblocking_filter_idc;
165 int i_alpha_c0_offset;
168 } x264_slice_header_t;
172 #define X264_SCAN8_SIZE (6*8)
173 #define X264_SCAN8_0 (4+1*8)
175 static const int x264_scan8[16+2*4] =
178 4+1*8, 5+1*8, 4+2*8, 5+2*8,
179 6+1*8, 7+1*8, 6+2*8, 7+2*8,
180 4+3*8, 5+3*8, 4+4*8, 5+4*8,
181 6+3*8, 7+3*8, 6+4*8, 7+4*8,
201 #define X264_BFRAME_MAX 16
202 #define X264_SLICE_MAX 4
203 #define X264_NAL_MAX (4 + X264_SLICE_MAX)
205 typedef struct x264_ratecontrol_t x264_ratecontrol_t;
206 typedef struct x264_vlc_table_t x264_vlc_table_t;
210 /* encoder parameters */
213 x264_t *thread[X264_SLICE_MAX];
215 /* bitstream output */
219 x264_nal_t nal[X264_NAL_MAX];
220 int i_bitstream; /* size of p_bitstream */
221 uint8_t *p_bitstream; /* will hold data for all nal */
225 /* frame number/poc */
228 int i_frame_offset; /* decoding only */
229 int i_frame_num; /* decoding only */
230 int i_poc_msb; /* decoding only */
231 int i_poc_lsb; /* decoding only */
232 int i_poc; /* decoding only */
234 int i_thread_num; /* threads only */
235 int i_nal_type; /* threads only */
236 int i_nal_ref_idc; /* threads only */
238 /* We use only one SPS and one PPS */
239 x264_sps_t sps_array[1];
241 x264_pps_t pps_array[1];
245 int dequant4_mf[4][6][4][4];
246 int dequant8_mf[2][6][8][8];
247 int quant4_mf[4][6][4][4];
248 int quant8_mf[2][6][8][8];
251 x264_slice_header_t sh;
258 /* Frames to be encoded (whose types have been decided) */
259 x264_frame_t *current[X264_BFRAME_MAX+3];
260 /* Temporary buffer (frames types not yet decided) */
261 x264_frame_t *next[X264_BFRAME_MAX+3];
263 x264_frame_t *unused[X264_BFRAME_MAX+3];
264 /* For adaptive B decision */
265 x264_frame_t *last_nonb;
267 /* frames used for reference +1 for decoding + sentinels */
268 x264_frame_t *reference[16+2+1+2];
270 int i_last_idr; /* Frame number of the last IDR */
272 int i_input; /* Number of input frames already accepted */
274 int i_max_dpb; /* Number of frames allocated in the decoded picture buffer */
277 int i_delay; /* Number of frames buffered for B reordering */
280 /* current frame being encoded */
283 /* frame being reconstructed */
286 /* references lists */
288 x264_frame_t *fref0[16+3]; /* ref list 0 */
290 x264_frame_t *fref1[16+3]; /* ref list 1 */
291 int b_ref_reorder[2];
295 /* Current MB DCT coeffs */
298 DECLARE_ALIGNED( int, luma16x16_dc[16], 16 );
299 DECLARE_ALIGNED( int, chroma_dc[2][4], 16 );
300 // FIXME merge with union
301 DECLARE_ALIGNED( int, luma8x8[4][64], 16 );
304 DECLARE_ALIGNED( int, residual_ac[15], 16 );
305 DECLARE_ALIGNED( int, luma4x4[16], 16 );
309 /* MB table and cache for current frame/mb */
312 int i_mb_count; /* number of mbs in a frame */
326 /* Search parameters */
330 /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
333 /* Fullpel MV range for motion search */
337 /* neighboring MBs */
338 unsigned int i_neighbour;
339 unsigned int i_neighbour8[4]; /* neighbours of each 8x8 or 4x4 block that are available */
340 unsigned int i_neighbour4[16]; /* at the time the block is coded */
343 int i_mb_type_topleft;
344 int i_mb_type_topright;
347 int8_t *type; /* mb type */
348 int8_t *qp; /* mb qp */
349 int16_t *cbp; /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x0200 and 0x0400: chroma dc (all set for PCM)*/
350 int8_t (*intra4x4_pred_mode)[7]; /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
351 uint8_t (*non_zero_count)[16+4+4]; /* nzc. for I_PCM set to 16 */
352 int8_t *chroma_pred_mode; /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
353 int16_t (*mv[2])[2]; /* mb mv. set to 0 for intra mb */
354 int16_t (*mvd[2])[2]; /* mb mv difference with predict. set to 0 if intra. cabac only */
355 int8_t *ref[2]; /* mb ref. set to -1 if non used (intra or Lx only) */
356 int16_t (*mvr[2][16])[2]; /* 16x16 mv for each possible ref */
357 int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
358 int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */
363 int i_sub_partition[4];
369 int i_intra16x16_pred_mode;
370 int i_chroma_pred_mode;
374 /* pointer over mb of the frame to be compressed */
377 /* pointer over mb of the frame to be reconstrucated */
380 /* pointer over mb of the references */
381 uint8_t *p_fref[2][16][4+2]; /* last: lN, lH, lV, lHV, cU, cV */
390 /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
391 int intra4x4_pred_mode[X264_SCAN8_SIZE];
393 /* i_non_zero_count if availble else 0x80 */
394 int non_zero_count[X264_SCAN8_SIZE];
396 /* -1 if unused, -2 if unavaible */
397 int8_t ref[2][X264_SCAN8_SIZE];
399 /* 0 if non avaible */
400 int16_t mv[2][X264_SCAN8_SIZE][2];
401 int16_t mvd[2][X264_SCAN8_SIZE][2];
403 /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
404 int8_t skip[X264_SCAN8_SIZE];
406 int16_t direct_mv[2][X264_SCAN8_SIZE][2];
407 int8_t direct_ref[2][X264_SCAN8_SIZE];
409 /* number of neighbors (top and left) that used 8x8 dct */
410 int i_neighbour_transform_size;
411 int b_transform_8x8_allowed;
415 int i_qp; /* current qp */
416 int i_last_qp; /* last qp */
417 int i_last_dqp; /* last delta qp */
418 int b_variable_qp; /* whether qp is allowed to vary per macroblock */
421 /* B_direct and weighted prediction */
422 int dist_scale_factor[16][16];
423 int bipred_weight[16][16];
424 /* maps fref1[0]'s ref indices into the current list0 */
425 int map_col_to_list0_buf[2]; // for negative indices
426 int map_col_to_list0[16];
429 /* rate control encoding only */
430 x264_ratecontrol_t *rc;
432 int i_last_inter_size;
433 int i_last_intra_size;
439 /* Current frame stats */
442 /* Headers bits (MV+Ref+MB Block Type */
444 /* Texture bits (Intra/Predicted) */
454 int i_mb_count_8x8dct[2];
455 /* Estimated (SATD) cost as Intra/Predicted frame */
456 /* XXX: both omit the cost of MBs coded as P_SKIP */
461 /* Cummulated stats */
464 int i_slice_count[5];
465 int64_t i_slice_size[5];
468 int64_t i_sqe_global[5];
469 float f_psnr_average[5];
470 float f_psnr_mean_y[5];
471 float f_psnr_mean_u[5];
472 float f_psnr_mean_v[5];
474 int64_t i_mb_count[5][19];
475 int64_t i_mb_count_8x8dct[2];
479 /* CPU functions dependants */
480 x264_predict_t predict_16x16[4+3];
481 x264_predict_t predict_8x8c[4+3];
482 x264_predict8x8_t predict_8x8[9+3];
483 x264_predict_t predict_4x4[9+3];
485 x264_pixel_function_t pixf;
486 x264_mc_functions_t mc;
487 x264_dct_function_t dctf;
488 x264_csp_function_t csp;
490 /* vlc table for decoding purpose only */
491 x264_vlc_table_t *x264_coeff_token_lookup[5];
492 x264_vlc_table_t *x264_level_prefix_lookup;
493 x264_vlc_table_t *x264_total_zeros_lookup[15];
494 x264_vlc_table_t *x264_total_zeros_dc_lookup[3];
495 x264_vlc_table_t *x264_run_before_lookup[7];
498 struct visualize_t *visualize;