Commit 82529d22 authored by Rupert Swarbrick's avatar Rupert Swarbrick

Make yv12_buffer_config more uniform

This patch slightly reorders the fields in yv12_buffer_config and then
uses anonymous unions in order to make it possible to write code that
iterates uniformly over planes.

The patch also ports some code (mostly in yv12extend.c and
aom_scale.c) to show how this can make things more concise.

This should make no difference to the coded results. I think it's
unlikely to have any significant performance impact (the reordered
fields in a yv12_buffer_config only come to 17*4 = 68 bytes in total,
so almost fit in a normal sized cache line).

Change-Id: Iebb46344500b9df82915f34cfd193e189d712062
parent 67c9ab0b
......@@ -317,9 +317,7 @@ void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
const int heights[3] = { a->y_crop_height, a->uv_crop_height,
a->uv_crop_height };
const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
int i;
uint64_t total_sse = 0;
......@@ -334,14 +332,15 @@ void aom_calc_highbd_psnr(const YV12_BUFFER_CONFIG *a,
uint64_t sse;
if (a->flags & YV12_FLAG_HIGHBITDEPTH) {
if (input_shift) {
sse = highbd_get_sse_shift(a_planes[i], a_strides[i], b_planes[i],
sse = highbd_get_sse_shift(a->buffers[i], a_strides[i], b->buffers[i],
b_strides[i], w, h, input_shift);
} else {
sse = highbd_get_sse(a_planes[i], a_strides[i], b_planes[i],
sse = highbd_get_sse(a->buffers[i], a_strides[i], b->buffers[i],
b_strides[i], w, h);
}
} else {
sse = get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
sse = get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w,
h);
}
psnr->sse[1 + i] = sse;
psnr->samples[1 + i] = samples;
......@@ -365,9 +364,7 @@ void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
const int widths[3] = { a->y_crop_width, a->uv_crop_width, a->uv_crop_width };
const int heights[3] = { a->y_crop_height, a->uv_crop_height,
a->uv_crop_height };
const uint8_t *a_planes[3] = { a->y_buffer, a->u_buffer, a->v_buffer };
const int a_strides[3] = { a->y_stride, a->uv_stride, a->uv_stride };
const uint8_t *b_planes[3] = { b->y_buffer, b->u_buffer, b->v_buffer };
const int b_strides[3] = { b->y_stride, b->uv_stride, b->uv_stride };
int i;
uint64_t total_sse = 0;
......@@ -378,7 +375,7 @@ void aom_calc_psnr(const YV12_BUFFER_CONFIG *a, const YV12_BUFFER_CONFIG *b,
const int h = heights[i];
const uint32_t samples = w * h;
const uint64_t sse =
get_sse(a_planes[i], a_strides[i], b_planes[i], b_strides[i], w, h);
get_sse(a->buffers[i], a_strides[i], b->buffers[i], b_strides[i], w, h);
psnr->sse[1 + i] = sse;
psnr->samples[1 + i] = samples;
psnr->psnr[1 + i] = aom_sse_to_psnr(samples, peak, (double)sse);
......
......@@ -168,23 +168,16 @@ static double aom_highbd_ssim2(const uint8_t *img1, const uint8_t *img2,
double aom_calc_ssim(const YV12_BUFFER_CONFIG *source,
const YV12_BUFFER_CONFIG *dest, double *weight) {
double a, b, c;
double ssimv;
a = aom_ssim2(source->y_buffer, dest->y_buffer, source->y_stride,
dest->y_stride, source->y_crop_width, source->y_crop_height);
b = aom_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride,
dest->uv_stride, source->uv_crop_width, source->uv_crop_height);
c = aom_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride,
dest->uv_stride, source->uv_crop_width, source->uv_crop_height);
ssimv = a * .8 + .1 * (b + c);
double abc[3];
for (int i = 0; i < 3; ++i) {
const int is_uv = i > 0;
abc[i] = aom_ssim2(source->buffers[i], dest->buffers[i],
source->strides[is_uv], dest->strides[is_uv],
source->crop_widths[is_uv], source->crop_heights[is_uv]);
}
*weight = 1;
return ssimv;
return abc[0] * .8 + .1 * (abc[1] + abc[2]);
}
// traditional ssim as per: http://en.wikipedia.org/wiki/Structural_similarity
......@@ -433,30 +426,19 @@ double aom_get_ssim_metrics(uint8_t *img1, int img1_pitch, uint8_t *img2,
double aom_highbd_calc_ssim(const YV12_BUFFER_CONFIG *source,
const YV12_BUFFER_CONFIG *dest, double *weight,
uint32_t bd, uint32_t in_bd) {
double a, b, c;
double ssimv;
uint32_t shift = 0;
assert(bd >= in_bd);
shift = bd - in_bd;
a = aom_highbd_ssim2(source->y_buffer, dest->y_buffer, source->y_stride,
dest->y_stride, source->y_crop_width,
source->y_crop_height, in_bd, shift);
b = aom_highbd_ssim2(source->u_buffer, dest->u_buffer, source->uv_stride,
dest->uv_stride, source->uv_crop_width,
source->uv_crop_height, in_bd, shift);
c = aom_highbd_ssim2(source->v_buffer, dest->v_buffer, source->uv_stride,
dest->uv_stride, source->uv_crop_width,
source->uv_crop_height, in_bd, shift);
ssimv = a * .8 + .1 * (b + c);
const uint32_t shift = bd - in_bd;
double abc[3];
for (int i = 0; i < 3; ++i) {
const int is_uv = i > 0;
abc[i] = aom_highbd_ssim2(source->buffers[i], dest->buffers[i],
source->strides[is_uv], dest->strides[is_uv],
source->crop_widths[is_uv],
source->crop_heights[is_uv], in_bd, shift);
}
*weight = 1;
return ssimv;
return abc[0] * .8 + .1 * (abc[1] + abc[2]);
}
#endif // CONFIG_HIGHBITDEPTH
......@@ -476,54 +476,30 @@ void aom_scale_frame(YV12_BUFFER_CONFIG *src, YV12_BUFFER_CONFIG *dst,
unsigned int hscale, unsigned int hratio,
unsigned int vscale, unsigned int vratio,
unsigned int interlaced) {
int i;
int dw = (hscale - 1 + src->y_width * hratio) / hscale;
int dh = (vscale - 1 + src->y_height * vratio) / vscale;
/* call our internal scaling routines!! */
Scale2D((unsigned char *)src->y_buffer, src->y_stride, src->y_width,
src->y_height, (unsigned char *)dst->y_buffer, dst->y_stride, dw, dh,
temp_area, temp_height, hscale, hratio, vscale, vratio, interlaced);
if (dw < (int)dst->y_width)
for (i = 0; i < dh; ++i)
memset(dst->y_buffer + i * dst->y_stride + dw - 1,
dst->y_buffer[i * dst->y_stride + dw - 2], dst->y_width - dw + 1);
if (dh < (int)dst->y_height)
for (i = dh - 1; i < (int)dst->y_height; ++i)
memcpy(dst->y_buffer + i * dst->y_stride,
dst->y_buffer + (dh - 2) * dst->y_stride, dst->y_width + 1);
Scale2D((unsigned char *)src->u_buffer, src->uv_stride, src->uv_width,
src->uv_height, (unsigned char *)dst->u_buffer, dst->uv_stride,
dw / 2, dh / 2, temp_area, temp_height, hscale, hratio, vscale,
vratio, interlaced);
if (dw / 2 < (int)dst->uv_width)
for (i = 0; i < dst->uv_height; ++i)
memset(dst->u_buffer + i * dst->uv_stride + dw / 2 - 1,
dst->u_buffer[i * dst->uv_stride + dw / 2 - 2],
dst->uv_width - dw / 2 + 1);
if (dh / 2 < (int)dst->uv_height)
for (i = dh / 2 - 1; i < (int)dst->y_height / 2; ++i)
memcpy(dst->u_buffer + i * dst->uv_stride,
dst->u_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
Scale2D((unsigned char *)src->v_buffer, src->uv_stride, src->uv_width,
src->uv_height, (unsigned char *)dst->v_buffer, dst->uv_stride,
dw / 2, dh / 2, temp_area, temp_height, hscale, hratio, vscale,
vratio, interlaced);
if (dw / 2 < (int)dst->uv_width)
for (i = 0; i < dst->uv_height; ++i)
memset(dst->v_buffer + i * dst->uv_stride + dw / 2 - 1,
dst->v_buffer[i * dst->uv_stride + dw / 2 - 2],
dst->uv_width - dw / 2 + 1);
if (dh / 2 < (int)dst->uv_height)
for (i = dh / 2 - 1; i < (int)dst->y_height / 2; ++i)
memcpy(dst->v_buffer + i * dst->uv_stride,
dst->v_buffer + (dh / 2 - 2) * dst->uv_stride, dst->uv_width);
const int dw = (hscale - 1 + src->y_width * hratio) / hscale;
const int dh = (vscale - 1 + src->y_height * vratio) / vscale;
for (int plane = 0; plane < 3; ++plane) {
const int is_uv = plane > 0;
const int plane_dw = dw >> is_uv;
const int plane_dh = dh >> is_uv;
Scale2D((unsigned char *)src->buffers[plane], src->strides[is_uv],
src->widths[is_uv], src->heights[is_uv],
(unsigned char *)dst->buffers[plane], dst->strides[is_uv], plane_dw,
plane_dh, temp_area, temp_height, hscale, hratio, vscale, vratio,
interlaced);
if (plane_dw < dst->widths[is_uv])
for (int i = 0; i < plane_dh; ++i)
memset(dst->buffers[plane] + i * dst->strides[is_uv] + plane_dw - 1,
dst->buffers[plane][i * dst->strides[is_uv] + plane_dw - 2],
dst->widths[is_uv] - plane_dw + 1);
if (plane_dh < dst->heights[is_uv])
for (int i = plane_dh - 1; i < dst->heights[is_uv]; ++i)
memcpy(dst->buffers[plane] + i * dst->strides[is_uv],
dst->buffers[plane] + (plane_dh - 2) * dst->strides[is_uv],
dst->widths[is_uv] + 1);
}
}
......@@ -101,8 +101,6 @@ static void extend_plane_high(uint8_t *const src8, int src_stride, int width,
#endif
void aom_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
const int uv_border = ybf->border / 2;
assert(ybf->border % 2 == 0);
assert(ybf->y_height - ybf->y_crop_height < 16);
assert(ybf->y_width - ybf->y_crop_width < 16);
......@@ -111,49 +109,33 @@ void aom_yv12_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
#if CONFIG_HIGHBITDEPTH
if (ybf->flags & YV12_FLAG_HIGHBITDEPTH) {
extend_plane_high(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
ybf->y_crop_height, ybf->border, ybf->border,
ybf->border + ybf->y_height - ybf->y_crop_height,
ybf->border + ybf->y_width - ybf->y_crop_width);
extend_plane_high(ybf->u_buffer, ybf->uv_stride, ybf->uv_crop_width,
ybf->uv_crop_height, uv_border, uv_border,
uv_border + ybf->uv_height - ybf->uv_crop_height,
uv_border + ybf->uv_width - ybf->uv_crop_width);
extend_plane_high(ybf->v_buffer, ybf->uv_stride, ybf->uv_crop_width,
ybf->uv_crop_height, uv_border, uv_border,
uv_border + ybf->uv_height - ybf->uv_crop_height,
uv_border + ybf->uv_width - ybf->uv_crop_width);
for (int plane = 0; plane < 3; ++plane) {
const int is_uv = plane > 0;
const int plane_border = ybf->border >> is_uv;
extend_plane_high(
ybf->buffers[plane], ybf->strides[is_uv], ybf->crop_widths[is_uv],
ybf->crop_heights[is_uv], plane_border, plane_border,
plane_border + ybf->heights[is_uv] - ybf->crop_heights[is_uv],
plane_border + ybf->widths[is_uv] - ybf->crop_widths[is_uv]);
}
return;
}
#endif
extend_plane(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
ybf->y_crop_height, ybf->border, ybf->border,
ybf->border + ybf->y_height - ybf->y_crop_height,
ybf->border + ybf->y_width - ybf->y_crop_width);
extend_plane(ybf->u_buffer, ybf->uv_stride, ybf->uv_crop_width,
ybf->uv_crop_height, uv_border, uv_border,
uv_border + ybf->uv_height - ybf->uv_crop_height,
uv_border + ybf->uv_width - ybf->uv_crop_width);
extend_plane(ybf->v_buffer, ybf->uv_stride, ybf->uv_crop_width,
ybf->uv_crop_height, uv_border, uv_border,
uv_border + ybf->uv_height - ybf->uv_crop_height,
uv_border + ybf->uv_width - ybf->uv_crop_width);
for (int plane = 0; plane < 3; ++plane) {
const int is_uv = plane > 0;
const int plane_border = ybf->border >> is_uv;
extend_plane(ybf->buffers[plane], ybf->strides[is_uv],
ybf->crop_widths[is_uv], ybf->crop_heights[is_uv],
plane_border, plane_border,
plane_border + ybf->heights[is_uv] - ybf->crop_heights[is_uv],
plane_border + ybf->widths[is_uv] - ybf->crop_widths[is_uv]);
}
}
#if CONFIG_AV1
static void extend_frame(YV12_BUFFER_CONFIG *const ybf, int ext_size) {
const int c_w = ybf->uv_crop_width;
const int c_h = ybf->uv_crop_height;
const int ss_x = ybf->uv_width < ybf->y_width;
const int ss_y = ybf->uv_height < ybf->y_height;
const int c_et = ext_size >> ss_y;
const int c_el = ext_size >> ss_x;
const int c_eb = c_et + ybf->uv_height - ybf->uv_crop_height;
const int c_er = c_el + ybf->uv_width - ybf->uv_crop_width;
assert(ybf->y_height - ybf->y_crop_height < 16);
assert(ybf->y_width - ybf->y_crop_width < 16);
......@@ -162,25 +144,29 @@ static void extend_frame(YV12_BUFFER_CONFIG *const ybf, int ext_size) {
#if CONFIG_HIGHBITDEPTH
if (ybf->flags & YV12_FLAG_HIGHBITDEPTH) {
extend_plane_high(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
ybf->y_crop_height, ext_size, ext_size,
ext_size + ybf->y_height - ybf->y_crop_height,
ext_size + ybf->y_width - ybf->y_crop_width);
extend_plane_high(ybf->u_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb,
c_er);
extend_plane_high(ybf->v_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb,
c_er);
for (int plane = 0; plane < 3; ++plane) {
const int is_uv = plane > 0;
const int top = ext_size >> (is_uv ? ss_y : 0);
const int left = ext_size >> (is_uv ? ss_x : 0);
const int bottom = top + ybf->heights[is_uv] - ybf->crop_heights[is_uv];
const int right = left + ybf->widths[is_uv] - ybf->crop_widths[is_uv];
extend_plane_high(ybf->buffers[plane], ybf->strides[is_uv],
ybf->crop_widths[is_uv], ybf->crop_heights[is_uv], top,
left, bottom, right);
}
return;
}
#endif
extend_plane(ybf->y_buffer, ybf->y_stride, ybf->y_crop_width,
ybf->y_crop_height, ext_size, ext_size,
ext_size + ybf->y_height - ybf->y_crop_height,
ext_size + ybf->y_width - ybf->y_crop_width);
extend_plane(ybf->u_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb, c_er);
extend_plane(ybf->v_buffer, ybf->uv_stride, c_w, c_h, c_et, c_el, c_eb, c_er);
for (int plane = 0; plane < 3; ++plane) {
const int is_uv = plane > 0;
const int top = ext_size >> (is_uv ? ss_y : 0);
const int left = ext_size >> (is_uv ? ss_x : 0);
const int bottom = top + ybf->heights[is_uv] - ybf->crop_heights[is_uv];
const int right = left + ybf->widths[is_uv] - ybf->crop_widths[is_uv];
extend_plane(ybf->buffers[plane], ybf->strides[is_uv],
ybf->crop_widths[is_uv], ybf->crop_heights[is_uv], top, left,
bottom, right);
}
}
void aom_extend_frame_borders_c(YV12_BUFFER_CONFIG *ybf) {
......@@ -230,10 +216,6 @@ static void memcpy_short_addr(uint8_t *dst8, const uint8_t *src8, int num) {
// Note: The frames are assumed to be identical in size.
void aom_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_bc,
YV12_BUFFER_CONFIG *dst_bc) {
int row;
const uint8_t *src = src_bc->y_buffer;
uint8_t *dst = dst_bc->y_buffer;
#if 0
/* These assertions are valid in the codec, but the libaom-tester uses
* this code slightly differently.
......@@ -243,63 +225,36 @@ void aom_yv12_copy_frame_c(const YV12_BUFFER_CONFIG *src_bc,
#endif
#if CONFIG_HIGHBITDEPTH
if (src_bc->flags & YV12_FLAG_HIGHBITDEPTH) {
assert(dst_bc->flags & YV12_FLAG_HIGHBITDEPTH);
for (row = 0; row < src_bc->y_height; ++row) {
memcpy_short_addr(dst, src, src_bc->y_width);
src += src_bc->y_stride;
dst += dst_bc->y_stride;
}
src = src_bc->u_buffer;
dst = dst_bc->u_buffer;
for (row = 0; row < src_bc->uv_height; ++row) {
memcpy_short_addr(dst, src, src_bc->uv_width);
src += src_bc->uv_stride;
dst += dst_bc->uv_stride;
}
src = src_bc->v_buffer;
dst = dst_bc->v_buffer;
assert((src_bc->flags & YV12_FLAG_HIGHBITDEPTH) ==
(dst_bc->flags & YV12_FLAG_HIGHBITDEPTH));
for (row = 0; row < src_bc->uv_height; ++row) {
memcpy_short_addr(dst, src, src_bc->uv_width);
src += src_bc->uv_stride;
dst += dst_bc->uv_stride;
if (src_bc->flags & YV12_FLAG_HIGHBITDEPTH) {
for (int plane = 0; plane < 3; ++plane) {
const uint8_t *plane_src = src_bc->buffers[plane];
uint8_t *plane_dst = dst_bc->buffers[plane];
const int is_uv = plane > 0;
for (int row = 0; row < src_bc->heights[is_uv]; ++row) {
memcpy_short_addr(plane_dst, plane_src, src_bc->widths[is_uv]);
plane_src += src_bc->strides[is_uv];
plane_dst += dst_bc->strides[is_uv];
}
}
aom_yv12_extend_frame_borders_c(dst_bc);
return;
} else {
assert(!(dst_bc->flags & YV12_FLAG_HIGHBITDEPTH));
}
#endif
for (row = 0; row < src_bc->y_height; ++row) {
memcpy(dst, src, src_bc->y_width);
src += src_bc->y_stride;
dst += dst_bc->y_stride;
}
src = src_bc->u_buffer;
dst = dst_bc->u_buffer;
for (row = 0; row < src_bc->uv_height; ++row) {
memcpy(dst, src, src_bc->uv_width);
src += src_bc->uv_stride;
dst += dst_bc->uv_stride;
}
src = src_bc->v_buffer;
dst = dst_bc->v_buffer;
for (row = 0; row < src_bc->uv_height; ++row) {
memcpy(dst, src, src_bc->uv_width);
src += src_bc->uv_stride;
dst += dst_bc->uv_stride;
for (int plane = 0; plane < 3; ++plane) {
const uint8_t *plane_src = src_bc->buffers[plane];
uint8_t *plane_dst = dst_bc->buffers[plane];
const int is_uv = plane > 0;
for (int row = 0; row < src_bc->heights[is_uv]; ++row) {
memcpy(plane_dst, plane_src, src_bc->widths[is_uv]);
plane_src += src_bc->strides[is_uv];
plane_dst += dst_bc->strides[is_uv];
}
}
aom_yv12_extend_frame_borders_c(dst_bc);
}
......
......@@ -34,26 +34,53 @@ extern "C" {
#define AOM_BORDER_IN_PIXELS 160
typedef struct yv12_buffer_config {
int y_width;
int y_height;
int y_crop_width;
int y_crop_height;
int y_stride;
int uv_width;
int uv_height;
int uv_crop_width;
int uv_crop_height;
int uv_stride;
int alpha_width;
int alpha_height;
int alpha_stride;
uint8_t *y_buffer;
uint8_t *u_buffer;
uint8_t *v_buffer;
uint8_t *alpha_buffer;
union {
struct {
int y_width;
int uv_width;
int alpha_width;
};
int widths[3];
};
union {
struct {
int y_height;
int uv_height;
int alpha_height;
};
int heights[3];
};
union {
struct {
int y_crop_width;
int uv_crop_width;
};
int crop_widths[2];
};
union {
struct {
int y_crop_height;
int uv_crop_height;
};
int crop_heights[2];
};
union {
struct {
int y_stride;
int uv_stride;
int alpha_stride;
};
int strides[3];
};
union {
struct {
uint8_t *y_buffer;
uint8_t *u_buffer;
uint8_t *v_buffer;
uint8_t *alpha_buffer;
};
uint8_t *buffers[4];
};
#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
// If the frame is stored in a 16-bit buffer, this stores an 8-bit version
......
......@@ -1748,8 +1748,6 @@ void av1_build_inter_predictors_sb(const AV1_COMMON *cm, MACROBLOCKD *xd,
void av1_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
BLOCK_SIZE bsize, const YV12_BUFFER_CONFIG *src,
int mi_row, int mi_col) {
uint8_t *const buffers[MAX_MB_PLANE] = { src->y_buffer, src->u_buffer,
src->v_buffer };
const int widths[MAX_MB_PLANE] = { src->y_crop_width, src->uv_crop_width,
src->uv_crop_width };
const int heights[MAX_MB_PLANE] = { src->y_crop_height, src->uv_crop_height,
......@@ -1760,7 +1758,7 @@ void av1_setup_dst_planes(struct macroblockd_plane planes[MAX_MB_PLANE],
for (i = 0; i < MAX_MB_PLANE; ++i) {
struct macroblockd_plane *const pd = &planes[i];
setup_pred_plane(&pd->dst, bsize, buffers[i], widths[i], heights[i],
setup_pred_plane(&pd->dst, bsize, src->buffers[i], widths[i], heights[i],
strides[i], mi_row, mi_col, NULL, pd->subsampling_x,
pd->subsampling_y);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment