2014-06-03 11:51:30 +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-03 11:51:30 +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-03 11:51:30 +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-03 11:51:30 +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-03 11:51:30 +00:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "encoderstate.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
2016-04-01 14:14:23 +00:00
|
|
|
#include <stdio.h>
|
2014-06-03 11:51:30 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "cabac.h"
|
|
|
|
#include "context.h"
|
2016-08-09 13:01:51 +00:00
|
|
|
#include "encode_coding_tree.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "encoder_state-bitstream.h"
|
2014-06-03 11:51:30 +00:00
|
|
|
#include "filter.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "image.h"
|
2015-03-13 12:23:54 +00:00
|
|
|
#include "rate_control.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
#include "sao.h"
|
|
|
|
#include "search.h"
|
|
|
|
#include "tables.h"
|
2017-04-04 12:36:08 +00:00
|
|
|
#include "threadqueue.h"
|
2019-08-15 08:01:38 +00:00
|
|
|
#include "alf.h"
|
2016-04-01 14:14:23 +00:00
|
|
|
|
2020-02-24 17:37:36 +00:00
|
|
|
#include "strategies/strategies-picture.h"
|
|
|
|
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
int kvz_encoder_state_match_children_of_previous_frame(encoder_state_t * const state) {
|
2014-06-05 07:09:25 +00:00
|
|
|
int i;
|
2015-03-04 15:00:23 +00:00
|
|
|
for (i = 0; state->children[i].encoder_control; ++i) {
|
2014-06-05 07:09:25 +00:00
|
|
|
//Child should also exist for previous encoder
|
2015-03-04 15:00:23 +00:00
|
|
|
assert(state->previous_encoder_state->children[i].encoder_control);
|
|
|
|
state->children[i].previous_encoder_state = &state->previous_encoder_state->children[i];
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_encoder_state_match_children_of_previous_frame(&state->children[i]);
|
2014-06-05 07:09:25 +00:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-06-30 13:12:52 +00:00
|
|
|
/**
|
|
|
|
* \brief Save edge pixels before SAO to buffers.
|
|
|
|
*
|
|
|
|
* Copies pixels at the edges of the area that will be filtered with SAO to
|
|
|
|
* the given buffers. If deblocking is enabled, the pixels must have been
|
|
|
|
* deblocked before this.
|
|
|
|
*
|
|
|
|
* The saved pixels will be needed later when doing SAO for the neighboring
|
|
|
|
* areas.
|
|
|
|
*/
|
|
|
|
static void encoder_state_recdata_before_sao_to_bufs(
|
|
|
|
encoder_state_t * const state,
|
|
|
|
const lcu_order_element_t * const lcu,
|
|
|
|
yuv_t * const hor_buf,
|
|
|
|
yuv_t * const ver_buf)
|
|
|
|
{
|
|
|
|
videoframe_t* const frame = state->tile->frame;
|
|
|
|
|
|
|
|
if (hor_buf && lcu->below) {
|
|
|
|
// Copy the bottommost row that will be filtered with SAO to the
|
|
|
|
// horizontal buffer.
|
|
|
|
vector2d_t pos = {
|
|
|
|
.x = lcu->position_px.x,
|
|
|
|
.y = lcu->position_px.y + LCU_WIDTH - SAO_DELAY_PX - 1,
|
|
|
|
};
|
|
|
|
// Copy all pixels that have been deblocked.
|
|
|
|
int length = lcu->size.x - DEBLOCK_DELAY_PX;
|
|
|
|
|
|
|
|
if (!lcu->right) {
|
|
|
|
// If there is no LCU to the right, the last pixels will be
|
|
|
|
// filtered too.
|
|
|
|
length += DEBLOCK_DELAY_PX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lcu->left) {
|
|
|
|
// The rightmost pixels of the CTU to the left will also be filtered.
|
|
|
|
pos.x -= DEBLOCK_DELAY_PX;
|
|
|
|
length += DEBLOCK_DELAY_PX;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned from_index = pos.x + pos.y * frame->rec->stride;
|
|
|
|
// NOTE: The horizontal buffer is indexed by
|
|
|
|
// x_px + y_lcu * frame->width
|
|
|
|
// where x_px is in pixels and y_lcu in number of LCUs.
|
|
|
|
const unsigned to_index = pos.x + lcu->position.y * frame->width;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->y[from_index],
|
|
|
|
&hor_buf->y[to_index],
|
|
|
|
length, 1,
|
|
|
|
frame->rec->stride,
|
|
|
|
frame->width);
|
|
|
|
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
const unsigned from_index_c = (pos.x / 2) + (pos.y / 2) * frame->rec->stride / 2;
|
|
|
|
const unsigned to_index_c = (pos.x / 2) + lcu->position.y * frame->width / 2;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->u[from_index_c],
|
|
|
|
&hor_buf->u[to_index_c],
|
|
|
|
length / 2, 1,
|
|
|
|
frame->rec->stride / 2,
|
|
|
|
frame->width / 2);
|
|
|
|
kvz_pixels_blit(&frame->rec->v[from_index_c],
|
|
|
|
&hor_buf->v[to_index_c],
|
|
|
|
length / 2, 1,
|
|
|
|
frame->rec->stride / 2,
|
|
|
|
frame->width / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ver_buf && lcu->right) {
|
|
|
|
// Copy the rightmost column that will be filtered with SAO to the
|
|
|
|
// vertical buffer.
|
|
|
|
vector2d_t pos = {
|
|
|
|
.x = lcu->position_px.x + LCU_WIDTH - SAO_DELAY_PX - 1,
|
|
|
|
.y = lcu->position_px.y,
|
|
|
|
};
|
|
|
|
int length = lcu->size.y - DEBLOCK_DELAY_PX;
|
|
|
|
|
|
|
|
if (!lcu->below) {
|
|
|
|
// If there is no LCU below, the last pixels will be filtered too.
|
|
|
|
length += DEBLOCK_DELAY_PX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lcu->above) {
|
|
|
|
// The bottommost pixels of the CTU above will also be filtered.
|
|
|
|
pos.y -= DEBLOCK_DELAY_PX;
|
|
|
|
length += DEBLOCK_DELAY_PX;
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned from_index = pos.x + pos.y * frame->rec->stride;
|
|
|
|
// NOTE: The vertical buffer is indexed by
|
|
|
|
// x_lcu * frame->height + y_px
|
|
|
|
// where x_lcu is in number of LCUs and y_px in pixels.
|
|
|
|
const unsigned to_index = lcu->position.x * frame->height + pos.y;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->y[from_index],
|
|
|
|
&ver_buf->y[to_index],
|
|
|
|
1, length,
|
|
|
|
frame->rec->stride, 1);
|
|
|
|
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
const unsigned from_index_c = (pos.x / 2) + (pos.y / 2) * frame->rec->stride / 2;
|
|
|
|
const unsigned to_index_c = lcu->position.x * frame->height / 2 + pos.y / 2;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->u[from_index_c],
|
|
|
|
&ver_buf->u[to_index_c],
|
|
|
|
1, length / 2,
|
|
|
|
frame->rec->stride / 2, 1);
|
|
|
|
kvz_pixels_blit(&frame->rec->v[from_index_c],
|
|
|
|
&ver_buf->v[to_index_c],
|
|
|
|
1, length / 2,
|
|
|
|
frame->rec->stride / 2, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encoder_state_recdata_to_bufs(encoder_state_t * const state,
|
|
|
|
const lcu_order_element_t * const lcu,
|
|
|
|
yuv_t * const hor_buf,
|
|
|
|
yuv_t * const ver_buf)
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
videoframe_t* const frame = state->tile->frame;
|
2014-06-03 12:25:16 +00:00
|
|
|
|
|
|
|
if (hor_buf) {
|
|
|
|
//Copy the bottom row of this LCU to the horizontal buffer
|
2016-08-25 13:05:46 +00:00
|
|
|
vector2d_t bottom = { lcu->position_px.x, lcu->position_px.y + lcu->size.y - 1 };
|
|
|
|
const int lcu_row = lcu->position.y;
|
|
|
|
|
|
|
|
unsigned from_index = bottom.y * frame->rec->stride + bottom.x;
|
|
|
|
unsigned to_index = lcu->position_px.x + lcu_row * frame->width;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->y[from_index],
|
|
|
|
&hor_buf->y[to_index],
|
|
|
|
lcu->size.x, 1,
|
|
|
|
frame->rec->stride, frame->width);
|
|
|
|
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
unsigned from_index_c = (bottom.y / 2) * frame->rec->stride / 2 + (bottom.x / 2);
|
|
|
|
unsigned to_index_c = lcu->position_px.x / 2 + lcu_row * frame->width / 2;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->u[from_index_c],
|
|
|
|
&hor_buf->u[to_index_c],
|
|
|
|
lcu->size.x / 2, 1,
|
|
|
|
frame->rec->stride / 2, frame->width / 2);
|
|
|
|
kvz_pixels_blit(&frame->rec->v[from_index_c],
|
|
|
|
&hor_buf->v[to_index_c],
|
|
|
|
lcu->size.x / 2, 1,
|
|
|
|
frame->rec->stride / 2, frame->width / 2);
|
|
|
|
}
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ver_buf) {
|
2016-08-25 13:05:46 +00:00
|
|
|
//Copy the right row of this LCU to the vertical buffer.
|
2014-06-03 12:25:16 +00:00
|
|
|
|
2016-08-25 13:05:46 +00:00
|
|
|
const int lcu_col = lcu->position.x;
|
|
|
|
vector2d_t left = { lcu->position_px.x + lcu->size.x - 1, lcu->position_px.y };
|
2014-06-03 12:25:16 +00:00
|
|
|
|
2016-08-25 13:05:46 +00:00
|
|
|
kvz_pixels_blit(&frame->rec->y[left.y * frame->rec->stride + left.x],
|
|
|
|
&ver_buf->y[lcu->position_px.y + lcu_col * frame->height],
|
|
|
|
1, lcu->size.y,
|
|
|
|
frame->rec->stride, 1);
|
|
|
|
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
unsigned from_index = (left.y / 2) * frame->rec->stride / 2 + (left.x / 2);
|
|
|
|
unsigned to_index = lcu->position_px.y / 2 + lcu_col * frame->height / 2;
|
|
|
|
|
|
|
|
kvz_pixels_blit(&frame->rec->u[from_index],
|
|
|
|
&ver_buf->u[to_index],
|
|
|
|
1, lcu->size.y / 2,
|
|
|
|
frame->rec->stride / 2, 1);
|
|
|
|
kvz_pixels_blit(&frame->rec->v[from_index],
|
|
|
|
&ver_buf->v[to_index],
|
|
|
|
1, lcu->size.y / 2,
|
|
|
|
frame->rec->stride / 2, 1);
|
|
|
|
}
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-30 13:12:52 +00:00
|
|
|
/**
|
|
|
|
* \brief Do SAO reconstuction for all available pixels.
|
|
|
|
*
|
|
|
|
* Does SAO reconstruction for all pixels that are available after the
|
|
|
|
* given LCU has been deblocked. This means the following pixels:
|
|
|
|
* - bottom-right block of SAO_DELAY_PX times SAO_DELAY_PX in the lcu to
|
|
|
|
* the left and up
|
|
|
|
* - the rightmost SAO_DELAY_PX pixels of the LCU to the left (excluding
|
|
|
|
* the bottommost pixel)
|
|
|
|
* - the bottommost SAO_DELAY_PX pixels of the LCU above (excluding the
|
|
|
|
* rightmost pixels)
|
|
|
|
* - all pixels inside the LCU, excluding the rightmost SAO_DELAY_PX and
|
|
|
|
* bottommost SAO_DELAY_PX
|
|
|
|
*/
|
|
|
|
static void encoder_sao_reconstruct(const encoder_state_t *const state,
|
|
|
|
const lcu_order_element_t *const lcu)
|
|
|
|
{
|
|
|
|
videoframe_t *const frame = state->tile->frame;
|
|
|
|
|
2018-07-05 12:12:45 +00:00
|
|
|
|
|
|
|
// Temporary buffers for SAO input pixels. The buffers cover the pixels
|
|
|
|
// inside the LCU (LCU_WIDTH x LCU_WIDTH), SAO_DELAY_PX wide bands to the
|
|
|
|
// left and above the LCU, and one pixel border on the left and top
|
|
|
|
// sides. We add two extra pixels to the buffers because the AVX2 SAO
|
|
|
|
// reconstruction reads up to two extra bytes when using edge SAO in the
|
|
|
|
// horizontal direction.
|
|
|
|
#define SAO_BUF_WIDTH (1 + SAO_DELAY_PX + LCU_WIDTH)
|
|
|
|
#define SAO_BUF_WIDTH_C (1 + SAO_DELAY_PX/2 + LCU_WIDTH_C)
|
|
|
|
kvz_pixel sao_buf_y_array[SAO_BUF_WIDTH * SAO_BUF_WIDTH + 2];
|
|
|
|
kvz_pixel sao_buf_u_array[SAO_BUF_WIDTH_C * SAO_BUF_WIDTH_C + 2];
|
|
|
|
kvz_pixel sao_buf_v_array[SAO_BUF_WIDTH_C * SAO_BUF_WIDTH_C + 2];
|
2017-06-30 13:12:52 +00:00
|
|
|
|
|
|
|
// Pointers to the top-left pixel of the LCU in the buffers.
|
|
|
|
kvz_pixel *const sao_buf_y = &sao_buf_y_array[(SAO_DELAY_PX + 1) * (SAO_BUF_WIDTH + 1)];
|
|
|
|
kvz_pixel *const sao_buf_u = &sao_buf_u_array[(SAO_DELAY_PX/2 + 1) * (SAO_BUF_WIDTH_C + 1)];
|
|
|
|
kvz_pixel *const sao_buf_v = &sao_buf_v_array[(SAO_DELAY_PX/2 + 1) * (SAO_BUF_WIDTH_C + 1)];
|
|
|
|
|
|
|
|
const int x_offsets[3] = {
|
|
|
|
// If there is an lcu to the left, we need to filter its rightmost
|
|
|
|
// pixels.
|
|
|
|
lcu->left ? -SAO_DELAY_PX : 0,
|
|
|
|
0,
|
|
|
|
// If there is an lcu to the right, the rightmost pixels of this LCU
|
|
|
|
// are filtered when filtering that LCU. Otherwise we filter them now.
|
|
|
|
lcu->size.x - (lcu->right ? SAO_DELAY_PX : 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
const int y_offsets[3] = {
|
|
|
|
// If there is an lcu above, we need to filter its bottommost pixels.
|
|
|
|
lcu->above ? -SAO_DELAY_PX : 0,
|
|
|
|
0,
|
|
|
|
// If there is an lcu below, the bottommost pixels of this LCU are
|
|
|
|
// filtered when filtering that LCU. Otherwise we filter them now.
|
|
|
|
lcu->size.y - (lcu->below ? SAO_DELAY_PX : 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Number of pixels around the block that need to be copied to the
|
|
|
|
// buffers.
|
|
|
|
const int border_left = lcu->left ? 1 : 0;
|
|
|
|
const int border_right = lcu->right ? 1 : 0;
|
|
|
|
const int border_above = lcu->above ? 1 : 0;
|
|
|
|
const int border_below = lcu->below ? 1 : 0;
|
|
|
|
|
|
|
|
// Index of the pixel at the intersection of the top and left borders.
|
|
|
|
const int border_index = (x_offsets[0] - border_left) +
|
|
|
|
(y_offsets[0] - border_above) * SAO_BUF_WIDTH;
|
|
|
|
const int border_index_c = (x_offsets[0]/2 - border_left) +
|
|
|
|
(y_offsets[0]/2 - border_above) * SAO_BUF_WIDTH_C;
|
|
|
|
// Width and height of the whole area to filter.
|
|
|
|
const int width = x_offsets[2] - x_offsets[0];
|
|
|
|
const int height = y_offsets[2] - y_offsets[0];
|
|
|
|
|
|
|
|
// Copy bordering pixels from above and left to buffers.
|
|
|
|
if (lcu->above) {
|
|
|
|
const int from_index = (lcu->position_px.x + x_offsets[0] - border_left) +
|
|
|
|
(lcu->position.y - 1) * frame->width;
|
|
|
|
kvz_pixels_blit(&state->tile->hor_buf_before_sao->y[from_index],
|
|
|
|
&sao_buf_y[border_index],
|
|
|
|
width + border_left + border_right,
|
|
|
|
1,
|
|
|
|
frame->width,
|
|
|
|
SAO_BUF_WIDTH);
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
const int from_index_c = (lcu->position_px.x + x_offsets[0])/2 - border_left +
|
|
|
|
(lcu->position.y - 1) * frame->width/2;
|
|
|
|
kvz_pixels_blit(&state->tile->hor_buf_before_sao->u[from_index_c],
|
|
|
|
&sao_buf_u[border_index_c],
|
|
|
|
width/2 + border_left + border_right,
|
|
|
|
1,
|
|
|
|
frame->width/2,
|
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
kvz_pixels_blit(&state->tile->hor_buf_before_sao->v[from_index_c],
|
|
|
|
&sao_buf_v[border_index_c],
|
|
|
|
width/2 + border_left + border_right,
|
|
|
|
1,
|
|
|
|
frame->width/2,
|
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lcu->left) {
|
|
|
|
const int from_index = (lcu->position.x - 1) * frame->height +
|
|
|
|
(lcu->position_px.y + y_offsets[0] - border_above);
|
|
|
|
kvz_pixels_blit(&state->tile->ver_buf_before_sao->y[from_index],
|
|
|
|
&sao_buf_y[border_index],
|
|
|
|
1,
|
|
|
|
height + border_above + border_below,
|
|
|
|
1,
|
|
|
|
SAO_BUF_WIDTH);
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
const int from_index_c = (lcu->position.x - 1) * frame->height/2 +
|
|
|
|
(lcu->position_px.y + y_offsets[0])/2 - border_above;
|
|
|
|
kvz_pixels_blit(&state->tile->ver_buf_before_sao->u[from_index_c],
|
|
|
|
&sao_buf_u[border_index_c],
|
|
|
|
1,
|
|
|
|
height/2 + border_above + border_below,
|
|
|
|
1,
|
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
kvz_pixels_blit(&state->tile->ver_buf_before_sao->v[from_index_c],
|
|
|
|
&sao_buf_v[border_index_c],
|
|
|
|
1,
|
|
|
|
height/2 + border_above + border_below,
|
|
|
|
1,
|
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Copy pixels that will be filtered and bordering pixels from right and
|
|
|
|
// below.
|
|
|
|
const int from_index = (lcu->position_px.x + x_offsets[0]) +
|
2017-08-01 12:40:49 +00:00
|
|
|
(lcu->position_px.y + y_offsets[0]) * frame->rec->stride;
|
2017-06-30 13:12:52 +00:00
|
|
|
const int to_index = x_offsets[0] + y_offsets[0] * SAO_BUF_WIDTH;
|
|
|
|
kvz_pixels_blit(&frame->rec->y[from_index],
|
|
|
|
&sao_buf_y[to_index],
|
|
|
|
width + border_right,
|
|
|
|
height + border_below,
|
2017-08-01 12:40:49 +00:00
|
|
|
frame->rec->stride,
|
2017-06-30 13:12:52 +00:00
|
|
|
SAO_BUF_WIDTH);
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
const int from_index_c = (lcu->position_px.x + x_offsets[0])/2 +
|
2017-08-01 12:40:49 +00:00
|
|
|
(lcu->position_px.y + y_offsets[0])/2 * frame->rec->stride/2;
|
2017-06-30 13:12:52 +00:00
|
|
|
const int to_index_c = x_offsets[0]/2 + y_offsets[0]/2 * SAO_BUF_WIDTH_C;
|
|
|
|
kvz_pixels_blit(&frame->rec->u[from_index_c],
|
|
|
|
&sao_buf_u[to_index_c],
|
|
|
|
width/2 + border_right,
|
|
|
|
height/2 + border_below,
|
2017-08-01 12:40:49 +00:00
|
|
|
frame->rec->stride/2,
|
2017-06-30 13:12:52 +00:00
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
kvz_pixels_blit(&frame->rec->v[from_index_c],
|
|
|
|
&sao_buf_v[to_index_c],
|
|
|
|
width/2 + border_right,
|
|
|
|
height/2 + border_below,
|
2017-08-01 12:40:49 +00:00
|
|
|
frame->rec->stride/2,
|
2017-06-30 13:12:52 +00:00
|
|
|
SAO_BUF_WIDTH_C);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We filter the pixels in four parts:
|
|
|
|
// 1. Pixels that belong to the LCU above and to the left
|
|
|
|
// 2. Pixels that belong to the LCU above
|
|
|
|
// 3. Pixels that belong to the LCU to the left
|
|
|
|
// 4. Pixels that belong to the current LCU
|
|
|
|
for (int y_offset_index = 0; y_offset_index < 2; y_offset_index++) {
|
|
|
|
for (int x_offset_index = 0; x_offset_index < 2; x_offset_index++) {
|
|
|
|
const int x = x_offsets[x_offset_index];
|
|
|
|
const int y = y_offsets[y_offset_index];
|
|
|
|
const int width = x_offsets[x_offset_index + 1] - x;
|
|
|
|
const int height = y_offsets[y_offset_index + 1] - y;
|
|
|
|
|
|
|
|
if (width == 0 || height == 0) continue;
|
|
|
|
|
|
|
|
const int lcu_x = (lcu->position_px.x + x) >> LOG2_LCU_WIDTH;
|
|
|
|
const int lcu_y = (lcu->position_px.y + y) >> LOG2_LCU_WIDTH;
|
|
|
|
const int lcu_index = lcu_x + lcu_y * frame->width_in_lcu;
|
|
|
|
const sao_info_t *sao_luma = &frame->sao_luma[lcu_index];
|
|
|
|
const sao_info_t *sao_chroma = &frame->sao_chroma[lcu_index];
|
|
|
|
|
|
|
|
kvz_sao_reconstruct(state,
|
|
|
|
&sao_buf_y[x + y * SAO_BUF_WIDTH],
|
|
|
|
SAO_BUF_WIDTH,
|
|
|
|
lcu->position_px.x + x,
|
|
|
|
lcu->position_px.y + y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
sao_luma,
|
|
|
|
COLOR_Y);
|
|
|
|
|
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
// Coordinates in chroma pixels.
|
|
|
|
int x_c = x >> 1;
|
|
|
|
int y_c = y >> 1;
|
|
|
|
|
|
|
|
kvz_sao_reconstruct(state,
|
|
|
|
&sao_buf_u[x_c + y_c * SAO_BUF_WIDTH_C],
|
|
|
|
SAO_BUF_WIDTH_C,
|
|
|
|
lcu->position_px.x / 2 + x_c,
|
|
|
|
lcu->position_px.y / 2 + y_c,
|
|
|
|
width / 2,
|
|
|
|
height / 2,
|
|
|
|
sao_chroma,
|
|
|
|
COLOR_U);
|
|
|
|
kvz_sao_reconstruct(state,
|
|
|
|
&sao_buf_v[x_c + y_c * SAO_BUF_WIDTH_C],
|
|
|
|
SAO_BUF_WIDTH_C,
|
|
|
|
lcu->position_px.x / 2 + x_c,
|
|
|
|
lcu->position_px.y / 2 + y_c,
|
|
|
|
width / 2,
|
|
|
|
height / 2,
|
|
|
|
sao_chroma,
|
|
|
|
COLOR_V);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static void encode_sao_color(encoder_state_t * const state, sao_info_t *sao,
|
2015-03-04 14:37:35 +00:00
|
|
|
color_t color_i)
|
2014-06-04 14:45:46 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
cabac_data_t * const cabac = &state->cabac;
|
2014-06-04 14:45:46 +00:00
|
|
|
sao_eo_cat i;
|
2014-09-12 09:33:58 +00:00
|
|
|
int offset_index = (color_i == COLOR_V) ? 5 : 0;
|
2014-06-04 14:45:46 +00:00
|
|
|
|
|
|
|
// Skip colors with no SAO.
|
|
|
|
//FIXME: for now, we always have SAO for all channels
|
|
|
|
if (color_i == COLOR_Y && 0) return;
|
|
|
|
if (color_i != COLOR_Y && 0) return;
|
|
|
|
|
|
|
|
/// sao_type_idx_luma: TR, cMax = 2, cRiceParam = 0, bins = {0, bypass}
|
|
|
|
/// sao_type_idx_chroma: TR, cMax = 2, cRiceParam = 0, bins = {0, bypass}
|
|
|
|
// Encode sao_type_idx for Y and U+V.
|
|
|
|
if (color_i != COLOR_V) {
|
2014-09-23 21:58:17 +00:00
|
|
|
cabac->cur_ctx = &(cabac->ctx.sao_type_idx_model);
|
2014-06-04 14:45:46 +00:00
|
|
|
CABAC_BIN(cabac, sao->type != SAO_TYPE_NONE, "sao_type_idx");
|
|
|
|
if (sao->type == SAO_TYPE_BAND) {
|
|
|
|
CABAC_BIN_EP(cabac, 0, "sao_type_idx_ep");
|
|
|
|
} else if (sao->type == SAO_TYPE_EDGE) {
|
|
|
|
CABAC_BIN_EP(cabac, 1, "sao_type_idx_ep");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sao->type == SAO_TYPE_NONE) return;
|
|
|
|
|
|
|
|
/// sao_offset_abs[][][][]: TR, cMax = (1 << (Min(bitDepth, 10) - 5)) - 1,
|
|
|
|
/// cRiceParam = 0, bins = {bypass x N}
|
|
|
|
for (i = SAO_EO_CAT1; i <= SAO_EO_CAT4; ++i) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_cabac_write_unary_max_symbol_ep(cabac, abs(sao->offsets[i + offset_index]), SAO_ABS_OFFSET_MAX);
|
2014-06-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// sao_offset_sign[][][][]: FL, cMax = 1, bins = {bypass}
|
|
|
|
/// sao_band_position[][][]: FL, cMax = 31, bins = {bypass x N}
|
|
|
|
/// sao_eo_class_luma: FL, cMax = 3, bins = {bypass x 3}
|
|
|
|
/// sao_eo_class_chroma: FL, cMax = 3, bins = {bypass x 3}
|
|
|
|
if (sao->type == SAO_TYPE_BAND) {
|
|
|
|
for (i = SAO_EO_CAT1; i <= SAO_EO_CAT4; ++i) {
|
|
|
|
// Positive sign is coded as 0.
|
2014-09-12 09:33:58 +00:00
|
|
|
if (sao->offsets[i + offset_index] != 0) {
|
|
|
|
CABAC_BIN_EP(cabac, sao->offsets[i + offset_index] < 0 ? 1 : 0, "sao_offset_sign");
|
2014-06-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: sao_band_position
|
|
|
|
// FL cMax=31 (5 bits)
|
2014-09-12 09:33:58 +00:00
|
|
|
CABAC_BINS_EP(cabac, sao->band_position[color_i == COLOR_V ? 1:0], 5, "sao_band_position");
|
2014-06-04 14:45:46 +00:00
|
|
|
} else if (color_i != COLOR_V) {
|
|
|
|
CABAC_BINS_EP(cabac, sao->eo_class, 2, "sao_eo_class");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static void encode_sao_merge_flags(encoder_state_t * const state, sao_info_t *sao, unsigned x_ctb, unsigned y_ctb)
|
2014-06-04 14:45:46 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
cabac_data_t * const cabac = &state->cabac;
|
2014-06-04 14:45:46 +00:00
|
|
|
// SAO merge flags are not present for the first row and column.
|
|
|
|
if (x_ctb > 0) {
|
2014-09-23 21:58:17 +00:00
|
|
|
cabac->cur_ctx = &(cabac->ctx.sao_merge_flag_model);
|
2014-06-04 14:45:46 +00:00
|
|
|
CABAC_BIN(cabac, sao->merge_left_flag, "sao_merge_left_flag");
|
|
|
|
}
|
|
|
|
if (y_ctb > 0 && !sao->merge_left_flag) {
|
2014-09-23 21:58:17 +00:00
|
|
|
cabac->cur_ctx = &(cabac->ctx.sao_merge_flag_model);
|
2014-06-04 14:45:46 +00:00
|
|
|
CABAC_BIN(cabac, sao->merge_up_flag, "sao_merge_up_flag");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Encode SAO information.
|
|
|
|
*/
|
2015-03-04 15:00:23 +00:00
|
|
|
static void encode_sao(encoder_state_t * const state,
|
2014-06-04 14:45:46 +00:00
|
|
|
unsigned x_lcu, uint16_t y_lcu,
|
2015-03-04 14:32:38 +00:00
|
|
|
sao_info_t *sao_luma, sao_info_t *sao_chroma)
|
2014-06-04 14:45:46 +00:00
|
|
|
{
|
|
|
|
// TODO: transmit merge flags outside sao_info
|
2015-03-04 15:00:23 +00:00
|
|
|
encode_sao_merge_flags(state, sao_luma, x_lcu, y_lcu);
|
2014-06-04 14:45:46 +00:00
|
|
|
|
|
|
|
// If SAO is merged, nothing else needs to be coded.
|
|
|
|
if (!sao_luma->merge_left_flag && !sao_luma->merge_up_flag) {
|
2015-03-04 15:00:23 +00:00
|
|
|
encode_sao_color(state, sao_luma, COLOR_Y);
|
2016-08-25 13:05:46 +00:00
|
|
|
if (state->encoder_control->chroma_format != KVZ_CSP_400) {
|
|
|
|
encode_sao_color(state, sao_chroma, COLOR_U);
|
|
|
|
encode_sao_color(state, sao_chroma, COLOR_V);
|
|
|
|
}
|
2014-06-04 14:45:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-27 11:39:37 +00:00
|
|
|
/**
|
|
|
|
* \brief Sets the QP for each CU in state->tile->frame->cu_array.
|
|
|
|
*
|
2017-05-08 07:54:06 +00:00
|
|
|
* The QPs are used in deblocking and QP prediction.
|
2016-09-27 11:39:37 +00:00
|
|
|
*
|
2017-05-08 07:54:06 +00:00
|
|
|
* The QP delta for a quantization group is coded when the first CU with
|
|
|
|
* coded block flag set is encountered. Hence, for the purposes of
|
|
|
|
* deblocking and QP prediction, all CUs in before the first one that has
|
|
|
|
* cbf set use the QP predictor and all CUs after that use (QP predictor
|
|
|
|
* + QP delta).
|
2016-09-27 11:39:37 +00:00
|
|
|
*
|
|
|
|
* \param state encoder state
|
|
|
|
* \param x x-coordinate of the left edge of the root CU
|
|
|
|
* \param y y-coordinate of the top edge of the root CU
|
|
|
|
* \param depth depth in the CU quadtree
|
2017-05-08 07:54:06 +00:00
|
|
|
* \param last_qp QP of the last CU in the last quantization group
|
|
|
|
* \param prev_qp -1 if QP delta has not been coded in current QG,
|
|
|
|
* otherwise the QP of the current QG
|
2016-09-27 11:39:37 +00:00
|
|
|
*/
|
2017-05-08 07:54:06 +00:00
|
|
|
static void set_cu_qps(encoder_state_t *state, int x, int y, int depth, int *last_qp, int *prev_qp)
|
2016-09-27 11:39:37 +00:00
|
|
|
{
|
2017-05-08 07:54:06 +00:00
|
|
|
|
|
|
|
// Stop recursion if the CU is completely outside the frame.
|
|
|
|
if (x >= state->tile->frame->width || y >= state->tile->frame->height) return;
|
2016-09-27 11:39:37 +00:00
|
|
|
|
|
|
|
cu_info_t *cu = kvz_cu_array_at(state->tile->frame->cu_array, x, y);
|
|
|
|
const int cu_width = LCU_WIDTH >> depth;
|
|
|
|
|
2017-05-08 07:54:06 +00:00
|
|
|
if (depth <= state->encoder_control->max_qp_delta_depth) {
|
|
|
|
*prev_qp = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cu->depth > depth) {
|
2016-09-27 11:39:37 +00:00
|
|
|
// Recursively process sub-CUs.
|
|
|
|
const int d = cu_width >> 1;
|
2017-05-08 07:54:06 +00:00
|
|
|
set_cu_qps(state, x, y, depth + 1, last_qp, prev_qp);
|
|
|
|
set_cu_qps(state, x + d, y, depth + 1, last_qp, prev_qp);
|
|
|
|
set_cu_qps(state, x, y + d, depth + 1, last_qp, prev_qp);
|
|
|
|
set_cu_qps(state, x + d, y + d, depth + 1, last_qp, prev_qp);
|
2016-09-27 11:39:37 +00:00
|
|
|
|
|
|
|
} else {
|
2017-05-08 07:54:06 +00:00
|
|
|
bool cbf_found = *prev_qp >= 0;
|
|
|
|
|
|
|
|
if (cu->tr_depth > depth) {
|
2016-09-27 11:39:37 +00:00
|
|
|
// The CU is split into smaller transform units. Check whether coded
|
|
|
|
// block flag is set for any of the TUs.
|
|
|
|
const int tu_width = LCU_WIDTH >> cu->tr_depth;
|
2017-05-08 07:54:06 +00:00
|
|
|
for (int y_scu = y; !cbf_found && y_scu < y + cu_width; y_scu += tu_width) {
|
|
|
|
for (int x_scu = x; !cbf_found && x_scu < x + cu_width; x_scu += tu_width) {
|
2016-09-27 11:39:37 +00:00
|
|
|
cu_info_t *tu = kvz_cu_array_at(state->tile->frame->cu_array, x_scu, y_scu);
|
|
|
|
if (cbf_is_set_any(tu->cbf, cu->depth)) {
|
2017-05-08 07:54:06 +00:00
|
|
|
cbf_found = true;
|
2016-09-27 11:39:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-08 07:54:06 +00:00
|
|
|
} else if (cbf_is_set_any(cu->cbf, cu->depth)) {
|
|
|
|
cbf_found = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int8_t qp;
|
|
|
|
if (cbf_found) {
|
|
|
|
*prev_qp = qp = cu->qp;
|
|
|
|
} else {
|
|
|
|
qp = kvz_get_cu_ref_qp(state, x, y, *last_qp);
|
2016-09-27 11:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the correct QP for all state->tile->frame->cu_array elements in
|
|
|
|
// the area covered by the CU.
|
|
|
|
for (int y_scu = y; y_scu < y + cu_width; y_scu += SCU_WIDTH) {
|
|
|
|
for (int x_scu = x; x_scu < x + cu_width; x_scu += SCU_WIDTH) {
|
|
|
|
kvz_cu_array_at(state->tile->frame->cu_array, x_scu, y_scu)->qp = qp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-08 07:54:06 +00:00
|
|
|
if (is_last_cu_in_qg(state, x, y, depth)) {
|
|
|
|
*last_qp = cu->qp;
|
|
|
|
}
|
|
|
|
}
|
2016-09-27 11:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void encoder_state_worker_encode_lcu(void * opaque)
|
|
|
|
{
|
2015-03-04 11:49:59 +00:00
|
|
|
const lcu_order_element_t * const lcu = opaque;
|
2015-03-04 15:00:23 +00:00
|
|
|
encoder_state_t *state = lcu->encoder_state;
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
videoframe_t* const frame = state->tile->frame;
|
2019-08-15 08:01:38 +00:00
|
|
|
encoder_state_config_slice_t *slice = state->slice;
|
2016-08-21 04:16:59 +00:00
|
|
|
|
2019-11-19 11:45:58 +00:00
|
|
|
switch (encoder->cfg.rc_algorithm) {
|
|
|
|
case KVZ_NO_RC:
|
|
|
|
case KVZ_LAMBDA:
|
|
|
|
kvz_set_lcu_lambda_and_qp(state, lcu->position);
|
|
|
|
break;
|
|
|
|
case KVZ_OBA:
|
|
|
|
kvz_set_ctu_qp_lambda(state, lcu->position);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2016-08-21 04:16:59 +00:00
|
|
|
|
2017-05-11 11:51:09 +00:00
|
|
|
lcu_coeff_t coeff;
|
|
|
|
state->coeff = &coeff;
|
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//This part doesn't write to bitstream, it's only search, deblock and sao
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_search_lcu(state, lcu->position_px.x, lcu->position_px.y, state->tile->hor_buf_search, state->tile->ver_buf_search);
|
2017-06-30 13:12:52 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
encoder_state_recdata_to_bufs(state, lcu, state->tile->hor_buf_search, state->tile->ver_buf_search);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2017-05-08 07:54:06 +00:00
|
|
|
if (encoder->max_qp_delta_depth >= 0) {
|
|
|
|
int last_qp = state->last_qp;
|
|
|
|
int prev_qp = -1;
|
|
|
|
set_cu_qps(state, lcu->position_px.x, lcu->position_px.y, 0, &last_qp, &prev_qp);
|
|
|
|
}
|
2016-09-27 11:39:37 +00:00
|
|
|
|
2017-05-08 07:54:06 +00:00
|
|
|
if (encoder->cfg.deblock_enable) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_filter_deblock_lcu(state, lcu->position_px.x, lcu->position_px.y);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2017-08-11 08:57:09 +00:00
|
|
|
if (encoder->cfg.sao_type) {
|
2017-06-30 13:12:52 +00:00
|
|
|
// Save the post-deblocking but pre-SAO pixels of the LCU to a buffer
|
|
|
|
// so that they can be used in SAO reconstruction later.
|
|
|
|
encoder_state_recdata_before_sao_to_bufs(state,
|
|
|
|
lcu,
|
|
|
|
state->tile->hor_buf_before_sao,
|
|
|
|
state->tile->ver_buf_before_sao);
|
2016-03-01 10:59:27 +00:00
|
|
|
kvz_sao_search_lcu(state, lcu->position.x, lcu->position.y);
|
2017-06-30 13:12:52 +00:00
|
|
|
encoder_sao_reconstruct(state, lcu);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
2016-03-01 10:59:27 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//Now write data to bitstream (required to have a correct CABAC state)
|
2016-08-24 01:16:48 +00:00
|
|
|
const uint64_t existing_bits = kvz_bitstream_tell(&state->stream);
|
2017-07-27 13:37:02 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//Encode SAO
|
2017-08-11 08:57:09 +00:00
|
|
|
if (encoder->cfg.sao_type) {
|
2015-03-04 15:00:23 +00:00
|
|
|
encode_sao(state, lcu->position.x, lcu->position.y, &frame->sao_luma[lcu->position.y * frame->width_in_lcu + lcu->position.x], &frame->sao_chroma[lcu->position.y * frame->width_in_lcu + lcu->position.x]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2016-08-21 04:36:13 +00:00
|
|
|
|
2020-09-24 14:07:37 +00:00
|
|
|
/*
|
2019-08-15 08:01:38 +00:00
|
|
|
//Tests
|
2019-10-02 09:25:02 +00:00
|
|
|
//alf_info_t *alf = &frame->alf_info[lcu->position.y * frame->rec->stride + lcu->position.x];
|
|
|
|
//kvz_alf_enc_process(state, lcu);
|
|
|
|
if (encoder->cfg.alf_enable) {
|
2020-09-01 12:17:47 +00:00
|
|
|
#if RUN_ALF_AFTER_FULL_FRAME
|
|
|
|
if (lcu->last_column && lcu->last_row)
|
|
|
|
{
|
|
|
|
int total_num_lcus = frame->height_in_lcu * frame->width_in_lcu;
|
|
|
|
for (int lcu_idx = 0; lcu_idx < total_num_lcus; lcu_idx++)
|
|
|
|
{
|
|
|
|
lcu_order_element_t *cur_lcu = &state->lcu_order[lcu_idx];
|
|
|
|
kvz_alf_enc_create(state, cur_lcu);
|
|
|
|
kvz_alf_init(state, slice);
|
|
|
|
kvz_alf_enc_process(state, cur_lcu);
|
|
|
|
kvz_frame_end(state, frame, cur_lcu);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2019-10-02 09:25:02 +00:00
|
|
|
kvz_alf_enc_create(state, lcu);
|
|
|
|
kvz_alf_init(state, slice);
|
|
|
|
kvz_alf_enc_process(state, lcu);
|
2019-11-20 13:24:44 +00:00
|
|
|
kvz_frame_end(state, frame, lcu);
|
2020-09-01 12:17:47 +00:00
|
|
|
#endif
|
|
|
|
//Moved to videoframe.c
|
|
|
|
//kvz_alf_enc_destroy(frame);
|
2020-09-24 14:07:37 +00:00
|
|
|
}*/
|
2019-08-15 08:01:38 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//Encode coding tree
|
2017-05-23 11:59:42 +00:00
|
|
|
kvz_encode_coding_tree(state, lcu->position.x * LCU_WIDTH, lcu->position.y * LCU_WIDTH, 0);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2017-05-11 11:51:09 +00:00
|
|
|
// Coeffs are not needed anymore.
|
|
|
|
state->coeff = NULL;
|
|
|
|
|
2017-01-31 10:56:04 +00:00
|
|
|
bool end_of_slice_segment_flag;
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.slices & KVZ_SLICES_WPP) {
|
2017-01-31 13:44:23 +00:00
|
|
|
// Slice segments end after each WPP row.
|
|
|
|
end_of_slice_segment_flag = lcu->last_column;
|
2017-02-05 09:59:21 +00:00
|
|
|
} else if (state->encoder_control->cfg.slices & KVZ_SLICES_TILES) {
|
2017-01-31 13:44:23 +00:00
|
|
|
// Slices end after each tile.
|
|
|
|
end_of_slice_segment_flag = lcu->last_column && lcu->last_row;
|
|
|
|
} else {
|
2017-01-31 10:56:04 +00:00
|
|
|
// Slice ends after the last row of the last tile.
|
2017-02-06 11:00:25 +00:00
|
|
|
int last_tile_id = -1 + encoder->cfg.tiles_width_count * encoder->cfg.tiles_height_count;
|
2017-01-31 10:56:04 +00:00
|
|
|
bool is_last_tile = state->tile->id == last_tile_id;
|
|
|
|
end_of_slice_segment_flag = is_last_tile && lcu->last_column && lcu->last_row;
|
|
|
|
}
|
2020-04-07 06:57:30 +00:00
|
|
|
//kvz_cabac_encode_bin_trm(&state->cabac, end_of_slice_segment_flag);
|
2017-01-31 10:56:04 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
const bool end_of_tile = lcu->last_column && lcu->last_row;
|
2017-02-05 09:59:21 +00:00
|
|
|
const bool end_of_wpp_row = encoder->cfg.wpp && lcu->last_column;
|
2017-01-31 10:56:04 +00:00
|
|
|
|
|
|
|
if (end_of_tile || end_of_wpp_row) {
|
2020-04-07 12:30:02 +00:00
|
|
|
// end_of_sub_stream_one_bit
|
|
|
|
kvz_cabac_encode_bin_trm(&state->cabac, 1);
|
2017-01-31 10:56:04 +00:00
|
|
|
|
|
|
|
// Finish the substream by writing out remaining state.
|
|
|
|
kvz_cabac_finish(&state->cabac);
|
|
|
|
|
|
|
|
// Write a rbsp_trailing_bits or a byte_alignment. The first one is used
|
|
|
|
// for ending a slice_segment_layer_rbsp and the second one for ending
|
|
|
|
// a substream. They are identical and align the byte stream.
|
|
|
|
kvz_bitstream_put(state->cabac.stream, 1, 1);
|
|
|
|
kvz_bitstream_align_zero(state->cabac.stream);
|
|
|
|
|
|
|
|
kvz_cabac_start(&state->cabac);
|
2017-05-19 07:44:37 +00:00
|
|
|
|
|
|
|
kvz_crypto_delete(&state->crypto_hdl);
|
2017-01-31 10:56:04 +00:00
|
|
|
}
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2016-08-24 01:16:48 +00:00
|
|
|
|
2019-11-12 10:12:57 +00:00
|
|
|
pthread_mutex_lock(&state->frame->rc_lock);
|
2016-08-24 01:16:48 +00:00
|
|
|
const uint32_t bits = kvz_bitstream_tell(&state->stream) - existing_bits;
|
2019-09-26 10:09:28 +00:00
|
|
|
state->frame->cur_frame_bits_coded += bits;
|
2019-11-12 10:12:57 +00:00
|
|
|
// This variable is used differently by intra and inter frames and shouldn't
|
|
|
|
// be touched in intra frames here
|
|
|
|
state->frame->remaining_weight -= !state->frame->is_irap ?
|
2019-11-13 06:55:31 +00:00
|
|
|
kvz_get_lcu_stats(state, lcu->position.x, lcu->position.y)->original_weight :
|
2019-11-12 10:12:57 +00:00
|
|
|
0;
|
|
|
|
pthread_mutex_unlock(&state->frame->rc_lock);
|
2016-08-24 01:16:48 +00:00
|
|
|
kvz_get_lcu_stats(state, lcu->position.x, lcu->position.y)->bits = bits;
|
|
|
|
|
2019-10-10 13:35:56 +00:00
|
|
|
uint8_t not_skip = false;
|
|
|
|
for(int y = 0; y < 64 && !not_skip; y+=8) {
|
|
|
|
for(int x = 0; x < 64 && !not_skip; x+=8) {
|
|
|
|
not_skip |= !kvz_cu_array_at_const(state->tile->frame->cu_array,
|
2019-09-26 10:09:28 +00:00
|
|
|
lcu->position_px.x + x,
|
|
|
|
lcu->position_px.y + y)->skipped;
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 13:35:56 +00:00
|
|
|
kvz_get_lcu_stats(state, lcu->position.x, lcu->position.y)->skipped = !not_skip;
|
2016-08-24 01:16:48 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//Wavefronts need the context to be copied to the next row
|
2020-12-15 12:51:46 +00:00
|
|
|
if (state->type == ENCODER_STATE_TYPE_WAVEFRONT_ROW && lcu->index == 0) {
|
2014-06-03 12:25:16 +00:00
|
|
|
int j;
|
|
|
|
//Find next encoder (next row)
|
2015-03-04 15:00:23 +00:00
|
|
|
for (j=0; state->parent->children[j].encoder_control; ++j) {
|
|
|
|
if (state->parent->children[j].wfrow->lcu_offset_y == state->wfrow->lcu_offset_y + 1) {
|
2014-06-03 12:25:16 +00:00
|
|
|
//And copy context
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_context_copy(&state->parent->children[j], state);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 14:07:37 +00:00
|
|
|
static void encoder_state_worker_encode_lcu_search(void * opaque)
|
|
|
|
{
|
|
|
|
lcu_order_element_t * const lcu = opaque;
|
|
|
|
encoder_state_t *state = lcu->encoder_state;
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
videoframe_t* const frame = state->tile->frame;
|
|
|
|
encoder_state_config_slice_t *slice = state->slice;
|
|
|
|
|
|
|
|
kvz_set_lcu_lambda_and_qp(state, lcu->position);
|
|
|
|
|
|
|
|
lcu->coeff = calloc(1, sizeof(lcu_coeff_t));
|
|
|
|
state->coeff = lcu->coeff;
|
|
|
|
|
|
|
|
//This part doesn't write to bitstream, it's only search, deblock and sao
|
|
|
|
kvz_search_lcu(state, lcu->position_px.x, lcu->position_px.y, state->tile->hor_buf_search, state->tile->ver_buf_search);
|
|
|
|
|
|
|
|
encoder_state_recdata_to_bufs(state, lcu, state->tile->hor_buf_search, state->tile->ver_buf_search);
|
|
|
|
|
|
|
|
if (encoder->max_qp_delta_depth >= 0) {
|
|
|
|
int last_qp = state->last_qp;
|
|
|
|
int prev_qp = -1;
|
|
|
|
set_cu_qps(state, lcu->position_px.x, lcu->position_px.y, 0, &last_qp, &prev_qp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->cfg.deblock_enable) {
|
|
|
|
kvz_filter_deblock_lcu(state, lcu->position_px.x, lcu->position_px.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->cfg.sao_type) {
|
|
|
|
// Save the post-deblocking but pre-SAO pixels of the LCU to a buffer
|
|
|
|
// so that they can be used in SAO reconstruction later.
|
|
|
|
encoder_state_recdata_before_sao_to_bufs(state,
|
|
|
|
lcu,
|
|
|
|
state->tile->hor_buf_before_sao,
|
|
|
|
state->tile->ver_buf_before_sao);
|
|
|
|
kvz_sao_search_lcu(state, lcu->position.x, lcu->position.y);
|
|
|
|
encoder_sao_reconstruct(state, lcu);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encoder_state_worker_encode_lcu_bitstream(void * opaque)
|
|
|
|
{
|
|
|
|
lcu_order_element_t * const lcu = opaque;
|
|
|
|
encoder_state_t *state = lcu->encoder_state;
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
|
|
|
videoframe_t* const frame = state->tile->frame;
|
|
|
|
encoder_state_config_slice_t *slice = state->slice;
|
|
|
|
|
|
|
|
kvz_set_lcu_lambda_and_qp(state, lcu->position);
|
|
|
|
|
|
|
|
state->coeff = lcu->coeff;
|
|
|
|
|
|
|
|
//Now write data to bitstream (required to have a correct CABAC state)
|
|
|
|
const uint64_t existing_bits = kvz_bitstream_tell(&state->stream);
|
|
|
|
|
|
|
|
//Encode SAO
|
|
|
|
if (encoder->cfg.sao_type) {
|
|
|
|
encode_sao(state, lcu->position.x, lcu->position.y, &frame->sao_luma[lcu->position.y * frame->width_in_lcu + lcu->position.x], &frame->sao_chroma[lcu->position.y * frame->width_in_lcu + lcu->position.x]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Encode ALF
|
|
|
|
//TODO
|
|
|
|
|
|
|
|
//Encode coding tree
|
|
|
|
kvz_encode_coding_tree(state, lcu->position.x * LCU_WIDTH, lcu->position.y * LCU_WIDTH, 0);
|
|
|
|
|
|
|
|
// Coeffs are not needed anymore.
|
|
|
|
free(lcu->coeff);
|
|
|
|
lcu->coeff = NULL;
|
|
|
|
state->coeff = NULL;
|
|
|
|
|
|
|
|
bool end_of_slice_segment_flag;
|
|
|
|
if (state->encoder_control->cfg.slices & KVZ_SLICES_WPP) {
|
|
|
|
// Slice segments end after each WPP row.
|
|
|
|
end_of_slice_segment_flag = lcu->last_column;
|
|
|
|
}
|
|
|
|
else if (state->encoder_control->cfg.slices & KVZ_SLICES_TILES) {
|
|
|
|
// Slices end after each tile.
|
|
|
|
end_of_slice_segment_flag = lcu->last_column && lcu->last_row;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Slice ends after the last row of the last tile.
|
|
|
|
int last_tile_id = -1 + encoder->cfg.tiles_width_count * encoder->cfg.tiles_height_count;
|
|
|
|
bool is_last_tile = state->tile->id == last_tile_id;
|
|
|
|
end_of_slice_segment_flag = is_last_tile && lcu->last_column && lcu->last_row;
|
|
|
|
}
|
|
|
|
//kvz_cabac_encode_bin_trm(&state->cabac, end_of_slice_segment_flag);
|
|
|
|
|
|
|
|
{
|
|
|
|
const bool end_of_tile = lcu->last_column && lcu->last_row;
|
|
|
|
const bool end_of_wpp_row = encoder->cfg.wpp && lcu->last_column;
|
|
|
|
|
|
|
|
|
|
|
|
if (end_of_tile || end_of_wpp_row) {
|
|
|
|
// end_of_sub_stream_one_bit
|
|
|
|
kvz_cabac_encode_bin_trm(&state->cabac, 1);
|
|
|
|
|
|
|
|
// Finish the substream by writing out remaining state.
|
|
|
|
kvz_cabac_finish(&state->cabac);
|
|
|
|
|
|
|
|
// Write a rbsp_trailing_bits or a byte_alignment. The first one is used
|
|
|
|
// for ending a slice_segment_layer_rbsp and the second one for ending
|
|
|
|
// a substream. They are identical and align the byte stream.
|
|
|
|
kvz_bitstream_put(state->cabac.stream, 1, 1);
|
|
|
|
kvz_bitstream_align_zero(state->cabac.stream);
|
|
|
|
|
|
|
|
kvz_cabac_start(&state->cabac);
|
|
|
|
|
|
|
|
kvz_crypto_delete(&state->crypto_hdl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t bits = kvz_bitstream_tell(&state->stream) - existing_bits;
|
|
|
|
kvz_get_lcu_stats(state, lcu->position.x, lcu->position.y)->bits = bits;
|
|
|
|
|
|
|
|
//Wavefronts need the context to be copied to the next row
|
|
|
|
if (state->type == ENCODER_STATE_TYPE_WAVEFRONT_ROW && lcu->index == 1) {
|
|
|
|
int j;
|
|
|
|
//Find next encoder (next row)
|
|
|
|
for (j = 0; state->parent->children[j].encoder_control; ++j) {
|
|
|
|
if (state->parent->children[j].wfrow->lcu_offset_y == state->wfrow->lcu_offset_y + 1) {
|
|
|
|
//And copy context
|
|
|
|
kvz_context_copy(&state->parent->children[j], state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 07:44:37 +00:00
|
|
|
static void encoder_state_encode_leaf(encoder_state_t * const state)
|
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
assert(state->is_leaf);
|
|
|
|
assert(state->lcu_order_count > 0);
|
2016-02-05 15:08:01 +00:00
|
|
|
|
2017-06-26 08:44:17 +00:00
|
|
|
const encoder_control_t *ctrl = state->encoder_control;
|
|
|
|
const kvz_config *cfg = &ctrl->cfg;
|
2016-08-21 04:36:13 +00:00
|
|
|
|
2019-05-22 14:26:10 +00:00
|
|
|
// Signaled slice QP may be different to frame QP with set-qp-in-cu enabled.
|
|
|
|
state->last_qp = ctrl->cfg.set_qp_in_cu ? 26 : state->frame->QP;
|
2016-08-21 04:36:13 +00:00
|
|
|
|
2017-05-19 07:44:37 +00:00
|
|
|
if (cfg->crypto_features) {
|
2017-08-31 09:51:41 +00:00
|
|
|
state->crypto_hdl = kvz_crypto_create(cfg);
|
2017-05-19 07:44:37 +00:00
|
|
|
state->crypto_prev_pos = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:56:15 +00:00
|
|
|
// Select whether to encode the frame/tile in current thread or to define
|
|
|
|
// wavefront jobs for other threads to handle.
|
|
|
|
bool wavefront = state->type == ENCODER_STATE_TYPE_WAVEFRONT_ROW;
|
|
|
|
bool use_parallel_encoding = (wavefront && state->parent->children[1].encoder_control);
|
|
|
|
if (!use_parallel_encoding) {
|
|
|
|
// Encode every LCU in order and perform SAO reconstruction after every
|
|
|
|
// frame is encoded. Deblocking and SAO search is done during LCU encoding.
|
2020-09-24 14:07:37 +00:00
|
|
|
#if 0
|
2015-03-11 13:56:15 +00:00
|
|
|
for (int i = 0; i < state->lcu_order_count; ++i) {
|
2015-03-04 15:00:23 +00:00
|
|
|
encoder_state_worker_encode_lcu(&state->lcu_order[i]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2020-09-24 14:07:37 +00:00
|
|
|
#else
|
|
|
|
for (int i = 0; i < state->lcu_order_count; ++i) {
|
|
|
|
encoder_state_worker_encode_lcu_search(&state->lcu_order[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Encode ALF
|
|
|
|
|
|
|
|
for (int i = 0; i < state->lcu_order_count; ++i) {
|
|
|
|
encoder_state_worker_encode_lcu_bitstream(&state->lcu_order[i]);
|
|
|
|
}
|
|
|
|
#endif // ORIG
|
2014-06-03 12:25:16 +00:00
|
|
|
} else {
|
2015-03-17 09:13:48 +00:00
|
|
|
// Add each LCU in the wavefront row as it's own job to the queue.
|
2015-03-11 13:56:15 +00:00
|
|
|
|
2016-02-05 15:08:01 +00:00
|
|
|
// Select which frame dependancies should be set to.
|
|
|
|
const encoder_state_t * ref_state = NULL;
|
2017-07-19 08:59:27 +00:00
|
|
|
|
|
|
|
if (state->frame->slicetype == KVZ_SLICE_I) {
|
|
|
|
// I-frames have no references.
|
|
|
|
ref_state = NULL;
|
|
|
|
} else if (cfg->gop_lowdelay &&
|
|
|
|
cfg->gop_len > 0 &&
|
|
|
|
state->previous_encoder_state != state)
|
2016-02-05 15:08:01 +00:00
|
|
|
{
|
|
|
|
// For LP-gop, depend on the state of the first reference.
|
2017-09-19 06:20:10 +00:00
|
|
|
int ref_neg = cfg->gop[state->frame->gop_offset].ref_neg[0];
|
2017-06-26 08:44:17 +00:00
|
|
|
if (ref_neg > cfg->owf) {
|
2016-02-05 15:08:01 +00:00
|
|
|
// If frame is not within OWF range, it's already done.
|
|
|
|
ref_state = NULL;
|
|
|
|
} else {
|
|
|
|
ref_state = state->previous_encoder_state;
|
|
|
|
while (ref_neg > 1) {
|
|
|
|
ref_neg -= 1;
|
|
|
|
ref_state = ref_state->previous_encoder_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, depend on the previous frame.
|
|
|
|
ref_state = state->previous_encoder_state;
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:56:15 +00:00
|
|
|
for (int i = 0; i < state->lcu_order_count; ++i) {
|
2015-03-04 15:00:23 +00:00
|
|
|
const lcu_order_element_t * const lcu = &state->lcu_order[i];
|
2016-02-05 15:08:01 +00:00
|
|
|
|
2017-04-04 12:36:08 +00:00
|
|
|
kvz_threadqueue_free_job(&state->tile->wf_jobs[lcu->id]);
|
2017-07-20 12:23:18 +00:00
|
|
|
state->tile->wf_jobs[lcu->id] = kvz_threadqueue_job_create(encoder_state_worker_encode_lcu, (void*)lcu);
|
2017-06-20 13:31:04 +00:00
|
|
|
threadqueue_job_t **job = &state->tile->wf_jobs[lcu->id];
|
|
|
|
|
2015-03-13 15:15:05 +00:00
|
|
|
// If job object was returned, add dependancies and allow it to run.
|
2017-06-20 13:31:04 +00:00
|
|
|
if (job[0]) {
|
2015-03-17 09:13:48 +00:00
|
|
|
// Add inter frame dependancies when ecoding more than one frame at
|
|
|
|
// once. The added dependancy is for the first LCU of each wavefront
|
|
|
|
// row to depend on the reconstruction status of the row below in the
|
|
|
|
// previous frame.
|
2016-02-05 15:08:01 +00:00
|
|
|
if (ref_state != NULL &&
|
|
|
|
state->previous_encoder_state->tqj_recon_done &&
|
2016-08-10 00:46:23 +00:00
|
|
|
state->frame->slicetype != KVZ_SLICE_I)
|
2016-02-05 15:08:01 +00:00
|
|
|
{
|
2017-06-20 13:31:04 +00:00
|
|
|
// We need to wait until the CTUs whose pixels we refer to are
|
|
|
|
// done before we can start this CTU.
|
2017-06-26 08:44:17 +00:00
|
|
|
const lcu_order_element_t *dep_lcu = lcu;
|
|
|
|
for (int i = 0; dep_lcu->below && i < ctrl->max_inter_ref_lcu.down; i++) {
|
|
|
|
dep_lcu = dep_lcu->below;
|
2014-06-16 08:18:48 +00:00
|
|
|
}
|
2017-06-26 08:44:17 +00:00
|
|
|
for (int i = 0; dep_lcu->right && i < ctrl->max_inter_ref_lcu.right; i++) {
|
|
|
|
dep_lcu = dep_lcu->right;
|
|
|
|
}
|
|
|
|
kvz_threadqueue_job_dep_add(job[0], ref_state->tile->wf_jobs[dep_lcu->id]);
|
2019-06-12 11:04:41 +00:00
|
|
|
|
2019-11-13 09:56:25 +00:00
|
|
|
//TODO: Preparation for the lock free implementation of the new rc
|
|
|
|
if (ref_state->frame->slicetype == KVZ_SLICE_I && ref_state->frame->num != 0 && state->encoder_control->cfg.owf > 1 && true) {
|
|
|
|
kvz_threadqueue_job_dep_add(job[0], ref_state->previous_encoder_state->tile->wf_jobs[dep_lcu->id]);
|
|
|
|
}
|
|
|
|
|
2019-06-12 11:04:41 +00:00
|
|
|
// Very spesific bug that happens when owf length is longer than the
|
|
|
|
// gop length. Takes care of that.
|
2019-06-12 11:08:18 +00:00
|
|
|
if(!state->encoder_control->cfg.gop_lowdelay &&
|
2019-06-12 11:35:24 +00:00
|
|
|
state->encoder_control->cfg.open_gop &&
|
2019-07-05 09:57:27 +00:00
|
|
|
state->encoder_control->cfg.gop_len != 0 &&
|
2019-06-12 11:04:41 +00:00
|
|
|
state->encoder_control->cfg.owf > state->encoder_control->cfg.gop_len &&
|
|
|
|
ref_state->frame->slicetype == KVZ_SLICE_I &&
|
|
|
|
ref_state->frame->num != 0){
|
|
|
|
|
2019-06-12 11:37:41 +00:00
|
|
|
while (ref_state->frame->poc != state->frame->poc - state->encoder_control->cfg.gop_len){
|
2019-06-12 11:04:41 +00:00
|
|
|
ref_state = ref_state->previous_encoder_state;
|
|
|
|
}
|
|
|
|
kvz_threadqueue_job_dep_add(job[0], ref_state->tile->wf_jobs[dep_lcu->id]);
|
|
|
|
}
|
2014-06-16 08:18:48 +00:00
|
|
|
}
|
2015-03-13 15:15:05 +00:00
|
|
|
|
|
|
|
// Add local WPP dependancy to the LCU on the left.
|
|
|
|
if (lcu->left) {
|
2017-06-20 13:31:04 +00:00
|
|
|
kvz_threadqueue_job_dep_add(job[0], job[-1]);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
2020-12-15 20:33:32 +00:00
|
|
|
// Add local WPP dependancy to the LCU on the top.
|
2015-03-13 15:15:05 +00:00
|
|
|
if (lcu->above) {
|
2020-12-15 20:33:32 +00:00
|
|
|
kvz_threadqueue_job_dep_add(job[0], job[-state->tile->frame->width_in_lcu]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2015-03-13 15:15:05 +00:00
|
|
|
|
2017-07-20 12:23:18 +00:00
|
|
|
kvz_threadqueue_submit(state->encoder_control->threadqueue, state->tile->wf_jobs[lcu->id]);
|
2015-03-17 09:22:50 +00:00
|
|
|
|
2017-06-30 13:12:52 +00:00
|
|
|
// The wavefront row is done when the last LCU in the row is done.
|
|
|
|
if (i + 1 == state->lcu_order_count) {
|
2017-04-04 12:36:08 +00:00
|
|
|
assert(!state->tqj_recon_done);
|
|
|
|
state->tqj_recon_done =
|
|
|
|
kvz_threadqueue_copy_ref(state->tile->wf_jobs[lcu->id]);
|
|
|
|
}
|
2015-05-11 08:48:58 +00:00
|
|
|
}
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-03-04 10:25:42 +00:00
|
|
|
static void encoder_state_encode(encoder_state_t * const main_state);
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2017-01-31 10:56:04 +00:00
|
|
|
static void encoder_state_worker_encode_children(void * opaque)
|
|
|
|
{
|
2015-03-04 10:25:42 +00:00
|
|
|
encoder_state_t *sub_state = opaque;
|
2014-06-03 12:25:16 +00:00
|
|
|
encoder_state_encode(sub_state);
|
2017-01-31 10:56:04 +00:00
|
|
|
|
2017-01-31 13:44:23 +00:00
|
|
|
if (sub_state->is_leaf && sub_state->type == ENCODER_STATE_TYPE_WAVEFRONT_ROW) {
|
2017-01-31 10:56:04 +00:00
|
|
|
// Set the last wavefront job of this row as the job that completes
|
|
|
|
// the bitstream for this wavefront row state.
|
|
|
|
|
|
|
|
int wpp_row = sub_state->wfrow->lcu_offset_y;
|
|
|
|
int tile_width = sub_state->tile->frame->width_in_lcu;
|
|
|
|
int end_of_row = (wpp_row + 1) * tile_width - 1;
|
|
|
|
assert(!sub_state->tqj_bitstream_written);
|
2017-04-04 12:36:08 +00:00
|
|
|
if (sub_state->tile->wf_jobs[end_of_row]) {
|
|
|
|
sub_state->tqj_bitstream_written =
|
|
|
|
kvz_threadqueue_copy_ref(sub_state->tile->wf_jobs[end_of_row]);
|
|
|
|
}
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static int encoder_state_tree_is_a_chain(const encoder_state_t * const state) {
|
|
|
|
if (!state->children[0].encoder_control) return 1;
|
|
|
|
if (state->children[1].encoder_control) return 0;
|
|
|
|
return encoder_state_tree_is_a_chain(&state->children[0]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:25:42 +00:00
|
|
|
static void encoder_state_encode(encoder_state_t * const main_state) {
|
2014-06-03 12:25:16 +00:00
|
|
|
//If we have children, encode at child level
|
|
|
|
if (main_state->children[0].encoder_control) {
|
|
|
|
//If we have only one child, than it cannot be the last split in tree
|
|
|
|
int node_is_the_last_split_in_tree = (main_state->children[1].encoder_control != 0);
|
2017-07-27 13:37:02 +00:00
|
|
|
|
|
|
|
for (int i = 0; main_state->children[i].encoder_control; ++i) {
|
2015-03-04 10:25:42 +00:00
|
|
|
encoder_state_t *sub_state = &(main_state->children[i]);
|
2017-07-27 13:37:02 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
if (sub_state->tile != main_state->tile) {
|
2017-05-31 10:17:59 +00:00
|
|
|
const int offset_x = sub_state->tile->offset_x;
|
|
|
|
const int offset_y = sub_state->tile->offset_y;
|
2014-06-12 05:13:37 +00:00
|
|
|
const int width = MIN(sub_state->tile->frame->width_in_lcu * LCU_WIDTH, main_state->tile->frame->width - offset_x);
|
|
|
|
const int height = MIN(sub_state->tile->frame->height_in_lcu * LCU_WIDTH, main_state->tile->frame->height - offset_y);
|
2017-07-27 13:37:02 +00:00
|
|
|
|
|
|
|
kvz_image_free(sub_state->tile->frame->source);
|
|
|
|
sub_state->tile->frame->source = NULL;
|
|
|
|
|
|
|
|
kvz_image_free(sub_state->tile->frame->rec);
|
|
|
|
sub_state->tile->frame->rec = NULL;
|
|
|
|
|
|
|
|
kvz_cu_array_free(&sub_state->tile->frame->cu_array);
|
|
|
|
|
|
|
|
sub_state->tile->frame->source = kvz_image_make_subimage(
|
|
|
|
main_state->tile->frame->source,
|
|
|
|
offset_x,
|
|
|
|
offset_y,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
|
|
|
sub_state->tile->frame->rec = kvz_image_make_subimage(
|
|
|
|
main_state->tile->frame->rec,
|
|
|
|
offset_x,
|
|
|
|
offset_y,
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
);
|
|
|
|
sub_state->tile->frame->cu_array = kvz_cu_subarray(
|
|
|
|
main_state->tile->frame->cu_array,
|
|
|
|
offset_x,
|
|
|
|
offset_y,
|
|
|
|
sub_state->tile->frame->width_in_lcu * LCU_WIDTH,
|
|
|
|
sub_state->tile->frame->height_in_lcu * LCU_WIDTH
|
|
|
|
);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2017-07-27 13:37:02 +00:00
|
|
|
|
2014-06-03 12:25:16 +00:00
|
|
|
//To be the last split, we require that every child is a chain
|
2017-07-27 13:37:02 +00:00
|
|
|
node_is_the_last_split_in_tree =
|
|
|
|
node_is_the_last_split_in_tree &&
|
|
|
|
encoder_state_tree_is_a_chain(&main_state->children[i]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
|
|
|
//If it's the latest split point
|
|
|
|
if (node_is_the_last_split_in_tree) {
|
2017-07-27 13:37:02 +00:00
|
|
|
for (int i = 0; main_state->children[i].encoder_control; ++i) {
|
2014-06-03 12:25:16 +00:00
|
|
|
//If we don't have wavefronts, parallelize encoding of children.
|
|
|
|
if (main_state->children[i].type != ENCODER_STATE_TYPE_WAVEFRONT_ROW) {
|
2017-04-04 12:36:08 +00:00
|
|
|
kvz_threadqueue_free_job(&main_state->children[i].tqj_recon_done);
|
2017-07-20 12:23:18 +00:00
|
|
|
main_state->children[i].tqj_recon_done =
|
|
|
|
kvz_threadqueue_job_create(encoder_state_worker_encode_children, &main_state->children[i]);
|
|
|
|
if (main_state->children[i].previous_encoder_state != &main_state->children[i] &&
|
|
|
|
main_state->children[i].previous_encoder_state->tqj_recon_done &&
|
2017-09-19 06:20:10 +00:00
|
|
|
!main_state->children[i].frame->is_irap)
|
2017-07-20 12:23:18 +00:00
|
|
|
{
|
2016-02-29 17:59:56 +00:00
|
|
|
#if 0
|
|
|
|
// Disabled due to non-determinism.
|
2016-02-29 16:39:21 +00:00
|
|
|
if (main_state->encoder_control->cfg->mv_constraint == KVZ_MV_CONSTRAIN_FRAME_AND_TILE_MARGIN)
|
2016-02-26 11:30:08 +00:00
|
|
|
{
|
|
|
|
// When MV's don't cross tile boundaries, add dependancy only to the same tile.
|
|
|
|
kvz_threadqueue_job_dep_add(main_state->children[i].tqj_recon_done, main_state->children[i].previous_encoder_state->tqj_recon_done);
|
2016-02-29 17:59:56 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2016-02-26 11:30:08 +00:00
|
|
|
// Add dependancy to each child in the previous frame.
|
|
|
|
for (int child_id = 0; main_state->children[child_id].encoder_control; ++child_id) {
|
|
|
|
kvz_threadqueue_job_dep_add(main_state->children[i].tqj_recon_done, main_state->children[child_id].previous_encoder_state->tqj_recon_done);
|
|
|
|
}
|
2015-05-12 09:00:32 +00:00
|
|
|
}
|
2014-06-16 06:19:42 +00:00
|
|
|
}
|
2017-07-20 12:23:18 +00:00
|
|
|
kvz_threadqueue_submit(main_state->encoder_control->threadqueue, main_state->children[i].tqj_recon_done);
|
2014-06-03 12:25:16 +00:00
|
|
|
} else {
|
|
|
|
//Wavefront rows have parallelism at LCU level, so we should not launch multiple threads here!
|
|
|
|
//FIXME: add an assert: we can only have wavefront children
|
|
|
|
encoder_state_worker_encode_children(&(main_state->children[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-27 13:37:02 +00:00
|
|
|
for (int i = 0; main_state->children[i].encoder_control; ++i) {
|
2014-06-03 12:25:16 +00:00
|
|
|
encoder_state_worker_encode_children(&(main_state->children[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (main_state->type) {
|
|
|
|
case ENCODER_STATE_TYPE_TILE:
|
|
|
|
case ENCODER_STATE_TYPE_SLICE:
|
|
|
|
case ENCODER_STATE_TYPE_WAVEFRONT_ROW:
|
|
|
|
encoder_state_encode_leaf(main_state);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Unsupported leaf type %c!\n", main_state->type);
|
|
|
|
assert(0);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 08:40:21 +00:00
|
|
|
|
2018-01-31 11:50:57 +00:00
|
|
|
static void encoder_ref_insertion_sort(const encoder_state_t *const state,
|
|
|
|
uint8_t reflist[16],
|
|
|
|
uint8_t length,
|
|
|
|
bool reverse)
|
|
|
|
{
|
2015-03-26 08:40:21 +00:00
|
|
|
|
|
|
|
for (uint8_t i = 1; i < length; ++i) {
|
2017-07-07 10:58:19 +00:00
|
|
|
const uint8_t cur_idx = reflist[i];
|
|
|
|
const int32_t cur_poc = state->frame->ref->pocs[cur_idx];
|
2017-06-26 12:31:57 +00:00
|
|
|
int8_t j = i;
|
2018-01-31 11:50:57 +00:00
|
|
|
while ((j > 0 && !reverse && cur_poc > state->frame->ref->pocs[reflist[j - 1]]) ||
|
|
|
|
(j > 0 && reverse && cur_poc < state->frame->ref->pocs[reflist[j - 1]]))
|
|
|
|
{
|
2015-03-26 08:40:21 +00:00
|
|
|
reflist[j] = reflist[j - 1];
|
|
|
|
--j;
|
|
|
|
}
|
2017-06-26 12:31:57 +00:00
|
|
|
reflist[j] = cur_idx;
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 07:01:02 +00:00
|
|
|
|
|
|
|
/**
|
2017-06-26 12:31:57 +00:00
|
|
|
* \brief Generate reference picture lists.
|
2015-09-09 07:01:02 +00:00
|
|
|
*
|
|
|
|
* \param state main encoder state
|
|
|
|
*/
|
2017-06-26 12:31:57 +00:00
|
|
|
void kvz_encoder_create_ref_lists(const encoder_state_t *const state)
|
2015-09-09 07:01:02 +00:00
|
|
|
{
|
2018-01-31 11:50:57 +00:00
|
|
|
const kvz_config *cfg = &state->encoder_control->cfg;
|
|
|
|
|
2017-06-26 12:31:57 +00:00
|
|
|
FILL_ARRAY(state->frame->ref_LX_size, 0, 2);
|
2015-03-26 08:40:21 +00:00
|
|
|
|
2018-01-31 11:50:57 +00:00
|
|
|
int num_negative = 0;
|
|
|
|
int num_positive = 0;
|
|
|
|
|
|
|
|
// Add positive references to L1 list
|
|
|
|
for (int i = 0; i < state->frame->ref->used_size; i++) {
|
|
|
|
if (state->frame->ref->pocs[i] > state->frame->poc) {
|
|
|
|
state->frame->ref_LX[1][state->frame->ref_LX_size[1]] = i;
|
2017-06-26 12:31:57 +00:00
|
|
|
state->frame->ref_LX_size[1] += 1;
|
2018-01-31 11:50:57 +00:00
|
|
|
num_positive++;
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 11:50:57 +00:00
|
|
|
// Add negative references to L1 list when bipred is enabled and GOP is
|
|
|
|
// either disabled or does not use picture reordering.
|
|
|
|
bool l1_negative_refs =
|
|
|
|
(cfg->bipred && (cfg->gop_len == 0 || cfg->gop_lowdelay));
|
|
|
|
|
|
|
|
// Add negative references to L0 and L1 lists.
|
|
|
|
for (int i = 0; i < state->frame->ref->used_size; i++) {
|
|
|
|
if (state->frame->ref->pocs[i] < state->frame->poc) {
|
|
|
|
state->frame->ref_LX[0][state->frame->ref_LX_size[0]] = i;
|
|
|
|
state->frame->ref_LX_size[0] += 1;
|
|
|
|
if (l1_negative_refs) {
|
|
|
|
state->frame->ref_LX[1][state->frame->ref_LX_size[1]] = i;
|
|
|
|
state->frame->ref_LX_size[1] += 1;
|
|
|
|
}
|
|
|
|
num_negative++;
|
|
|
|
}
|
2015-09-09 07:01:02 +00:00
|
|
|
}
|
2015-03-26 08:40:21 +00:00
|
|
|
|
2018-01-31 11:50:57 +00:00
|
|
|
// Fill the rest with -1.
|
|
|
|
for (int i = state->frame->ref_LX_size[0]; i < 16; i++) {
|
|
|
|
state->frame->ref_LX[0][i] = 0xff;
|
|
|
|
}
|
|
|
|
for (int i = state->frame->ref_LX_size[1]; i < 16; i++) {
|
|
|
|
state->frame->ref_LX[1][i] = 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort reference lists.
|
|
|
|
encoder_ref_insertion_sort(state, state->frame->ref_LX[0], num_negative, false);
|
|
|
|
encoder_ref_insertion_sort(state, state->frame->ref_LX[1], num_positive, true);
|
|
|
|
if (l1_negative_refs) {
|
|
|
|
encoder_ref_insertion_sort(state, state->frame->ref_LX[1] + num_positive, num_negative, false);
|
|
|
|
}
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 09:54:31 +00:00
|
|
|
/**
|
|
|
|
* \brief Remove any references that should no longer be used.
|
|
|
|
*/
|
2015-03-26 08:40:21 +00:00
|
|
|
static void encoder_state_remove_refs(encoder_state_t *state) {
|
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
2017-10-19 10:25:57 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
int neg_refs = encoder->cfg.gop[state->frame->gop_offset].ref_neg_count;
|
|
|
|
int pos_refs = encoder->cfg.gop[state->frame->gop_offset].ref_pos_count;
|
2016-05-27 09:54:31 +00:00
|
|
|
|
|
|
|
unsigned target_ref_num;
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.gop_len) {
|
2016-05-27 09:54:31 +00:00
|
|
|
target_ref_num = neg_refs + pos_refs;
|
|
|
|
} else {
|
2017-02-05 09:59:21 +00:00
|
|
|
target_ref_num = encoder->cfg.ref_frames;
|
2016-05-27 10:01:55 +00:00
|
|
|
}
|
2017-10-19 10:25:57 +00:00
|
|
|
|
|
|
|
if (state->frame->pictype == KVZ_NAL_IDR_W_RADL ||
|
|
|
|
state->frame->pictype == KVZ_NAL_IDR_N_LP)
|
|
|
|
{
|
2016-05-27 10:01:55 +00:00
|
|
|
target_ref_num = 0;
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
2016-05-27 09:54:31 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (encoder->cfg.gop_len && target_ref_num > 0) {
|
2016-05-27 09:54:31 +00:00
|
|
|
// With GOP in use, go through all the existing reference pictures and
|
|
|
|
// remove any picture that is not referenced by the current picture.
|
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
for (int ref = state->frame->ref->used_size - 1; ref >= 0; --ref) {
|
2016-05-27 09:54:31 +00:00
|
|
|
bool is_referenced = false;
|
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
int ref_poc = state->frame->ref->pocs[ref];
|
2017-10-19 10:25:57 +00:00
|
|
|
|
2016-05-27 09:54:31 +00:00
|
|
|
for (int i = 0; i < neg_refs; i++) {
|
2017-02-05 09:59:21 +00:00
|
|
|
int ref_relative_poc = -encoder->cfg.gop[state->frame->gop_offset].ref_neg[i];
|
2016-08-10 00:46:23 +00:00
|
|
|
if (ref_poc == state->frame->poc + ref_relative_poc) {
|
2016-05-27 09:54:31 +00:00
|
|
|
is_referenced = true;
|
|
|
|
break;
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
2016-05-27 09:54:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < pos_refs; i++) {
|
2017-02-05 09:59:21 +00:00
|
|
|
int ref_relative_poc = encoder->cfg.gop[state->frame->gop_offset].ref_pos[i];
|
2016-08-10 00:46:23 +00:00
|
|
|
if (ref_poc == state->frame->poc + ref_relative_poc) {
|
2016-05-27 09:54:31 +00:00
|
|
|
is_referenced = true;
|
|
|
|
break;
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 09:54:31 +00:00
|
|
|
|
2017-09-19 06:20:10 +00:00
|
|
|
if (ref_poc < state->frame->irap_poc &&
|
|
|
|
state->frame->irap_poc < state->frame->poc)
|
|
|
|
{
|
|
|
|
// Trailing frames cannot refer to leading frames.
|
|
|
|
is_referenced = false;
|
|
|
|
}
|
|
|
|
|
2017-10-19 10:25:57 +00:00
|
|
|
if (encoder->cfg.intra_period > 0 &&
|
|
|
|
ref_poc < state->frame->irap_poc - encoder->cfg.intra_period)
|
|
|
|
{
|
|
|
|
// No frame can refer past the two preceding IRAP frames.
|
|
|
|
is_referenced = false;
|
|
|
|
}
|
|
|
|
|
2016-05-27 09:54:31 +00:00
|
|
|
if (!is_referenced) {
|
|
|
|
// This reference is not referred to by this frame, it must be removed.
|
2016-08-10 00:46:23 +00:00
|
|
|
kvz_image_list_rem(state->frame->ref, ref);
|
2016-05-27 09:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Without GOP, remove the oldest picture.
|
2016-08-10 00:46:23 +00:00
|
|
|
while (state->frame->ref->used_size > target_ref_num) {
|
|
|
|
int8_t oldest_ref = state->frame->ref->used_size - 1;
|
|
|
|
kvz_image_list_rem(state->frame->ref, oldest_ref);
|
2016-05-27 09:54:31 +00:00
|
|
|
}
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
2015-03-26 09:14:13 +00:00
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
assert(state->frame->ref->used_size <= target_ref_num);
|
2015-03-26 08:40:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
static void encoder_set_source_picture(encoder_state_t * const state, kvz_picture* frame)
|
|
|
|
{
|
|
|
|
assert(!state->tile->frame->source);
|
|
|
|
assert(!state->tile->frame->rec);
|
2015-04-29 14:22:53 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
state->tile->frame->source = frame;
|
2017-02-05 09:59:21 +00:00
|
|
|
if (state->encoder_control->cfg.lossless) {
|
2016-06-20 02:48:17 +00:00
|
|
|
// In lossless mode, the reconstruction is equal to the source frame.
|
|
|
|
state->tile->frame->rec = kvz_image_copy_ref(frame);
|
|
|
|
} else {
|
2016-08-16 16:03:21 +00:00
|
|
|
state->tile->frame->rec = kvz_image_alloc(state->encoder_control->chroma_format, frame->width, frame->height);
|
2016-10-28 16:05:47 +00:00
|
|
|
state->tile->frame->rec->dts = frame->dts;
|
|
|
|
state->tile->frame->rec->pts = frame->pts;
|
2016-06-20 02:48:17 +00:00
|
|
|
}
|
2015-04-30 10:02:02 +00:00
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
kvz_videoframe_set_poc(state->tile->frame, state->frame->poc);
|
2016-06-20 00:36:21 +00:00
|
|
|
}
|
2015-03-03 10:22:50 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
static void encoder_state_init_children(encoder_state_t * const state) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_bitstream_clear(&state->stream);
|
2016-06-20 00:36:21 +00:00
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->is_leaf) {
|
2014-06-03 12:25:16 +00:00
|
|
|
//Leaf states have cabac and context
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_cabac_start(&state->cabac);
|
2019-05-22 14:26:10 +00:00
|
|
|
kvz_init_contexts(state, state->encoder_control->cfg.set_qp_in_cu ? 26 : state->frame->QP, state->frame->slicetype);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
2016-06-20 00:36:21 +00:00
|
|
|
|
2014-06-13 09:03:15 +00:00
|
|
|
//Clear the jobs
|
2017-04-04 12:36:08 +00:00
|
|
|
kvz_threadqueue_free_job(&state->tqj_bitstream_written);
|
|
|
|
kvz_threadqueue_free_job(&state->tqj_recon_done);
|
2016-06-20 00:36:21 +00:00
|
|
|
|
2019-08-28 15:45:58 +00:00
|
|
|
//Copy the constraint pointer
|
2020-01-27 11:11:31 +00:00
|
|
|
// TODO: Try to do it in the if (state->is_leaf)
|
2019-09-12 06:45:32 +00:00
|
|
|
//if (state->parent != NULL) {
|
2020-01-27 11:11:31 +00:00
|
|
|
// state->constraint = state->parent->constraint;
|
2019-09-12 06:45:32 +00:00
|
|
|
//}
|
2019-08-28 15:45:58 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
for (int i = 0; state->children[i].encoder_control; ++i) {
|
|
|
|
encoder_state_init_children(&state->children[i]);
|
2014-06-03 12:25:16 +00:00
|
|
|
}
|
2016-06-20 00:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 01:16:48 +00:00
|
|
|
static void normalize_lcu_weights(encoder_state_t * const state)
|
|
|
|
{
|
|
|
|
if (state->frame->num == 0) return;
|
|
|
|
|
|
|
|
const uint32_t num_lcus = state->encoder_control->in.width_in_lcu *
|
|
|
|
state->encoder_control->in.height_in_lcu;
|
|
|
|
double sum = 0.0;
|
|
|
|
for (uint32_t i = 0; i < num_lcus; i++) {
|
|
|
|
sum += state->frame->lcu_stats[i].weight;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < num_lcus; i++) {
|
|
|
|
state->frame->lcu_stats[i].weight /= sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 10:33:14 +00:00
|
|
|
// Check if lcu is edge lcu. Return false if frame dimensions are 64 divisible
|
2020-04-03 13:22:34 +00:00
|
|
|
static bool edge_lcu(int id, int lcus_x, int lcus_y, bool xdiv64, bool ydiv64)
|
2020-04-03 10:33:14 +00:00
|
|
|
{
|
|
|
|
if (xdiv64 && ydiv64) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int last_row_first_id = (lcus_y - 1) * lcus_x;
|
|
|
|
if ((id % lcus_x == lcus_x - 1 && !xdiv64) || (id >= last_row_first_id && !ydiv64)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
static void encoder_state_init_new_frame(encoder_state_t * const state, kvz_picture* frame) {
|
|
|
|
assert(state->type == ENCODER_STATE_TYPE_MAIN);
|
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
const kvz_config * const cfg = &state->encoder_control->cfg;
|
2016-06-20 00:36:21 +00:00
|
|
|
|
|
|
|
encoder_set_source_picture(state, frame);
|
|
|
|
|
2017-07-27 13:37:02 +00:00
|
|
|
assert(!state->tile->frame->cu_array);
|
|
|
|
state->tile->frame->cu_array = kvz_cu_array_alloc(
|
|
|
|
state->tile->frame->width,
|
|
|
|
state->tile->frame->height
|
|
|
|
);
|
|
|
|
|
2020-02-04 12:15:38 +00:00
|
|
|
// Variance adaptive quantization
|
|
|
|
if (cfg->vaq) {
|
2020-02-11 19:34:38 +00:00
|
|
|
const bool has_chroma = state->encoder_control->chroma_format != KVZ_CSP_400;
|
2020-02-07 17:45:41 +00:00
|
|
|
double d = cfg->vaq * 0.1; // Empirically decided constant. Affects delta-QP strength
|
2020-02-04 12:15:38 +00:00
|
|
|
|
|
|
|
// Calculate frame pixel variance
|
|
|
|
uint32_t len = state->tile->frame->width * state->tile->frame->height;
|
2020-03-26 12:33:00 +00:00
|
|
|
uint32_t c_len = len / 4;
|
2020-02-24 17:37:36 +00:00
|
|
|
double frame_var = kvz_pixel_var(state->tile->frame->source->y, len);
|
2020-03-26 12:33:00 +00:00
|
|
|
if (has_chroma) {
|
2020-03-31 19:32:58 +00:00
|
|
|
frame_var += kvz_pixel_var(state->tile->frame->source->u, c_len);
|
|
|
|
frame_var += kvz_pixel_var(state->tile->frame->source->v, c_len);
|
2020-03-26 12:33:00 +00:00
|
|
|
}
|
2020-02-04 12:15:38 +00:00
|
|
|
|
|
|
|
// Loop through LCUs
|
|
|
|
// For each LCU calculate: D * (log(LCU pixel variance) - log(frame pixel variance))
|
|
|
|
unsigned x_lim = state->tile->frame->width_in_lcu;
|
|
|
|
unsigned y_lim = state->tile->frame->height_in_lcu;
|
|
|
|
|
|
|
|
unsigned id = 0;
|
2020-02-07 14:04:23 +00:00
|
|
|
for (int y = 0; y < y_lim; ++y) {
|
|
|
|
for (int x = 0; x < x_lim; ++x) {
|
2020-02-04 12:15:38 +00:00
|
|
|
kvz_pixel tmp[LCU_LUMA_SIZE];
|
2020-02-07 14:04:23 +00:00
|
|
|
int pxl_x = x * LCU_WIDTH;
|
|
|
|
int pxl_y = y * LCU_WIDTH;
|
2020-02-11 19:34:38 +00:00
|
|
|
int x_max = MIN(pxl_x + LCU_WIDTH, frame->width) - pxl_x;
|
|
|
|
int y_max = MIN(pxl_y + LCU_WIDTH, frame->height) - pxl_y;
|
|
|
|
|
2020-04-03 10:33:14 +00:00
|
|
|
bool xdiv64 = false;
|
|
|
|
bool ydiv64 = false;
|
|
|
|
if (frame->width % 64 == 0) xdiv64 = true;
|
|
|
|
if (frame->height % 64 == 0) ydiv64 = true;
|
2020-02-11 19:34:38 +00:00
|
|
|
|
2020-04-03 10:33:14 +00:00
|
|
|
// Luma variance
|
|
|
|
if (!edge_lcu(id, x_lim, y_lim, xdiv64, ydiv64)) {
|
|
|
|
kvz_pixels_blit(&state->tile->frame->source->y[pxl_x + pxl_y * state->tile->frame->source->stride], tmp,
|
|
|
|
x_max, y_max, state->tile->frame->source->stride, LCU_WIDTH);
|
|
|
|
} else {
|
|
|
|
// Extend edge pixels for edge lcus
|
|
|
|
for (int y = 0; y < LCU_WIDTH; y++) {
|
|
|
|
for (int x = 0; x < LCU_WIDTH; x++) {
|
|
|
|
int src_y = CLIP(0, frame->height - 1, pxl_y + y);
|
|
|
|
int src_x = CLIP(0, frame->width - 1, pxl_x + x);
|
|
|
|
tmp[y * LCU_WIDTH + x] = state->tile->frame->source->y[src_y * state->tile->frame->source->stride + src_x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:37:36 +00:00
|
|
|
double lcu_var = kvz_pixel_var(tmp, LCU_LUMA_SIZE);
|
2020-02-11 19:34:38 +00:00
|
|
|
|
|
|
|
if (has_chroma) {
|
|
|
|
// Add chroma variance if not monochrome
|
|
|
|
int32_t c_stride = state->tile->frame->source->stride >> 1;
|
2020-02-24 17:54:34 +00:00
|
|
|
kvz_pixel chromau_tmp[LCU_CHROMA_SIZE];
|
|
|
|
kvz_pixel chromav_tmp[LCU_CHROMA_SIZE];
|
|
|
|
int lcu_chroma_width = LCU_WIDTH >> 1;
|
2020-02-11 19:34:38 +00:00
|
|
|
int c_pxl_x = x * lcu_chroma_width;
|
|
|
|
int c_pxl_y = y * lcu_chroma_width;
|
2020-02-24 17:54:34 +00:00
|
|
|
int c_x_max = MIN(c_pxl_x + lcu_chroma_width, frame->width >> 1) - c_pxl_x;
|
|
|
|
int c_y_max = MIN(c_pxl_y + lcu_chroma_width, frame->height >> 1) - c_pxl_y;
|
2020-02-11 19:34:38 +00:00
|
|
|
|
2020-04-03 10:33:14 +00:00
|
|
|
if (!edge_lcu(id, x_lim, y_lim, xdiv64, ydiv64)) {
|
|
|
|
kvz_pixels_blit(&state->tile->frame->source->u[c_pxl_x + c_pxl_y * c_stride], chromau_tmp, c_x_max, c_y_max, c_stride, lcu_chroma_width);
|
|
|
|
kvz_pixels_blit(&state->tile->frame->source->v[c_pxl_x + c_pxl_y * c_stride], chromav_tmp, c_x_max, c_y_max, c_stride, lcu_chroma_width);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (int y = 0; y < lcu_chroma_width; y++) {
|
|
|
|
for (int x = 0; x < lcu_chroma_width; x++) {
|
|
|
|
int src_y = CLIP(0, (frame->height >> 1) - 1, c_pxl_y + y);
|
|
|
|
int src_x = CLIP(0, (frame->width >> 1) - 1, c_pxl_x + x);
|
|
|
|
chromau_tmp[y * lcu_chroma_width + x] = state->tile->frame->source->u[src_y * c_stride + src_x];
|
|
|
|
chromav_tmp[y * lcu_chroma_width + x] = state->tile->frame->source->v[src_y * c_stride + src_x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 19:32:58 +00:00
|
|
|
lcu_var += kvz_pixel_var(chromau_tmp, LCU_CHROMA_SIZE);
|
|
|
|
lcu_var += kvz_pixel_var(chromav_tmp, LCU_CHROMA_SIZE);
|
2020-02-11 19:34:38 +00:00
|
|
|
}
|
2020-02-24 17:54:34 +00:00
|
|
|
|
2020-02-04 12:15:38 +00:00
|
|
|
state->frame->aq_offsets[id] = d * (log(lcu_var) - log(frame_var));
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Variance adaptive quantization - END
|
|
|
|
|
2018-10-04 12:16:02 +00:00
|
|
|
// Use this flag to handle closed gop irap picture selection.
|
|
|
|
// If set to true, irap is already set and we avoid
|
|
|
|
// setting it based on the intra period
|
|
|
|
bool is_closed_normal_gop = false;
|
|
|
|
|
2020-03-18 10:29:25 +00:00
|
|
|
encoder_state_t *previous = state->previous_encoder_state;
|
|
|
|
int owf = MIN(state->encoder_control->cfg.owf, state->frame->num);
|
|
|
|
|
|
|
|
const int layer = state->encoder_control->cfg.gop[state->frame->gop_offset].layer;
|
|
|
|
|
|
|
|
while (--owf > 0 && layer != state->encoder_control->cfg.gop[previous->frame->gop_offset].layer) {
|
|
|
|
previous = previous->previous_encoder_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owf == 0) previous = state;
|
|
|
|
state->frame->previous_layer_state = previous;
|
2017-10-19 10:25:57 +00:00
|
|
|
// Set POC.
|
2016-08-10 00:46:23 +00:00
|
|
|
if (state->frame->num == 0) {
|
2017-10-19 10:25:57 +00:00
|
|
|
state->frame->poc = 0;
|
|
|
|
} else if (cfg->gop_len && !cfg->gop_lowdelay) {
|
2018-10-04 08:17:03 +00:00
|
|
|
|
|
|
|
int32_t framenum = state->frame->num - 1;
|
|
|
|
// Handle closed GOP
|
|
|
|
// Closed GOP structure has an extra IDR between the GOPs
|
|
|
|
if (cfg->intra_period > 0 && !cfg->open_gop) {
|
2018-10-04 12:16:02 +00:00
|
|
|
is_closed_normal_gop = true;
|
|
|
|
if (framenum % (cfg->intra_period + 1) == cfg->intra_period) {
|
2018-10-04 08:17:03 +00:00
|
|
|
// Insert IDR before each new GOP after intra period in closed GOP configuration
|
|
|
|
state->frame->poc = 0;
|
|
|
|
} else {
|
2018-10-10 08:12:03 +00:00
|
|
|
// Calculate frame number again and use that for the POC
|
2018-10-04 08:17:03 +00:00
|
|
|
framenum = framenum % (cfg->intra_period + 1);
|
|
|
|
int32_t poc_offset = cfg->gop[state->frame->gop_offset].poc_offset;
|
|
|
|
state->frame->poc = framenum - framenum % cfg->gop_len + poc_offset;
|
|
|
|
// This should not be an irap picture in closed GOP
|
|
|
|
state->frame->is_irap = false;
|
|
|
|
}
|
|
|
|
} else { // Open GOP
|
|
|
|
// Calculate POC according to the global frame counter and GOP structure
|
|
|
|
int32_t poc_offset = cfg->gop[state->frame->gop_offset].poc_offset;
|
|
|
|
state->frame->poc = framenum - framenum % cfg->gop_len + poc_offset;
|
|
|
|
}
|
|
|
|
|
2017-10-19 10:25:57 +00:00
|
|
|
kvz_videoframe_set_poc(state->tile->frame, state->frame->poc);
|
2020-08-25 08:53:55 +00:00
|
|
|
} else if (cfg->intra_period > 1) {
|
2017-10-19 10:25:57 +00:00
|
|
|
state->frame->poc = state->frame->num % cfg->intra_period;
|
2016-06-20 00:36:21 +00:00
|
|
|
} else {
|
2017-10-19 10:25:57 +00:00
|
|
|
state->frame->poc = state->frame->num;
|
2017-06-21 09:41:07 +00:00
|
|
|
}
|
2016-06-20 00:36:21 +00:00
|
|
|
|
2017-10-19 10:25:57 +00:00
|
|
|
// Check whether the frame is a keyframe or not.
|
2018-10-04 08:17:03 +00:00
|
|
|
if (state->frame->num == 0 || state->frame->poc == 0) {
|
2017-10-19 10:25:57 +00:00
|
|
|
state->frame->is_irap = true;
|
2018-10-04 12:16:02 +00:00
|
|
|
} else if(!is_closed_normal_gop) { // In closed-GOP IDR frames are poc==0 so skip this check
|
2017-10-19 10:25:57 +00:00
|
|
|
state->frame->is_irap =
|
|
|
|
cfg->intra_period > 0 &&
|
|
|
|
(state->frame->poc % cfg->intra_period) == 0;
|
2017-06-21 09:41:07 +00:00
|
|
|
}
|
2017-10-19 10:25:57 +00:00
|
|
|
if (state->frame->is_irap) {
|
|
|
|
state->frame->irap_poc = state->frame->poc;
|
2017-09-19 06:20:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set pictype.
|
|
|
|
if (state->frame->is_irap) {
|
2017-10-19 10:25:57 +00:00
|
|
|
if (state->frame->num == 0 ||
|
|
|
|
cfg->intra_period == 1 ||
|
|
|
|
cfg->gop_len == 0 ||
|
2018-10-04 08:17:03 +00:00
|
|
|
cfg->gop_lowdelay ||
|
|
|
|
!cfg->open_gop) // Closed GOP uses IDR pictures
|
2017-10-19 10:25:57 +00:00
|
|
|
{
|
2020-08-25 08:53:55 +00:00
|
|
|
state->frame->pictype = KVZ_NAL_IDR_N_LP;
|
|
|
|
if (cfg->intra_period == 1 && state->frame->num > 0) state->frame->pictype = KVZ_NAL_IDR_W_RADL;
|
2017-10-19 10:25:57 +00:00
|
|
|
} else {
|
|
|
|
state->frame->pictype = KVZ_NAL_CRA_NUT;
|
2017-09-19 06:20:10 +00:00
|
|
|
}
|
|
|
|
} else if (state->frame->poc < state->frame->irap_poc) {
|
2019-05-29 13:17:02 +00:00
|
|
|
state->frame->pictype = KVZ_NAL_RASL;
|
2017-09-19 06:20:10 +00:00
|
|
|
} else {
|
2019-05-29 13:17:02 +00:00
|
|
|
state->frame->pictype = KVZ_NAL_TRAIL;
|
2016-06-20 00:36:21 +00:00
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
encoder_state_remove_refs(state);
|
2017-06-26 12:31:57 +00:00
|
|
|
kvz_encoder_create_ref_lists(state);
|
2016-08-21 05:03:57 +00:00
|
|
|
|
2017-10-19 10:25:57 +00:00
|
|
|
// Set slicetype.
|
|
|
|
if (state->frame->is_irap) {
|
|
|
|
state->frame->slicetype = KVZ_SLICE_I;
|
|
|
|
} else if (state->frame->ref_LX_size[1] > 0) {
|
|
|
|
state->frame->slicetype = KVZ_SLICE_B;
|
|
|
|
} else {
|
|
|
|
state->frame->slicetype = KVZ_SLICE_P;
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:20:31 +00:00
|
|
|
if (cfg->target_bitrate > 0 && state->frame->num > cfg->owf) {
|
|
|
|
normalize_lcu_weights(state);
|
|
|
|
}
|
2019-09-26 10:09:28 +00:00
|
|
|
state->frame->cur_frame_bits_coded = 0;
|
2016-06-20 00:36:21 +00:00
|
|
|
|
2019-11-19 11:45:58 +00:00
|
|
|
switch (state->encoder_control->cfg.rc_algorithm) {
|
|
|
|
case KVZ_NO_RC:
|
|
|
|
case KVZ_LAMBDA:
|
|
|
|
kvz_set_picture_lambda_and_qp(state);
|
|
|
|
break;
|
|
|
|
case KVZ_OBA:
|
|
|
|
kvz_estimate_pic_lambda(state);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
encoder_state_init_children(state);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 15:00:23 +00:00
|
|
|
static void _encode_one_frame_add_bitstream_deps(const encoder_state_t * const state, threadqueue_job_t * const job) {
|
2014-06-13 09:30:08 +00:00
|
|
|
int i;
|
2015-03-04 15:00:23 +00:00
|
|
|
for (i = 0; state->children[i].encoder_control; ++i) {
|
|
|
|
_encode_one_frame_add_bitstream_deps(&state->children[i], job);
|
2014-06-13 09:30:08 +00:00
|
|
|
}
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->tqj_bitstream_written) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_threadqueue_job_dep_add(job, state->tqj_bitstream_written);
|
2014-06-13 09:30:08 +00:00
|
|
|
}
|
2015-03-04 15:00:23 +00:00
|
|
|
if (state->tqj_recon_done) {
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_threadqueue_job_dep_add(job, state->tqj_recon_done);
|
2014-06-13 09:30:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-03 11:51:30 +00:00
|
|
|
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
void kvz_encode_one_frame(encoder_state_t * const state, kvz_picture* frame)
|
2014-06-03 12:25:16 +00:00
|
|
|
{
|
2018-09-26 09:01:23 +00:00
|
|
|
#if KVZ_DEBUG_PRINT_CABAC == 1
|
|
|
|
kvz_cabac_bins_count = 0;
|
2020-12-15 11:04:39 +00:00
|
|
|
if (state->frame->num == 0) kvz_cabac_bins_verbose = true;
|
2018-09-26 09:01:23 +00:00
|
|
|
else kvz_cabac_bins_verbose = false;
|
|
|
|
#endif
|
2017-07-20 12:18:30 +00:00
|
|
|
encoder_state_init_new_frame(state, frame);
|
|
|
|
encoder_state_encode(state);
|
|
|
|
|
|
|
|
threadqueue_job_t *job =
|
2017-07-20 12:23:18 +00:00
|
|
|
kvz_threadqueue_job_create(kvz_encoder_state_worker_write_bitstream, state);
|
2017-07-20 12:18:30 +00:00
|
|
|
|
|
|
|
_encode_one_frame_add_bitstream_deps(state, job);
|
|
|
|
if (state->previous_encoder_state != state && state->previous_encoder_state->tqj_bitstream_written) {
|
|
|
|
//We need to depend on previous bitstream generation
|
|
|
|
kvz_threadqueue_job_dep_add(job, state->previous_encoder_state->tqj_bitstream_written);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
2017-07-20 12:23:18 +00:00
|
|
|
kvz_threadqueue_submit(state->encoder_control->threadqueue, job);
|
2017-07-20 12:18:30 +00:00
|
|
|
assert(!state->tqj_bitstream_written);
|
|
|
|
state->tqj_bitstream_written = job;
|
2017-07-20 12:23:18 +00:00
|
|
|
|
2016-08-21 03:27:58 +00:00
|
|
|
state->frame->done = 0;
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 06:10:47 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
/**
|
|
|
|
* Prepare the encoder state for encoding the next frame.
|
|
|
|
*
|
|
|
|
* - Add the previous reconstructed picture as a reference, if needed.
|
|
|
|
* - Free the previous reconstructed and source pictures.
|
|
|
|
* - Create a new cu array, if needed.
|
|
|
|
* - Update frame count and POC.
|
|
|
|
*/
|
|
|
|
void kvz_encoder_prepare(encoder_state_t *state)
|
2015-05-18 15:21:23 +00:00
|
|
|
{
|
2015-03-04 15:00:23 +00:00
|
|
|
const encoder_control_t * const encoder = state->encoder_control;
|
2015-09-07 12:08:31 +00:00
|
|
|
|
|
|
|
// The previous frame must be done before the next one is started.
|
2016-08-21 03:27:58 +00:00
|
|
|
assert(state->frame->done);
|
2015-05-18 15:21:23 +00:00
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
if (state->frame->num == -1) {
|
2016-06-20 00:36:21 +00:00
|
|
|
// We're at the first frame, so don't care about all this stuff.
|
2016-08-10 00:46:23 +00:00
|
|
|
state->frame->num = 0;
|
2017-09-19 06:20:10 +00:00
|
|
|
state->frame->poc = 0;
|
|
|
|
state->frame->irap_poc = 0;
|
2015-06-18 06:10:47 +00:00
|
|
|
assert(!state->tile->frame->source);
|
2015-03-04 15:00:23 +00:00
|
|
|
assert(!state->tile->frame->rec);
|
2017-07-27 13:37:02 +00:00
|
|
|
assert(!state->tile->frame->cu_array);
|
2016-08-21 03:27:58 +00:00
|
|
|
state->frame->prepared = 1;
|
2019-08-28 15:45:58 +00:00
|
|
|
|
2014-06-13 09:58:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-06-17 06:56:53 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
// NOTE: prev_state is equal to state when OWF is zero
|
|
|
|
encoder_state_t *prev_state = state->previous_encoder_state;
|
2015-03-10 07:18:19 +00:00
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
if (state->previous_encoder_state != state) {
|
2017-07-27 13:37:02 +00:00
|
|
|
kvz_cu_array_free(&state->tile->frame->cu_array);
|
2016-06-20 00:36:21 +00:00
|
|
|
unsigned width = state->tile->frame->width_in_lcu * LCU_WIDTH;
|
|
|
|
unsigned height = state->tile->frame->height_in_lcu * LCU_WIDTH;
|
|
|
|
state->tile->frame->cu_array = kvz_cu_array_alloc(width, height);
|
2015-03-06 14:32:50 +00:00
|
|
|
|
2016-08-10 00:46:23 +00:00
|
|
|
kvz_image_list_copy_contents(state->frame->ref, prev_state->frame->ref);
|
2017-10-19 10:25:57 +00:00
|
|
|
kvz_encoder_create_ref_lists(state);
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
2015-03-20 08:28:17 +00:00
|
|
|
|
2017-02-05 09:59:21 +00:00
|
|
|
if (!encoder->cfg.gop_len ||
|
2016-08-10 00:46:23 +00:00
|
|
|
!prev_state->frame->poc ||
|
2017-02-05 09:59:21 +00:00
|
|
|
encoder->cfg.gop[prev_state->frame->gop_offset].is_ref) {
|
2017-02-10 07:49:33 +00:00
|
|
|
|
|
|
|
// Store current list of POCs for use in TMVP derivation
|
|
|
|
memcpy(prev_state->tile->frame->rec->ref_pocs, state->frame->ref->pocs, sizeof(int32_t)*state->frame->ref->used_size);
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
// Add previous reconstructed picture as a reference
|
2016-08-10 00:46:23 +00:00
|
|
|
kvz_image_list_add(state->frame->ref,
|
2016-06-20 00:36:21 +00:00
|
|
|
prev_state->tile->frame->rec,
|
|
|
|
prev_state->tile->frame->cu_array,
|
2017-08-21 13:34:57 +00:00
|
|
|
prev_state->frame->poc,
|
|
|
|
prev_state->frame->ref_LX);
|
2017-07-27 13:37:02 +00:00
|
|
|
kvz_cu_array_free(&state->tile->frame->cu_array);
|
2016-06-27 07:11:02 +00:00
|
|
|
unsigned height = state->tile->frame->height_in_lcu * LCU_WIDTH;
|
|
|
|
unsigned width = state->tile->frame->width_in_lcu * LCU_WIDTH;
|
|
|
|
state->tile->frame->cu_array = kvz_cu_array_alloc(width, height);
|
2015-03-20 08:28:17 +00:00
|
|
|
}
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
// Remove source and reconstructed picture.
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_image_free(state->tile->frame->source);
|
2015-06-18 06:10:47 +00:00
|
|
|
state->tile->frame->source = NULL;
|
2017-07-27 13:37:02 +00:00
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
kvz_image_free(state->tile->frame->rec);
|
2016-06-20 00:36:21 +00:00
|
|
|
state->tile->frame->rec = NULL;
|
|
|
|
|
2017-07-27 13:37:02 +00:00
|
|
|
kvz_cu_array_free(&state->tile->frame->cu_array);
|
|
|
|
|
2016-06-20 00:36:21 +00:00
|
|
|
// Update POC and frame count.
|
2016-08-10 00:46:23 +00:00
|
|
|
state->frame->num = prev_state->frame->num + 1;
|
2017-09-19 06:20:10 +00:00
|
|
|
state->frame->poc = prev_state->frame->poc + 1;
|
|
|
|
state->frame->irap_poc = prev_state->frame->irap_poc;
|
2015-03-20 08:28:17 +00:00
|
|
|
|
2016-08-21 03:27:58 +00:00
|
|
|
state->frame->prepared = 1;
|
2019-08-28 15:45:58 +00:00
|
|
|
|
|
|
|
|
2014-06-03 11:51:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 08:50:27 +00:00
|
|
|
coeff_scan_order_t kvz_get_scan_order(int8_t cu_type, int intra_mode, int depth)
|
2014-06-03 11:51:30 +00:00
|
|
|
{
|
|
|
|
// Scan mode is diagonal, except for 4x4+8x8 luma and 4x4 chroma, where:
|
|
|
|
// - angular 6-14 = vertical
|
|
|
|
// - angular 22-30 = horizontal
|
2018-08-17 08:38:40 +00:00
|
|
|
#if HEVC_USE_MDCS
|
2014-06-03 11:51:30 +00:00
|
|
|
if (cu_type == CU_INTRA && depth >= 3) {
|
|
|
|
if (intra_mode >= 6 && intra_mode <= 14) {
|
|
|
|
return SCAN_VER;
|
|
|
|
} else if (intra_mode >= 22 && intra_mode <= 30) {
|
|
|
|
return SCAN_HOR;
|
|
|
|
}
|
|
|
|
}
|
2018-08-17 08:38:40 +00:00
|
|
|
#endif
|
2014-06-03 11:51:30 +00:00
|
|
|
return SCAN_DIAG;
|
|
|
|
}
|
2016-08-24 01:16:48 +00:00
|
|
|
|
|
|
|
lcu_stats_t* kvz_get_lcu_stats(encoder_state_t *state, int lcu_x, int lcu_y)
|
|
|
|
{
|
|
|
|
const int index = lcu_x + state->tile->lcu_offset_x +
|
|
|
|
(lcu_y + state->tile->lcu_offset_y) *
|
|
|
|
state->encoder_control->in.width_in_lcu;
|
|
|
|
return &state->frame->lcu_stats[index];
|
|
|
|
}
|
2017-05-08 07:54:06 +00:00
|
|
|
|
|
|
|
int kvz_get_cu_ref_qp(const encoder_state_t *state, int x, int y, int last_qp)
|
|
|
|
{
|
|
|
|
const encoder_control_t *ctrl = state->encoder_control;
|
|
|
|
const cu_array_t *cua = state->tile->frame->cu_array;
|
|
|
|
// Quantization group width
|
|
|
|
const int qg_width = LCU_WIDTH >> MIN(ctrl->max_qp_delta_depth, kvz_cu_array_at_const(cua, x, y)->depth);
|
|
|
|
|
|
|
|
// Coordinates of the top-left corner of the quantization group
|
|
|
|
const int x_qg = x & ~(qg_width - 1);
|
|
|
|
const int y_qg = y & ~(qg_width - 1);
|
|
|
|
|
|
|
|
int qp_pred_a = last_qp;
|
|
|
|
if (x_qg % LCU_WIDTH > 0) {
|
|
|
|
qp_pred_a = kvz_cu_array_at_const(cua, x_qg - 1, y_qg)->qp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int qp_pred_b = last_qp;
|
|
|
|
if (y_qg % LCU_WIDTH > 0) {
|
|
|
|
qp_pred_b = kvz_cu_array_at_const(cua, x_qg, y_qg - 1)->qp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((qp_pred_a + qp_pred_b + 1) >> 1);
|
|
|
|
}
|