/src/libheif/libheif/codecs/avif_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 "avif_enc.h" |
22 | | #include "avif_boxes.h" |
23 | | #include "error.h" |
24 | | #include "context.h" |
25 | | #include "api_structs.h" |
26 | | |
27 | | #include <string> |
28 | | |
29 | | enum heif_av1_obu_type : uint8_t |
30 | | { |
31 | | heif_av1_obu_type_frame = 2 |
32 | | }; |
33 | | |
34 | | |
35 | | Result<Encoder::CodedImageData> Encoder_AVIF::encode(const std::shared_ptr<HeifPixelImage>& image, |
36 | | heif_encoder* encoder, |
37 | | const heif_encoding_options& options, |
38 | | heif_image_input_class input_class) |
39 | 0 | { |
40 | 0 | CodedImageData codedImage; |
41 | |
|
42 | 0 | Box_av1C::configuration config; |
43 | | |
44 | | // Fill preliminary av1C in case we cannot parse the sequence_header() correctly in the code below. |
45 | | // TODO: maybe we can remove this later. |
46 | 0 | fill_av1C_configuration(&config, image); |
47 | |
|
48 | 0 | heif_image c_api_image; |
49 | 0 | c_api_image.image = image; |
50 | |
|
51 | 0 | heif_error err = encoder->plugin->encode_image(encoder->encoder, &c_api_image, input_class); |
52 | 0 | if (err.code) { |
53 | 0 | return Error(err.code, |
54 | 0 | err.subcode, |
55 | 0 | err.message); |
56 | 0 | } |
57 | | |
58 | 0 | for (;;) { |
59 | 0 | uint8_t* data; |
60 | 0 | int size; |
61 | |
|
62 | 0 | err = encoder->plugin->get_compressed_data(encoder->encoder, &data, &size, nullptr); |
63 | 0 | if (err.code) { |
64 | 0 | return Error(err.code, err.subcode, err.message); |
65 | 0 | } |
66 | | |
67 | 0 | bool found_config = fill_av1C_configuration_from_stream(&config, data, size); |
68 | 0 | (void) found_config; |
69 | |
|
70 | 0 | if (data == nullptr) { |
71 | 0 | break; |
72 | 0 | } |
73 | | |
74 | 0 | codedImage.append(data, size); |
75 | 0 | } |
76 | | |
77 | 0 | auto av1C = std::make_shared<Box_av1C>(); |
78 | 0 | av1C->set_configuration(config); |
79 | 0 | codedImage.properties.push_back(av1C); |
80 | |
|
81 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
82 | 0 | codedImage.codingConstraints.all_ref_pics_intra = true; // TODO: change when we use predicted frames |
83 | |
|
84 | 0 | return codedImage; |
85 | 0 | } |
86 | | |
87 | | |
88 | | Error Encoder_AVIF::encode_sequence_frame(const std::shared_ptr<HeifPixelImage>& image, |
89 | | heif_encoder* encoder, |
90 | | const heif_sequence_encoding_options& options, |
91 | | heif_image_input_class input_class, |
92 | | uint32_t framerate_num, uint32_t framerate_denom, |
93 | | uintptr_t frame_number) |
94 | 0 | { |
95 | | // Box_av1C::configuration config; |
96 | | |
97 | | // Fill preliminary av1C in case we cannot parse the sequence_header() correctly in the code below. |
98 | | // TODO: maybe we can remove this later. |
99 | 0 | fill_av1C_configuration(&m_config, image); |
100 | |
|
101 | 0 | heif_image c_api_image; |
102 | 0 | c_api_image.image = image; |
103 | |
|
104 | 0 | if (!m_encoder_active) { |
105 | 0 | heif_error err = encoder->plugin->start_sequence_encoding(encoder->encoder, |
106 | 0 | &c_api_image, |
107 | 0 | input_class, |
108 | 0 | framerate_num, framerate_denom, |
109 | 0 | &options); |
110 | 0 | if (err.code) { |
111 | 0 | return { |
112 | 0 | err.code, |
113 | 0 | err.subcode, |
114 | 0 | err.message |
115 | 0 | }; |
116 | 0 | } |
117 | | |
118 | | //m_hvcC = std::make_shared<Box_hvcC>(); |
119 | 0 | m_encoder_active = true; |
120 | 0 | } |
121 | | |
122 | | |
123 | 0 | heif_error err = encoder->plugin->encode_sequence_frame(encoder->encoder, &c_api_image, frame_number); |
124 | 0 | if (err.code) { |
125 | 0 | return Error(err.code, |
126 | 0 | err.subcode, |
127 | 0 | err.message); |
128 | 0 | } |
129 | | |
130 | 0 | m_all_refs_intra = (options.gop_structure == heif_sequence_gop_structure_intra_only); |
131 | |
|
132 | 0 | return get_data(encoder); |
133 | 0 | } |
134 | | |
135 | | |
136 | | Error Encoder_AVIF::get_data(heif_encoder* encoder) |
137 | 0 | { |
138 | 0 | CodedImageData codedImage; |
139 | |
|
140 | 0 | for (;;) { |
141 | 0 | uint8_t* data; |
142 | 0 | int size; |
143 | |
|
144 | 0 | uintptr_t out_frame_number; |
145 | 0 | int is_keyframe = 0; |
146 | 0 | struct heif_error err = encoder->plugin->get_compressed_data2(encoder->encoder, &data, &size, &out_frame_number, &is_keyframe, nullptr); |
147 | 0 | if (err.code) { |
148 | 0 | return Error(err.code, err.subcode, err.message); |
149 | 0 | } |
150 | | |
151 | 0 | if (data == nullptr) { |
152 | 0 | break; |
153 | 0 | } |
154 | | |
155 | 0 | uint8_t obu_type = (data[0]>>3) & 0x0F; |
156 | | |
157 | | // printf("got OBU type=%d size=%d framenr=%d\n", obu_type, size, out_frame_number); |
158 | |
|
159 | 0 | bool found_config = fill_av1C_configuration_from_stream(&m_config, data, size); |
160 | 0 | (void) found_config; |
161 | |
|
162 | 0 | codedImage.append(data, size); |
163 | 0 | codedImage.frame_nr = out_frame_number; |
164 | |
|
165 | 0 | if (encoder->plugin->plugin_api_version >= 4 && |
166 | 0 | encoder->plugin->does_indicate_keyframes) { |
167 | 0 | codedImage.is_sync_frame = is_keyframe; |
168 | 0 | } |
169 | | |
170 | | // If we got an image, stop collecting data packets. |
171 | 0 | if (obu_type == heif_av1_obu_type_frame) { |
172 | 0 | break; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | 0 | if (m_end_of_sequence_reached && !m_av1C_sent) { |
177 | 0 | auto av1C = std::make_shared<Box_av1C>(); |
178 | 0 | av1C->set_configuration(m_config); |
179 | 0 | codedImage.properties.push_back(av1C); |
180 | 0 | m_av1C_sent = true; |
181 | 0 | } |
182 | |
|
183 | 0 | codedImage.codingConstraints.intra_pred_used = true; |
184 | 0 | codedImage.codingConstraints.all_ref_pics_intra = m_all_refs_intra; |
185 | |
|
186 | 0 | m_current_output_data = std::move(codedImage); |
187 | |
|
188 | 0 | return {}; |
189 | 0 | } |
190 | | |
191 | | |
192 | | Error Encoder_AVIF::encode_sequence_flush(heif_encoder* encoder) |
193 | 0 | { |
194 | 0 | encoder->plugin->end_sequence_encoding(encoder->encoder); |
195 | 0 | m_encoder_active = false; |
196 | 0 | m_end_of_sequence_reached = true; |
197 | |
|
198 | 0 | return get_data(encoder); |
199 | 0 | } |
200 | | |
201 | | |
202 | | std::optional<Encoder::CodedImageData> Encoder_AVIF::encode_sequence_get_data() |
203 | 0 | { |
204 | 0 | return std::move(m_current_output_data); |
205 | 0 | } |
206 | | |
207 | | |
208 | | std::shared_ptr<Box_VisualSampleEntry> Encoder_AVIF::get_sample_description_box(const CodedImageData& data) const |
209 | 0 | { |
210 | 0 | auto av01 = std::make_shared<Box_av01>(); |
211 | 0 | av01->get_VisualSampleEntry().compressorname = "AVIF"; |
212 | |
|
213 | 0 | for (auto prop : data.properties) { |
214 | 0 | if (prop->get_short_type() == fourcc("av1C")) { |
215 | 0 | av01->append_child_box(prop); |
216 | 0 | return av01; |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | | // box not available yet |
221 | 0 | return nullptr; |
222 | 0 | } |