Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * EXIF metadata parser | ||
3 | * Copyright (c) 2013 Thilo Borgmann <thilo.borgmann _at_ mail.de> | ||
4 | * Copyright (c) 2024-2025 Leo Izen <leo.izen@gmail.com> | ||
5 | * | ||
6 | * This file is part of FFmpeg. | ||
7 | * | ||
8 | * FFmpeg is free software; you can redistribute it and/or | ||
9 | * modify it under the terms of the GNU Lesser General Public | ||
10 | * License as published by the Free Software Foundation; either | ||
11 | * version 2.1 of the License, or (at your option) any later version. | ||
12 | * | ||
13 | * FFmpeg is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * Lesser General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU Lesser General Public | ||
19 | * License along with FFmpeg; if not, write to the Free Software | ||
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | |||
23 | /** | ||
24 | * @file | ||
25 | * EXIF metadata parser | ||
26 | * @author Thilo Borgmann <thilo.borgmann _at_ mail.de> | ||
27 | * @author Leo Izen <leo.izen@gmail.com> | ||
28 | */ | ||
29 | |||
30 | #include <inttypes.h> | ||
31 | |||
32 | #include "libavutil/avconfig.h" | ||
33 | #include "libavutil/bprint.h" | ||
34 | #include "libavutil/display.h" | ||
35 | #include "libavutil/intreadwrite.h" | ||
36 | #include "libavutil/mem.h" | ||
37 | |||
38 | #include "bytestream.h" | ||
39 | #include "exif_internal.h" | ||
40 | #include "tiff_common.h" | ||
41 | |||
42 | #define EXIF_II_LONG 0x49492a00 | ||
43 | #define EXIF_MM_LONG 0x4d4d002a | ||
44 | |||
45 | #define BASE_TAG_SIZE 12 | ||
46 | #define IFD_EXTRA_SIZE 6 | ||
47 | |||
48 | #define EXIF_TAG_NAME_LENGTH 32 | ||
49 | #define MAKERNOTE_TAG 0x927c | ||
50 | #define ORIENTATION_TAG 0x112 | ||
51 | #define EXIFIFD_TAG 0x8769 | ||
52 | #define IMAGE_WIDTH_TAG 0x100 | ||
53 | #define IMAGE_LENGTH_TAG 0x101 | ||
54 | #define PIXEL_X_TAG 0xa002 | ||
55 | #define PIXEL_Y_TAG 0xa003 | ||
56 | |||
57 | struct exif_tag { | ||
58 | const char name[EXIF_TAG_NAME_LENGTH]; | ||
59 | uint16_t id; | ||
60 | }; | ||
61 | |||
62 | static const struct exif_tag tag_list[] = { // JEITA CP-3451 EXIF specification: | ||
63 | {"GPSVersionID", 0x00}, // <- Table 12 GPS Attribute Information | ||
64 | {"GPSLatitudeRef", 0x01}, | ||
65 | {"GPSLatitude", 0x02}, | ||
66 | {"GPSLongitudeRef", 0x03}, | ||
67 | {"GPSLongitude", 0x04}, | ||
68 | {"GPSAltitudeRef", 0x05}, | ||
69 | {"GPSAltitude", 0x06}, | ||
70 | {"GPSTimeStamp", 0x07}, | ||
71 | {"GPSSatellites", 0x08}, | ||
72 | {"GPSStatus", 0x09}, | ||
73 | {"GPSMeasureMode", 0x0A}, | ||
74 | {"GPSDOP", 0x0B}, | ||
75 | {"GPSSpeedRef", 0x0C}, | ||
76 | {"GPSSpeed", 0x0D}, | ||
77 | {"GPSTrackRef", 0x0E}, | ||
78 | {"GPSTrack", 0x0F}, | ||
79 | {"GPSImgDirectionRef", 0x10}, | ||
80 | {"GPSImgDirection", 0x11}, | ||
81 | {"GPSMapDatum", 0x12}, | ||
82 | {"GPSDestLatitudeRef", 0x13}, | ||
83 | {"GPSDestLatitude", 0x14}, | ||
84 | {"GPSDestLongitudeRef", 0x15}, | ||
85 | {"GPSDestLongitude", 0x16}, | ||
86 | {"GPSDestBearingRef", 0x17}, | ||
87 | {"GPSDestBearing", 0x18}, | ||
88 | {"GPSDestDistanceRef", 0x19}, | ||
89 | {"GPSDestDistance", 0x1A}, | ||
90 | {"GPSProcessingMethod", 0x1B}, | ||
91 | {"GPSAreaInformation", 0x1C}, | ||
92 | {"GPSDateStamp", 0x1D}, | ||
93 | {"GPSDifferential", 0x1E}, | ||
94 | {"ImageWidth", 0x100}, // <- Table 3 TIFF Rev. 6.0 Attribute Information Used in Exif | ||
95 | {"ImageLength", 0x101}, | ||
96 | {"BitsPerSample", 0x102}, | ||
97 | {"Compression", 0x103}, | ||
98 | {"PhotometricInterpretation", 0x106}, | ||
99 | {"Orientation", 0x112}, | ||
100 | {"SamplesPerPixel", 0x115}, | ||
101 | {"PlanarConfiguration", 0x11C}, | ||
102 | {"YCbCrSubSampling", 0x212}, | ||
103 | {"YCbCrPositioning", 0x213}, | ||
104 | {"XResolution", 0x11A}, | ||
105 | {"YResolution", 0x11B}, | ||
106 | {"ResolutionUnit", 0x128}, | ||
107 | {"StripOffsets", 0x111}, | ||
108 | {"RowsPerStrip", 0x116}, | ||
109 | {"StripByteCounts", 0x117}, | ||
110 | {"JPEGInterchangeFormat", 0x201}, | ||
111 | {"JPEGInterchangeFormatLength",0x202}, | ||
112 | {"TransferFunction", 0x12D}, | ||
113 | {"WhitePoint", 0x13E}, | ||
114 | {"PrimaryChromaticities", 0x13F}, | ||
115 | {"YCbCrCoefficients", 0x211}, | ||
116 | {"ReferenceBlackWhite", 0x214}, | ||
117 | {"DateTime", 0x132}, | ||
118 | {"ImageDescription", 0x10E}, | ||
119 | {"Make", 0x10F}, | ||
120 | {"Model", 0x110}, | ||
121 | {"Software", 0x131}, | ||
122 | {"Artist", 0x13B}, | ||
123 | {"Copyright", 0x8298}, | ||
124 | {"ExifVersion", 0x9000}, // <- Table 4 Exif IFD Attribute Information (1) | ||
125 | {"FlashpixVersion", 0xA000}, | ||
126 | {"ColorSpace", 0xA001}, | ||
127 | {"ComponentsConfiguration", 0x9101}, | ||
128 | {"CompressedBitsPerPixel", 0x9102}, | ||
129 | {"PixelXDimension", 0xA002}, | ||
130 | {"PixelYDimension", 0xA003}, | ||
131 | {"MakerNote", 0x927C}, | ||
132 | {"UserComment", 0x9286}, | ||
133 | {"RelatedSoundFile", 0xA004}, | ||
134 | {"DateTimeOriginal", 0x9003}, | ||
135 | {"DateTimeDigitized", 0x9004}, | ||
136 | {"SubSecTime", 0x9290}, | ||
137 | {"SubSecTimeOriginal", 0x9291}, | ||
138 | {"SubSecTimeDigitized", 0x9292}, | ||
139 | {"ImageUniqueID", 0xA420}, | ||
140 | {"ExposureTime", 0x829A}, // <- Table 5 Exif IFD Attribute Information (2) | ||
141 | {"FNumber", 0x829D}, | ||
142 | {"ExposureProgram", 0x8822}, | ||
143 | {"SpectralSensitivity", 0x8824}, | ||
144 | {"ISOSpeedRatings", 0x8827}, | ||
145 | {"OECF", 0x8828}, | ||
146 | {"ShutterSpeedValue", 0x9201}, | ||
147 | {"ApertureValue", 0x9202}, | ||
148 | {"BrightnessValue", 0x9203}, | ||
149 | {"ExposureBiasValue", 0x9204}, | ||
150 | {"MaxApertureValue", 0x9205}, | ||
151 | {"SubjectDistance", 0x9206}, | ||
152 | {"MeteringMode", 0x9207}, | ||
153 | {"LightSource", 0x9208}, | ||
154 | {"Flash", 0x9209}, | ||
155 | {"FocalLength", 0x920A}, | ||
156 | {"SubjectArea", 0x9214}, | ||
157 | {"FlashEnergy", 0xA20B}, | ||
158 | {"SpatialFrequencyResponse", 0xA20C}, | ||
159 | {"FocalPlaneXResolution", 0xA20E}, | ||
160 | {"FocalPlaneYResolution", 0xA20F}, | ||
161 | {"FocalPlaneResolutionUnit", 0xA210}, | ||
162 | {"SubjectLocation", 0xA214}, | ||
163 | {"ExposureIndex", 0xA215}, | ||
164 | {"SensingMethod", 0xA217}, | ||
165 | {"FileSource", 0xA300}, | ||
166 | {"SceneType", 0xA301}, | ||
167 | {"CFAPattern", 0xA302}, | ||
168 | {"CustomRendered", 0xA401}, | ||
169 | {"ExposureMode", 0xA402}, | ||
170 | {"WhiteBalance", 0xA403}, | ||
171 | {"DigitalZoomRatio", 0xA404}, | ||
172 | {"FocalLengthIn35mmFilm", 0xA405}, | ||
173 | {"SceneCaptureType", 0xA406}, | ||
174 | {"GainControl", 0xA407}, | ||
175 | {"Contrast", 0xA408}, | ||
176 | {"Saturation", 0xA409}, | ||
177 | {"Sharpness", 0xA40A}, | ||
178 | {"DeviceSettingDescription", 0xA40B}, | ||
179 | {"SubjectDistanceRange", 0xA40C}, | ||
180 | |||
181 | /* InteropIFD tags */ | ||
182 | {"RelatedImageFileFormat", 0x1000}, | ||
183 | {"RelatedImageWidth", 0x1001}, | ||
184 | {"RelatedImageLength", 0x1002}, | ||
185 | |||
186 | /* private EXIF tags */ | ||
187 | {"PrintImageMatching", 0xC4A5}, // <- undocumented meaning | ||
188 | |||
189 | /* IFD tags */ | ||
190 | {"ExifIFD", 0x8769}, // <- An IFD pointing to standard Exif metadata | ||
191 | {"GPSInfo", 0x8825}, // <- An IFD pointing to GPS Exif Metadata | ||
192 | {"InteropIFD", 0xA005}, // <- Table 13 Interoperability IFD Attribute Information | ||
193 | {"GlobalParametersIFD", 0x0190}, | ||
194 | {"ProfileIFD", 0xc6f5}, | ||
195 | }; | ||
196 | |||
197 | /* same as type_sizes but with string == 1 */ | ||
198 | static const size_t exif_sizes[] = { | ||
199 | [0] = 0, | ||
200 | [AV_TIFF_BYTE] = 1, | ||
201 | [AV_TIFF_STRING] = 1, | ||
202 | [AV_TIFF_SHORT] = 2, | ||
203 | [AV_TIFF_LONG] = 4, | ||
204 | [AV_TIFF_RATIONAL] = 8, | ||
205 | [AV_TIFF_SBYTE] = 1, | ||
206 | [AV_TIFF_UNDEFINED] = 1, | ||
207 | [AV_TIFF_SSHORT] = 2, | ||
208 | [AV_TIFF_SLONG] = 4, | ||
209 | [AV_TIFF_SRATIONAL] = 8, | ||
210 | [AV_TIFF_FLOAT] = 4, | ||
211 | [AV_TIFF_DOUBLE] = 8, | ||
212 | [AV_TIFF_IFD] = 4, | ||
213 | }; | ||
214 | |||
215 | 811 | const char *av_exif_get_tag_name(uint16_t id) | |
216 | { | ||
217 |
2/2✓ Branch 0 taken 62650 times.
✓ Branch 1 taken 284 times.
|
62934 | for (size_t i = 0; i < FF_ARRAY_ELEMS(tag_list); i++) { |
218 |
2/2✓ Branch 0 taken 527 times.
✓ Branch 1 taken 62123 times.
|
62650 | if (tag_list[i].id == id) |
219 | 527 | return tag_list[i].name; | |
220 | } | ||
221 | |||
222 | 284 | return NULL; | |
223 | } | ||
224 | |||
225 | 266 | int32_t av_exif_get_tag_id(const char *name) | |
226 | { | ||
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 266 times.
|
266 | if (!name) |
228 | ✗ | return -1; | |
229 | |||
230 |
1/2✓ Branch 0 taken 9842 times.
✗ Branch 1 not taken.
|
9842 | for (size_t i = 0; i < FF_ARRAY_ELEMS(tag_list); i++) { |
231 |
2/2✓ Branch 0 taken 266 times.
✓ Branch 1 taken 9576 times.
|
9842 | if (!strcmp(tag_list[i].name, name)) |
232 | 266 | return tag_list[i].id; | |
233 | } | ||
234 | |||
235 | ✗ | return -1; | |
236 | } | ||
237 | |||
238 | 2932 | static inline void tput16(PutByteContext *pb, const int le, const uint16_t value) | |
239 | { | ||
240 |
1/2✓ Branch 0 taken 2932 times.
✗ Branch 1 not taken.
|
2932 | le ? bytestream2_put_le16(pb, value) : bytestream2_put_be16(pb, value); |
241 | 2932 | } | |
242 | |||
243 | 3055 | static inline void tput32(PutByteContext *pb, const int le, const uint32_t value) | |
244 | { | ||
245 |
1/2✓ Branch 0 taken 3055 times.
✗ Branch 1 not taken.
|
3055 | le ? bytestream2_put_le32(pb, value) : bytestream2_put_be32(pb, value); |
246 | 3055 | } | |
247 | |||
248 | ✗ | static inline void tput64(PutByteContext *pb, const int le, const uint64_t value) | |
249 | { | ||
250 | ✗ | le ? bytestream2_put_le64(pb, value) : bytestream2_put_be64(pb, value); | |
251 | ✗ | } | |
252 | |||
253 | 1622 | static int exif_read_values(void *logctx, GetByteContext *gb, int le, AVExifEntry *entry) | |
254 | { | ||
255 |
5/9✓ Branch 0 taken 1078 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 181 times.
✓ Branch 4 taken 194 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 149 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1622 | switch (entry->type) { |
256 | 1078 | case AV_TIFF_SHORT: | |
257 | case AV_TIFF_LONG: | ||
258 | 1078 | entry->value.uint = av_calloc(entry->count, sizeof(*entry->value.uint)); | |
259 | 1078 | break; | |
260 | ✗ | case AV_TIFF_SSHORT: | |
261 | case AV_TIFF_SLONG: | ||
262 | ✗ | entry->value.sint = av_calloc(entry->count, sizeof(*entry->value.sint)); | |
263 | ✗ | break; | |
264 | 20 | case AV_TIFF_DOUBLE: | |
265 | case AV_TIFF_FLOAT: | ||
266 | 20 | entry->value.dbl = av_calloc(entry->count, sizeof(*entry->value.dbl)); | |
267 | 20 | break; | |
268 | 181 | case AV_TIFF_RATIONAL: | |
269 | case AV_TIFF_SRATIONAL: | ||
270 | 181 | entry->value.rat = av_calloc(entry->count, sizeof(*entry->value.rat)); | |
271 | 181 | break; | |
272 | 194 | case AV_TIFF_UNDEFINED: | |
273 | case AV_TIFF_BYTE: | ||
274 | 194 | entry->value.ubytes = av_mallocz(entry->count); | |
275 | 194 | break; | |
276 | ✗ | case AV_TIFF_SBYTE: | |
277 | ✗ | entry->value.sbytes = av_mallocz(entry->count); | |
278 | ✗ | break; | |
279 | 149 | case AV_TIFF_STRING: | |
280 | 149 | entry->value.str = av_mallocz(entry->count + 1); | |
281 | 149 | break; | |
282 | ✗ | case AV_TIFF_IFD: | |
283 | ✗ | av_log(logctx, AV_LOG_WARNING, "Bad IFD type for non-IFD tag\n"); | |
284 | ✗ | return AVERROR_INVALIDDATA; | |
285 | } | ||
286 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1622 times.
|
1622 | if (!entry->value.ptr) |
287 | ✗ | return AVERROR(ENOMEM); | |
288 |
6/11✓ Branch 0 taken 835 times.
✓ Branch 1 taken 243 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 20 times.
✓ Branch 6 taken 181 times.
✓ Branch 7 taken 194 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 149 times.
✗ Branch 10 not taken.
|
1622 | switch (entry->type) { |
289 | 835 | case AV_TIFF_SHORT: | |
290 |
2/2✓ Branch 0 taken 2711 times.
✓ Branch 1 taken 835 times.
|
3546 | for (size_t i = 0; i < entry->count; i++) |
291 | 2711 | entry->value.uint[i] = ff_tget_short(gb, le); | |
292 | 835 | break; | |
293 | 243 | case AV_TIFF_LONG: | |
294 |
2/2✓ Branch 0 taken 2240 times.
✓ Branch 1 taken 243 times.
|
2483 | for (size_t i = 0; i < entry->count; i++) |
295 | 2240 | entry->value.uint[i] = ff_tget_long(gb, le); | |
296 | 243 | break; | |
297 | ✗ | case AV_TIFF_SSHORT: | |
298 | ✗ | for (size_t i = 0; i < entry->count; i++) | |
299 | ✗ | entry->value.sint[i] = (int16_t) ff_tget_short(gb, le); | |
300 | ✗ | break; | |
301 | ✗ | case AV_TIFF_SLONG: | |
302 | ✗ | for (size_t i = 0; i < entry->count; i++) | |
303 | ✗ | entry->value.sint[i] = (int32_t) ff_tget_long(gb, le); | |
304 | ✗ | break; | |
305 | ✗ | case AV_TIFF_DOUBLE: | |
306 | ✗ | for (size_t i = 0; i < entry->count; i++) | |
307 | ✗ | entry->value.dbl[i] = ff_tget_double(gb, le); | |
308 | ✗ | break; | |
309 | 20 | case AV_TIFF_FLOAT: | |
310 |
2/2✓ Branch 0 taken 68 times.
✓ Branch 1 taken 20 times.
|
88 | for (size_t i = 0; i < entry->count; i++) { |
311 | 68 | av_alias32 alias = { .u32 = ff_tget_long(gb, le) }; | |
312 | 68 | entry->value.dbl[i] = alias.f32; | |
313 | } | ||
314 | 20 | break; | |
315 | 181 | case AV_TIFF_RATIONAL: | |
316 | case AV_TIFF_SRATIONAL: | ||
317 |
2/2✓ Branch 0 taken 250 times.
✓ Branch 1 taken 181 times.
|
431 | for (size_t i = 0; i < entry->count; i++) { |
318 | 250 | int32_t num = ff_tget_long(gb, le); | |
319 | 250 | int32_t den = ff_tget_long(gb, le); | |
320 | 250 | entry->value.rat[i] = av_make_q(num, den); | |
321 | } | ||
322 | 181 | break; | |
323 | 194 | case AV_TIFF_UNDEFINED: | |
324 | case AV_TIFF_BYTE: | ||
325 | /* these three fields are aliased to entry->value.ptr via a union */ | ||
326 | /* and entry->value.ptr will always be nonzero here */ | ||
327 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 194 times.
|
194 | av_assert0(entry->value.ubytes); |
328 | 194 | bytestream2_get_buffer(gb, entry->value.ubytes, entry->count); | |
329 | 194 | break; | |
330 | ✗ | case AV_TIFF_SBYTE: | |
331 | ✗ | av_assert0(entry->value.sbytes); | |
332 | ✗ | bytestream2_get_buffer(gb, entry->value.sbytes, entry->count); | |
333 | ✗ | break; | |
334 | 149 | case AV_TIFF_STRING: | |
335 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
|
149 | av_assert0(entry->value.str); |
336 | 149 | bytestream2_get_buffer(gb, entry->value.str, entry->count); | |
337 | 149 | break; | |
338 | } | ||
339 | |||
340 | 1622 | return 0; | |
341 | } | ||
342 | |||
343 | 930 | static void exif_write_values(PutByteContext *pb, int le, const AVExifEntry *entry) | |
344 | { | ||
345 |
6/11✓ Branch 0 taken 484 times.
✓ Branch 1 taken 155 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 97 times.
✓ Branch 7 taken 112 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 72 times.
✗ Branch 10 not taken.
|
930 | switch (entry->type) { |
346 | 484 | case AV_TIFF_SHORT: | |
347 |
2/2✓ Branch 0 taken 1003 times.
✓ Branch 1 taken 484 times.
|
1487 | for (size_t i = 0; i < entry->count; i++) |
348 | 1003 | tput16(pb, le, entry->value.uint[i]); | |
349 | 484 | break; | |
350 | 155 | case AV_TIFF_LONG: | |
351 |
2/2✓ Branch 0 taken 1438 times.
✓ Branch 1 taken 155 times.
|
1593 | for (size_t i = 0; i < entry->count; i++) |
352 | 1438 | tput32(pb, le, entry->value.uint[i]); | |
353 | 155 | break; | |
354 | ✗ | case AV_TIFF_SSHORT: | |
355 | ✗ | for (size_t i = 0; i < entry->count; i++) | |
356 | ✗ | tput16(pb, le, entry->value.sint[i]); | |
357 | ✗ | break; | |
358 | ✗ | case AV_TIFF_SLONG: | |
359 | ✗ | for (size_t i = 0; i < entry->count; i++) | |
360 | ✗ | tput32(pb, le, entry->value.sint[i]); | |
361 | ✗ | break; | |
362 | ✗ | case AV_TIFF_DOUBLE: | |
363 | ✗ | for (size_t i = 0; i < entry->count; i++) { | |
364 | ✗ | const av_alias64 a = { .f64 = entry->value.dbl[i] }; | |
365 | ✗ | tput64(pb, le, a.u64); | |
366 | } | ||
367 | ✗ | break; | |
368 | 10 | case AV_TIFF_FLOAT: | |
369 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10 times.
|
44 | for (size_t i = 0; i < entry->count; i++) { |
370 | 34 | const av_alias32 a = { .f32 = entry->value.dbl[i] }; | |
371 | 34 | tput32(pb, le, a.u32); | |
372 | } | ||
373 | 10 | break; | |
374 | 97 | case AV_TIFF_RATIONAL: | |
375 | case AV_TIFF_SRATIONAL: | ||
376 |
2/2✓ Branch 0 taken 133 times.
✓ Branch 1 taken 97 times.
|
230 | for (size_t i = 0; i < entry->count; i++) { |
377 | 133 | tput32(pb, le, entry->value.rat[i].num); | |
378 | 133 | tput32(pb, le, entry->value.rat[i].den); | |
379 | } | ||
380 | 97 | break; | |
381 | 112 | case AV_TIFF_UNDEFINED: | |
382 | case AV_TIFF_BYTE: | ||
383 | 112 | bytestream2_put_buffer(pb, entry->value.ubytes, entry->count); | |
384 | 112 | break; | |
385 | ✗ | case AV_TIFF_SBYTE: | |
386 | ✗ | bytestream2_put_buffer(pb, entry->value.sbytes, entry->count); | |
387 | ✗ | break; | |
388 | 72 | case AV_TIFF_STRING: | |
389 | 72 | bytestream2_put_buffer(pb, entry->value.str, entry->count); | |
390 | 72 | break; | |
391 | } | ||
392 | 930 | } | |
393 | |||
394 | static const uint8_t aoc_header[] = { 'A', 'O', 'C', 0, }; | ||
395 | static const uint8_t casio_header[] = { 'Q', 'V', 'C', 0, 0, 0, }; | ||
396 | static const uint8_t foveon_header[] = { 'F', 'O', 'V', 'E', 'O', 'N', 0, 0, }; | ||
397 | static const uint8_t fuji_header[] = { 'F', 'U', 'J', 'I', }; | ||
398 | static const uint8_t nikon_header[] = { 'N', 'i', 'k', 'o', 'n', 0, }; | ||
399 | static const uint8_t olympus1_header[] = { 'O', 'L', 'Y', 'M', 'P', 0, }; | ||
400 | static const uint8_t olympus2_header[] = { 'O', 'L', 'Y', 'M', 'P', 'U', 'S', 0, 'I', 'I', }; | ||
401 | static const uint8_t panasonic_header[] = { 'P', 'a', 'n', 'a', 's', 'o', 'n', 'i', 'c', 0, 0, 0, }; | ||
402 | static const uint8_t sigma_header[] = { 'S', 'I', 'G', 'M', 'A', 0, 0, 0, }; | ||
403 | static const uint8_t sony_header[] = { 'S', 'O', 'N', 'Y', ' ', 'D', 'S', 'C', ' ', 0, 0, 0, }; | ||
404 | |||
405 | struct exif_makernote_data { | ||
406 | const uint8_t *header; | ||
407 | size_t header_size; | ||
408 | int result; | ||
409 | }; | ||
410 | |||
411 | #define MAKERNOTE_STRUCT(h, r) { \ | ||
412 | .header = (h), \ | ||
413 | .header_size = sizeof((h)), \ | ||
414 | .result = (r), \ | ||
415 | } | ||
416 | |||
417 | static const struct exif_makernote_data makernote_data[] = { | ||
418 | MAKERNOTE_STRUCT(aoc_header, 6), | ||
419 | MAKERNOTE_STRUCT(casio_header, -1), | ||
420 | MAKERNOTE_STRUCT(foveon_header, 10), | ||
421 | MAKERNOTE_STRUCT(fuji_header, -1), | ||
422 | MAKERNOTE_STRUCT(olympus1_header, 8), | ||
423 | MAKERNOTE_STRUCT(olympus2_header, -1), | ||
424 | MAKERNOTE_STRUCT(panasonic_header, 12), | ||
425 | MAKERNOTE_STRUCT(sigma_header, 10), | ||
426 | MAKERNOTE_STRUCT(sony_header, 12), | ||
427 | }; | ||
428 | |||
429 | /* | ||
430 | * derived from Exiv2 MakerNote's article | ||
431 | * https://exiv2.org/makernote.html or archived at | ||
432 | * https://web.archive.org/web/20250311155857/https://exiv2.org/makernote.html | ||
433 | */ | ||
434 | 7 | static int exif_get_makernote_offset(GetByteContext *gb) | |
435 | { | ||
436 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
|
7 | if (bytestream2_get_bytes_left(gb) < BASE_TAG_SIZE) |
437 | ✗ | return -1; | |
438 | |||
439 |
2/2✓ Branch 0 taken 57 times.
✓ Branch 1 taken 4 times.
|
61 | for (int i = 0; i < FF_ARRAY_ELEMS(makernote_data); i++) { |
440 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 54 times.
|
57 | if (!memcmp(gb->buffer, makernote_data[i].header, makernote_data[i].header_size)) |
441 | 3 | return makernote_data[i].result; | |
442 | } | ||
443 | |||
444 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (!memcmp(gb->buffer, nikon_header, sizeof(nikon_header))) { |
445 | ✗ | if (bytestream2_get_bytes_left(gb) < 14) | |
446 | ✗ | return -1; | |
447 | ✗ | else if (AV_RB32(gb->buffer + 10) == EXIF_MM_LONG || AV_RB32(gb->buffer + 10) == EXIF_II_LONG) | |
448 | ✗ | return -1; | |
449 | ✗ | return 8; | |
450 | } | ||
451 | |||
452 | 4 | return 0; | |
453 | } | ||
454 | |||
455 | static int exif_parse_ifd_list(void *logctx, GetByteContext *gb, int le, | ||
456 | int depth, AVExifMetadata *ifd); | ||
457 | |||
458 | 1652 | static int exif_decode_tag(void *logctx, GetByteContext *gb, int le, | |
459 | int depth, AVExifEntry *entry) | ||
460 | { | ||
461 | 1652 | int ret = 0, makernote_offset = -1, tell, is_ifd, count; | |
462 | enum AVTiffDataType type; | ||
463 | uint32_t payload; | ||
464 | |||
465 | /* safety check to prevent infinite recursion on malicious IFDs */ | ||
466 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1652 times.
|
1652 | if (depth > 3) |
467 | ✗ | return AVERROR_INVALIDDATA; | |
468 | |||
469 | 1652 | tell = bytestream2_tell(gb); | |
470 | |||
471 | 1652 | entry->id = ff_tget_short(gb, le); | |
472 | 1652 | type = ff_tget_short(gb, le); | |
473 | 1652 | count = ff_tget_long(gb, le); | |
474 | 1652 | payload = ff_tget_long(gb, le); | |
475 | |||
476 | 1652 | av_log(logctx, AV_LOG_DEBUG, "TIFF Tag: id: 0x%04x, type: %d, count: %u, offset: %d, " | |
477 | 1652 | "payload: %" PRIu32 "\n", entry->id, type, count, tell, payload); | |
478 | |||
479 | /* AV_TIFF_IFD is the largest, numerically */ | ||
480 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1652 times.
|
1652 | if (type > AV_TIFF_IFD) |
481 | ✗ | return AVERROR_INVALIDDATA; | |
482 | |||
483 |
6/6✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1629 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 7 times.
✓ Branch 6 taken 1622 times.
|
1652 | is_ifd = type == AV_TIFF_IFD || ff_tis_ifd(entry->id) || entry->id == MAKERNOTE_TAG; |
484 | |||
485 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1622 times.
|
1652 | if (is_ifd) { |
486 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!payload) |
487 | ✗ | goto end; | |
488 | 30 | bytestream2_seek(gb, payload, SEEK_SET); | |
489 | } | ||
490 | |||
491 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1645 times.
|
1652 | if (entry->id == MAKERNOTE_TAG) { |
492 | 7 | makernote_offset = exif_get_makernote_offset(gb); | |
493 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | if (makernote_offset < 0) |
494 | ✗ | is_ifd = 0; | |
495 | } | ||
496 | |||
497 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1622 times.
|
1652 | if (is_ifd) { |
498 | 30 | entry->type = AV_TIFF_IFD; | |
499 | 30 | entry->count = 1; | |
500 | 30 | entry->ifd_offset = makernote_offset > 0 ? makernote_offset : 0; | |
501 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
|
30 | if (entry->ifd_offset) { |
502 | 3 | entry->ifd_lead = av_malloc(entry->ifd_offset); | |
503 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (!entry->ifd_lead) |
504 | ✗ | return AVERROR(ENOMEM); | |
505 | 3 | bytestream2_get_buffer(gb, entry->ifd_lead, entry->ifd_offset); | |
506 | } | ||
507 | 30 | ret = exif_parse_ifd_list(logctx, gb, le, depth + 1, &entry->value.ifd); | |
508 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
30 | if (ret < 0 && entry->id == MAKERNOTE_TAG) { |
509 | /* | ||
510 | * we guessed that MakerNote was an IFD | ||
511 | * but we were probably incorrect at this | ||
512 | * point so we try again as a binary blob | ||
513 | */ | ||
514 | ✗ | av_exif_free(&entry->value.ifd); | |
515 | ✗ | av_log(logctx, AV_LOG_DEBUG, "unrecognized MakerNote IFD, retrying as blob\n"); | |
516 | ✗ | is_ifd = 0; | |
517 | } | ||
518 | } | ||
519 | |||
520 | /* inverted condition instead of else so we can fall through from above */ | ||
521 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1622 times.
|
1652 | if (!is_ifd) { |
522 |
1/2✓ Branch 0 taken 1622 times.
✗ Branch 1 not taken.
|
1622 | entry->type = type == AV_TIFF_IFD ? AV_TIFF_UNDEFINED : type; |
523 | 1622 | entry->count = count; | |
524 |
2/2✓ Branch 0 taken 1076 times.
✓ Branch 1 taken 546 times.
|
1622 | bytestream2_seek(gb, count * exif_sizes[type] > 4 ? payload : tell + 8, SEEK_SET); |
525 | 1622 | ret = exif_read_values(logctx, gb, le, entry); | |
526 | } | ||
527 | |||
528 | 30 | end: | |
529 | 1652 | bytestream2_seek(gb, tell + BASE_TAG_SIZE, SEEK_SET); | |
530 | |||
531 | 1652 | return ret; | |
532 | } | ||
533 | |||
534 | 88 | static int exif_parse_ifd_list(void *logctx, GetByteContext *gb, int le, | |
535 | int depth, AVExifMetadata *ifd) | ||
536 | { | ||
537 | uint32_t entries; | ||
538 | size_t required_size; | ||
539 | void *temp; | ||
540 | |||
541 | 88 | av_log(logctx, AV_LOG_DEBUG, "parsing IFD list at offset: %d\n", bytestream2_tell(gb)); | |
542 | |||
543 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | if (bytestream2_get_bytes_left(gb) < 2) { |
544 | ✗ | av_log(logctx, AV_LOG_ERROR, "not enough bytes remaining in EXIF buffer: 2 required\n"); | |
545 | ✗ | return AVERROR_INVALIDDATA; | |
546 | } | ||
547 | |||
548 | 88 | entries = ff_tget_short(gb, le); | |
549 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | if (bytestream2_get_bytes_left(gb) < entries * BASE_TAG_SIZE) { |
550 | ✗ | av_log(logctx, AV_LOG_ERROR, "not enough bytes remaining in EXIF buffer. entries: %" PRIu32 "\n", entries); | |
551 | ✗ | return AVERROR_INVALIDDATA; | |
552 | } | ||
553 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (entries > 4096) { |
554 | /* that is a lot of entries, probably an error */ | ||
555 | ✗ | av_log(logctx, AV_LOG_ERROR, "too many entries: %" PRIu32 "\n", entries); | |
556 | ✗ | return AVERROR_INVALIDDATA; | |
557 | } | ||
558 | |||
559 | 88 | ifd->count = entries; | |
560 | 88 | av_log(logctx, AV_LOG_DEBUG, "entry count for IFD: %u\n", ifd->count); | |
561 | |||
562 | /* empty IFD is technically legal but equivalent to no metadata present */ | ||
563 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (!ifd->count) |
564 | ✗ | goto end; | |
565 | |||
566 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
|
88 | if (av_size_mult(ifd->count, sizeof(*ifd->entries), &required_size) < 0) |
567 | ✗ | return AVERROR(ENOMEM); | |
568 | 88 | temp = av_fast_realloc(ifd->entries, &ifd->size, required_size); | |
569 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
|
88 | if (!temp) { |
570 | ✗ | av_freep(&ifd->entries); | |
571 | ✗ | return AVERROR(ENOMEM); | |
572 | } | ||
573 | 88 | ifd->entries = temp; | |
574 | |||
575 | /* entries have pointers in them which can cause issues if */ | ||
576 | /* they are freed or realloc'd when garbage */ | ||
577 | 88 | memset(ifd->entries, 0, required_size); | |
578 | |||
579 |
2/2✓ Branch 0 taken 1652 times.
✓ Branch 1 taken 88 times.
|
1740 | for (uint32_t i = 0; i < entries; i++) { |
580 | 1652 | int ret = exif_decode_tag(logctx, gb, le, depth, &ifd->entries[i]); | |
581 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1652 times.
|
1652 | if (ret < 0) |
582 | ✗ | return ret; | |
583 | } | ||
584 | |||
585 | 88 | end: | |
586 | /* | ||
587 | * at the end of an IFD is an pointer to the next IFD | ||
588 | * or zero if there are no more IFDs, which is usually the case | ||
589 | */ | ||
590 | 88 | return ff_tget_long(gb, le); | |
591 | } | ||
592 | |||
593 | /* | ||
594 | * note that this function does not free the entry pointer itself | ||
595 | * because it's probably part of a larger array that should be freed | ||
596 | * all at once | ||
597 | */ | ||
598 | 1661 | static void exif_free_entry(AVExifEntry *entry) | |
599 | { | ||
600 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1661 times.
|
1661 | if (!entry) |
601 | ✗ | return; | |
602 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1630 times.
|
1661 | if (entry->type == AV_TIFF_IFD) |
603 | 31 | av_exif_free(&entry->value.ifd); | |
604 | else | ||
605 | 1630 | av_freep(&entry->value.ptr); | |
606 | 1661 | av_freep(&entry->ifd_lead); | |
607 | } | ||
608 | |||
609 | 2938 | void av_exif_free(AVExifMetadata *ifd) | |
610 | { | ||
611 |
2/2✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2910 times.
|
2938 | if (!ifd) |
612 | 28 | return; | |
613 |
2/2✓ Branch 0 taken 2821 times.
✓ Branch 1 taken 89 times.
|
2910 | if (!ifd->entries) { |
614 | 2821 | ifd->count = 0; | |
615 | 2821 | ifd->size = 0; | |
616 | 2821 | return; | |
617 | } | ||
618 |
2/2✓ Branch 0 taken 1659 times.
✓ Branch 1 taken 89 times.
|
1748 | for (size_t i = 0; i < ifd->count; i++) { |
619 | 1659 | AVExifEntry *entry = &ifd->entries[i]; | |
620 | 1659 | exif_free_entry(entry); | |
621 | } | ||
622 | 89 | av_freep(&ifd->entries); | |
623 | 89 | ifd->count = 0; | |
624 | 89 | ifd->size = 0; | |
625 | } | ||
626 | |||
627 | 46 | static size_t exif_get_ifd_size(const AVExifMetadata *ifd) | |
628 | { | ||
629 | /* 6 == 4 + 2; 2-byte entry-count at the beginning */ | ||
630 | /* plus 4-byte next-IFD pointer at the end */ | ||
631 | 46 | size_t total_size = IFD_EXTRA_SIZE; | |
632 |
2/2✓ Branch 0 taken 1370 times.
✓ Branch 1 taken 46 times.
|
1416 | for (size_t i = 0; i < ifd->count; i++) { |
633 | 1370 | const AVExifEntry *entry = &ifd->entries[i]; | |
634 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1357 times.
|
1370 | if (entry->type == AV_TIFF_IFD) { |
635 | 13 | total_size += BASE_TAG_SIZE + exif_get_ifd_size(&entry->value.ifd) + entry->ifd_offset; | |
636 | } else { | ||
637 | 1357 | size_t payload_size = entry->count * exif_sizes[entry->type]; | |
638 |
2/2✓ Branch 0 taken 402 times.
✓ Branch 1 taken 955 times.
|
1357 | total_size += BASE_TAG_SIZE + (payload_size > 4 ? payload_size : 0); |
639 | } | ||
640 | } | ||
641 | 46 | return total_size; | |
642 | } | ||
643 | |||
644 | 43 | static int exif_write_ifd(void *logctx, PutByteContext *pb, int le, int depth, const AVExifMetadata *ifd) | |
645 | { | ||
646 | int offset, ret, tell, tell2; | ||
647 | 43 | tell = bytestream2_tell_p(pb); | |
648 | 43 | tput16(pb, le, ifd->count); | |
649 | 43 | offset = tell + IFD_EXTRA_SIZE + BASE_TAG_SIZE * (uint32_t) ifd->count; | |
650 | 43 | av_log(logctx, AV_LOG_DEBUG, "writing IFD with %u entries and initial offset %d\n", ifd->count, offset); | |
651 |
2/2✓ Branch 0 taken 943 times.
✓ Branch 1 taken 43 times.
|
986 | for (size_t i = 0; i < ifd->count; i++) { |
652 | 943 | const AVExifEntry *entry = &ifd->entries[i]; | |
653 | 943 | av_log(logctx, AV_LOG_DEBUG, "writing TIFF entry: id: 0x%04" PRIx16 ", type: %d, count: %" | |
654 | PRIu32 ", offset: %d, offset value: %d\n", | ||
655 | 943 | entry->id, entry->type, entry->count, | |
656 | bytestream2_tell_p(pb), offset); | ||
657 | 943 | tput16(pb, le, entry->id); | |
658 |
3/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 940 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
946 | if (entry->id == MAKERNOTE_TAG && entry->type == AV_TIFF_IFD) { |
659 | 3 | size_t ifd_size = exif_get_ifd_size(&entry->value.ifd); | |
660 | 3 | tput16(pb, le, AV_TIFF_UNDEFINED); | |
661 | 3 | tput32(pb, le, ifd_size); | |
662 | } else { | ||
663 | 940 | tput16(pb, le, entry->type); | |
664 | 940 | tput32(pb, le, entry->count); | |
665 | } | ||
666 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 930 times.
|
943 | if (entry->type == AV_TIFF_IFD) { |
667 | 13 | tput32(pb, le, offset); | |
668 | 13 | tell2 = bytestream2_tell_p(pb); | |
669 | 13 | bytestream2_seek_p(pb, offset, SEEK_SET); | |
670 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
|
13 | if (entry->ifd_offset) |
671 | 2 | bytestream2_put_buffer(pb, entry->ifd_lead, entry->ifd_offset); | |
672 | 13 | ret = exif_write_ifd(logctx, pb, le, depth + 1, &entry->value.ifd); | |
673 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
|
13 | if (ret < 0) |
674 | ✗ | return ret; | |
675 | 13 | offset += ret + entry->ifd_offset; | |
676 | 13 | bytestream2_seek_p(pb, tell2, SEEK_SET); | |
677 | } else { | ||
678 | 930 | size_t payload_size = entry->count * exif_sizes[entry->type]; | |
679 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 642 times.
|
930 | if (payload_size > 4) { |
680 | 288 | tput32(pb, le, offset); | |
681 | 288 | tell2 = bytestream2_tell_p(pb); | |
682 | 288 | bytestream2_seek_p(pb, offset, SEEK_SET); | |
683 | 288 | exif_write_values(pb, le, entry); | |
684 | 288 | offset += payload_size; | |
685 | 288 | bytestream2_seek_p(pb, tell2, SEEK_SET); | |
686 | } else { | ||
687 | /* zero uninitialized excess payload values */ | ||
688 | 642 | AV_WN32(pb->buffer, 0); | |
689 | 642 | exif_write_values(pb, le, entry); | |
690 | 642 | bytestream2_seek_p(pb, 4 - payload_size, SEEK_CUR); | |
691 | } | ||
692 | } | ||
693 | } | ||
694 | |||
695 | /* | ||
696 | * we write 0 if this is the top-level exif IFD | ||
697 | * indicating that there are no more IFD pointers | ||
698 | */ | ||
699 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 30 times.
|
43 | tput32(pb, le, depth ? offset : 0); |
700 | 43 | return offset - tell; | |
701 | } | ||
702 | |||
703 | 30 | int av_exif_write(void *logctx, const AVExifMetadata *ifd, AVBufferRef **buffer, enum AVExifHeaderMode header_mode) | |
704 | { | ||
705 | 30 | AVBufferRef *buf = NULL; | |
706 | 30 | size_t size, headsize = 8; | |
707 | PutByteContext pb; | ||
708 | 30 | int ret, off = 0; | |
709 | |||
710 | 30 | int le = 1; | |
711 | |||
712 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (*buffer) |
713 | ✗ | return AVERROR(EINVAL); | |
714 | |||
715 | 30 | size = exif_get_ifd_size(ifd); | |
716 |
2/5✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
|
30 | switch (header_mode) { |
717 | ✗ | case AV_EXIF_EXIF00: | |
718 | ✗ | off = 6; | |
719 | ✗ | break; | |
720 | 2 | case AV_EXIF_T_OFF: | |
721 | 2 | off = 4; | |
722 | 2 | break; | |
723 | ✗ | case AV_EXIF_ASSUME_BE: | |
724 | ✗ | le = 0; | |
725 | ✗ | headsize = 0; | |
726 | ✗ | break; | |
727 | ✗ | case AV_EXIF_ASSUME_LE: | |
728 | ✗ | le = 1; | |
729 | ✗ | headsize = 0; | |
730 | ✗ | break; | |
731 | } | ||
732 | 30 | buf = av_buffer_alloc(size + off + headsize); | |
733 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (!buf) |
734 | ✗ | return AVERROR(ENOMEM); | |
735 | |||
736 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (header_mode == AV_EXIF_EXIF00) { |
737 | ✗ | AV_WL32(buf->data, MKTAG('E','x','i','f')); | |
738 | ✗ | AV_WN16(buf->data + 4, 0); | |
739 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
|
30 | } else if (header_mode == AV_EXIF_T_OFF) { |
740 | 2 | AV_WN32(buf->data, 0); | |
741 | } | ||
742 | |||
743 | 30 | bytestream2_init_writer(&pb, buf->data + off, buf->size - off); | |
744 | |||
745 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
|
30 | if (header_mode != AV_EXIF_ASSUME_BE && header_mode != AV_EXIF_ASSUME_LE) { |
746 | /* these constants are be32 in both cases */ | ||
747 | /* le == 1 always in this case */ | ||
748 | 30 | bytestream2_put_be32(&pb, EXIF_II_LONG); | |
749 | 30 | tput32(&pb, le, 8); | |
750 | } | ||
751 | |||
752 | 30 | ret = exif_write_ifd(logctx, &pb, le, 0, ifd); | |
753 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | if (ret < 0) { |
754 | ✗ | av_buffer_unref(&buf); | |
755 | ✗ | av_log(logctx, AV_LOG_ERROR, "error writing EXIF data: %s\n", av_err2str(ret)); | |
756 | ✗ | return ret; | |
757 | } | ||
758 | |||
759 | 30 | *buffer = buf; | |
760 | |||
761 | 30 | return 0; | |
762 | } | ||
763 | |||
764 | 58 | int av_exif_parse_buffer(void *logctx, const uint8_t *buf, size_t size, | |
765 | AVExifMetadata *ifd, enum AVExifHeaderMode header_mode) | ||
766 | { | ||
767 | int ret, le; | ||
768 | GetByteContext gbytes; | ||
769 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (size > INT_MAX) |
770 | ✗ | return AVERROR(EINVAL); | |
771 | 58 | size_t off = 0; | |
772 |
2/6✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
58 | switch (header_mode) { |
773 | ✗ | case AV_EXIF_EXIF00: | |
774 | ✗ | if (size < 6) | |
775 | ✗ | return AVERROR_INVALIDDATA; | |
776 | ✗ | off = 6; | |
777 | /* fallthrough */ | ||
778 | 2 | case AV_EXIF_T_OFF: | |
779 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (size < 4) |
780 | ✗ | return AVERROR_INVALIDDATA; | |
781 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (!off) |
782 | 2 | off = AV_RB32(buf) + 4; | |
783 | /* fallthrough */ | ||
784 | case AV_EXIF_TIFF_HEADER: { | ||
785 | int ifd_offset; | ||
786 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (size <= off) |
787 | ✗ | return AVERROR_INVALIDDATA; | |
788 | 58 | bytestream2_init(&gbytes, buf + off, size - off); | |
789 | // read TIFF header | ||
790 | 58 | ret = ff_tdecode_header(&gbytes, &le, &ifd_offset); | |
791 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (ret < 0) { |
792 | ✗ | av_log(logctx, AV_LOG_ERROR, "invalid TIFF header in EXIF data: %s\n", av_err2str(ret)); | |
793 | ✗ | return ret; | |
794 | } | ||
795 | 58 | bytestream2_seek(&gbytes, ifd_offset, SEEK_SET); | |
796 | 58 | break; | |
797 | } | ||
798 | ✗ | case AV_EXIF_ASSUME_LE: | |
799 | ✗ | le = 1; | |
800 | ✗ | bytestream2_init(&gbytes, buf, size); | |
801 | ✗ | break; | |
802 | ✗ | case AV_EXIF_ASSUME_BE: | |
803 | ✗ | le = 0; | |
804 | ✗ | bytestream2_init(&gbytes, buf, size); | |
805 | ✗ | break; | |
806 | ✗ | default: | |
807 | ✗ | return AVERROR(EINVAL); | |
808 | } | ||
809 | |||
810 | /* | ||
811 | * parse IFD0 here. If the return value is positive that tells us | ||
812 | * there is subimage metadata, but we don't parse that IFD here | ||
813 | */ | ||
814 | 58 | ret = exif_parse_ifd_list(logctx, &gbytes, le, 0, ifd); | |
815 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (ret < 0) { |
816 | ✗ | av_exif_free(ifd); | |
817 | ✗ | av_log(logctx, AV_LOG_ERROR, "error decoding EXIF data: %s\n", av_err2str(ret)); | |
818 | ✗ | return ret; | |
819 | } | ||
820 | |||
821 | 58 | return bytestream2_tell(&gbytes); | |
822 | } | ||
823 | |||
824 | #define COLUMN_SEP(i, c) ((i) ? ((i) % (c) ? ", " : "\n") : "") | ||
825 | |||
826 | 43 | static int exif_ifd_to_dict(void *logctx, const char *prefix, const AVExifMetadata *ifd, AVDictionary **metadata) | |
827 | { | ||
828 | AVBPrint bp; | ||
829 | 43 | int ret = 0; | |
830 | 43 | char *key = NULL; | |
831 | 43 | char *value = NULL; | |
832 | |||
833 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
|
43 | if (!prefix) |
834 | ✗ | prefix = ""; | |
835 | |||
836 |
2/2✓ Branch 0 taken 811 times.
✓ Branch 1 taken 43 times.
|
854 | for (uint16_t i = 0; i < ifd->count; i++) { |
837 | 811 | const AVExifEntry *entry = &ifd->entries[i]; | |
838 | 811 | const char *name = av_exif_get_tag_name(entry->id); | |
839 | 811 | av_bprint_init(&bp, entry->count * 10, AV_BPRINT_SIZE_UNLIMITED); | |
840 |
2/2✓ Branch 0 taken 432 times.
✓ Branch 1 taken 379 times.
|
811 | if (*prefix) |
841 | 432 | av_bprintf(&bp, "%s/", prefix); | |
842 |
2/2✓ Branch 0 taken 527 times.
✓ Branch 1 taken 284 times.
|
811 | if (name) |
843 | 527 | av_bprintf(&bp, "%s", name); | |
844 | else | ||
845 | 284 | av_bprintf(&bp, "0x%04X", entry->id); | |
846 | 811 | ret = av_bprint_finalize(&bp, &key); | |
847 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 811 times.
|
811 | if (ret < 0) |
848 | ✗ | goto end; | |
849 | 811 | av_bprint_init(&bp, entry->count * 10, AV_BPRINT_SIZE_UNLIMITED); | |
850 |
6/9✓ Branch 0 taken 14 times.
✓ Branch 1 taken 544 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 65 times.
✓ Branch 6 taken 76 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
811 | switch (entry->type) { |
851 | 14 | case AV_TIFF_IFD: | |
852 | 14 | ret = exif_ifd_to_dict(logctx, key, &entry->value.ifd, metadata); | |
853 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (ret < 0) |
854 | ✗ | goto end; | |
855 | 14 | break; | |
856 | 544 | case AV_TIFF_SHORT: | |
857 | case AV_TIFF_LONG: | ||
858 |
2/2✓ Branch 0 taken 3539 times.
✓ Branch 1 taken 544 times.
|
4083 | for (uint32_t j = 0; j < entry->count; j++) |
859 |
4/4✓ Branch 0 taken 2995 times.
✓ Branch 1 taken 544 times.
✓ Branch 2 taken 2655 times.
✓ Branch 3 taken 340 times.
|
3539 | av_bprintf(&bp, "%s%7" PRIu32, COLUMN_SEP(j, 8), (uint32_t)entry->value.uint[j]); |
860 | 544 | break; | |
861 | ✗ | case AV_TIFF_SSHORT: | |
862 | case AV_TIFF_SLONG: | ||
863 | ✗ | for (uint32_t j = 0; j < entry->count; j++) | |
864 | ✗ | av_bprintf(&bp, "%s%7" PRId32, COLUMN_SEP(j, 8), (int32_t)entry->value.sint[j]); | |
865 | ✗ | break; | |
866 | 102 | case AV_TIFF_RATIONAL: | |
867 | case AV_TIFF_SRATIONAL: | ||
868 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 102 times.
|
237 | for (uint32_t j = 0; j < entry->count; j++) |
869 |
4/4✓ Branch 0 taken 33 times.
✓ Branch 1 taken 102 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 5 times.
|
135 | av_bprintf(&bp, "%s%7i:%-7i", COLUMN_SEP(j, 4), entry->value.rat[j].num, entry->value.rat[j].den); |
870 | 102 | break; | |
871 | 10 | case AV_TIFF_DOUBLE: | |
872 | case AV_TIFF_FLOAT: | ||
873 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10 times.
|
44 | for (uint32_t j = 0; j < entry->count; j++) |
874 |
3/4✓ Branch 0 taken 24 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
|
34 | av_bprintf(&bp, "%s%.15g", COLUMN_SEP(j, 4), entry->value.dbl[j]); |
875 | 10 | break; | |
876 | 65 | case AV_TIFF_STRING: | |
877 | 65 | av_bprintf(&bp, "%s", entry->value.str); | |
878 | 65 | break; | |
879 | 76 | case AV_TIFF_UNDEFINED: | |
880 | case AV_TIFF_BYTE: | ||
881 |
2/2✓ Branch 0 taken 27795 times.
✓ Branch 1 taken 76 times.
|
27871 | for (uint32_t j = 0; j < entry->count; j++) |
882 |
4/4✓ Branch 0 taken 27719 times.
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 26012 times.
✓ Branch 3 taken 1707 times.
|
27795 | av_bprintf(&bp, "%s%3i", COLUMN_SEP(j, 16), entry->value.ubytes[j]); |
883 | 76 | break; | |
884 | ✗ | case AV_TIFF_SBYTE: | |
885 | ✗ | for (uint32_t j = 0; j < entry->count; j++) | |
886 | ✗ | av_bprintf(&bp, "%s%3i", COLUMN_SEP(j, 16), entry->value.sbytes[j]); | |
887 | ✗ | break; | |
888 | } | ||
889 |
2/2✓ Branch 0 taken 797 times.
✓ Branch 1 taken 14 times.
|
811 | if (entry->type != AV_TIFF_IFD) { |
890 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 797 times.
|
797 | if (!av_bprint_is_complete(&bp)) { |
891 | ✗ | av_bprint_finalize(&bp, NULL); | |
892 | ✗ | ret = AVERROR(ENOMEM); | |
893 | ✗ | goto end; | |
894 | } | ||
895 | 797 | ret = av_bprint_finalize(&bp, &value); | |
896 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 797 times.
|
797 | if (ret < 0) |
897 | ✗ | goto end; | |
898 | 797 | ret = av_dict_set(metadata, key, value, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL); | |
899 | 797 | key = NULL; | |
900 | 797 | value = NULL; | |
901 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 797 times.
|
797 | if (ret < 0) |
902 | ✗ | goto end; | |
903 | } else { | ||
904 | 14 | av_freep(&key); | |
905 | } | ||
906 | } | ||
907 | |||
908 | 43 | end: | |
909 | 43 | av_freep(&key); | |
910 | 43 | av_freep(&value); | |
911 | 43 | return ret; | |
912 | } | ||
913 | |||
914 | 29 | int av_exif_ifd_to_dict(void *logctx, const AVExifMetadata *ifd, AVDictionary **metadata) | |
915 | { | ||
916 | 29 | return exif_ifd_to_dict(logctx, "", ifd, metadata); | |
917 | } | ||
918 | |||
919 | #if LIBAVCODEC_VERSION_MAJOR < 63 | ||
920 | ✗ | int avpriv_exif_decode_ifd(void *logctx, const uint8_t *buf, int size, | |
921 | int le, int depth, AVDictionary **metadata) | ||
922 | { | ||
923 | ✗ | AVExifMetadata ifd = { 0 }; | |
924 | GetByteContext gb; | ||
925 | int ret; | ||
926 | ✗ | bytestream2_init(&gb, buf, size); | |
927 | ✗ | ret = exif_parse_ifd_list(logctx, &gb, le, depth, &ifd); | |
928 | ✗ | if (ret < 0) | |
929 | ✗ | return ret; | |
930 | ✗ | ret = av_exif_ifd_to_dict(logctx, &ifd, metadata); | |
931 | ✗ | av_exif_free(&ifd); | |
932 | ✗ | return ret; | |
933 | } | ||
934 | #endif | ||
935 | |||
936 | #define EXIF_COPY(fname, srcname) do { \ | ||
937 | size_t sz; \ | ||
938 | if (av_size_mult(src->count, sizeof(*(fname)), &sz) < 0) { \ | ||
939 | ret = AVERROR(ENOMEM); \ | ||
940 | goto end; \ | ||
941 | } \ | ||
942 | (fname) = av_memdup((srcname), sz); \ | ||
943 | if (!(fname)) { \ | ||
944 | ret = AVERROR(ENOMEM); \ | ||
945 | goto end; \ | ||
946 | } \ | ||
947 | } while (0) | ||
948 | |||
949 | 9 | static int exif_clone_entry(AVExifEntry *dst, const AVExifEntry *src) | |
950 | { | ||
951 | 9 | int ret = 0; | |
952 | |||
953 | 9 | dst->count = src->count; | |
954 | 9 | dst->id = src->id; | |
955 | 9 | dst->type = src->type; | |
956 | |||
957 | 9 | dst->ifd_offset = src->ifd_offset; | |
958 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (src->ifd_lead) { |
959 | ✗ | dst->ifd_lead = av_memdup(src->ifd_lead, src->ifd_offset); | |
960 | ✗ | if (!dst->ifd_lead) { | |
961 | ✗ | ret = AVERROR(ENOMEM); | |
962 | ✗ | goto end; | |
963 | } | ||
964 | } else { | ||
965 | 9 | dst->ifd_lead = NULL; | |
966 | } | ||
967 | |||
968 |
2/9✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
9 | switch(src->type) { |
969 | 1 | case AV_TIFF_IFD: { | |
970 | 1 | AVExifMetadata *cloned = av_exif_clone_ifd(&src->value.ifd); | |
971 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!cloned) { |
972 | ✗ | ret = AVERROR(ENOMEM); | |
973 | ✗ | goto end; | |
974 | } | ||
975 | 1 | dst->value.ifd = *cloned; | |
976 | 1 | av_freep(&cloned); | |
977 | 1 | break; | |
978 | } | ||
979 | 8 | case AV_TIFF_SHORT: | |
980 | case AV_TIFF_LONG: | ||
981 |
2/4✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
|
8 | EXIF_COPY(dst->value.uint, src->value.uint); |
982 | 8 | break; | |
983 | ✗ | case AV_TIFF_SLONG: | |
984 | case AV_TIFF_SSHORT: | ||
985 | ✗ | EXIF_COPY(dst->value.sint, src->value.sint); | |
986 | ✗ | break; | |
987 | ✗ | case AV_TIFF_RATIONAL: | |
988 | case AV_TIFF_SRATIONAL: | ||
989 | ✗ | EXIF_COPY(dst->value.rat, src->value.rat); | |
990 | ✗ | break; | |
991 | ✗ | case AV_TIFF_DOUBLE: | |
992 | case AV_TIFF_FLOAT: | ||
993 | ✗ | EXIF_COPY(dst->value.dbl, src->value.dbl); | |
994 | ✗ | break; | |
995 | ✗ | case AV_TIFF_BYTE: | |
996 | case AV_TIFF_UNDEFINED: | ||
997 | ✗ | EXIF_COPY(dst->value.ubytes, src->value.ubytes); | |
998 | ✗ | break; | |
999 | ✗ | case AV_TIFF_SBYTE: | |
1000 | ✗ | EXIF_COPY(dst->value.sbytes, src->value.sbytes); | |
1001 | ✗ | break; | |
1002 | ✗ | case AV_TIFF_STRING: | |
1003 | ✗ | dst->value.str = av_memdup(src->value.str, src->count+1); | |
1004 | ✗ | if (!dst->value.str) { | |
1005 | ✗ | ret = AVERROR(ENOMEM); | |
1006 | ✗ | goto end; | |
1007 | } | ||
1008 | ✗ | break; | |
1009 | } | ||
1010 | |||
1011 | 9 | return 0; | |
1012 | |||
1013 | ✗ | end: | |
1014 | ✗ | av_freep(&dst->ifd_lead); | |
1015 | ✗ | if (src->type == AV_TIFF_IFD) | |
1016 | ✗ | av_exif_free(&dst->value.ifd); | |
1017 | else | ||
1018 | ✗ | av_freep(&dst->value.ptr); | |
1019 | ✗ | memset(dst, 0, sizeof(*dst)); | |
1020 | |||
1021 | ✗ | return ret; | |
1022 | } | ||
1023 | |||
1024 | 12 | static int exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int depth, AVExifEntry **value) | |
1025 | { | ||
1026 | 12 | int offset = 1; | |
1027 | |||
1028 |
5/8✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 12 times.
|
12 | if (!ifd || ifd->count && !ifd->entries || !value) |
1029 | ✗ | return AVERROR(EINVAL); | |
1030 | |||
1031 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 11 times.
|
52 | for (size_t i = 0; i < ifd->count; i++) { |
1032 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 40 times.
|
41 | if (ifd->entries[i].id == id) { |
1033 | 1 | *value = &ifd->entries[i]; | |
1034 | 1 | return i + offset; | |
1035 | } | ||
1036 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 37 times.
|
40 | if (ifd->entries[i].type == AV_TIFF_IFD) { |
1037 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (depth < 3) { |
1038 | ✗ | int ret = exif_get_entry(logctx, &ifd->entries[i].value.ifd, id, depth + 1, value); | |
1039 | ✗ | if (ret) | |
1040 | ✗ | return ret < 0 ? ret : ret + offset; | |
1041 | } | ||
1042 | 3 | offset += ifd->entries[i].value.ifd.count; | |
1043 | } | ||
1044 | } | ||
1045 | |||
1046 | 11 | return 0; | |
1047 | } | ||
1048 | |||
1049 | 12 | int av_exif_get_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags, AVExifEntry **value) | |
1050 | { | ||
1051 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | return exif_get_entry(logctx, ifd, id, (flags & AV_EXIF_FLAG_RECURSIVE) ? 0 : INT_MAX, value); |
1052 | } | ||
1053 | |||
1054 | 9 | int av_exif_set_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, enum AVTiffDataType type, | |
1055 | uint32_t count, const uint8_t *ifd_lead, uint32_t ifd_offset, const void *value) | ||
1056 | { | ||
1057 | void *temp; | ||
1058 | 9 | int ret = 0; | |
1059 | 9 | AVExifEntry *entry = NULL; | |
1060 | 9 | AVExifEntry src = { 0 }; | |
1061 | |||
1062 |
4/6✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
|
9 | if (!ifd || ifd->count && !ifd->entries |
1063 |
3/8✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
|
9 | || ifd_lead && !ifd_offset || !ifd_lead && ifd_offset |
1064 |
2/4✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
|
9 | || !value || ifd->count == 0xFFFFu) |
1065 | ✗ | return AVERROR(EINVAL); | |
1066 | |||
1067 | 9 | ret = av_exif_get_entry(logctx, ifd, id, 0, &entry); | |
1068 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ret < 0) |
1069 | ✗ | return ret; | |
1070 | |||
1071 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (entry) { |
1072 | ✗ | exif_free_entry(entry); | |
1073 | } else { | ||
1074 | size_t required_size; | ||
1075 | 9 | ret = av_size_mult(ifd->count + 1, sizeof(*ifd->entries), &required_size); | |
1076 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ret < 0) |
1077 | ✗ | return AVERROR(ENOMEM); | |
1078 | 9 | temp = av_fast_realloc(ifd->entries, &ifd->size, required_size); | |
1079 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (!temp) |
1080 | ✗ | return AVERROR(ENOMEM); | |
1081 | 9 | ifd->entries = temp; | |
1082 | 9 | entry = &ifd->entries[ifd->count++]; | |
1083 | } | ||
1084 | |||
1085 | 9 | src.count = count; | |
1086 | 9 | src.id = id; | |
1087 | 9 | src.type = type; | |
1088 | 9 | src.ifd_lead = (uint8_t *) ifd_lead; | |
1089 | 9 | src.ifd_offset = ifd_offset; | |
1090 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (type == AV_TIFF_IFD) |
1091 | 1 | src.value.ifd = * (const AVExifMetadata *) value; | |
1092 | else | ||
1093 | 8 | src.value.ptr = (void *) value; | |
1094 | |||
1095 | 9 | ret = exif_clone_entry(entry, &src); | |
1096 | |||
1097 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (ret < 0) |
1098 | ✗ | ifd->count--; | |
1099 | |||
1100 | 9 | return ret; | |
1101 | } | ||
1102 | |||
1103 | 2 | static int exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int depth) | |
1104 | { | ||
1105 | 2 | int32_t index = -1; | |
1106 | 2 | int ret = 0; | |
1107 | |||
1108 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
|
2 | if (!ifd || ifd->count && !ifd->entries) |
1109 | ✗ | return AVERROR(EINVAL); | |
1110 | |||
1111 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | for (size_t i = 0; i < ifd->count; i++) { |
1112 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
|
10 | if (ifd->entries[i].id == id) { |
1113 | 2 | index = i; | |
1114 | 2 | break; | |
1115 | } | ||
1116 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
8 | if (ifd->entries[i].type == AV_TIFF_IFD && depth < 3) { |
1117 | ✗ | ret = exif_remove_entry(logctx, &ifd->entries[i].value.ifd, id, depth + 1); | |
1118 | ✗ | if (ret) | |
1119 | ✗ | return ret; | |
1120 | } | ||
1121 | } | ||
1122 | |||
1123 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (index < 0) |
1124 | ✗ | return 0; | |
1125 | 2 | exif_free_entry(&ifd->entries[index]); | |
1126 | |||
1127 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (index == --ifd->count) { |
1128 | ✗ | if (!index) | |
1129 | ✗ | av_freep(&ifd->entries); | |
1130 | ✗ | return 1; | |
1131 | } | ||
1132 | |||
1133 | 2 | memmove(&ifd->entries[index], &ifd->entries[index + 1], (ifd->count - index) * sizeof(*ifd->entries)); | |
1134 | |||
1135 | 2 | return 1 + (ifd->count - index); | |
1136 | } | ||
1137 | |||
1138 | 2 | int av_exif_remove_entry(void *logctx, AVExifMetadata *ifd, uint16_t id, int flags) | |
1139 | { | ||
1140 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | return exif_remove_entry(logctx, ifd, id, (flags & AV_EXIF_FLAG_RECURSIVE) ? 0 : INT_MAX); |
1141 | } | ||
1142 | |||
1143 | 1 | AVExifMetadata *av_exif_clone_ifd(const AVExifMetadata *ifd) | |
1144 | { | ||
1145 | 1 | AVExifMetadata *ret = av_mallocz(sizeof(*ret)); | |
1146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ret) |
1147 | ✗ | return NULL; | |
1148 | |||
1149 | 1 | ret->count = ifd->count; | |
1150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret->count) { |
1151 | size_t required_size; | ||
1152 | ✗ | if (av_size_mult(ret->count, sizeof(*ret->entries), &required_size) < 0) | |
1153 | ✗ | goto fail; | |
1154 | ✗ | ret->entries = av_fast_realloc(NULL, &ret->size, required_size); | |
1155 | ✗ | if (!ret->entries) | |
1156 | ✗ | goto fail; | |
1157 | } | ||
1158 | |||
1159 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | for (size_t i = 0; i < ret->count; i++) { |
1160 | ✗ | const AVExifEntry *entry = &ifd->entries[i]; | |
1161 | ✗ | AVExifEntry *ret_entry = &ret->entries[i]; | |
1162 | ✗ | int status = exif_clone_entry(ret_entry, entry); | |
1163 | ✗ | if (status < 0) | |
1164 | ✗ | goto fail; | |
1165 | } | ||
1166 | |||
1167 | 1 | return ret; | |
1168 | |||
1169 | ✗ | fail: | |
1170 | ✗ | av_exif_free(ret); | |
1171 | ✗ | av_free(ret); | |
1172 | ✗ | return NULL; | |
1173 | } | ||
1174 | |||
1175 | static const int rotation_lut[2][4] = { | ||
1176 | {1, 8, 3, 6}, {4, 7, 2, 5}, | ||
1177 | }; | ||
1178 | |||
1179 | ✗ | int av_exif_matrix_to_orientation(const int32_t *matrix) | |
1180 | { | ||
1181 | ✗ | double rotation = av_display_rotation_get(matrix); | |
1182 | // determinant | ||
1183 | ✗ | int vflip = ((int64_t)matrix[0] * (int64_t)matrix[4] | |
1184 | ✗ | - (int64_t)matrix[1] * (int64_t)matrix[3]) < 0; | |
1185 | ✗ | if (!isfinite(rotation)) | |
1186 | ✗ | return 0; | |
1187 | ✗ | int rot = (int)(rotation + 0.5); | |
1188 | ✗ | rot = (((rot % 360) + 360) % 360) / 90; | |
1189 | ✗ | return rotation_lut[vflip][rot]; | |
1190 | } | ||
1191 | |||
1192 | ✗ | int av_exif_orientation_to_matrix(int32_t *matrix, int orientation) | |
1193 | { | ||
1194 | ✗ | switch (orientation) { | |
1195 | ✗ | case 1: | |
1196 | ✗ | av_display_rotation_set(matrix, 0.0); | |
1197 | ✗ | break; | |
1198 | ✗ | case 2: | |
1199 | ✗ | av_display_rotation_set(matrix, 0.0); | |
1200 | ✗ | av_display_matrix_flip(matrix, 1, 0); | |
1201 | ✗ | break; | |
1202 | ✗ | case 3: | |
1203 | ✗ | av_display_rotation_set(matrix, 180.0); | |
1204 | ✗ | break; | |
1205 | ✗ | case 4: | |
1206 | ✗ | av_display_rotation_set(matrix, 180.0); | |
1207 | ✗ | av_display_matrix_flip(matrix, 1, 0); | |
1208 | ✗ | break; | |
1209 | ✗ | case 5: | |
1210 | ✗ | av_display_rotation_set(matrix, 90.0); | |
1211 | ✗ | av_display_matrix_flip(matrix, 1, 0); | |
1212 | ✗ | break; | |
1213 | ✗ | case 6: | |
1214 | ✗ | av_display_rotation_set(matrix, 90.0); | |
1215 | ✗ | break; | |
1216 | ✗ | case 7: | |
1217 | ✗ | av_display_rotation_set(matrix, -90.0); | |
1218 | ✗ | av_display_matrix_flip(matrix, 1, 0); | |
1219 | ✗ | break; | |
1220 | ✗ | case 8: | |
1221 | ✗ | av_display_rotation_set(matrix, -90.0); | |
1222 | ✗ | break; | |
1223 | ✗ | default: | |
1224 | ✗ | return AVERROR(EINVAL); | |
1225 | } | ||
1226 | |||
1227 | ✗ | return 0; | |
1228 | } | ||
1229 | |||
1230 | 2 | int ff_exif_sanitize_ifd(void *logctx, const AVFrame *frame, AVExifMetadata *ifd) | |
1231 | { | ||
1232 | 2 | int ret = 0; | |
1233 | 2 | AVFrameSideData *sd_orient = NULL; | |
1234 | 2 | AVExifEntry *or = NULL; | |
1235 | 2 | AVExifEntry *iw = NULL; | |
1236 | 2 | AVExifEntry *ih = NULL; | |
1237 | 2 | AVExifEntry *pw = NULL; | |
1238 | 2 | AVExifEntry *ph = NULL; | |
1239 | 2 | uint64_t orientation = 1; | |
1240 | 2 | uint64_t w = frame->width; | |
1241 | 2 | uint64_t h = frame->height; | |
1242 | 2 | int rewrite = 0; | |
1243 | |||
1244 | 2 | sd_orient = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX); | |
1245 | |||
1246 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (sd_orient) |
1247 | ✗ | orientation = av_exif_matrix_to_orientation((int32_t *) sd_orient->data); | |
1248 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (orientation != 1) |
1249 | ✗ | av_log(logctx, AV_LOG_DEBUG, "matrix contains nontrivial EXIF orientation: %" PRIu64 "\n", orientation); | |
1250 | |||
1251 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | for (size_t i = 0; i < ifd->count; i++) { |
1252 | 5 | AVExifEntry *entry = &ifd->entries[i]; | |
1253 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
5 | if (entry->id == ORIENTATION_TAG && entry->count > 0 && entry->type == AV_TIFF_SHORT) { |
1254 | ✗ | or = entry; | |
1255 | ✗ | continue; | |
1256 | } | ||
1257 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
5 | if (entry->id == IMAGE_WIDTH_TAG && entry->count > 0 && entry->type == AV_TIFF_LONG) { |
1258 | ✗ | iw = entry; | |
1259 | ✗ | continue; | |
1260 | } | ||
1261 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
5 | if (entry->id == IMAGE_LENGTH_TAG && entry->count > 0 && entry->type == AV_TIFF_LONG) { |
1262 | ✗ | ih = entry; | |
1263 | ✗ | continue; | |
1264 | } | ||
1265 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
5 | if (entry->id == EXIFIFD_TAG && entry->type == AV_TIFF_IFD) { |
1266 | 1 | AVExifMetadata *exif = &entry->value.ifd; | |
1267 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (size_t j = 0; j < exif->count; j++) { |
1268 | 1 | AVExifEntry *exifentry = &exif->entries[j]; | |
1269 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (exifentry->id == PIXEL_X_TAG && exifentry->count > 0 && exifentry->type == AV_TIFF_SHORT) { |
1270 | ✗ | pw = exifentry; | |
1271 | ✗ | continue; | |
1272 | } | ||
1273 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
1 | if (exifentry->id == PIXEL_Y_TAG && exifentry->count > 0 && exifentry->type == AV_TIFF_SHORT) { |
1274 | ✗ | ph = exifentry; | |
1275 | ✗ | continue; | |
1276 | } | ||
1277 | } | ||
1278 | } | ||
1279 | } | ||
1280 | |||
1281 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (or && or->value.uint[0] != orientation) { |
1282 | ✗ | rewrite = 1; | |
1283 | ✗ | or->value.uint[0] = orientation; | |
1284 | } | ||
1285 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (iw && iw->value.uint[0] != w) { |
1286 | ✗ | rewrite = 1; | |
1287 | ✗ | iw->value.uint[0] = w; | |
1288 | } | ||
1289 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (ih && ih->value.uint[0] != h) { |
1290 | ✗ | rewrite = 1; | |
1291 | ✗ | ih->value.uint[0] = h; | |
1292 | } | ||
1293 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (pw && pw->value.uint[0] != w) { |
1294 | ✗ | rewrite = 1; | |
1295 | ✗ | pw->value.uint[0] = w; | |
1296 | } | ||
1297 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (ph && ph->value.uint[0] != h) { |
1298 | ✗ | rewrite = 1; | |
1299 | ✗ | ph->value.uint[0] = h; | |
1300 | } | ||
1301 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if (!or && orientation != 1) { |
1302 | ✗ | rewrite = 1; | |
1303 | ✗ | ret = av_exif_set_entry(logctx, ifd, ORIENTATION_TAG, AV_TIFF_SHORT, 1, NULL, 0, &orientation); | |
1304 | ✗ | if (ret < 0) | |
1305 | ✗ | goto end; | |
1306 | } | ||
1307 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (!iw && w) { |
1308 | 2 | rewrite = 1; | |
1309 | 2 | ret = av_exif_set_entry(logctx, ifd, IMAGE_WIDTH_TAG, AV_TIFF_LONG, 1, NULL, 0, &w); | |
1310 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1311 | ✗ | goto end; | |
1312 | } | ||
1313 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if (!ih && h) { |
1314 | 2 | rewrite = 1; | |
1315 | 2 | ret = av_exif_set_entry(logctx, ifd, IMAGE_LENGTH_TAG, AV_TIFF_LONG, 1, NULL, 0, &h); | |
1316 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1317 | ✗ | goto end; | |
1318 | } | ||
1319 |
3/12✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
2 | if (!pw && w && w < 0xFFFFu || !ph && h && h < 0xFFFFu) { |
1320 | AVExifMetadata *exif; | ||
1321 | AVExifEntry *exif_entry; | ||
1322 | 2 | int exif_found = av_exif_get_entry(logctx, ifd, EXIFIFD_TAG, 0, &exif_entry); | |
1323 | 2 | rewrite = 1; | |
1324 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (exif_found < 0) |
1325 | ✗ | goto end; | |
1326 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (exif_found > 0) { |
1327 | 1 | exif = &exif_entry->value.ifd; | |
1328 | } else { | ||
1329 | 1 | AVExifMetadata exif_new = { 0 }; | |
1330 | 1 | ret = av_exif_set_entry(logctx, ifd, EXIFIFD_TAG, AV_TIFF_IFD, 1, NULL, 0, &exif_new); | |
1331 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
1332 | ✗ | av_exif_free(&exif_new); | |
1333 | ✗ | goto end; | |
1334 | } | ||
1335 | 1 | exif = &ifd->entries[ifd->count - 1].value.ifd; | |
1336 | } | ||
1337 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | if (!pw && w && w < 0xFFFFu) { |
1338 | 2 | ret = av_exif_set_entry(logctx, exif, PIXEL_X_TAG, AV_TIFF_SHORT, 1, NULL, 0, &w); | |
1339 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1340 | ✗ | goto end; | |
1341 | } | ||
1342 |
3/6✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | if (!ph && h && h < 0xFFFFu) { |
1343 | 2 | ret = av_exif_set_entry(logctx, exif, PIXEL_Y_TAG, AV_TIFF_SHORT, 1, NULL, 0, &h); | |
1344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1345 | ✗ | goto end; | |
1346 | } | ||
1347 | } | ||
1348 | |||
1349 | 2 | return rewrite; | |
1350 | |||
1351 | ✗ | end: | |
1352 | ✗ | return ret; | |
1353 | } | ||
1354 | |||
1355 | 248 | int ff_exif_get_buffer(void *logctx, const AVFrame *frame, AVBufferRef **buffer_ptr, enum AVExifHeaderMode header_mode) | |
1356 | { | ||
1357 | 248 | AVFrameSideData *sd_exif = NULL; | |
1358 | 248 | AVBufferRef *buffer = NULL; | |
1359 | 248 | AVExifMetadata ifd = { 0 }; | |
1360 | 248 | int ret = 0; | |
1361 | 248 | int rewrite = 0; | |
1362 | |||
1363 |
2/4✓ Branch 0 taken 248 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 248 times.
|
248 | if (!buffer_ptr || *buffer_ptr) |
1364 | ✗ | return AVERROR(EINVAL); | |
1365 | |||
1366 | 248 | sd_exif = av_frame_get_side_data(frame, AV_FRAME_DATA_EXIF); | |
1367 |
2/2✓ Branch 0 taken 246 times.
✓ Branch 1 taken 2 times.
|
248 | if (!sd_exif) |
1368 | 246 | return 0; | |
1369 | |||
1370 | 2 | ret = av_exif_parse_buffer(logctx, sd_exif->data, sd_exif->size, &ifd, AV_EXIF_TIFF_HEADER); | |
1371 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1372 | ✗ | goto end; | |
1373 | |||
1374 | 2 | rewrite = ff_exif_sanitize_ifd(logctx, frame, &ifd); | |
1375 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (rewrite < 0) { |
1376 | ✗ | ret = rewrite; | |
1377 | ✗ | goto end; | |
1378 | } | ||
1379 | |||
1380 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (rewrite) { |
1381 | 2 | ret = av_exif_write(logctx, &ifd, &buffer, header_mode); | |
1382 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ret < 0) |
1383 | ✗ | goto end; | |
1384 | |||
1385 | 2 | *buffer_ptr = buffer; | |
1386 | } else { | ||
1387 | ✗ | *buffer_ptr = av_buffer_ref(sd_exif->buf); | |
1388 | ✗ | if (!*buffer_ptr) { | |
1389 | ✗ | ret = AVERROR(ENOMEM); | |
1390 | ✗ | goto end; | |
1391 | } | ||
1392 | } | ||
1393 | |||
1394 | 2 | av_exif_free(&ifd); | |
1395 | 2 | return rewrite; | |
1396 | |||
1397 | ✗ | end: | |
1398 | ✗ | av_exif_free(&ifd); | |
1399 | ✗ | return ret; | |
1400 | } | ||
1401 |