YUV 4:2:0 から YUV 4:2:2 に変換する正しい方法は、6 タップ FIR フィルターを使用することです。
この src は、ここで見つけることができる mpeg2 参照実装から取得されますhttp://www.mpeg.org/MPEG/video/mssg-free-mpeg-software.html
「mpegv12.zip」ファイルに移動します
C での実装:
/* vertical 1:2 interpolation filter */
static void conv420to422(unsigned char* src, unsigned char* dst)
{
int w, h, i, j, j2;
int jm6, jm5, jm4, jm3, jm2, jm1, jp1, jp2, jp3, jp4, jp5, jp6, jp7;
w = 352>>1;
h = 288>>1;
printf("hello \n");
if (1)
{
/* intra frame */
for (i=0; i<w; i++)
{
for (j=0; j<h; j++)
{
//printf("%d,%d\n", i, j);
j2 = j<<1;
jm3 = (j<3) ? 0 : j-3;
jm2 = (j<2) ? 0 : j-2;
jm1 = (j<1) ? 0 : j-1;
jp1 = (j<h-1) ? j+1 : h-1;
jp2 = (j<h-2) ? j+2 : h-1;
jp3 = (j<h-3) ? j+3 : h-1;
/* FIR filter coefficients (*256): 5 -21 70 228 -37 11 */
/* New FIR filter coefficients (*256): 3 -16 67 227 -32 7 */
dst[w*j2] = Clip[(int)( 3*src[w*jm3]
-16*src[w*jm2]
+67*src[w*jm1]
+227*src[w*j]
-32*src[w*jp1]
+7*src[w*jp2]+128)>>8];
dst[w*(j2+1)] = Clip[(int)( 3*src[w*jp3]
-16*src[w*jp2]
+67*src[w*jp1]
+227*src[w*j]
-32*src[w*jm1]
+7*src[w*jm2]+128)>>8];
}
src++;
dst++;
}
}
else
{
/* intra field */
for (i=0; i<w; i++)
{
for (j=0; j<h; j+=2)
{
j2 = j<<1;
/* top field */
jm6 = (j<6) ? 0 : j-6;
jm4 = (j<4) ? 0 : j-4;
jm2 = (j<2) ? 0 : j-2;
jp2 = (j<h-2) ? j+2 : h-2;
jp4 = (j<h-4) ? j+4 : h-2;
jp6 = (j<h-6) ? j+6 : h-2;
/* Polyphase FIR filter coefficients (*256): 2 -10 35 242 -18 5 */
/* New polyphase FIR filter coefficients (*256): 1 -7 30 248 -21 5 */
dst[w*j2] = Clip[(int)( 1*src[w*jm6]
-7*src[w*jm4]
+30*src[w*jm2]
+248*src[w*j]
-21*src[w*jp2]
+5*src[w*jp4]+128)>>8];
/* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
/* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
dst[w*(j2+2)] = Clip[(int)( 7*src[w*jm4]
-35*src[w*jm2]
+194*src[w*j]
+110*src[w*jp2]
-24*src[w*jp4]
+4*src[w*jp6]+128)>>8];
/* bottom field */
jm5 = (j<5) ? 1 : j-5;
jm3 = (j<3) ? 1 : j-3;
jm1 = (j<1) ? 1 : j-1;
jp1 = (j<h-1) ? j+1 : h-1;
jp3 = (j<h-3) ? j+3 : h-1;
jp5 = (j<h-5) ? j+5 : h-1;
jp7 = (j<h-7) ? j+7 : h-1;
/* Polyphase FIR filter coefficients (*256): 11 -38 192 113 -30 8 */
/* New polyphase FIR filter coefficients (*256):7 -35 194 110 -24 4 */
dst[w*(j2+1)] = Clip[(int)( 7*src[w*jp5]
-35*src[w*jp3]
+194*src[w*jp1]
+110*src[w*jm1]
-24*src[w*jm3]
+4*src[w*jm5]+128)>>8];
dst[w*(j2+3)] = Clip[(int)( 1*src[w*jp7]
-7*src[w*jp5]
+30*src[w*jp3]
+248*src[w*jp1]
-21*src[w*jm1]
+5*src[w*jm3]+128)>>8];
}
src++;
dst++;
}
}
}
そしてPythonで:
def conv420to422(src, dst):
"""420 to 422 - vertical 1:2 interpolation filter """
width = 352 # 352
height = 288 # 288
w = width >> 1 # 176
h = height >> 1 # 144
n = 0
k = 0
for i in range(0, w):
for j in range(0, h):
j2 = j<<1
jm3 = 0 if (j < 3) else j - 3
jm2 = 0 if (j < 2) else j - 2
jm1 = 0 if (j < 1) else j - 1
jp1 = j + 1 if (j < h - 1) else h - 1
jp2 = j + 2 if (j < h - 2) else h - 1
jp3 = j + 3 if (j < h - 3) else h - 1
a = (3*src[n+w*jm3]-16*src[n+w*jm2]+67*src[n+w*jm1]+227*src[n+w*j]-32*src[n+w*jp1]+7*src[n+w*jp2]+128)>>8
dst[k+w*j2] = clip(a)
b = (3*src[n+w*jp3]-16*src[n+w*jp2]+67*src[n+w*jp1]+227*src[n+w*j]-32*src[n+w*jm1]+7*src[n+w*jm2]+128)>>8
dst[k+w*(j2+1)] = clip(b)
n += 1
k += 1
return dst