Coverage Report

Created: 2026-04-01 07:49

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