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