4 * Copyright (c) 2015 Tampere University of Technology
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "libavutil/attributes.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/error.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/log.h"
34 #include "libavutil/mem.h"
35 #include "libavutil/pixdesc.h"
36 #include "libavutil/opt.h"
41 typedef struct LibkvazaarContext {
51 static av_cold int libkvazaar_init(AVCodecContext *avctx)
53 LibkvazaarContext *const ctx = avctx->priv_data;
54 const kvz_api *const api = ctx->api = kvz_api_get(8);
55 kvz_config *cfg = NULL;
56 kvz_encoder *enc = NULL;
58 /* Kvazaar requires width and height to be multiples of eight. */
59 if (avctx->width % 8 || avctx->height % 8) {
60 av_log(avctx, AV_LOG_ERROR,
61 "Video dimensions are not a multiple of 8 (%dx%d).\n",
62 avctx->width, avctx->height);
63 return AVERROR(ENOSYS);
66 ctx->config = cfg = api->config_alloc();
68 av_log(avctx, AV_LOG_ERROR,
69 "Could not allocate kvazaar config structure.\n");
70 return AVERROR(ENOMEM);
73 if (!api->config_init(cfg)) {
74 av_log(avctx, AV_LOG_ERROR,
75 "Could not initialize kvazaar config structure.\n");
79 cfg->width = avctx->width;
80 cfg->height = avctx->height;
82 if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
83 av_log(avctx, AV_LOG_ERROR,
84 "Could not set framerate for kvazaar: integer overflow\n");
85 return AVERROR(EINVAL);
87 cfg->framerate_num = avctx->time_base.den;
88 cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
89 cfg->target_bitrate = avctx->bit_rate;
90 cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
91 cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
93 if (ctx->kvz_params) {
94 AVDictionary *dict = NULL;
95 if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
96 AVDictionaryEntry *entry = NULL;
97 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
98 if (!api->config_parse(cfg, entry->key, entry->value)) {
99 av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
100 entry->key, entry->value);
107 ctx->encoder = enc = api->encoder_open(cfg);
109 av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
113 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
114 kvz_data_chunk *data_out = NULL;
115 kvz_data_chunk *chunk = NULL;
119 if (!api->encoder_headers(enc, &data_out, &len_out))
120 return AVERROR(ENOMEM);
122 avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
124 ctx->api->chunk_free(data_out);
125 return AVERROR(ENOMEM);
128 avctx->extradata_size = len_out;
130 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
131 memcpy(p, chunk->data, chunk->len);
135 ctx->api->chunk_free(data_out);
141 static av_cold int libkvazaar_close(AVCodecContext *avctx)
143 LibkvazaarContext *ctx = avctx->priv_data;
146 ctx->api->encoder_close(ctx->encoder);
147 ctx->api->config_destroy(ctx->config);
150 if (avctx->extradata)
151 av_freep(&avctx->extradata);
156 static int libkvazaar_encode(AVCodecContext *avctx,
158 const AVFrame *frame,
161 LibkvazaarContext *ctx = avctx->priv_data;
162 kvz_picture *input_pic = NULL;
163 kvz_picture *recon_pic = NULL;
164 kvz_frame_info frame_info;
165 kvz_data_chunk *data_out = NULL;
166 uint32_t len_out = 0;
172 if (frame->width != ctx->config->width ||
173 frame->height != ctx->config->height) {
174 av_log(avctx, AV_LOG_ERROR,
175 "Changing video dimensions during encoding is not supported. "
176 "(changed from %dx%d to %dx%d)\n",
177 ctx->config->width, ctx->config->height,
178 frame->width, frame->height);
179 retval = AVERROR_INVALIDDATA;
183 if (frame->format != avctx->pix_fmt) {
184 av_log(avctx, AV_LOG_ERROR,
185 "Changing pixel format during encoding is not supported. "
186 "(changed from %s to %s)\n",
187 av_get_pix_fmt_name(avctx->pix_fmt),
188 av_get_pix_fmt_name(frame->format));
189 retval = AVERROR_INVALIDDATA;
193 // Allocate input picture for kvazaar.
194 input_pic = ctx->api->picture_alloc(frame->width, frame->height);
196 av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
197 retval = AVERROR(ENOMEM);
201 // Copy pixels from frame to input_pic.
203 int dst_linesizes[4] = {
209 av_image_copy(input_pic->data, dst_linesizes,
210 frame->data, frame->linesize,
211 frame->format, frame->width, frame->height);
214 input_pic->pts = frame->pts;
217 retval = ctx->api->encoder_encode(ctx->encoder,
223 av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
224 retval = AVERROR_INVALIDDATA;
228 retval = 0; /* kvazaar returns 1 on success */
231 kvz_data_chunk *chunk = NULL;
232 uint64_t written = 0;
234 retval = ff_alloc_packet(avpkt, len_out);
236 av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
240 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
241 av_assert0(written + chunk->len <= len_out);
242 memcpy(avpkt->data + written, chunk->data, chunk->len);
243 written += chunk->len;
246 avpkt->pts = recon_pic->pts;
247 avpkt->dts = recon_pic->dts;
249 // IRAP VCL NAL unit types span the range
250 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
251 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
252 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
253 avpkt->flags |= AV_PKT_FLAG_KEY;
260 ctx->api->picture_free(input_pic);
261 ctx->api->picture_free(recon_pic);
262 ctx->api->chunk_free(data_out);
266 static const enum AVPixelFormat pix_fmts[] = {
271 #define OFFSET(x) offsetof(LibkvazaarContext, x)
272 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
273 static const AVOption options[] = {
274 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
275 OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
279 static const AVClass class = {
280 .class_name = "libkvazaar",
281 .item_name = av_default_item_name,
283 .version = LIBAVUTIL_VERSION_INT,
286 static const AVCodecDefault defaults[] = {
291 AVCodec ff_libkvazaar_encoder = {
292 .name = "libkvazaar",
293 .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
294 .type = AVMEDIA_TYPE_VIDEO,
295 .id = AV_CODEC_ID_HEVC,
296 .capabilities = AV_CODEC_CAP_DELAY,
297 .pix_fmts = pix_fmts,
299 .priv_class = &class,
300 .priv_data_size = sizeof(LibkvazaarContext),
301 .defaults = defaults,
303 .init = libkvazaar_init,
304 .encode2 = libkvazaar_encode,
305 .close = libkvazaar_close,
307 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,