1 /*
2 * libkvazaar encoder
3 *
4 * Copyright (c) 2015 Tampere University of Technology
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #include <kvazaar.h>
24 #include <stdint.h>
25 #include <string.h>
26
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"
37
38 #include "avcodec.h"
39 #include "codec_internal.h"
40 #include "encode.h"
41 #include "packet_internal.h"
42
43 typedef struct LibkvazaarContext {
44 const AVClass *class;
45
46 const kvz_api *api;
47 kvz_encoder *encoder;
48 kvz_config *config;
49
50 char *kvz_params;
51 } LibkvazaarContext;
52
libkvazaar_init(AVCodecContext * avctx)53 static av_cold int libkvazaar_init(AVCodecContext *avctx)
54 {
55 LibkvazaarContext *const ctx = avctx->priv_data;
56 const kvz_api *const api = ctx->api = kvz_api_get(8);
57 kvz_config *cfg = NULL;
58 kvz_encoder *enc = NULL;
59
60 /* Kvazaar requires width and height to be multiples of eight. */
61 if (avctx->width % 8 || avctx->height % 8) {
62 av_log(avctx, AV_LOG_ERROR,
63 "Video dimensions are not a multiple of 8 (%dx%d).\n",
64 avctx->width, avctx->height);
65 return AVERROR(ENOSYS);
66 }
67
68 ctx->config = cfg = api->config_alloc();
69 if (!cfg) {
70 av_log(avctx, AV_LOG_ERROR,
71 "Could not allocate kvazaar config structure.\n");
72 return AVERROR(ENOMEM);
73 }
74
75 if (!api->config_init(cfg)) {
76 av_log(avctx, AV_LOG_ERROR,
77 "Could not initialize kvazaar config structure.\n");
78 return AVERROR_BUG;
79 }
80
81 cfg->width = avctx->width;
82 cfg->height = avctx->height;
83
84 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
85 cfg->framerate_num = avctx->framerate.num;
86 cfg->framerate_denom = avctx->framerate.den;
87 } else {
88 if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
89 av_log(avctx, AV_LOG_ERROR,
90 "Could not set framerate for kvazaar: integer overflow\n");
91 return AVERROR(EINVAL);
92 }
93 cfg->framerate_num = avctx->time_base.den;
94 cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
95 }
96 cfg->target_bitrate = avctx->bit_rate;
97 cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
98 cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
99 if (avctx->bit_rate) {
100 cfg->rc_algorithm = KVZ_LAMBDA;
101 }
102
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);
111 }
112 }
113 }
114 av_dict_free(&dict);
115 }
116
117 ctx->encoder = enc = api->encoder_open(cfg);
118 if (!enc) {
119 av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
120 return AVERROR_BUG;
121 }
122
123 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
124 kvz_data_chunk *data_out = NULL;
125 kvz_data_chunk *chunk = NULL;
126 uint32_t len_out;
127 uint8_t *p;
128
129 if (!api->encoder_headers(enc, &data_out, &len_out))
130 return AVERROR(ENOMEM);
131
132 avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
133 if (!p) {
134 ctx->api->chunk_free(data_out);
135 return AVERROR(ENOMEM);
136 }
137
138 avctx->extradata_size = len_out;
139
140 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
141 memcpy(p, chunk->data, chunk->len);
142 p += chunk->len;
143 }
144
145 ctx->api->chunk_free(data_out);
146 }
147
148 return 0;
149 }
150
libkvazaar_close(AVCodecContext * avctx)151 static av_cold int libkvazaar_close(AVCodecContext *avctx)
152 {
153 LibkvazaarContext *ctx = avctx->priv_data;
154
155 if (ctx->api) {
156 ctx->api->encoder_close(ctx->encoder);
157 ctx->api->config_destroy(ctx->config);
158 }
159
160 return 0;
161 }
162
libkvazaar_encode(AVCodecContext * avctx,AVPacket * avpkt,const AVFrame * frame,int * got_packet_ptr)163 static int libkvazaar_encode(AVCodecContext *avctx,
164 AVPacket *avpkt,
165 const AVFrame *frame,
166 int *got_packet_ptr)
167 {
168 LibkvazaarContext *ctx = avctx->priv_data;
169 kvz_picture *input_pic = NULL;
170 kvz_picture *recon_pic = NULL;
171 kvz_frame_info frame_info;
172 kvz_data_chunk *data_out = NULL;
173 uint32_t len_out = 0;
174 int retval = 0;
175 int pict_type;
176
177 *got_packet_ptr = 0;
178
179 if (frame) {
180 if (frame->width != ctx->config->width ||
181 frame->height != ctx->config->height) {
182 av_log(avctx, AV_LOG_ERROR,
183 "Changing video dimensions during encoding is not supported. "
184 "(changed from %dx%d to %dx%d)\n",
185 ctx->config->width, ctx->config->height,
186 frame->width, frame->height);
187 retval = AVERROR_INVALIDDATA;
188 goto done;
189 }
190
191 if (frame->format != avctx->pix_fmt) {
192 av_log(avctx, AV_LOG_ERROR,
193 "Changing pixel format during encoding is not supported. "
194 "(changed from %s to %s)\n",
195 av_get_pix_fmt_name(avctx->pix_fmt),
196 av_get_pix_fmt_name(frame->format));
197 retval = AVERROR_INVALIDDATA;
198 goto done;
199 }
200
201 // Allocate input picture for kvazaar.
202 input_pic = ctx->api->picture_alloc(frame->width, frame->height);
203 if (!input_pic) {
204 av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
205 retval = AVERROR(ENOMEM);
206 goto done;
207 }
208
209 // Copy pixels from frame to input_pic.
210 {
211 uint8_t *dst[4] = {
212 input_pic->data[0],
213 input_pic->data[1],
214 input_pic->data[2],
215 NULL,
216 };
217 int dst_linesizes[4] = {
218 frame->width,
219 frame->width / 2,
220 frame->width / 2,
221 0
222 };
223 av_image_copy(dst, dst_linesizes,
224 (const uint8_t **)frame->data, frame->linesize,
225 frame->format, frame->width, frame->height);
226 }
227
228 input_pic->pts = frame->pts;
229 }
230
231 retval = ctx->api->encoder_encode(ctx->encoder,
232 input_pic,
233 &data_out, &len_out,
234 &recon_pic, NULL,
235 &frame_info);
236 if (!retval) {
237 av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
238 retval = AVERROR_INVALIDDATA;
239 goto done;
240 } else
241 retval = 0; /* kvazaar returns 1 on success */
242
243 if (data_out) {
244 kvz_data_chunk *chunk = NULL;
245 uint64_t written = 0;
246
247 retval = ff_get_encode_buffer(avctx, avpkt, len_out, 0);
248 if (retval < 0) {
249 av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
250 goto done;
251 }
252
253 for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
254 av_assert0(written + chunk->len <= len_out);
255 memcpy(avpkt->data + written, chunk->data, chunk->len);
256 written += chunk->len;
257 }
258
259 avpkt->pts = recon_pic->pts;
260 avpkt->dts = recon_pic->dts;
261 avpkt->flags = 0;
262 // IRAP VCL NAL unit types span the range
263 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
264 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
265 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
266 avpkt->flags |= AV_PKT_FLAG_KEY;
267 }
268
269 switch (frame_info.slice_type) {
270 case KVZ_SLICE_I:
271 pict_type = AV_PICTURE_TYPE_I;
272 break;
273 case KVZ_SLICE_P:
274 pict_type = AV_PICTURE_TYPE_P;
275 break;
276 case KVZ_SLICE_B:
277 pict_type = AV_PICTURE_TYPE_B;
278 break;
279 default:
280 av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
281 return AVERROR_EXTERNAL;
282 }
283
284 ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
285
286 *got_packet_ptr = 1;
287 }
288
289 done:
290 ctx->api->picture_free(input_pic);
291 ctx->api->picture_free(recon_pic);
292 ctx->api->chunk_free(data_out);
293 return retval;
294 }
295
296 static const enum AVPixelFormat pix_fmts[] = {
297 AV_PIX_FMT_YUV420P,
298 AV_PIX_FMT_NONE
299 };
300
301 #define OFFSET(x) offsetof(LibkvazaarContext, x)
302 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
303 static const AVOption options[] = {
304 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
305 OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
306 { NULL },
307 };
308
309 static const AVClass class = {
310 .class_name = "libkvazaar",
311 .item_name = av_default_item_name,
312 .option = options,
313 .version = LIBAVUTIL_VERSION_INT,
314 };
315
316 static const FFCodecDefault defaults[] = {
317 { "b", "0" },
318 { NULL },
319 };
320
321 const FFCodec ff_libkvazaar_encoder = {
322 .p.name = "libkvazaar",
323 .p.long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
324 .p.type = AVMEDIA_TYPE_VIDEO,
325 .p.id = AV_CODEC_ID_HEVC,
326 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
327 AV_CODEC_CAP_OTHER_THREADS,
328 .p.pix_fmts = pix_fmts,
329
330 .p.priv_class = &class,
331 .priv_data_size = sizeof(LibkvazaarContext),
332 .defaults = defaults,
333
334 .init = libkvazaar_init,
335 FF_CODEC_ENCODE_CB(libkvazaar_encode),
336 .close = libkvazaar_close,
337
338 .caps_internal = FF_CODEC_CAP_INIT_THREADSAFE | FF_CODEC_CAP_INIT_CLEANUP |
339 FF_CODEC_CAP_AUTO_THREADS,
340
341 .p.wrapper_name = "libkvazaar",
342 };
343