/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 | uint32_t encoded_width = 0; |
54 | 0 | uint32_t 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 | uint64_t avg_fr = framerate_denom ? (256ULL * framerate_num + framerate_denom / 2) / framerate_denom : 0; |
147 | 0 | m_avg_frame_rate = avg_fr <= UINT16_MAX ? static_cast<uint16_t>(avg_fr) : 0; |
148 | 0 | } |
149 | | |
150 | 0 | Error dataErr = get_data(encoder); |
151 | 0 | if (dataErr) { |
152 | 0 | return dataErr; |
153 | 0 | } |
154 | | |
155 | 0 | heif_error err = encoder->plugin->encode_sequence_frame(encoder->encoder, &c_api_image, frame_number); |
156 | 0 | if (err.code) { |
157 | 0 | return { |
158 | 0 | err.code, |
159 | 0 | err.subcode, |
160 | 0 | err.message |
161 | 0 | }; |
162 | 0 | } |
163 | | |
164 | 0 | return get_data(encoder); |
165 | 0 | } |
166 | | |
167 | | |
168 | | Error Encoder_VVC::encode_sequence_flush(heif_encoder* encoder) |
169 | 0 | { |
170 | 0 | encoder->plugin->end_sequence_encoding(encoder->encoder); |
171 | 0 | m_encoder_active = false; |
172 | 0 | m_end_of_sequence_reached = true; |
173 | |
|
174 | 0 | return get_data(encoder); |
175 | 0 | } |
176 | | |
177 | | |
178 | | std::optional<Encoder::CodedImageData> Encoder_VVC::encode_sequence_get_data() |
179 | 0 | { |
180 | 0 | return std::move(m_current_output_data); |
181 | 0 | } |
182 | | |
183 | | Error Encoder_VVC::get_data(heif_encoder* encoder) |
184 | 0 | { |
185 | | //CodedImageData codedImage; |
186 | |
|
187 | 0 | bool got_some_data = false; |
188 | |
|
189 | 0 | for (;;) { |
190 | 0 | uint8_t* data; |
191 | 0 | int size; |
192 | |
|
193 | 0 | uintptr_t frameNr=0; |
194 | 0 | int more_frame_packets = 1; |
195 | 0 | struct heif_error err = encoder->plugin->get_compressed_data2(encoder->encoder, &data, &size, &frameNr, nullptr, &more_frame_packets); |
196 | 0 | if (err.code) { |
197 | 0 | return Error(err.code, err.subcode, err.message); |
198 | 0 | } |
199 | | |
200 | 0 | if (data == nullptr) { |
201 | 0 | break; |
202 | 0 | } |
203 | | |
204 | 0 | got_some_data = true; |
205 | |
|
206 | 0 | const uint8_t nal_type = (data[1] >> 3); |
207 | 0 | const bool is_sync = (nal_type == 7 || nal_type == 8 || nal_type == 9); |
208 | 0 | const bool is_image_data = (nal_type >= 0 && nal_type <= VVC_NAL_UNIT_MAX_VCL); |
209 | | |
210 | | // std::cout << "received frameNr=" << frameNr << " nal_type:" << ((int)nal_type) << " size: " << size << "\n"; |
211 | |
|
212 | 0 | if (nal_type == VVC_NAL_UNIT_SPS_NUT && m_vvcC) { |
213 | 0 | parse_sps_for_vvcC_configuration(data, size, |
214 | 0 | &m_vvcC->get_configuration(), |
215 | 0 | &m_encoded_image_width, &m_encoded_image_height); |
216 | 0 | m_vvcC->get_configuration().avg_frame_rate = m_avg_frame_rate; |
217 | 0 | } |
218 | |
|
219 | 0 | switch (nal_type) { |
220 | 0 | case VVC_NAL_UNIT_VPS_NUT: |
221 | 0 | if (m_vvcC && !m_vvcC_has_VPS) m_vvcC->append_nal_data(data, size); |
222 | 0 | m_vvcC_has_VPS = true; |
223 | 0 | break; |
224 | | |
225 | 0 | case VVC_NAL_UNIT_SPS_NUT: |
226 | 0 | if (m_vvcC && !m_vvcC_has_SPS) m_vvcC->append_nal_data(data, size); |
227 | 0 | m_vvcC_has_SPS = true; |
228 | 0 | break; |
229 | | |
230 | 0 | case VVC_NAL_UNIT_PPS_NUT: |
231 | 0 | if (m_vvcC && !m_vvcC_has_PPS) m_vvcC->append_nal_data(data, size); |
232 | 0 | m_vvcC_has_PPS = true; |
233 | 0 | break; |
234 | | |
235 | 0 | default: |
236 | 0 | if (!m_current_output_data) { |
237 | 0 | m_current_output_data = CodedImageData{}; |
238 | 0 | } |
239 | 0 | m_current_output_data->append_with_4bytes_size(data, size); |
240 | |
|
241 | 0 | if (is_image_data) { |
242 | 0 | m_current_output_data->is_sync_frame = is_sync; |
243 | 0 | m_current_output_data->frame_nr = frameNr; |
244 | 0 | } |
245 | 0 | } |
246 | | |
247 | 0 | if (!more_frame_packets) { |
248 | 0 | break; |
249 | 0 | } |
250 | 0 | } |
251 | | |
252 | 0 | if (!got_some_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 | } |