FFmpeg
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 
41 typedef struct LibkvazaarContext {
42  const AVClass *class;
43 
44  const kvz_api *api;
45  kvz_encoder *encoder;
46  kvz_config *config;
47 
48  char *kvz_params;
50 
52 {
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;
57 
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);
64  }
65 
66  ctx->config = cfg = api->config_alloc();
67  if (!cfg) {
68  av_log(avctx, AV_LOG_ERROR,
69  "Could not allocate kvazaar config structure.\n");
70  return AVERROR(ENOMEM);
71  }
72 
73  if (!api->config_init(cfg)) {
74  av_log(avctx, AV_LOG_ERROR,
75  "Could not initialize kvazaar config structure.\n");
76  return AVERROR_BUG;
77  }
78 
79  cfg->width = avctx->width;
80  cfg->height = avctx->height;
81 
82  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
83  cfg->framerate_num = avctx->framerate.num;
84  cfg->framerate_denom = avctx->framerate.den;
85  } else {
86  if (avctx->ticks_per_frame > INT_MAX / avctx->time_base.num) {
87  av_log(avctx, AV_LOG_ERROR,
88  "Could not set framerate for kvazaar: integer overflow\n");
89  return AVERROR(EINVAL);
90  }
91  cfg->framerate_num = avctx->time_base.den;
92  cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
93  }
94  cfg->target_bitrate = avctx->bit_rate;
95  cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
96  cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
97 
98  if (ctx->kvz_params) {
99  AVDictionary *dict = NULL;
100  if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
101  AVDictionaryEntry *entry = NULL;
102  while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
103  if (!api->config_parse(cfg, entry->key, entry->value)) {
104  av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
105  entry->key, entry->value);
106  }
107  }
108  }
109  av_dict_free(&dict);
110  }
111 
112  ctx->encoder = enc = api->encoder_open(cfg);
113  if (!enc) {
114  av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
115  return AVERROR_BUG;
116  }
117 
118  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
119  kvz_data_chunk *data_out = NULL;
120  kvz_data_chunk *chunk = NULL;
121  uint32_t len_out;
122  uint8_t *p;
123 
124  if (!api->encoder_headers(enc, &data_out, &len_out))
125  return AVERROR(ENOMEM);
126 
127  avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
128  if (!p) {
129  ctx->api->chunk_free(data_out);
130  return AVERROR(ENOMEM);
131  }
132 
133  avctx->extradata_size = len_out;
134 
135  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
136  memcpy(p, chunk->data, chunk->len);
137  p += chunk->len;
138  }
139 
140  ctx->api->chunk_free(data_out);
141  }
142 
143  return 0;
144 }
145 
147 {
148  LibkvazaarContext *ctx = avctx->priv_data;
149 
150  if (ctx->api) {
151  ctx->api->encoder_close(ctx->encoder);
152  ctx->api->config_destroy(ctx->config);
153  }
154 
155  if (avctx->extradata)
156  av_freep(&avctx->extradata);
157 
158  return 0;
159 }
160 
162  AVPacket *avpkt,
163  const AVFrame *frame,
164  int *got_packet_ptr)
165 {
166  LibkvazaarContext *ctx = avctx->priv_data;
167  kvz_picture *input_pic = NULL;
168  kvz_picture *recon_pic = NULL;
169  kvz_frame_info frame_info;
170  kvz_data_chunk *data_out = NULL;
171  uint32_t len_out = 0;
172  int retval = 0;
173 
174  *got_packet_ptr = 0;
175 
176  if (frame) {
177  if (frame->width != ctx->config->width ||
178  frame->height != ctx->config->height) {
179  av_log(avctx, AV_LOG_ERROR,
180  "Changing video dimensions during encoding is not supported. "
181  "(changed from %dx%d to %dx%d)\n",
182  ctx->config->width, ctx->config->height,
183  frame->width, frame->height);
184  retval = AVERROR_INVALIDDATA;
185  goto done;
186  }
187 
188  if (frame->format != avctx->pix_fmt) {
189  av_log(avctx, AV_LOG_ERROR,
190  "Changing pixel format during encoding is not supported. "
191  "(changed from %s to %s)\n",
193  av_get_pix_fmt_name(frame->format));
194  retval = AVERROR_INVALIDDATA;
195  goto done;
196  }
197 
198  // Allocate input picture for kvazaar.
199  input_pic = ctx->api->picture_alloc(frame->width, frame->height);
200  if (!input_pic) {
201  av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
202  retval = AVERROR(ENOMEM);
203  goto done;
204  }
205 
206  // Copy pixels from frame to input_pic.
207  {
208  int dst_linesizes[4] = {
209  frame->width,
210  frame->width / 2,
211  frame->width / 2,
212  0
213  };
214  av_image_copy(input_pic->data, dst_linesizes,
215  (const uint8_t **)frame->data, frame->linesize,
216  frame->format, frame->width, frame->height);
217  }
218 
219  input_pic->pts = frame->pts;
220  }
221 
222  retval = ctx->api->encoder_encode(ctx->encoder,
223  input_pic,
224  &data_out, &len_out,
225  &recon_pic, NULL,
226  &frame_info);
227  if (!retval) {
228  av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
229  retval = AVERROR_INVALIDDATA;
230  goto done;
231  } else
232  retval = 0; /* kvazaar returns 1 on success */
233 
234  if (data_out) {
235  kvz_data_chunk *chunk = NULL;
236  uint64_t written = 0;
237 
238  retval = ff_alloc_packet2(avctx, avpkt, len_out, len_out);
239  if (retval < 0) {
240  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
241  goto done;
242  }
243 
244  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
245  av_assert0(written + chunk->len <= len_out);
246  memcpy(avpkt->data + written, chunk->data, chunk->len);
247  written += chunk->len;
248  }
249 
250  avpkt->pts = recon_pic->pts;
251  avpkt->dts = recon_pic->dts;
252  avpkt->flags = 0;
253  // IRAP VCL NAL unit types span the range
254  // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
255  if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
256  frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
257  avpkt->flags |= AV_PKT_FLAG_KEY;
258  }
259 
260  *got_packet_ptr = 1;
261  }
262 
263 done:
264  ctx->api->picture_free(input_pic);
265  ctx->api->picture_free(recon_pic);
266  ctx->api->chunk_free(data_out);
267  return retval;
268 }
269 
270 static const enum AVPixelFormat pix_fmts[] = {
273 };
274 
275 #define OFFSET(x) offsetof(LibkvazaarContext, x)
276 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
277 static const AVOption options[] = {
278  { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
279  OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
280  { NULL },
281 };
282 
283 static const AVClass class = {
284  .class_name = "libkvazaar",
285  .item_name = av_default_item_name,
286  .option = options,
288 };
289 
290 static const AVCodecDefault defaults[] = {
291  { "b", "0" },
292  { NULL },
293 };
294 
296  .name = "libkvazaar",
297  .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
298  .type = AVMEDIA_TYPE_VIDEO,
299  .id = AV_CODEC_ID_HEVC,
301  .pix_fmts = pix_fmts,
302 
303  .priv_class = &class,
304  .priv_data_size = sizeof(LibkvazaarContext),
305  .defaults = defaults,
306 
308  .encode2 = libkvazaar_encode,
309  .close = libkvazaar_close,
310 
312 
313  .wrapper_name = "libkvazaar",
314 };
#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:48
#define NULL
Definition: coverity.c:32
AVRational framerate
Definition: avcodec.h:2069
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
AVOption.
Definition: opt.h:248
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
int64_t bit_rate
the average bitrate
Definition: avcodec.h:576
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
Memory handling functions.
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int num
Numerator.
Definition: rational.h:59
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
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:905
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
static av_cold int libkvazaar_close(AVCodecContext *avctx)
Definition: libkvazaar.c:146
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
AVCodec ff_libkvazaar_encoder
Definition: libkvazaar.c:295
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: codec.h:118
#define VE
Definition: libkvazaar.c:276
AVCodec.
Definition: codec.h:190
Macro definitions for various function/variable attributes.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:649
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
#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:75
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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
#define OFFSET(x)
Definition: libkvazaar.c:275
#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:40
uint8_t
#define av_cold
Definition: attributes.h:88
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:401
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
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
uint8_t * data
Definition: packet.h:363
kvz_encoder * encoder
Definition: libkvazaar.c:45
kvz_config * config
Definition: libkvazaar.c:46
#define av_log(a,...)
const kvz_api * api
Definition: libkvazaar.c:44
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:396
int width
Definition: frame.h:366
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
error code definitions
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:203
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: codec.h:197
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
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:369
static const AVOption options[]
Definition: libkvazaar.c:277
common internal API header
int width
picture width / height.
Definition: avcodec.h:699
AVFormatContext * ctx
Definition: movenc.c:48
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:658
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:290
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:381
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
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:339
main external API structure.
Definition: avcodec.h:526
int extradata_size
Definition: avcodec.h:628
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
Describe the class of an AVClass context structure.
Definition: log.h:67
static int libkvazaar_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libkvazaar.c:161
static av_cold int libkvazaar_init(AVCodecContext *avctx)
Definition: libkvazaar.c:51
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:270
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:329
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
common internal api header.
char * key
Definition: dict.h:86
int den
Denominator.
Definition: rational.h:60
#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
void * priv_data
Definition: avcodec.h:553
char * value
Definition: dict.h:87
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: packet.h:362
int height
Definition: frame.h:366
#define av_freep(p)
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key, ignoring the suffix of the found key string.
Definition: dict.h:70
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
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356