1 /* Mode selection routines, select the least SATD cost mode for each lowres
2 * macroblock. When measuring B slices, this includes measuring the cost of
3 * three bidir modes. */
5 /* Four threads cooperatively measure 8x8 BIDIR cost with SATD */
6 int bidir_satd_8x8_ii_coop4( read_only image2d_t fenc_lowres,
8 read_only image2d_t fref0_planes,
10 read_only image2d_t fref1_planes,
16 volatile local sum2_t( *tmp )[4] = (volatile local sum2_t( * )[4])tmpp;
17 sum2_t b0, b1, b2, b3;
20 // fencpos is full-pel position of original MB
21 // qpos0 is qpel position within reference frame 0
22 // qpos1 is qpel position within reference frame 1
24 int2 fref0Apos = (int2)(qpos0.x>>2, qpos0.y>>2);
25 int hpel0A = ((qpos0.x&2)>>1) + (qpos0.y&2);
27 int2 qpos0B = (int2)qpos0 + (int2)(((qpos0.x&1)<<1), ((qpos0.y&1)<<1));
28 int2 fref0Bpos = (int2)(qpos0B.x>>2, qpos0B.y>>2);
29 int hpel0B = ((qpos0B.x&2)>>1) + (qpos0B.y&2);
31 int2 fref1Apos = (int2)(qpos1.x>>2, qpos1.y>>2);
32 int hpel1A = ((qpos1.x&2)>>1) + (qpos1.y&2);
34 int2 qpos1B = (int2)qpos1 + (int2)(((qpos1.x&1)<<1), ((qpos1.y&1)<<1));
35 int2 fref1Bpos = (int2)(qpos1B.x>>2, qpos1B.y>>2);
36 int hpel1B = ((qpos1B.x&2)>>1) + (qpos1B.y&2);
38 uint mask_shift0A = 8 * hpel0A, mask_shift0B = 8 * hpel0B;
39 uint mask_shift1A = 8 * hpel1A, mask_shift1B = 8 * hpel1B;
44 const int weight2 = 64 - weight;
46 #define READ_BIDIR_DIFF( OUT, X )\
47 enc = read_imageui( fenc_lowres, sampler, fencpos + (int2)(X, idx) ).s0;\
48 vA = (read_imageui( fref0_planes, sampler, fref0Apos + (int2)(X, idx) ).s0 >> mask_shift0A) & 0xFF;\
49 vB = (read_imageui( fref0_planes, sampler, fref0Bpos + (int2)(X, idx) ).s0 >> mask_shift0B) & 0xFF;\
50 ref0 = rhadd( vA, vB );\
51 vA = (read_imageui( fref1_planes, sampler, fref1Apos + (int2)(X, idx) ).s0 >> mask_shift1A) & 0xFF;\
52 vB = (read_imageui( fref1_planes, sampler, fref1Bpos + (int2)(X, idx) ).s0 >> mask_shift1B) & 0xFF;\
53 ref1 = rhadd( vA, vB );\
54 OUT = enc - ((ref0 * weight + ref1 * weight2 + (1 << 5)) >> 6);
56 #define READ_DIFF_EX( OUT, a, b )\
57 READ_BIDIR_DIFF( a0, a );\
58 READ_BIDIR_DIFF( a1, b );\
59 OUT = a0 + (a1<<BITS_PER_SUM);
61 #define ROW_8x4_SATD( a, b, c )\
67 READ_DIFF_EX( b0, 0, 4 );\
68 READ_DIFF_EX( b1, 1, 5 );\
69 READ_DIFF_EX( b2, 2, 6 );\
70 READ_DIFF_EX( b3, 3, 7 );\
71 HADAMARD4( tmp[idx][0], tmp[idx][1], tmp[idx][2], tmp[idx][3], b0, b1, b2, b3 );\
72 HADAMARD4( b0, b1, b2, b3, tmp[0][idx], tmp[1][idx], tmp[2][idx], tmp[3][idx] );\
73 sum += abs2( b0 ) + abs2( b1 ) + abs2( b2 ) + abs2( b3 );
75 ROW_8x4_SATD( 0, 0, 0 );
76 ROW_8x4_SATD( 4, 4, 4 );
78 #undef READ_BIDIR_DIFF
82 return (((sum_t)sum) + (sum>>BITS_PER_SUM)) >> 1;
86 * mode selection - pick the least cost partition type for each 8x8 macroblock.
87 * Intra, list0 or list1. When measuring a B slice, also test three bidir
90 * fenc_lowres_mvs[0|1] and fenc_lowres_mv_costs[0|1] are large buffers that
91 * hold many frames worth of motion vectors. We must offset into the correct
92 * location for this frame's vectors:
94 * CPU equivalent: fenc->lowres_mvs[0][b - p0 - 1]
95 * GPU equivalent: fenc_lowres_mvs0[(b - p0 - 1) * mb_count]
97 * global launch dimensions for P slice estimate: [mb_width, mb_height]
98 * global launch dimensions for B slice estimate: [mb_width * 4, mb_height]
100 kernel void mode_selection( read_only image2d_t fenc_lowres,
101 read_only image2d_t fref0_planes,
102 read_only image2d_t fref1_planes,
103 const global short2 *fenc_lowres_mvs0,
104 const global short2 *fenc_lowres_mvs1,
105 const global short2 *fref1_lowres_mvs0,
106 const global int16_t *fenc_lowres_mv_costs0,
107 const global int16_t *fenc_lowres_mv_costs1,
108 const global uint16_t *fenc_intra_cost,
109 global uint16_t *lowres_costs,
110 global int *frame_stats,
111 local int16_t *cost_local,
112 local sum2_t *satd_local,
115 int dist_scale_factor,
121 int mb_x = get_global_id( 0 );
122 int b_bidir = b < p1;
125 /* when mode_selection is run for B frames, it must perform BIDIR SATD
126 * measurements, so it is launched with four times as many threads in
127 * order to spread the work around more of the GPU. And it can add
128 * padding threads in the X direction. */
130 if( mb_x >= mb_width )
133 int mb_y = get_global_id( 1 );
134 int mb_height = get_global_size( 1 );
135 int mb_count = mb_width * mb_height;
136 int mb_xy = mb_x + mb_y * mb_width;
138 /* Initialize int frame_stats[4] for next kernel (sum_inter_cost) */
139 if( mb_x < 4 && mb_y == 0 )
140 frame_stats[mb_x] = 0;
142 int bcost = COST_MAX;
147 int icost = fenc_intra_cost[mb_xy];
148 COPY2_IF_LT( bcost, icost, list_used, 0 );
152 int mv_cost0 = fenc_lowres_mv_costs0[(b - p0 - 1) * mb_count + mb_xy];
153 COPY2_IF_LT( bcost, mv_cost0, list_used, 1 );
157 int mv_cost1 = fenc_lowres_mv_costs1[(p1 - b - 1) * mb_count + mb_xy];
158 COPY2_IF_LT( bcost, mv_cost1, list_used, 2 );
163 int2 coord = (int2)(mb_x, mb_y) << 3;
164 int mb_i = get_global_id( 0 ) & 3;
165 int mb_in_group = get_local_id( 1 ) * (get_local_size( 0 ) >> 2) + (get_local_id( 0 ) >> 2);
166 cost_local += mb_in_group * 4;
167 satd_local += mb_in_group * 16;
169 #define TRY_BIDIR( mv0, mv1, penalty )\
171 int2 qpos0 = (int2)((coord.x<<2) + mv0.x, (coord.y<<2) + mv0.y);\
172 int2 qpos1 = (int2)((coord.x<<2) + mv1.x, (coord.y<<2) + mv1.y);\
173 cost_local[mb_i] = bidir_satd_8x8_ii_coop4( fenc_lowres, coord, fref0_planes, qpos0, fref1_planes, qpos1, bipred_weight, satd_local, mb_i );\
174 int cost = cost_local[0] + cost_local[1] + cost_local[2] + cost_local[3];\
175 COPY2_IF_LT( bcost, penalty * lambda + cost, list_used, 3 );\
178 /* temporal prediction */
180 short2 mvr = fref1_lowres_mvs0[mb_xy];
181 dmv0 = (mvr * (short) dist_scale_factor + (short) 128) >> (short) 8;
183 TRY_BIDIR( dmv0, dmv1, 0 )
185 if( as_uint( dmv0 ) || as_uint( dmv1 ) )
187 /* B-direct prediction */
189 TRY_BIDIR( dmv0, dmv1, 0 );
192 /* L0+L1 prediction */
193 dmv0 = fenc_lowres_mvs0[(b - p0 - 1) * mb_count + mb_xy];
194 dmv1 = fenc_lowres_mvs1[(p1 - b - 1) * mb_count + mb_xy];
195 TRY_BIDIR( dmv0, dmv1, 5 );
199 lowres_costs[mb_xy] = min( bcost, LOWRES_COST_MASK ) + (list_used << LOWRES_COST_SHIFT);
203 * parallel sum inter costs
205 * global launch dimensions: [256, mb_height]
207 kernel void sum_inter_cost( const global uint16_t *fenc_lowres_costs,
208 const global uint16_t *inv_qscale_factor,
209 global int *fenc_row_satds,
210 global int *frame_stats,
217 int y = get_global_id( 1 );
218 int mb_height = get_global_size( 1 );
225 for( int x = get_global_id( 0 ); x < mb_width; x += get_global_size( 0 ))
227 int mb_xy = x + y * mb_width;
228 int cost = fenc_lowres_costs[mb_xy] & LOWRES_COST_MASK;
229 int list = fenc_lowres_costs[mb_xy] >> LOWRES_COST_SHIFT;
230 int b_frame_score_mb = (x > 0 && x < mb_width - 1 && y > 0 && y < mb_height - 1) || mb_width <= 2 || mb_height <= 2;
232 if( list == 0 && b_frame_score_mb )
235 int cost_aq = (cost * inv_qscale_factor[mb_xy] + 128) >> 8;
237 row_satds += cost_aq;
239 if( b_frame_score_mb )
242 cost_est_aq += cost_aq;
246 local int buffer[256];
247 int x = get_global_id( 0 );
249 row_satds = parallel_sum( row_satds, x, buffer );
250 cost_est = parallel_sum( cost_est, x, buffer );
251 cost_est_aq = parallel_sum( cost_est_aq, x, buffer );
252 intra_mbs = parallel_sum( intra_mbs, x, buffer );
255 // Use floating point math to avoid 32bit integer overflow conditions
256 cost_est = (int)((float)cost_est * 100.0f / (120.0f + (float)bframe_bias));
258 if( get_global_id( 0 ) == 0 )
260 fenc_row_satds[y] = row_satds;
261 atomic_add( frame_stats + COST_EST, cost_est );
262 atomic_add( frame_stats + COST_EST_AQ, cost_est_aq );
263 atomic_add( frame_stats + INTRA_MBS, intra_mbs );