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 {
90 "Could not set framerate for kvazaar: integer overflow\n");
92 }
95 }
96 cfg->target_bitrate = avctx->
bit_rate;
100 cfg->rc_algorithm = KVZ_LAMBDA;
101 }
102
103 if (
ctx->kvz_params) {
108 if (!api->config_parse(cfg, entry->
key, entry->
value)) {
111 }
112 }
113 }
115 }
116
117 ctx->encoder = enc = api->encoder_open(cfg);
118 if (!enc) {
121 }
122
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))
131
133 if (!p) {
134 ctx->api->chunk_free(data_out);
136 }
137
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 {
154
156 ctx->api->encoder_close(
ctx->encoder);
157 ctx->api->config_destroy(
ctx->config);
158 }
159
160 return 0;
161 }
162
166 int *got_packet_ptr)
167 {
169 kvz_picture *input_pic =
NULL;
170 kvz_picture *recon_pic =
NULL;
171 kvz_frame_info frame_info;
172 kvz_data_chunk *data_out =
NULL;
173 uint32_t len_out = 0;
174 int retval = 0;
175 int pict_type;
176
177 *got_packet_ptr = 0;
178
180 if (
frame->width !=
ctx->config->width ||
181 frame->height !=
ctx->config->height) {
183 "Changing video dimensions during encoding is not supported. "
184 "(changed from %dx%d to %dx%d)\n",
185 ctx->config->width,
ctx->config->height,
188 goto done;
189 }
190
193 "Changing pixel format during encoding is not supported. "
194 "(changed from %s to %s)\n",
198 goto done;
199 }
200
201 // Allocate input picture for kvazaar.
202 input_pic =
ctx->api->picture_alloc(
frame->width,
frame->height);
203 if (!input_pic) {
206 goto done;
207 }
208
209 // Copy pixels from frame to input_pic.
210 {
211 uint8_t *dst[4] = {
212 input_pic->data[0],
213 input_pic->data[1],
214 input_pic->data[2],
216 };
217 int dst_linesizes[4] = {
221 0
222 };
224 (
const uint8_t **)
frame->data,
frame->linesize,
226 }
227
228 input_pic->pts =
frame->pts;
229 }
230
231 retval =
ctx->api->encoder_encode(
ctx->encoder,
232 input_pic,
233 &data_out, &len_out,
235 &frame_info);
236 if (!retval) {
239 goto done;
240 } else
241 retval = 0; /* kvazaar returns 1 on success */
242
243 if (data_out) {
244 kvz_data_chunk *chunk =
NULL;
245 uint64_t written = 0;
246
248 if (retval < 0) {
250 goto done;
251 }
252
253 for (chunk = data_out; chunk !=
NULL; chunk = chunk->next) {
255 memcpy(avpkt->
data + written, chunk->data, chunk->len);
256 written += chunk->len;
257 }
258
259 avpkt->
pts = recon_pic->pts;
260 avpkt->
dts = recon_pic->dts;
262 // IRAP VCL NAL unit types span the range
263 // [BLA_W_LP (16), RSV_IRAP_VCL23 (23)].
264 if (frame_info.nal_unit_type >= KVZ_NAL_BLA_W_LP &&
265 frame_info.nal_unit_type <= KVZ_NAL_RSV_IRAP_VCL23) {
267 }
268
269 switch (frame_info.slice_type) {
270 case KVZ_SLICE_I:
272 break;
273 case KVZ_SLICE_P:
275 break;
276 case KVZ_SLICE_B:
278 break;
279 default:
282 }
283
285
286 *got_packet_ptr = 1;
287 }
288
289 done:
290 ctx->api->picture_free(input_pic);
291 ctx->api->picture_free(recon_pic);
292 ctx->api->chunk_free(data_out);
293 return retval;
294 }
295
299 };
300
301 #define OFFSET(x) offsetof(LibkvazaarContext, x)
302 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
304 { "kvazaar-params", "Set kvazaar parameters as a comma-separated list of key=value pairs.",
307 };
308
314 };
315
317 { "b", "0" },
319 };
320
322 .
p.
name =
"libkvazaar",
329
330 .p.priv_class = &class,
333
337
340
341 .p.wrapper_name = "libkvazaar",
342 };