common.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*****************************************************************************
  2. * common.h: misc common functions
  3. *****************************************************************************
  4. * Copyright (C) 2003-2018 x264 project
  5. *
  6. * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  7. * Loren Merritt <lorenm@u.washington.edu>
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
  22. *
  23. * This program is also available under a commercial proprietary license.
  24. * For more information, contact us at licensing@x264.com.
  25. *****************************************************************************/
  26. #ifndef X264_COMMON_H
  27. #define X264_COMMON_H
  28. #include "base.h"
  29. /* Macros for templating function calls according to bit depth */
  30. #define x264_template(w) x264_glue3(x264, BIT_DEPTH, w)
  31. /****************************************************************************
  32. * API Templates
  33. ****************************************************************************/
  34. #define x264_nal_encode x264_template(nal_encode)
  35. #define x264_encoder_reconfig x264_template(encoder_reconfig)
  36. #define x264_encoder_parameters x264_template(encoder_parameters)
  37. #define x264_encoder_headers x264_template(encoder_headers)
  38. #define x264_encoder_encode x264_template(encoder_encode)
  39. #define x264_encoder_close x264_template(encoder_close)
  40. #define x264_encoder_delayed_frames x264_template(encoder_delayed_frames)
  41. #define x264_encoder_maximum_delayed_frames x264_template(encoder_maximum_delayed_frames)
  42. #define x264_encoder_intra_refresh x264_template(encoder_intra_refresh)
  43. #define x264_encoder_invalidate_reference x264_template(encoder_invalidate_reference)
  44. /* This undef allows to rename the external symbol and force link failure in case
  45. * of incompatible libraries. Then the define enables templating as above. */
  46. #undef x264_encoder_open
  47. #define x264_encoder_open x264_template(encoder_open)
  48. /****************************************************************************
  49. * Macros
  50. ****************************************************************************/
  51. #define X264_PCM_COST (FRAME_SIZE(256*BIT_DEPTH)+16)
  52. #define QP_BD_OFFSET (6*(BIT_DEPTH-8))
  53. #define QP_MAX_SPEC (51+QP_BD_OFFSET)
  54. #define QP_MAX (QP_MAX_SPEC+18)
  55. #define PIXEL_MAX ((1 << BIT_DEPTH)-1)
  56. // arbitrary, but low because SATD scores are 1/4 normal
  57. #define X264_LOOKAHEAD_QP (12+QP_BD_OFFSET)
  58. #define SPEC_QP(x) X264_MIN((x), QP_MAX_SPEC)
  59. #define NALU_OVERHEAD 5 // startcode + NAL type costs 5 bytes per frame
  60. #define FILLER_OVERHEAD (NALU_OVERHEAD+1)
  61. #define SEI_OVERHEAD (NALU_OVERHEAD - (h->param.b_annexb && !h->param.i_avcintra_class && (h->out.i_nal-1)))
  62. #if HAVE_INTERLACED
  63. # define MB_INTERLACED h->mb.b_interlaced
  64. # define SLICE_MBAFF h->sh.b_mbaff
  65. # define PARAM_INTERLACED h->param.b_interlaced
  66. #else
  67. # define MB_INTERLACED 0
  68. # define SLICE_MBAFF 0
  69. # define PARAM_INTERLACED 0
  70. #endif
  71. #ifdef CHROMA_FORMAT
  72. # define CHROMA_H_SHIFT (CHROMA_FORMAT == CHROMA_420 || CHROMA_FORMAT == CHROMA_422)
  73. # define CHROMA_V_SHIFT (CHROMA_FORMAT == CHROMA_420)
  74. #else
  75. # define CHROMA_FORMAT h->sps->i_chroma_format_idc
  76. # define CHROMA_H_SHIFT h->mb.chroma_h_shift
  77. # define CHROMA_V_SHIFT h->mb.chroma_v_shift
  78. #endif
  79. #define CHROMA_SIZE(s) (CHROMA_FORMAT ? (s)>>(CHROMA_H_SHIFT+CHROMA_V_SHIFT) : 0)
  80. #define FRAME_SIZE(s) ((s)+2*CHROMA_SIZE(s))
  81. #define CHROMA444 (CHROMA_FORMAT == CHROMA_444)
  82. #if HIGH_BIT_DEPTH
  83. typedef uint16_t pixel;
  84. typedef uint64_t pixel4;
  85. typedef int32_t dctcoef;
  86. typedef uint32_t udctcoef;
  87. # define PIXEL_SPLAT_X4(x) ((x)*0x0001000100010001ULL)
  88. # define MPIXEL_X4(src) M64(src)
  89. #else
  90. typedef uint8_t pixel;
  91. typedef uint32_t pixel4;
  92. typedef int16_t dctcoef;
  93. typedef uint16_t udctcoef;
  94. # define PIXEL_SPLAT_X4(x) ((x)*0x01010101U)
  95. # define MPIXEL_X4(src) M32(src)
  96. #endif
  97. #define CPPIXEL_X4(dst,src) MPIXEL_X4(dst) = MPIXEL_X4(src)
  98. /****************************************************************************
  99. * Includes
  100. ****************************************************************************/
  101. #if HAVE_OPENCL
  102. #include "opencl.h"
  103. #endif
  104. #include "cabac.h"
  105. #include "bitstream.h"
  106. #include "set.h"
  107. #include "predict.h"
  108. #include "pixel.h"
  109. #include "mc.h"
  110. #include "frame.h"
  111. #include "dct.h"
  112. #include "quant.h"
  113. #include "threadpool.h"
  114. /****************************************************************************
  115. * General functions
  116. ****************************************************************************/
  117. /* log */
  118. #define x264_log x264_template(log)
  119. void x264_log( x264_t *h, int i_level, const char *psz_fmt, ... );
  120. #define x264_cavlc_init x264_template(cavlc_init)
  121. void x264_cavlc_init( x264_t *h );
  122. #define x264_cabac_init x264_template(cabac_init)
  123. void x264_cabac_init( x264_t *h );
  124. static ALWAYS_INLINE pixel x264_clip_pixel( int x )
  125. {
  126. return ( (x & ~PIXEL_MAX) ? (-x)>>31 & PIXEL_MAX : x );
  127. }
  128. /****************************************************************************
  129. *
  130. ****************************************************************************/
  131. typedef struct
  132. {
  133. x264_sps_t *sps;
  134. x264_pps_t *pps;
  135. int i_type;
  136. int i_first_mb;
  137. int i_last_mb;
  138. int i_pps_id;
  139. int i_frame_num;
  140. int b_mbaff;
  141. int b_field_pic;
  142. int b_bottom_field;
  143. int i_idr_pic_id; /* -1 if nal_type != 5 */
  144. int i_poc;
  145. int i_delta_poc_bottom;
  146. int i_delta_poc[2];
  147. int i_redundant_pic_cnt;
  148. int b_direct_spatial_mv_pred;
  149. int b_num_ref_idx_override;
  150. int i_num_ref_idx_l0_active;
  151. int i_num_ref_idx_l1_active;
  152. int b_ref_pic_list_reordering[2];
  153. struct
  154. {
  155. int idc;
  156. int arg;
  157. } ref_pic_list_order[2][X264_REF_MAX];
  158. /* P-frame weighting */
  159. int b_weighted_pred;
  160. x264_weight_t weight[X264_REF_MAX*2][3];
  161. int i_mmco_remove_from_end;
  162. int i_mmco_command_count;
  163. struct /* struct for future expansion */
  164. {
  165. int i_difference_of_pic_nums;
  166. int i_poc;
  167. } mmco[X264_REF_MAX];
  168. int i_cabac_init_idc;
  169. int i_qp;
  170. int i_qp_delta;
  171. int b_sp_for_swidth;
  172. int i_qs_delta;
  173. /* deblocking filter */
  174. int i_disable_deblocking_filter_idc;
  175. int i_alpha_c0_offset;
  176. int i_beta_offset;
  177. } x264_slice_header_t;
  178. typedef struct x264_lookahead_t
  179. {
  180. volatile uint8_t b_exit_thread;
  181. uint8_t b_thread_active;
  182. uint8_t b_analyse_keyframe;
  183. int i_last_keyframe;
  184. int i_slicetype_length;
  185. x264_frame_t *last_nonb;
  186. x264_pthread_t thread_handle;
  187. x264_sync_frame_list_t ifbuf;
  188. x264_sync_frame_list_t next;
  189. x264_sync_frame_list_t ofbuf;
  190. } x264_lookahead_t;
  191. typedef struct x264_ratecontrol_t x264_ratecontrol_t;
  192. typedef struct x264_left_table_t
  193. {
  194. uint8_t intra[4];
  195. uint8_t nnz[4];
  196. uint8_t nnz_chroma[4];
  197. uint8_t mv[4];
  198. uint8_t ref[4];
  199. } x264_left_table_t;
  200. /* Current frame stats */
  201. typedef struct
  202. {
  203. /* MV bits (MV+Ref+Block Type) */
  204. int i_mv_bits;
  205. /* Texture bits (DCT coefs) */
  206. int i_tex_bits;
  207. /* ? */
  208. int i_misc_bits;
  209. /* MB type counts */
  210. int i_mb_count[19];
  211. int i_mb_count_i;
  212. int i_mb_count_p;
  213. int i_mb_count_skip;
  214. int i_mb_count_8x8dct[2];
  215. int i_mb_count_ref[2][X264_REF_MAX*2];
  216. int i_mb_partition[17];
  217. int i_mb_cbp[6];
  218. int i_mb_pred_mode[4][13];
  219. int i_mb_field[3];
  220. /* Adaptive direct mv pred */
  221. int i_direct_score[2];
  222. /* Metrics */
  223. int64_t i_ssd[3];
  224. double f_ssim;
  225. int i_ssim_cnt;
  226. } x264_frame_stat_t;
  227. struct x264_t
  228. {
  229. /* encoder parameters */
  230. x264_param_t param;
  231. x264_t *thread[X264_THREAD_MAX+1];
  232. x264_t *lookahead_thread[X264_LOOKAHEAD_THREAD_MAX];
  233. int b_thread_active;
  234. int i_thread_phase; /* which thread to use for the next frame */
  235. int i_thread_idx; /* which thread this is */
  236. int i_threadslice_start; /* first row in this thread slice */
  237. int i_threadslice_end; /* row after the end of this thread slice */
  238. int i_threadslice_pass; /* which pass of encoding we are on */
  239. x264_threadpool_t *threadpool;
  240. x264_threadpool_t *lookaheadpool;
  241. x264_pthread_mutex_t mutex;
  242. x264_pthread_cond_t cv;
  243. /* bitstream output */
  244. struct
  245. {
  246. int i_nal;
  247. int i_nals_allocated;
  248. x264_nal_t *nal;
  249. int i_bitstream; /* size of p_bitstream */
  250. uint8_t *p_bitstream; /* will hold data for all nal */
  251. bs_t bs;
  252. } out;
  253. uint8_t *nal_buffer;
  254. int nal_buffer_size;
  255. x264_t *reconfig_h;
  256. int reconfig;
  257. /**** thread synchronization starts here ****/
  258. /* frame number/poc */
  259. int i_frame;
  260. int i_frame_num;
  261. int i_thread_frames; /* Number of different frames being encoded by threads;
  262. * 1 when sliced-threads is on. */
  263. int i_nal_type;
  264. int i_nal_ref_idc;
  265. int64_t i_disp_fields; /* Number of displayed fields (both coded and implied via pic_struct) */
  266. int i_disp_fields_last_frame;
  267. int64_t i_prev_duration; /* Duration of previous frame */
  268. int64_t i_coded_fields; /* Number of coded fields (both coded and implied via pic_struct) */
  269. int64_t i_cpb_delay; /* Equal to number of fields preceding this field
  270. * since last buffering_period SEI */
  271. int64_t i_coded_fields_lookahead; /* Use separate counters for lookahead */
  272. int64_t i_cpb_delay_lookahead;
  273. int64_t i_cpb_delay_pir_offset;
  274. int64_t i_cpb_delay_pir_offset_next;
  275. int b_queued_intra_refresh;
  276. int64_t i_last_idr_pts;
  277. int i_idr_pic_id;
  278. /* quantization matrix for decoding, [cqm][qp%6][coef] */
  279. int (*dequant4_mf[4])[16]; /* [4][6][16] */
  280. int (*dequant8_mf[4])[64]; /* [4][6][64] */
  281. /* quantization matrix for trellis, [cqm][qp][coef] */
  282. int (*unquant4_mf[4])[16]; /* [4][QP_MAX_SPEC+1][16] */
  283. int (*unquant8_mf[4])[64]; /* [4][QP_MAX_SPEC+1][64] */
  284. /* quantization matrix for deadzone */
  285. udctcoef (*quant4_mf[4])[16]; /* [4][QP_MAX_SPEC+1][16] */
  286. udctcoef (*quant8_mf[4])[64]; /* [4][QP_MAX_SPEC+1][64] */
  287. udctcoef (*quant4_bias[4])[16]; /* [4][QP_MAX_SPEC+1][16] */
  288. udctcoef (*quant8_bias[4])[64]; /* [4][QP_MAX_SPEC+1][64] */
  289. udctcoef (*quant4_bias0[4])[16]; /* [4][QP_MAX_SPEC+1][16] */
  290. udctcoef (*quant8_bias0[4])[64]; /* [4][QP_MAX_SPEC+1][64] */
  291. udctcoef (*nr_offset_emergency)[4][64];
  292. /* mv/ref/mode cost arrays. */
  293. uint16_t *cost_mv[QP_MAX+1];
  294. uint16_t *cost_mv_fpel[QP_MAX+1][4];
  295. struct
  296. {
  297. uint16_t ref[QP_MAX+1][3][33];
  298. uint16_t i4x4_mode[QP_MAX+1][17];
  299. } *cost_table;
  300. const uint8_t *chroma_qp_table; /* includes both the nonlinear luma->chroma mapping and chroma_qp_offset */
  301. /* Slice header */
  302. x264_slice_header_t sh;
  303. /* SPS / PPS */
  304. x264_sps_t sps[1];
  305. x264_pps_t pps[1];
  306. /* Slice header backup, for SEI_DEC_REF_PIC_MARKING */
  307. int b_sh_backup;
  308. x264_slice_header_t sh_backup;
  309. /* cabac context */
  310. x264_cabac_t cabac;
  311. struct
  312. {
  313. /* Frames to be encoded (whose types have been decided) */
  314. x264_frame_t **current;
  315. /* Unused frames: 0 = fenc, 1 = fdec */
  316. x264_frame_t **unused[2];
  317. /* Unused blank frames (for duplicates) */
  318. x264_frame_t **blank_unused;
  319. /* frames used for reference + sentinels */
  320. x264_frame_t *reference[X264_REF_MAX+2];
  321. int i_last_keyframe; /* Frame number of the last keyframe */
  322. int i_last_idr; /* Frame number of the last IDR (not RP)*/
  323. int i_poc_last_open_gop; /* Poc of the I frame of the last open-gop. The value
  324. * is only assigned during the period between that
  325. * I frame and the next P or I frame, else -1 */
  326. int i_input; /* Number of input frames already accepted */
  327. int i_max_dpb; /* Number of frames allocated in the decoded picture buffer */
  328. int i_max_ref0;
  329. int i_max_ref1;
  330. int i_delay; /* Number of frames buffered for B reordering */
  331. int i_bframe_delay;
  332. int64_t i_bframe_delay_time;
  333. int64_t i_first_pts;
  334. int64_t i_prev_reordered_pts[2];
  335. int64_t i_largest_pts;
  336. int64_t i_second_largest_pts;
  337. int b_have_lowres; /* Whether 1/2 resolution luma planes are being used */
  338. int b_have_sub8x8_esa;
  339. } frames;
  340. /* current frame being encoded */
  341. x264_frame_t *fenc;
  342. /* frame being reconstructed */
  343. x264_frame_t *fdec;
  344. /* references lists */
  345. int i_ref[2];
  346. x264_frame_t *fref[2][X264_REF_MAX+3];
  347. x264_frame_t *fref_nearest[2];
  348. int b_ref_reorder[2];
  349. /* hrd */
  350. int initial_cpb_removal_delay;
  351. int initial_cpb_removal_delay_offset;
  352. int64_t i_reordered_pts_delay;
  353. /* Current MB DCT coeffs */
  354. struct
  355. {
  356. ALIGNED_64( dctcoef luma16x16_dc[3][16] );
  357. ALIGNED_16( dctcoef chroma_dc[2][8] );
  358. // FIXME share memory?
  359. ALIGNED_64( dctcoef luma8x8[12][64] );
  360. ALIGNED_64( dctcoef luma4x4[16*3][16] );
  361. } dct;
  362. /* MB table and cache for current frame/mb */
  363. struct
  364. {
  365. int i_mb_width;
  366. int i_mb_height;
  367. int i_mb_count; /* number of mbs in a frame */
  368. /* Chroma subsampling */
  369. int chroma_h_shift;
  370. int chroma_v_shift;
  371. /* Strides */
  372. int i_mb_stride;
  373. int i_b8_stride;
  374. int i_b4_stride;
  375. int left_b8[2];
  376. int left_b4[2];
  377. /* Current index */
  378. int i_mb_x;
  379. int i_mb_y;
  380. int i_mb_xy;
  381. int i_b8_xy;
  382. int i_b4_xy;
  383. /* Search parameters */
  384. int i_me_method;
  385. int i_subpel_refine;
  386. int b_chroma_me;
  387. int b_trellis;
  388. int b_noise_reduction;
  389. int b_dct_decimate;
  390. int i_psy_rd; /* Psy RD strength--fixed point value*/
  391. int i_psy_trellis; /* Psy trellis strength--fixed point value*/
  392. int b_interlaced;
  393. int b_adaptive_mbaff; /* MBAFF+subme 0 requires non-adaptive MBAFF i.e. all field mbs */
  394. /* Allowed qpel MV range to stay within the picture + emulated edge pixels */
  395. int mv_min[2];
  396. int mv_max[2];
  397. int mv_miny_row[3]; /* 0 == top progressive, 1 == bot progressive, 2 == interlaced */
  398. int mv_maxy_row[3];
  399. /* Subpel MV range for motion search.
  400. * same mv_min/max but includes levels' i_mv_range. */
  401. int mv_min_spel[2];
  402. int mv_max_spel[2];
  403. int mv_miny_spel_row[3];
  404. int mv_maxy_spel_row[3];
  405. /* Fullpel MV range for motion search */
  406. ALIGNED_8( int16_t mv_limit_fpel[2][2] ); /* min_x, min_y, max_x, max_y */
  407. int mv_miny_fpel_row[3];
  408. int mv_maxy_fpel_row[3];
  409. /* neighboring MBs */
  410. unsigned int i_neighbour;
  411. unsigned int i_neighbour8[4]; /* neighbours of each 8x8 or 4x4 block that are available */
  412. unsigned int i_neighbour4[16]; /* at the time the block is coded */
  413. unsigned int i_neighbour_intra; /* for constrained intra pred */
  414. unsigned int i_neighbour_frame; /* ignoring slice boundaries */
  415. int i_mb_type_top;
  416. int i_mb_type_left[2];
  417. int i_mb_type_topleft;
  418. int i_mb_type_topright;
  419. int i_mb_prev_xy;
  420. int i_mb_left_xy[2];
  421. int i_mb_top_xy;
  422. int i_mb_topleft_xy;
  423. int i_mb_topright_xy;
  424. int i_mb_top_y;
  425. int i_mb_topleft_y;
  426. int i_mb_topright_y;
  427. const x264_left_table_t *left_index_table;
  428. int i_mb_top_mbpair_xy;
  429. int topleft_partition;
  430. int b_allow_skip;
  431. int field_decoding_flag;
  432. /**** thread synchronization ends here ****/
  433. /* subsequent variables are either thread-local or constant,
  434. * and won't be copied from one thread to another */
  435. /* mb table */
  436. uint8_t *base; /* base pointer for all malloced data in this mb */
  437. int8_t *type; /* mb type */
  438. uint8_t *partition; /* mb partition */
  439. int8_t *qp; /* mb qp */
  440. int16_t *cbp; /* mb cbp: 0x0?: luma, 0x?0: chroma, 0x100: luma dc, 0x200 and 0x400: chroma dc, 0x1000 PCM (all set for PCM) */
  441. int8_t (*intra4x4_pred_mode)[8]; /* intra4x4 pred mode. for non I4x4 set to I_PRED_4x4_DC(2) */
  442. /* actually has only 7 entries; set to 8 for write-combining optimizations */
  443. uint8_t (*non_zero_count)[16*3]; /* nzc. for I_PCM set to 16 */
  444. int8_t *chroma_pred_mode; /* chroma_pred_mode. cabac only. for non intra I_PRED_CHROMA_DC(0) */
  445. int16_t (*mv[2])[2]; /* mb mv. set to 0 for intra mb */
  446. uint8_t (*mvd[2])[8][2]; /* absolute value of mb mv difference with predict, clipped to [0,33]. set to 0 if intra. cabac only */
  447. int8_t *ref[2]; /* mb ref. set to -1 if non used (intra or Lx only) */
  448. int16_t (*mvr[2][X264_REF_MAX*2])[2];/* 16x16 mv for each possible ref */
  449. int8_t *skipbp; /* block pattern for SKIP or DIRECT (sub)mbs. B-frames + cabac only */
  450. int8_t *mb_transform_size; /* transform_size_8x8_flag of each mb */
  451. uint32_t *slice_table; /* sh->first_mb of the slice that the indexed mb is part of */
  452. uint8_t *field;
  453. /* buffer for weighted versions of the reference frames */
  454. pixel *p_weight_buf[X264_REF_MAX];
  455. /* current value */
  456. int i_type;
  457. int i_partition;
  458. ALIGNED_4( uint8_t i_sub_partition[4] );
  459. int b_transform_8x8;
  460. int i_cbp_luma;
  461. int i_cbp_chroma;
  462. int i_intra16x16_pred_mode;
  463. int i_chroma_pred_mode;
  464. /* skip flags for i4x4 and i8x8
  465. * 0 = encode as normal.
  466. * 1 (non-RD only) = the DCT is still in h->dct, restore fdec and skip reconstruction.
  467. * 2 (RD only) = the DCT has since been overwritten by RD; restore that too. */
  468. int i_skip_intra;
  469. /* skip flag for motion compensation */
  470. /* if we've already done MC, we don't need to do it again */
  471. int b_skip_mc;
  472. /* set to true if we are re-encoding a macroblock. */
  473. int b_reencode_mb;
  474. int ip_offset; /* Used by PIR to offset the quantizer of intra-refresh blocks. */
  475. int b_deblock_rdo;
  476. int b_overflow; /* If CAVLC had a level code overflow during bitstream writing. */
  477. struct
  478. {
  479. /* space for p_fenc and p_fdec */
  480. #define FENC_STRIDE 16
  481. #define FDEC_STRIDE 32
  482. ALIGNED_64( pixel fenc_buf[48*FENC_STRIDE] );
  483. ALIGNED_64( pixel fdec_buf[54*FDEC_STRIDE] );
  484. /* i4x4 and i8x8 backup data, for skipping the encode stage when possible */
  485. ALIGNED_32( pixel i4x4_fdec_buf[16*16] );
  486. ALIGNED_32( pixel i8x8_fdec_buf[16*16] );
  487. ALIGNED_64( dctcoef i8x8_dct_buf[3][64] );
  488. ALIGNED_64( dctcoef i4x4_dct_buf[15][16] );
  489. uint32_t i4x4_nnz_buf[4];
  490. uint32_t i8x8_nnz_buf[4];
  491. /* Psy trellis DCT data */
  492. ALIGNED_64( dctcoef fenc_dct8[4][64] );
  493. ALIGNED_64( dctcoef fenc_dct4[16][16] );
  494. /* Psy RD SATD/SA8D scores cache */
  495. ALIGNED_64( uint32_t fenc_satd_cache[32] );
  496. ALIGNED_16( uint64_t fenc_hadamard_cache[9] );
  497. int i4x4_cbp;
  498. int i8x8_cbp;
  499. /* pointer over mb of the frame to be compressed */
  500. pixel *p_fenc[3]; /* y,u,v */
  501. /* pointer to the actual source frame, not a block copy */
  502. pixel *p_fenc_plane[3];
  503. /* pointer over mb of the frame to be reconstructed */
  504. pixel *p_fdec[3];
  505. /* pointer over mb of the references */
  506. int i_fref[2];
  507. /* [12]: yN, yH, yV, yHV, (NV12 ? uv : I444 ? (uN, uH, uV, uHV, vN, ...)) */
  508. pixel *p_fref[2][X264_REF_MAX*2][12];
  509. pixel *p_fref_w[X264_REF_MAX*2]; /* weighted fullpel luma */
  510. uint16_t *p_integral[2][X264_REF_MAX];
  511. /* fref stride */
  512. int i_stride[3];
  513. } pic;
  514. /* cache */
  515. struct
  516. {
  517. /* real intra4x4_pred_mode if I_4X4 or I_8X8, I_PRED_4x4_DC if mb available, -1 if not */
  518. ALIGNED_16( int8_t intra4x4_pred_mode[X264_SCAN8_LUMA_SIZE] );
  519. /* i_non_zero_count if available else 0x80. intentionally misaligned by 8 for asm */
  520. ALIGNED_8( uint8_t non_zero_count[X264_SCAN8_SIZE] );
  521. /* -1 if unused, -2 if unavailable */
  522. ALIGNED_4( int8_t ref[2][X264_SCAN8_LUMA_SIZE] );
  523. /* 0 if not available */
  524. ALIGNED_16( int16_t mv[2][X264_SCAN8_LUMA_SIZE][2] );
  525. ALIGNED_8( uint8_t mvd[2][X264_SCAN8_LUMA_SIZE][2] );
  526. /* 1 if SKIP or DIRECT. set only for B-frames + CABAC */
  527. ALIGNED_4( int8_t skip[X264_SCAN8_LUMA_SIZE] );
  528. ALIGNED_4( int16_t direct_mv[2][4][2] );
  529. ALIGNED_4( int8_t direct_ref[2][4] );
  530. int direct_partition;
  531. ALIGNED_4( int16_t pskip_mv[2] );
  532. /* number of neighbors (top and left) that used 8x8 dct */
  533. int i_neighbour_transform_size;
  534. int i_neighbour_skip;
  535. /* neighbor CBPs */
  536. int i_cbp_top;
  537. int i_cbp_left;
  538. /* extra data required for mbaff in mv prediction */
  539. int16_t topright_mv[2][3][2];
  540. int8_t topright_ref[2][3];
  541. /* current mb deblock strength */
  542. uint8_t (*deblock_strength)[8][4];
  543. } cache;
  544. /* */
  545. int i_qp; /* current qp */
  546. int i_chroma_qp;
  547. int i_last_qp; /* last qp */
  548. int i_last_dqp; /* last delta qp */
  549. int b_variable_qp; /* whether qp is allowed to vary per macroblock */
  550. int b_lossless;
  551. int b_direct_auto_read; /* take stats for --direct auto from the 2pass log */
  552. int b_direct_auto_write; /* analyse direct modes, to use and/or save */
  553. /* lambda values */
  554. int i_trellis_lambda2[2][2]; /* [luma,chroma][inter,intra] */
  555. int i_psy_rd_lambda;
  556. int i_chroma_lambda2_offset;
  557. /* B_direct and weighted prediction */
  558. int16_t dist_scale_factor_buf[2][2][X264_REF_MAX*2][4];
  559. int16_t (*dist_scale_factor)[4];
  560. int8_t bipred_weight_buf[2][2][X264_REF_MAX*2][4];
  561. int8_t (*bipred_weight)[4];
  562. /* maps fref1[0]'s ref indices into the current list0 */
  563. #define map_col_to_list0(col) h->mb.map_col_to_list0[(col)+2]
  564. int8_t map_col_to_list0[X264_REF_MAX+2];
  565. int ref_blind_dupe; /* The index of the blind reference frame duplicate. */
  566. int8_t deblock_ref_table[X264_REF_MAX*2+2];
  567. #define deblock_ref_table(x) h->mb.deblock_ref_table[(x)+2]
  568. } mb;
  569. /* rate control encoding only */
  570. x264_ratecontrol_t *rc;
  571. /* stats */
  572. struct
  573. {
  574. /* Cumulated stats */
  575. /* per slice info */
  576. int i_frame_count[3];
  577. int64_t i_frame_size[3];
  578. double f_frame_qp[3];
  579. int i_consecutive_bframes[X264_BFRAME_MAX+1];
  580. /* */
  581. double f_ssd_global[3];
  582. double f_psnr_average[3];
  583. double f_psnr_mean_y[3];
  584. double f_psnr_mean_u[3];
  585. double f_psnr_mean_v[3];
  586. double f_ssim_mean_y[3];
  587. double f_frame_duration[3];
  588. /* */
  589. int64_t i_mb_count[3][19];
  590. int64_t i_mb_partition[2][17];
  591. int64_t i_mb_count_8x8dct[2];
  592. int64_t i_mb_count_ref[2][2][X264_REF_MAX*2];
  593. int64_t i_mb_cbp[6];
  594. int64_t i_mb_pred_mode[4][13];
  595. int64_t i_mb_field[3];
  596. /* */
  597. int i_direct_score[2];
  598. int i_direct_frames[2];
  599. /* num p-frames weighted */
  600. int i_wpred[2];
  601. /* Current frame stats */
  602. x264_frame_stat_t frame;
  603. } stat;
  604. /* 0 = luma 4x4, 1 = luma 8x8, 2 = chroma 4x4, 3 = chroma 8x8 */
  605. udctcoef (*nr_offset)[64];
  606. uint32_t (*nr_residual_sum)[64];
  607. uint32_t *nr_count;
  608. ALIGNED_32( udctcoef nr_offset_denoise[4][64] );
  609. ALIGNED_32( uint32_t nr_residual_sum_buf[2][4][64] );
  610. uint32_t nr_count_buf[2][4];
  611. uint8_t luma2chroma_pixel[7]; /* Subsampled pixel size */
  612. /* Buffers that are allocated per-thread even in sliced threads. */
  613. void *scratch_buffer; /* for any temporary storage that doesn't want repeated malloc */
  614. void *scratch_buffer2; /* if the first one's already in use */
  615. pixel *intra_border_backup[5][3]; /* bottom pixels of the previous mb row, used for intra prediction after the framebuffer has been deblocked */
  616. /* Deblock strength values are stored for each 4x4 partition. In MBAFF
  617. * there are four extra values that need to be stored, located in [4][i]. */
  618. uint8_t (*deblock_strength[2])[2][8][4];
  619. /* CPU functions dependents */
  620. x264_predict_t predict_16x16[4+3];
  621. x264_predict8x8_t predict_8x8[9+3];
  622. x264_predict_t predict_4x4[9+3];
  623. x264_predict_t predict_chroma[4+3];
  624. x264_predict_t predict_8x8c[4+3];
  625. x264_predict_t predict_8x16c[4+3];
  626. x264_predict_8x8_filter_t predict_8x8_filter;
  627. x264_pixel_function_t pixf;
  628. x264_mc_functions_t mc;
  629. x264_dct_function_t dctf;
  630. x264_zigzag_function_t zigzagf;
  631. x264_zigzag_function_t zigzagf_interlaced;
  632. x264_zigzag_function_t zigzagf_progressive;
  633. x264_quant_function_t quantf;
  634. x264_deblock_function_t loopf;
  635. x264_bitstream_function_t bsf;
  636. x264_lookahead_t *lookahead;
  637. #if HAVE_OPENCL
  638. x264_opencl_t opencl;
  639. #endif
  640. };
  641. typedef struct
  642. {
  643. int sad;
  644. int16_t mv[2];
  645. } mvsad_t;
  646. // included at the end because it needs x264_t
  647. #include "macroblock.h"
  648. static ALWAYS_INLINE int x264_predictor_roundclip( int16_t (*dst)[2], int16_t (*mvc)[2], int i_mvc, int16_t mv_limit[2][2], uint32_t pmv )
  649. {
  650. int cnt = 0;
  651. for( int i = 0; i < i_mvc; i++ )
  652. {
  653. int mx = (mvc[i][0] + 2) >> 2;
  654. int my = (mvc[i][1] + 2) >> 2;
  655. uint32_t mv = pack16to32_mask(mx, my);
  656. if( !mv || mv == pmv ) continue;
  657. dst[cnt][0] = x264_clip3( mx, mv_limit[0][0], mv_limit[1][0] );
  658. dst[cnt][1] = x264_clip3( my, mv_limit[0][1], mv_limit[1][1] );
  659. cnt++;
  660. }
  661. return cnt;
  662. }
  663. static ALWAYS_INLINE int x264_predictor_clip( int16_t (*dst)[2], int16_t (*mvc)[2], int i_mvc, int16_t mv_limit[2][2], uint32_t pmv )
  664. {
  665. int cnt = 0;
  666. int qpel_limit[4] = {mv_limit[0][0] << 2, mv_limit[0][1] << 2, mv_limit[1][0] << 2, mv_limit[1][1] << 2};
  667. for( int i = 0; i < i_mvc; i++ )
  668. {
  669. uint32_t mv = M32( mvc[i] );
  670. int mx = mvc[i][0];
  671. int my = mvc[i][1];
  672. if( !mv || mv == pmv ) continue;
  673. dst[cnt][0] = x264_clip3( mx, qpel_limit[0], qpel_limit[2] );
  674. dst[cnt][1] = x264_clip3( my, qpel_limit[1], qpel_limit[3] );
  675. cnt++;
  676. }
  677. return cnt;
  678. }
  679. #if ARCH_X86 || ARCH_X86_64
  680. #include "x86/util.h"
  681. #endif
  682. #include "rectangle.h"
  683. #endif