package org.jcodec.codecs.mpeg12;
import static org.jcodec.common.ArrayUtil.toByteArrayShifted;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class MPEGPredOctTest {
static byte[] padded = toByteArrayShifted(new int[] {
10, 10, 10, 20, 30, 40, 40, 40, 40,
10, 10, 10, 20, 30, 40, 40, 40, 40,
10, 10, 10, 20, 30, 40, 40, 40, 40,
50, 50, 50, 60, 70, 80, 80, 80, 80,
90, 90, 90, 100, 110, 120, 120, 120, 120,
130, 130, 130, 140, 150, 160, 160, 160, 160,
130, 130, 130, 140, 150, 160, 160, 160, 160,
130, 130, 130, 140, 150, 160, 160, 160, 160,
130, 130, 130, 140, 150, 160, 160, 160, 160
});
static byte[] unpadded = toByteArrayShifted(new int[] {
10, 20, 30, 40,
50, 60, 70, 80,
90, 100, 110, 120,
130, 140, 150, 160
});
static int[][] interp = new int[][] {
/* 0-7,0 */
{ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160 },
{ 11, 21, 31, 40, 51, 61, 71, 80, 91, 101, 111, 120, 131, 141, 151, 160 },
{ 12, 22, 33, 41, 52, 62, 73, 81, 92, 102, 113, 121, 132, 142, 153, 161 },
{ 13, 24, 34, 41, 53, 64, 74, 81, 93, 104, 114, 121, 133, 144, 154, 161 },
{ 14, 25, 36, 41, 54, 65, 76, 81, 94, 105, 116, 121, 134, 145, 156, 161 },
{ 16, 26, 37, 40, 56, 66, 77, 80, 96, 106, 117, 120, 136, 146, 157, 160 },
{ 17, 28, 38, 40, 57, 68, 78, 80, 97, 108, 118, 120, 137, 148, 158, 160 },
{ 19, 29, 39, 40, 59, 69, 79, 80, 99, 109, 119, 120, 139, 149, 159, 160 },
/* 0-7,1 */
{ 13, 23, 33, 43, 55, 65, 75, 85, 95, 105, 115, 125, 132, 142, 152, 162 },
{ 14, 24, 34, 44, 56, 66, 76, 85, 96, 107, 117, 126, 133, 143, 153, 162 },
{ 15, 26, 36, 44, 57, 67, 78, 86, 97, 108, 118, 126, 134, 144, 155, 162 },
{ 16, 27, 37, 44, 58, 69, 79, 86, 98, 109, 119, 126, 135, 146, 156, 163 },
{ 17, 28, 39, 44, 59, 70, 81, 86, 100, 110, 121, 126, 136, 147, 158, 163 },
{ 19, 29, 40, 44, 61, 71, 82, 85, 101, 112, 122, 126, 138, 148, 159, 162 },
{ 20, 31, 41, 44, 62, 73, 83, 85, 103, 113, 124, 126, 139, 149, 160, 162 },
{ 22, 32, 42, 43, 64, 74, 84, 85, 104, 114, 125, 125, 141, 151, 161, 162 },
/* 0-7,2 */
{ 17, 27, 37, 47, 60, 70, 80, 90, 101, 111, 121, 131, 132, 142, 152, 162 },
{ 18, 28, 39, 48, 60, 71, 81, 90, 102, 113, 123, 132, 133, 143, 154, 163 },
{ 19, 30, 40, 48, 61, 72, 83, 90, 103, 114, 124, 132, 134, 145, 155, 163 },
{ 20, 31, 41, 48, 63, 73, 84, 90, 104, 115, 125, 132, 135, 146, 156, 163 },
{ 21, 32, 43, 48, 64, 75, 85, 90, 105, 116, 127, 132, 136, 147, 158, 163 },
{ 23, 34, 44, 48, 66, 76, 87, 90, 107, 118, 128, 132, 138, 149, 159, 163 },
{ 24, 35, 45, 48, 67, 77, 88, 90, 108, 119, 129, 132, 139, 150, 160, 163 },
{ 26, 36, 46, 47, 68, 78, 89, 90, 110, 120, 130, 131, 141, 151, 161, 162 },
/* 0-7,3 */
{ 22, 32, 42, 52, 65, 75, 85, 95, 107, 117, 127, 137, 133, 143, 153, 163 },
{ 23, 33, 43, 52, 65, 76, 86, 95, 107, 118, 128, 137, 134, 144, 154, 163 },
{ 24, 34, 45, 52, 66, 77, 88, 95, 108, 119, 129, 137, 135, 145, 156, 163 },
{ 25, 36, 46, 53, 68, 78, 89, 95, 110, 120, 131, 137, 136, 146, 157, 164 },
{ 26, 37, 48, 53, 69, 80, 90, 95, 111, 122, 132, 137, 137, 148, 159, 164 },
{ 28, 38, 49, 52, 71, 81, 92, 95, 112, 123, 134, 137, 139, 149, 160, 163 },
{ 29, 39, 50, 52, 72, 82, 93, 95, 114, 124, 135, 137, 140, 150, 161, 163 },
{ 31, 41, 51, 52, 73, 83, 94, 95, 115, 125, 136, 137, 141, 152, 162, 163 },
/* 0-7,4 */
{ 27, 37, 47, 57, 70, 80, 90, 100, 113, 123, 133, 143, 133, 143, 153, 163 },
{ 28, 38, 48, 57, 71, 81, 91, 100, 114, 124, 134, 144, 134, 144, 154, 164 },
{ 29, 39, 50, 57, 72, 82, 93, 101, 115, 126, 136, 144, 135, 146, 156, 164 },
{ 30, 41, 51, 58, 73, 84, 94, 101, 116, 127, 137, 144, 136, 147, 157, 164 },
{ 31, 42, 53, 58, 74, 85, 96, 101, 117, 128, 139, 144, 137, 148, 159, 164 },
{ 33, 43, 54, 57, 76, 86, 97, 100, 119, 129, 140, 144, 139, 149, 160, 164 },
{ 34, 44, 55, 57, 77, 88, 98, 100, 120, 131, 141, 144, 140, 151, 161, 164 },
{ 36, 46, 56, 57, 79, 89, 99, 100, 122, 132, 142, 143, 142, 152, 162, 163 },
/* 0-7,5 */
{ 33, 43, 53, 63, 75, 85, 95, 105, 118, 128, 138, 148, 132, 142, 152, 162 },
{ 34, 45, 55, 64, 76, 87, 97, 106, 119, 129, 139, 149, 133, 143, 153, 162 },
{ 35, 46, 56, 64, 77, 88, 98, 106, 120, 131, 141, 149, 134, 144, 155, 162 },
{ 36, 47, 58, 64, 78, 89, 99, 106, 121, 132, 142, 149, 135, 146, 156, 163 },
{ 38, 48, 59, 64, 80, 90, 101, 106, 122, 133, 144, 149, 136, 147, 158, 163 },
{ 39, 50, 60, 64, 81, 92, 102, 106, 124, 134, 145, 149, 138, 148, 159, 162 },
{ 41, 51, 62, 64, 83, 93, 104, 106, 125, 136, 146, 149, 139, 149, 160, 162 },
{ 42, 52, 63, 64, 84, 94, 105, 105, 127, 137, 147, 148, 141, 151, 161, 162 },
/* 0-7,6 */
{ 39, 49, 59, 69, 80, 90, 100, 110, 123, 133, 143, 153, 132, 142, 152, 162 },
{ 40, 50, 60, 69, 81, 92, 102, 111, 124, 134, 144, 153, 133, 143, 153, 162 },
{ 41, 51, 62, 69, 82, 93, 103, 111, 125, 135, 146, 153, 134, 144, 155, 162 },
{ 42, 52, 63, 69, 83, 94, 104, 111, 126, 136, 147, 154, 135, 146, 156, 163 },
{ 43, 54, 65, 70, 85, 95, 106, 111, 127, 138, 149, 154, 136, 147, 158, 163 },
{ 45, 55, 66, 69, 86, 97, 107, 111, 129, 139, 150, 153, 138, 148, 159, 162 },
{ 46, 56, 67, 69, 88, 98, 109, 111, 130, 140, 151, 153, 139, 149, 160, 162 },
{ 47, 58, 68, 69, 89, 99, 110, 110, 131, 142, 152, 153, 141, 151, 161, 162 },
/* 0-7,7 */
{ 45, 55, 65, 75, 85, 95, 105, 115, 127, 137, 147, 157, 130, 140, 150, 160 },
{ 45, 56, 66, 75, 86, 96, 106, 115, 128, 138, 148, 157, 131, 142, 152, 161 },
{ 46, 57, 68, 75, 87, 97, 108, 116, 129, 139, 150, 157, 132, 143, 153, 161 },
{ 48, 58, 69, 75, 88, 99, 109, 116, 130, 141, 151, 158, 133, 144, 154, 161 },
{ 49, 60, 70, 75, 89, 100, 111, 116, 131, 142, 153, 158, 135, 145, 156, 161 },
{ 51, 61, 72, 75, 91, 101, 112, 115, 133, 143, 154, 157, 136, 147, 157, 161 },
{ 52, 62, 73, 75, 92, 103, 113, 115, 134, 144, 155, 157, 138, 148, 159, 161 },
{ 53, 63, 74, 75, 94, 104, 114, 115, 136, 146, 156, 157, 139, 149, 160, 160 } };
public static int[] topFieldInt(int[] pix, int w, int h) {
int[] result = new int[pix.length * 2];
for (int i = 0, soff = 0, roff = 0; i < h; i++) {
for (int j = 0; j < w; j++, soff++, roff++) {
result[roff] = pix[soff];
}
for (int j = 0; j < w; j++, roff++) {
result[roff] = 0;
}
}
return result;
}
public static int[] bottomFieldInt(int[] pix, int w, int h) {
int[] result = new int[pix.length * 2];
for (int i = 0, soff = 0, roff = 0; i < h; i++) {
for (int j = 0; j < w; j++, roff++) {
result[roff] = 0;
}
for (int j = 0; j < w; j++, soff++, roff++) {
result[roff] = pix[soff];
}
}
return result;
}
public static byte[] topField(byte[] pix, int w, int h) {
byte[] result = new byte[pix.length * 2];
for (int i = 0, soff = 0, roff = 0; i < h; i++) {
for (int j = 0; j < w; j++, soff++, roff++) {
result[roff] = pix[soff];
}
for (int j = 0; j < w; j++, roff++) {
result[roff] = 0;
}
}
return result;
}
public static byte[] bottomField(byte[] pix, int w, int h) {
byte[] result = new byte[pix.length * 2];
for (int i = 0, soff = 0, roff = 0; i < h; i++) {
for (int j = 0; j < w; j++, roff++) {
result[roff] = 0;
}
for (int j = 0; j < w; j++, soff++, roff++) {
result[roff] = pix[soff];
}
}
return result;
}
@Test
public void testSafe() {
MPEGPredOct pred = new MPEGPredOct(new MPEGPred(new int[][] {}, 0, true));
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
int[] result = new int[16];
pred.predictPlane(padded, (2 << 3) + x, (2 << 3) + y, 9, 9, 0, 0, result, 0, 32, 32, 0);
Assert.assertArrayEquals("@ " + x + "," + y, interp[(y << 3) + x], result);
int[] result32 = new int[32];
pred.predictPlane(topField(padded, 9, 9), (2 << 3) + x, (2 << 3) + y, 9, 18, 1, 0, result32, 0, 32, 32,
1);
Assert.assertArrayEquals("@ " + x + "," + y + ":tff", topFieldInt(interp[(y << 3) + x], 4, 4),
result32);
Arrays.fill(result32, 0);
pred.predictPlane(bottomField(padded, 9, 9), (2 << 3) + x, (2 << 3) + y, 9, 18, 1, 1, result32, 1, 32,
32, 1);
Assert.assertArrayEquals("@ " + x + "," + y + ":bff",
bottomFieldInt(interp[(y << 3) + x], 4, 4), result32);
}
}
}
@Test
public void testUnsafe() {
MPEGPredOct pred = new MPEGPredOct(new MPEGPred(new int[][] {}, 0, true));
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
int[] result = new int[16];
pred.predictPlane(unpadded, x, y, 4, 4, 0, 0, result, 0, 32, 32, 0);
Assert.assertArrayEquals("@ " + x + "," + y, interp[(y << 3) + x], result);
int[] result32 = new int[32];
pred.predictPlane(topField(unpadded, 4, 4), x, y, 4, 8, 1, 0, result32, 0, 32, 32, 1);
Assert.assertArrayEquals("@ " + x + "," + y + ":tff", topFieldInt(interp[(y << 3) + x], 4, 4),
result32);
Arrays.fill(result32, 0);
pred.predictPlane(bottomField(unpadded, 4, 4), x, y, 4, 8, 1, 1, result32, 1, 32, 32, 1);
Assert.assertArrayEquals("@ " + x + "," + y + ":bff",
bottomFieldInt(interp[(y << 3) + x], 4, 4), result32);
}
}
}
}