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