2014-01-24 10:37:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Tampere University of Technology and others (see
|
2014-01-24 10:37:15 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as published
|
|
|
|
* by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* Kvazaar is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
2012-06-05 11:01:47 +00:00
|
|
|
*/
|
|
|
|
|
2014-05-05 05:54:39 +00:00
|
|
|
#include "threads.h"
|
2013-09-18 09:16:03 +00:00
|
|
|
#include "picture.h"
|
2014-04-29 08:14:42 +00:00
|
|
|
#include "strategyselector.h"
|
2012-06-05 11:01:47 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-09-20 08:07:31 +00:00
|
|
|
#include <math.h>
|
2014-04-24 06:30:09 +00:00
|
|
|
#include <assert.h>
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-11-04 17:27:47 +00:00
|
|
|
#include "sao.h"
|
|
|
|
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
#define PSNRMAX (255.0 * 255.0)
|
2013-06-12 12:41:57 +00:00
|
|
|
|
2014-03-18 09:26:42 +00:00
|
|
|
|
2014-05-02 09:45:27 +00:00
|
|
|
yuv_t * yuv_t_alloc(int luma_size)
|
2014-03-18 09:26:42 +00:00
|
|
|
{
|
2014-03-18 13:26:40 +00:00
|
|
|
// Get buffers with separate mallocs in order to take advantage of
|
|
|
|
// automatic buffer overrun checks.
|
|
|
|
yuv_t *yuv = (yuv_t *)malloc(sizeof(*yuv));
|
|
|
|
yuv->y = (pixel *)malloc(luma_size * sizeof(*yuv->y));
|
|
|
|
yuv->u = (pixel *)malloc(luma_size / 2 * sizeof(*yuv->u));
|
|
|
|
yuv->v = (pixel *)malloc(luma_size / 2 * sizeof(*yuv->v));
|
2014-03-18 09:26:42 +00:00
|
|
|
yuv->size = luma_size;
|
2014-03-18 13:26:40 +00:00
|
|
|
|
2014-03-18 09:26:42 +00:00
|
|
|
return yuv;
|
|
|
|
}
|
|
|
|
|
2014-05-02 09:45:27 +00:00
|
|
|
void yuv_t_free(yuv_t * yuv)
|
2014-03-18 09:26:42 +00:00
|
|
|
{
|
2014-03-18 13:26:40 +00:00
|
|
|
free(yuv->y);
|
|
|
|
free(yuv->u);
|
|
|
|
free(yuv->v);
|
2014-03-18 09:26:42 +00:00
|
|
|
free(yuv);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-25 14:14:20 +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.
|
|
|
|
*/
|
2013-10-31 22:52:06 +00:00
|
|
|
void picture_blit_pixels(const pixel *orig, pixel *dst,
|
2013-10-25 14:14:20 +00:00
|
|
|
unsigned width, unsigned height,
|
|
|
|
unsigned orig_stride, unsigned dst_stride)
|
|
|
|
{
|
|
|
|
unsigned y, x;
|
2014-05-09 04:56:31 +00:00
|
|
|
//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);
|
2013-10-25 14:14:20 +00:00
|
|
|
|
2014-02-27 09:56:16 +00:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
dst[x] = orig[x];
|
|
|
|
}
|
|
|
|
// Move pointers to the next row.
|
|
|
|
orig += orig_stride;
|
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void picture_blit_coeffs(const coefficient *orig, coefficient *dst,
|
|
|
|
unsigned width, unsigned height,
|
|
|
|
unsigned orig_stride, unsigned dst_stride)
|
|
|
|
{
|
|
|
|
unsigned y, x;
|
|
|
|
|
2013-10-25 14:14:20 +00:00
|
|
|
for (y = 0; y < height; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
dst[x] = orig[x];
|
|
|
|
}
|
|
|
|
// Move pointers to the next row.
|
|
|
|
orig += orig_stride;
|
|
|
|
dst += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 14:08:52 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocate memory for picture_list
|
|
|
|
* \param size initial array size
|
|
|
|
* \return picture_list pointer, NULL on failure
|
|
|
|
*/
|
|
|
|
picture_list * picture_list_init(int size)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
|
|
|
picture_list *list = (picture_list *)malloc(sizeof(picture_list));
|
|
|
|
list->size = size;
|
2013-09-20 08:07:31 +00:00
|
|
|
if (size > 0) {
|
|
|
|
list->pics = (picture**)malloc(sizeof(picture*) * size);
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
list->used_size = 0;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
return list;
|
|
|
|
}
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Resize picture_list array
|
|
|
|
* \param list picture_list pointer
|
|
|
|
* \param size new array size
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2014-02-10 14:48:50 +00:00
|
|
|
int picture_list_resize(picture_list *list, unsigned size)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
picture** old_pics = NULL;
|
2012-06-05 11:01:47 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// No need to do anything when resizing to same size
|
|
|
|
if (size == list->size) {
|
2012-06-05 11:01:47 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// Save the old list
|
|
|
|
if (list->used_size > 0) {
|
2013-09-20 07:11:11 +00:00
|
|
|
old_pics = list->pics;
|
2012-06-05 11:01:47 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// allocate space for the new list
|
2013-09-20 07:11:11 +00:00
|
|
|
list->pics = (picture**)malloc(sizeof(picture*)*size);
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// Copy everything from the old list to the new if needed.
|
|
|
|
if (old_pics != NULL) {
|
|
|
|
for (i = 0; i < list->used_size; ++i) {
|
2013-09-20 07:11:11 +00:00
|
|
|
list->pics[i] = old_pics[i];
|
2013-09-12 14:15:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
free(old_pics);
|
2013-09-12 14:15:14 +00:00
|
|
|
}
|
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Free memory allocated to the picture_list
|
|
|
|
* \param list picture_list pointer
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2013-09-20 07:11:11 +00:00
|
|
|
int picture_list_destroy(picture_list *list)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2013-09-20 08:07:31 +00:00
|
|
|
if (list->used_size > 0) {
|
|
|
|
for (i = 0; i < list->used_size; ++i) {
|
2014-05-02 08:58:28 +00:00
|
|
|
picture_free(list->pics[i]);
|
|
|
|
list->pics[i] = NULL;
|
2013-09-12 14:15:14 +00:00
|
|
|
}
|
2013-09-20 07:11:11 +00:00
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
if (list->size > 0) {
|
2013-09-20 07:11:11 +00:00
|
|
|
free(list->pics);
|
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-12 14:15:14 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2014-02-07 22:32:10 +00:00
|
|
|
* \brief Add picture to the front of the picturelist
|
2013-09-20 08:07:31 +00:00
|
|
|
* \param pic picture pointer to add
|
|
|
|
* \param picture_list list to use
|
|
|
|
* \return 1 on success
|
|
|
|
*/
|
2013-09-20 07:11:11 +00:00
|
|
|
int picture_list_add(picture_list *list,picture* pic)
|
|
|
|
{
|
2014-05-05 08:18:43 +00:00
|
|
|
int i = 0;
|
2014-05-05 05:54:39 +00:00
|
|
|
if (ATOMIC_INC(&(pic->refcount)) == 1) {
|
|
|
|
fprintf(stderr, "Tried to add an unreferenced picture. This is a bug!\n");
|
|
|
|
assert(0); //Stop for debugging
|
|
|
|
return 0;
|
|
|
|
}
|
2014-05-05 08:18:43 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
if (list->size == list->used_size) {
|
|
|
|
if (!picture_list_resize(list, list->size*2)) return 0;
|
2013-09-20 07:11:11 +00:00
|
|
|
}
|
2013-09-12 14:26:27 +00:00
|
|
|
|
2014-02-07 22:32:10 +00:00
|
|
|
for (i = list->used_size; i > 0; i--) {
|
|
|
|
list->pics[i] = list->pics[i - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
list->pics[0] = pic;
|
2013-09-20 07:11:11 +00:00
|
|
|
list->used_size++;
|
|
|
|
return 1;
|
|
|
|
}
|
2013-09-12 14:15:14 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Add picture to picturelist
|
|
|
|
* \param pic picture pointer to add
|
|
|
|
* \param picture_list list to use
|
|
|
|
* \return 1 on success
|
|
|
|
*/
|
2014-05-05 05:54:39 +00:00
|
|
|
int picture_list_rem(picture_list * const list, const unsigned n)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
2013-09-20 08:07:31 +00:00
|
|
|
// Must be within list boundaries
|
2014-02-10 14:48:50 +00:00
|
|
|
if (n >= list->used_size)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
|
|
|
return 0;
|
2013-09-12 14:15:14 +00:00
|
|
|
}
|
2013-09-20 07:11:11 +00:00
|
|
|
|
2014-05-05 05:54:39 +00:00
|
|
|
if (!picture_free(list->pics[n])) {
|
|
|
|
fprintf(stderr, "Could not free picture!\n");
|
|
|
|
assert(0); //Stop here
|
|
|
|
return 0;
|
2013-09-20 07:11:11 +00:00
|
|
|
}
|
2014-05-05 08:18:43 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// The last item is easy to remove
|
|
|
|
if (n == list->used_size - 1) {
|
2013-09-20 07:11:11 +00:00
|
|
|
list->pics[n] = NULL;
|
|
|
|
list->used_size--;
|
2013-09-20 08:07:31 +00:00
|
|
|
} else {
|
2014-05-05 05:54:39 +00:00
|
|
|
int i = n;
|
2013-09-20 08:07:31 +00:00
|
|
|
// Shift all following pics one backward in the list
|
2014-05-05 05:54:39 +00:00
|
|
|
for (i = n; i < list->used_size - 1; ++i) {
|
|
|
|
list->pics[i] = list->pics[i + 1];
|
2013-09-12 13:28:40 +00:00
|
|
|
}
|
2013-09-20 08:07:31 +00:00
|
|
|
list->pics[list->used_size - 1] = NULL;
|
2013-09-20 07:11:11 +00:00
|
|
|
list->used_size--;
|
|
|
|
}
|
2013-09-12 13:28:40 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Allocate new picture
|
|
|
|
* \param pic picture pointer
|
|
|
|
* \return picture pointer
|
|
|
|
*/
|
2014-05-02 08:58:28 +00:00
|
|
|
picture *picture_alloc(const int32_t width, const int32_t height,
|
|
|
|
const int32_t width_in_lcu, const int32_t height_in_lcu)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
2014-02-21 13:00:20 +00:00
|
|
|
picture *pic = (picture *)malloc(sizeof(picture));
|
2013-09-20 07:11:11 +00:00
|
|
|
unsigned int luma_size = width * height;
|
|
|
|
unsigned int chroma_size = luma_size / 4;
|
2013-09-12 13:28:40 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
if (!pic) return 0;
|
2013-09-12 13:28:40 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
memset(pic, 0, sizeof(picture));
|
|
|
|
|
|
|
|
pic->width = width;
|
|
|
|
pic->height = height;
|
2013-09-20 08:07:31 +00:00
|
|
|
pic->width_in_lcu = width_in_lcu;
|
|
|
|
pic->height_in_lcu = height_in_lcu;
|
2013-09-20 07:11:11 +00:00
|
|
|
pic->referenced = 0;
|
2014-05-05 05:54:39 +00:00
|
|
|
pic->refcount = 1; //We give a reference to caller
|
2013-09-20 08:07:31 +00:00
|
|
|
// Allocate buffers
|
2013-10-15 12:27:32 +00:00
|
|
|
pic->y_data = MALLOC(pixel, luma_size);
|
|
|
|
pic->u_data = MALLOC(pixel, chroma_size);
|
|
|
|
pic->v_data = MALLOC(pixel, chroma_size);
|
2013-11-13 12:42:41 +00:00
|
|
|
pic->data[COLOR_Y] = pic->y_data;
|
|
|
|
pic->data[COLOR_U] = pic->u_data;
|
|
|
|
pic->data[COLOR_V] = pic->v_data;
|
2013-09-20 07:11:11 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// Reconstruction buffers
|
2013-10-15 12:27:32 +00:00
|
|
|
pic->y_recdata = MALLOC(pixel, luma_size);
|
|
|
|
pic->u_recdata = MALLOC(pixel, chroma_size);
|
|
|
|
pic->v_recdata = MALLOC(pixel, chroma_size);
|
2013-11-13 12:42:41 +00:00
|
|
|
pic->recdata[COLOR_Y] = pic->y_recdata;
|
|
|
|
pic->recdata[COLOR_U] = pic->u_recdata;
|
|
|
|
pic->recdata[COLOR_V] = pic->v_recdata;
|
2013-09-20 07:11:11 +00:00
|
|
|
|
|
|
|
memset(pic->u_recdata, 128, (chroma_size));
|
|
|
|
memset(pic->v_recdata, 128, (chroma_size));
|
|
|
|
|
2014-04-28 07:18:22 +00:00
|
|
|
{
|
2013-09-20 08:07:31 +00:00
|
|
|
// Allocate height_in_scu x width_in_scu x sizeof(CU_info)
|
|
|
|
unsigned height_in_scu = height_in_lcu << MAX_DEPTH;
|
|
|
|
unsigned width_in_scu = width_in_lcu << MAX_DEPTH;
|
|
|
|
unsigned cu_array_size = height_in_scu * width_in_scu;
|
2014-04-28 07:18:22 +00:00
|
|
|
pic->cu_array = (cu_info*)malloc(sizeof(cu_info) * cu_array_size);
|
|
|
|
memset(pic->cu_array, 0, sizeof(cu_info) * cu_array_size);
|
2013-09-20 07:11:11 +00:00
|
|
|
}
|
2014-05-05 08:18:43 +00:00
|
|
|
|
2013-10-18 08:39:13 +00:00
|
|
|
pic->coeff_y = NULL; pic->coeff_u = NULL; pic->coeff_v = NULL;
|
2013-10-15 14:56:50 +00:00
|
|
|
|
2013-11-04 17:27:47 +00:00
|
|
|
pic->sao_luma = MALLOC(sao_info, width_in_lcu * height_in_lcu);
|
|
|
|
pic->sao_chroma = MALLOC(sao_info, width_in_lcu * height_in_lcu);
|
2013-10-23 16:51:39 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
return pic;
|
|
|
|
}
|
2013-09-12 14:26:27 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2014-05-05 05:54:39 +00:00
|
|
|
* \brief Free memory allocated to picture (if we have no reference left)
|
2013-09-20 08:07:31 +00:00
|
|
|
* \param pic picture pointer
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2014-05-02 08:58:28 +00:00
|
|
|
int picture_free(picture * const pic)
|
2013-09-20 07:11:11 +00:00
|
|
|
{
|
2014-05-05 05:54:39 +00:00
|
|
|
int32_t new_refcount = ATOMIC_DEC(&(pic->refcount));
|
|
|
|
if (new_refcount > 0) return 1;
|
2013-09-20 07:11:11 +00:00
|
|
|
free(pic->u_data);
|
|
|
|
free(pic->v_data);
|
|
|
|
free(pic->y_data);
|
|
|
|
pic->y_data = pic->u_data = pic->v_data = NULL;
|
2013-11-13 12:42:41 +00:00
|
|
|
pic->data[COLOR_Y] = pic->data[COLOR_U] = pic->data[COLOR_V] = NULL;
|
2013-09-12 14:26:27 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
free(pic->y_recdata);
|
|
|
|
free(pic->u_recdata);
|
|
|
|
free(pic->v_recdata);
|
|
|
|
pic->y_recdata = pic->u_recdata = pic->v_recdata = NULL;
|
2013-11-13 12:42:41 +00:00
|
|
|
pic->recdata[COLOR_Y] = pic->recdata[COLOR_U] = pic->recdata[COLOR_V] = NULL;
|
2013-09-12 14:26:27 +00:00
|
|
|
|
2014-04-28 07:18:22 +00:00
|
|
|
FREE_POINTER(pic->cu_array);
|
2013-09-20 07:11:11 +00:00
|
|
|
|
2013-10-18 08:39:13 +00:00
|
|
|
FREE_POINTER(pic->coeff_y);
|
|
|
|
FREE_POINTER(pic->coeff_u);
|
|
|
|
FREE_POINTER(pic->coeff_v);
|
|
|
|
|
2013-11-04 17:27:47 +00:00
|
|
|
FREE_POINTER(pic->sao_luma);
|
|
|
|
FREE_POINTER(pic->sao_chroma);
|
2014-05-05 08:18:43 +00:00
|
|
|
|
2014-05-02 08:58:28 +00:00
|
|
|
free(pic);
|
2013-11-04 17:27:47 +00:00
|
|
|
|
2013-09-20 07:11:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculates image PSNR value
|
|
|
|
*/
|
2013-10-14 14:27:25 +00:00
|
|
|
double image_psnr(pixel *frame1, pixel *frame2, int32_t x, int32_t y)
|
2013-09-20 08:07:31 +00:00
|
|
|
{
|
|
|
|
uint64_t error_sum = 0;
|
|
|
|
int32_t error = 0;
|
|
|
|
int32_t pixels = x * y;
|
|
|
|
int32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < pixels; ++i) {
|
|
|
|
error = frame1[i] - frame2[i];
|
|
|
|
error_sum += error * error;
|
2013-04-03 08:05:07 +00:00
|
|
|
}
|
2013-03-12 15:06:21 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// Avoid division by zero
|
|
|
|
if (error_sum == 0) return 99.0;
|
2013-03-12 15:06:21 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
return 10 * log10((pixels * PSNRMAX) / ((double)error_sum));
|
2013-03-12 15:06:21 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 08:12:42 +00:00
|
|
|
/**
|
|
|
|
* \brief Calculate SATD between two 4x4 blocks inside bigger arrays.
|
|
|
|
* From HM 13.0
|
|
|
|
*/
|
|
|
|
static unsigned satd_16bit_4x4(pixel *piOrg,pixel *piCur)
|
|
|
|
{
|
|
|
|
int32_t k, satd = 0, diff[16], m[16], d[16];
|
|
|
|
int32_t iStrideOrg = 4, iStrideCur = 4;
|
|
|
|
for( k = 0; k < 16; k+=4 ) {
|
|
|
|
diff[k+0] = piOrg[0] - piCur[0];
|
|
|
|
diff[k+1] = piOrg[1] - piCur[1];
|
|
|
|
diff[k+2] = piOrg[2] - piCur[2];
|
|
|
|
diff[k+3] = piOrg[3] - piCur[3];
|
|
|
|
|
|
|
|
piCur += iStrideCur;
|
|
|
|
piOrg += iStrideOrg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===== hadamard transform =====*/
|
|
|
|
m[ 0] = diff[ 0] + diff[12];
|
|
|
|
m[ 1] = diff[ 1] + diff[13];
|
|
|
|
m[ 2] = diff[ 2] + diff[14];
|
|
|
|
m[ 3] = diff[ 3] + diff[15];
|
|
|
|
m[ 4] = diff[ 4] + diff[ 8];
|
|
|
|
m[ 5] = diff[ 5] + diff[ 9];
|
|
|
|
m[ 6] = diff[ 6] + diff[10];
|
|
|
|
m[ 7] = diff[ 7] + diff[11];
|
|
|
|
m[ 8] = diff[ 4] - diff[ 8];
|
|
|
|
m[ 9] = diff[ 5] - diff[ 9];
|
|
|
|
m[10] = diff[ 6] - diff[10];
|
|
|
|
m[11] = diff[ 7] - diff[11];
|
|
|
|
m[12] = diff[ 0] - diff[12];
|
|
|
|
m[13] = diff[ 1] - diff[13];
|
|
|
|
m[14] = diff[ 2] - diff[14];
|
|
|
|
m[15] = diff[ 3] - diff[15];
|
|
|
|
|
|
|
|
d[ 0] = m[ 0] + m[ 4];
|
|
|
|
d[ 1] = m[ 1] + m[ 5];
|
|
|
|
d[ 2] = m[ 2] + m[ 6];
|
|
|
|
d[ 3] = m[ 3] + m[ 7];
|
|
|
|
d[ 4] = m[ 8] + m[12];
|
|
|
|
d[ 5] = m[ 9] + m[13];
|
|
|
|
d[ 6] = m[10] + m[14];
|
|
|
|
d[ 7] = m[11] + m[15];
|
|
|
|
d[ 8] = m[ 0] - m[ 4];
|
|
|
|
d[ 9] = m[ 1] - m[ 5];
|
|
|
|
d[10] = m[ 2] - m[ 6];
|
|
|
|
d[11] = m[ 3] - m[ 7];
|
|
|
|
d[12] = m[12] - m[ 8];
|
|
|
|
d[13] = m[13] - m[ 9];
|
|
|
|
d[14] = m[14] - m[10];
|
|
|
|
d[15] = m[15] - m[11];
|
|
|
|
|
|
|
|
m[ 0] = d[ 0] + d[ 3];
|
|
|
|
m[ 1] = d[ 1] + d[ 2];
|
|
|
|
m[ 2] = d[ 1] - d[ 2];
|
|
|
|
m[ 3] = d[ 0] - d[ 3];
|
|
|
|
m[ 4] = d[ 4] + d[ 7];
|
|
|
|
m[ 5] = d[ 5] + d[ 6];
|
|
|
|
m[ 6] = d[ 5] - d[ 6];
|
|
|
|
m[ 7] = d[ 4] - d[ 7];
|
|
|
|
m[ 8] = d[ 8] + d[11];
|
|
|
|
m[ 9] = d[ 9] + d[10];
|
|
|
|
m[10] = d[ 9] - d[10];
|
|
|
|
m[11] = d[ 8] - d[11];
|
|
|
|
m[12] = d[12] + d[15];
|
|
|
|
m[13] = d[13] + d[14];
|
|
|
|
m[14] = d[13] - d[14];
|
|
|
|
m[15] = d[12] - d[15];
|
|
|
|
|
|
|
|
d[ 0] = m[ 0] + m[ 1];
|
|
|
|
d[ 1] = m[ 0] - m[ 1];
|
|
|
|
d[ 2] = m[ 2] + m[ 3];
|
|
|
|
d[ 3] = m[ 3] - m[ 2];
|
|
|
|
d[ 4] = m[ 4] + m[ 5];
|
|
|
|
d[ 5] = m[ 4] - m[ 5];
|
|
|
|
d[ 6] = m[ 6] + m[ 7];
|
|
|
|
d[ 7] = m[ 7] - m[ 6];
|
|
|
|
d[ 8] = m[ 8] + m[ 9];
|
|
|
|
d[ 9] = m[ 8] - m[ 9];
|
|
|
|
d[10] = m[10] + m[11];
|
|
|
|
d[11] = m[11] - m[10];
|
|
|
|
d[12] = m[12] + m[13];
|
|
|
|
d[13] = m[12] - m[13];
|
|
|
|
d[14] = m[14] + m[15];
|
|
|
|
d[15] = m[15] - m[14];
|
|
|
|
|
|
|
|
for (k=0; k<16; ++k) {
|
|
|
|
satd += abs(d[k]);
|
|
|
|
}
|
|
|
|
satd = ((satd+1)>>1);
|
|
|
|
|
|
|
|
return satd;
|
|
|
|
}
|
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2013-10-16 12:08:53 +00:00
|
|
|
* \brief Calculate SATD between two 8x8 blocks inside bigger arrays.
|
2013-09-20 08:07:31 +00:00
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned satd_16bit_8x8_general(pixel *piOrg, int32_t iStrideOrg,
|
|
|
|
pixel *piCur, int32_t iStrideCur)
|
2013-04-04 12:08:28 +00:00
|
|
|
{
|
|
|
|
int32_t k, i, j, jj, sad=0;
|
|
|
|
int32_t diff[64], m1[8][8], m2[8][8], m3[8][8];
|
2013-10-16 12:08:53 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
for (k = 0; k < 64; k += 8) {
|
2013-04-04 12:08:28 +00:00
|
|
|
diff[k+0] = piOrg[0] - piCur[0];
|
|
|
|
diff[k+1] = piOrg[1] - piCur[1];
|
|
|
|
diff[k+2] = piOrg[2] - piCur[2];
|
|
|
|
diff[k+3] = piOrg[3] - piCur[3];
|
|
|
|
diff[k+4] = piOrg[4] - piCur[4];
|
|
|
|
diff[k+5] = piOrg[5] - piCur[5];
|
|
|
|
diff[k+6] = piOrg[6] - piCur[6];
|
|
|
|
diff[k+7] = piOrg[7] - piCur[7];
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
piCur += iStrideCur;
|
|
|
|
piOrg += iStrideOrg;
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// horizontal
|
|
|
|
for (j = 0; j < 8; ++j) {
|
2013-04-04 12:08:28 +00:00
|
|
|
jj = j << 3;
|
|
|
|
m2[j][0] = diff[jj ] + diff[jj+4];
|
|
|
|
m2[j][1] = diff[jj+1] + diff[jj+5];
|
|
|
|
m2[j][2] = diff[jj+2] + diff[jj+6];
|
|
|
|
m2[j][3] = diff[jj+3] + diff[jj+7];
|
|
|
|
m2[j][4] = diff[jj ] - diff[jj+4];
|
|
|
|
m2[j][5] = diff[jj+1] - diff[jj+5];
|
|
|
|
m2[j][6] = diff[jj+2] - diff[jj+6];
|
|
|
|
m2[j][7] = diff[jj+3] - diff[jj+7];
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
m1[j][0] = m2[j][0] + m2[j][2];
|
|
|
|
m1[j][1] = m2[j][1] + m2[j][3];
|
|
|
|
m1[j][2] = m2[j][0] - m2[j][2];
|
|
|
|
m1[j][3] = m2[j][1] - m2[j][3];
|
|
|
|
m1[j][4] = m2[j][4] + m2[j][6];
|
|
|
|
m1[j][5] = m2[j][5] + m2[j][7];
|
|
|
|
m1[j][6] = m2[j][4] - m2[j][6];
|
|
|
|
m1[j][7] = m2[j][5] - m2[j][7];
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
m2[j][0] = m1[j][0] + m1[j][1];
|
|
|
|
m2[j][1] = m1[j][0] - m1[j][1];
|
|
|
|
m2[j][2] = m1[j][2] + m1[j][3];
|
|
|
|
m2[j][3] = m1[j][2] - m1[j][3];
|
|
|
|
m2[j][4] = m1[j][4] + m1[j][5];
|
|
|
|
m2[j][5] = m1[j][4] - m1[j][5];
|
|
|
|
m2[j][6] = m1[j][6] + m1[j][7];
|
|
|
|
m2[j][7] = m1[j][6] - m1[j][7];
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
// vertical
|
|
|
|
for (i = 0; i < 8; ++i) {
|
2013-04-04 12:08:28 +00:00
|
|
|
m3[0][i] = m2[0][i] + m2[4][i];
|
|
|
|
m3[1][i] = m2[1][i] + m2[5][i];
|
|
|
|
m3[2][i] = m2[2][i] + m2[6][i];
|
|
|
|
m3[3][i] = m2[3][i] + m2[7][i];
|
|
|
|
m3[4][i] = m2[0][i] - m2[4][i];
|
|
|
|
m3[5][i] = m2[1][i] - m2[5][i];
|
|
|
|
m3[6][i] = m2[2][i] - m2[6][i];
|
|
|
|
m3[7][i] = m2[3][i] - m2[7][i];
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
m1[0][i] = m3[0][i] + m3[2][i];
|
|
|
|
m1[1][i] = m3[1][i] + m3[3][i];
|
|
|
|
m1[2][i] = m3[0][i] - m3[2][i];
|
|
|
|
m1[3][i] = m3[1][i] - m3[3][i];
|
|
|
|
m1[4][i] = m3[4][i] + m3[6][i];
|
|
|
|
m1[5][i] = m3[5][i] + m3[7][i];
|
|
|
|
m1[6][i] = m3[4][i] - m3[6][i];
|
|
|
|
m1[7][i] = m3[5][i] - m3[7][i];
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
m2[0][i] = m1[0][i] + m1[1][i];
|
|
|
|
m2[1][i] = m1[0][i] - m1[1][i];
|
|
|
|
m2[2][i] = m1[2][i] + m1[3][i];
|
|
|
|
m2[3][i] = m1[2][i] - m1[3][i];
|
|
|
|
m2[4][i] = m1[4][i] + m1[5][i];
|
|
|
|
m2[5][i] = m1[4][i] - m1[5][i];
|
|
|
|
m2[6][i] = m1[6][i] + m1[7][i];
|
|
|
|
m2[7][i] = m1[6][i] - m1[7][i];
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
for (j = 0; j < 8; ++j) {
|
2013-04-04 12:08:28 +00:00
|
|
|
sad += abs(m2[i][j]);
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
sad = (sad + 2) >> 2;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-04-04 12:08:28 +00:00
|
|
|
return sad;
|
|
|
|
}
|
|
|
|
|
2014-04-02 08:12:42 +00:00
|
|
|
// Function macro for defining hadamard calculating functions
|
|
|
|
// for fixed size blocks. They calculate hadamard for integer
|
|
|
|
// multiples of 8x8 with the 8x8 hadamard function.
|
2013-10-16 12:08:53 +00:00
|
|
|
#define SATD_NXN(n, pixel_type, suffix) \
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned satd_ ## suffix ## _ ## n ## x ## n( \
|
|
|
|
pixel_type *block1, pixel_type *block2) \
|
2013-10-16 12:08:53 +00:00
|
|
|
{ \
|
|
|
|
unsigned y, x; \
|
|
|
|
unsigned sum = 0; \
|
|
|
|
for (y = 0; y < (n); y += 8) { \
|
|
|
|
unsigned row = y * (n); \
|
|
|
|
for (x = 0; x < (n); x += 8) { \
|
|
|
|
sum += satd_16bit_8x8_general(&block1[row + x], (n), &block2[row + x], (n)); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return sum; \
|
2013-10-18 08:39:13 +00:00
|
|
|
}
|
2013-10-16 12:08:53 +00:00
|
|
|
|
|
|
|
// These macros define sadt_16bit_NxN for N = 8, 16, 32, 64
|
2013-10-18 11:23:21 +00:00
|
|
|
SATD_NXN(8, pixel, 16bit)
|
|
|
|
SATD_NXN(16, pixel, 16bit)
|
|
|
|
SATD_NXN(32, pixel, 16bit)
|
|
|
|
SATD_NXN(64, pixel, 16bit)
|
2013-10-16 12:08:53 +00:00
|
|
|
|
2014-02-21 13:00:20 +00:00
|
|
|
// Function macro for defining SAD calculating functions
|
2013-10-16 12:08:53 +00:00
|
|
|
// for fixed size blocks.
|
|
|
|
#define SAD_NXN(n, pixel_type, suffix) \
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned sad_ ## suffix ## _ ## n ## x ## n( \
|
|
|
|
pixel_type *block1, pixel_type *block2) \
|
2013-10-16 12:08:53 +00:00
|
|
|
{ \
|
|
|
|
unsigned x, y, row; \
|
|
|
|
unsigned sum = 0; \
|
|
|
|
for(y = 0; y < (n); y++) { \
|
|
|
|
row = y * (n); \
|
|
|
|
for (x = 0; x < (n); ++x) { \
|
|
|
|
sum += abs(block1[row + x] - block2[row + x]); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
return sum; \
|
2013-04-03 08:05:07 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 12:08:53 +00:00
|
|
|
// These macros define sad_16bit_nxn functions for n = 4, 8, 16, 32, 64
|
|
|
|
// with function signatures of cost_16bit_nxn_func.
|
|
|
|
// They are used through get_sad_16bit_nxn_func.
|
2013-10-18 11:23:21 +00:00
|
|
|
SAD_NXN(4, pixel, 16bit)
|
|
|
|
SAD_NXN(8, pixel, 16bit)
|
|
|
|
SAD_NXN(16, pixel, 16bit)
|
|
|
|
SAD_NXN(32, pixel, 16bit)
|
|
|
|
SAD_NXN(64, pixel, 16bit)
|
2013-03-25 15:17:24 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2013-10-16 12:08:53 +00:00
|
|
|
* \brief Get a function that calculates SATD for NxN block.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \param n Width of the region for which SATD is calculated.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \returns Pointer to cost_16bit_nxn_func.
|
2013-09-20 08:07:31 +00:00
|
|
|
*/
|
2013-10-16 12:08:53 +00:00
|
|
|
cost_16bit_nxn_func get_satd_16bit_nxn_func(unsigned n)
|
2013-03-12 15:06:21 +00:00
|
|
|
{
|
2013-10-16 12:08:53 +00:00
|
|
|
switch (n) {
|
2014-04-02 08:12:42 +00:00
|
|
|
case 4:
|
|
|
|
return &satd_16bit_4x4;
|
2013-10-16 12:08:53 +00:00
|
|
|
case 8:
|
|
|
|
return &satd_16bit_8x8;
|
|
|
|
case 16:
|
|
|
|
return &satd_16bit_16x16;
|
|
|
|
case 32:
|
|
|
|
return &satd_16bit_32x32;
|
|
|
|
case 64:
|
|
|
|
return &satd_16bit_64x64;
|
|
|
|
default:
|
|
|
|
return NULL;
|
2013-10-18 08:39:13 +00:00
|
|
|
}
|
2013-04-03 08:05:07 +00:00
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2013-10-16 12:08:53 +00:00
|
|
|
* \brief Get a function that calculates SAD for NxN block.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \param n Width of the region for which SAD is calculated.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \returns Pointer to cost_16bit_nxn_func.
|
2013-09-20 08:07:31 +00:00
|
|
|
*/
|
2013-10-16 12:08:53 +00:00
|
|
|
cost_16bit_nxn_func get_sad_16bit_nxn_func(unsigned n)
|
2013-10-18 08:39:13 +00:00
|
|
|
{
|
2013-10-16 12:08:53 +00:00
|
|
|
switch (n) {
|
|
|
|
case 4:
|
|
|
|
return &sad_16bit_4x4;
|
|
|
|
case 8:
|
|
|
|
return &sad_16bit_8x8;
|
|
|
|
case 16:
|
|
|
|
return &sad_16bit_16x16;
|
|
|
|
case 32:
|
|
|
|
return &sad_16bit_32x32;
|
|
|
|
case 64:
|
|
|
|
return &sad_16bit_64x64;
|
|
|
|
default:
|
|
|
|
return NULL;
|
2014-02-21 13:00:20 +00:00
|
|
|
}
|
2013-03-14 15:55:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 13:57:07 +00:00
|
|
|
#if 0
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2013-10-16 12:08:53 +00:00
|
|
|
* \brief Calculate SATD for NxN block of size N.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \param block1 Start of the first block.
|
|
|
|
* \param block2 Start of the second block.
|
|
|
|
* \param n Width of the region for which SAD is calculated.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \returns Sum of Absolute Transformed Differences (SATD)
|
2013-09-20 08:07:31 +00:00
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned satd_nxn_16bit(pixel *block1, pixel *block2, unsigned n)
|
2013-03-14 15:55:06 +00:00
|
|
|
{
|
2013-10-16 12:08:53 +00:00
|
|
|
cost_16bit_nxn_func sad_func = get_satd_16bit_nxn_func(n);
|
|
|
|
return sad_func(block1, block2);
|
2013-10-18 08:39:13 +00:00
|
|
|
}
|
2013-03-14 15:55:06 +00:00
|
|
|
|
2013-09-20 08:07:31 +00:00
|
|
|
/**
|
2013-10-16 12:08:53 +00:00
|
|
|
* \brief Calculate SAD for NxN block of size N.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \param block1 Start of the first block.
|
|
|
|
* \param block2 Start of the second block.
|
|
|
|
* \param n Width of the region for which SAD is calculated.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-16 12:08:53 +00:00
|
|
|
* \returns Sum of Absolute Differences
|
2013-09-20 08:07:31 +00:00
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned sad_nxn_16bit(pixel *block1, pixel *block2, unsigned n)
|
2013-03-14 15:55:06 +00:00
|
|
|
{
|
2013-10-16 12:08:53 +00:00
|
|
|
cost_16bit_nxn_func sad_func = get_sad_16bit_nxn_func(n);
|
|
|
|
if (sad_func) {
|
|
|
|
return sad_func(block1, block2);
|
|
|
|
} else {
|
|
|
|
unsigned row, x;
|
|
|
|
unsigned sum = 0;
|
|
|
|
for (row = 0; row < n; row += n) {
|
|
|
|
for (x = 0; x < n; ++x) {
|
|
|
|
sum += abs(block1[row + x] - block2[row + x]);
|
2013-04-03 08:05:07 +00:00
|
|
|
}
|
2013-10-18 08:39:13 +00:00
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
2013-09-16 14:34:20 +00:00
|
|
|
}
|
2014-02-21 13:57:07 +00:00
|
|
|
#endif
|
2013-09-16 14:34:20 +00:00
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Diagonally interpolate SAD outside the frame.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \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.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \returns Sum of Absolute Differences
|
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned cor_sad(const pixel *pic_data, const pixel *ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
int block_width, int block_height, unsigned pic_width)
|
2013-10-10 15:51:10 +00:00
|
|
|
{
|
2013-10-14 14:27:25 +00:00
|
|
|
pixel ref = *ref_data;
|
2013-10-11 09:51:53 +00:00
|
|
|
int x, y;
|
2013-10-10 15:51:10 +00:00
|
|
|
unsigned sad = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < block_height; ++y) {
|
|
|
|
for (x = 0; x < block_width; ++x) {
|
2014-04-24 06:30:09 +00:00
|
|
|
sad += abs(pic_data[y * pic_width + x] - ref);
|
2013-10-10 15:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sad;
|
|
|
|
}
|
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Vertically interpolate SAD outside the frame.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \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.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \returns Sum of Absolute Differences
|
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned ver_sad(const pixel *pic_data, const pixel *ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
int block_width, int block_height, unsigned pic_width)
|
2013-10-10 15:51:10 +00:00
|
|
|
{
|
2013-10-11 09:51:53 +00:00
|
|
|
int x, y;
|
2013-10-10 15:51:10 +00:00
|
|
|
unsigned sad = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < block_height; ++y) {
|
|
|
|
for (x = 0; x < block_width; ++x) {
|
2014-04-24 06:30:09 +00:00
|
|
|
sad += abs(pic_data[y * pic_width + x] - ref_data[x]);
|
2013-10-10 15:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sad;
|
|
|
|
}
|
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Horizontally interpolate SAD outside the frame.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \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.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \returns Sum of Absolute Differences
|
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned hor_sad(const pixel *pic_data, const pixel *ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
int block_width, int block_height, unsigned pic_width, unsigned ref_width)
|
2013-10-10 15:51:10 +00:00
|
|
|
{
|
2013-10-11 09:51:53 +00:00
|
|
|
int x, y;
|
2013-10-10 15:51:10 +00:00
|
|
|
unsigned sad = 0;
|
|
|
|
|
|
|
|
for (y = 0; y < block_height; ++y) {
|
|
|
|
for (x = 0; x < block_width; ++x) {
|
2014-04-24 06:30:09 +00:00
|
|
|
sad += abs(pic_data[y * pic_width + x] - ref_data[y * ref_width]);
|
2013-10-10 15:51:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sad;
|
|
|
|
}
|
|
|
|
|
2014-04-28 12:20:49 +00:00
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
/**
|
|
|
|
* \brief Handle special cases of comparing blocks that are not completely
|
|
|
|
* inside the frame.
|
2014-02-21 13:00:20 +00:00
|
|
|
*
|
2013-10-11 12:31:55 +00:00
|
|
|
* \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.
|
|
|
|
*/
|
2014-02-21 13:11:57 +00:00
|
|
|
static unsigned interpolated_sad(const picture *pic, const picture *ref,
|
|
|
|
int pic_x, int pic_y, int ref_x, int ref_y,
|
|
|
|
int block_width, int block_height)
|
2013-10-11 12:31:55 +00:00
|
|
|
{
|
2013-10-14 14:27:25 +00:00
|
|
|
pixel *pic_data, *ref_data;
|
2013-10-11 12:31:55 +00:00
|
|
|
|
2014-02-07 11:11:21 +00:00
|
|
|
int left, right, top, bottom;
|
|
|
|
int result = 0;
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2014-02-07 11:11:21 +00:00
|
|
|
// Change the movement vector to point right next to the frame. This doesn't
|
|
|
|
// affect the result but removes some special cases.
|
2014-04-24 06:30:09 +00:00
|
|
|
if (ref_x > ref->width) ref_x = ref->width;
|
|
|
|
if (ref_y > ref->height) ref_y = ref->height;
|
2014-02-07 11:11:21 +00:00
|
|
|
if (ref_x + block_width < 0) ref_x = -block_width;
|
|
|
|
if (ref_y + block_height < 0) ref_y = -block_height;
|
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
// 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.
|
2014-02-07 11:11:21 +00:00
|
|
|
left = (ref_x < 0) ? -ref_x : 0;
|
|
|
|
top = (ref_y < 0) ? -ref_y : 0;
|
2014-04-24 06:30:09 +00:00
|
|
|
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;
|
2013-10-11 12:31:55 +00:00
|
|
|
|
|
|
|
// 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-04-24 06:30:09 +00:00
|
|
|
pic_data = &pic->y_data[pic_y * pic->width + pic_x];
|
|
|
|
ref_data = &ref->y_data[ref_y * ref->width + ref_x];
|
2013-10-11 12:31:55 +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.
|
|
|
|
if (top && left) {
|
|
|
|
result += cor_sad(pic_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
&ref_data[top * ref->width + left],
|
|
|
|
left, top, pic->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += ver_sad(&pic_data[left],
|
2014-04-24 06:30:09 +00:00
|
|
|
&ref_data[top * ref->width + left],
|
|
|
|
block_width - left, top, pic->width);
|
|
|
|
result += hor_sad(&pic_data[top * pic->width],
|
|
|
|
&ref_data[top * ref->width + left],
|
|
|
|
left, block_height - top, pic->width, ref->width);
|
|
|
|
result += reg_sad(&pic_data[top * pic->width + left],
|
|
|
|
&ref_data[top * ref->width + left],
|
|
|
|
block_width - left, block_height - top, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (top && right) {
|
|
|
|
result += ver_sad(pic_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
&ref_data[top * ref->width],
|
|
|
|
block_width - right, top, pic->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += cor_sad(&pic_data[block_width - right],
|
2014-04-24 06:30:09 +00:00
|
|
|
&ref_data[top * ref->width + (block_width - right - 1)],
|
|
|
|
right, top, pic->width);
|
|
|
|
result += reg_sad(&pic_data[top * pic->width],
|
|
|
|
&ref_data[top * ref->width],
|
|
|
|
block_width - right, block_height - top, pic->width, ref->width);
|
|
|
|
result += hor_sad(&pic_data[top * pic->width + (block_width - right)],
|
|
|
|
&ref_data[top * ref->width + (block_width - right - 1)],
|
|
|
|
right, block_height - top, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (bottom && left) {
|
|
|
|
result += hor_sad(pic_data,
|
|
|
|
&ref_data[left],
|
2014-04-24 06:30:09 +00:00
|
|
|
left, block_height - bottom, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += reg_sad(&pic_data[left],
|
|
|
|
&ref_data[left],
|
2014-04-24 06:30:09 +00:00
|
|
|
block_width - left, block_height - bottom, pic->width, ref->width);
|
|
|
|
result += cor_sad(&pic_data[(block_height - bottom) * pic->width],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->width + left],
|
|
|
|
left, bottom, pic->width);
|
|
|
|
result += ver_sad(&pic_data[(block_height - bottom) * pic->width + left],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->width + left],
|
|
|
|
block_width - left, bottom, pic->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (bottom && right) {
|
|
|
|
result += reg_sad(pic_data,
|
|
|
|
ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
block_width - right, block_height - bottom, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += hor_sad(&pic_data[block_width - right],
|
|
|
|
&ref_data[block_width - right - 1],
|
2014-04-24 06:30:09 +00:00
|
|
|
right, block_height - bottom, pic->width, ref->width);
|
|
|
|
result += ver_sad(&pic_data[(block_height - bottom) * pic->width],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->width],
|
|
|
|
block_width - right, bottom, pic->width);
|
|
|
|
result += cor_sad(&pic_data[(block_height - bottom) * pic->width + block_width - right],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->width + block_width - right - 1],
|
|
|
|
right, bottom, pic->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (top) {
|
|
|
|
result += ver_sad(pic_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
&ref_data[top * ref->width],
|
|
|
|
block_width, top, pic->width);
|
|
|
|
result += reg_sad(&pic_data[top * pic->width],
|
|
|
|
&ref_data[top * ref->width],
|
|
|
|
block_width, block_height - top, pic->width, ref->width);
|
2014-02-21 13:00:20 +00:00
|
|
|
} else if (bottom) {
|
2013-10-11 12:31:55 +00:00
|
|
|
result += reg_sad(pic_data,
|
|
|
|
ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
block_width, block_height - bottom, pic->width, ref->width);
|
|
|
|
result += ver_sad(&pic_data[(block_height - bottom) * pic->width],
|
|
|
|
&ref_data[(block_height - bottom - 1) * ref->width],
|
|
|
|
block_width, bottom, pic->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (left) {
|
|
|
|
result += hor_sad(pic_data,
|
|
|
|
&ref_data[left],
|
2014-04-24 06:30:09 +00:00
|
|
|
left, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += reg_sad(&pic_data[left],
|
|
|
|
&ref_data[left],
|
2014-04-24 06:30:09 +00:00
|
|
|
block_width - left, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else if (right) {
|
|
|
|
result += reg_sad(pic_data,
|
|
|
|
ref_data,
|
2014-04-24 06:30:09 +00:00
|
|
|
block_width - right, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
result += hor_sad(&pic_data[block_width - right],
|
|
|
|
&ref_data[block_width - right - 1],
|
2014-04-24 06:30:09 +00:00
|
|
|
right, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else {
|
2014-04-24 06:30:09 +00:00
|
|
|
result += reg_sad(pic_data, ref_data, block_width, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
}
|
2014-02-21 13:00:20 +00:00
|
|
|
|
2013-10-11 12:31:55 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Get Sum of Absolute Differences (SAD) between two blocks in two
|
|
|
|
* different frames.
|
|
|
|
*
|
|
|
|
* \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.
|
|
|
|
*/
|
2014-02-21 13:00:20 +00:00
|
|
|
unsigned calc_sad(const picture *pic, const picture *ref,
|
|
|
|
int pic_x, int pic_y, int ref_x, int ref_y,
|
2013-10-11 12:31:55 +00:00
|
|
|
int block_width, int block_height)
|
|
|
|
{
|
2014-04-24 06:30:09 +00:00
|
|
|
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)
|
2013-10-11 12:31:55 +00:00
|
|
|
{
|
|
|
|
// 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.
|
2013-10-18 14:51:16 +00:00
|
|
|
const pixel *pic_data = &pic->y_data[pic_y * pic->width + pic_x];
|
2014-04-24 06:30:09 +00:00
|
|
|
const pixel *ref_data = &ref->y_data[ref_y * ref->width + ref_x];
|
|
|
|
return reg_sad(pic_data, ref_data, block_width, block_height, pic->width, ref->width);
|
2013-10-11 12:31:55 +00:00
|
|
|
} else {
|
|
|
|
// Call a routine that knows how to interpolate pixels outside the frame.
|
|
|
|
return interpolated_sad(pic, ref, pic_x, pic_y, ref_x, ref_y, block_width, block_height);
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 16:45:50 +00:00
|
|
|
|
|
|
|
unsigned calc_ssd(const pixel *const ref, const pixel *const rec,
|
|
|
|
const int ref_stride, const int rec_stride,
|
|
|
|
const int width)
|
|
|
|
{
|
|
|
|
int ssd = 0;
|
|
|
|
int y, x;
|
|
|
|
|
|
|
|
for (y = 0; y < width; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
int diff = ref[x + y * ref_stride] - rec[x + y * rec_stride];
|
|
|
|
ssd += diff * diff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ssd;
|
|
|
|
}
|
2014-05-14 08:35:38 +00:00
|
|
|
|
|
|
|
unsigned calc_abs_coeff(const coefficient *const buf, const int buf_stride,
|
|
|
|
const int width)
|
|
|
|
{
|
|
|
|
int sum = 0;
|
|
|
|
int y, x;
|
|
|
|
|
|
|
|
for (y = 0; y < width; ++y) {
|
|
|
|
for (x = 0; x < width; ++x) {
|
|
|
|
sum += abs(buf[x + y * buf_stride]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|