/src/libheif/libheif/codecs/hevc_enc.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2024 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 "hevc_enc.h" |
22 | | #include "hevc_boxes.h" |
23 | | #include "error.h" |
24 | | #include "context.h" |
25 | | #include "libheif/api_structs.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | |
30 | | Result<Encoder::CodedImageData> Encoder_HEVC::encode(const std::shared_ptr<HeifPixelImage>& image, |
31 | | struct heif_encoder* encoder, |
32 | | const struct heif_encoding_options& options, |
33 | | enum heif_image_input_class input_class) |
34 | 0 | { |
35 | 0 | CodedImageData codedImage; |
36 | |
|
37 | 0 | auto hvcC = std::make_shared<Box_hvcC>(); |
38 | |
|
39 | 0 | heif_image c_api_image; |
40 | 0 | c_api_image.image = image; |
41 | |
|
42 | 0 | struct heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
43 | 0 | if (err.code) { |
44 | 0 | return Error(err.code, |
45 | 0 | err.subcode, |
46 | 0 | err.message); |
47 | 0 | } |
48 | | |
49 | 0 | int encoded_width = 0; |
50 | 0 | int encoded_height = 0; |
51 | |
|
52 | 0 | for (;;) { |
53 | 0 | uint8_t* data; |
54 | 0 | int size; |
55 | |
|
56 | 0 | encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr); |
57 | |
|
58 | 0 | if (data == nullptr) { |
59 | 0 | break; |
60 | 0 | } |
61 | | |
62 | | |
63 | 0 | const uint8_t NAL_SPS = 33; |
64 | |
|
65 | 0 | if ((data[0] >> 1) == NAL_SPS) { |
66 | 0 | parse_sps_for_hvcC_configuration(data, size, &hvcC->get_configuration(), &encoded_width, &encoded_height); |
67 | |
|
68 | 0 | codedImage.encoded_image_width = encoded_width; |
69 | 0 | codedImage.encoded_image_height = encoded_height; |
70 | 0 | } |
71 | |
|
72 | 0 | switch (data[0] >> 1) { |
73 | 0 | case 0x20: |
74 | 0 | case 0x21: |
75 | 0 | case 0x22: |
76 | 0 | hvcC->append_nal_data(data, size); |
77 | 0 | break; |
78 | | |
79 | 0 | default: |
80 | 0 | codedImage.append_with_4bytes_size(data, size); |
81 | | // m_heif_file->append_iloc_data_with_4byte_size(image_id, data, size); |
82 | 0 | } |
83 | 0 | } |
84 | | |
85 | 0 | if (!encoded_width || !encoded_height) { |
86 | 0 | return Error(heif_error_Encoder_plugin_error, |
87 | 0 | heif_suberror_Invalid_image_size); |
88 | 0 | } |
89 | | |
90 | 0 | codedImage.properties.push_back(hvcC); |
91 | | |
92 | | |
93 | | // Make sure that the encoder plugin works correctly and the encoded image has the correct size. |
94 | |
|
95 | 0 | if (encoder->plugin->plugin_api_version >= 3 && |
96 | 0 | encoder->plugin->query_encoded_size != nullptr) { |
97 | 0 | uint32_t check_encoded_width = image->get_width(), check_encoded_height = image->get_height(); |
98 | |
|
99 | 0 | encoder->plugin->query_encoded_size(encoder->encoder, |
100 | 0 | image->get_width(), image->get_height(), |
101 | 0 | &check_encoded_width, |
102 | 0 | &check_encoded_height); |
103 | |
|
104 | 0 | assert((int)check_encoded_width == encoded_width); |
105 | 0 | assert((int)check_encoded_height == encoded_height); |
106 | 0 | } |
107 | | |
108 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
109 | 0 | codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
110 | |
|
111 | 0 | return codedImage; |
112 | 0 | } |
113 | | |
114 | | |
115 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_HEVC::get_sample_description_box(const CodedImageData& data) const |
116 | 0 | { |
117 | 0 | auto hvc1 = std::make_shared<Box_hvc1>(); |
118 | 0 | hvc1->get_VisualSampleEntry().compressorname = "HEVC"; |
119 | |
|
120 | 0 | for (auto prop : data.properties) { |
121 | 0 | if (prop->get_short_type() == fourcc("hvcC")) { |
122 | 0 | hvc1->append_child_box(prop); |
123 | 0 | return hvc1; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | 0 | assert(false); // no hvcC generated |
128 | 0 | return nullptr; |
129 | 0 | } |