private void searchRefImage(int block)
	{
		RGB[][] tg = targetMacroBlocks.get(block);
		double max = Double.MAX_VALUE;
		int mx = 0;
		int my = 0;

		//center of macroblock
		int xpos = ((block % h) * 16) + 8;
		int ypos = ((block / h) * 16) + 8;

		for(int y = -p; y <= p; y++)
		{
			for(int x = -p; x <= p; x++)
			{
				if(xpos + x + 7 < rfr.getW() && xpos + x - 8 >= 0 && ypos + y + 7 < rfr.getH() && ypos + y - 8 >= 0)//constrain
				{
					int m = 16;
					int n = 16;
					int xoff = xpos + x - 8;
					int yoff = ypos + y - 8;
					double agg = 0.0;

					for(int yy = 0; yy < n; yy++)
					{
						for(int xx = 0; xx < m; xx++)
						{
							int[] rgb = new int[3];
							rfr.getPixel(xoff + xx, yoff + yy, rgb);
							agg += Math.pow(getGrayPixel(rgb) - getGrayPixel(tg[xx][yy].getRGB()), 2); 
						}
					}
					agg *= 1.0/(m*n);

					if(agg < max)
					{
						max = agg;
						mx = xoff;
						my = yoff;
					}	
				}
			}
		}

		RGB[][] best = getBlockFromRef(mx, my);
		bestBlocks.add(best);
		int dx = 0;
		int dy = 0;
		
		dx = xpos - (mx + 8);
		dy = ypos - (my + 8);
		
		int[][] pair = {{dx}, {dy}};
		mvs.add(pair);
		
	}