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  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);
87  }
88  cfg->framerate_num = avctx->framerate.num;
89  cfg->framerate_denom = avctx->time_base.den * avctx->ticks_per_frame;
90  } else {
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);
95  }
96  cfg->framerate_num = avctx->time_base.den;
97  cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
98  }
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;
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  av_dict_free(&dict);
114  }
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 
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  if (avctx->extradata)
161  av_freep(&avctx->extradata);
162 
163  return 0;
164 }
165 
167  AVPacket *avpkt,
168  const AVFrame *frame,
169  int *got_packet_ptr)
170 {
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;
177  int retval = 0;
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",
198  av_get_pix_fmt_name(frame->format));
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  int dst_linesizes[4] = {
214  frame->width,
215  frame->width / 2,
216  frame->width / 2,
217  0
218  };
219  av_image_copy(input_pic->data, dst_linesizes,
220  (const uint8_t **)frame->data, frame->linesize,
221  frame->format, frame->width, frame->height);
222  }
223 
224  input_pic->pts = frame->pts;
225  }
226 
227  retval = ctx->api->encoder_encode(ctx->encoder,
228  input_pic,
229  &data_out, &len_out,
230  &recon_pic, NULL,
231  &frame_info);
232  if (!retval) {
233  av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
234  retval = AVERROR_INVALIDDATA;
235  goto done;
236  } else
237  retval = 0; /* kvazaar returns 1 on success */
238 
239  if (data_out) {
240  kvz_data_chunk *chunk = NULL;
241  uint64_t written = 0;
242 
243  retval = ff_alloc_packet2(avctx, avpkt, len_out, len_out);
244  if (retval < 0) {
245  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
246  goto done;
247  }
248 
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;
253  }
254 
255  avpkt->pts = recon_pic->pts;
256  avpkt->dts = recon_pic->dts;
257  avpkt->flags = 0;
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;
263  }
264 
265  *got_packet_ptr = 1;
266  }
267 
268 done:
269  ctx->api->picture_free(input_pic);
270  ctx->api->picture_free(recon_pic);
271  ctx->api->chunk_free(data_out);
272  return retval;
273 }
274 
275 static const enum AVPixelFormat pix_fmts[] = {
278 };
279 
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 },
285  { NULL },
286 };
287 
288 static const AVClass class = {
289  .class_name = "libkvazaar",
290  .item_name = av_default_item_name,
291  .option = options,
293 };
294 
295 static const AVCodecDefault defaults[] = {
296  { "b", "0" },
297  { NULL },
298 };
299 
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,
306  .pix_fmts = pix_fmts,
307 
308  .priv_class = &class,
309  .priv_data_size = sizeof(LibkvazaarContext),
310  .defaults = defaults,
311 
313  .encode2 = libkvazaar_encode,
314  .close = libkvazaar_close,
315 
317 
318  .wrapper_name = "libkvazaar",
319 };
AVCodec
AVCodec.
Definition: avcodec.h:3481
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
FF_CODEC_CAP_INIT_THREADSAFE
#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
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
LibkvazaarContext
Definition: libkvazaar.c:41
OFFSET
#define OFFSET(x)
Definition: libkvazaar.c:280
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
AVERROR
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
opt.h
options
static const AVOption options[]
Definition: libkvazaar.c:282
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: avcodec.h:1477
AVOption
AVOption.
Definition: opt.h:246
LibkvazaarContext::kvz_params
char * kvz_params
Definition: libkvazaar.c:48
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
AVDictionary
Definition: dict.c:30
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1509
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:904
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:3105
libkvazaar_init
static av_cold int libkvazaar_init(AVCodecContext *avctx)
Definition: libkvazaar.c:51
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1645
AVRational::num
int num
Numerator.
Definition: rational.h:59
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
av_dict_get
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
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:1667
LibkvazaarContext::encoder
kvz_encoder * encoder
Definition: libkvazaar.c:45
AVDictionaryEntry::key
char * key
Definition: dict.h:82
AVCodecContext::ticks_per_frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1697
LibkvazaarContext::api
const kvz_api * api
Definition: libkvazaar.c:44
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:275
ctx
AVFormatContext * ctx
Definition: movenc.c:48
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVCodecDefault
Definition: internal.h:231
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:1615
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
error.h
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:1688
AV_CODEC_CAP_AUTO_THREADS
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
Definition: avcodec.h:1049
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:188
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: avcodec.h:1476
attributes.h
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1483
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
ff_libkvazaar_encoder
AVCodec ff_libkvazaar_encoder
Definition: libkvazaar.c:300
LibkvazaarContext::config
kvz_config * config
Definition: libkvazaar.c:46
VE
#define VE
Definition: libkvazaar.c:281
log.h
defaults
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:295
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1470
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1666
FF_CODEC_CAP_INIT_CLEANUP
#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
internal.h
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: avcodec.h:392
uint8_t
uint8_t
Definition: audio_convert.c:194
libkvazaar_close
static av_cold int libkvazaar_close(AVCodecContext *avctx)
Definition: libkvazaar.c:151
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
AVCodec::name
const char * name
Name of the codec implementation.
Definition: avcodec.h:3488
AVCodecContext::height
int height
Definition: avcodec.h:1738
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1775
avcodec.h
AVClass::class_name
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
frame
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
Definition: filter_design.txt:264
dict.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:790
AVCodecContext
main external API structure.
Definition: avcodec.h:1565
av_image_copy
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:387
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
av_dict_parse_string
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
AV_CODEC_CAP_DELAY
#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: avcodec.h:1006
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVDictionaryEntry
Definition: dict.h:81
AVPacket
This structure stores compressed data.
Definition: avcodec.h:1454
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:1592
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:1738
imgutils.h
AVERROR_BUG
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
AVDictionaryEntry::value
char * value
Definition: dict.h:83
AV_OPT_TYPE_STRING
@ AV_OPT_TYPE_STRING
Definition: opt.h:227
ff_alloc_packet2
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:32
AVCodecContext::sample_aspect_ratio
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:1944
libkvazaar_encode
static int libkvazaar_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libkvazaar.c:166
av_get_pix_fmt_name
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:2438