Commit 3cd37dfe authored by Deb Mukherjee's avatar Deb Mukherjee

Adds a non-normative resize library to vp9 encoder

Adds an arbitrary-size resize library for use in scaling of input
frames in a non-normative manner in the vp9 encoder. The method
used is as follows:
Downsampling - Uses a 8 tap filter for factor of 2 decimation upto
a size just higher than the desired size. Then interpolates pixels
at a precision of 1/32 pel using a set of 8-tap filters.
Upsampling - Interpolates pixels at a precision of 1/32 pel using
a set of 8-tap filters.

There is no assembly optimization yet.

Change-Id: Ib5b81e174fc139da322bb97c8214d52289d60d8a
parent 7a197b2d
......@@ -208,7 +208,7 @@ class ResizeInternalTest : public ResizeTest {
virtual void PSNRPktHook(const vpx_codec_cx_pkt_t *pkt) {
if (!frame0_psnr_)
frame0_psnr_ = pkt->data.psnr.psnr[0];
EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 1.5);
EXPECT_NEAR(pkt->data.psnr.psnr[0], frame0_psnr_, 2.0);
}
virtual void FramePktHook(const vpx_codec_cx_pkt_t *pkt) {
......
......@@ -36,6 +36,7 @@
#include "vp9/encoder/vp9_segmentation.h"
#include "vp9/encoder/vp9_temporal_filter.h"
#include "vp9/encoder/vp9_vaq.h"
#include "vp9/encoder/vp9_resize.h"
#include "vpx_ports/vpx_timer.h"
......@@ -2295,6 +2296,42 @@ void vp9_write_yuv_rec_frame(VP9_COMMON *cm) {
}
#endif
static void scale_and_extend_frame_nonnormative(YV12_BUFFER_CONFIG *src_fb,
YV12_BUFFER_CONFIG *dst_fb) {
const int in_w = src_fb->y_crop_width;
const int in_h = src_fb->y_crop_height;
const int out_w = dst_fb->y_crop_width;
const int out_h = dst_fb->y_crop_height;
const int in_w_uv = src_fb->uv_crop_width;
const int in_h_uv = src_fb->uv_crop_height;
const int out_w_uv = dst_fb->uv_crop_width;
const int out_h_uv = dst_fb->uv_crop_height;
int i;
uint8_t *srcs[4] = {src_fb->y_buffer, src_fb->u_buffer, src_fb->v_buffer,
src_fb->alpha_buffer};
int src_strides[4] = {src_fb->y_stride, src_fb->uv_stride, src_fb->uv_stride,
src_fb->alpha_stride};
uint8_t *dsts[4] = {dst_fb->y_buffer, dst_fb->u_buffer, dst_fb->v_buffer,
dst_fb->alpha_buffer};
int dst_strides[4] = {dst_fb->y_stride, dst_fb->uv_stride, dst_fb->uv_stride,
dst_fb->alpha_stride};
for (i = 0; i < MAX_MB_PLANE; ++i) {
if (i == 0 || i == 3) {
// Y and alpha planes
vp9_resize_plane(srcs[i], in_h, in_w, src_strides[i],
dsts[i], out_h, out_w, dst_strides[i]);
} else {
// Chroma planes
vp9_resize_plane(srcs[i], in_h_uv, in_w_uv, src_strides[i],
dsts[i], out_h_uv, out_w_uv, dst_strides[i]);
}
}
vp8_yv12_extend_frame_borders(dst_fb);
}
static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
YV12_BUFFER_CONFIG *dst_fb) {
const int in_w = src_fb->y_crop_width;
......@@ -2316,7 +2353,7 @@ static void scale_and_extend_frame(YV12_BUFFER_CONFIG *src_fb,
for (y = 0; y < out_h; y += 16) {
for (x = 0; x < out_w; x += 16) {
for (i = 0; i < MAX_MB_PLANE; ++i) {
const int factor = i == 0 ? 1 : 2;
const int factor = (i == 0 || i == 3 ? 1 : 2);
const int x_q4 = x * (16 / factor) * in_w / out_w;
const int y_q4 = y * (16 / factor) * in_h / out_h;
const int src_stride = src_strides[i];
......@@ -2924,7 +2961,8 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi,
/* Scale the source buffer, if required. */
if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width ||
cm->mi_rows * 8 != cpi->un_scaled_source->y_height) {
scale_and_extend_frame(cpi->un_scaled_source, &cpi->scaled_source);
scale_and_extend_frame_nonnormative(cpi->un_scaled_source,
&cpi->scaled_source);
cpi->Source = &cpi->scaled_source;
} else {
cpi->Source = cpi->un_scaled_source;
......
/*
* Copyright (c) 2014 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vp9/common/vp9_common.h"
#include "vp9/encoder/vp9_resize.h"
#define FILTER_BITS 7
#define INTERP_TAPS 8
#define SUBPEL_BITS 5
#define SUBPEL_MASK ((1 << SUBPEL_BITS) - 1)
#define INTERP_PRECISION_BITS 32
#define ROUND_POWER_OF_TWO(value, n) \
(((value) + (1 << ((n) - 1))) >> (n))
typedef int16_t interp_kernel[INTERP_TAPS];
// Filters for interpolation - note this also filters integer pels.
const interp_kernel vp9_filteredinterp_filters[(1 << SUBPEL_BITS)] = {
{-1, -8, 33, 80, 33, -8, -1, 0},
{-1, -8, 30, 80, 35, -8, -1, 1},
{-1, -8, 28, 80, 37, -7, -2, 1},
{0, -8, 26, 79, 39, -7, -2, 1},
{0, -8, 24, 79, 41, -7, -2, 1},
{0, -8, 22, 78, 43, -6, -2, 1},
{0, -8, 20, 78, 45, -5, -3, 1},
{0, -8, 18, 77, 48, -5, -3, 1},
{0, -8, 16, 76, 50, -4, -3, 1},
{0, -8, 15, 75, 52, -3, -4, 1},
{0, -7, 13, 74, 54, -3, -4, 1},
{0, -7, 11, 73, 56, -2, -4, 1},
{0, -7, 10, 71, 58, -1, -4, 1},
{1, -7, 8, 70, 60, 0, -5, 1},
{1, -6, 6, 68, 62, 1, -5, 1},
{1, -6, 5, 67, 63, 2, -5, 1},
{1, -6, 4, 65, 65, 4, -6, 1},
{1, -5, 2, 63, 67, 5, -6, 1},
{1, -5, 1, 62, 68, 6, -6, 1},
{1, -5, 0, 60, 70, 8, -7, 1},
{1, -4, -1, 58, 71, 10, -7, 0},
{1, -4, -2, 56, 73, 11, -7, 0},
{1, -4, -3, 54, 74, 13, -7, 0},
{1, -4, -3, 52, 75, 15, -8, 0},
{1, -3, -4, 50, 76, 16, -8, 0},
{1, -3, -5, 48, 77, 18, -8, 0},
{1, -3, -5, 45, 78, 20, -8, 0},
{1, -2, -6, 43, 78, 22, -8, 0},
{1, -2, -7, 41, 79, 24, -8, 0},
{1, -2, -7, 39, 79, 26, -8, 0},
{1, -2, -7, 37, 80, 28, -8, -1},
{1, -1, -8, 35, 80, 30, -8, -1},
};
// Filters for factor of 2 downsampling.
static const int16_t vp9_down2_symeven_half_filter[] = {56, 12, -3, -1};
static const int16_t vp9_down2_symodd_half_filter[] = {64, 35, 0, -3};
static void interpolate(const uint8_t *const input, int inlength,
uint8_t *output, int outlength) {
const int64_t delta = (((uint64_t)inlength << 32) + outlength / 2) /
outlength;
const int64_t offset = inlength > outlength ?
(((int64_t)(inlength - outlength) << 31) + outlength / 2) / outlength :
-(((int64_t)(outlength - inlength) << 31) + outlength / 2) / outlength;
uint8_t *optr = output;
int x, x1, x2, sum, k, int_pel, sub_pel;
int64_t y;
x = 0;
y = offset;
while ((y >> INTERP_PRECISION_BITS) < (INTERP_TAPS / 2 - 1)) {
x++;
y += delta;
}
x1 = x;
x = outlength - 1;
y = delta * x + offset;
while ((y >> INTERP_PRECISION_BITS) +
(int64_t)(INTERP_TAPS / 2) >= inlength) {
x--;
y -= delta;
}
x2 = x;
if (x1 > x2) {
for (x = 0, y = offset; x < outlength; ++x, y += delta) {
const int16_t *filter;
int_pel = y >> INTERP_PRECISION_BITS;
sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
filter = vp9_filteredinterp_filters[sub_pel];
sum = 0;
for (k = 0; k < INTERP_TAPS; ++k) {
const int pk = int_pel - INTERP_TAPS / 2 + 1 + k;
sum += filter[k] * input[(pk < 0 ? 0 :
(pk >= inlength ? inlength - 1 : pk))];
}
*optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
}
} else {
// Initial part.
for (x = 0, y = offset; x < x1; ++x, y += delta) {
const int16_t *filter;
int_pel = y >> INTERP_PRECISION_BITS;
sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
filter = vp9_filteredinterp_filters[sub_pel];
sum = 0;
for (k = 0; k < INTERP_TAPS; ++k)
sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k < 0 ?
0 :
int_pel - INTERP_TAPS / 2 + 1 + k)];
*optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
}
// Middle part.
for (; x <= x2; ++x, y += delta) {
const int16_t *filter;
int_pel = y >> INTERP_PRECISION_BITS;
sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
filter = vp9_filteredinterp_filters[sub_pel];
sum = 0;
for (k = 0; k < INTERP_TAPS; ++k)
sum += filter[k] * input[int_pel - INTERP_TAPS / 2 + 1 + k];
*optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
}
// End part.
for (; x < outlength; ++x, y += delta) {
const int16_t *filter;
int_pel = y >> INTERP_PRECISION_BITS;
sub_pel = (y >> (INTERP_PRECISION_BITS - SUBPEL_BITS)) & SUBPEL_MASK;
filter = vp9_filteredinterp_filters[sub_pel];
sum = 0;
for (k = 0; k < INTERP_TAPS; ++k)
sum += filter[k] * input[(int_pel - INTERP_TAPS / 2 + 1 + k >=
inlength ? inlength - 1 :
int_pel - INTERP_TAPS / 2 + 1 + k)];
*optr++ = clip_pixel(ROUND_POWER_OF_TWO(sum, FILTER_BITS));
}
}
}
static void down2_symeven(const uint8_t *const input, int length,
uint8_t *output) {
// Actual filter len = 2 * filter_len_half.
static const int16_t *filter = vp9_down2_symeven_half_filter;
const int filter_len_half = sizeof(vp9_down2_symeven_half_filter) / 2;
int i, j;
uint8_t *optr = output;
int l1 = filter_len_half;
int l2 = (length - filter_len_half);
l1 += (l1 & 1);
l2 += (l2 & 1);
if (l1 > l2) {
// Short input length.
for (i = 0; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum += (input[(i - j < 0 ? 0 : i - j)] +
input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
} else {
// Initial part.
for (i = 0; i < l1; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + 1 + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
// Middle part.
for (; i < l2; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum += (input[i - j] + input[i + 1 + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
// End part.
for (; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum += (input[i - j] +
input[(i + 1 + j >= length ? length - 1 : i + 1 + j)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
}
}
static void down2_symodd(const uint8_t *const input, int length,
uint8_t *output) {
// Actual filter len = 2 * filter_len_half - 1.
static const int16_t *filter = vp9_down2_symodd_half_filter;
const int filter_len_half = sizeof(vp9_down2_symodd_half_filter) / 2;
int i, j;
uint8_t *optr = output;
int l1 = filter_len_half - 1;
int l2 = (length - filter_len_half + 1);
l1 += (l1 & 1);
l2 += (l2 & 1);
if (l1 > l2) {
// Short input length.
for (i = 0; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
for (j = 1; j < filter_len_half; ++j) {
sum += (input[(i - j < 0 ? 0 : i - j)] +
input[(i + j >= length ? length - 1 : i + j)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
} else {
// Initial part.
for (i = 0; i < l1; i += 2) {
int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
for (j = 1; j < filter_len_half; ++j) {
sum += (input[(i - j < 0 ? 0 : i - j)] + input[i + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
// Middle part.
for (; i < l2; i += 2) {
int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
for (j = 1; j < filter_len_half; ++j) {
sum += (input[i - j] + input[i + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
// End part.
for (; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1)) + input[i] * filter[0];
for (j = 1; j < filter_len_half; ++j) {
sum += (input[i - j] + input[(i + j >= length ? length - 1 : i + j)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel(sum);
}
}
}
static int get_down2_length(int length, int steps) {
int s;
for (s = 0; s < steps; ++s)
length = (length + 1) >> 1;
return length;
}
int get_down2_steps(int in_length, int out_length) {
int steps = 0;
int proj_in_length;
while ((proj_in_length = get_down2_length(in_length, 1)) >= out_length) {
++steps;
in_length = proj_in_length;
}
return steps;
}
static void resize_multistep(const uint8_t *const input,
int length,
uint8_t *output,
int olength,
uint8_t *buf) {
int steps;
if (length == olength) {
memcpy(output, input, sizeof(uint8_t) * length);
return;
}
steps = get_down2_steps(length, olength);
if (steps > 0) {
int s;
uint8_t *out = NULL;
uint8_t *tmpbuf = NULL;
uint8_t *otmp, *otmp2;
int filteredlength = length;
if (!tmpbuf) {
tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) * length);
otmp = tmpbuf;
} else {
otmp = buf;
}
otmp2 = otmp + get_down2_length(length, 1);
for (s = 0; s < steps; ++s) {
const int proj_filteredlength = get_down2_length(filteredlength, 1);
const uint8_t *const in = (s == 0 ? input : out);
if (s == steps - 1 && proj_filteredlength == olength)
out = output;
else
out = (s & 1 ? otmp2 : otmp);
if (filteredlength & 1)
down2_symodd(in, filteredlength, out);
else
down2_symeven(in, filteredlength, out);
filteredlength = proj_filteredlength;
}
if (filteredlength != olength) {
interpolate(out, filteredlength, output, olength);
}
if (tmpbuf)
free(tmpbuf);
} else {
interpolate(input, length, output, olength);
}
}
static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) {
int i;
uint8_t *iptr = img;
uint8_t *aptr = arr;
for (i = 0; i < len; ++i, iptr += stride) {
*aptr++ = *iptr;
}
}
static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) {
int i;
uint8_t *iptr = img;
uint8_t *aptr = arr;
for (i = 0; i < len; ++i, iptr += stride) {
*iptr = *aptr++;
}
}
void vp9_resize_plane(const uint8_t *const input,
int height,
int width,
int in_stride,
uint8_t *output,
int height2,
int width2,
int out_stride) {
int i;
uint8_t *intbuf = (uint8_t *)malloc(sizeof(uint8_t) * width2 * height);
uint8_t *tmpbuf = (uint8_t *)malloc(sizeof(uint8_t) *
(width < height ? height : width));
uint8_t *arrbuf = (uint8_t *)malloc(sizeof(uint8_t) * (height + height2));
for (i = 0; i < height; ++i)
resize_multistep(input + in_stride * i, width,
intbuf + width2 * i, width2, tmpbuf);
for (i = 0; i < width2; ++i) {
fill_col_to_arr(intbuf + i, width2, height, arrbuf);
resize_multistep(arrbuf, height, arrbuf + height, height2, tmpbuf);
fill_arr_to_col(output + i, out_stride, height2, arrbuf + height);
}
free(intbuf);
free(tmpbuf);
free(arrbuf);
}
void vp9_resize_frame420(const uint8_t *const y,
int y_stride,
const uint8_t *const u, const uint8_t *const v,
int uv_stride,
int height, int width,
uint8_t *oy, int oy_stride,
uint8_t *ou, uint8_t *ov, int ouv_stride,
int oheight, int owidth) {
vp9_resize_plane(y, height, width, y_stride,
oy, oheight, owidth, oy_stride);
vp9_resize_plane(u, height / 2, width / 2, uv_stride,
ou, oheight / 2, owidth / 2, ouv_stride);
vp9_resize_plane(v, height / 2, width / 2, uv_stride,
ov, oheight / 2, owidth / 2, ouv_stride);
}
void vp9_resize_frame422(const uint8_t *const y, int y_stride,
const uint8_t *const u, const uint8_t *const v,
int uv_stride,
int height, int width,
uint8_t *oy, int oy_stride,
uint8_t *ou, uint8_t *ov, int ouv_stride,
int oheight, int owidth) {
vp9_resize_plane(y, height, width, y_stride,
oy, oheight, owidth, oy_stride);
vp9_resize_plane(u, height, width / 2, uv_stride,
ou, oheight, owidth / 2, ouv_stride);
vp9_resize_plane(v, height, width / 2, uv_stride,
ov, oheight, owidth / 2, ouv_stride);
}
void vp9_resize_frame444(const uint8_t *const y, int y_stride,
const uint8_t *const u, const uint8_t *const v,
int uv_stride,
int height, int width,
uint8_t *oy, int oy_stride,
uint8_t *ou, uint8_t *ov, int ouv_stride,
int oheight, int owidth) {
vp9_resize_plane(y, height, width, y_stride,
oy, oheight, owidth, oy_stride);
vp9_resize_plane(u, height, width, uv_stride,
ou, oheight, owidth, ouv_stride);
vp9_resize_plane(v, height, width, uv_stride,
ov, oheight, owidth, ouv_stride);
}
/*
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef VP9_ENCODER_VP9_RESIZE_H_
#define VP9_ENCODER_VP9_RESIZE_H_
#include <stdio.h>
void vp9_resize_plane(const uint8_t *const input,
int height,
int width,
int in_stride,
uint8_t *output,
int height2,
int width2,
int out_stride);
void vp9_resize_frame420(const uint8_t *const y,
int y_stride,
const uint8_t *const u,
const uint8_t *const v,
int uv_stride,
int height,
int width,
uint8_t *oy,
int oy_stride,
uint8_t *ou,
uint8_t *ov,
int ouv_stride,
int oheight,
int owidth);
void vp9_resize_frame422(const uint8_t *const y,
int y_stride,
const uint8_t *const u,
const uint8_t *const v,
int uv_stride,
int height,
int width,
uint8_t *oy,
int oy_stride,
uint8_t *ou,
uint8_t *ov,
int ouv_stride,
int oheight,
int owidth);
void vp9_resize_frame444(const uint8_t *const y,
int y_stride,
const uint8_t *const u,
const uint8_t *const v,
int uv_stride,
int height,
int width,
uint8_t *oy,
int oy_stride,
uint8_t *ou,
uint8_t *ov,
int ouv_stride,
int oheight,
int owidth);
#endif // VP9_ENCODER_VP9_RESIZE_H_
......@@ -61,6 +61,8 @@ VP9_CX_SRCS-yes += encoder/vp9_segmentation.c
VP9_CX_SRCS-yes += encoder/vp9_segmentation.h
VP9_CX_SRCS-yes += encoder/vp9_subexp.c
VP9_CX_SRCS-yes += encoder/vp9_subexp.h
VP9_CX_SRCS-yes += encoder/vp9_resize.c
VP9_CX_SRCS-yes += encoder/vp9_resize.h
VP9_CX_SRCS-$(CONFIG_INTERNAL_STATS) += encoder/vp9_ssim.c
VP9_CX_SRCS-yes += encoder/vp9_tokenize.c
VP9_CX_SRCS-yes += encoder/vp9_treewriter.c
......
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