+ int intra_cost = intra_costs[i];
+ int inter_cost = X264_MIN(intra_costs[i], inter_costs[i] & LOWRES_COST_MASK);
+ float propagate_intra = intra_cost * inv_qscales[i];
+ float propagate_amount = propagate_in[i] + propagate_intra*fps;
+ float propagate_num = intra_cost - inter_cost;
+ float propagate_denom = intra_cost;
+ dst[i] = X264_MIN((int)(propagate_amount * propagate_num / propagate_denom + 0.5f), 32767);
+ }
+}
+
+static void mbtree_propagate_list( x264_t *h, uint16_t *ref_costs, int16_t (*mvs)[2],
+ int16_t *propagate_amount, uint16_t *lowres_costs,
+ int bipred_weight, int mb_y, int len, int list )
+{
+ unsigned stride = h->mb.i_mb_stride;
+ unsigned width = h->mb.i_mb_width;
+ unsigned height = h->mb.i_mb_height;
+
+ for( unsigned i = 0; i < len; i++ )
+ {
+#define CLIP_ADD(s,x) (s) = X264_MIN((s)+(x),(1<<15)-1)
+ int lists_used = lowres_costs[i]>>LOWRES_COST_SHIFT;
+
+ if( !(lists_used & (1 << list)) )
+ continue;
+
+ int listamount = propagate_amount[i];
+ /* Apply bipred weighting. */
+ if( lists_used == 3 )
+ listamount = (listamount * bipred_weight + 32) >> 6;
+
+ /* Early termination for simple case of mv0. */
+ if( !M32( mvs[i] ) )
+ {
+ CLIP_ADD( ref_costs[mb_y*stride + i], listamount );
+ continue;
+ }
+
+ int x = mvs[i][0];
+ int y = mvs[i][1];
+ unsigned mbx = (x>>5)+i;
+ unsigned mby = (y>>5)+mb_y;
+ unsigned idx0 = mbx + mby * stride;
+ unsigned idx2 = idx0 + stride;
+ x &= 31;
+ y &= 31;
+ int idx0weight = (32-y)*(32-x);
+ int idx1weight = (32-y)*x;
+ int idx2weight = y*(32-x);
+ int idx3weight = y*x;
+ idx0weight = (idx0weight * listamount + 512) >> 10;
+ idx1weight = (idx1weight * listamount + 512) >> 10;
+ idx2weight = (idx2weight * listamount + 512) >> 10;
+ idx3weight = (idx3weight * listamount + 512) >> 10;
+
+ if( mbx < width-1 && mby < height-1 )
+ {
+ CLIP_ADD( ref_costs[idx0+0], idx0weight );
+ CLIP_ADD( ref_costs[idx0+1], idx1weight );
+ CLIP_ADD( ref_costs[idx2+0], idx2weight );
+ CLIP_ADD( ref_costs[idx2+1], idx3weight );
+ }
+ else
+ {
+ /* Note: this takes advantage of unsigned representation to
+ * catch negative mbx/mby. */
+ if( mby < height )
+ {
+ if( mbx < width )
+ CLIP_ADD( ref_costs[idx0+0], idx0weight );
+ if( mbx+1 < width )
+ CLIP_ADD( ref_costs[idx0+1], idx1weight );
+ }
+ if( mby+1 < height )
+ {
+ if( mbx < width )
+ CLIP_ADD( ref_costs[idx2+0], idx2weight );
+ if( mbx+1 < width )
+ CLIP_ADD( ref_costs[idx2+1], idx3weight );
+ }
+ }