/src/libheif/libheif/codecs/avc_enc.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2025 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 "avc_enc.h" |
22 | | #include "avc_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 | | // TODO: can we use the new sequences interface for this to avoid duplicate code. |
34 | | Result<Encoder::CodedImageData> Encoder_AVC::encode(const std::shared_ptr<HeifPixelImage>& image, |
35 | | heif_encoder* encoder, |
36 | | const heif_encoding_options& options, |
37 | | heif_image_input_class input_class) |
38 | 0 | { |
39 | 0 | CodedImageData codedImage; |
40 | |
|
41 | 0 | auto avcC = std::make_shared<Box_avcC>(); |
42 | |
|
43 | 0 | heif_image c_api_image; |
44 | 0 | c_api_image.image = image; |
45 | |
|
46 | 0 | 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, nullptr); |
61 | 0 | if (err.code) { |
62 | 0 | return Error(err.code, err.subcode, err.message); |
63 | 0 | } |
64 | | |
65 | 0 | if (data == nullptr) { |
66 | 0 | break; |
67 | 0 | } |
68 | | |
69 | 0 | uint8_t nal_type = data[0] & 0x1f; |
70 | |
|
71 | 0 | if (nal_type == AVC_NAL_UNIT_SPS_NUT) { |
72 | 0 | parse_sps_for_avcC_configuration(data, size, &avcC->get_configuration(), &encoded_width, &encoded_height); |
73 | |
|
74 | 0 | codedImage.encoded_image_width = encoded_width; |
75 | 0 | codedImage.encoded_image_height = encoded_height; |
76 | 0 | } |
77 | |
|
78 | 0 | switch (nal_type) { |
79 | 0 | case AVC_NAL_UNIT_SPS_NUT: |
80 | 0 | avcC->append_sps_nal(data, size); |
81 | 0 | break; |
82 | 0 | case AVC_NAL_UNIT_SPS_EXT_NUT: |
83 | 0 | avcC->append_sps_ext_nal(data, size); |
84 | 0 | break; |
85 | 0 | case AVC_NAL_UNIT_PPS_NUT: |
86 | 0 | avcC->append_pps_nal(data, size); |
87 | 0 | break; |
88 | | |
89 | 0 | default: |
90 | 0 | codedImage.append_with_4bytes_size(data, size); |
91 | 0 | } |
92 | 0 | } |
93 | | |
94 | 0 | if (!encoded_width || !encoded_height) { |
95 | 0 | return Error(heif_error_Encoder_plugin_error, |
96 | 0 | heif_suberror_Invalid_image_size); |
97 | 0 | } |
98 | | |
99 | 0 | codedImage.properties.push_back(avcC); |
100 | | |
101 | | |
102 | | // Make sure that the encoder plugin works correctly and the encoded image has the correct size. |
103 | |
|
104 | 0 | if (encoder->plugin->plugin_api_version >= 3 && |
105 | 0 | encoder->plugin->query_encoded_size != nullptr) { |
106 | 0 | uint32_t check_encoded_width = image->get_width(), check_encoded_height = image->get_height(); |
107 | |
|
108 | 0 | encoder->plugin->query_encoded_size(encoder->encoder, |
109 | 0 | image->get_width(), image->get_height(), |
110 | 0 | &check_encoded_width, |
111 | 0 | &check_encoded_height); |
112 | |
|
113 | 0 | assert(check_encoded_width == encoded_width); |
114 | 0 | assert(check_encoded_height == encoded_height); |
115 | 0 | } |
116 | | |
117 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
118 | 0 | codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
119 | |
|
120 | 0 | return codedImage; |
121 | 0 | } |
122 | | |
123 | | |
124 | | Error Encoder_AVC::encode_sequence_frame(const std::shared_ptr<HeifPixelImage>& image, |
125 | | heif_encoder* encoder, |
126 | | const heif_sequence_encoding_options& options, |
127 | | heif_image_input_class input_class, |
128 | | uint32_t framerate_num, uint32_t framerate_denom, |
129 | | uintptr_t frame_number) |
130 | 0 | { |
131 | 0 | heif_image c_api_image; |
132 | 0 | c_api_image.image = image; |
133 | |
|
134 | 0 | if (!m_encoder_active) { |
135 | 0 | heif_error err = encoder->plugin->start_sequence_encoding(encoder->encoder, &c_api_image, |
136 | 0 | input_class, |
137 | 0 | framerate_num, framerate_denom, |
138 | 0 | &options); |
139 | 0 | if (err.code) { |
140 | 0 | return { |
141 | 0 | err.code, |
142 | 0 | err.subcode, |
143 | 0 | err.message |
144 | 0 | }; |
145 | 0 | } |
146 | | |
147 | 0 | m_avcC = std::make_shared<Box_avcC>(); |
148 | 0 | m_encoder_active = true; |
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_AVC::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_AVC::encode_sequence_extract_data() |
180 | 0 | { |
181 | | // Non-VCL NALs can arrive ahead of their picture (x264 emits an SEI together with the |
182 | | // SPS/PPS headers). They are held back until the slice data has been collected. |
183 | 0 | if (!m_output_image_complete) { |
184 | 0 | return std::nullopt; |
185 | 0 | } |
186 | | |
187 | 0 | m_output_image_complete = false; |
188 | 0 | return std::exchange(m_current_output_data, std::nullopt); |
189 | 0 | } |
190 | | |
191 | | Error Encoder_AVC::get_data(heif_encoder* encoder) |
192 | 0 | { |
193 | | //CodedImageData codedImage; |
194 | |
|
195 | 0 | for (;;) { |
196 | 0 | uint8_t* data; |
197 | 0 | int size; |
198 | |
|
199 | 0 | uintptr_t frameNr=0; |
200 | 0 | int more_frame_packets = 1; |
201 | 0 | struct heif_error err = encoder->plugin->get_compressed_data2(encoder->encoder, &data, &size, &frameNr, nullptr, &more_frame_packets); |
202 | 0 | if (err.code) { |
203 | 0 | return Error(err.code, err.subcode, err.message); |
204 | 0 | } |
205 | | |
206 | 0 | if (data == nullptr) { |
207 | 0 | break; |
208 | 0 | } |
209 | | |
210 | 0 | const uint8_t nal_type = (data[0] & 0x1f); |
211 | 0 | const bool is_sync = (nal_type == 5); |
212 | 0 | const bool is_image_data = (nal_type > 0 && nal_type <= AVC_NAL_UNIT_MAX_VCL); |
213 | |
|
214 | 0 | m_output_image_complete |= is_image_data; |
215 | | |
216 | | // std::cout << "received frameNr=" << frameNr << " nal_type:" << ((int)nal_type) << " size: " << size << "\n"; |
217 | |
|
218 | 0 | if (nal_type == AVC_NAL_UNIT_SPS_NUT && m_avcC) { |
219 | 0 | parse_sps_for_avcC_configuration(data, size, |
220 | 0 | &m_avcC->get_configuration(), |
221 | 0 | &m_encoded_image_width, &m_encoded_image_height); |
222 | 0 | } |
223 | |
|
224 | 0 | if (is_image_data) { |
225 | | // more_frame_packets = 0; |
226 | 0 | } |
227 | |
|
228 | 0 | switch (nal_type) { |
229 | 0 | case AVC_NAL_UNIT_SPS_NUT: |
230 | 0 | if (m_avcC && !m_avcC_has_SPS) m_avcC->append_sps_nal(data, size); |
231 | 0 | m_avcC_has_SPS = true; |
232 | 0 | break; |
233 | | |
234 | 0 | case AVC_NAL_UNIT_SPS_EXT_NUT: |
235 | 0 | if (m_avcC /*&& !m_avcC_has_SPS*/) m_avcC->append_sps_ext_nal(data, size); |
236 | | //m_avcC_has_SPS = true; |
237 | 0 | break; |
238 | | |
239 | 0 | case AVC_NAL_UNIT_PPS_NUT: |
240 | 0 | if (m_avcC && !m_avcC_has_PPS) m_avcC->append_pps_nal(data, size); |
241 | 0 | m_avcC_has_PPS = true; |
242 | 0 | break; |
243 | | |
244 | 0 | default: |
245 | 0 | if (!m_current_output_data) { |
246 | 0 | m_current_output_data = CodedImageData{}; |
247 | 0 | } |
248 | 0 | m_current_output_data->append_with_4bytes_size(data, size); |
249 | |
|
250 | 0 | if (is_image_data) { |
251 | 0 | m_current_output_data->is_sync_frame = is_sync; |
252 | 0 | m_current_output_data->frame_nr = frameNr; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | if (!more_frame_packets) { |
257 | 0 | break; |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 0 | if (!m_output_image_complete) { |
262 | 0 | return {}; |
263 | 0 | } |
264 | | |
265 | 0 | if (!m_encoded_image_width || !m_encoded_image_height) { |
266 | 0 | return Error(heif_error_Encoder_plugin_error, |
267 | 0 | heif_suberror_Invalid_image_size); |
268 | 0 | } |
269 | | |
270 | | |
271 | | // --- return avcC when all headers are included and it was not returned yet |
272 | | // TODO: it's maybe better to return this at the end so that we are sure to have all headers |
273 | | // and also complete codingConstraints. |
274 | | |
275 | 0 | if (m_end_of_sequence_reached && m_avcC && !m_avcC_sent) { |
276 | 0 | m_current_output_data->properties.push_back(m_avcC); |
277 | 0 | m_avcC = nullptr; |
278 | 0 | m_avcC_sent = true; |
279 | 0 | } |
280 | |
|
281 | 0 | m_current_output_data->encoded_image_width = m_encoded_image_width; |
282 | 0 | m_current_output_data->encoded_image_height = m_encoded_image_height; |
283 | |
|
284 | 0 | m_current_output_data->codingConstraints.intra_pred_used = true; |
285 | 0 | m_current_output_data->codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
286 | |
|
287 | 0 | return {}; |
288 | 0 | } |
289 | | |
290 | | |
291 | | std::shared_ptr<Box_VisualSampleEntry> Encoder_AVC::get_sample_description_box(const CodedImageData& data) const |
292 | 0 | { |
293 | 0 | auto avc1 = std::make_shared<Box_avc1>(); |
294 | 0 | avc1->get_VisualSampleEntry().compressorname = "AVC"; |
295 | |
|
296 | 0 | for (const auto& prop : data.properties) { |
297 | 0 | if (prop->get_short_type() == fourcc("avcC")) { |
298 | 0 | avc1->append_child_box(prop); |
299 | 0 | return avc1; |
300 | 0 | } |
301 | 0 | } |
302 | | |
303 | | // box not yet available |
304 | 0 | return nullptr; |
305 | 0 | } |