2014-06-05 12:08:31 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Copyright (C) 2013-2015 Tampere University of Technology and others (see
|
2014-06-05 12:08:31 +00:00
|
|
|
* COPYING file).
|
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* Kvazaar is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2.1 of the License, or (at your
|
|
|
|
* option) any later version.
|
2014-06-05 12:08:31 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* 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 Lesser General Public License for
|
|
|
|
* more details.
|
2014-06-05 12:08:31 +00:00
|
|
|
*
|
2015-02-23 11:18:48 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
2014-06-05 12:08:31 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* \file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "sao.h"
|
|
|
|
#include "threads.h"
|
|
|
|
#include "videoframe.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Allocate new frame
|
|
|
|
* \param pic picture pointer
|
|
|
|
* \return picture pointer
|
|
|
|
*/
|
2015-03-04 11:17:33 +00:00
|
|
|
videoframe_t *videoframe_alloc(const int32_t width, const int32_t height, const int32_t poc) {
|
|
|
|
videoframe_t *frame = MALLOC(videoframe_t, 1);
|
2014-06-05 12:08:31 +00:00
|
|
|
|
|
|
|
if (!frame) return 0;
|
|
|
|
|
2015-02-13 09:56:55 +00:00
|
|
|
FILL(*frame, 0);
|
2014-06-05 12:08:31 +00:00
|
|
|
|
|
|
|
frame->width = width;
|
|
|
|
frame->height = height;
|
|
|
|
frame->width_in_lcu = frame->width / LCU_WIDTH;
|
|
|
|
if (frame->width_in_lcu * LCU_WIDTH < frame->width) frame->width_in_lcu++;
|
|
|
|
frame->height_in_lcu = frame->height / LCU_WIDTH;
|
|
|
|
if (frame->height_in_lcu * LCU_WIDTH < frame->height) frame->height_in_lcu++;
|
|
|
|
|
|
|
|
{
|
|
|
|
// Allocate height_in_scu x width_in_scu x sizeof(CU_info)
|
|
|
|
unsigned height_in_scu = frame->height_in_lcu << MAX_DEPTH;
|
|
|
|
unsigned width_in_scu = frame->width_in_lcu << MAX_DEPTH;
|
2014-06-16 06:17:22 +00:00
|
|
|
frame->cu_array = cu_array_alloc(width_in_scu, height_in_scu);
|
2014-06-05 12:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
frame->coeff_y = NULL; frame->coeff_u = NULL; frame->coeff_v = NULL;
|
|
|
|
|
2015-03-04 14:32:38 +00:00
|
|
|
frame->sao_luma = MALLOC(sao_info_t, frame->width_in_lcu * frame->height_in_lcu);
|
|
|
|
frame->sao_chroma = MALLOC(sao_info_t, frame->width_in_lcu * frame->height_in_lcu);
|
2014-06-05 12:08:31 +00:00
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Free memory allocated to frame
|
|
|
|
* \param pic picture pointer
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2015-03-04 11:17:33 +00:00
|
|
|
int videoframe_free(videoframe_t * const frame)
|
2014-06-05 12:08:31 +00:00
|
|
|
{
|
2015-06-17 08:06:09 +00:00
|
|
|
image_free(frame->source);
|
|
|
|
frame->source = NULL;
|
|
|
|
image_free(frame->rec);
|
|
|
|
frame->rec = NULL;
|
2014-06-05 12:08:31 +00:00
|
|
|
|
2014-06-16 08:42:28 +00:00
|
|
|
cu_array_free(frame->cu_array);
|
2014-06-05 12:08:31 +00:00
|
|
|
|
|
|
|
FREE_POINTER(frame->coeff_y);
|
|
|
|
FREE_POINTER(frame->coeff_u);
|
|
|
|
FREE_POINTER(frame->coeff_v);
|
|
|
|
|
|
|
|
FREE_POINTER(frame->sao_luma);
|
|
|
|
FREE_POINTER(frame->sao_chroma);
|
|
|
|
|
|
|
|
free(frame);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:17:33 +00:00
|
|
|
void videoframe_set_poc(videoframe_t * const frame, const int32_t poc) {
|
2014-06-11 08:09:38 +00:00
|
|
|
frame->poc = poc;
|
|
|
|
}
|
|
|
|
|
2015-03-04 11:17:33 +00:00
|
|
|
const cu_info_t* videoframe_get_cu_const(const videoframe_t * const frame, unsigned int x_in_scu, unsigned int y_in_scu)
|
2015-03-04 11:15:45 +00:00
|
|
|
{
|
2014-06-05 12:08:31 +00:00
|
|
|
assert(x_in_scu < (frame->width_in_lcu << MAX_DEPTH));
|
|
|
|
assert(y_in_scu < (frame->height_in_lcu << MAX_DEPTH));
|
|
|
|
|
2014-06-16 06:17:22 +00:00
|
|
|
return &frame->cu_array->data[x_in_scu + y_in_scu * (frame->width_in_lcu << MAX_DEPTH)];
|
2014-06-05 12:08:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 11:17:33 +00:00
|
|
|
cu_info_t* videoframe_get_cu(videoframe_t * const frame, const unsigned int x_in_scu, const unsigned int y_in_scu)
|
2015-03-04 11:15:45 +00:00
|
|
|
{
|
2014-06-05 12:08:31 +00:00
|
|
|
assert(x_in_scu < (frame->width_in_lcu << MAX_DEPTH));
|
|
|
|
assert(y_in_scu < (frame->height_in_lcu << MAX_DEPTH));
|
|
|
|
|
2014-06-16 06:17:22 +00:00
|
|
|
return &frame->cu_array->data[x_in_scu + y_in_scu * (frame->width_in_lcu << MAX_DEPTH)];
|
2014-06-05 12:08:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 05:42:09 +00:00
|
|
|
#if KVZ_BIT_DEPTH == 8
|
2014-06-05 12:08:31 +00:00
|
|
|
#define PSNRMAX (255.0 * 255.0)
|
2015-04-29 12:10:37 +00:00
|
|
|
#else
|
|
|
|
#define PSNRMAX ((double)PIXEL_MAX * (double)PIXEL_MAX)
|
|
|
|
#endif
|
2014-06-05 12:08:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Calculates image PSNR value
|
|
|
|
*/
|
2015-03-04 11:17:33 +00:00
|
|
|
void videoframe_compute_psnr(const videoframe_t * const frame, double psnr[NUM_COLORS])
|
2014-06-05 12:08:31 +00:00
|
|
|
{
|
|
|
|
int32_t pixels = frame->width * frame->height;
|
|
|
|
int32_t i, c;
|
|
|
|
|
|
|
|
for (c = 0; c < NUM_COLORS; ++c) {
|
2014-06-05 12:54:58 +00:00
|
|
|
int32_t num_pixels = pixels;
|
|
|
|
if (c != COLOR_Y) {
|
|
|
|
num_pixels >>= 2;
|
|
|
|
}
|
2014-06-05 12:08:31 +00:00
|
|
|
psnr[c] = 0;
|
2014-06-05 12:54:58 +00:00
|
|
|
for (i = 0; i < num_pixels; ++i) {
|
2014-06-05 12:08:31 +00:00
|
|
|
const int32_t error = frame->source->data[c][i] - frame->rec->data[c][i];
|
|
|
|
psnr[c] += error * error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid division by zero
|
|
|
|
if (psnr[c] == 0) psnr[c] = 99.0;
|
2014-06-05 12:54:58 +00:00
|
|
|
psnr[c] = 10 * log10((num_pixels * PSNRMAX) / ((double)psnr[c]));;
|
2014-06-05 12:08:31 +00:00
|
|
|
}
|
|
|
|
}
|