In typical bidirectional motion compensation, a prediction block of the current block is generated using the pixels in the reference block. On the other hand, to use a mask, access to pixels other than the pixels in the reference block should be allowed. For example, the mask for the pixel at the top leftmost position (position (0, 0)) of a reference block as shown in FIG. 6(a) includes pixels located at positions outside the reference block. In order to maintain the same memory access as in typical bidirectional motion compensation and to reduce the computational complexity of the BIO, I(k), Ix(k), and Iy(k) of pixels outside the reference block which are positioned within the mask may be padded with the corresponding values of the closest pixel in the reference block. For example, as shown in FIG. 6(b), when the size of the mask is 5×5, I(k), Ix(k), and Iy(k) of external pixels located above the reference block may be padded with I(k), Ix(k), and Iy(k) of the pixels in the top row of the reference block. I(k), Ix(k), and Iy(k) of external pixels on the left side of the reference block may be padded with I(k), Ix(k), and Iy(k) of the pixels in the leftmost column of the reference block.