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
33
36
39
43
46
48 {
50 const kvz_api *
const api = ctx->
api = kvz_api_get(8);
51 kvz_config *cfg =
NULL;
52 kvz_encoder *enc =
NULL;
53
54 /* Kvazaar requires width and height to be multiples of eight. */
57 "Video dimensions are not a multiple of 8 (%dx%d).\n",
60 }
61
62 ctx->
config = cfg = api->config_alloc();
63 if (!cfg) {
65 "Could not allocate kvazaar config structure.\n");
67 }
68
69 if (!api->config_init(cfg)) {
71 "Could not initialize kvazaar config structure.\n");
73 }
74
75 cfg->width = avctx->
width;
76 cfg->height = avctx->
height;
77
80 "Could not set framerate for kvazaar: integer overflow\n");
82 }
85 cfg->target_bitrate = avctx->
bit_rate;
88
94 if (!api->config_parse(cfg, entry->
key, entry->
value)) {
97 }
98 }
100 }
101 }
102
103 ctx->
encoder = enc = api->encoder_open(cfg);
104 if (!enc) {
107 }
108
110 kvz_data_chunk *data_out =
NULL;
111 kvz_data_chunk *chunk =
NULL;
112 uint32_t len_out;
114
115 if (!api->encoder_headers(enc, &data_out, &len_out))
117
119 if (!p) {
120 ctx->
api->chunk_free(data_out);
122 }
123
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 {
140
144 }
145
148
149 return 0;
150 }
151
155 int *got_packet_ptr)
156 {
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 *got_packet_ptr = 0;
166
167 if (frame) {
171 "Changing video dimensions during encoding is not supported. "
172 "(changed from %dx%d to %dx%d)\n",
176 goto done;
177 }
178
181 "Changing pixel format during encoding is not supported. "
182 "(changed from %s to %s)\n",
186 goto done;
187 }
188
189 // Allocate input picture for kvazaar.
191 if (!input_pic) {
194 goto done;
195 }
196
197 // Copy pixels from frame to input_pic.
198 {
199 int dst_linesizes[4] = {
203 0
204 };
208 }
209
210 input_pic->pts = frame->
pts;
211 }
212
213 retval = ctx->
api->encoder_encode(ctx->
encoder,
214 input_pic,
215 &data_out, &len_out,
217 &frame_info);
218 if (!retval) {
221 goto done;
222 }
223 else
224 retval = 0; /* kvazaar returns 1 on success */
225
226 if (data_out) {
227 kvz_data_chunk *chunk =
NULL;
228 uint64_t written = 0;
229
231 if (retval < 0) {
233 goto done;
234 }
235
236 for (chunk = data_out; chunk !=
NULL; chunk = chunk->next) {
238 memcpy(avpkt->
data + written, chunk->data, chunk->len);
239 written += chunk->len;
240 }
241
242 avpkt->
pts = recon_pic->pts;
243 avpkt->
dts = recon_pic->dts;
245 // IRAP VCL NAL unit types span the range
246 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
247 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
248 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
250 }
251
252 *got_packet_ptr = 1;
253 }
254
255 done:
256 ctx->
api->picture_free(input_pic);
257 ctx->
api->picture_free(recon_pic);
258 ctx->
api->chunk_free(data_out);
259 return retval;
260 }
261
265 };
266
267 #define OFFSET(x) offsetof(LibkvazaarContext, x)
268 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
270 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
273 };
274
280 };
281
283 { "b", "0" },
285 };
286
288 .
name =
"libkvazaar",
294
295 .priv_class = &class,
297 .defaults = defaults,
298
302
304 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
This structure describes decoded (raw) audio or video data.
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
static av_cold int init(AVCodecContext *avctx)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static av_cold int libkvazaar_close(AVCodecContext *avctx)
AVCodec ff_libkvazaar_encoder
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
int width
width and height of the video frame
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
int flags
AV_CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
const char * name
Name of the codec implementation.
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.
int flags
A combination of AV_PKT_FLAG values.
static const AVOption options[]
common internal API header
int width
picture width / height.
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
static const AVCodecDefault defaults[]
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
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.
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
main external API structure.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Describe the class of an AVClass context structure.
static int libkvazaar_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static av_cold int libkvazaar_init(AVCodecContext *avctx)
static enum AVPixelFormat pix_fmts[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common internal api header.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
attribute_deprecated int ff_alloc_packet(AVPacket *avpkt, int size)
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
#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.
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.
AVPixelFormat
Pixel format.
This structure stores compressed data.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...