2014-06-05 12:04:12 +00:00
|
|
|
/*****************************************************************************
|
2021-11-23 06:46:06 +00:00
|
|
|
* This file is part of uvg266 VVC encoder.
|
2014-06-05 12:04:12 +00:00
|
|
|
*
|
2021-10-07 08:32:59 +00:00
|
|
|
* Copyright (c) 2021, Tampere University, ITU/ISO/IEC, project contributors
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer in the documentation and/or
|
|
|
|
* other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* * Neither the name of the Tampere University or ITU/ISO/IEC nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND ON
|
|
|
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
* INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
|
2014-06-05 12:04:12 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "image.h"
|
|
|
|
|
2016-04-01 14:14:23 +00:00
|
|
|
#include <limits.h>
|
2014-06-05 12:04:12 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2017-05-19 10:08:36 +00:00
|
|
|
#include "strategies/strategies-ipol.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "strategies/strategies-picture.h"
|
|
|
|
#include "threads.h"
|
2014-06-05 12:04:12 +00:00
|
|
|
|
2016-08-16 16:03:21 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocate a new image with 420.
|
|
|
|
* This function signature is part of the libkvz API.
|
|
|
|
* \return image pointer or NULL on failure
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture * uvg_image_alloc_420(const int32_t width, const int32_t height)
|
2016-08-16 16:03:21 +00:00
|
|
|
{
|
2022-04-28 11:18:09 +00:00
|
|
|
return uvg_image_alloc(UVG_CSP_420, width, height);
|
2016-08-16 16:03:21 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
/**
|
2015-06-17 08:06:09 +00:00
|
|
|
* \brief Allocate a new image.
|
|
|
|
* \return image pointer or NULL on failure
|
2014-06-05 12:04:12 +00:00
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture * uvg_image_alloc(enum uvg_chroma_format chroma_format, const int32_t width, const int32_t height)
|
2014-06-05 12:04:12 +00:00
|
|
|
{
|
|
|
|
//Assert that we have a well defined image
|
|
|
|
assert((width % 2) == 0);
|
|
|
|
assert((height % 2) == 0);
|
|
|
|
|
2019-01-29 23:33:39 +00:00
|
|
|
const size_t simd_padding_width = 64;
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture *im = MALLOC(uvg_picture, 1);
|
2014-06-05 12:04:12 +00:00
|
|
|
if (!im) return NULL;
|
2015-06-17 08:06:09 +00:00
|
|
|
|
2019-08-28 13:08:26 +00:00
|
|
|
//Add 4 pixel boundary to each side of luma for ALF
|
|
|
|
//This results also 2 pixel boundary for chroma
|
2021-01-14 12:30:50 +00:00
|
|
|
unsigned int luma_size = (width + FRAME_PADDING_LUMA) * (height + FRAME_PADDING_LUMA);
|
2019-10-07 12:16:38 +00:00
|
|
|
|
2016-08-16 16:03:21 +00:00
|
|
|
unsigned chroma_sizes[] = { 0, luma_size / 4, luma_size / 2, luma_size };
|
|
|
|
unsigned chroma_size = chroma_sizes[chroma_format];
|
|
|
|
|
|
|
|
im->chroma_format = chroma_format;
|
2015-06-17 08:06:09 +00:00
|
|
|
|
2019-01-29 23:33:39 +00:00
|
|
|
//Allocate memory, pad the full data buffer from both ends
|
2022-04-28 11:18:09 +00:00
|
|
|
im->fulldata_buf = MALLOC_SIMD_PADDED(uvg_pixel, (luma_size + 2 * chroma_size), simd_padding_width * 2);
|
2019-01-29 23:33:39 +00:00
|
|
|
if (!im->fulldata_buf) {
|
2015-06-17 08:06:09 +00:00
|
|
|
free(im);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-15 08:01:38 +00:00
|
|
|
//Shift the image to allow ALF filtering
|
2015-06-17 08:06:09 +00:00
|
|
|
im->refcount = 1; //We give a reference to caller
|
2014-06-05 12:04:12 +00:00
|
|
|
im->width = width;
|
|
|
|
im->height = height;
|
2021-01-14 12:30:50 +00:00
|
|
|
im->stride = width + FRAME_PADDING_LUMA;
|
2016-08-16 16:03:21 +00:00
|
|
|
im->chroma_format = chroma_format;
|
2021-01-14 12:30:50 +00:00
|
|
|
const int padding_before_first_pixel_luma = (FRAME_PADDING_LUMA / 2) * (im->stride) + FRAME_PADDING_LUMA / 2;
|
|
|
|
const int padding_before_first_pixel_chroma = (FRAME_PADDING_CHROMA / 2) * (im->stride/2) + FRAME_PADDING_CHROMA / 2;
|
2022-04-28 11:18:09 +00:00
|
|
|
im->fulldata = &im->fulldata_buf[padding_before_first_pixel_luma] + simd_padding_width / sizeof(uvg_pixel);
|
2021-01-14 12:30:50 +00:00
|
|
|
im->base_image = im;
|
2015-06-17 08:06:09 +00:00
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
im->y = im->data[COLOR_Y] = &im->fulldata[0];
|
2016-08-16 16:03:21 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
if (chroma_format == UVG_CSP_400) {
|
2016-08-16 16:03:21 +00:00
|
|
|
im->u = im->data[COLOR_U] = NULL;
|
|
|
|
im->v = im->data[COLOR_V] = NULL;
|
|
|
|
} else {
|
2021-01-14 12:30:50 +00:00
|
|
|
im->u = im->data[COLOR_U] = &im->fulldata[luma_size - padding_before_first_pixel_luma + padding_before_first_pixel_chroma];
|
|
|
|
im->v = im->data[COLOR_V] = &im->fulldata[luma_size - padding_before_first_pixel_luma + chroma_size + padding_before_first_pixel_chroma];
|
2016-08-16 16:03:21 +00:00
|
|
|
}
|
2014-06-05 12:04:12 +00:00
|
|
|
|
2015-09-07 08:56:49 +00:00
|
|
|
im->pts = 0;
|
2015-09-08 08:58:35 +00:00
|
|
|
im->dts = 0;
|
2015-09-07 08:56:49 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
im->interlacing = UVG_INTERLACING_NONE;
|
2016-01-25 17:05:10 +00:00
|
|
|
|
2018-03-21 08:46:30 +00:00
|
|
|
im->roi.roi_array = NULL;
|
|
|
|
im->roi.width = 0;
|
|
|
|
im->roi.height = 0;
|
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
return im;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-17 08:06:09 +00:00
|
|
|
* \brief Free an image.
|
|
|
|
*
|
|
|
|
* Decrement reference count of the image and deallocate associated memory
|
|
|
|
* if no references exist any more.
|
|
|
|
*
|
|
|
|
* \param im image to free
|
2014-06-05 12:04:12 +00:00
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_image_free(uvg_picture *const im)
|
2014-06-05 12:04:12 +00:00
|
|
|
{
|
2015-06-17 08:06:09 +00:00
|
|
|
if (im == NULL) return;
|
2014-06-05 12:04:12 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
int32_t new_refcount = UVG_ATOMIC_DEC(&(im->refcount));
|
2015-06-17 08:06:09 +00:00
|
|
|
if (new_refcount > 0) {
|
|
|
|
// There are still references so we don't free the data yet.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (im->base_image != im) {
|
|
|
|
// Free our reference to the base image.
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_image_free(im->base_image);
|
2015-06-17 08:06:09 +00:00
|
|
|
} else {
|
2019-01-29 23:33:39 +00:00
|
|
|
free(im->fulldata_buf);
|
2018-03-21 08:46:30 +00:00
|
|
|
if (im->roi.roi_array) FREE_POINTER(im->roi.roi_array);
|
2015-06-17 08:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure freed data won't be used.
|
|
|
|
im->base_image = NULL;
|
2019-01-29 23:33:39 +00:00
|
|
|
im->fulldata_buf = NULL;
|
2015-06-17 08:06:09 +00:00
|
|
|
im->fulldata = NULL;
|
|
|
|
im->y = im->u = im->v = NULL;
|
|
|
|
im->data[COLOR_Y] = im->data[COLOR_U] = im->data[COLOR_V] = NULL;
|
|
|
|
free(im);
|
2014-06-05 12:04:12 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 08:06:09 +00:00
|
|
|
/**
|
|
|
|
* \brief Get a new pointer to an image.
|
|
|
|
*
|
|
|
|
* Increment reference count and return the image.
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture *uvg_image_copy_ref(uvg_picture *im)
|
2015-06-17 08:06:09 +00:00
|
|
|
{
|
2022-04-28 11:18:09 +00:00
|
|
|
int32_t new_refcount = UVG_ATOMIC_INC(&im->refcount);
|
2018-06-12 06:35:07 +00:00
|
|
|
// The caller should have had another reference and we added one
|
|
|
|
// reference so refcount should be at least 2.
|
|
|
|
assert(new_refcount >= 2);
|
2015-06-17 08:06:09 +00:00
|
|
|
return im;
|
|
|
|
}
|
2014-06-05 12:04:12 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture *uvg_image_make_subimage(uvg_picture *const orig_image,
|
2015-06-17 08:06:09 +00:00
|
|
|
const unsigned x_offset,
|
|
|
|
const unsigned y_offset,
|
|
|
|
const unsigned width,
|
|
|
|
const unsigned height)
|
2014-06-05 12:04:12 +00:00
|
|
|
{
|
2015-06-17 08:06:09 +00:00
|
|
|
// Assert that we have a well defined image
|
2014-06-05 12:04:12 +00:00
|
|
|
assert((width % 2) == 0);
|
|
|
|
assert((height % 2) == 0);
|
2015-06-17 08:06:09 +00:00
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
assert((x_offset % 2) == 0);
|
|
|
|
assert((y_offset % 2) == 0);
|
2015-06-17 08:06:09 +00:00
|
|
|
|
|
|
|
assert(x_offset + width <= orig_image->width);
|
|
|
|
assert(y_offset + height <= orig_image->height);
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_picture *im = MALLOC(uvg_picture, 1);
|
2015-06-17 08:06:09 +00:00
|
|
|
if (!im) return NULL;
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
im->base_image = uvg_image_copy_ref(orig_image->base_image);
|
2015-06-17 08:06:09 +00:00
|
|
|
im->refcount = 1; // We give a reference to caller
|
2014-06-05 12:04:12 +00:00
|
|
|
im->width = width;
|
|
|
|
im->height = height;
|
2015-06-17 08:06:09 +00:00
|
|
|
im->stride = orig_image->stride;
|
2016-08-16 16:03:21 +00:00
|
|
|
im->chroma_format = orig_image->chroma_format;
|
2015-06-17 08:06:09 +00:00
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
im->y = im->data[COLOR_Y] = &orig_image->y[x_offset + y_offset * orig_image->stride];
|
2022-04-28 11:18:09 +00:00
|
|
|
if (orig_image->chroma_format != UVG_CSP_400) {
|
2016-08-16 16:03:21 +00:00
|
|
|
im->u = im->data[COLOR_U] = &orig_image->u[x_offset / 2 + y_offset / 2 * orig_image->stride / 2];
|
|
|
|
im->v = im->data[COLOR_V] = &orig_image->v[x_offset / 2 + y_offset / 2 * orig_image->stride / 2];
|
|
|
|
}
|
2014-06-05 12:04:12 +00:00
|
|
|
|
2015-09-07 08:56:49 +00:00
|
|
|
im->pts = 0;
|
2015-09-08 08:58:35 +00:00
|
|
|
im->dts = 0;
|
2015-09-07 08:56:49 +00:00
|
|
|
|
2018-03-21 08:46:30 +00:00
|
|
|
im->roi = orig_image->roi;
|
|
|
|
|
2014-06-05 12:04:12 +00:00
|
|
|
return im;
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
yuv_t * uvg_yuv_t_alloc(int luma_size, int chroma_size)
|
2014-06-05 12:54:58 +00:00
|
|
|
{
|
|
|
|
yuv_t *yuv = (yuv_t *)malloc(sizeof(*yuv));
|
|
|
|
yuv->size = luma_size;
|
|
|
|
|
2016-08-17 13:43:16 +00:00
|
|
|
// Get buffers with separate mallocs in order to take advantage of
|
|
|
|
// automatic buffer overrun checks.
|
2022-04-28 11:18:09 +00:00
|
|
|
yuv->y = (uvg_pixel *)malloc(luma_size * sizeof(*yuv->y));
|
2016-08-17 13:43:16 +00:00
|
|
|
if (chroma_size == 0) {
|
|
|
|
yuv->u = NULL;
|
|
|
|
yuv->v = NULL;
|
|
|
|
} else {
|
2022-04-28 11:18:09 +00:00
|
|
|
yuv->u = (uvg_pixel *)malloc(chroma_size * sizeof(*yuv->u));
|
|
|
|
yuv->v = (uvg_pixel *)malloc(chroma_size * sizeof(*yuv->v));
|
2016-08-17 13:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
return yuv;
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_yuv_t_free(yuv_t *yuv)
|
2014-06-05 12:54:58 +00:00
|
|
|
{
|
2017-06-30 13:12:52 +00:00
|
|
|
if (yuv) {
|
|
|
|
FREE_POINTER(yuv->y);
|
|
|
|
FREE_POINTER(yuv->u);
|
|
|
|
FREE_POINTER(yuv->v);
|
|
|
|
}
|
|
|
|
FREE_POINTER(yuv);
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
hi_prec_buf_t * uvg_hi_prec_buf_t_alloc(int luma_size)
|
2015-08-04 12:42:18 +00:00
|
|
|
{
|
|
|
|
// Get buffers with separate mallocs in order to take advantage of
|
|
|
|
// automatic buffer overrun checks.
|
|
|
|
hi_prec_buf_t *yuv = (hi_prec_buf_t *)malloc(sizeof(*yuv));
|
|
|
|
yuv->y = (int16_t *)malloc(luma_size * sizeof(*yuv->y));
|
|
|
|
yuv->u = (int16_t *)malloc(luma_size / 2 * sizeof(*yuv->u));
|
|
|
|
yuv->v = (int16_t *)malloc(luma_size / 2 * sizeof(*yuv->v));
|
2021-08-13 12:37:23 +00:00
|
|
|
yuv->joint_u = (int16_t *)malloc(luma_size / 2 * sizeof(*yuv->u));
|
|
|
|
yuv->joint_v = (int16_t *)malloc(luma_size / 2 * sizeof(*yuv->v));
|
2015-08-04 12:42:18 +00:00
|
|
|
yuv->size = luma_size;
|
|
|
|
|
|
|
|
return yuv;
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_hi_prec_buf_t_free(hi_prec_buf_t * yuv)
|
2015-08-04 12:42:18 +00:00
|
|
|
{
|
|
|
|
free(yuv->y);
|
|
|
|
free(yuv->u);
|
|
|
|
free(yuv->v);
|
2021-08-13 12:37:23 +00:00
|
|
|
free(yuv->joint_v);
|
|
|
|
free(yuv->joint_u);
|
2015-08-04 12:42:18 +00:00
|
|
|
free(yuv);
|
|
|
|
}
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
static INLINE uint32_t reg_sad_maybe_optimized(const uvg_pixel * const data1, const uvg_pixel * const data2,
|
2019-01-22 17:56:15 +00:00
|
|
|
const int32_t width, const int32_t height, const uint32_t stride1,
|
|
|
|
const uint32_t stride2, optimized_sad_func_ptr_t optimized_sad)
|
|
|
|
{
|
|
|
|
if (optimized_sad != NULL)
|
|
|
|
return optimized_sad(data1, data2, height, stride1, stride2);
|
|
|
|
else
|
2022-04-28 11:18:09 +00:00
|
|
|
return uvg_reg_sad(data1, data2, width, height, stride1, stride2);
|
2019-01-22 17:56:15 +00:00
|
|
|
}
|
2015-08-04 12:42:18 +00:00
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
/**
|
|
|
|
* \brief Diagonally interpolate SAD outside the frame.
|
|
|
|
*
|
|
|
|
* \param data1 Starting point of the first picture.
|
|
|
|
* \param data2 Starting point of the second picture.
|
|
|
|
* \param width Width of the region for which SAD is calculated.
|
|
|
|
* \param height Height of the region for which SAD is calculated.
|
|
|
|
* \param width Width of the pixel array.
|
|
|
|
*
|
|
|
|
* \returns Sum of Absolute Differences
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
static unsigned cor_sad(const uvg_pixel *pic_data, const uvg_pixel *ref_data,
|
2014-06-12 05:13:37 +00:00
|
|
|
int block_width, int block_height, unsigned pic_stride)
|
2014-06-05 12:54:58 +00:00
|
|
|
{
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_pixel ref = *ref_data;
|
2014-06-05 12:54:58 +00:00
|
|
|
int x, y;
|
|
|
|
unsigned sad = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < block_height; ++y) {
|
|
|
|
for (x = 0; x < block_width; ++x) {
|
2014-06-12 05:13:37 +00:00
|
|
|
sad += abs(pic_data[y * pic_stride + x] - ref);
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sad;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Handle special cases of comparing blocks that are not completely
|
|
|
|
* inside the frame.
|
|
|
|
*
|
|
|
|
* \param pic First frame.
|
|
|
|
* \param ref Second frame.
|
|
|
|
* \param pic_x X coordinate of the first block.
|
|
|
|
* \param pic_y Y coordinate of the first block.
|
|
|
|
* \param ref_x X coordinate of the second block.
|
|
|
|
* \param ref_y Y coordinate of the second block.
|
|
|
|
* \param block_width Width of the blocks.
|
|
|
|
* \param block_height Height of the blocks.
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
static unsigned image_interpolated_sad(const uvg_picture *pic, const uvg_picture *ref,
|
2014-06-05 12:54:58 +00:00
|
|
|
int pic_x, int pic_y, int ref_x, int ref_y,
|
2019-01-21 18:41:47 +00:00
|
|
|
int block_width, int block_height,
|
|
|
|
optimized_sad_func_ptr_t optimized_sad)
|
2014-06-05 12:54:58 +00:00
|
|
|
{
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_pixel *pic_data, *ref_data;
|
2014-06-05 12:54:58 +00:00
|
|
|
|
|
|
|
int left, right, top, bottom;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
// Change the movement vector to point right next to the frame. This doesn't
|
|
|
|
// affect the result but removes some special cases.
|
|
|
|
if (ref_x > ref->width) ref_x = ref->width;
|
|
|
|
if (ref_y > ref->height) ref_y = ref->height;
|
|
|
|
if (ref_x + block_width < 0) ref_x = -block_width;
|
|
|
|
if (ref_y + block_height < 0) ref_y = -block_height;
|
|
|
|
|
|
|
|
// These are the number of pixels by how far the movement vector points
|
|
|
|
// outside the frame. They are always >= 0. If all of them are 0, the
|
|
|
|
// movement vector doesn't point outside the frame.
|
|
|
|
left = (ref_x < 0) ? -ref_x : 0;
|
|
|
|
top = (ref_y < 0) ? -ref_y : 0;
|
|
|
|
right = (ref_x + block_width > ref->width) ? ref_x + block_width - ref->width : 0;
|
|
|
|
bottom = (ref_y + block_height > ref->height) ? ref_y + block_height - ref->height : 0;
|
|
|
|
|
|
|
|
// Center picture to the current block and reference to the point where
|
|
|
|
// movement vector is pointing to. That point might be outside the buffer,
|
|
|
|
// but that is ok because we project the movement vector to the buffer
|
|
|
|
// before dereferencing the pointer.
|
2014-06-12 05:13:37 +00:00
|
|
|
pic_data = &pic->y[pic_y * pic->stride + pic_x];
|
|
|
|
ref_data = &ref->y[ref_y * ref->stride + ref_x];
|
2014-06-05 12:54:58 +00:00
|
|
|
|
|
|
|
// The handling of movement vectors that point outside the picture is done
|
|
|
|
// in the following way.
|
|
|
|
// - Correct the index of ref_data so that it points to the top-left
|
|
|
|
// of the area we want to compare against.
|
|
|
|
// - Correct the index of pic_data to point inside the current block, so
|
|
|
|
// that we compare the right part of the block to the ref_data.
|
|
|
|
// - Reduce block_width and block_height so that the the size of the area
|
|
|
|
// being compared is correct.
|
2019-01-31 12:53:57 +00:00
|
|
|
//
|
|
|
|
// NOTE: No more correct since hor_sad was modified to be a separate
|
|
|
|
// strategy
|
2014-06-05 12:54:58 +00:00
|
|
|
if (top && left) {
|
|
|
|
result += cor_sad(pic_data,
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride + left],
|
|
|
|
left, top, pic->stride);
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(&pic_data[left],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride + left],
|
|
|
|
block_width - left, top, pic->stride);
|
2019-01-31 12:53:57 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_hor_sad(pic_data + top * pic->stride,
|
2019-01-31 12:53:57 +00:00
|
|
|
ref_data + top * ref->stride,
|
|
|
|
block_width, block_height - top,
|
|
|
|
pic->stride, ref->stride,
|
|
|
|
left, right);
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
} else if (top && right) {
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(pic_data,
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride],
|
|
|
|
block_width - right, top, pic->stride);
|
2014-06-05 12:54:58 +00:00
|
|
|
result += cor_sad(&pic_data[block_width - right],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride + (block_width - right - 1)],
|
|
|
|
right, top, pic->stride);
|
2019-01-31 12:53:57 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_hor_sad(pic_data + top * pic->stride,
|
2019-01-31 12:53:57 +00:00
|
|
|
ref_data + top * ref->stride,
|
|
|
|
block_width, block_height - top,
|
|
|
|
pic->stride, ref->stride,
|
|
|
|
left, right);
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
} else if (bottom && left) {
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_hor_sad(pic_data, ref_data, block_width, block_height - bottom,
|
2019-01-31 12:53:57 +00:00
|
|
|
pic->stride, ref->stride, left, right);
|
|
|
|
|
2014-06-12 05:13:37 +00:00
|
|
|
result += cor_sad(&pic_data[(block_height - bottom) * pic->stride],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->stride + left],
|
|
|
|
left, bottom, pic->stride);
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(&pic_data[(block_height - bottom) * pic->stride + left],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[(block_height - bottom - 1) * ref->stride + left],
|
|
|
|
block_width - left, bottom, pic->stride);
|
2014-06-05 12:54:58 +00:00
|
|
|
} else if (bottom && right) {
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_hor_sad(pic_data, ref_data, block_width, block_height - bottom,
|
2019-01-31 12:53:57 +00:00
|
|
|
pic->stride, ref->stride, left, right);
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(&pic_data[(block_height - bottom) * pic->stride],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[(block_height - bottom - 1) * ref->stride],
|
|
|
|
block_width - right, bottom, pic->stride);
|
|
|
|
result += cor_sad(&pic_data[(block_height - bottom) * pic->stride + block_width - right],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->stride + block_width - right - 1],
|
|
|
|
right, bottom, pic->stride);
|
2014-06-05 12:54:58 +00:00
|
|
|
} else if (top) {
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(pic_data,
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride],
|
|
|
|
block_width, top, pic->stride);
|
2019-01-21 18:41:47 +00:00
|
|
|
result += reg_sad_maybe_optimized(&pic_data[top * pic->stride],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[top * ref->stride],
|
2019-01-21 18:41:47 +00:00
|
|
|
block_width, block_height - top, pic->stride, ref->stride,
|
|
|
|
optimized_sad);
|
2014-06-05 12:54:58 +00:00
|
|
|
} else if (bottom) {
|
2019-01-21 18:41:47 +00:00
|
|
|
result += reg_sad_maybe_optimized(pic_data,
|
2014-06-05 12:54:58 +00:00
|
|
|
ref_data,
|
2019-01-21 18:41:47 +00:00
|
|
|
block_width, block_height - bottom, pic->stride, ref->stride,
|
|
|
|
optimized_sad);
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_ver_sad(&pic_data[(block_height - bottom) * pic->stride],
|
2014-06-12 05:13:37 +00:00
|
|
|
&ref_data[(block_height - bottom - 1) * ref->stride],
|
|
|
|
block_width, bottom, pic->stride);
|
2019-01-31 12:53:57 +00:00
|
|
|
} else if (left | right) {
|
2022-04-28 11:18:09 +00:00
|
|
|
result += uvg_hor_sad(pic_data, ref_data,
|
2019-01-30 20:57:06 +00:00
|
|
|
block_width, block_height, pic->stride,
|
|
|
|
ref->stride, left, right);
|
2014-06-05 12:54:58 +00:00
|
|
|
} else {
|
2019-01-31 12:53:57 +00:00
|
|
|
result += reg_sad_maybe_optimized(pic_data, ref_data,
|
|
|
|
block_width, block_height,
|
|
|
|
pic->stride, ref->stride,
|
|
|
|
optimized_sad);
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-24 22:55:10 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculate interpolated SAD between two blocks.
|
|
|
|
*
|
|
|
|
* \param pic Image for the block we are trying to find.
|
|
|
|
* \param ref Image where we are trying to find the block.
|
|
|
|
*
|
2017-07-20 07:46:54 +00:00
|
|
|
* \returns Sum of absolute differences
|
2015-01-24 22:55:10 +00:00
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
unsigned uvg_image_calc_sad(const uvg_picture *pic,
|
|
|
|
const uvg_picture *ref,
|
2017-05-17 11:31:01 +00:00
|
|
|
int pic_x,
|
|
|
|
int pic_y,
|
|
|
|
int ref_x,
|
|
|
|
int ref_y,
|
|
|
|
int block_width,
|
2019-01-21 15:20:51 +00:00
|
|
|
int block_height,
|
|
|
|
optimized_sad_func_ptr_t optimized_sad)
|
2017-07-20 07:46:54 +00:00
|
|
|
{
|
2014-06-05 12:54:58 +00:00
|
|
|
assert(pic_x >= 0 && pic_x <= pic->width - block_width);
|
|
|
|
assert(pic_y >= 0 && pic_y <= pic->height - block_height);
|
2015-01-24 22:55:10 +00:00
|
|
|
|
2019-01-22 17:56:15 +00:00
|
|
|
uint32_t res;
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
if (ref_x >= 0 && ref_x <= ref->width - block_width &&
|
|
|
|
ref_y >= 0 && ref_y <= ref->height - block_height)
|
|
|
|
{
|
|
|
|
// Reference block is completely inside the frame, so just calculate the
|
|
|
|
// SAD directly. This is the most common case, which is why it's first.
|
2022-04-28 11:18:09 +00:00
|
|
|
const uvg_pixel *pic_data = &pic->y[pic_y * pic->stride + pic_x];
|
|
|
|
const uvg_pixel *ref_data = &ref->y[ref_y * ref->stride + ref_x];
|
2019-01-21 15:20:51 +00:00
|
|
|
|
2019-01-22 17:56:15 +00:00
|
|
|
res = reg_sad_maybe_optimized(pic_data,
|
|
|
|
ref_data,
|
|
|
|
block_width,
|
|
|
|
block_height,
|
|
|
|
pic->stride,
|
|
|
|
ref->stride,
|
|
|
|
optimized_sad);
|
2014-06-05 12:54:58 +00:00
|
|
|
} else {
|
|
|
|
// Call a routine that knows how to interpolate pixels outside the frame.
|
2019-01-22 17:56:15 +00:00
|
|
|
res = image_interpolated_sad(pic, ref, pic_x, pic_y, ref_x, ref_y, block_width, block_height, optimized_sad);
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
2022-04-28 11:18:09 +00:00
|
|
|
return res >> (UVG_BIT_DEPTH - 8);
|
2014-06-05 12:54:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-17 11:31:01 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculate interpolated SATD between two blocks.
|
|
|
|
*
|
|
|
|
* \param pic Image for the block we are trying to find.
|
|
|
|
* \param ref Image where we are trying to find the block.
|
|
|
|
*/
|
2022-04-28 11:18:09 +00:00
|
|
|
unsigned uvg_image_calc_satd(const uvg_picture *pic,
|
|
|
|
const uvg_picture *ref,
|
2017-05-17 11:31:01 +00:00
|
|
|
int pic_x,
|
|
|
|
int pic_y,
|
|
|
|
int ref_x,
|
|
|
|
int ref_y,
|
|
|
|
int block_width,
|
|
|
|
int block_height)
|
|
|
|
{
|
|
|
|
assert(pic_x >= 0 && pic_x <= pic->width - block_width);
|
|
|
|
assert(pic_y >= 0 && pic_y <= pic->height - block_height);
|
|
|
|
|
|
|
|
if (ref_x >= 0 && ref_x <= ref->width - block_width &&
|
|
|
|
ref_y >= 0 && ref_y <= ref->height - block_height)
|
|
|
|
{
|
|
|
|
// Reference block is completely inside the frame, so just calculate the
|
|
|
|
// SAD directly. This is the most common case, which is why it's first.
|
2022-04-28 11:18:09 +00:00
|
|
|
const uvg_pixel *pic_data = &pic->y[pic_y * pic->stride + pic_x];
|
|
|
|
const uvg_pixel *ref_data = &ref->y[ref_y * ref->stride + ref_x];
|
|
|
|
return uvg_satd_any_size(block_width,
|
2017-05-17 11:31:01 +00:00
|
|
|
block_height,
|
|
|
|
pic_data,
|
|
|
|
pic->stride,
|
|
|
|
ref_data,
|
2022-04-28 11:18:09 +00:00
|
|
|
ref->stride) >> (UVG_BIT_DEPTH - 8);
|
2017-05-17 11:31:01 +00:00
|
|
|
} else {
|
2017-05-19 10:08:36 +00:00
|
|
|
// Extrapolate pixels from outside the frame.
|
2021-03-02 17:48:57 +00:00
|
|
|
|
|
|
|
// Space for extrapolated pixels and the part from the picture
|
|
|
|
// The extrapolation function will set the pointers and stride.
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_pixel ext_buffer[LCU_LUMA_SIZE];
|
|
|
|
uvg_pixel *ext = NULL;
|
|
|
|
uvg_pixel *ext_origin = NULL;
|
2021-03-02 17:48:57 +00:00
|
|
|
int ext_s = 0;
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_epol_args epol_args = {
|
2021-03-02 17:48:57 +00:00
|
|
|
.src = ref->y,
|
|
|
|
.src_w = ref->width,
|
|
|
|
.src_h = ref->height,
|
|
|
|
.src_s = ref->stride,
|
|
|
|
.blk_x = ref_x,
|
|
|
|
.blk_y = ref_y,
|
|
|
|
.blk_w = block_width,
|
|
|
|
.blk_h = block_height,
|
|
|
|
.pad_l = 0,
|
|
|
|
.pad_r = 0,
|
|
|
|
.pad_t = 0,
|
|
|
|
.pad_b = 0,
|
2021-03-08 14:05:34 +00:00
|
|
|
.pad_b_simd = 0,
|
2021-03-02 17:48:57 +00:00
|
|
|
};
|
|
|
|
|
2021-03-05 16:50:00 +00:00
|
|
|
// Initialize separately. Gets rid of warning
|
|
|
|
// about using nonstandard extension.
|
|
|
|
epol_args.buf = ext_buffer;
|
|
|
|
epol_args.ext = &ext;
|
|
|
|
epol_args.ext_origin = &ext_origin;
|
|
|
|
epol_args.ext_s = &ext_s;
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
uvg_get_extended_block(&epol_args);
|
2017-05-19 10:08:36 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
const uvg_pixel *pic_data = &pic->y[pic_y * pic->stride + pic_x];
|
2017-05-19 10:08:36 +00:00
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
unsigned satd = uvg_satd_any_size(block_width,
|
2021-03-02 17:48:57 +00:00
|
|
|
block_height,
|
|
|
|
pic_data,
|
|
|
|
pic->stride,
|
|
|
|
ext_origin,
|
2022-04-28 11:18:09 +00:00
|
|
|
ext_s) >> (UVG_BIT_DEPTH - 8);
|
2017-05-19 10:08:36 +00:00
|
|
|
|
|
|
|
return satd;
|
2017-05-17 11:31:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-04-06 15:44:04 +00:00
|
|
|
/**
|
|
|
|
* \brief BLock Image Transfer from one buffer to another.
|
|
|
|
*
|
|
|
|
* It's a stupidly simple loop that copies pixels.
|
|
|
|
*
|
|
|
|
* \param orig Start of the originating buffer.
|
|
|
|
* \param dst Start of the destination buffer.
|
|
|
|
* \param width Width of the copied region.
|
|
|
|
* \param height Height of the copied region.
|
|
|
|
* \param orig_stride Width of a row in the originating buffer.
|
|
|
|
* \param dst_stride Width of a row in the destination buffer.
|
|
|
|
*
|
|
|
|
* This should be inlined, but it's defined here for now to see if Visual
|
|
|
|
* Studios LTCG will inline it.
|
|
|
|
*/
|
|
|
|
#define BLIT_PIXELS_CASE(n) case n:\
|
|
|
|
for (y = 0; y < n; ++y) {\
|
2022-04-28 11:18:09 +00:00
|
|
|
memcpy(&dst[y*dst_stride], &orig[y*orig_stride], n * sizeof(uvg_pixel));\
|
2016-04-06 15:44:04 +00:00
|
|
|
}\
|
|
|
|
break;
|
|
|
|
|
2022-04-28 11:18:09 +00:00
|
|
|
void uvg_pixels_blit(const uvg_pixel * const orig, uvg_pixel * const dst,
|
2016-04-06 15:44:04 +00:00
|
|
|
const unsigned width, const unsigned height,
|
|
|
|
const unsigned orig_stride, const unsigned dst_stride)
|
|
|
|
{
|
|
|
|
unsigned y;
|
|
|
|
//There is absolutely no reason to have a width greater than the source or the destination stride.
|
|
|
|
assert(width <= orig_stride);
|
|
|
|
assert(width <= dst_stride);
|
|
|
|
|
|
|
|
#ifdef CHECKPOINTS
|
|
|
|
char *buffer = malloc((3 * width + 1) * sizeof(char));
|
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
int p;
|
|
|
|
for (p = 0; p < width; ++p) {
|
|
|
|
sprintf((buffer + 3*p), "%02X ", orig[y*orig_stride]);
|
|
|
|
}
|
|
|
|
buffer[3*width] = 0;
|
2022-04-28 11:18:09 +00:00
|
|
|
CHECKPOINT("uvg_pixels_blit_avx2: %04d: %s", y, buffer);
|
2016-04-06 15:44:04 +00:00
|
|
|
}
|
|
|
|
FREE_POINTER(buffer);
|
|
|
|
#endif //CHECKPOINTS
|
|
|
|
|
|
|
|
if (width == orig_stride && width == dst_stride) {
|
2022-04-28 11:18:09 +00:00
|
|
|
memcpy(dst, orig, width * height * sizeof(uvg_pixel));
|
2016-04-06 15:44:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nxn_width = (width == height) ? width : 0;
|
|
|
|
switch (nxn_width) {
|
|
|
|
BLIT_PIXELS_CASE(4)
|
|
|
|
BLIT_PIXELS_CASE(8)
|
|
|
|
BLIT_PIXELS_CASE(16)
|
|
|
|
BLIT_PIXELS_CASE(32)
|
|
|
|
BLIT_PIXELS_CASE(64)
|
|
|
|
default:
|
|
|
|
|
|
|
|
if (orig == dst) {
|
|
|
|
//If we have the same array, then we should have the same stride
|
|
|
|
assert(orig_stride == dst_stride);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(orig != dst || orig_stride == dst_stride);
|
|
|
|
|
|
|
|
for (y = 0; y < height; ++y) {
|
2022-04-28 11:18:09 +00:00
|
|
|
memcpy(&dst[y*dst_stride], &orig[y*orig_stride], width * sizeof(uvg_pixel));
|
2016-04-06 15:44:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-08-09 13:16:33 +00:00
|
|
|
}
|