FFmpeg  2.8.15
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 <string.h>
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avcodec.h"
32 #include "internal.h"
33 
34 typedef struct LibkvazaarContext {
35  const AVClass *class;
36 
37  const kvz_api *api;
38  kvz_encoder *encoder;
39  kvz_config *config;
40 
41  char *kvz_params;
43 
45 {
46  int retval = 0;
47  kvz_config *cfg = NULL;
48  kvz_encoder *enc = NULL;
49  const kvz_api *const api = kvz_api_get(8);
50 
51  LibkvazaarContext *const ctx = avctx->priv_data;
52 
53  // Kvazaar requires width and height to be multiples of eight.
54  if (avctx->width % 8 || avctx->height % 8) {
55  av_log(avctx, AV_LOG_ERROR, "Video dimensions are not a multiple of 8.\n");
56  retval = AVERROR_INVALIDDATA;
57  goto done;
58  }
59 
60  cfg = api->config_alloc();
61  if (!cfg) {
62  av_log(avctx, AV_LOG_ERROR, "Could not allocate kvazaar config structure.\n");
63  retval = AVERROR(ENOMEM);
64  goto done;
65  }
66 
67  if (!api->config_init(cfg)) {
68  av_log(avctx, AV_LOG_ERROR, "Could not initialize kvazaar config structure.\n");
69  retval = AVERROR_EXTERNAL;
70  goto done;
71  }
72 
73  cfg->width = avctx->width;
74  cfg->height = avctx->height;
75  cfg->framerate =
76  avctx->time_base.den / (double)(avctx->time_base.num * avctx->ticks_per_frame);
77  cfg->target_bitrate = avctx->bit_rate;
78  cfg->vui.sar_width = avctx->sample_aspect_ratio.num;
79  cfg->vui.sar_height = avctx->sample_aspect_ratio.den;
80 
81  if (ctx->kvz_params) {
82  AVDictionary *dict = NULL;
83  if (!av_dict_parse_string(&dict, ctx->kvz_params, "=", ",", 0)) {
84  AVDictionaryEntry *entry = NULL;
85  while ((entry = av_dict_get(dict, "", entry, AV_DICT_IGNORE_SUFFIX))) {
86  if (!api->config_parse(cfg, entry->key, entry->value)) {
87  av_log(avctx, AV_LOG_WARNING,
88  "Invalid option: %s=%s.\n",
89  entry->key, entry->value);
90  }
91  }
92  av_dict_free(&dict);
93  }
94  }
95 
96  enc = api->encoder_open(cfg);
97  if (!enc) {
98  av_log(avctx, AV_LOG_ERROR, "Could not open kvazaar encoder.\n");
99  retval = AVERROR_EXTERNAL;
100  goto done;
101  }
102 
103  ctx->api = api;
104  ctx->encoder = enc;
105  ctx->config = cfg;
106  enc = NULL;
107  cfg = NULL;
108 
109 done:
110  api->config_destroy(cfg);
111  api->encoder_close(enc);
112 
113  return retval;
114 }
115 
117 {
118  LibkvazaarContext *ctx = avctx->priv_data;
119  if (!ctx->api) return 0;
120 
121  if (ctx->encoder) {
122  ctx->api->encoder_close(ctx->encoder);
123  ctx->encoder = NULL;
124  }
125 
126  if (ctx->config) {
127  ctx->api->config_destroy(ctx->config);
128  ctx->config = NULL;
129  }
130 
131  return 0;
132 }
133 
135  AVPacket *avpkt,
136  const AVFrame *frame,
137  int *got_packet_ptr)
138 {
139  int retval = 0;
140  kvz_picture *img_in = NULL;
141 
142  kvz_data_chunk *data_out = NULL;
143  uint32_t len_out = 0;
144  kvz_picture *recon_pic = NULL;
145  kvz_frame_info frame_info;
146 
147  LibkvazaarContext *ctx = avctx->priv_data;
148 
149  *got_packet_ptr = 0;
150 
151  if (frame) {
152  if (frame->width != ctx->config->width ||
153  frame->height != ctx->config->height) {
154  av_log(avctx, AV_LOG_ERROR,
155  "Changing video dimensions during encoding is not supported. "
156  "(changed from %dx%d to %dx%d)\n",
157  ctx->config->width, ctx->config->height,
158  frame->width, frame->height);
159  retval = AVERROR_INVALIDDATA;
160  goto done;
161  }
162 
163  if (frame->format != avctx->pix_fmt) {
164  av_log(avctx, AV_LOG_ERROR,
165  "Changing pixel format during encoding is not supported. "
166  "(changed from %s to %s)\n",
168  av_get_pix_fmt_name(frame->format));
169  retval = AVERROR_INVALIDDATA;
170  goto done;
171  }
172 
173  // Allocate input picture for kvazaar.
174  img_in = ctx->api->picture_alloc(frame->width, frame->height);
175  if (!img_in) {
176  av_log(avctx, AV_LOG_ERROR, "Failed to allocate picture.\n");
177  retval = AVERROR(ENOMEM);
178  goto done;
179  }
180 
181  // Copy pixels from frame to img_in.
182  {
183  int dst_linesizes[4] = {
184  frame->width,
185  frame->width / 2,
186  frame->width / 2,
187  0
188  };
189  av_image_copy(img_in->data, dst_linesizes,
190  frame->data, frame->linesize,
191  frame->format, frame->width, frame->height);
192  }
193 
194  img_in->pts = frame->pts;
195  }
196 
197  if (!ctx->api->encoder_encode(ctx->encoder, img_in,
198  &data_out, &len_out,
199  &recon_pic, NULL,
200  &frame_info)) {
201  av_log(avctx, AV_LOG_ERROR, "Failed to encode frame.\n");
202  retval = AVERROR_EXTERNAL;
203  goto done;
204  }
205 
206  if (data_out) {
207  kvz_data_chunk *chunk = NULL;
208  uint64_t written = 0;
209 
210  retval = ff_alloc_packet(avpkt, len_out);
211  if (retval < 0) {
212  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
213  goto done;
214  }
215 
216  for (chunk = data_out; chunk != NULL; chunk = chunk->next) {
217  av_assert0(written + chunk->len <= len_out);
218  memcpy(avpkt->data + written, chunk->data, chunk->len);
219  written += chunk->len;
220  }
221  *got_packet_ptr = 1;
222 
223  ctx->api->chunk_free(data_out);
224  data_out = NULL;
225 
226  avpkt->pts = recon_pic->pts;
227  avpkt->dts = recon_pic->dts;
228 
229  avpkt->flags = 0;
230  // IRAP VCL NAL unit types span the range
231  // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
232  if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
233  frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
234  avpkt->flags |= AV_PKT_FLAG_KEY;
235  }
236  }
237 
238 done:
239  ctx->api->picture_free(img_in);
240  ctx->api->picture_free(recon_pic);
241  ctx->api->chunk_free(data_out);
242  return retval;
243 }
244 
245 static const enum AVPixelFormat pix_fmts[] = {
248 };
249 
250 static const AVOption options[] = {
251  { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of name=value pairs.",
252  offsetof(LibkvazaarContext, kvz_params), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
254  { NULL },
255 };
256 
257 static const AVClass class = {
258  .class_name = "libkvazaar",
259  .item_name = av_default_item_name,
260  .option = options,
262 };
263 
264 static const AVCodecDefault defaults[] = {
265  { "b", "0" },
266  { NULL },
267 };
268 
270  .name = "libkvazaar",
271  .long_name = NULL_IF_CONFIG_SMALL("libkvazaar H.265 / HEVC"),
272  .type = AVMEDIA_TYPE_VIDEO,
273  .id = AV_CODEC_ID_HEVC,
274  .capabilities = AV_CODEC_CAP_DELAY,
275  .pix_fmts = pix_fmts,
276 
277  .priv_class = &class,
278  .priv_data_size = sizeof(LibkvazaarContext),
279  .defaults = defaults,
280 
282  .encode2 = libkvazaar_encode,
283  .close = libkvazaar_close,
284 };
#define NULL
Definition: coverity.c:32
#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:171
AVOption.
Definition: opt.h:255
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:62
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int num
numerator
Definition: rational.h:44
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:1912
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1732
static av_cold int libkvazaar_close(AVCodecContext *avctx)
Definition: libkvazaar.c:116
AVCodec ff_libkvazaar_encoder
Definition: libkvazaar.c:269
AVCodec.
Definition: avcodec.h:3482
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1641
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: avcodec.h:882
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Public dictionary API.
#define av_cold
Definition: attributes.h:74
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
static AVFrame * frame
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:39
uint8_t * data
Definition: avcodec.h:1433
kvz_encoder * encoder
Definition: libkvazaar.c:38
kvz_config * config
Definition: libkvazaar.c:39
#define av_log(a,...)
const kvz_api * api
Definition: libkvazaar.c:37
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
Definition: opt.h:285
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1479
int width
width and height of the video frame
Definition: frame.h:220
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_default_item_name
#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:178
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:199
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
Definition: avcodec.h:3489
Libavcodec external API header.
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:307
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1439
static const AVOption options[]
Definition: libkvazaar.c:250
int bit_rate
the average bitrate
Definition: avcodec.h:1577
int width
picture width / height.
Definition: avcodec.h:1691
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1650
static const AVCodecDefault defaults[]
Definition: libkvazaar.c:264
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:232
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:176
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
#define AV_OPT_FLAG_VIDEO_PARAM
Definition: opt.h:291
main external API structure.
Definition: avcodec.h:1512
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:134
static av_cold int libkvazaar_init(AVCodecContext *avctx)
Definition: libkvazaar.c:44
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:245
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
common internal api header.
char * key
Definition: dict.h:87
int den
denominator
Definition: rational.h:45
void * priv_data
Definition: avcodec.h:1554
char * value
Definition: dict.h:88
attribute_deprecated int ff_alloc_packet(AVPacket *avpkt, int size)
Definition: utils.c:1850
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1432
int height
Definition: frame.h:220
#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:72
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:2050
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
This structure stores compressed data.
Definition: avcodec.h:1410
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1426