Libav
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 Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 <string.h>
25 
26 #include "libavutil/dict.h"
27 #include "libavutil/error.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/pixdesc.h"
31 #include "libavutil/opt.h"
32 
33 #include "avcodec.h"
34 #include "internal.h"
35 
36 typedef struct LibkvazaarContext {
37  const AVClass *class;
38 
39  const kvz_api *api;
40  kvz_encoder *encoder;
41  kvz_config *config;
42 
43  char *kvz_params;
45 
47 {
48  LibkvazaarContext *const ctx = avctx->priv_data;
49  const kvz_api *const api = ctx->api = kvz_api_get(8);
50  kvz_config *cfg = NULL;
51  kvz_encoder *enc = NULL;
52 
54  av_log(avctx, AV_LOG_ERROR,
55  "Set -strict experimental to use this encoder.\n");
56  return AVERROR_EXPERIMENTAL;
57  }
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  cfg->framerate_num = avctx->time_base.den;
84  cfg->framerate_denom = avctx->time_base.num * avctx->ticks_per_frame;
85  cfg->target_bitrate = avctx->bit_rate;
86  cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
87  cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
88 
89  if (ctx->kvz_params) {
90  AVDictionary *dict = NULL;
91  if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
92  AVDictionaryEntry *entry = NULL;
93  while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
94  if (!api->config_parse(cfg, entry->key, entry->value)) {
95  av_log(avctx, AV_LOG_WARNING, "Invalid option: %s=%s.\n",
96  entry->key, entry->value);
97  }
98  }
99  av_dict_free(&dict);
100  }
101  }
102 
103  ctx->encoder = enc = api->encoder_open(cfg);
104  if (!enc) {
105  av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
106  return AVERROR_BUG;
107  }
108 
109  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
110  kvz_data_chunk *data_out = NULL;
111  kvz_data_chunk *chunk = NULL;
112  uint32_t len_out;
113  uint8_t *p;
114 
115  if (!api->encoder_headers(enc, &data_out, &len_out))
116  return AVERROR(ENOMEM);
117 
118  avctx->extradata = p = av_mallocz(len_out + AV_INPUT_BUFFER_PADDING_SIZE);
119  if (!p) {
120  ctx->api->chunk_free(data_out);
121  return AVERROR(ENOMEM);
122  }
123 
124  avctx->extradata_size = len_out;
125 
126  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
127  memcpy(p, chunk->data, chunk->len);
128  p += chunk->len;
129  }
130 
131  ctx->api->chunk_free(data_out);
132  }
133 
134  return 0;
135 }
136 
138 {
139  LibkvazaarContext *ctx = avctx->priv_data;
140 
141  if (ctx->api) {
142  ctx->api->encoder_close(ctx->encoder);
143  ctx->api->config_destroy(ctx->config);
144  }
145 
146  if (avctx->extradata)
147  av_freep(&avctx->extradata);
148 
149  return 0;
150 }
151 
153  AVPacket *avpkt,
154  const AVFrame *frame,
155  int *got_packet_ptr)
156 {
157  LibkvazaarContext *ctx = avctx->priv_data;
158  kvz_picture *input_pic = NULL;
159  kvz_picture *recon_pic = NULL;
160  kvz_frame_info frame_info;
161  kvz_data_chunk *data_out = NULL;
162  uint32_t len_out = 0;
163  int retval = 0;
164 
165  if (frame) {
166  if (frame->width != ctx->config->width ||
167  frame->height != ctx->config->height) {
168  av_log(avctx, AV_LOG_ERROR,
169  "Changing video dimensions during encoding is not supported. "
170  "(changed from %dx%d to %dx%d)\n",
171  ctx->config->width, ctx->config->height,
172  frame->width, frame->height);
173  retval = AVERROR_INVALIDDATA;
174  goto done;
175  }
176 
177  if (frame->format != avctx->pix_fmt) {
178  av_log(avctx, AV_LOG_ERROR,
179  "Changing pixel format during encoding is not supported. "
180  "(changed from %s to %s)\n",
182  av_get_pix_fmt_name(frame->format));
183  retval = AVERROR_INVALIDDATA;
184  goto done;
185  }
186 
187  // Allocate input picture for kvazaar.
188  input_pic = ctx->api->picture_alloc(frame->width, frame->height);
189  if (!input_pic) {
190  av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
191  retval = AVERROR(ENOMEM);
192  goto done;
193  }
194 
195  // Copy pixels from frame to input_pic.
196  {
197  int dst_linesizes[4] = {
198  frame->width,
199  frame->width / 2,
200  frame->width / 2,
201  0
202  };
203  av_image_copy(input_pic->data, dst_linesizes,
204  frame->data, frame->linesize,
205  frame->format, frame->width, frame->height);
206  }
207 
208  input_pic->pts = frame->pts;
209  }
210 
211  retval = ctx->api->encoder_encode(ctx->encoder,
212  input_pic,
213  &data_out, &len_out,
214  &recon_pic, NULL,
215  &frame_info);
216  if (!retval) {
217  av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
218  retval = AVERROR_INVALIDDATA;
219  goto done;
220  }
221 
222  if (data_out) {
223  kvz_data_chunk *chunk = NULL;
224  uint64_t written = 0;
225 
226  retval = ff_alloc_packet(avpkt, len_out);
227  if (retval < 0) {
228  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
229  goto done;
230  }
231 
232  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
233  memcpy(avpkt->data + written, chunk->data, chunk->len);
234  written += chunk->len;
235  }
236 
237  avpkt->pts = recon_pic->pts;
238  avpkt->dts = recon_pic->dts;
239  avpkt->flags = 0;
240  // IRAP VCL NAL unit types span the range
241  // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
242  if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
243  frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
244  avpkt->flags |= AV_PKT_FLAG_KEY;
245  }
246 
247  *got_packet_ptr = 1;
248  }
249 
250 done:
251  ctx->api->picture_free(input_pic);
252  ctx->api->picture_free(recon_pic);
253  ctx->api->chunk_free(data_out);
254  return retval;
255 }
256 
257 static const enum AVPixelFormat pix_fmts[] = {
260 };
261 
262 #define OFFSET(x) offsetof(LibkvazaarContext, x)
263 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
264 static const AVOption options[] = {
265  { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
266  OFFSET(kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VE },
267 
268  { NULL },
269 };
270 
271 static const AVClass class = {
272  .class_name = "libkvazaar",
273  .item_name = av_default_item_name,
274  .option = options,
276 };
277 
278 static const AVCodecDefault defaults[] = {
279  { "b", "0" },
280  { NULL },
281 };
282 
284  .name = "libkvazaar",
285  .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
286  .type = AVMEDIA_TYPE_VIDEO,
287  .id = AV_CODEC_ID_HEVC,
288  .capabilities = AV_CODEC_CAP_DELAY,
289  .pix_fmts = pix_fmts,
290 
291  .priv_class = &class,
292  .priv_data_size = sizeof(LibkvazaarContext),
293  .defaults = defaults,
294 
296  .encode2 = libkvazaar_encode,
297  .close = libkvazaar_close,
298 
300 };
#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 FF_COMPLIANCE_EXPERIMENTAL
Allow nonstandardized experimental things.
Definition: avcodec.h:2610
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
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:1804
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_cold int libkvazaar_close(AVCodecContext *avctx)
Definition: libkvazaar.c:137
AVCodec ff_libkvazaar_encoder
Definition: libkvazaar.c:283
#define VE
Definition: libkvazaar.c:263
AVCodec.
Definition: avcodec.h:3120
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1535
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
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:39
#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:863
Public dictionary API.
#define OFFSET(x)
Definition: libkvazaar.c:262
#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:66
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
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:38
uint8_t * data
Definition: avcodec.h:1346
kvz_encoder * encoder
Definition: libkvazaar.c:40
kvz_config * config
Definition: libkvazaar.c:41
const kvz_api * api
Definition: libkvazaar.c:39
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int width
width and height of the video frame
Definition: frame.h:179
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
error code definitions
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1503
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
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:268
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
static const AVOption options[]
Definition: libkvazaar.c:264
common internal API header
int bit_rate
the average bitrate
Definition: avcodec.h:1473
int width
picture width / height.
Definition: avcodec.h:1580
AVFormatContext * ctx
Definition: movenc.c:48
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1544
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:278
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define AVERROR_EXPERIMENTAL
Requested feature is flagged experimental. Set strict_std_compliance if you really want to use it...
Definition: error.h:62
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:191
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 to a dictionary.
Definition: dict.c:152
NULL
Definition: eval.c:55
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
av_default_item_name
Definition: dnxhdenc.c:55
main external API structure.
Definition: avcodec.h:1409
int extradata_size
Definition: avcodec.h:1524
#define AVERROR_BUG
Bug detected, please report the issue.
Definition: error.h:60
Describe the class of an AVClass context structure.
Definition: log.h:34
static int libkvazaar_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
Definition: libkvazaar.c:152
static av_cold int libkvazaar_init(AVCodecContext *avctx)
Definition: libkvazaar.c:46
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:784
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
common internal api header.
char * key
Definition: dict.h:73
int den
denominator
Definition: rational.h:45
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void * priv_data
Definition: avcodec.h:1451
char * value
Definition: dict.h:74
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
int height
Definition: frame.h:179
#define AV_DICT_IGNORE_SUFFIX
Definition: dict.h:60
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:1704
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2605
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339