2015-07-14 05:53:35 +00:00
|
|
|
/*
|
|
|
|
* libkvazaar encoder
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Tampere University of Technology
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kvazaar.h>
|
2016-12-06 22:25:58 +00:00
|
|
|
#include <stdint.h>
|
2015-07-14 05:53:35 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-12-06 22:25:58 +00:00
|
|
|
#include "libavutil/attributes.h"
|
2015-07-14 05:53:35 +00:00
|
|
|
#include "libavutil/avassert.h"
|
|
|
|
#include "libavutil/dict.h"
|
2015-08-25 08:33:19 +00:00
|
|
|
#include "libavutil/error.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/internal.h"
|
2016-12-06 22:25:58 +00:00
|
|
|
#include "libavutil/log.h"
|
|
|
|
#include "libavutil/mem.h"
|
2015-09-28 08:08:19 +00:00
|
|
|
#include "libavutil/pixdesc.h"
|
2015-08-25 08:33:19 +00:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
|
2015-07-14 05:53:35 +00:00
|
|
|
#include "avcodec.h"
|
2022-03-16 17:18:28 +00:00
|
|
|
#include "codec_internal.h"
|
2021-04-24 23:43:26 +00:00
|
|
|
#include "encode.h"
|
2020-07-26 02:43:10 +00:00
|
|
|
#include "packet_internal.h"
|
2015-07-14 05:53:35 +00:00
|
|
|
|
|
|
|
typedef struct LibkvazaarContext {
|
|
|
|
const AVClass *class;
|
|
|
|
|
|
|
|
const kvz_api *api;
|
|
|
|
kvz_encoder *encoder;
|
|
|
|
kvz_config *config;
|
|
|
|
|
|
|
|
char *kvz_params;
|
|
|
|
} LibkvazaarContext;
|
|
|
|
|
|
|
|
static av_cold int libkvazaar_init(AVCodecContext *avctx)
|
|
|
|
{
|
2015-08-25 08:33:19 +00:00
|
|
|
LibkvazaarContext *const ctx = avctx->priv_data;
|
|
|
|
const kvz_api *const api = ctx->api = kvz_api_get(8);
|
2015-07-14 05:53:35 +00:00
|
|
|
kvz_config *cfg = NULL;
|
|
|
|
kvz_encoder *enc = NULL;
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
/* Kvazaar requires width and height to be multiples of eight. */
|
2015-07-14 05:53:35 +00:00
|
|
|
if (avctx->width % 8 || avctx->height % 8) {
|
2015-08-25 08:33:19 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Video dimensions are not a multiple of 8 (%dx%d).\n",
|
|
|
|
avctx->width, avctx->height);
|
|
|
|
return AVERROR(ENOSYS);
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
ctx->config = cfg = api->config_alloc();
|
2015-07-14 05:53:35 +00:00
|
|
|
if (!cfg) {
|
2015-08-25 08:33:19 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Could not allocate kvazaar config structure.\n");
|
|
|
|
return AVERROR(ENOMEM);
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!api->config_init(cfg)) {
|
2015-08-25 08:33:19 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Could not initialize kvazaar config structure.\n");
|
|
|
|
return AVERROR_BUG;
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
cfg->width = avctx->width;
|
2015-07-14 05:53:35 +00:00
|
|
|
cfg->height = avctx->height;
|
2015-08-25 08:33:19 +00:00
|
|
|
|
2018-12-15 09:12:46 +00:00
|
|
|
if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
|
|
|
|
cfg->framerate_num = avctx->framerate.num;
|
2020-07-26 10:51:19 +00:00
|
|
|
cfg->framerate_denom = avctx->framerate.den;
|
2018-12-15 09:12:46 +00:00
|
|
|
} else {
|
|
|
|
cfg->framerate_num = avctx->time_base.den;
|
2023-05-04 09:50:48 +00:00
|
|
|
FF_DISABLE_DEPRECATION_WARNINGS
|
|
|
|
cfg->framerate_denom = avctx->time_base.num
|
|
|
|
#if FF_API_TICKS_PER_FRAME
|
|
|
|
* avctx->ticks_per_frame
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
FF_ENABLE_DEPRECATION_WARNINGS
|
2016-01-15 11:47:10 +00:00
|
|
|
}
|
2015-07-14 05:53:35 +00:00
|
|
|
cfg->target_bitrate = avctx->bit_rate;
|
2015-08-25 08:33:19 +00:00
|
|
|
cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
|
2015-07-14 05:53:35 +00:00
|
|
|
cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
|
2021-01-01 14:31:17 +00:00
|
|
|
if (avctx->bit_rate) {
|
|
|
|
cfg->rc_algorithm = KVZ_LAMBDA;
|
|
|
|
}
|
2015-07-14 05:53:35 +00:00
|
|
|
|
2023-10-02 17:20:49 +00:00
|
|
|
cfg->vui.fullrange = avctx->color_range == AVCOL_RANGE_JPEG;
|
|
|
|
cfg->vui.colorprim = avctx->color_primaries;
|
|
|
|
cfg->vui.transfer = avctx->color_trc;
|
|
|
|
cfg->vui.colormatrix = avctx->colorspace;
|
|
|
|
if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED)
|
|
|
|
cfg->vui.chroma_loc = avctx->chroma_sample_location - 1;
|
|
|
|
|
2015-07-14 05:53:35 +00:00
|
|
|
if (ctx->kvz_params) {
|
|
|
|
AVDictionary *dict = NULL;
|
|
|
|
if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
|
2024-05-17 15:02:47 +00:00
|
|
|
const AVDictionaryEntry *entry = NULL;
|
|
|
|
while ((entry = av_dict_iterate(dict, entry))) {
|
2015-07-14 05:53:35 +00:00
|
|
|
if (!api->config_parse(cfg, entry->key, entry->value)) {
|
2015-08-25 08:33:19 +00:00
|
|
|
av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
|
2015-07-14 05:53:35 +00:00
|
|
|
entry->key, entry->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-01 04:09:52 +00:00
|
|
|
av_dict_free(&dict);
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
ctx->encoder = enc = api->encoder_open(cfg);
|
2015-07-14 05:53:35 +00:00
|
|
|
if (!enc) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
|
2015-08-25 08:33:19 +00:00
|
|
|
return AVERROR_BUG;
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
|
|
|
|
kvz_data_chunk *data_out = NULL;
|
|
|
|
kvz_data_chunk *chunk = NULL;
|
|
|
|
uint32_t len_out;
|
|
|
|
uint8_t *p;
|
2015-07-14 05:53:35 +00:00
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
if (!api->encoder_headers(enc, &data_out, &len_out))
|
|
|
|
return AVERROR(ENOMEM);
|
2015-07-14 05:53:35 +00:00
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!p) {
|
|
|
|
ctx->api->chunk_free(data_out);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
avctx->extradata_size = len_out;
|
|
|
|
|
|
|
|
for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
|
|
|
|
memcpy(p, chunk->data, chunk->len);
|
|
|
|
p += chunk->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->api->chunk_free(data_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold int libkvazaar_close(AVCodecContext *avctx)
|
|
|
|
{
|
|
|
|
LibkvazaarContext *ctx = avctx->priv_data;
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
if (ctx->api) {
|
2018-12-15 11:20:41 +00:00
|
|
|
ctx->api->encoder_close(ctx->encoder);
|
|
|
|
ctx->api->config_destroy(ctx->config);
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int libkvazaar_encode(AVCodecContext *avctx,
|
|
|
|
AVPacket *avpkt,
|
|
|
|
const AVFrame *frame,
|
|
|
|
int *got_packet_ptr)
|
|
|
|
{
|
2015-08-25 08:33:19 +00:00
|
|
|
LibkvazaarContext *ctx = avctx->priv_data;
|
|
|
|
kvz_picture *input_pic = NULL;
|
2015-09-28 08:11:17 +00:00
|
|
|
kvz_picture *recon_pic = NULL;
|
2015-09-28 07:55:14 +00:00
|
|
|
kvz_frame_info frame_info;
|
2015-08-25 08:33:19 +00:00
|
|
|
kvz_data_chunk *data_out = NULL;
|
|
|
|
uint32_t len_out = 0;
|
|
|
|
int retval = 0;
|
2020-07-26 02:43:10 +00:00
|
|
|
int pict_type;
|
2015-07-14 05:53:35 +00:00
|
|
|
|
|
|
|
*got_packet_ptr = 0;
|
|
|
|
|
|
|
|
if (frame) {
|
2015-09-28 08:08:19 +00:00
|
|
|
if (frame->width != ctx->config->width ||
|
2018-12-15 11:20:41 +00:00
|
|
|
frame->height != ctx->config->height) {
|
2015-09-28 08:08:19 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Changing video dimensions during encoding is not supported. "
|
|
|
|
"(changed from %dx%d to %dx%d)\n",
|
|
|
|
ctx->config->width, ctx->config->height,
|
|
|
|
frame->width, frame->height);
|
|
|
|
retval = AVERROR_INVALIDDATA;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame->format != avctx->pix_fmt) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR,
|
|
|
|
"Changing pixel format during encoding is not supported. "
|
|
|
|
"(changed from %s to %s)\n",
|
|
|
|
av_get_pix_fmt_name(avctx->pix_fmt),
|
|
|
|
av_get_pix_fmt_name(frame->format));
|
|
|
|
retval = AVERROR_INVALIDDATA;
|
|
|
|
goto done;
|
|
|
|
}
|
2015-07-14 05:53:35 +00:00
|
|
|
|
|
|
|
// Allocate input picture for kvazaar.
|
2015-08-25 08:33:19 +00:00
|
|
|
input_pic = ctx->api->picture_alloc(frame->width, frame->height);
|
|
|
|
if (!input_pic) {
|
2015-07-14 05:53:35 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
|
|
|
|
retval = AVERROR(ENOMEM);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
// Copy pixels from frame to input_pic.
|
2015-09-28 08:27:16 +00:00
|
|
|
{
|
2021-12-06 11:19:46 +00:00
|
|
|
uint8_t *dst[4] = {
|
|
|
|
input_pic->data[0],
|
|
|
|
input_pic->data[1],
|
|
|
|
input_pic->data[2],
|
|
|
|
NULL,
|
|
|
|
};
|
2015-09-28 08:27:16 +00:00
|
|
|
int dst_linesizes[4] = {
|
|
|
|
frame->width,
|
|
|
|
frame->width / 2,
|
|
|
|
frame->width / 2,
|
|
|
|
0
|
|
|
|
};
|
2023-09-06 22:09:10 +00:00
|
|
|
av_image_copy2(dst, dst_linesizes,
|
|
|
|
frame->data, frame->linesize,
|
|
|
|
frame->format, frame->width, frame->height);
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
2015-09-28 08:11:17 +00:00
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
input_pic->pts = frame->pts;
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
retval = ctx->api->encoder_encode(ctx->encoder,
|
|
|
|
input_pic,
|
|
|
|
&data_out, &len_out,
|
|
|
|
&recon_pic, NULL,
|
|
|
|
&frame_info);
|
|
|
|
if (!retval) {
|
2015-07-14 05:53:35 +00:00
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
|
2015-08-25 08:33:19 +00:00
|
|
|
retval = AVERROR_INVALIDDATA;
|
2015-07-14 05:53:35 +00:00
|
|
|
goto done;
|
2018-12-15 11:20:41 +00:00
|
|
|
} else
|
2015-10-27 12:47:32 +00:00
|
|
|
retval = 0; /* kvazaar returns 1 on success */
|
2015-07-14 05:53:35 +00:00
|
|
|
|
|
|
|
if (data_out) {
|
|
|
|
kvz_data_chunk *chunk = NULL;
|
|
|
|
uint64_t written = 0;
|
|
|
|
|
2021-04-24 23:43:26 +00:00
|
|
|
retval = ff_get_encode_buffer(avctx, avpkt, len_out, 0);
|
2015-07-14 05:53:35 +00:00
|
|
|
if (retval < 0) {
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
|
|
|
|
av_assert0(written + chunk->len <= len_out);
|
|
|
|
memcpy(avpkt->data + written, chunk->data, chunk->len);
|
|
|
|
written += chunk->len;
|
|
|
|
}
|
2015-09-28 07:55:14 +00:00
|
|
|
|
2015-09-28 08:11:17 +00:00
|
|
|
avpkt->pts = recon_pic->pts;
|
|
|
|
avpkt->dts = recon_pic->dts;
|
2015-09-28 07:55:14 +00:00
|
|
|
avpkt->flags = 0;
|
|
|
|
// IRAP VCL NAL unit types span the range
|
|
|
|
// [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
|
|
|
|
if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
|
2018-12-15 11:20:41 +00:00
|
|
|
frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
|
2015-09-28 07:55:14 +00:00
|
|
|
avpkt->flags |= AV_PKT_FLAG_KEY;
|
|
|
|
}
|
2015-08-25 08:33:19 +00:00
|
|
|
|
2020-07-26 02:43:10 +00:00
|
|
|
switch (frame_info.slice_type) {
|
|
|
|
case KVZ_SLICE_I:
|
|
|
|
pict_type = AV_PICTURE_TYPE_I;
|
|
|
|
break;
|
|
|
|
case KVZ_SLICE_P:
|
|
|
|
pict_type = AV_PICTURE_TYPE_P;
|
|
|
|
break;
|
|
|
|
case KVZ_SLICE_B:
|
|
|
|
pict_type = AV_PICTURE_TYPE_B;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
|
|
|
|
return AVERROR_EXTERNAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
*got_packet_ptr = 1;
|
2015-07-14 05:53:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2015-08-25 08:33:19 +00:00
|
|
|
ctx->api->picture_free(input_pic);
|
2015-09-28 08:11:17 +00:00
|
|
|
ctx->api->picture_free(recon_pic);
|
2015-09-28 08:03:07 +00:00
|
|
|
ctx->api->chunk_free(data_out);
|
2015-07-14 05:53:35 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const enum AVPixelFormat pix_fmts[] = {
|
|
|
|
AV_PIX_FMT_YUV420P,
|
|
|
|
AV_PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
2015-08-25 08:33:19 +00:00
|
|
|
#define OFFSET(x) offsetof(LibkvazaarContext, x)
|
|
|
|
#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
|
2015-07-14 05:53:35 +00:00
|
|
|
static const AVOption options[] = {
|
2015-08-25 08:33:19 +00:00
|
|
|
{ "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
|
|
|
|
OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
|
2015-07-14 05:53:35 +00:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVClass class = {
|
|
|
|
.class_name = "libkvazaar",
|
2024-01-19 12:33:28 +00:00
|
|
|
.item_name = av_default_item_name,
|
2015-07-14 05:53:35 +00:00
|
|
|
.option = options,
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
};
|
|
|
|
|
2022-03-16 20:26:11 +00:00
|
|
|
static const FFCodecDefault defaults[] = {
|
2015-07-14 05:53:35 +00:00
|
|
|
{ "b", "0" },
|
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
const FFCodec ff_libkvazaar_encoder = {
|
|
|
|
.p.name = "libkvazaar",
|
2022-08-29 11:38:02 +00:00
|
|
|
CODEC_LONG_NAME("libkvazaar H.265 / HEVC"),
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.p.id = AV_CODEC_ID_HEVC,
|
|
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
|
2021-04-24 23:43:26 +00:00
|
|
|
AV_CODEC_CAP_OTHER_THREADS,
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.pix_fmts = pix_fmts,
|
2015-07-14 05:53:35 +00:00
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.priv_class = &class,
|
2015-07-14 05:53:35 +00:00
|
|
|
.priv_data_size = sizeof(LibkvazaarContext),
|
|
|
|
.defaults = defaults,
|
|
|
|
|
|
|
|
.init = libkvazaar_init,
|
2022-03-30 21:28:24 +00:00
|
|
|
FF_CODEC_ENCODE_CB(libkvazaar_encode),
|
2015-07-14 05:53:35 +00:00
|
|
|
.close = libkvazaar_close,
|
2015-08-25 08:33:19 +00:00
|
|
|
|
2022-07-09 22:05:45 +00:00
|
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
|
2021-03-09 17:00:44 +00:00
|
|
|
FF_CODEC_CAP_AUTO_THREADS,
|
2017-12-11 15:18:44 +00:00
|
|
|
|
2022-03-16 20:09:54 +00:00
|
|
|
.p.wrapper_name = "libkvazaar",
|
2015-07-14 05:53:35 +00:00
|
|
|
};
|