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
26 #include "libavutil/avassert.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/opt.h"
32 typedef struct LibkvazaarContext {
42 static av_cold int libkvazaar_init(AVCodecContext *avctx)
45 kvz_config *cfg = NULL;
46 kvz_encoder *enc = NULL;
47 const kvz_api *const api = kvz_api_get(8);
49 LibkvazaarContext *const ctx = avctx->priv_data;
51 // Kvazaar requires width and height to be multiples of eight.
52 if (avctx->width % 8 || avctx->height % 8) {
53 av_log(avctx, AV_LOG_ERROR, "Video dimensions are not a multiple of 8.\n");
54 retval = AVERROR_INVALIDDATA;
58 cfg = api->config_alloc();
60 av_log(avctx, AV_LOG_ERROR, "Could not allocate kvazaar config structure.\n");
61 retval = AVERROR(ENOMEM);
65 if (!api->config_init(cfg)) {
66 av_log(avctx, AV_LOG_ERROR, "Could not initialize kvazaar config structure.\n");
67 retval = AVERROR_EXTERNAL;
71 cfg->width = avctx->width;
72 cfg->height = avctx->height;
74 (double)(avctx->time_base.num * avctx->ticks_per_frame) / avctx->time_base.den;
75 cfg->threads = avctx->thread_count;
76 cfg->target_bitrate = avctx->bit_rate;
77 cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
78 cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
80 if (ctx->kvz_params) {
81 AVDictionary *dict = NULL;
82 if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
83 AVDictionaryEntry *entry = NULL;
84 while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
85 if (!api->config_parse(cfg, entry->key, entry->value)) {
86 av_log(avctx, AV_LOG_WARNING,
87 "Invalid option: %s=%s.\n",
88 entry->key, entry->value);
95 enc = api->encoder_open(cfg);
97 av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
98 retval = AVERROR_EXTERNAL;
109 if (cfg) api->config_destroy(cfg);
110 if (enc) api->encoder_close(enc);
115 static av_cold int libkvazaar_close(AVCodecContext *avctx)
117 LibkvazaarContext *ctx = avctx->priv_data;
118 if (!ctx->api) return 0;
121 ctx->api->encoder_close(ctx->encoder);
126 ctx->api->config_destroy(ctx->config);
133 static int libkvazaar_encode(AVCodecContext *avctx,
135 const AVFrame *frame,
139 kvz_picture *img_in = NULL;
140 kvz_data_chunk *data_out = NULL;
141 uint32_t len_out = 0;
142 LibkvazaarContext *ctx = avctx->priv_data;
149 av_assert0(frame->width == ctx->config->width);
150 av_assert0(frame->height == ctx->config->height);
151 av_assert0(frame->format == avctx->pix_fmt);
153 // Allocate input picture for kvazaar.
154 img_in = ctx->api->picture_alloc(frame->width, frame->height);
156 av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
157 retval = AVERROR(ENOMEM);
161 // Copy pixels from frame to img_in.
162 for (i = 0; i < 3; ++i) {
163 uint8_t *dst = img_in->data[i];
164 uint8_t *src = frame->data[i];
165 int width = (i == 0) ? frame->width : (frame->width / 2);
166 int height = (i == 0) ? frame->height : (frame->height / 2);
168 for (y = 0; y < height; ++y) {
169 memcpy(dst, src, width);
170 src += frame->linesize[i];
176 if (!ctx->api->encoder_encode(ctx->encoder, img_in, &data_out, &len_out, NULL)) {
177 av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
178 retval = AVERROR_EXTERNAL;
183 kvz_data_chunk *chunk = NULL;
184 uint64_t written = 0;
186 retval = ff_alloc_packet(avpkt, len_out);
188 av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
192 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
193 av_assert0(written + chunk->len <= len_out);
194 memcpy(avpkt->data + written, chunk->data, chunk->len);
195 written += chunk->len;
199 ctx->api->chunk_free(data_out);
204 if (img_in) ctx->api->picture_free(img_in);
205 if (data_out) ctx->api->chunk_free(data_out);
209 static const enum AVPixelFormat pix_fmts[] = {
214 static const AVOption options[] = {
215 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
216 offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
217 AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM },
221 static const AVClass class = {
222 .class_name = "libkvazaar",
223 .item_name = av_default_item_name,
225 .version = LIBAVUTIL_VERSION_INT,
228 static const AVCodecDefault defaults[] = {
233 AVCodec ff_libkvazaar_encoder = {
234 .name = "libkvazaar",
235 .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
236 .type = AVMEDIA_TYPE_VIDEO,
237 .id = AV_CODEC_ID_HEVC,
238 .capabilities = AV_CODEC_CAP_DELAY,
239 .pix_fmts = pix_fmts,
241 .priv_class = &class,
242 .priv_data_size = sizeof(LibkvazaarContext),
243 .defaults = defaults,
245 .init = libkvazaar_init,
246 .encode2 = libkvazaar_encode,
247 .close = libkvazaar_close,