#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config/aom_config.h"
#include "config/av1_rtcd.h"
#include "aom_dsp/aom_dsp_common.h"
#include "aom_dsp/flow_estimation/corner_detect.h"
#include "aom_ports/mem.h"
#include "av1/common/common.h"
#include "av1/common/resize.h"
#include "config/aom_dsp_rtcd.h"
#include "config/aom_scale_rtcd.h"
static const InterpKernel filteredinterp_filters500[(1 << RS_SUBPEL_BITS)] = …;
static const InterpKernel filteredinterp_filters625[(1 << RS_SUBPEL_BITS)] = …;
static const InterpKernel filteredinterp_filters750[(1 << RS_SUBPEL_BITS)] = …;
static const InterpKernel filteredinterp_filters875[(1 << RS_SUBPEL_BITS)] = …;
const int16_t av1_resize_filter_normative[(
1 << RS_SUBPEL_BITS)][UPSCALE_NORMATIVE_TAPS] = …;
#define filteredinterp_filters1000 …
static const InterpKernel *choose_interp_filter(int in_length, int out_length) { … }
static void interpolate_core(const uint8_t *const input, int in_length,
uint8_t *output, int out_length,
const int16_t *interp_filters, int interp_taps) { … }
static void interpolate(const uint8_t *const input, int in_length,
uint8_t *output, int out_length) { … }
int32_t av1_get_upscale_convolve_step(int in_length, int out_length) { … }
static int32_t get_upscale_convolve_x0(int in_length, int out_length,
int32_t x_step_qn) { … }
void down2_symeven(const uint8_t *const input, int length, uint8_t *output,
int start_offset) { … }
static void down2_symodd(const uint8_t *const input, int length,
uint8_t *output) { … }
static int get_down2_length(int length, int steps) { … }
static int get_down2_steps(int in_length, int out_length) { … }
static void resize_multistep(const uint8_t *const input, int length,
uint8_t *output, int olength, uint8_t *otmp) { … }
static void fill_col_to_arr(uint8_t *img, int stride, int len, uint8_t *arr) { … }
static void fill_arr_to_col(uint8_t *img, int stride, int len, uint8_t *arr) { … }
bool av1_resize_vert_dir_c(uint8_t *intbuf, uint8_t *output, int out_stride,
int height, int height2, int width2, int start_col) { … }
void av1_resize_horz_dir_c(const uint8_t *const input, int in_stride,
uint8_t *intbuf, int height, int filtered_length,
int width2) { … }
bool av1_resize_plane_to_half(const uint8_t *const input, int height, int width,
int in_stride, uint8_t *output, int height2,
int width2, int out_stride) { … }
bool should_resize_by_half(int height, int width, int height2, int width2) { … }
bool av1_resize_plane(const uint8_t *input, int height, int width,
int in_stride, uint8_t *output, int height2, int width2,
int out_stride) { … }
static bool upscale_normative_rect(const uint8_t *const input, int height,
int width, int in_stride, uint8_t *output,
int height2, int width2, int out_stride,
int x_step_qn, int x0_qn, int pad_left,
int pad_right) { … }
#if CONFIG_AV1_HIGHBITDEPTH
static void highbd_interpolate_core(const uint16_t *const input, int in_length,
uint16_t *output, int out_length, int bd,
const int16_t *interp_filters,
int interp_taps) {
const int32_t delta =
(((uint32_t)in_length << RS_SCALE_SUBPEL_BITS) + out_length / 2) /
out_length;
const int32_t offset =
in_length > out_length
? (((int32_t)(in_length - out_length) << (RS_SCALE_SUBPEL_BITS - 1)) +
out_length / 2) /
out_length
: -(((int32_t)(out_length - in_length)
<< (RS_SCALE_SUBPEL_BITS - 1)) +
out_length / 2) /
out_length;
uint16_t *optr = output;
int x, x1, x2, sum, k, int_pel, sub_pel;
int32_t y;
x = 0;
y = offset + RS_SCALE_EXTRA_OFF;
while ((y >> RS_SCALE_SUBPEL_BITS) < (interp_taps / 2 - 1)) {
x++;
y += delta;
}
x1 = x;
x = out_length - 1;
y = delta * x + offset + RS_SCALE_EXTRA_OFF;
while ((y >> RS_SCALE_SUBPEL_BITS) + (int32_t)(interp_taps / 2) >=
in_length) {
x--;
y -= delta;
}
x2 = x;
if (x1 > x2) {
for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < out_length;
++x, y += delta) {
int_pel = y >> RS_SCALE_SUBPEL_BITS;
sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
const int16_t *filter = &interp_filters[sub_pel * interp_taps];
sum = 0;
for (k = 0; k < interp_taps; ++k) {
const int pk = int_pel - interp_taps / 2 + 1 + k;
sum += filter[k] * input[AOMMAX(AOMMIN(pk, in_length - 1), 0)];
}
*optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
}
} else {
for (x = 0, y = offset + RS_SCALE_EXTRA_OFF; x < x1; ++x, y += delta) {
int_pel = y >> RS_SCALE_SUBPEL_BITS;
sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
const int16_t *filter = &interp_filters[sub_pel * interp_taps];
sum = 0;
for (k = 0; k < interp_taps; ++k)
sum += filter[k] * input[AOMMAX(int_pel - interp_taps / 2 + 1 + k, 0)];
*optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
}
for (; x <= x2; ++x, y += delta) {
int_pel = y >> RS_SCALE_SUBPEL_BITS;
sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
const int16_t *filter = &interp_filters[sub_pel * interp_taps];
sum = 0;
for (k = 0; k < interp_taps; ++k)
sum += filter[k] * input[int_pel - interp_taps / 2 + 1 + k];
*optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
}
for (; x < out_length; ++x, y += delta) {
int_pel = y >> RS_SCALE_SUBPEL_BITS;
sub_pel = (y >> RS_SCALE_EXTRA_BITS) & RS_SUBPEL_MASK;
const int16_t *filter = &interp_filters[sub_pel * interp_taps];
sum = 0;
for (k = 0; k < interp_taps; ++k)
sum += filter[k] *
input[AOMMIN(int_pel - interp_taps / 2 + 1 + k, in_length - 1)];
*optr++ = clip_pixel_highbd(ROUND_POWER_OF_TWO(sum, FILTER_BITS), bd);
}
}
}
static void highbd_interpolate(const uint16_t *const input, int in_length,
uint16_t *output, int out_length, int bd) {
const InterpKernel *interp_filters =
choose_interp_filter(in_length, out_length);
highbd_interpolate_core(input, in_length, output, out_length, bd,
&interp_filters[0][0], SUBPEL_TAPS);
}
static void highbd_down2_symeven(const uint16_t *const input, int length,
uint16_t *output, int bd) {
static const int16_t *filter = av1_down2_symeven_half_filter;
const int filter_len_half = sizeof(av1_down2_symeven_half_filter) / 2;
int i, j;
uint16_t *optr = output;
int l1 = filter_len_half;
int l2 = (length - filter_len_half);
l1 += (l1 & 1);
l2 += (l2 & 1);
if (l1 > l2) {
for (i = 0; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum +=
(input[AOMMAX(0, i - j)] + input[AOMMIN(i + 1 + j, length - 1)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
} else {
for (i = 0; i < l1; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum += (input[AOMMAX(0, i - j)] + input[i + 1 + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
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_highbd(sum, bd);
}
for (; i < length; i += 2) {
int sum = (1 << (FILTER_BITS - 1));
for (j = 0; j < filter_len_half; ++j) {
sum +=
(input[i - j] + input[AOMMIN(i + 1 + j, length - 1)]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
}
}
static void highbd_down2_symodd(const uint16_t *const input, int length,
uint16_t *output, int bd) {
static const int16_t *filter = av1_down2_symodd_half_filter;
const int filter_len_half = sizeof(av1_down2_symodd_half_filter) / 2;
int i, j;
uint16_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) {
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[AOMMAX(i - j, 0)] + input[AOMMIN(i + j, length - 1)]) *
filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
} else {
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[AOMMAX(i - j, 0)] + input[i + j]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
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_highbd(sum, bd);
}
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[AOMMIN(i + j, length - 1)]) * filter[j];
}
sum >>= FILTER_BITS;
*optr++ = clip_pixel_highbd(sum, bd);
}
}
}
static void highbd_resize_multistep(const uint16_t *const input, int length,
uint16_t *output, int olength,
uint16_t *otmp, int bd) {
if (length == olength) {
memcpy(output, input, sizeof(output[0]) * length);
return;
}
const int steps = get_down2_steps(length, olength);
if (steps > 0) {
uint16_t *out = NULL;
int filteredlength = length;
assert(otmp != NULL);
uint16_t *otmp2 = otmp + get_down2_length(length, 1);
for (int s = 0; s < steps; ++s) {
const int proj_filteredlength = get_down2_length(filteredlength, 1);
const uint16_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)
highbd_down2_symodd(in, filteredlength, out, bd);
else
highbd_down2_symeven(in, filteredlength, out, bd);
filteredlength = proj_filteredlength;
}
if (filteredlength != olength) {
highbd_interpolate(out, filteredlength, output, olength, bd);
}
} else {
highbd_interpolate(input, length, output, olength, bd);
}
}
static void highbd_fill_col_to_arr(uint16_t *img, int stride, int len,
uint16_t *arr) {
int i;
uint16_t *iptr = img;
uint16_t *aptr = arr;
for (i = 0; i < len; ++i, iptr += stride) {
*aptr++ = *iptr;
}
}
static void highbd_fill_arr_to_col(uint16_t *img, int stride, int len,
uint16_t *arr) {
int i;
uint16_t *iptr = img;
uint16_t *aptr = arr;
for (i = 0; i < len; ++i, iptr += stride) {
*iptr = *aptr++;
}
}
void av1_highbd_resize_plane(const uint8_t *input, int height, int width,
int in_stride, uint8_t *output, int height2,
int width2, int out_stride, int bd) {
int i;
uint16_t *intbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * width2 * height);
uint16_t *tmpbuf =
(uint16_t *)aom_malloc(sizeof(uint16_t) * AOMMAX(width, height));
uint16_t *arrbuf = (uint16_t *)aom_malloc(sizeof(uint16_t) * height);
uint16_t *arrbuf2 = (uint16_t *)aom_malloc(sizeof(uint16_t) * height2);
if (intbuf == NULL || tmpbuf == NULL || arrbuf == NULL || arrbuf2 == NULL)
goto Error;
for (i = 0; i < height; ++i) {
highbd_resize_multistep(CONVERT_TO_SHORTPTR(input + in_stride * i), width,
intbuf + width2 * i, width2, tmpbuf, bd);
}
for (i = 0; i < width2; ++i) {
highbd_fill_col_to_arr(intbuf + i, width2, height, arrbuf);
highbd_resize_multistep(arrbuf, height, arrbuf2, height2, tmpbuf, bd);
highbd_fill_arr_to_col(CONVERT_TO_SHORTPTR(output + i), out_stride, height2,
arrbuf2);
}
Error:
aom_free(intbuf);
aom_free(tmpbuf);
aom_free(arrbuf);
aom_free(arrbuf2);
}
static bool highbd_upscale_normative_rect(const uint8_t *const input,
int height, int width, int in_stride,
uint8_t *output, int height2,
int width2, int out_stride,
int x_step_qn, int x0_qn,
int pad_left, int pad_right, int bd) {
assert(width > 0);
assert(height > 0);
assert(width2 > 0);
assert(height2 > 0);
assert(height2 == height);
const int border_cols = UPSCALE_NORMATIVE_TAPS / 2 + 1;
const int border_size = border_cols * sizeof(uint16_t);
uint16_t *tmp_left =
NULL;
uint16_t *tmp_right = NULL;
uint16_t *const input16 = CONVERT_TO_SHORTPTR(input);
uint16_t *const in_tl = input16 - border_cols;
uint16_t *const in_tr = input16 + width;
if (pad_left) {
tmp_left = (uint16_t *)aom_malloc(sizeof(*tmp_left) * border_cols * height);
if (!tmp_left) return false;
for (int i = 0; i < height; i++) {
memcpy(tmp_left + i * border_cols, in_tl + i * in_stride, border_size);
aom_memset16(in_tl + i * in_stride, input16[i * in_stride], border_cols);
}
}
if (pad_right) {
tmp_right =
(uint16_t *)aom_malloc(sizeof(*tmp_right) * border_cols * height);
if (!tmp_right) {
aom_free(tmp_left);
return false;
}
for (int i = 0; i < height; i++) {
memcpy(tmp_right + i * border_cols, in_tr + i * in_stride, border_size);
aom_memset16(in_tr + i * in_stride, input16[i * in_stride + width - 1],
border_cols);
}
}
av1_highbd_convolve_horiz_rs(CONVERT_TO_SHORTPTR(input - 1), in_stride,
CONVERT_TO_SHORTPTR(output), out_stride, width2,
height2, &av1_resize_filter_normative[0][0],
x0_qn, x_step_qn, bd);
if (pad_left) {
for (int i = 0; i < height; i++) {
memcpy(in_tl + i * in_stride, tmp_left + i * border_cols, border_size);
}
aom_free(tmp_left);
}
if (pad_right) {
for (int i = 0; i < height; i++) {
memcpy(in_tr + i * in_stride, tmp_right + i * border_cols, border_size);
}
aom_free(tmp_right);
}
return true;
}
#endif
void av1_resize_frame420(const uint8_t *y, int y_stride, const uint8_t *u,
const uint8_t *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 av1_resize_and_extend_frame_c(const YV12_BUFFER_CONFIG *src,
YV12_BUFFER_CONFIG *dst,
const InterpFilter filter,
const int phase_scaler,
const int num_planes) { … }
bool av1_resize_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
YV12_BUFFER_CONFIG *dst, int bd,
int num_planes) { … }
void av1_upscale_normative_rows(const AV1_COMMON *cm, const uint8_t *src,
int src_stride, uint8_t *dst, int dst_stride,
int plane, int rows) { … }
void av1_upscale_normative_and_extend_frame(const AV1_COMMON *cm,
const YV12_BUFFER_CONFIG *src,
YV12_BUFFER_CONFIG *dst) { … }
YV12_BUFFER_CONFIG *av1_realloc_and_scale_if_required(
AV1_COMMON *cm, YV12_BUFFER_CONFIG *unscaled, YV12_BUFFER_CONFIG *scaled,
const InterpFilter filter, const int phase, const bool use_optimized_scaler,
const bool for_psnr, const int border_in_pixels, const bool alloc_pyramid) { … }
static void calculate_scaled_size_helper(int *dim, int denom) { … }
void av1_calculate_scaled_size(int *width, int *height, int resize_denom) { … }
void av1_calculate_scaled_superres_size(int *width, int *height,
int superres_denom) { … }
void av1_calculate_unscaled_superres_size(int *width, int *height, int denom) { … }
static void copy_buffer_config(const YV12_BUFFER_CONFIG *const src,
YV12_BUFFER_CONFIG *const dst) { … }
void av1_superres_upscale(AV1_COMMON *cm, BufferPool *const pool,
bool alloc_pyramid) { … }