/src/libheif/libheif/plugins/encoder_uncompressed.cc
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 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 "encoder_uncompressed.h" |
22 | | #include "libheif/heif.h" |
23 | | #include "libheif/heif_plugin.h" |
24 | | #include <algorithm> |
25 | | #include <cstring> |
26 | | #include <cassert> |
27 | | #include <vector> |
28 | | #include <string> |
29 | | #include <memory> |
30 | | |
31 | | #include <mutex> |
32 | | |
33 | | |
34 | | static const char* kParam_interleave = "interleave"; |
35 | | |
36 | | static const int PLUGIN_PRIORITY = 60; |
37 | | |
38 | | static const char* plugin_name = "builtin"; |
39 | | |
40 | | |
41 | | static void uncompressed_set_default_parameters(void* encoder); |
42 | | |
43 | | |
44 | | static const char* uncompressed_plugin_name() |
45 | 0 | { |
46 | 0 | return plugin_name; |
47 | 0 | } |
48 | | |
49 | | |
50 | | #define MAX_NPARAMETERS 14 |
51 | | |
52 | | static heif_encoder_parameter uncompressed_encoder_params[MAX_NPARAMETERS]; |
53 | | static const heif_encoder_parameter* uncompressed_encoder_parameter_ptrs[MAX_NPARAMETERS + 1]; |
54 | | |
55 | | static void uncompressed_init_parameters() |
56 | 2 | { |
57 | 2 | heif_encoder_parameter* p = uncompressed_encoder_params; |
58 | 2 | const heif_encoder_parameter** d = uncompressed_encoder_parameter_ptrs; |
59 | 2 | int i = 0; |
60 | | |
61 | 2 | assert(i < MAX_NPARAMETERS); |
62 | 2 | p->version = 2; |
63 | 2 | p->name = kParam_interleave; |
64 | 2 | p->type = heif_encoder_parameter_type_string; |
65 | 2 | p->string.default_value = "planar"; |
66 | 2 | p->has_default = true; |
67 | 2 | d[i++] = p++; |
68 | | |
69 | 2 | assert(i < MAX_NPARAMETERS + 1); |
70 | 2 | d[i++] = nullptr; |
71 | 2 | } |
72 | | |
73 | | |
74 | | const heif_encoder_parameter** uncompressed_list_parameters(void* encoder) |
75 | 0 | { |
76 | 0 | return uncompressed_encoder_parameter_ptrs; |
77 | 0 | } |
78 | | |
79 | | static void uncompressed_init_plugin() |
80 | 2 | { |
81 | 2 | uncompressed_init_parameters(); |
82 | 2 | } |
83 | | |
84 | | |
85 | | static void uncompressed_cleanup_plugin() |
86 | 0 | { |
87 | 0 | } |
88 | | |
89 | | heif_error uncompressed_new_encoder(void** enc) |
90 | 0 | { |
91 | 0 | encoder_struct_uncompressed* encoder = new encoder_struct_uncompressed(); |
92 | 0 | heif_error err = heif_error_ok; |
93 | |
|
94 | 0 | *enc = encoder; |
95 | | |
96 | | // set default parameters |
97 | |
|
98 | 0 | uncompressed_set_default_parameters(encoder); |
99 | |
|
100 | 0 | return err; |
101 | 0 | } |
102 | | |
103 | | void uncompressed_free_encoder(void* encoder_raw) |
104 | 0 | { |
105 | 0 | encoder_struct_uncompressed* encoder = (encoder_struct_uncompressed*) encoder_raw; |
106 | |
|
107 | 0 | delete encoder; |
108 | 0 | } |
109 | | |
110 | | |
111 | | heif_error uncompressed_set_parameter_quality(void* encoder_raw, int quality) |
112 | 0 | { |
113 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
114 | |
|
115 | 0 | return heif_error_ok; |
116 | 0 | } |
117 | | |
118 | | heif_error uncompressed_get_parameter_quality(void* encoder_raw, int* quality) |
119 | 0 | { |
120 | 0 | *quality = 100; |
121 | |
|
122 | 0 | return heif_error_ok; |
123 | 0 | } |
124 | | |
125 | | heif_error uncompressed_set_parameter_lossless(void* encoder_raw, int enable) |
126 | 0 | { |
127 | 0 | return heif_error_ok; |
128 | 0 | } |
129 | | |
130 | | heif_error uncompressed_get_parameter_lossless(void* encoder_raw, int* enable) |
131 | 0 | { |
132 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
133 | |
|
134 | 0 | *enable = true; |
135 | |
|
136 | 0 | return heif_error_ok; |
137 | 0 | } |
138 | | |
139 | | heif_error uncompressed_set_parameter_logging_level(void* encoder_raw, int logging) |
140 | 0 | { |
141 | | #if 0 |
142 | | struct encoder_struct_x265* encoder = (struct encoder_struct_x265*)encoder_raw; |
143 | | |
144 | | if (logging<0 || logging>4) { |
145 | | return heif_error_invalid_parameter_value; |
146 | | } |
147 | | |
148 | | encoder->logLevel = logging; |
149 | | #endif |
150 | |
|
151 | 0 | return heif_error_ok; |
152 | 0 | } |
153 | | |
154 | | heif_error uncompressed_get_parameter_logging_level(void* encoder_raw, int* loglevel) |
155 | 0 | { |
156 | | #if 0 |
157 | | struct encoder_struct_x265* encoder = (struct encoder_struct_x265*)encoder_raw; |
158 | | |
159 | | *loglevel = encoder->logLevel; |
160 | | #else |
161 | 0 | *loglevel = 0; |
162 | 0 | #endif |
163 | |
|
164 | 0 | return heif_error_ok; |
165 | 0 | } |
166 | | |
167 | | #define set_value(paramname, paramvar) if (strcmp(name, paramname)==0) { encoder->paramvar = value; return heif_error_ok; } |
168 | | #define get_value(paramname, paramvar) if (strcmp(name, paramname)==0) { *value = encoder->paramvar; return heif_error_ok; } |
169 | | |
170 | | |
171 | | heif_error uncompressed_set_parameter_integer(void* encoder_raw, const char* name, int value) |
172 | 0 | { |
173 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
174 | |
|
175 | 0 | return heif_error_unsupported_parameter; |
176 | 0 | } |
177 | | |
178 | | heif_error uncompressed_get_parameter_integer(void* encoder_raw, const char* name, int* value) |
179 | 0 | { |
180 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
181 | |
|
182 | 0 | return heif_error_unsupported_parameter; |
183 | 0 | } |
184 | | |
185 | | |
186 | | heif_error uncompressed_set_parameter_boolean(void* encoder_raw, const char* name, int value) |
187 | 0 | { |
188 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
189 | |
|
190 | 0 | return heif_error_unsupported_parameter; |
191 | 0 | } |
192 | | |
193 | | heif_error uncompressed_get_parameter_boolean(void* encoder_raw, const char* name, int* value) |
194 | 0 | { |
195 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
196 | |
|
197 | 0 | return heif_error_unsupported_parameter; |
198 | 0 | } |
199 | | |
200 | | |
201 | | heif_error uncompressed_set_parameter_string(void* encoder_raw, const char* name, const char* value) |
202 | 0 | { |
203 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
204 | |
|
205 | 0 | return heif_error_unsupported_parameter; |
206 | 0 | } |
207 | | |
208 | | |
209 | | heif_error uncompressed_get_parameter_string(void* encoder_raw, const char* name, |
210 | | char* value, int value_size) |
211 | 0 | { |
212 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
213 | |
|
214 | 0 | return heif_error_unsupported_parameter; |
215 | 0 | } |
216 | | |
217 | | |
218 | | static void uncompressed_set_default_parameters(void* encoder) |
219 | 0 | { |
220 | 0 | for (const heif_encoder_parameter** p = uncompressed_encoder_parameter_ptrs; *p; p++) { |
221 | 0 | const heif_encoder_parameter* param = *p; |
222 | |
|
223 | 0 | if (param->has_default) { |
224 | 0 | switch (param->type) { |
225 | 0 | case heif_encoder_parameter_type_integer: |
226 | 0 | uncompressed_set_parameter_integer(encoder, param->name, param->integer.default_value); |
227 | 0 | break; |
228 | 0 | case heif_encoder_parameter_type_boolean: |
229 | 0 | uncompressed_set_parameter_boolean(encoder, param->name, param->boolean.default_value); |
230 | 0 | break; |
231 | 0 | case heif_encoder_parameter_type_string: |
232 | 0 | uncompressed_set_parameter_string(encoder, param->name, param->string.default_value); |
233 | 0 | break; |
234 | 0 | } |
235 | 0 | } |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | |
240 | | void uncompressed_query_input_colorspace(heif_colorspace* colorspace, heif_chroma* chroma) |
241 | 0 | { |
242 | | //*colorspace = heif_colorspace_YCbCr; |
243 | | //*chroma = heif_chroma_420; |
244 | 0 | } |
245 | | |
246 | | |
247 | | void uncompressed_query_input_colorspace2(void* encoder_raw, heif_colorspace* colorspace, heif_chroma* chroma) |
248 | 0 | { |
249 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
250 | |
|
251 | 0 | if (*colorspace == heif_colorspace_monochrome) { |
252 | | // keep the monochrome colorspace |
253 | 0 | } |
254 | 0 | else { |
255 | | //*colorspace = heif_colorspace_YCbCr; |
256 | | //*chroma = encoder->chroma; |
257 | 0 | } |
258 | 0 | } |
259 | | |
260 | | |
261 | | heif_error uncompressed_encode_image(void* encoder_raw, const heif_image* image, |
262 | | heif_image_input_class input_class) |
263 | 0 | { |
264 | | //struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
265 | | |
266 | | // Note: this is not used. It is a dummy plugin. |
267 | |
|
268 | 0 | return heif_error_ok; |
269 | 0 | } |
270 | | |
271 | | |
272 | | heif_error uncompressed_get_compressed_data(void* encoder_raw, uint8_t** data, int* size, |
273 | | heif_encoded_data_type* type) |
274 | 0 | { |
275 | | #if 0 |
276 | | struct encoder_struct_uncompressed* encoder = (struct encoder_struct_uncompressed*) encoder_raw; |
277 | | |
278 | | if (encoder->data_read) { |
279 | | *size = 0; |
280 | | *data = nullptr; |
281 | | } |
282 | | else { |
283 | | *size = (int) encoder->compressedData.size(); |
284 | | *data = encoder->compressedData.data(); |
285 | | encoder->data_read = true; |
286 | | } |
287 | | #endif |
288 | |
|
289 | 0 | return heif_error_ok; |
290 | 0 | } |
291 | | |
292 | | |
293 | | static const heif_encoder_plugin encoder_plugin_uncompressed |
294 | | { |
295 | | /* plugin_api_version */ 3, |
296 | | /* compression_format */ heif_compression_uncompressed, |
297 | | /* id_name */ "uncompressed", |
298 | | /* priority */ PLUGIN_PRIORITY, |
299 | | /* supports_lossy_compression */ true, |
300 | | /* supports_lossless_compression */ true, |
301 | | /* get_plugin_name */ uncompressed_plugin_name, |
302 | | /* init_plugin */ uncompressed_init_plugin, |
303 | | /* cleanup_plugin */ uncompressed_cleanup_plugin, |
304 | | /* new_encoder */ uncompressed_new_encoder, |
305 | | /* free_encoder */ uncompressed_free_encoder, |
306 | | /* set_parameter_quality */ uncompressed_set_parameter_quality, |
307 | | /* get_parameter_quality */ uncompressed_get_parameter_quality, |
308 | | /* set_parameter_lossless */ uncompressed_set_parameter_lossless, |
309 | | /* get_parameter_lossless */ uncompressed_get_parameter_lossless, |
310 | | /* set_parameter_logging_level */ uncompressed_set_parameter_logging_level, |
311 | | /* get_parameter_logging_level */ uncompressed_get_parameter_logging_level, |
312 | | /* list_parameters */ uncompressed_list_parameters, |
313 | | /* set_parameter_integer */ uncompressed_set_parameter_integer, |
314 | | /* get_parameter_integer */ uncompressed_get_parameter_integer, |
315 | | /* set_parameter_boolean */ uncompressed_set_parameter_boolean, |
316 | | /* get_parameter_boolean */ uncompressed_get_parameter_boolean, |
317 | | /* set_parameter_string */ uncompressed_set_parameter_string, |
318 | | /* get_parameter_string */ uncompressed_get_parameter_string, |
319 | | /* query_input_colorspace */ uncompressed_query_input_colorspace, |
320 | | /* encode_image */ uncompressed_encode_image, |
321 | | /* get_compressed_data */ uncompressed_get_compressed_data, |
322 | | /* query_input_colorspace (v2) */ uncompressed_query_input_colorspace2, |
323 | | /* query_encoded_size (v3) */ nullptr |
324 | | }; |
325 | | |
326 | | const heif_encoder_plugin* get_encoder_plugin_uncompressed() |
327 | 2 | { |
328 | 2 | return &encoder_plugin_uncompressed; |
329 | 2 | } |
330 | | |
331 | | |
332 | | #if 0 // PLUGIN_uncompressed_ENCODER |
333 | | heif_plugin_info plugin_info { |
334 | | 1, |
335 | | heif_plugin_type_encoder, |
336 | | &encoder_plugin_uncompressed |
337 | | }; |
338 | | #endif |