FFmpeg  4.4.5
libkvazaar.c
Go to the documentation of this file.
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 "internal.h"
40 #include "packet_internal.h"
41 
42 typedef struct LibkvazaarContext {
43  const AVClass *class;
44 
45  const kvz_api *api;
46  kvz_encoder *encoder;
47  kvz_config *config;
48 
49  char *kvz_params;
51 
53 {
54  LibkvazaarContext *const ctx = avctx->priv_data;
55  const kvz_api *const api = ctx->api = kvz_api_get(8);
56  kvz_config *cfg = NULL;
57  kvz_encoder *enc = NULL;
58 
59  /* Kvazaar requires width and height to be multiples of eight. */
60  if (avctx->width % 8 || avctx->height % 8) {
61  av_log(avctx, AV_LOG_ERROR,
62  "Video dimensions are not a multiple of 8 (%dx%d).\n",
63  avctx->width, avctx->height);
64  return AVERROR(ENOSYS);
65  }
66 
67  ctx->config = cfg = api->config_alloc();
68  if (!cfg) {
69  av_log(avctx, AV_LOG_ERROR,
70  "Could not allocate kvazaar config structure.\n");
71  return AVERROR(ENOMEM);
72  }
73 
74  if (!api->config_init(cfg)) {
75  av_log(avctx, AV_LOG_ERROR,
76  "Could not initialize kvazaar config structure.\n");
77  return AVERROR_BUG;
78  }
79 
80  cfg->width = avctx->width;
81  cfg->height = avctx->height;
82 
83  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
84  cfg->framerate_num = avctx->framerate.num;
85  cfg->framerate_denom = avctx->framerate.den;
86  } else {
87  if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
88  av_log(avctx, AV_LOG_ERROR,
89  "Could not set framerate for kvazaar: integer overflow\n");
90  return AVERROR(EINVAL);
91  }
92  cfg->framerate_num = avctx->time_base.den;
93  cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
94  }
95  cfg->target_bitrate = avctx->bit_rate;
96  cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
97  cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
98  if (avctx->bit_rate) {
99  cfg->rc_algorithm = KVZ_LAMBDA;
100  }
101 
102  if (ctx->kvz_params) {
103  AVDictionary *dict = NULL;
104  if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
105  AVDictionaryEntry *entry = NULL;
106  while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
107  if (!api->config_parse(cfg, entry->key, entry->value)) {
108  av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
109  entry->key, entry->value);
110  }
111  }
112  }
113  av_dict_free(&dict);
114  }
115 
116  ctx->encoder = enc = api->encoder_open(cfg);
117  if (!enc) {
118  av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
119  return AVERROR_BUG;
120  }
121 
122  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
123  kvz_data_chunk *data_out = NULL;
124  kvz_data_chunk *chunk = NULL;
125  uint32_t len_out;
126  uint8_t *p;
127 
128  if (!api->encoder_headers(enc, &data_out, &len_out))
129  return AVERROR(ENOMEM);
130 
131  avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
132  if (!p) {
133  ctx->api->chunk_free(data_out);
134  return AVERROR(ENOMEM);
135  }
136 
137  avctx->extradata_size = len_out;
138 
139  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
140  memcpy(p, chunk->data, chunk->len);
141  p += chunk->len;
142  }
143 
144  ctx->api->chunk_free(data_out);
145  }
146 
147  return 0;
148 }
149 
151 {
152  LibkvazaarContext *ctx = avctx->priv_data;
153 
154  if (ctx->api) {
155  ctx->api->encoder_close(ctx->encoder);
156  ctx->api->config_destroy(ctx->config);
157  }
158 
159  if (avctx->extradata)
160  av_freep(&avctx->extradata);
161 
162  return 0;
163 }
164 
166  AVPacket *avpkt,
167  const AVFrame *frame,
168  int *got_packet_ptr)
169 {
170  LibkvazaarContext *ctx = avctx->priv_data;
171  kvz_picture *input_pic = NULL;
172  kvz_picture *recon_pic = NULL;
173  kvz_frame_info frame_info;
174  kvz_data_chunk *data_out = NULL;
175  uint32_t len_out = 0;
176  int retval = 0;
177  int pict_type;
178 
179  *got_packet_ptr = 0;
180 
181  if (frame) {
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;
190  goto done;
191  }
192 
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",
199  retval = AVERROR_INVALIDDATA;
200  goto done;
201  }
202 
203  // Allocate input picture for kvazaar.
204  input_pic = ctx->api->picture_alloc(frame->width, frame->height);
205  if (!input_pic) {
206  av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
207  retval = AVERROR(ENOMEM);
208  goto done;
209  }
210 
211  // Copy pixels from frame to input_pic.
212  {
213  uint8_t *dst[4] = {
214  input_pic->data[0],
215  input_pic->data[1],
216  input_pic->data[2],
217  NULL,
218  };
219  int dst_linesizes[4] = {
220  frame->width,
221  frame->width / 2,
222  frame->width / 2,
223  0
224  };
225  av_image_copy(dst, dst_linesizes,
226  (const uint8_t **)frame->data, frame->linesize,
228  }
229 
230  input_pic->pts = frame->pts;
231  }
232 
233  retval = ctx->api->encoder_encode(ctx->encoder,
234  input_pic,
235  &data_out, &len_out,
236  &recon_pic, NULL,
237  &frame_info);
238  if (!retval) {
239  av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
240  retval = AVERROR_INVALIDDATA;
241  goto done;
242  } else
243  retval = 0; /* kvazaar returns 1 on success */
244 
245  if (data_out) {
246  kvz_data_chunk *chunk = NULL;
247  uint64_t written = 0;
248 
249  retval = ff_alloc_packet2(avctx, avpkt, len_out, len_out);
250  if (retval < 0) {
251  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
252  goto done;
253  }
254 
255  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
256  av_assert0(written + chunk->len <= len_out);
257  memcpy(avpkt->data + written, chunk->data, chunk->len);
258  written += chunk->len;
259  }
260 
261  avpkt->pts = recon_pic->pts;
262  avpkt->dts = recon_pic->dts;
263  avpkt->flags = 0;
264  // IRAP VCL NAL unit types span the range
265  // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
266  if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
267  frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
268  avpkt->flags |= AV_PKT_FLAG_KEY;
269  }
270 
271  switch (frame_info.slice_type) {
272  case KVZ_SLICE_I:
273  pict_type = AV_PICTURE_TYPE_I;
274  break;
275  case KVZ_SLICE_P:
276  pict_type = AV_PICTURE_TYPE_P;
277  break;
278  case KVZ_SLICE_B:
279  pict_type = AV_PICTURE_TYPE_B;
280  break;
281  default:
282  av_log(avctx, AV_LOG_ERROR, "Unknown picture type encountered.\n");
283  return AVERROR_EXTERNAL;
284  }
285 #if FF_API_CODED_FRAME
287  avctx->coded_frame->pict_type = pict_type;
289 #endif
290 
291  ff_side_data_set_encoder_stats(avpkt, frame_info.qp * FF_QP2LAMBDA, NULL, 0, pict_type);
292 
293 #if FF_API_CODED_FRAME
295  avctx->coded_frame->quality = frame_info.qp * FF_QP2LAMBDA;
297 #endif
298 
299  *got_packet_ptr = 1;
300  }
301 
302 done:
303  ctx->api->picture_free(input_pic);
304  ctx->api->picture_free(recon_pic);
305  ctx->api->chunk_free(data_out);
306  return retval;
307 }
308 
309 static const enum AVPixelFormat pix_fmts[] = {
312 };
313 
314 #define OFFSET(x) offsetof(LibkvazaarContext, x)
315 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
316 static const AVOption options[] = {
317  { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
318  OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
319  { NULL },
320 };
321 
322 static const AVClass class = {
323  .class_name = "libkvazaar",
324  .item_name = av_default_item_name,
325  .option = options,
327 };
328 
329 static const AVCodecDefault defaults[] = {
330  { "b", "0" },
331  { NULL },
332 };
333 
335  .name = "libkvazaar",
336  .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
337  .type = AVMEDIA_TYPE_VIDEO,
338  .id = AV_CODEC_ID_HEVC,
340  .pix_fmts = pix_fmts,
341 
342  .priv_class = &class,
343  .priv_data_size = sizeof(LibkvazaarContext),
344  .defaults = defaults,
345 
347  .encode2 = libkvazaar_encode,
348  .close = libkvazaar_close,
349 
352 
353  .wrapper_name = "libkvazaar",
354 };
Macro definitions for various function/variable attributes.
#define av_cold
Definition: attributes.h:88
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:820
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define NULL
Definition: coverity.c:32
static AVFrame * frame
Public dictionary API.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
error code definitions
@ AV_OPT_TYPE_STRING
Definition: opt.h:229
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:122
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:77
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:329
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:215
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition: dict.c:180
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:422
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
misc image utilities
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:41
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:80
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:329
static const AVOption options[]
Definition: libkvazaar.c:316
#define VE
Definition: libkvazaar.c:315
static av_cold int libkvazaar_init(AVCodecContext *avctx)
Definition: libkvazaar.c:52
static av_cold int libkvazaar_close(AVCodecContext *avctx)
Definition: libkvazaar.c:150
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:309
AVCodec ff_libkvazaar_encoder
Definition: libkvazaar.c:334
#define OFFSET(x)
Definition: libkvazaar.c:314
static int libkvazaar_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libkvazaar.c:165
Memory handling functions.
AVOptions.
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
Describe the class of an AVClass context structure.
Definition: log.h:67
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
AVRational framerate
Definition: avcodec.h:2071
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:915
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1764
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:668
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:659
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
int extradata_size
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
char * key
Definition: dict.h:82
char * value
Definition: dict.h:83
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
int height
Definition: frame.h:376
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
Definition: frame.h:441
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
const kvz_api * api
Definition: libkvazaar.c:45
kvz_config * config
Definition: libkvazaar.c:47
kvz_encoder * encoder
Definition: libkvazaar.c:46
#define av_freep(p)
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48