/src/libheif/libheif/codecs/vvc_enc.cc
Line | Count | Source |
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 "vvc_enc.h" |
22 | | #include "vvc_boxes.h" |
23 | | #include "error.h" |
24 | | #include "context.h" |
25 | | #include "api_structs.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | #include "plugins/nalu_utils.h" |
30 | | |
31 | | |
32 | | Result<Encoder::CodedImageData> Encoder_VVC::encode(const std::shared_ptr<HeifPixelImage>& image, |
33 | | struct heif_encoder* encoder, |
34 | | const struct heif_encoding_options& options, |
35 | | enum heif_image_input_class input_class) |
36 | 0 | { |
37 | 0 | Encoder::CodedImageData codedImage; |
38 | |
|
39 | 0 | auto vvcC = std::make_shared<Box_vvcC>(); |
40 | 0 | codedImage.properties.push_back(vvcC); |
41 | | |
42 | |
|
43 | 0 | heif_image c_api_image; |
44 | 0 | c_api_image.image = image; |
45 | |
|
46 | 0 | struct heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
47 | 0 | if (err.code) { |
48 | 0 | return Error(err.code, |
49 | 0 | err.subcode, |
50 | 0 | err.message); |
51 | 0 | } |
52 | | |
53 | 0 | int encoded_width = 0; |
54 | 0 | int encoded_height = 0; |
55 | |
|
56 | 0 | for (;;) { |
57 | 0 | uint8_t* data; |
58 | 0 | int size; |
59 | |
|
60 | 0 | err = encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, NULL); |
61 | 0 | if (err.code) { |
62 | 0 | return Error(err.code, err.subcode, err.message); |
63 | 0 | } |
64 | | |
65 | 0 | if (data == NULL) { |
66 | 0 | break; |
67 | 0 | } |
68 | | |
69 | | |
70 | 0 | const uint8_t NAL_SPS = 15; |
71 | |
|
72 | 0 | uint8_t nal_type = 0; |
73 | 0 | if (size>=2) { |
74 | 0 | nal_type = (data[1] >> 3) & 0x1F; |
75 | 0 | } |
76 | |
|
77 | 0 | if (nal_type == NAL_SPS) { |
78 | 0 | Box_vvcC::configuration config; |
79 | |
|
80 | 0 | parse_sps_for_vvcC_configuration(data, size, &config, &encoded_width, &encoded_height); |
81 | |
|
82 | 0 | vvcC->set_configuration(config); |
83 | 0 | } |
84 | |
|
85 | 0 | switch (nal_type) { |
86 | 0 | case 14: // VPS |
87 | 0 | case 15: // SPS |
88 | 0 | case 16: // PPS |
89 | 0 | vvcC->append_nal_data(data, size); |
90 | 0 | break; |
91 | | |
92 | 0 | default: |
93 | 0 | codedImage.append_with_4bytes_size(data, size); |
94 | 0 | } |
95 | 0 | } |
96 | | |
97 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
98 | 0 | codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
99 | 0 | codedImage.codingConstraints.max_ref_per_pic = 0; |
100 | |
|
101 | 0 | return codedImage; |
102 | 0 | } |
103 | | |
104 | | |
105 | | std::shared_ptr<class Box_VisualSampleEntry> Encoder_VVC::get_sample_description_box(const CodedImageData& data) const |
106 | 0 | { |
107 | 0 | auto vvc1 = std::make_shared<Box_vvc1>(); |
108 | 0 | vvc1->get_VisualSampleEntry().compressorname = "VVC"; |
109 | |
|
110 | 0 | for (auto prop : data.properties) { |
111 | 0 | if (prop->get_short_type() == fourcc("vvcC")) { |
112 | 0 | vvc1->append_child_box(prop); |
113 | 0 | return vvc1; |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | | // box not yet available |
118 | 0 | return nullptr; |
119 | 0 | } |
120 | | |
121 | | |
122 | | Error Encoder_VVC::encode_sequence_frame(const std::shared_ptr<HeifPixelImage>& image, |
123 | | heif_encoder* encoder, |
124 | | const heif_sequence_encoding_options& options, |
125 | | heif_image_input_class input_class, |
126 | | uint32_t framerate_num, uint32_t framerate_denom, |
127 | | uintptr_t frame_number) |
128 | 0 | { |
129 | 0 | heif_image c_api_image; |
130 | 0 | c_api_image.image = image; |
131 | |
|
132 | 0 | if (!m_encoder_active) { |
133 | 0 | heif_error err = encoder->plugin->start_sequence_encoding(encoder->encoder, &c_api_image, input_class, |
134 | 0 | framerate_num, framerate_denom, |
135 | 0 | &options); |
136 | 0 | if (err.code) { |
137 | 0 | return { |
138 | 0 | err.code, |
139 | 0 | err.subcode, |
140 | 0 | err.message |
141 | 0 | }; |
142 | 0 | } |
143 | | |
144 | 0 | m_vvcC = std::make_shared<Box_vvcC>(); |
145 | 0 | m_encoder_active = true; |
146 | 0 | } |
147 | | |
148 | 0 | Error dataErr = get_data(encoder); |
149 | 0 | if (dataErr) { |
150 | 0 | return dataErr; |
151 | 0 | } |
152 | | |
153 | 0 | heif_error err = encoder->plugin->encode_sequence_frame(encoder->encoder, &c_api_image, frame_number); |
154 | 0 | if (err.code) { |
155 | 0 | return { |
156 | 0 | err.code, |
157 | 0 | err.subcode, |
158 | 0 | err.message |
159 | 0 | }; |
160 | 0 | } |
161 | | |
162 | 0 | return get_data(encoder); |
163 | 0 | } |
164 | | |
165 | | |
166 | | Error Encoder_VVC::encode_sequence_flush(heif_encoder* encoder) |
167 | 0 | { |
168 | 0 | encoder->plugin->end_sequence_encoding(encoder->encoder); |
169 | 0 | m_encoder_active = false; |
170 | 0 | m_end_of_sequence_reached = true; |
171 | |
|
172 | 0 | return get_data(encoder); |
173 | 0 | } |
174 | | |
175 | | |
176 | | std::optional<Encoder::CodedImageData> Encoder_VVC::encode_sequence_get_data() |
177 | 0 | { |
178 | 0 | return std::move(m_current_output_data); |
179 | 0 | } |
180 | | |
181 | | Error Encoder_VVC::get_data(heif_encoder* encoder) |
182 | 0 | { |
183 | | //CodedImageData codedImage; |
184 | |
|
185 | 0 | bool got_some_data = false; |
186 | |
|
187 | 0 | for (;;) { |
188 | 0 | uint8_t* data; |
189 | 0 | int size; |
190 | |
|
191 | 0 | uintptr_t frameNr=0; |
192 | 0 | int more_frame_packets = 1; |
193 | 0 | struct heif_error err = encoder->plugin->get_compressed_data2(encoder->encoder, &data, &size, &frameNr, nullptr, &more_frame_packets); |
194 | 0 | if (err.code) { |
195 | 0 | return Error(err.code, err.subcode, err.message); |
196 | 0 | } |
197 | | |
198 | 0 | if (data == nullptr) { |
199 | 0 | break; |
200 | 0 | } |
201 | | |
202 | 0 | got_some_data = true; |
203 | |
|
204 | 0 | const uint8_t nal_type = (data[1] >> 3); |
205 | 0 | const bool is_sync = (nal_type == 7 || nal_type == 8 || nal_type == 9); |
206 | 0 | const bool is_image_data = (nal_type >= 0 && nal_type <= VVC_NAL_UNIT_MAX_VCL); |
207 | | |
208 | | // std::cout << "received frameNr=" << frameNr << " nal_type:" << ((int)nal_type) << " size: " << size << "\n"; |
209 | |
|
210 | 0 | if (nal_type == VVC_NAL_UNIT_SPS_NUT && m_vvcC) { |
211 | 0 | parse_sps_for_vvcC_configuration(data, size, |
212 | 0 | &m_vvcC->get_configuration(), |
213 | 0 | &m_encoded_image_width, &m_encoded_image_height); |
214 | 0 | } |
215 | |
|
216 | 0 | switch (nal_type) { |
217 | 0 | case VVC_NAL_UNIT_VPS_NUT: |
218 | 0 | if (m_vvcC && !m_vvcC_has_VPS) m_vvcC->append_nal_data(data, size); |
219 | 0 | m_vvcC_has_VPS = true; |
220 | 0 | break; |
221 | | |
222 | 0 | case VVC_NAL_UNIT_SPS_NUT: |
223 | 0 | if (m_vvcC && !m_vvcC_has_SPS) m_vvcC->append_nal_data(data, size); |
224 | 0 | m_vvcC_has_SPS = true; |
225 | 0 | break; |
226 | | |
227 | 0 | case VVC_NAL_UNIT_PPS_NUT: |
228 | 0 | if (m_vvcC && !m_vvcC_has_PPS) m_vvcC->append_nal_data(data, size); |
229 | 0 | m_vvcC_has_PPS = true; |
230 | 0 | break; |
231 | | |
232 | 0 | default: |
233 | 0 | if (!m_current_output_data) { |
234 | 0 | m_current_output_data = CodedImageData{}; |
235 | 0 | } |
236 | 0 | m_current_output_data->append_with_4bytes_size(data, size); |
237 | |
|
238 | 0 | if (is_image_data) { |
239 | 0 | m_current_output_data->is_sync_frame = is_sync; |
240 | 0 | m_current_output_data->frame_nr = frameNr; |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | 0 | if (!more_frame_packets) { |
245 | 0 | break; |
246 | 0 | } |
247 | 0 | } |
248 | | |
249 | 0 | if (!got_some_data) { |
250 | 0 | return {}; |
251 | 0 | } |
252 | | |
253 | 0 | if (!m_encoded_image_width || !m_encoded_image_height) { |
254 | 0 | return Error(heif_error_Encoder_plugin_error, |
255 | 0 | heif_suberror_Invalid_image_size); |
256 | 0 | } |
257 | | |
258 | | |
259 | | // --- return hvcC when all headers are included and it was not returned yet |
260 | | // TODO: it's maybe better to return this at the end so that we are sure to have all headers |
261 | | // and also complete codingConstraints. |
262 | | |
263 | | //if (hvcC_has_VPS && m_hvcC_has_SPS && m_hvcC_has_PPS && !m_hvcC_returned) { |
264 | 0 | if (m_end_of_sequence_reached && m_vvcC && !m_vvcC_sent) { |
265 | 0 | m_current_output_data->properties.push_back(m_vvcC); |
266 | 0 | m_vvcC = nullptr; |
267 | 0 | m_vvcC_sent = true; |
268 | 0 | } |
269 | |
|
270 | 0 | m_current_output_data->encoded_image_width = m_encoded_image_width; |
271 | 0 | m_current_output_data->encoded_image_height = m_encoded_image_height; |
272 | | |
273 | | |
274 | | // Make sure that the encoder plugin works correctly and the encoded image has the correct size. |
275 | | #if 0 |
276 | | if (encoder->plugin->plugin_api_version >= 3 && |
277 | | encoder->plugin->query_encoded_size != nullptr) { |
278 | | uint32_t check_encoded_width = image->get_width(), check_encoded_height = image->get_height(); |
279 | | |
280 | | encoder->plugin->query_encoded_size(encoder->encoder, |
281 | | image->get_width(), image->get_height(), |
282 | | &check_encoded_width, |
283 | | &check_encoded_height); |
284 | | |
285 | | assert((int)check_encoded_width == encoded_width); |
286 | | assert((int)check_encoded_height == encoded_height); |
287 | | } |
288 | | #endif |
289 | |
|
290 | 0 | m_current_output_data->codingConstraints.intra_pred_used = true; |
291 | 0 | m_current_output_data->codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
292 | |
|
293 | 0 | return {}; |
294 | 0 | } |