2014-06-05 07:48:59 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2014 Tampere University of Technology and others (see
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "threads.h"
|
2014-06-05 12:54:58 +00:00
|
|
|
#include "imagelist.h"
|
2014-06-05 07:48:59 +00:00
|
|
|
#include "strategyselector.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-05 12:54:58 +00:00
|
|
|
* \brief Allocate memory for image_list
|
2014-06-05 07:48:59 +00:00
|
|
|
* \param size initial array size
|
2014-06-05 12:54:58 +00:00
|
|
|
* \return image_list pointer, NULL on failure
|
2014-06-05 07:48:59 +00:00
|
|
|
*/
|
2014-06-05 12:54:58 +00:00
|
|
|
image_list * image_list_alloc(int size)
|
2014-06-05 07:48:59 +00:00
|
|
|
{
|
2014-06-05 12:54:58 +00:00
|
|
|
image_list *list = (image_list *)malloc(sizeof(image_list));
|
2014-06-05 07:48:59 +00:00
|
|
|
list->size = size;
|
|
|
|
if (size > 0) {
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images = (image**)malloc(sizeof(image*) * size);
|
2014-06-16 06:17:22 +00:00
|
|
|
list->cu_arrays = (cu_array**)malloc(sizeof(cu_array*) * size);
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list->used_size = 0;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-05 12:54:58 +00:00
|
|
|
* \brief Resize image_list array
|
|
|
|
* \param list image_list pointer
|
2014-06-05 07:48:59 +00:00
|
|
|
* \param size new array size
|
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2014-06-05 12:54:58 +00:00
|
|
|
int image_list_resize(image_list *list, unsigned size)
|
2014-06-05 07:48:59 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2014-06-05 12:54:58 +00:00
|
|
|
image** old_images = NULL;
|
2014-06-16 06:17:22 +00:00
|
|
|
cu_array** old_cu_arrays = NULL;
|
2014-06-05 12:54:58 +00:00
|
|
|
|
|
|
|
//FIXME This could be done in a simple way using realloc...
|
2014-06-05 07:48:59 +00:00
|
|
|
|
|
|
|
// No need to do anything when resizing to same size
|
|
|
|
if (size == list->size) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the old list
|
|
|
|
if (list->used_size > 0) {
|
2014-06-05 12:54:58 +00:00
|
|
|
old_images = list->images;
|
|
|
|
old_cu_arrays = list->cu_arrays;
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// allocate space for the new list
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images = (image**)malloc(sizeof(image*)*size);
|
2014-06-16 06:17:22 +00:00
|
|
|
list->cu_arrays = (cu_array**)malloc(sizeof(cu_array*)*size);
|
2014-06-05 07:48:59 +00:00
|
|
|
|
|
|
|
// Copy everything from the old list to the new if needed.
|
2014-06-05 12:54:58 +00:00
|
|
|
if (old_images != NULL) {
|
2014-06-05 07:48:59 +00:00
|
|
|
for (i = 0; i < list->used_size; ++i) {
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[i] = old_images[i];
|
|
|
|
list->cu_arrays[i] = old_cu_arrays[i];
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
free(old_images);
|
|
|
|
free(old_cu_arrays);
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Free memory allocated to the picture_list
|
2014-06-05 12:54:58 +00:00
|
|
|
* \param list image_list pointer
|
2014-06-05 07:48:59 +00:00
|
|
|
* \return 1 on success, 0 on failure
|
|
|
|
*/
|
2014-06-05 12:54:58 +00:00
|
|
|
int image_list_destroy(image_list *list)
|
2014-06-05 07:48:59 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
if (list->used_size > 0) {
|
|
|
|
for (i = 0; i < list->used_size; ++i) {
|
2014-06-05 12:54:58 +00:00
|
|
|
image_free(list->images[i]);
|
|
|
|
list->images[i] = NULL;
|
2014-06-16 06:17:22 +00:00
|
|
|
cu_array_free(list->cu_arrays[i]);
|
2014-06-11 08:08:16 +00:00
|
|
|
list->cu_arrays[i] = NULL;
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (list->size > 0) {
|
2014-06-05 12:54:58 +00:00
|
|
|
free(list->images);
|
|
|
|
free(list->cu_arrays);
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
free(list);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Add picture to the front of the picturelist
|
|
|
|
* \param pic picture pointer to add
|
|
|
|
* \param picture_list list to use
|
|
|
|
* \return 1 on success
|
|
|
|
*/
|
2014-06-16 06:17:22 +00:00
|
|
|
int image_list_add(image_list *list, image* im, cu_array* cua)
|
2014-06-05 07:48:59 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2014-06-05 12:54:58 +00:00
|
|
|
if (ATOMIC_INC(&(im->refcount)) == 1) {
|
2014-06-05 07:48:59 +00:00
|
|
|
fprintf(stderr, "Tried to add an unreferenced picture. This is a bug!\n");
|
|
|
|
assert(0); //Stop for debugging
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-16 06:17:22 +00:00
|
|
|
|
|
|
|
if (ATOMIC_INC(&(cua->refcount)) == 1) {
|
|
|
|
fprintf(stderr, "Tried to add an unreferenced cu_array. This is a bug!\n");
|
|
|
|
assert(0); //Stop for debugging
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-05 07:48:59 +00:00
|
|
|
|
|
|
|
if (list->size == list->used_size) {
|
2014-06-05 12:54:58 +00:00
|
|
|
if (!image_list_resize(list, list->size*2)) return 0;
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = list->used_size; i > 0; i--) {
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[i] = list->images[i - 1];
|
|
|
|
list->cu_arrays[i] = list->cu_arrays[i - 1];
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[0] = im;
|
2014-06-16 06:17:22 +00:00
|
|
|
list->cu_arrays[0] = cua;
|
|
|
|
|
2014-06-05 07:48:59 +00:00
|
|
|
list->used_size++;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Remove picture from picturelist
|
|
|
|
* \param list list to use
|
|
|
|
* \param n index to remove
|
|
|
|
* \return 1 on success
|
|
|
|
*/
|
2014-06-05 12:54:58 +00:00
|
|
|
int image_list_rem(image_list * const list, const unsigned n)
|
2014-06-05 07:48:59 +00:00
|
|
|
{
|
|
|
|
// Must be within list boundaries
|
|
|
|
if (n >= list->used_size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-05 12:54:58 +00:00
|
|
|
if (!image_free(list->images[n])) {
|
|
|
|
fprintf(stderr, "Could not free image!\n");
|
2014-06-05 07:48:59 +00:00
|
|
|
assert(0); //Stop here
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-16 06:17:22 +00:00
|
|
|
|
|
|
|
if (!cu_array_free(list->cu_arrays[n])) {
|
|
|
|
fprintf(stderr, "Could not free cu_array!\n");
|
|
|
|
assert(0); //Stop here
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-05 07:48:59 +00:00
|
|
|
|
|
|
|
// The last item is easy to remove
|
|
|
|
if (n == list->used_size - 1) {
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[n] = NULL;
|
2014-06-11 08:08:16 +00:00
|
|
|
list->cu_arrays[n] = NULL;
|
2014-06-05 07:48:59 +00:00
|
|
|
list->used_size--;
|
|
|
|
} else {
|
|
|
|
int i = n;
|
|
|
|
// Shift all following pics one backward in the list
|
|
|
|
for (i = n; i < list->used_size - 1; ++i) {
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[i] = list->images[i + 1];
|
2014-06-11 08:08:16 +00:00
|
|
|
list->cu_arrays[i] = list->cu_arrays[i + 1];
|
2014-06-05 07:48:59 +00:00
|
|
|
}
|
2014-06-05 12:54:58 +00:00
|
|
|
list->images[list->used_size - 1] = NULL;
|
2014-06-11 08:08:16 +00:00
|
|
|
list->cu_arrays[list->used_size - 1] = NULL;
|
2014-06-05 07:48:59 +00:00
|
|
|
list->used_size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2014-06-16 06:18:26 +00:00
|
|
|
|
|
|
|
int image_list_copy_contents(image_list *target, image_list *source) {
|
|
|
|
int i;
|
|
|
|
while (target->used_size > 0) {
|
|
|
|
image_list_rem(target, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = source->used_size - 1; i >= 0; --i) {
|
|
|
|
image_list_add(target, source->images[i], source->cu_arrays[i]);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|