3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Image management functions - |
* - Image management functions - |
5 |
* |
* |
6 |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
* Copyright(C) 2001-2004 Peter Ross <pross@xvid.org> |
7 |
* |
* |
8 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
26 |
#include <stdlib.h> |
#include <stdlib.h> |
27 |
#include <string.h> /* memcpy, memset */ |
#include <string.h> /* memcpy, memset */ |
28 |
#include <math.h> |
#include <math.h> |
|
|
|
29 |
#include "../portab.h" |
#include "../portab.h" |
30 |
#include "../global.h" /* XVID_CSP_XXX's */ |
#include "../global.h" /* XVID_CSP_XXX's */ |
31 |
#include "../xvid.h" /* XVID_CSP_XXX's */ |
#include "../xvid.h" /* XVID_CSP_XXX's */ |
32 |
#include "image.h" |
#include "image.h" |
33 |
#include "colorspace.h" |
#include "colorspace.h" |
34 |
#include "interpolate8x8.h" |
#include "interpolate8x8.h" |
|
#include "reduced.h" |
|
35 |
#include "../utils/mem_align.h" |
#include "../utils/mem_align.h" |
36 |
|
#include "../motion/sad.h" |
37 |
|
|
38 |
#include "font.h" /* XXX: remove later */ |
#include "font.h" /* XXX: remove later */ |
39 |
|
|
126 |
memcpy(image1->v, image2->v, edged_width * height / 4); |
memcpy(image1->v, image2->v, edged_width * height / 4); |
127 |
} |
} |
128 |
|
|
129 |
|
/* setedges bug was fixed in this BS version */ |
130 |
|
#define SETEDGES_BUG_BEFORE 18 |
131 |
|
|
132 |
void |
void |
133 |
image_setedges(IMAGE * image, |
image_setedges(IMAGE * image, |
134 |
uint32_t edged_width, |
uint32_t edged_width, |
135 |
uint32_t edged_height, |
uint32_t edged_height, |
136 |
uint32_t width, |
uint32_t width, |
137 |
uint32_t height) |
uint32_t height, |
138 |
|
int bs_version) |
139 |
{ |
{ |
140 |
const uint32_t edged_width2 = edged_width / 2; |
const uint32_t edged_width2 = edged_width / 2; |
141 |
uint32_t width2; |
uint32_t width2; |
143 |
uint8_t *dst; |
uint8_t *dst; |
144 |
uint8_t *src; |
uint8_t *src; |
145 |
|
|
|
|
|
146 |
dst = image->y - (EDGE_SIZE + EDGE_SIZE * edged_width); |
dst = image->y - (EDGE_SIZE + EDGE_SIZE * edged_width); |
147 |
src = image->y; |
src = image->y; |
148 |
|
|
149 |
/* According to the Standard Clause 7.6.4, padding is done starting at 16 |
/* According to the Standard Clause 7.6.4, padding is done starting at 16 |
150 |
* pixel width and height multiples */ |
* pixel width and height multiples. This was not respected in old xvids */ |
151 |
|
if (bs_version == 0 || bs_version >= SETEDGES_BUG_BEFORE) { |
152 |
width = (width+15)&~15; |
width = (width+15)&~15; |
153 |
height = (height+15)&~15; |
height = (height+15)&~15; |
154 |
|
} |
155 |
|
|
156 |
width2 = width/2; |
width2 = width/2; |
157 |
|
|
158 |
for (i = 0; i < EDGE_SIZE; i++) { |
for (i = 0; i < EDGE_SIZE; i++) { |
621 |
|
|
622 |
case XVID_CSP_YVYU: /* u/v swapped */ |
case XVID_CSP_YVYU: /* u/v swapped */ |
623 |
safe_packed_conv( |
safe_packed_conv( |
624 |
src[0], src_stride[0], image->y, image->v, image->y, |
src[0], src_stride[0], image->y, image->v, image->u, |
625 |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
edged_width, edged_width2, width, height, (csp & XVID_CSP_VFLIP), |
626 |
interlacing?yuyvi_to_yv12 :yuyv_to_yv12, |
interlacing?yuyvi_to_yv12 :yuyv_to_yv12, |
627 |
interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2); |
interlacing?yuyvi_to_yv12_c:yuyv_to_yv12_c, 2); |
635 |
interlacing?uyvyi_to_yv12_c:uyvy_to_yv12_c, 2); |
interlacing?uyvyi_to_yv12_c:uyvy_to_yv12_c, 2); |
636 |
break; |
break; |
637 |
|
|
638 |
case XVID_CSP_YV12: /* YCrCb == internal colorspace for MPEG */ |
case XVID_CSP_I420: /* YCbCr == YUV == internal colorspace for MPEG */ |
639 |
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
yv12_to_yv12(image->y, image->u, image->v, edged_width, edged_width2, |
640 |
src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
641 |
src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); |
src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); |
642 |
break; |
break; |
643 |
|
|
644 |
case XVID_CSP_I420: /* YCbCr == U and V plane swapped */ |
case XVID_CSP_YV12: /* YCrCb == YVA == U and V plane swapped */ |
645 |
yv12_to_yv12(image->y, image->v, image->u, edged_width, edged_width2, |
yv12_to_yv12(image->y, image->v, image->u, edged_width, edged_width2, |
646 |
src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
src[0], src[0] + src_stride[0]*height, src[0] + src_stride[0]*height + (src_stride[0]/2)*height2, |
647 |
src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); |
src_stride[0], src_stride[0]/2, width, height, (csp & XVID_CSP_VFLIP)); |
811 |
interlacing?yv12_to_uyvyi_c:yv12_to_uyvy_c, 2); |
interlacing?yv12_to_uyvyi_c:yv12_to_uyvy_c, 2); |
812 |
return 0; |
return 0; |
813 |
|
|
814 |
case XVID_CSP_YV12: /* YCbCr == internal colorspace for MPEG */ |
case XVID_CSP_I420: /* YCbCr == YUV == internal colorspace for MPEG */ |
815 |
yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
816 |
dst_stride[0], dst_stride[0]/2, |
dst_stride[0], dst_stride[0]/2, |
817 |
image->y, image->u, image->v, edged_width, edged_width2, |
image->y, image->u, image->v, edged_width, edged_width2, |
818 |
width, height, (csp & XVID_CSP_VFLIP)); |
width, height, (csp & XVID_CSP_VFLIP)); |
819 |
return 0; |
return 0; |
820 |
|
|
821 |
case XVID_CSP_I420: /* YCrCb == U and V plane swapped */ |
case XVID_CSP_YV12: /* YCrCb == YVU == U and V plane swapped */ |
822 |
yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
yv12_to_yv12(dst[0], dst[0] + dst_stride[0]*height, dst[0] + dst_stride[0]*height + (dst_stride[0]/2)*height2, |
823 |
dst_stride[0], dst_stride[0]/2, |
dst_stride[0], dst_stride[0]/2, |
824 |
image->y, image->v, image->u, edged_width, edged_width2, |
image->y, image->v, image->u, edged_width, edged_width2, |
898 |
uint16_t width, |
uint16_t width, |
899 |
uint16_t height) |
uint16_t height) |
900 |
{ |
{ |
901 |
int diff, x, y; |
int y, bwidth, bheight; |
902 |
long sse=0; |
long sse=0; |
903 |
|
|
904 |
for (y = 0; y < height; y++) { |
bwidth = width & (~0x07); |
905 |
|
bheight = height & (~0x07); |
906 |
|
|
907 |
|
/* Compute the 8x8 integer part */ |
908 |
|
for (y = 0; y<bheight; y += 8) { |
909 |
|
int x; |
910 |
|
|
911 |
|
/* Compute sse for the band */ |
912 |
|
for (x = 0; x<bwidth; x += 8) |
913 |
|
sse += sse8_8bit(orig + x, recon + x, stride); |
914 |
|
|
915 |
|
/* remaining pixels of the 8 pixels high band */ |
916 |
|
for (x = bwidth; x < width; x++) { |
917 |
|
int diff; |
918 |
|
diff = *(orig + 0*stride + x) - *(recon + 0*stride + x); |
919 |
|
sse += diff * diff; |
920 |
|
diff = *(orig + 1*stride + x) - *(recon + 1*stride + x); |
921 |
|
sse += diff * diff; |
922 |
|
diff = *(orig + 2*stride + x) - *(recon + 2*stride + x); |
923 |
|
sse += diff * diff; |
924 |
|
diff = *(orig + 3*stride + x) - *(recon + 3*stride + x); |
925 |
|
sse += diff * diff; |
926 |
|
diff = *(orig + 4*stride + x) - *(recon + 4*stride + x); |
927 |
|
sse += diff * diff; |
928 |
|
diff = *(orig + 5*stride + x) - *(recon + 5*stride + x); |
929 |
|
sse += diff * diff; |
930 |
|
diff = *(orig + 6*stride + x) - *(recon + 6*stride + x); |
931 |
|
sse += diff * diff; |
932 |
|
diff = *(orig + 7*stride + x) - *(recon + 7*stride + x); |
933 |
|
sse += diff * diff; |
934 |
|
} |
935 |
|
|
936 |
|
orig += 8*stride; |
937 |
|
recon += 8*stride; |
938 |
|
} |
939 |
|
|
940 |
|
/* Compute the down rectangle sse */ |
941 |
|
for (y = bheight; y < height; y++) { |
942 |
|
int x; |
943 |
for (x = 0; x < width; x++) { |
for (x = 0; x < width; x++) { |
944 |
|
int diff; |
945 |
diff = *(orig + x) - *(recon + x); |
diff = *(orig + x) - *(recon + x); |
946 |
sse += diff * diff; |
sse += diff * diff; |
947 |
} |
} |
948 |
orig += stride; |
orig += stride; |
949 |
recon += stride; |
recon += stride; |
950 |
} |
} |
951 |
return sse; |
|
952 |
|
return (sse); |
953 |
} |
} |
954 |
|
|
955 |
#if 0 |
#if 0 |
1137 |
p += edged_width/2; |
p += edged_width/2; |
1138 |
} |
} |
1139 |
} |
} |
|
|
|
|
|
|
|
/* reduced resolution deblocking filter |
|
|
block = block size (16=rrv, 8=full resolution) |
|
|
flags = XVID_DEC_YDEBLOCK|XVID_DEC_UVDEBLOCK |
|
|
*/ |
|
|
void |
|
|
image_deblock_rrv(IMAGE * img, int edged_width, |
|
|
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
|
|
int block, int flags) |
|
|
{ |
|
|
const int edged_width2 = edged_width /2; |
|
|
const int nblocks = block / 8; /* skals code uses 8pixel block uints */ |
|
|
int i,j; |
|
|
|
|
|
/* luma: j,i in block units */ |
|
|
|
|
|
for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
|
|
for (i = 0; i < mb_width*2; i++) |
|
|
{ |
|
|
if (mbs[(j-1)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED || |
|
|
mbs[(j+0)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED) |
|
|
{ |
|
|
hfilter_31(img->y + (j*block - 1)*edged_width + i*block, |
|
|
img->y + (j*block + 0)*edged_width + i*block, nblocks); |
|
|
} |
|
|
} |
|
|
|
|
|
for (j = 0; j < mb_height*2; j++) /* vertical deblocking */ |
|
|
for (i = 1; i < mb_width*2; i++) |
|
|
{ |
|
|
if (mbs[(j/2)*mb_stride + (i-1)/2].mode != MODE_NOT_CODED || |
|
|
mbs[(j/2)*mb_stride + (i+0)/2].mode != MODE_NOT_CODED) |
|
|
{ |
|
|
vfilter_31(img->y + (j*block)*edged_width + i*block - 1, |
|
|
img->y + (j*block)*edged_width + i*block + 0, |
|
|
edged_width, nblocks); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* chroma */ |
|
|
|
|
|
for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
|
|
for (i = 0; i < mb_width; i++) |
|
|
{ |
|
|
if (mbs[(j-1)*mb_stride + i].mode != MODE_NOT_CODED || |
|
|
mbs[(j+0)*mb_stride + i].mode != MODE_NOT_CODED) |
|
|
{ |
|
|
hfilter_31(img->u + (j*block - 1)*edged_width2 + i*block, |
|
|
img->u + (j*block + 0)*edged_width2 + i*block, nblocks); |
|
|
hfilter_31(img->v + (j*block - 1)*edged_width2 + i*block, |
|
|
img->v + (j*block + 0)*edged_width2 + i*block, nblocks); |
|
|
} |
|
|
} |
|
|
|
|
|
for (j = 0; j < mb_height; j++) /* vertical deblocking */ |
|
|
for (i = 1; i < mb_width; i++) |
|
|
{ |
|
|
if (mbs[j*mb_stride + i - 1].mode != MODE_NOT_CODED || |
|
|
mbs[j*mb_stride + i + 0].mode != MODE_NOT_CODED) |
|
|
{ |
|
|
vfilter_31(img->u + (j*block)*edged_width2 + i*block - 1, |
|
|
img->u + (j*block)*edged_width2 + i*block + 0, |
|
|
edged_width2, nblocks); |
|
|
vfilter_31(img->v + (j*block)*edged_width2 + i*block - 1, |
|
|
img->v + (j*block)*edged_width2 + i*block + 0, |
|
|
edged_width2, nblocks); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|