2015-05-18 08:43:10 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
* This file is part of Kvazaar HEVC encoder.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013-2015 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 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with Kvazaar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-06-30 10:14:24 +00:00
|
|
|
#include "kvazaar_internal.h"
|
2015-05-18 08:43:10 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "encoder.h"
|
|
|
|
#include "strategyselector.h"
|
|
|
|
#include "encoderstate.h"
|
2015-05-19 14:48:37 +00:00
|
|
|
#include "checkpoint.h"
|
2015-05-20 14:08:35 +00:00
|
|
|
#include "bitstream.h"
|
2015-05-18 08:43:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void kvazaar_close(kvz_encoder *encoder)
|
|
|
|
{
|
|
|
|
if (encoder) {
|
2015-06-08 14:29:10 +00:00
|
|
|
if (encoder->states) {
|
|
|
|
for (unsigned i = 0; i < encoder->num_encoder_states; ++i) {
|
|
|
|
encoder_state_finalize(&encoder->states[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 08:43:10 +00:00
|
|
|
FREE_POINTER(encoder->states);
|
2015-06-30 06:14:31 +00:00
|
|
|
|
|
|
|
encoder_control_free(encoder->control);
|
|
|
|
encoder->control = NULL;
|
2015-05-18 08:43:10 +00:00
|
|
|
}
|
|
|
|
FREE_POINTER(encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
static kvz_encoder * kvazaar_open(const kvz_config *cfg)
|
2015-05-18 08:43:10 +00:00
|
|
|
{
|
|
|
|
kvz_encoder *encoder = NULL;
|
|
|
|
|
|
|
|
//Initialize strategies
|
|
|
|
// TODO: Make strategies non-global
|
|
|
|
if (!strategyselector_init(cfg->cpuid)) {
|
|
|
|
fprintf(stderr, "Failed to initialize strategies.\n");
|
|
|
|
goto kvazaar_open_failure;
|
|
|
|
}
|
|
|
|
|
2015-06-03 08:18:45 +00:00
|
|
|
init_exp_golomb();
|
2015-05-18 08:43:10 +00:00
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder = calloc(1, sizeof(kvz_encoder));
|
2015-05-18 08:43:10 +00:00
|
|
|
if (!encoder) {
|
|
|
|
goto kvazaar_open_failure;
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder->control = encoder_control_init(cfg);
|
|
|
|
if (!encoder->control) {
|
2015-05-18 08:43:10 +00:00
|
|
|
goto kvazaar_open_failure;
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder->num_encoder_states = encoder->control->owf + 1;
|
2015-05-18 08:43:10 +00:00
|
|
|
encoder->cur_state_num = 0;
|
2015-05-19 14:48:37 +00:00
|
|
|
encoder->frames_started = 0;
|
2015-06-03 17:09:36 +00:00
|
|
|
encoder->frames_done = 0;
|
2015-06-30 06:14:31 +00:00
|
|
|
encoder->states = calloc(encoder->num_encoder_states, sizeof(encoder_state_t));
|
2015-05-18 08:43:10 +00:00
|
|
|
if (!encoder->states) {
|
|
|
|
goto kvazaar_open_failure;
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
for (unsigned i = 0; i < encoder->num_encoder_states; ++i) {
|
2015-05-18 08:43:10 +00:00
|
|
|
encoder->states[i].encoder_control = encoder->control;
|
|
|
|
|
|
|
|
if (!encoder_state_init(&encoder->states[i], NULL)) {
|
|
|
|
goto kvazaar_open_failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder->states[i].global->QP = (int8_t)cfg->qp;
|
|
|
|
}
|
|
|
|
|
2015-06-30 06:14:31 +00:00
|
|
|
for (int i = 0; i < encoder->num_encoder_states; ++i) {
|
2015-05-18 08:43:10 +00:00
|
|
|
if (i == 0) {
|
|
|
|
encoder->states[i].previous_encoder_state = &encoder->states[encoder->num_encoder_states - 1];
|
|
|
|
} else {
|
|
|
|
encoder->states[i].previous_encoder_state = &encoder->states[(i - 1) % encoder->num_encoder_states];
|
|
|
|
}
|
|
|
|
encoder_state_match_children_of_previous_frame(&encoder->states[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder->states[encoder->cur_state_num].global->frame = -1;
|
|
|
|
|
|
|
|
return encoder;
|
|
|
|
|
|
|
|
kvazaar_open_failure:
|
|
|
|
kvazaar_close(encoder);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
static int kvazaar_encode(kvz_encoder *enc,
|
|
|
|
kvz_picture *pic_in,
|
2015-06-30 09:22:30 +00:00
|
|
|
kvz_data_chunk **data_out,
|
|
|
|
uint32_t *len_out,
|
|
|
|
kvz_picture **pic_out)
|
2015-05-18 08:43:10 +00:00
|
|
|
{
|
2015-06-30 09:04:00 +00:00
|
|
|
if (data_out) *data_out = NULL;
|
2015-06-30 09:22:30 +00:00
|
|
|
if (len_out) *len_out = 0;
|
|
|
|
if (pic_out) *pic_out = NULL;
|
2015-05-19 14:48:37 +00:00
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
encoder_state_t *state = &enc->states[enc->cur_state_num];
|
2015-05-19 14:48:37 +00:00
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
if (!state->prepared) {
|
|
|
|
encoder_next_frame(state);
|
|
|
|
}
|
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
if (pic_in != NULL) {
|
2015-06-18 06:10:47 +00:00
|
|
|
// FIXME: The frame number printed here is wrong when GOP is enabled.
|
2015-05-19 14:48:37 +00:00
|
|
|
CHECKPOINT_MARK("read source frame: %d", state->global->frame + enc->control->cfg->seek);
|
2015-06-18 06:10:47 +00:00
|
|
|
}
|
2015-05-19 14:48:37 +00:00
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
if (encoder_feed_frame(state, pic_in)) {
|
2015-06-18 06:10:47 +00:00
|
|
|
assert(state->global->frame == enc->frames_started);
|
|
|
|
// Start encoding.
|
2015-05-19 14:48:37 +00:00
|
|
|
encode_one_frame(state);
|
2015-06-18 06:10:47 +00:00
|
|
|
enc->frames_started += 1;
|
2015-05-19 14:48:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 17:09:36 +00:00
|
|
|
// If we have finished encoding as many frames as we have started, we are done.
|
|
|
|
if (enc->frames_done == enc->frames_started) {
|
2015-06-18 06:10:47 +00:00
|
|
|
return 1;
|
2015-06-03 17:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
// Move to the next encoder state.
|
2015-05-19 14:48:37 +00:00
|
|
|
enc->cur_state_num = (enc->cur_state_num + 1) % (enc->num_encoder_states);
|
2015-06-18 06:10:47 +00:00
|
|
|
state = &enc->states[enc->cur_state_num];
|
2015-05-19 14:48:37 +00:00
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
if (!state->frame_done) {
|
2015-05-19 14:48:37 +00:00
|
|
|
threadqueue_waitfor(enc->control->threadqueue, state->tqj_bitstream_written);
|
2015-06-18 06:10:47 +00:00
|
|
|
|
2015-06-30 09:22:30 +00:00
|
|
|
// Get stream length before taking chunks since that clears the stream.
|
|
|
|
if (len_out) *len_out = bitstream_tell(&state->stream) / 8;
|
2015-06-30 09:04:00 +00:00
|
|
|
if (data_out) *data_out = bitstream_take_chunks(&state->stream);
|
2015-06-30 09:22:30 +00:00
|
|
|
if (pic_out) *pic_out = image_copy_ref(state->tile->frame->rec);
|
2015-06-03 17:09:36 +00:00
|
|
|
|
2015-06-17 11:17:30 +00:00
|
|
|
state->frame_done = 1;
|
2015-06-18 06:10:47 +00:00
|
|
|
state->prepared = 0;
|
|
|
|
enc->frames_done += 1;
|
2015-05-19 14:48:37 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 17:09:36 +00:00
|
|
|
return 1;
|
2015-05-18 08:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kvz_api kvz_8bit_api = {
|
|
|
|
.config_alloc = config_alloc,
|
|
|
|
.config_init = config_init,
|
|
|
|
.config_destroy = config_destroy,
|
2015-05-20 13:19:40 +00:00
|
|
|
.config_parse = config_parse,
|
2015-05-18 08:43:10 +00:00
|
|
|
|
2015-06-30 09:11:58 +00:00
|
|
|
.picture_alloc = image_alloc,
|
|
|
|
.picture_free = image_free,
|
|
|
|
|
2015-06-30 09:04:00 +00:00
|
|
|
.chunk_free = bitstream_free_chunks,
|
|
|
|
|
2015-05-18 08:43:10 +00:00
|
|
|
.encoder_open = kvazaar_open,
|
|
|
|
.encoder_close = kvazaar_close,
|
|
|
|
.encoder_encode = kvazaar_encode,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const kvz_api * kvz_api_get(int bit_depth)
|
|
|
|
{
|
|
|
|
return &kvz_8bit_api;
|
|
|
|
}
|