59 |
#include "image.h" |
#include "image.h" |
60 |
#include "colorspace.h" |
#include "colorspace.h" |
61 |
#include "interpolate8x8.h" |
#include "interpolate8x8.h" |
62 |
|
#include "reduced.h" |
63 |
#include "../divx4.h" |
#include "../divx4.h" |
64 |
#include "../utils/mem_align.h" |
#include "../utils/mem_align.h" |
65 |
|
|
324 |
h_ptr += 8; |
h_ptr += 8; |
325 |
} |
} |
326 |
|
|
327 |
hv_ptr += EDGE_SIZE2; |
hv_ptr += EDGE_SIZE; |
328 |
h_ptr += EDGE_SIZE2; |
h_ptr += EDGE_SIZE; |
329 |
|
|
330 |
hv_ptr += stride_add; |
hv_ptr += stride_add; |
331 |
h_ptr += stride_add; |
h_ptr += stride_add; |
476 |
} |
} |
477 |
|
|
478 |
|
|
479 |
|
/* |
480 |
|
chroma optimize filter, invented by mf |
481 |
|
a chroma pixel is average from the surrounding pixels, when the |
482 |
|
correpsonding luma pixels are pure black or white. |
483 |
|
*/ |
484 |
|
|
485 |
|
void |
486 |
|
image_chroma_optimize(IMAGE * img, int width, int height, int edged_width) |
487 |
|
{ |
488 |
|
int x,y; |
489 |
|
int pixels = 0; |
490 |
|
|
491 |
|
for (y = 1; y < height/2 - 1; y++) |
492 |
|
for (x = 1; x < width/2 - 1; x++) |
493 |
|
{ |
494 |
|
#define IS_PURE(a) ((a)<=16||(a)>=235) |
495 |
|
#define IMG_Y(Y,X) img->y[(Y)*edged_width + (X)] |
496 |
|
#define IMG_U(Y,X) img->u[(Y)*edged_width/2 + (X)] |
497 |
|
#define IMG_V(Y,X) img->v[(Y)*edged_width/2 + (X)] |
498 |
|
|
499 |
|
if (IS_PURE(IMG_Y(y*2 ,x*2 )) && |
500 |
|
IS_PURE(IMG_Y(y*2 ,x*2+1)) && |
501 |
|
IS_PURE(IMG_Y(y*2+1,x*2 )) && |
502 |
|
IS_PURE(IMG_Y(y*2+1,x*2+1))) |
503 |
|
{ |
504 |
|
IMG_U(y,x) = (IMG_U(y,x-1) + IMG_U(y-1, x) + IMG_U(y, x+1) + IMG_U(y+1, x)) / 4; |
505 |
|
IMG_V(y,x) = (IMG_V(y,x-1) + IMG_V(y-1, x) + IMG_V(y, x+1) + IMG_V(y+1, x)) / 4; |
506 |
|
pixels++; |
507 |
|
} |
508 |
|
|
509 |
|
#undef IS_PURE |
510 |
|
#undef IMG_Y |
511 |
|
#undef IMG_U |
512 |
|
#undef IMG_V |
513 |
|
} |
514 |
|
|
515 |
|
DPRINTF(DPRINTF_DEBUG,"chroma_optimized_pixels = %i/%i", pixels, width*height/4); |
516 |
|
} |
517 |
|
|
518 |
|
|
519 |
|
|
520 |
|
|
521 |
|
|
522 |
/* |
/* |
523 |
perform safe packed colorspace conversion, by splitting |
perform safe packed colorspace conversion, by splitting |
677 |
|
|
678 |
case XVID_CSP_I420: |
case XVID_CSP_I420: |
679 |
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, |
680 |
src, src + width*height, src + width*height + width2*height2, |
src, src + src_stride*height, src + src_stride*height + (src_stride/2)*height2, |
681 |
width, width2, width, height, (csp & XVID_CSP_VFLIP)); |
src_stride, src_stride/2, width, height, (csp & XVID_CSP_VFLIP)); |
682 |
break |
break |
683 |
; |
; |
684 |
case XVID_CSP_YV12: /* u/v swapped */ |
case XVID_CSP_YV12: /* u/v swapped */ |
685 |
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, |
686 |
src, src + width*height, src + width*height + width2*height2, |
src, src + src_stride*height, src + src_stride*height + (src_stride/2)*height2, |
687 |
width, width2, width, height, (csp & XVID_CSP_VFLIP)); |
src_stride, src_stride/2, width, height, (csp & XVID_CSP_VFLIP)); |
688 |
break; |
break; |
689 |
|
|
690 |
case XVID_CSP_USER: |
case XVID_CSP_USER: |
765 |
int interlacing) |
int interlacing) |
766 |
{ |
{ |
767 |
const int edged_width2 = edged_width/2; |
const int edged_width2 = edged_width/2; |
|
int width2 = width/2; |
|
768 |
int height2 = height/2; |
int height2 = height/2; |
769 |
|
|
770 |
/* |
/* |
873 |
return 0; |
return 0; |
874 |
|
|
875 |
case XVID_CSP_I420: |
case XVID_CSP_I420: |
876 |
yv12_to_yv12(dst, dst + width*height, dst + width*height + width2*height2, |
yv12_to_yv12(dst, dst + dst_stride*height, dst + dst_stride*height + (dst_stride/2)*height2, |
877 |
width, width2, |
dst_stride, dst_stride/2, |
878 |
image->y, image->u, image->v, edged_width, edged_width2, |
image->y, image->u, image->v, edged_width, edged_width2, |
879 |
width, height, (csp & XVID_CSP_VFLIP)); |
width, height, (csp & XVID_CSP_VFLIP)); |
880 |
return 0; |
return 0; |
881 |
|
|
882 |
case XVID_CSP_YV12: // u,v swapped |
case XVID_CSP_YV12: // u,v swapped |
883 |
yv12_to_yv12(dst, dst + width*height, dst + width*height + width2*height2, |
yv12_to_yv12(dst, dst + dst_stride*height, dst + dst_stride*height + (dst_stride/2)*height2, |
884 |
width, width2, |
dst_stride, dst_stride/2, |
885 |
image->y, image->v, image->u, edged_width, edged_width2, |
image->y, image->v, image->u, edged_width, edged_width2, |
886 |
width, height, (csp & XVID_CSP_VFLIP)); |
width, height, (csp & XVID_CSP_VFLIP)); |
887 |
return 0; |
return 0; |
938 |
return psnr_y; |
return psnr_y; |
939 |
} |
} |
940 |
|
|
941 |
|
|
942 |
|
float sse_to_PSNR(long sse, int pixels) |
943 |
|
{ |
944 |
|
if (sse==0) |
945 |
|
return 99.99F; |
946 |
|
|
947 |
|
return 48.131F - 10*(float)log10((float)sse/(float)(pixels)); // log10(255*255)=4.8131 |
948 |
|
|
949 |
|
} |
950 |
|
|
951 |
|
long plane_sse(uint8_t * orig, |
952 |
|
uint8_t * recon, |
953 |
|
uint16_t stride, |
954 |
|
uint16_t width, |
955 |
|
uint16_t height) |
956 |
|
{ |
957 |
|
int diff, x, y; |
958 |
|
long sse=0; |
959 |
|
|
960 |
|
for (y = 0; y < height; y++) { |
961 |
|
for (x = 0; x < width; x++) { |
962 |
|
diff = *(orig + x) - *(recon + x); |
963 |
|
sse += diff * diff; |
964 |
|
} |
965 |
|
orig += stride; |
966 |
|
recon += stride; |
967 |
|
} |
968 |
|
return sse; |
969 |
|
} |
970 |
|
|
971 |
/* |
/* |
972 |
|
|
973 |
#include <stdio.h> |
#include <stdio.h> |
1064 |
} |
} |
1065 |
|
|
1066 |
|
|
|
#define ABS(X) (((X)>0)?(X):-(X)) |
|
1067 |
float |
float |
1068 |
image_mad(const IMAGE * img1, |
image_mad(const IMAGE * img1, |
1069 |
const IMAGE * img2, |
const IMAGE * img2, |
1126 |
sV += std2; |
sV += std2; |
1127 |
} |
} |
1128 |
} |
} |
1129 |
|
|
1130 |
|
|
1131 |
|
void |
1132 |
|
image_clear(IMAGE * img, int width, int height, int edged_width, |
1133 |
|
int y, int u, int v) |
1134 |
|
{ |
1135 |
|
uint8_t * p; |
1136 |
|
int i; |
1137 |
|
|
1138 |
|
p = img->y; |
1139 |
|
for (i = 0; i < height; i++) { |
1140 |
|
memset(p, y, width); |
1141 |
|
p += edged_width; |
1142 |
|
} |
1143 |
|
|
1144 |
|
p = img->u; |
1145 |
|
for (i = 0; i < height/2; i++) { |
1146 |
|
memset(p, u, width/2); |
1147 |
|
p += edged_width/2; |
1148 |
|
} |
1149 |
|
|
1150 |
|
p = img->v; |
1151 |
|
for (i = 0; i < height/2; i++) { |
1152 |
|
memset(p, v, width/2); |
1153 |
|
p += edged_width/2; |
1154 |
|
} |
1155 |
|
} |
1156 |
|
|
1157 |
|
|
1158 |
|
/* reduced resolution deblocking filter |
1159 |
|
block = block size (16=rrv, 8=full resolution) |
1160 |
|
flags = XVID_DEC_YDEBLOCK|XVID_DEC_UVDEBLOCK |
1161 |
|
*/ |
1162 |
|
void |
1163 |
|
image_deblock_rrv(IMAGE * img, int edged_width, |
1164 |
|
const MACROBLOCK * mbs, int mb_width, int mb_height, int mb_stride, |
1165 |
|
int block, int flags) |
1166 |
|
{ |
1167 |
|
const int edged_width2 = edged_width /2; |
1168 |
|
const int nblocks = block / 8; /* skals code uses 8pixel block uints */ |
1169 |
|
int i,j; |
1170 |
|
|
1171 |
|
/* luma: j,i in block units */ |
1172 |
|
if ((flags & XVID_DEC_DEBLOCKY)) |
1173 |
|
{ |
1174 |
|
for (j = 1; j < mb_height*2; j++) /* horizontal deblocking */ |
1175 |
|
for (i = 0; i < mb_width*2; i++) |
1176 |
|
{ |
1177 |
|
if (mbs[(j-1)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED || |
1178 |
|
mbs[(j+0)/2*mb_stride + (i/2)].mode != MODE_NOT_CODED) |
1179 |
|
{ |
1180 |
|
hfilter_31(img->y + (j*block - 1)*edged_width + i*block, |
1181 |
|
img->y + (j*block + 0)*edged_width + i*block, nblocks); |
1182 |
|
} |
1183 |
|
} |
1184 |
|
|
1185 |
|
for (j = 0; j < mb_height*2; j++) /* vertical deblocking */ |
1186 |
|
for (i = 1; i < mb_width*2; i++) |
1187 |
|
{ |
1188 |
|
if (mbs[(j/2)*mb_stride + (i-1)/2].mode != MODE_NOT_CODED || |
1189 |
|
mbs[(j/2)*mb_stride + (i+0)/2].mode != MODE_NOT_CODED) |
1190 |
|
{ |
1191 |
|
vfilter_31(img->y + (j*block)*edged_width + i*block - 1, |
1192 |
|
img->y + (j*block)*edged_width + i*block + 0, |
1193 |
|
edged_width, nblocks); |
1194 |
|
} |
1195 |
|
} |
1196 |
|
} |
1197 |
|
|
1198 |
|
|
1199 |
|
/* chroma */ |
1200 |
|
if ((flags & XVID_DEC_DEBLOCKUV)) |
1201 |
|
{ |
1202 |
|
for (j = 1; j < mb_height; j++) /* horizontal deblocking */ |
1203 |
|
for (i = 0; i < mb_width; i++) |
1204 |
|
{ |
1205 |
|
if (mbs[(j-1)*mb_stride + i].mode != MODE_NOT_CODED || |
1206 |
|
mbs[(j+0)*mb_stride + i].mode != MODE_NOT_CODED) |
1207 |
|
{ |
1208 |
|
hfilter_31(img->u + (j*block - 1)*edged_width2 + i*block, |
1209 |
|
img->u + (j*block + 0)*edged_width2 + i*block, nblocks); |
1210 |
|
hfilter_31(img->v + (j*block - 1)*edged_width2 + i*block, |
1211 |
|
img->v + (j*block + 0)*edged_width2 + i*block, nblocks); |
1212 |
|
} |
1213 |
|
} |
1214 |
|
|
1215 |
|
for (j = 0; j < mb_height; j++) /* vertical deblocking */ |
1216 |
|
for (i = 1; i < mb_width; i++) |
1217 |
|
{ |
1218 |
|
if (mbs[j*mb_stride + i - 1].mode != MODE_NOT_CODED || |
1219 |
|
mbs[j*mb_stride + i + 0].mode != MODE_NOT_CODED) |
1220 |
|
{ |
1221 |
|
vfilter_31(img->u + (j*block)*edged_width2 + i*block - 1, |
1222 |
|
img->u + (j*block)*edged_width2 + i*block + 0, |
1223 |
|
edged_width2, nblocks); |
1224 |
|
vfilter_31(img->v + (j*block)*edged_width2 + i*block - 1, |
1225 |
|
img->v + (j*block)*edged_width2 + i*block + 0, |
1226 |
|
edged_width2, nblocks); |
1227 |
|
} |
1228 |
|
} |
1229 |
|
} |
1230 |
|
|
1231 |
|
} |
1232 |
|
|