1 /*****************************************************************************
2  * pixel.c: pixel metrics
3  *****************************************************************************
4  * Copyright (C) 2004-2021 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Fiona Glaser <fiona@x264.com>
8             Henrik Gramner <henrik@gramner.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27 
28 #ifndef X264_PIXEL_H
29 #define X264_PIXEL_H
30 
31 // SSD assumes all args aligned
32 // other cmp functions assume first arg aligned
33 typedef int  (*x264_pixel_cmp_t)( pixel *, intptr_t, pixel *, intptr_t );
34 typedef void (*x264_pixel_cmp_x3_t)( pixel *, pixel *, pixel *, pixel *, intptr_t, int[3] );
35 typedef void (*x264_pixel_cmp_x4_t)( pixel *, pixel *, pixel *, pixel *, pixel *, intptr_t, int[4] );
36 
37 enum
38 {
39     PIXEL_16x16 = 0,
40     PIXEL_16x8  = 1,
41     PIXEL_8x16  = 2,
42     PIXEL_8x8   = 3,
43     PIXEL_8x4   = 4,
44     PIXEL_4x8   = 5,
45     PIXEL_4x4   = 6,
46 
47     /* Subsampled chroma only */
48     PIXEL_4x16  = 7,  /* 4:2:2 */
49     PIXEL_4x2   = 8,
50     PIXEL_2x8   = 9,  /* 4:2:2 */
51     PIXEL_2x4   = 10,
52     PIXEL_2x2   = 11,
53 };
54 
55 static const struct { uint8_t w, h; } x264_pixel_size[12] =
56 {
57     { 16, 16 }, { 16, 8 }, { 8, 16 }, { 8, 8 }, { 8, 4 }, { 4, 8 }, { 4, 4 },
58     {  4, 16 }, {  4, 2 }, { 2,  8 }, { 2, 4 }, { 2, 2 },
59 };
60 
61 static const uint8_t x264_size2pixel[5][5] =
62 {
63     { 0, },
64     { 0, PIXEL_4x4, PIXEL_8x4, 0, 0 },
65     { 0, PIXEL_4x8, PIXEL_8x8, 0, PIXEL_16x8 },
66     { 0, },
67     { 0, 0,        PIXEL_8x16, 0, PIXEL_16x16 }
68 };
69 
70 static const uint8_t x264_luma2chroma_pixel[4][7] =
71 {
72     { 0 },
73     { PIXEL_8x8,   PIXEL_8x4,  PIXEL_4x8,  PIXEL_4x4, PIXEL_4x2, PIXEL_2x4, PIXEL_2x2 }, /* 4:2:0 */
74     { PIXEL_8x16,  PIXEL_8x8,  PIXEL_4x16, PIXEL_4x8, PIXEL_4x4, PIXEL_2x8, PIXEL_2x4 }, /* 4:2:2 */
75     { PIXEL_16x16, PIXEL_16x8, PIXEL_8x16, PIXEL_8x8, PIXEL_8x4, PIXEL_4x8, PIXEL_4x4 }, /* 4:4:4 */
76 };
77 
78 typedef struct
79 {
80     x264_pixel_cmp_t  sad[8];
81     x264_pixel_cmp_t  ssd[8];
82     x264_pixel_cmp_t satd[8];
83     x264_pixel_cmp_t ssim[7];
84     x264_pixel_cmp_t sa8d[4];
85     x264_pixel_cmp_t mbcmp[8]; /* either satd or sad for subpel refine and mode decision */
86     x264_pixel_cmp_t mbcmp_unaligned[8]; /* unaligned mbcmp for subpel */
87     x264_pixel_cmp_t fpelcmp[8]; /* either satd or sad for fullpel motion search */
88     x264_pixel_cmp_x3_t fpelcmp_x3[7];
89     x264_pixel_cmp_x4_t fpelcmp_x4[7];
90     x264_pixel_cmp_t sad_aligned[8]; /* Aligned SAD for mbcmp */
91     int (*vsad)( pixel *, intptr_t, int );
92     int (*asd8)( pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2, int height );
93     uint64_t (*sa8d_satd[1])( pixel *pix1, intptr_t stride1, pixel *pix2, intptr_t stride2 );
94 
95     uint64_t (*var[4])( pixel *pix, intptr_t stride );
96     int (*var2[4])( pixel *fenc, pixel *fdec, int ssd[2] );
97     uint64_t (*hadamard_ac[4])( pixel *pix, intptr_t stride );
98 
99     void (*ssd_nv12_core)( pixel *pixuv1, intptr_t stride1,
100                            pixel *pixuv2, intptr_t stride2, int width, int height,
101                            uint64_t *ssd_u, uint64_t *ssd_v );
102     void (*ssim_4x4x2_core)( const pixel *pix1, intptr_t stride1,
103                              const pixel *pix2, intptr_t stride2, int sums[2][4] );
104     float (*ssim_end4)( int sum0[5][4], int sum1[5][4], int width );
105 
106     /* multiple parallel calls to cmp. */
107     x264_pixel_cmp_x3_t sad_x3[7];
108     x264_pixel_cmp_x4_t sad_x4[7];
109     x264_pixel_cmp_x3_t satd_x3[7];
110     x264_pixel_cmp_x4_t satd_x4[7];
111 
112     /* abs-diff-sum for successive elimination.
113      * may round width up to a multiple of 16. */
114     int (*ads[7])( int enc_dc[4], uint16_t *sums, int delta,
115                    uint16_t *cost_mvx, int16_t *mvs, int width, int thresh );
116 
117     /* calculate satd or sad of V, H, and DC modes. */
118     void (*intra_mbcmp_x3_16x16)( pixel *fenc, pixel *fdec, int res[3] );
119     void (*intra_satd_x3_16x16) ( pixel *fenc, pixel *fdec, int res[3] );
120     void (*intra_sad_x3_16x16)  ( pixel *fenc, pixel *fdec, int res[3] );
121     void (*intra_mbcmp_x3_4x4)  ( pixel *fenc, pixel *fdec, int res[3] );
122     void (*intra_satd_x3_4x4)   ( pixel *fenc, pixel *fdec, int res[3] );
123     void (*intra_sad_x3_4x4)    ( pixel *fenc, pixel *fdec, int res[3] );
124     void (*intra_mbcmp_x3_chroma)( pixel *fenc, pixel *fdec, int res[3] );
125     void (*intra_satd_x3_chroma) ( pixel *fenc, pixel *fdec, int res[3] );
126     void (*intra_sad_x3_chroma)  ( pixel *fenc, pixel *fdec, int res[3] );
127     void (*intra_mbcmp_x3_8x16c) ( pixel *fenc, pixel *fdec, int res[3] );
128     void (*intra_satd_x3_8x16c)  ( pixel *fenc, pixel *fdec, int res[3] );
129     void (*intra_sad_x3_8x16c)   ( pixel *fenc, pixel *fdec, int res[3] );
130     void (*intra_mbcmp_x3_8x8c)  ( pixel *fenc, pixel *fdec, int res[3] );
131     void (*intra_satd_x3_8x8c)   ( pixel *fenc, pixel *fdec, int res[3] );
132     void (*intra_sad_x3_8x8c)    ( pixel *fenc, pixel *fdec, int res[3] );
133     void (*intra_mbcmp_x3_8x8)  ( pixel *fenc, pixel edge[36], int res[3] );
134     void (*intra_sa8d_x3_8x8)   ( pixel *fenc, pixel edge[36], int res[3] );
135     void (*intra_sad_x3_8x8)    ( pixel *fenc, pixel edge[36], int res[3] );
136     /* find minimum satd or sad of all modes, and set fdec.
137      * may be NULL, in which case just use pred+satd instead. */
138     int (*intra_mbcmp_x9_4x4)( pixel *fenc, pixel *fdec, uint16_t *bitcosts );
139     int (*intra_satd_x9_4x4) ( pixel *fenc, pixel *fdec, uint16_t *bitcosts );
140     int (*intra_sad_x9_4x4)  ( pixel *fenc, pixel *fdec, uint16_t *bitcosts );
141     int (*intra_mbcmp_x9_8x8)( pixel *fenc, pixel *fdec, pixel edge[36], uint16_t *bitcosts, uint16_t *satds );
142     int (*intra_sa8d_x9_8x8) ( pixel *fenc, pixel *fdec, pixel edge[36], uint16_t *bitcosts, uint16_t *satds );
143     int (*intra_sad_x9_8x8)  ( pixel *fenc, pixel *fdec, pixel edge[36], uint16_t *bitcosts, uint16_t *satds );
144 } x264_pixel_function_t;
145 
146 #define x264_pixel_init x264_template(pixel_init)
147 void x264_pixel_init( uint32_t cpu, x264_pixel_function_t *pixf );
148 #define x264_pixel_ssd_nv12 x264_template(pixel_ssd_nv12)
149 void x264_pixel_ssd_nv12   ( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2,
150                              int i_width, int i_height, uint64_t *ssd_u, uint64_t *ssd_v );
151 #define x264_pixel_ssd_wxh x264_template(pixel_ssd_wxh)
152 uint64_t x264_pixel_ssd_wxh( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2,
153                              int i_width, int i_height );
154 #define x264_pixel_ssim_wxh x264_template(pixel_ssim_wxh)
155 float x264_pixel_ssim_wxh  ( x264_pixel_function_t *pf, pixel *pix1, intptr_t i_pix1, pixel *pix2, intptr_t i_pix2,
156                              int i_width, int i_height, void *buf, int *cnt );
157 #define x264_field_vsad x264_template(field_vsad)
158 int x264_field_vsad( x264_t *h, int mb_x, int mb_y );
159 
160 #endif
161