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