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
37
42
45
49
52
54 {
56 const kvz_api *
const api =
ctx->api = kvz_api_get(8);
57 kvz_config *cfg =
NULL;
58 kvz_encoder *enc =
NULL;
59
60 /* Kvazaar requires width and height to be multiples of eight. */
63 "Video dimensions are not a multiple of 8 (%dx%d).\n",
66 }
67
68 ctx->config = cfg = api->config_alloc();
69 if (!cfg) {
71 "Could not allocate kvazaar config structure.\n");
73 }
74
75 if (!api->config_init(cfg)) {
77 "Could not initialize kvazaar config structure.\n");
79 }
80
81 cfg->width = avctx->
width;
82 cfg->height = avctx->
height;
83
87 } else {
91 #if FF_API_TICKS_PER_FRAME
93 #endif
94 ;
96 }
97 cfg->target_bitrate = avctx->
bit_rate;
101 cfg->rc_algorithm = KVZ_LAMBDA;
102 }
103
110
111 if (
ctx->kvz_params) {
116 if (!api->config_parse(cfg,
entry->key,
entry->value)) {
119 }
120 }
121 }
123 }
124
125 ctx->encoder = enc = api->encoder_open(cfg);
126 if (!enc) {
129 }
130
132 kvz_data_chunk *data_out =
NULL;
133 kvz_data_chunk *chunk =
NULL;
134 uint32_t len_out;
135 uint8_t *p;
136
137 if (!api->encoder_headers(enc, &data_out, &len_out))
139
141 if (!p) {
142 ctx->api->chunk_free(data_out);
144 }
145
147
148 for (chunk = data_out; chunk !=
NULL; chunk = chunk->next) {
149 memcpy(p, chunk->data, chunk->len);
150 p += chunk->len;
151 }
152
153 ctx->api->chunk_free(data_out);
154 }
155
156 return 0;
157 }
158
160 {
162
164 ctx->api->encoder_close(
ctx->encoder);
165 ctx->api->config_destroy(
ctx->config);
166 }
167
168 return 0;
169 }
170
174 int *got_packet_ptr)
175 {
177 kvz_picture *input_pic =
NULL;
178 kvz_picture *recon_pic =
NULL;
179 kvz_frame_info frame_info;
180 kvz_data_chunk *data_out =
NULL;
181 uint32_t len_out = 0;
182 int retval = 0;
183 int pict_type;
184
185 *got_packet_ptr = 0;
186
188 if (
frame->width !=
ctx->config->width ||
189 frame->height !=
ctx->config->height) {
191 "Changing video dimensions during encoding is not supported. "
192 "(changed from %dx%d to %dx%d)\n",
193 ctx->config->width,
ctx->config->height,
196 goto done;
197 }
198
201 "Changing pixel format during encoding is not supported. "
202 "(changed from %s to %s)\n",
206 goto done;
207 }
208
209 // Allocate input picture for kvazaar.
210 input_pic =
ctx->api->picture_alloc(
frame->width,
frame->height);
211 if (!input_pic) {
214 goto done;
215 }
216
217 // Copy pixels from frame to input_pic.
218 {
220 input_pic->data[0],
221 input_pic->data[1],
222 input_pic->data[2],
224 };
225 int dst_linesizes[4] = {
229 0
230 };
234 }
235
236 input_pic->pts =
frame->pts;
237 }
238
239 retval =
ctx->api->encoder_encode(
ctx->encoder,
240 input_pic,
241 &data_out, &len_out,
243 &frame_info);
244 if (!retval) {
247 goto done;
248 } else
249 retval = 0; /* kvazaar returns 1 on success */
250
251 if (data_out) {
252 kvz_data_chunk *chunk =
NULL;
253 uint64_t written = 0;
254
256 if (retval < 0) {
258 goto done;
259 }
260
261 for (chunk = data_out; chunk !=
NULL; chunk = chunk->next) {
263 memcpy(avpkt->
data + written, chunk->data, chunk->len);
264 written += chunk->len;
265 }
266
267 avpkt->
pts = recon_pic->pts;
268 avpkt->
dts = recon_pic->dts;
270 // IRAP VCL NAL unit types span the range
271 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
272 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
273 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
275 }
276
277 switch (frame_info.slice_type) {
278 case KVZ_SLICE_I:
280 break;
281 case KVZ_SLICE_P:
283 break;
284 case KVZ_SLICE_B:
286 break;
287 default:
290 }
291
293
294 *got_packet_ptr = 1;
295 }
296
297 done:
298 ctx->api->picture_free(input_pic);
299 ctx->api->picture_free(recon_pic);
300 ctx->api->chunk_free(data_out);
301 return retval;
302 }
303
307 };
308
309 #define OFFSET(x) offsetof(LibkvazaarContext, x)
310 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
312 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
315 };
316
322 };
323
325 { "b", "0" },
327 };
328
330 .
p.
name =
"libkvazaar",
338
339 .p.priv_class = &class,
342
346
349
350 .p.wrapper_name = "libkvazaar",
351 };