/src/libheif/libheif/plugins/encoder_x265.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include "libheif/heif.h" |
22 | | #include "libheif/heif_plugin.h" |
23 | | #include "encoder_x265.h" |
24 | | #include "encoder_input_check.h" |
25 | | #include <memory> |
26 | | #include <sstream> |
27 | | #include <string> |
28 | | #include <cstring> |
29 | | #include <cstdio> |
30 | | #include <cassert> |
31 | | #include <deque> |
32 | | #include <vector> |
33 | | #include <utility> |
34 | | |
35 | | extern "C" { |
36 | | #include <x265.h> |
37 | | } |
38 | | |
39 | | |
40 | | static const char* kError_unsupported_bit_depth = "Bit depth not supported by x265"; |
41 | | static const char* kError_unsupported_image_size = "Images smaller than 16 pixels are not supported"; |
42 | | static const char* kError_unsupported_ctu_size = "Unsupported CTU size"; |
43 | | |
44 | | |
45 | | enum parameter_type |
46 | | { |
47 | | UndefinedType, Int, Bool, String |
48 | | }; |
49 | | |
50 | | struct parameter |
51 | | { |
52 | | parameter_type type = UndefinedType; |
53 | | std::string name; |
54 | | |
55 | | int value_int = 0; // also used for boolean |
56 | | std::string value_string; |
57 | | }; |
58 | | |
59 | | |
60 | | struct encoder_struct_x265 |
61 | | { |
62 | | x265_encoder* encoder = nullptr; |
63 | | const x265_api* api = nullptr; |
64 | | x265_param* param = nullptr; |
65 | | |
66 | | int bit_depth = 0; |
67 | | |
68 | | heif_chroma chroma; |
69 | | |
70 | | |
71 | | // --- output |
72 | | |
73 | | struct Packet |
74 | | { |
75 | | std::vector<uint8_t> data; |
76 | | uintptr_t frameNr = 0; |
77 | | }; |
78 | | |
79 | | std::deque<Packet> output_data; |
80 | | std::vector<uint8_t> active_output_nal; |
81 | | |
82 | | void append_nal_packets(x265_nal* nals, uint32_t num_nals, uintptr_t frameNr); |
83 | | |
84 | | // --- parameters |
85 | | |
86 | | std::vector<parameter> parameters; |
87 | | |
88 | | void add_param(const parameter&); |
89 | | |
90 | | void add_param(const std::string& name, int value); |
91 | | |
92 | | void add_param(const std::string& name, bool value); |
93 | | |
94 | | void add_param(const std::string& name, const std::string& value); |
95 | | |
96 | | parameter get_param(const std::string& name) const; |
97 | | |
98 | | std::string preset; |
99 | | std::string tune; |
100 | | |
101 | | int logLevel = X265_LOG_NONE; |
102 | | |
103 | | std::string last_error_message; |
104 | | }; |
105 | | |
106 | | |
107 | | void encoder_struct_x265::add_param(const parameter& p) |
108 | 0 | { |
109 | | // if there is already a parameter of that name, remove it from list |
110 | |
|
111 | 0 | for (size_t i = 0; i < parameters.size(); i++) { |
112 | 0 | if (parameters[i].name == p.name) { |
113 | 0 | for (size_t k = i + 1; k < parameters.size(); k++) { |
114 | 0 | parameters[k - 1] = parameters[k]; |
115 | 0 | } |
116 | 0 | parameters.pop_back(); |
117 | 0 | break; |
118 | 0 | } |
119 | 0 | } |
120 | | |
121 | | // and add the new parameter at the end of the list |
122 | |
|
123 | 0 | parameters.push_back(p); |
124 | 0 | } |
125 | | |
126 | | |
127 | | void encoder_struct_x265::add_param(const std::string& name, int value) |
128 | 0 | { |
129 | 0 | parameter p; |
130 | 0 | p.type = Int; |
131 | 0 | p.name = name; |
132 | 0 | p.value_int = value; |
133 | 0 | add_param(p); |
134 | 0 | } |
135 | | |
136 | | void encoder_struct_x265::add_param(const std::string& name, bool value) |
137 | 0 | { |
138 | 0 | parameter p; |
139 | 0 | p.type = Bool; |
140 | 0 | p.name = name; |
141 | 0 | p.value_int = value; |
142 | 0 | add_param(p); |
143 | 0 | } |
144 | | |
145 | | void encoder_struct_x265::add_param(const std::string& name, const std::string& value) |
146 | 0 | { |
147 | 0 | parameter p; |
148 | 0 | p.type = String; |
149 | 0 | p.name = name; |
150 | 0 | p.value_string = value; |
151 | 0 | add_param(p); |
152 | 0 | } |
153 | | |
154 | | |
155 | | parameter encoder_struct_x265::get_param(const std::string& name) const |
156 | 0 | { |
157 | 0 | for (size_t i = 0; i < parameters.size(); i++) { |
158 | 0 | if (parameters[i].name == name) { |
159 | 0 | return parameters[i]; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | return parameter(); |
164 | 0 | } |
165 | | |
166 | | |
167 | | static const char* kParam_preset = "preset"; |
168 | | static const char* kParam_tune = "tune"; |
169 | | static const char* kParam_TU_intra_depth = "tu-intra-depth"; |
170 | | static const char* kParam_complexity = "complexity"; |
171 | | |
172 | | static const char* const kParam_preset_valid_values[] = { |
173 | | "ultrafast", "superfast", "veryfast", "faster", "fast", "medium", |
174 | | "slow", "slower", "veryslow", "placebo", nullptr |
175 | | }; |
176 | | |
177 | | static const char* const kParam_tune_valid_values[] = { |
178 | | "psnr", "ssim", "grain", "fastdecode", nullptr |
179 | | // note: zerolatency is missing, because we do not need it for single images |
180 | | }; |
181 | | |
182 | | static const char* kParam_chroma = "chroma"; |
183 | | static const char* const kParam_chroma_valid_values[] = { |
184 | | "420", "422", "444", nullptr |
185 | | }; |
186 | | |
187 | | |
188 | | static const int X265_PLUGIN_PRIORITY = 100; |
189 | | |
190 | 0 | #define MAX_PLUGIN_NAME_LENGTH 80 |
191 | | |
192 | | static char plugin_name[MAX_PLUGIN_NAME_LENGTH]; |
193 | | |
194 | | |
195 | | static void x265_set_default_parameters(void* encoder); |
196 | | |
197 | | |
198 | | static const char* x265_plugin_name() |
199 | 0 | { |
200 | 0 | strcpy(plugin_name, "x265 HEVC encoder"); |
201 | |
|
202 | 0 | const x265_api* api = x265_api_get(0); |
203 | 0 | const char* x265_version = ((api != nullptr && api->version_str != nullptr) ? api->version_str : "null"); |
204 | |
|
205 | 0 | if (strlen(x265_version) + strlen(plugin_name) + 4 < MAX_PLUGIN_NAME_LENGTH) { |
206 | 0 | strcat(plugin_name, " ("); |
207 | 0 | strcat(plugin_name, x265_version); |
208 | 0 | strcat(plugin_name, ")"); |
209 | 0 | } |
210 | |
|
211 | 0 | return plugin_name; |
212 | 0 | } |
213 | | |
214 | | |
215 | | #define MAX_NPARAMETERS 10 |
216 | | |
217 | | static heif_encoder_parameter x265_encoder_params[MAX_NPARAMETERS]; |
218 | | static const heif_encoder_parameter* x265_encoder_parameter_ptrs[MAX_NPARAMETERS + 1]; |
219 | | |
220 | | static void x265_init_parameters() |
221 | 252 | { |
222 | 252 | heif_encoder_parameter* p = x265_encoder_params; |
223 | 252 | const heif_encoder_parameter** d = x265_encoder_parameter_ptrs; |
224 | 252 | int i = 0; |
225 | | |
226 | 252 | assert(i < MAX_NPARAMETERS); |
227 | 252 | p->version = 2; |
228 | 252 | p->name = heif_encoder_parameter_name_quality; |
229 | 252 | p->type = heif_encoder_parameter_type_integer; |
230 | 252 | p->integer.default_value = 50; |
231 | 252 | p->has_default = true; |
232 | 252 | p->integer.have_minimum_maximum = true; |
233 | 252 | p->integer.minimum = 0; |
234 | 252 | p->integer.maximum = 100; |
235 | 252 | p->integer.valid_values = NULL; |
236 | 252 | p->integer.num_valid_values = 0; |
237 | 252 | d[i++] = p++; |
238 | | |
239 | 252 | assert(i < MAX_NPARAMETERS); |
240 | 252 | p->version = 2; |
241 | 252 | p->name = heif_encoder_parameter_name_lossless; |
242 | 252 | p->type = heif_encoder_parameter_type_boolean; |
243 | 252 | p->boolean.default_value = false; |
244 | 252 | p->has_default = true; |
245 | 252 | d[i++] = p++; |
246 | | |
247 | 252 | assert(i < MAX_NPARAMETERS); |
248 | 252 | p->version = 2; |
249 | 252 | p->name = kParam_preset; |
250 | 252 | p->type = heif_encoder_parameter_type_string; |
251 | 252 | p->string.default_value = "slow"; // increases computation time |
252 | 252 | p->has_default = true; |
253 | 252 | p->string.valid_values = kParam_preset_valid_values; |
254 | 252 | d[i++] = p++; |
255 | | |
256 | 252 | assert(i < MAX_NPARAMETERS); |
257 | 252 | p->version = 2; |
258 | 252 | p->name = kParam_tune; |
259 | 252 | p->type = heif_encoder_parameter_type_string; |
260 | 252 | p->string.default_value = "ssim"; |
261 | 252 | p->has_default = true; |
262 | 252 | p->string.valid_values = kParam_tune_valid_values; |
263 | 252 | d[i++] = p++; |
264 | | |
265 | 252 | assert(i < MAX_NPARAMETERS); |
266 | 252 | p->version = 2; |
267 | 252 | p->name = kParam_TU_intra_depth; |
268 | 252 | p->type = heif_encoder_parameter_type_integer; |
269 | 252 | p->integer.default_value = 2; // increases computation time |
270 | 252 | p->has_default = true; |
271 | 252 | p->integer.have_minimum_maximum = true; |
272 | 252 | p->integer.minimum = 1; |
273 | 252 | p->integer.maximum = 4; |
274 | 252 | p->integer.valid_values = NULL; |
275 | 252 | p->integer.num_valid_values = 0; |
276 | 252 | d[i++] = p++; |
277 | | |
278 | 252 | assert(i < MAX_NPARAMETERS); |
279 | 252 | p->version = 2; |
280 | 252 | p->name = kParam_complexity; |
281 | 252 | p->type = heif_encoder_parameter_type_integer; |
282 | 252 | p->integer.default_value = 50; |
283 | 252 | p->has_default = false; |
284 | 252 | p->integer.have_minimum_maximum = true; |
285 | 252 | p->integer.minimum = 0; |
286 | 252 | p->integer.maximum = 100; |
287 | 252 | p->integer.valid_values = NULL; |
288 | 252 | p->integer.num_valid_values = 0; |
289 | 252 | d[i++] = p++; |
290 | | |
291 | 252 | assert(i < MAX_NPARAMETERS); |
292 | 252 | p->version = 2; |
293 | 252 | p->name = kParam_chroma; |
294 | 252 | p->type = heif_encoder_parameter_type_string; |
295 | 252 | p->string.default_value = "420"; |
296 | 252 | p->has_default = true; |
297 | 252 | p->string.valid_values = kParam_chroma_valid_values; |
298 | 252 | d[i++] = p++; |
299 | | |
300 | 252 | d[i++] = nullptr; |
301 | 252 | } |
302 | | |
303 | | |
304 | | const heif_encoder_parameter** x265_list_parameters(void* encoder) |
305 | 0 | { |
306 | 0 | return x265_encoder_parameter_ptrs; |
307 | 0 | } |
308 | | |
309 | | |
310 | | static void x265_init_plugin() |
311 | 252 | { |
312 | 252 | x265_init_parameters(); |
313 | 252 | } |
314 | | |
315 | | |
316 | | static void x265_cleanup_plugin() |
317 | 0 | { |
318 | 0 | x265_cleanup(); |
319 | 0 | } |
320 | | |
321 | | |
322 | | static heif_error x265_new_encoder(void** enc) |
323 | 0 | { |
324 | 0 | encoder_struct_x265* encoder = new encoder_struct_x265(); |
325 | 0 | heif_error err = heif_error_ok; |
326 | | |
327 | | |
328 | | // encoder has to be allocated in x265_encode_image, because it needs to know the image size |
329 | 0 | encoder->encoder = nullptr; |
330 | |
|
331 | 0 | encoder->bit_depth = 8; |
332 | |
|
333 | 0 | *enc = encoder; |
334 | | |
335 | | |
336 | | // set default parameters |
337 | |
|
338 | 0 | x265_set_default_parameters(encoder); |
339 | |
|
340 | 0 | return err; |
341 | 0 | } |
342 | | |
343 | | static void x265_free_encoder(void* encoder_raw) |
344 | 0 | { |
345 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
346 | |
|
347 | 0 | if (encoder->encoder) { |
348 | 0 | const x265_api* api = x265_api_get(encoder->bit_depth); |
349 | 0 | api->encoder_close(encoder->encoder); |
350 | 0 | } |
351 | |
|
352 | 0 | if (encoder->param && encoder->api) { |
353 | 0 | encoder->api->param_free(encoder->param); |
354 | 0 | } |
355 | |
|
356 | 0 | delete encoder; |
357 | 0 | } |
358 | | |
359 | | static heif_error x265_set_parameter_quality(void* encoder_raw, int quality) |
360 | 0 | { |
361 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
362 | |
|
363 | 0 | if (quality < 0 || quality > 100) { |
364 | 0 | return heif_error_invalid_parameter_value; |
365 | 0 | } |
366 | | |
367 | 0 | encoder->add_param(heif_encoder_parameter_name_quality, quality); |
368 | |
|
369 | 0 | return heif_error_ok; |
370 | 0 | } |
371 | | |
372 | | static heif_error x265_get_parameter_quality(void* encoder_raw, int* quality) |
373 | 0 | { |
374 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
375 | |
|
376 | 0 | parameter p = encoder->get_param(heif_encoder_parameter_name_quality); |
377 | 0 | *quality = p.value_int; |
378 | |
|
379 | 0 | return heif_error_ok; |
380 | 0 | } |
381 | | |
382 | | static heif_error x265_set_parameter_lossless(void* encoder_raw, int enable) |
383 | 0 | { |
384 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
385 | |
|
386 | 0 | encoder->add_param(heif_encoder_parameter_name_lossless, (bool) enable); |
387 | |
|
388 | 0 | return heif_error_ok; |
389 | 0 | } |
390 | | |
391 | | static heif_error x265_get_parameter_lossless(void* encoder_raw, int* enable) |
392 | 0 | { |
393 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
394 | |
|
395 | 0 | parameter p = encoder->get_param(heif_encoder_parameter_name_lossless); |
396 | 0 | *enable = p.value_int; |
397 | |
|
398 | 0 | return heif_error_ok; |
399 | 0 | } |
400 | | |
401 | | static heif_error x265_set_parameter_logging_level(void* encoder_raw, int logging) |
402 | 0 | { |
403 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
404 | |
|
405 | 0 | if (logging < 0 || logging > 4) { |
406 | 0 | return heif_error_invalid_parameter_value; |
407 | 0 | } |
408 | | |
409 | 0 | encoder->logLevel = logging; |
410 | |
|
411 | 0 | return heif_error_ok; |
412 | 0 | } |
413 | | |
414 | | static heif_error x265_get_parameter_logging_level(void* encoder_raw, int* loglevel) |
415 | 0 | { |
416 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
417 | |
|
418 | 0 | *loglevel = encoder->logLevel; |
419 | |
|
420 | 0 | return heif_error_ok; |
421 | 0 | } |
422 | | |
423 | | |
424 | | static heif_error x265_set_parameter_integer(void* encoder_raw, const char* name, int value) |
425 | 0 | { |
426 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
427 | |
|
428 | 0 | if (strcmp(name, heif_encoder_parameter_name_quality) == 0) { |
429 | 0 | return x265_set_parameter_quality(encoder, value); |
430 | 0 | } |
431 | 0 | else if (strcmp(name, heif_encoder_parameter_name_lossless) == 0) { |
432 | 0 | return x265_set_parameter_lossless(encoder, value); |
433 | 0 | } |
434 | 0 | else if (strcmp(name, kParam_TU_intra_depth) == 0) { |
435 | 0 | if (value < 1 || value > 4) { |
436 | 0 | return heif_error_invalid_parameter_value; |
437 | 0 | } |
438 | | |
439 | 0 | encoder->add_param(name, value); |
440 | 0 | return heif_error_ok; |
441 | 0 | } |
442 | 0 | else if (strcmp(name, kParam_complexity) == 0) { |
443 | 0 | if (value < 0 || value > 100) { |
444 | 0 | return heif_error_invalid_parameter_value; |
445 | 0 | } |
446 | | |
447 | 0 | encoder->add_param(name, value); |
448 | 0 | return heif_error_ok; |
449 | 0 | } |
450 | | |
451 | 0 | return heif_error_unsupported_parameter; |
452 | 0 | } |
453 | | |
454 | | static heif_error x265_get_parameter_integer(void* encoder_raw, const char* name, int* value) |
455 | 0 | { |
456 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
457 | |
|
458 | 0 | if (strcmp(name, heif_encoder_parameter_name_quality) == 0) { |
459 | 0 | return x265_get_parameter_quality(encoder, value); |
460 | 0 | } |
461 | 0 | else if (strcmp(name, heif_encoder_parameter_name_lossless) == 0) { |
462 | 0 | return x265_get_parameter_lossless(encoder, value); |
463 | 0 | } |
464 | 0 | else if (strcmp(name, kParam_TU_intra_depth) == 0) { |
465 | 0 | *value = encoder->get_param(name).value_int; |
466 | 0 | return heif_error_ok; |
467 | 0 | } |
468 | 0 | else if (strcmp(name, kParam_complexity) == 0) { |
469 | 0 | *value = encoder->get_param(name).value_int; |
470 | 0 | return heif_error_ok; |
471 | 0 | } |
472 | | |
473 | 0 | return heif_error_unsupported_parameter; |
474 | 0 | } |
475 | | |
476 | | |
477 | | static heif_error x265_set_parameter_boolean(void* encoder, const char* name, int value) |
478 | 0 | { |
479 | 0 | if (strcmp(name, heif_encoder_parameter_name_lossless) == 0) { |
480 | 0 | return x265_set_parameter_lossless(encoder, value); |
481 | 0 | } |
482 | | |
483 | 0 | return heif_error_unsupported_parameter; |
484 | 0 | } |
485 | | |
486 | | // Unused, will use "x265_get_parameter_integer" instead. |
487 | | /* |
488 | | static struct heif_error x265_get_parameter_boolean(void* encoder, const char* name, int* value) |
489 | | { |
490 | | if (strcmp(name, heif_encoder_parameter_name_lossless)==0) { |
491 | | return x265_get_parameter_lossless(encoder,value); |
492 | | } |
493 | | |
494 | | return heif_error_unsupported_parameter; |
495 | | } |
496 | | */ |
497 | | |
498 | | |
499 | | static bool string_list_contains(const char* const* values_list, const char* value) |
500 | 0 | { |
501 | 0 | for (int i = 0; values_list[i]; i++) { |
502 | 0 | if (strcmp(values_list[i], value) == 0) { |
503 | 0 | return true; |
504 | 0 | } |
505 | 0 | } |
506 | | |
507 | 0 | return false; |
508 | 0 | } |
509 | | |
510 | | |
511 | | static heif_error x265_set_parameter_string(void* encoder_raw, const char* name, const char* value) |
512 | 0 | { |
513 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
514 | |
|
515 | 0 | if (strcmp(name, kParam_preset) == 0) { |
516 | 0 | if (!string_list_contains(kParam_preset_valid_values, value)) { |
517 | 0 | return heif_error_invalid_parameter_value; |
518 | 0 | } |
519 | | |
520 | 0 | encoder->preset = value; |
521 | 0 | return heif_error_ok; |
522 | 0 | } |
523 | 0 | else if (strcmp(name, kParam_tune) == 0) { |
524 | 0 | if (!string_list_contains(kParam_tune_valid_values, value)) { |
525 | 0 | return heif_error_invalid_parameter_value; |
526 | 0 | } |
527 | | |
528 | 0 | encoder->tune = value; |
529 | 0 | return heif_error_ok; |
530 | 0 | } |
531 | 0 | else if (strncmp(name, "x265:", 5) == 0) { |
532 | 0 | encoder->add_param(name, std::string(value)); |
533 | 0 | return heif_error_ok; |
534 | 0 | } |
535 | 0 | else if (strcmp(name, kParam_chroma) == 0) { |
536 | 0 | if (strcmp(value, "420") == 0) { |
537 | 0 | encoder->chroma = heif_chroma_420; |
538 | 0 | return heif_error_ok; |
539 | 0 | } |
540 | 0 | else if (strcmp(value, "422") == 0) { |
541 | 0 | encoder->chroma = heif_chroma_422; |
542 | 0 | return heif_error_ok; |
543 | 0 | } |
544 | 0 | else if (strcmp(value, "444") == 0) { |
545 | 0 | encoder->chroma = heif_chroma_444; |
546 | 0 | return heif_error_ok; |
547 | 0 | } |
548 | 0 | else { |
549 | 0 | return heif_error_invalid_parameter_value; |
550 | 0 | } |
551 | 0 | } |
552 | | |
553 | 0 | return heif_error_unsupported_parameter; |
554 | 0 | } |
555 | | |
556 | | static void save_strcpy(char* dst, int dst_size, const char* src) |
557 | 0 | { |
558 | 0 | strncpy(dst, src, dst_size - 1); |
559 | 0 | dst[dst_size - 1] = 0; |
560 | 0 | } |
561 | | |
562 | | static heif_error x265_get_parameter_string(void* encoder_raw, const char* name, |
563 | | char* value, int value_size) |
564 | 0 | { |
565 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
566 | |
|
567 | 0 | if (strcmp(name, kParam_preset) == 0) { |
568 | 0 | save_strcpy(value, value_size, encoder->preset.c_str()); |
569 | 0 | return heif_error_ok; |
570 | 0 | } |
571 | 0 | else if (strcmp(name, kParam_tune) == 0) { |
572 | 0 | save_strcpy(value, value_size, encoder->tune.c_str()); |
573 | 0 | return heif_error_ok; |
574 | 0 | } |
575 | 0 | else if (strcmp(name, kParam_chroma) == 0) { |
576 | 0 | switch (encoder->chroma) { |
577 | 0 | case heif_chroma_420: |
578 | 0 | save_strcpy(value, value_size, "420"); |
579 | 0 | break; |
580 | 0 | case heif_chroma_422: |
581 | 0 | save_strcpy(value, value_size, "422"); |
582 | 0 | break; |
583 | 0 | case heif_chroma_444: |
584 | 0 | save_strcpy(value, value_size, "444"); |
585 | 0 | break; |
586 | 0 | default: |
587 | 0 | assert(false); |
588 | 0 | return heif_error_invalid_parameter_value; |
589 | 0 | } |
590 | 0 | return heif_error_ok; |
591 | 0 | } |
592 | | |
593 | 0 | return heif_error_unsupported_parameter; |
594 | 0 | } |
595 | | |
596 | | |
597 | | static void x265_set_default_parameters(void* encoder) |
598 | 0 | { |
599 | 0 | for (const heif_encoder_parameter** p = x265_encoder_parameter_ptrs; *p; p++) { |
600 | 0 | const heif_encoder_parameter* param = *p; |
601 | |
|
602 | 0 | if (param->has_default) { |
603 | 0 | switch (param->type) { |
604 | 0 | case heif_encoder_parameter_type_integer: |
605 | 0 | x265_set_parameter_integer(encoder, param->name, param->integer.default_value); |
606 | 0 | break; |
607 | 0 | case heif_encoder_parameter_type_boolean: |
608 | 0 | x265_set_parameter_boolean(encoder, param->name, param->boolean.default_value); |
609 | 0 | break; |
610 | 0 | case heif_encoder_parameter_type_string: |
611 | 0 | x265_set_parameter_string(encoder, param->name, param->string.default_value); |
612 | 0 | break; |
613 | 0 | } |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | | |
618 | | |
619 | | static void x265_query_input_colorspace(heif_colorspace* colorspace, heif_chroma* chroma) |
620 | 0 | { |
621 | 0 | if (*colorspace == heif_colorspace_monochrome) { |
622 | 0 | *colorspace = heif_colorspace_monochrome; |
623 | 0 | *chroma = heif_chroma_monochrome; |
624 | 0 | } |
625 | 0 | else { |
626 | 0 | *colorspace = heif_colorspace_YCbCr; |
627 | 0 | *chroma = heif_chroma_420; |
628 | 0 | } |
629 | 0 | } |
630 | | |
631 | | |
632 | | static void x265_query_input_colorspace2(void* encoder_raw, heif_colorspace* colorspace, heif_chroma* chroma) |
633 | 0 | { |
634 | 0 | auto* encoder = (encoder_struct_x265*) encoder_raw; |
635 | |
|
636 | 0 | if (*colorspace == heif_colorspace_monochrome) { |
637 | 0 | *colorspace = heif_colorspace_monochrome; |
638 | 0 | *chroma = heif_chroma_monochrome; |
639 | 0 | } |
640 | 0 | else { |
641 | 0 | *colorspace = heif_colorspace_YCbCr; |
642 | 0 | *chroma = encoder->chroma; |
643 | 0 | } |
644 | 0 | } |
645 | | |
646 | | |
647 | | static int rounded_size(int s) |
648 | 0 | { |
649 | 0 | s = (s + 1) & ~1; |
650 | |
|
651 | 0 | if (s < 64) { |
652 | 0 | s = 64; |
653 | 0 | } |
654 | |
|
655 | 0 | return s; |
656 | 0 | } |
657 | | |
658 | | #if 0 |
659 | | static const char* naltype_table[] = { |
660 | | /* 0 */ "TRAIL_N", |
661 | | /* 1 */ "TRAIL_R", |
662 | | /* 2 */ "TSA_N", |
663 | | /* 3 */ "TSA_R", |
664 | | /* 4 */ "STSA_N", |
665 | | /* 5 */ "STSA_R", |
666 | | /* 6 */ "RADL_N", |
667 | | /* 7 */ "RADL_R", |
668 | | /* 8 */ "RASL_N", |
669 | | /* 9 */ "RASL_R", |
670 | | /* 10 */ "RSV_VCL_N10", |
671 | | /* 11 */ "RSV_VCL_R11", |
672 | | /* 12 */ "RSV_VCL_N12", |
673 | | /* 13 */ "RSV_VCL_R13", |
674 | | /* 14 */ "RSV_VCL_N14", |
675 | | /* 15 */ "RSV_VCL_R15", |
676 | | /* 16 */ "BLA_W_LP", |
677 | | /* 17 */ "BLA_W_RADL", |
678 | | /* 18 */ "BLA_N_LP", |
679 | | /* 19 */ "IDR_W_RADL", |
680 | | /* 20 */ "IDR_N_LP", |
681 | | /* 21 */ "CRA_NUT", |
682 | | /* 22 */ "RSV_IRAP_VCL22", |
683 | | /* 23 */ "RSV_IRAP_VCL23", |
684 | | /* 24 */ "RSV_VCL24", |
685 | | /* 25 */ "RSV_VCL25", |
686 | | /* 26 */ "RSV_VCL26", |
687 | | /* 27 */ "RSV_VCL27", |
688 | | /* 28 */ "RSV_VCL28", |
689 | | /* 29 */ "RSV_VCL29", |
690 | | /* 30 */ "RSV_VCL30", |
691 | | /* 31 */ "RSV_VCL31", |
692 | | /* 32 */ "VPS_NUT", |
693 | | /* 33 */ "SPS_NUT", |
694 | | /* 34 */ "PPS_NUT", |
695 | | /* 35 */ "AUD_NUT", |
696 | | /* 36 */ "EOS_NUT", |
697 | | /* 37 */ "EOB_NUT", |
698 | | /* 38 */ "FD_NUT", |
699 | | /* 39 */ "PREFIX_SEI_NUT", |
700 | | /* 40 */ "SUFFIX_SEI_NUT" |
701 | | }; |
702 | | |
703 | | |
704 | | static const char* naltype(uint8_t type) |
705 | | { |
706 | | if (type <= 40) { |
707 | | return naltype_table[type]; |
708 | | } |
709 | | else { |
710 | | return "reserved"; |
711 | | } |
712 | | } |
713 | | #endif |
714 | | |
715 | | void encoder_struct_x265::append_nal_packets(x265_nal* nals, uint32_t num_nals, uintptr_t frameNr) |
716 | 0 | { |
717 | 0 | for (uint32_t nal_idx=0 ; nal_idx < num_nals; nal_idx++) { |
718 | 0 | uint8_t* data = nals[nal_idx].payload; |
719 | 0 | uint32_t size = nals[nal_idx].sizeBytes; |
720 | | |
721 | | // --- skip start code --- |
722 | | |
723 | | // skip '0' bytes |
724 | 0 | while (*data == 0 && size > 0) { |
725 | 0 | data++; |
726 | 0 | size--; |
727 | 0 | } |
728 | | |
729 | | // skip '1' byte |
730 | 0 | data++; |
731 | 0 | size--; |
732 | | |
733 | | |
734 | | // --- skip NALs with irrelevant data --- |
735 | |
|
736 | 0 | if (size >= 3 && data[0] == 0x4e && data[2] == 5) { |
737 | | // skip "unregistered user data SEI" |
738 | 0 | } |
739 | 0 | else { |
740 | | // output NAL |
741 | |
|
742 | 0 | Packet pkt; |
743 | 0 | pkt.data.resize(size); |
744 | 0 | memcpy(pkt.data.data(), data, size); |
745 | 0 | pkt.frameNr = frameNr; |
746 | |
|
747 | 0 | output_data.push_back(pkt); |
748 | 0 | } |
749 | 0 | } |
750 | 0 | } |
751 | | |
752 | | |
753 | | static heif_error x265_start_sequence_encoding_intern(void* encoder_raw, const heif_image* image, |
754 | | enum heif_image_input_class input_class, |
755 | | uint32_t framerate_num, uint32_t framerate_denom, |
756 | | const heif_sequence_encoding_options* options, |
757 | | bool image_sequence) |
758 | 0 | { |
759 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
760 | | |
761 | | |
762 | | // close previous encoder if there is still one hanging around |
763 | 0 | if (encoder->encoder) { |
764 | 0 | const x265_api* api = x265_api_get(encoder->bit_depth); |
765 | 0 | api->encoder_close(encoder->encoder); |
766 | 0 | encoder->encoder = nullptr; |
767 | 0 | } |
768 | | |
769 | |
|
770 | 0 | int bit_depth = heif_image_get_bits_per_pixel_range(image, heif_channel_Y); |
771 | 0 | bool isGreyscale = (heif_image_get_colorspace(image) == heif_colorspace_monochrome); |
772 | 0 | heif_chroma chroma = heif_image_get_chroma_format(image); |
773 | |
|
774 | 0 | const x265_api* api = x265_api_get(bit_depth); |
775 | 0 | encoder->api = api; |
776 | 0 | if (api == nullptr) { |
777 | 0 | struct heif_error err = { |
778 | 0 | heif_error_Encoder_plugin_error, |
779 | 0 | heif_suberror_Unsupported_bit_depth, |
780 | 0 | kError_unsupported_bit_depth |
781 | 0 | }; |
782 | 0 | return err; |
783 | 0 | } |
784 | | |
785 | 0 | x265_param* param = api->param_alloc(); |
786 | 0 | if (encoder->param) { |
787 | 0 | api->param_free(encoder->param); |
788 | 0 | } |
789 | 0 | encoder->param = param; |
790 | |
|
791 | 0 | api->param_default_preset(param, encoder->preset.c_str(), encoder->tune.c_str()); |
792 | |
|
793 | 0 | if (bit_depth == 8) { |
794 | 0 | if (image_sequence) { |
795 | 0 | api->param_apply_profile(param, "main"); |
796 | 0 | } |
797 | 0 | else { |
798 | 0 | api->param_apply_profile(param, "mainstillpicture"); |
799 | 0 | } |
800 | 0 | } |
801 | 0 | else if (bit_depth == 10) api->param_apply_profile(param, "main10-intra"); |
802 | 0 | else if (bit_depth == 12) api->param_apply_profile(param, "main12-intra"); |
803 | 0 | else { |
804 | 0 | api->param_free(param); |
805 | 0 | return heif_error_unsupported_parameter; |
806 | 0 | } |
807 | | |
808 | | |
809 | | // x265 cannot encode images smaller than one CTU size |
810 | | // https://bitbucket.org/multicoreware/x265/issues/475/x265-does-not-allow-image-sizes-smaller |
811 | | // -> use smaller CTU sizes for very small images |
812 | 0 | const char* ctu = NULL; |
813 | 0 | int ctuSize = 64; |
814 | |
|
815 | 0 | #if 1 |
816 | 0 | while (ctuSize > 16 && |
817 | 0 | (heif_image_get_width(image, heif_channel_Y) < ctuSize || |
818 | 0 | heif_image_get_height(image, heif_channel_Y) < ctuSize)) { |
819 | 0 | ctuSize /= 2; |
820 | 0 | } |
821 | |
|
822 | 0 | if (ctuSize < 16) { |
823 | 0 | api->param_free(param); |
824 | 0 | return { |
825 | 0 | heif_error_Encoder_plugin_error, |
826 | 0 | heif_suberror_Invalid_parameter_value, |
827 | 0 | kError_unsupported_image_size |
828 | 0 | }; |
829 | 0 | } |
830 | | #else |
831 | | // TODO: There seems to be a bug in x265 where increasing the CTU size between |
832 | | // multiple encoding jobs causes a segmentation fault. E.g. encoding multiple |
833 | | // times with a CTU of 16 works, the next encoding with a CTU of 32 crashes. |
834 | | // Use hardcoded value of 64 and reject images that are too small. |
835 | | |
836 | | if (heif_image_get_width(image, heif_channel_Y) < ctuSize || |
837 | | heif_image_get_height(image, heif_channel_Y) < ctuSize) { |
838 | | api->param_free(param); |
839 | | struct heif_error err = { |
840 | | heif_error_Encoder_plugin_error, |
841 | | heif_suberror_Invalid_parameter_value, |
842 | | kError_unsupported_image_size |
843 | | }; |
844 | | return err; |
845 | | } |
846 | | #endif |
847 | | |
848 | | // ctuSize should be a power of 2 in [16;64] |
849 | 0 | switch (ctuSize) { |
850 | 0 | case 64: |
851 | 0 | ctu = "64"; |
852 | 0 | break; |
853 | 0 | case 32: |
854 | 0 | ctu = "32"; |
855 | 0 | break; |
856 | 0 | case 16: |
857 | 0 | ctu = "16"; |
858 | 0 | break; |
859 | 0 | default: |
860 | 0 | return { |
861 | 0 | heif_error_Encoder_plugin_error, |
862 | 0 | heif_suberror_Invalid_parameter_value, |
863 | 0 | kError_unsupported_ctu_size |
864 | 0 | }; |
865 | 0 | } |
866 | | |
867 | 0 | if (options) { |
868 | 0 | if (options->keyframe_distance_min) { |
869 | 0 | param->keyframeMin = options->keyframe_distance_min; |
870 | 0 | } |
871 | |
|
872 | 0 | if (options->keyframe_distance_max) { |
873 | 0 | param->keyframeMax = options->keyframe_distance_max; |
874 | 0 | } |
875 | |
|
876 | 0 | switch (options->gop_structure) { |
877 | 0 | case heif_sequence_gop_structure_intra_only: |
878 | 0 | param->bframes = 0; |
879 | 0 | param->keyframeMin=1; |
880 | 0 | param->keyframeMax=1; |
881 | 0 | break; |
882 | 0 | case heif_sequence_gop_structure_lowdelay: |
883 | 0 | param->bframes = 0; |
884 | 0 | break; |
885 | 0 | case heif_sequence_gop_structure_unrestricted: |
886 | 0 | param->bframes = 1; |
887 | 0 | break; |
888 | 0 | } |
889 | 0 | } |
890 | | |
891 | | // BPG uses CQP. It does not seem to be better though. |
892 | | // param->rc.rateControlMode = X265_RC_CQP; |
893 | | // param->rc.qp = (100 - encoder->quality)/2; |
894 | 0 | param->totalFrames = image_sequence ? 0 : 1; // TODO |
895 | |
|
896 | 0 | if (isGreyscale) { |
897 | 0 | param->internalCsp = X265_CSP_I400; |
898 | 0 | } |
899 | 0 | else if (chroma == heif_chroma_420) { |
900 | 0 | param->internalCsp = X265_CSP_I420; |
901 | 0 | } |
902 | 0 | else if (chroma == heif_chroma_422) { |
903 | 0 | param->internalCsp = X265_CSP_I422; |
904 | 0 | } |
905 | 0 | else if (chroma == heif_chroma_444) { |
906 | 0 | param->internalCsp = X265_CSP_I444; |
907 | 0 | } |
908 | |
|
909 | 0 | if (chroma != heif_chroma_monochrome) { |
910 | 0 | int w = heif_image_get_width(image, heif_channel_Y); |
911 | 0 | int h = heif_image_get_height(image, heif_channel_Y); |
912 | 0 | if (chroma != heif_chroma_444) { w = (w + 1) / 2; } |
913 | 0 | if (chroma == heif_chroma_420) { h = (h + 1) / 2; } |
914 | |
|
915 | 0 | assert(heif_image_get_width(image, heif_channel_Cb)==w); |
916 | 0 | assert(heif_image_get_width(image, heif_channel_Cr)==w); |
917 | 0 | assert(heif_image_get_height(image, heif_channel_Cb)==h); |
918 | 0 | assert(heif_image_get_height(image, heif_channel_Cr)==h); |
919 | 0 | (void) w; |
920 | 0 | (void) h; |
921 | 0 | } |
922 | | |
923 | 0 | api->param_parse(param, "info", "0"); |
924 | 0 | api->param_parse(param, "limit-modes", "0"); |
925 | 0 | api->param_parse(param, "limit-refs", "0"); |
926 | 0 | api->param_parse(param, "ctu", ctu); |
927 | 0 | api->param_parse(param, "rskip", "0"); |
928 | |
|
929 | 0 | api->param_parse(param, "rect", "1"); |
930 | 0 | api->param_parse(param, "amp", "1"); |
931 | 0 | api->param_parse(param, "aq-mode", "1"); |
932 | 0 | api->param_parse(param, "psy-rd", "1.0"); |
933 | 0 | api->param_parse(param, "psy-rdoq", "1.0"); |
934 | |
|
935 | 0 | heif_color_profile_nclx* nclx = nullptr; |
936 | 0 | heif_error err = heif_image_get_nclx_color_profile(image, &nclx); |
937 | 0 | if (err.code != heif_error_Ok) { |
938 | 0 | assert(nclx == nullptr); |
939 | 0 | } |
940 | | |
941 | | // make sure NCLX profile is deleted at end of function |
942 | 0 | auto nclx_deleter = std::unique_ptr<heif_color_profile_nclx, void (*)(heif_color_profile_nclx*)>(nclx, heif_nclx_color_profile_free); |
943 | |
|
944 | 0 | if (nclx) { |
945 | 0 | api->param_parse(param, "range", nclx->full_range_flag ? "full" : "limited"); |
946 | 0 | } |
947 | 0 | else { |
948 | 0 | api->param_parse(param, "range", "full"); |
949 | 0 | } |
950 | |
|
951 | 0 | if (nclx && |
952 | 0 | (input_class == heif_image_input_class_normal || |
953 | 0 | input_class == heif_image_input_class_thumbnail)) { |
954 | |
|
955 | 0 | { |
956 | 0 | std::stringstream sstr; |
957 | 0 | sstr << nclx->color_primaries; |
958 | 0 | api->param_parse(param, "colorprim", sstr.str().c_str()); |
959 | 0 | } |
960 | |
|
961 | 0 | { |
962 | 0 | std::stringstream sstr; |
963 | 0 | sstr << nclx->transfer_characteristics; |
964 | 0 | api->param_parse(param, "transfer", sstr.str().c_str()); |
965 | 0 | } |
966 | |
|
967 | 0 | { |
968 | 0 | std::stringstream sstr; |
969 | 0 | sstr << nclx->matrix_coefficients; |
970 | 0 | api->param_parse(param, "colormatrix", sstr.str().c_str()); |
971 | 0 | } |
972 | 0 | } |
973 | | |
974 | | // Write the pixel aspect ratio into the VUI. Extended_SAR stores sar_width/sar_height |
975 | | // as u(16), so ratios that do not fit are only signalled through the pasp property. |
976 | | // A 1:1 ratio is omitted (VUI absence already means unspecified/square). |
977 | | // Set before the user parameters below so that an explicit "x265:sar" takes precedence. |
978 | |
|
979 | 0 | uint32_t aspect_h = 1, aspect_v = 1; |
980 | 0 | heif_image_get_pixel_aspect_ratio(image, &aspect_h, &aspect_v); |
981 | 0 | if ((input_class == heif_image_input_class_normal || |
982 | 0 | input_class == heif_image_input_class_thumbnail) && |
983 | 0 | aspect_h != aspect_v && |
984 | 0 | aspect_h > 0 && aspect_v > 0 && |
985 | 0 | aspect_h <= 0xFFFF && aspect_v <= 0xFFFF) { |
986 | 0 | std::stringstream sstr; |
987 | 0 | sstr << aspect_h << ':' << aspect_v; |
988 | 0 | api->param_parse(param, "sar", sstr.str().c_str()); |
989 | 0 | } |
990 | |
|
991 | 0 | for (const auto& p : encoder->parameters) { |
992 | 0 | if (p.name == heif_encoder_parameter_name_quality) { |
993 | | // quality=0 -> crf=50 |
994 | | // quality=50 -> crf=25 |
995 | | // quality=100 -> crf=0 |
996 | |
|
997 | 0 | param->rc.rfConstant = (100 - p.value_int) / 2.0; |
998 | 0 | } |
999 | 0 | else if (p.name == heif_encoder_parameter_name_lossless) { |
1000 | 0 | param->bLossless = p.value_int; |
1001 | 0 | } |
1002 | 0 | else if (p.name == kParam_TU_intra_depth) { |
1003 | 0 | auto valString = std::to_string(p.value_int); |
1004 | 0 | api->param_parse(param, "tu-intra-depth", valString.c_str()); |
1005 | 0 | } |
1006 | 0 | else if (p.name == kParam_complexity) { |
1007 | 0 | const int complexity = p.value_int; |
1008 | |
|
1009 | 0 | if (complexity >= 60) { |
1010 | 0 | api->param_parse(param, "rd-refine", "1"); // increases computation time |
1011 | 0 | api->param_parse(param, "rd", "6"); |
1012 | 0 | } |
1013 | |
|
1014 | 0 | if (complexity >= 70) { |
1015 | 0 | api->param_parse(param, "cu-lossless", "1"); // increases computation time |
1016 | 0 | } |
1017 | |
|
1018 | 0 | if (complexity >= 90) { |
1019 | 0 | api->param_parse(param, "wpp", "0"); // setting to 0 significantly increases computation time |
1020 | 0 | } |
1021 | 0 | } |
1022 | 0 | else if (strncmp(p.name.c_str(), "x265:", 5) == 0) { |
1023 | 0 | std::string x265p = p.name.substr(5); |
1024 | 0 | if (api->param_parse(param, x265p.c_str(), p.value_string.c_str()) < 0) { |
1025 | 0 | encoder->last_error_message = std::string{"Unsupported x265 encoder parameter: "} + x265p; |
1026 | |
|
1027 | 0 | return { |
1028 | 0 | .code = heif_error_Usage_error, |
1029 | 0 | .subcode = heif_suberror_Unsupported_parameter, |
1030 | 0 | .message = encoder->last_error_message.c_str() |
1031 | 0 | }; |
1032 | 0 | } |
1033 | 0 | } |
1034 | 0 | } |
1035 | | |
1036 | 0 | param->logLevel = encoder->logLevel; |
1037 | | |
1038 | |
|
1039 | 0 | param->sourceWidth = heif_image_get_width(image, heif_channel_Y); |
1040 | 0 | param->sourceHeight = heif_image_get_height(image, heif_channel_Y); |
1041 | 0 | param->internalBitDepth = bit_depth; |
1042 | |
|
1043 | 0 | param->sourceWidth = rounded_size(param->sourceWidth); |
1044 | 0 | param->sourceHeight = rounded_size(param->sourceHeight); |
1045 | | |
1046 | | // x265 sizes some of its internal picture buffers with 32-bit arithmetic and, up to at least |
1047 | | // v3.5, neither validates the picture size nor the result of these allocations. Pictures of |
1048 | | // roughly 700 Mpixel or more make it crash in one of its worker threads (GHSA-2c3g-p585-8rpq). |
1049 | | // Newer x265 versions refuse pictures above the HEVC Level 7.2 maximum of 142,606,336 luma |
1050 | | // samples (16384x8704) in x265_check_params() unless non-conformance is explicitly allowed. |
1051 | | // Apply the same limit here, so that the behavior is the same with all x265 versions and the |
1052 | | // encoder is never opened with a picture it cannot handle. |
1053 | 0 | const uint64_t max_luma_samples = 142606336; |
1054 | 0 | if (static_cast<uint64_t>(param->sourceWidth) * static_cast<uint64_t>(param->sourceHeight) > max_luma_samples) { |
1055 | 0 | return {heif_error_Encoding_error, |
1056 | 0 | heif_suberror_Encoder_encoding, |
1057 | 0 | "Image too large for x265: at most 142,606,336 luma samples (HEVC Level 7.2) are supported"}; |
1058 | 0 | } |
1059 | | |
1060 | 0 | param->fpsNum = framerate_num; |
1061 | 0 | param->fpsDenom = framerate_denom; |
1062 | |
|
1063 | 0 | encoder->bit_depth = bit_depth; |
1064 | |
|
1065 | 0 | encoder->encoder = api->encoder_open(param); |
1066 | 0 | if (encoder->encoder == nullptr) { |
1067 | | // x265 rejected the parameters (e.g. newer versions refuse oversized pictures). |
1068 | 0 | return {heif_error_Encoding_error, |
1069 | 0 | heif_suberror_Encoder_initialization, |
1070 | 0 | "x265 encoder could not be opened with the given parameters"}; |
1071 | 0 | } |
1072 | | |
1073 | 0 | if (image_sequence) { |
1074 | 0 | x265_nal* nals = nullptr; |
1075 | 0 | uint32_t num_nals = 0; |
1076 | |
|
1077 | 0 | api->encoder_headers(encoder->encoder, |
1078 | 0 | &nals, |
1079 | 0 | &num_nals); |
1080 | |
|
1081 | 0 | encoder->append_nal_packets(nals, num_nals, 0); |
1082 | |
|
1083 | 0 | for (uint32_t i = 0; i < num_nals; i++) { |
1084 | | //std::cout << "dequeue header NAL : " << naltype(encoder->nals[i].type) << "\n"; |
1085 | 0 | } |
1086 | 0 | } |
1087 | |
|
1088 | 0 | return heif_error_ok; |
1089 | 0 | } |
1090 | | |
1091 | | |
1092 | | static heif_error x265_start_sequence_encoding(void* encoder_raw, const heif_image* image, |
1093 | | enum heif_image_input_class input_class, |
1094 | | uint32_t framerate_num, uint32_t framerate_denom, |
1095 | | const heif_sequence_encoding_options* options) |
1096 | 0 | { |
1097 | 0 | return x265_start_sequence_encoding_intern(encoder_raw, image, input_class, |
1098 | 0 | framerate_num, framerate_denom, options, true); |
1099 | 0 | } |
1100 | | |
1101 | | |
1102 | | static heif_error x265_encode_sequence_frame(void* encoder_raw, const heif_image* image, |
1103 | | uintptr_t frame_nr) |
1104 | 0 | { |
1105 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
1106 | |
|
1107 | 0 | if (!encoder->api) { |
1108 | 0 | return { |
1109 | 0 | heif_error_Usage_error, |
1110 | 0 | heif_suberror_Unspecified, |
1111 | 0 | "called plugin encode_sequence_frame() without start_sequence_encoding()" |
1112 | 0 | }; |
1113 | 0 | } |
1114 | | |
1115 | | // HEVC can signal different luma and chroma bit depths, but x265 has a |
1116 | | // single internal bit depth and cannot produce such a stream. Whether this |
1117 | | // build of libx265 has 10 or 12 bit support is checked separately via |
1118 | | // x265_api_get(). |
1119 | 0 | heif_error input_error = check_encoder_input_image(image, /*supports_monochrome=*/true, |
1120 | 0 | {8, 10, 12}); |
1121 | 0 | if (input_error.code != heif_error_Ok) { |
1122 | 0 | return input_error; |
1123 | 0 | } |
1124 | | |
1125 | | // pic->bitDepth below is the depth the encoder was opened with, while the plane |
1126 | | // pointers are this frame's. A deeper first frame would make libx265 read the |
1127 | | // planes of a shallower later frame at two bytes per sample. |
1128 | 0 | input_error = check_sequence_frame_bit_depth(image, encoder->bit_depth); |
1129 | 0 | if (input_error.code != heif_error_Ok) { |
1130 | 0 | return input_error; |
1131 | 0 | } |
1132 | | |
1133 | 0 | const x265_api* api = encoder->api; |
1134 | |
|
1135 | 0 | heif_error err; |
1136 | | |
1137 | | // Note: it is ok to cast away the const, as the image content is not changed. |
1138 | | // However, we have to guarantee that there are no plane pointers or stride values kept over calling the svt_encode_image() function. |
1139 | 0 | err = heif_image_extend_padding_to_size(const_cast<heif_image*>(image), |
1140 | 0 | encoder->param->sourceWidth, |
1141 | 0 | encoder->param->sourceHeight); |
1142 | 0 | if (err.code) { |
1143 | 0 | return err; |
1144 | 0 | } |
1145 | | |
1146 | 0 | x265_picture* pic = api->picture_alloc(); |
1147 | 0 | api->picture_init(encoder->param, pic); |
1148 | | |
1149 | | // TODO: check that all input images use the same color format |
1150 | |
|
1151 | 0 | bool isGreyscale = (heif_image_get_colorspace(image) == heif_colorspace_monochrome); |
1152 | |
|
1153 | 0 | if (isGreyscale) { |
1154 | 0 | pic->planes[0] = (void*) heif_image_get_plane_readonly(image, heif_channel_Y, &pic->stride[0]); |
1155 | 0 | } |
1156 | 0 | else { |
1157 | 0 | pic->planes[0] = (void*) heif_image_get_plane_readonly(image, heif_channel_Y, &pic->stride[0]); |
1158 | 0 | pic->planes[1] = (void*) heif_image_get_plane_readonly(image, heif_channel_Cb, &pic->stride[1]); |
1159 | 0 | pic->planes[2] = (void*) heif_image_get_plane_readonly(image, heif_channel_Cr, &pic->stride[2]); |
1160 | 0 | } |
1161 | |
|
1162 | 0 | pic->bitDepth = encoder->bit_depth; |
1163 | | // The codec hands this opaque pointer back unchanged; it carries an integer, not an address. |
1164 | 0 | pic->userData = reinterpret_cast<void*>(frame_nr); // NOLINT(performance-no-int-to-ptr) |
1165 | |
|
1166 | 0 | x265_nal* nals = nullptr; |
1167 | 0 | uint32_t num_nals = 0; |
1168 | |
|
1169 | | #if X265_BUILD == 212 |
1170 | | // In x265 build version 212, the signature of the encoder_encode() function was changed. But it was changed back in version 213. |
1171 | | // https://bitbucket.org/multicoreware/x265_git/issues/952/crash-in-libheif-tests |
1172 | | x265_picture* out_pic = NULL; |
1173 | | api->encoder_encode(encoder->encoder, |
1174 | | &nals, |
1175 | | &num_nals, |
1176 | | pic, |
1177 | | &out_pic); |
1178 | | if (num_nals > 0 && out_pic) { |
1179 | | uintptr_t frameNr = reinterpret_cast<uintptr_t>(out_pic->userData); |
1180 | | encoder->append_nal_packets(nals, num_nals, frameNr); |
1181 | | } |
1182 | | #else |
1183 | 0 | x265_picture out_pic; |
1184 | 0 | api->encoder_encode(encoder->encoder, |
1185 | 0 | &nals, |
1186 | 0 | &num_nals, |
1187 | 0 | pic, |
1188 | 0 | &out_pic); |
1189 | |
|
1190 | 0 | if (num_nals > 0) { |
1191 | 0 | uintptr_t frameNr = reinterpret_cast<uintptr_t>(out_pic.userData); |
1192 | 0 | encoder->append_nal_packets(nals, num_nals, frameNr); |
1193 | 0 | } |
1194 | |
|
1195 | 0 | #endif |
1196 | |
|
1197 | 0 | api->picture_free(pic); |
1198 | |
|
1199 | 0 | return heif_error_ok; |
1200 | 0 | } |
1201 | | |
1202 | | |
1203 | | static heif_error x265_end_sequence_encoding(void* encoder_raw) |
1204 | 0 | { |
1205 | 0 | encoder_struct_x265* encoder = (encoder_struct_x265*) encoder_raw; |
1206 | |
|
1207 | 0 | const x265_api* api = encoder->api; |
1208 | |
|
1209 | 0 | x265_nal* nals = nullptr; |
1210 | 0 | uint32_t num_nals = 0; |
1211 | |
|
1212 | | #if X265_BUILD == 212 |
1213 | | x265_picture* out_pic = NULL; |
1214 | | int result = api->encoder_encode(encoder->encoder, |
1215 | | &nals, |
1216 | | &num_nals, |
1217 | | NULL, |
1218 | | &out_pic); |
1219 | | #else |
1220 | 0 | x265_picture out_pic; |
1221 | 0 | int result = api->encoder_encode(encoder->encoder, |
1222 | 0 | &nals, |
1223 | 0 | &num_nals, |
1224 | 0 | NULL, |
1225 | 0 | &out_pic); |
1226 | 0 | #endif |
1227 | 0 | if (result > 0 && num_nals > 0) { |
1228 | | #if X265_BUILD == 212 |
1229 | | uintptr_t frameNr = out_pic ? reinterpret_cast<uintptr_t>(out_pic->userData) : 0; |
1230 | | #else |
1231 | 0 | uintptr_t frameNr = reinterpret_cast<uintptr_t>(out_pic.userData); |
1232 | 0 | #endif |
1233 | 0 | encoder->append_nal_packets(nals, num_nals, frameNr); |
1234 | 0 | } |
1235 | |
|
1236 | 0 | encoder->api->param_free(encoder->param); |
1237 | 0 | encoder->param = nullptr; |
1238 | |
|
1239 | 0 | return heif_error_ok; |
1240 | 0 | } |
1241 | | |
1242 | | |
1243 | | static heif_error x265_encode_image(void* encoder_raw, const heif_image* image, |
1244 | | heif_image_input_class input_class) |
1245 | 0 | { |
1246 | 0 | heif_error err; |
1247 | 0 | err = x265_start_sequence_encoding_intern(encoder_raw, image, input_class, 1,25, nullptr, false); |
1248 | 0 | if (err.code) { |
1249 | 0 | return err; |
1250 | 0 | } |
1251 | | |
1252 | 0 | err = x265_encode_sequence_frame(encoder_raw, image, 0); |
1253 | 0 | if (err.code) { |
1254 | 0 | return err; |
1255 | 0 | } |
1256 | | |
1257 | 0 | x265_end_sequence_encoding(encoder_raw); |
1258 | |
|
1259 | 0 | return heif_error_ok; |
1260 | 0 | } |
1261 | | |
1262 | | |
1263 | | static heif_error x265_get_compressed_data_intern(void* encoder_raw, uint8_t** data, int* size, |
1264 | | uintptr_t* out_frame_nr) |
1265 | 0 | { |
1266 | 0 | encoder_struct_x265* encoder = ( encoder_struct_x265*) encoder_raw; |
1267 | |
|
1268 | 0 | if (encoder->output_data.empty()) { |
1269 | 0 | *data = nullptr; |
1270 | 0 | *size = 0; |
1271 | |
|
1272 | 0 | return heif_error_ok; |
1273 | 0 | } |
1274 | | |
1275 | 0 | encoder->active_output_nal = std::move(encoder->output_data.front().data); |
1276 | |
|
1277 | 0 | if (out_frame_nr) { |
1278 | 0 | *out_frame_nr = encoder->output_data.front().frameNr; |
1279 | 0 | } |
1280 | |
|
1281 | 0 | encoder->output_data.pop_front(); |
1282 | |
|
1283 | 0 | *data = encoder->active_output_nal.data(); |
1284 | 0 | *size = static_cast<int>(encoder->active_output_nal.size()); |
1285 | |
|
1286 | 0 | return heif_error_ok; |
1287 | 0 | } |
1288 | | |
1289 | | |
1290 | | static heif_error x265_get_compressed_data(void* encoder_raw, uint8_t** data, int* size, |
1291 | | heif_encoded_data_type* type) |
1292 | 0 | { |
1293 | 0 | return x265_get_compressed_data_intern(encoder_raw, data, size, nullptr); |
1294 | 0 | } |
1295 | | |
1296 | | static heif_error x265_get_compressed_data2(void* encoder_raw, uint8_t** data, int* size, |
1297 | | uintptr_t* frame_nr, int* is_keyframe, |
1298 | | int* more_frame_packets) |
1299 | 0 | { |
1300 | 0 | return x265_get_compressed_data_intern(encoder_raw, data, size, frame_nr); |
1301 | 0 | } |
1302 | | |
1303 | | |
1304 | | static const heif_encoder_plugin encoder_plugin_x265 |
1305 | | { |
1306 | | /* plugin_api_version */ 4, |
1307 | | /* compression_format */ heif_compression_HEVC, |
1308 | | /* id_name */ "x265", |
1309 | | /* priority */ X265_PLUGIN_PRIORITY, |
1310 | | /* supports_lossy_compression */ true, |
1311 | | /* supports_lossless_compression */ true, |
1312 | | /* get_plugin_name */ x265_plugin_name, |
1313 | | /* init_plugin */ x265_init_plugin, |
1314 | | /* cleanup_plugin */ x265_cleanup_plugin, |
1315 | | /* new_encoder */ x265_new_encoder, |
1316 | | /* free_encoder */ x265_free_encoder, |
1317 | | /* set_parameter_quality */ x265_set_parameter_quality, |
1318 | | /* get_parameter_quality */ x265_get_parameter_quality, |
1319 | | /* set_parameter_lossless */ x265_set_parameter_lossless, |
1320 | | /* get_parameter_lossless */ x265_get_parameter_lossless, |
1321 | | /* set_parameter_logging_level */ x265_set_parameter_logging_level, |
1322 | | /* get_parameter_logging_level */ x265_get_parameter_logging_level, |
1323 | | /* list_parameters */ x265_list_parameters, |
1324 | | /* set_parameter_integer */ x265_set_parameter_integer, |
1325 | | /* get_parameter_integer */ x265_get_parameter_integer, |
1326 | | /* set_parameter_boolean */ x265_set_parameter_integer, // boolean also maps to integer function |
1327 | | /* get_parameter_boolean */ x265_get_parameter_integer, // boolean also maps to integer function |
1328 | | /* set_parameter_string */ x265_set_parameter_string, |
1329 | | /* get_parameter_string */ x265_get_parameter_string, |
1330 | | /* query_input_colorspace */ x265_query_input_colorspace, |
1331 | | /* encode_image */ x265_encode_image, |
1332 | | /* get_compressed_data */ x265_get_compressed_data, |
1333 | | /* query_input_colorspace (v2) */ x265_query_input_colorspace2, |
1334 | | /* query_encoded_size (v3) */ nullptr, |
1335 | | /* minimum_required_libheif_version */ LIBHEIF_MAKE_VERSION(1,21,0), |
1336 | | /* start_sequence_encoding (v4) */ x265_start_sequence_encoding, |
1337 | | /* encode_sequence_frame (v4) */ x265_encode_sequence_frame, |
1338 | | /* end_sequence_encoding (v4) */ x265_end_sequence_encoding, |
1339 | | /* get_compressed_data2 (v4) */ x265_get_compressed_data2, |
1340 | | /* does_indicate_keyframes (v4) */ 0 |
1341 | | }; |
1342 | | |
1343 | | const heif_encoder_plugin* get_encoder_plugin_x265() |
1344 | 252 | { |
1345 | 252 | return &encoder_plugin_x265; |
1346 | 252 | } |
1347 | | |
1348 | | |
1349 | | #if PLUGIN_X265 |
1350 | | heif_plugin_info plugin_info { |
1351 | | 1, |
1352 | | heif_plugin_type_encoder, |
1353 | | &encoder_plugin_x265 |
1354 | | }; |
1355 | | #endif |