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->framerate.num > 0 && avctx->framerate.den > 0) {
83 if (avctx->ticks_per_frame > INT_MAX / avctx->framerate.den) {
84 av_log(avctx, AV_LOG_ERROR,
85 "Could not set framerate for kvazaar: integer overflow\n");
86 return AVERROR(EINVAL);
88 cfg->framerate_num = avctx->framerate.num;
89 cfg->framerate_denom = avctx->time_base.den * avctx->ticks_per_frame;
91 if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
92 av_log(avctx, AV_LOG_ERROR,
93 "Could not set framerate for kvazaar: integer overflow\n");
94 return AVERROR(EINVAL);
96 cfg->framerate_num = avctx->time_base.den;
97 cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
99 cfg->target_bitrate = avctx->bit_rate;
100 cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
101 cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
103 if (ctx->kvz_params) {
104 AVDictionary *dict = NULL;
105 if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
106 AVDictionaryEntry *entry = NULL;
107 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
108 if (!api->config_parse(cfg, entry->key, entry->value)) {
109 av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
110 entry->key, entry->value);
117 ctx->encoder = enc = api->encoder_open(cfg);
119 av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
123 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
124 kvz_data_chunk *data_out = NULL;
125 kvz_data_chunk *chunk = NULL;
129 if (!api->encoder_headers(enc, &data_out, &len_out))
130 return AVERROR(ENOMEM);
132 avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
134 ctx->api->chunk_free(data_out);
135 return AVERROR(ENOMEM);
138 avctx->extradata_size = len_out;
140 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
141 memcpy(p, chunk->data, chunk->len);
145 ctx->api->chunk_free(data_out);
151 static av_cold int libkvazaar_close(AVCodecContext *avctx)
153 LibkvazaarContext *ctx = avctx->priv_data;
156 ctx->api->encoder_close(ctx->encoder);
157 ctx->api->config_destroy(ctx->config);
160 if (avctx->extradata)
161 av_freep(&avctx->extradata);
166 static int libkvazaar_encode(AVCodecContext *avctx,
168 const AVFrame *frame,
171 LibkvazaarContext *ctx = avctx->priv_data;
172 kvz_picture *input_pic = NULL;
173 kvz_picture *recon_pic = NULL;
174 kvz_frame_info frame_info;
175 kvz_data_chunk *data_out = NULL;
176 uint32_t len_out = 0;
182 if (frame->width != ctx->config->width ||
183 frame->height != ctx->config->height) {
184 av_log(avctx, AV_LOG_ERROR,
185 "Changing video dimensions during encoding is not supported. "
186 "(changed from %dx%d to %dx%d)\n",
187 ctx->config->width, ctx->config->height,
188 frame->width, frame->height);
189 retval = AVERROR_INVALIDDATA;
193 if (frame->format != avctx->pix_fmt) {
194 av_log(avctx, AV_LOG_ERROR,
195 "Changing pixel format during encoding is not supported. "
196 "(changed from %s to %s)\n",
197 av_get_pix_fmt_name(avctx->pix_fmt),
198 av_get_pix_fmt_name(frame->format));
199 retval = AVERROR_INVALIDDATA;
203 // Allocate input picture for kvazaar.
204 input_pic = ctx->api->picture_alloc(frame->width, frame->height);
206 av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
207 retval = AVERROR(ENOMEM);
211 // Copy pixels from frame to input_pic.
213 int dst_linesizes[4] = {
219 av_image_copy(input_pic->data, dst_linesizes,
220 (const uint8_t **)frame->data, frame->linesize,
221 frame->format, frame->width, frame->height);
224 input_pic->pts = frame->pts;
227 retval = ctx->api->encoder_encode(ctx->encoder,
233 av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
234 retval = AVERROR_INVALIDDATA;
237 retval = 0; /* kvazaar returns 1 on success */
240 kvz_data_chunk *chunk = NULL;
241 uint64_t written = 0;
243 retval = ff_alloc_packet2(avctx, avpkt, len_out, len_out);
245 av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
249 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
250 av_assert0(written + chunk->len <= len_out);
251 memcpy(avpkt->data + written, chunk->data, chunk->len);
252 written += chunk->len;
255 avpkt->pts = recon_pic->pts;
256 avpkt->dts = recon_pic->dts;
258 // IRAP VCL NAL unit types span the range
259 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
260 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
261 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
262 avpkt->flags |= AV_PKT_FLAG_KEY;
269 ctx->api->picture_free(input_pic);
270 ctx->api->picture_free(recon_pic);
271 ctx->api->chunk_free(data_out);
275 static const enum AVPixelFormat pix_fmts[] = {
280 #define OFFSET(x) offsetof(LibkvazaarContext, x)
281 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
282 static const AVOption options[] = {
283 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
284 OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
288 static const AVClass class = {
289 .class_name = "libkvazaar",
290 .item_name = av_default_item_name,
292 .version = LIBAVUTIL_VERSION_INT,
295 static const AVCodecDefault defaults[] = {
300 AVCodec ff_libkvazaar_encoder = {
301 .name = "libkvazaar",
302 .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
303 .type = AVMEDIA_TYPE_VIDEO,
304 .id = AV_CODEC_ID_HEVC,
305 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
306 .pix_fmts = pix_fmts,
308 .priv_class = &class,
309 .priv_data_size = sizeof(LibkvazaarContext),
310 .defaults = defaults,
312 .init = libkvazaar_init,
313 .encode2 = libkvazaar_encode,
314 .close = libkvazaar_close,
316 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP,
318 .wrapper_name = "libkvazaar",