2 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
4 * gstexiftag.c: library for reading / modifying exif tags
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: tag mappings and support functions for plugins
25 * dealing with exif tags
26 * @see_also: #GstTagList
28 * Contains utility function to parse #GstTagList<!-- -->s from exif
29 * buffers and to create exif buffers from #GstTagList<!-- -->s
31 * Note that next IFD fields on the created exif buffers are set to 0.
37 #include <gst/gsttagsetter.h>
38 #include <gst/base/gstbytewriter.h>
39 #include "gsttageditingprivate.h"
44 #include <gst/math-compat.h>
46 /* Some useful constants */
47 #define TIFF_LITTLE_ENDIAN 0x4949
48 #define TIFF_BIG_ENDIAN 0x4D4D
49 #define TIFF_HEADER_SIZE 8
50 #define EXIF_TAG_ENTRY_SIZE (2 + 2 + 4 + 4)
53 #define EXIF_TYPE_BYTE 1
54 #define EXIF_TYPE_ASCII 2
55 #define EXIF_TYPE_SHORT 3
56 #define EXIF_TYPE_LONG 4
57 #define EXIF_TYPE_RATIONAL 5
58 #define EXIF_TYPE_UNDEFINED 7
59 #define EXIF_TYPE_SLONG 9
60 #define EXIF_TYPE_SRATIONAL 10
62 typedef struct _GstExifTagMatch GstExifTagMatch;
63 typedef struct _GstExifWriter GstExifWriter;
64 typedef struct _GstExifReader GstExifReader;
65 typedef struct _GstExifTagData GstExifTagData;
67 typedef void (*GstExifSerializationFunc) (GstExifWriter * writer,
68 const GstTagList * taglist, const GstExifTagMatch * exiftag);
71 * Function used to deserialize tags that don't follow the usual
72 * deserialization conversions. Usually those that have 'Ref' complementary
75 * Those functions receive a exif tag data in the parameters, plus the taglist
76 * and the reader and buffer if they need to get more information to build
77 * its tags. There are lots of parameters, but this is needed to make it
78 * versatile. Explanation of them follows:
80 * exif_reader: The #GstExifReader with the reading parameter and taglist for
82 * reader: The #GstByteReader pointing to the start of the next tag entry in
83 * the ifd, useful for tags that use other complementary tags.
85 * exiftag: The #GstExifTagMatch that contains this tag info
86 * tagdata: values from the already parsed tag
88 typedef gint (*GstExifDeserializationFunc) (GstExifReader * exif_reader,
89 GstByteReader * reader, const GstExifTagMatch * exiftag,
90 GstExifTagData * tagdata);
92 #define EXIF_SERIALIZATION_FUNC(name) \
93 static void serialize_ ## name (GstExifWriter * writer, \
94 const GstTagList * taglist, const GstExifTagMatch * exiftag)
96 #define EXIF_DESERIALIZATION_FUNC(name) \
97 static gint deserialize_ ## name (GstExifReader * exif_reader, \
98 GstByteReader * reader, const GstExifTagMatch * exiftag, \
99 GstExifTagData * tagdata)
101 #define EXIF_SERIALIZATION_DESERIALIZATION_FUNC(name) \
102 EXIF_SERIALIZATION_FUNC (name); \
103 EXIF_DESERIALIZATION_FUNC (name)
106 * A common case among serialization/deserialization routines is that
107 * the gstreamer tag is a string (with a predefined set of allowed values)
108 * and exif is an int. These macros cover these cases
110 #define EXIF_SERIALIZATION_MAP_STRING_TO_INT_FUNC(name,funcname) \
112 serialize_ ## name (GstExifWriter * writer, const GstTagList * taglist, \
113 const GstExifTagMatch * exiftag) \
118 if (!gst_tag_list_get_string_index (taglist, exiftag->gst_tag, 0, &str)) { \
119 GST_WARNING ("No %s tag present in taglist", exiftag->gst_tag); \
123 exif_value = __exif_tag_ ## funcname ## _to_exif_value (str); \
124 if (exif_value == -1) { \
130 switch (exiftag->exif_type) { \
131 case EXIF_TYPE_SHORT: \
132 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, exif_value); \
134 case EXIF_TYPE_LONG: \
135 gst_exif_writer_write_long_tag (writer, exiftag->exif_tag, exif_value); \
137 case EXIF_TYPE_UNDEFINED: \
139 guint8 data = (guint8) exif_value; \
140 write_exif_undefined_tag (writer, exiftag->exif_tag, &data, 1); \
144 g_assert_not_reached (); \
145 GST_WARNING ("Unmapped serialization for type %d", exiftag->exif_type); \
150 #define EXIF_DESERIALIZATION_MAP_STRING_TO_INT_FUNC(name,funcname) \
152 deserialize_ ## name (GstExifReader * exif_reader, \
153 GstByteReader * reader, const GstExifTagMatch * exiftag, \
154 GstExifTagData * tagdata) \
156 const gchar *str = NULL; \
159 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag, \
160 exiftag->exif_tag); \
163 if (tagdata->count != 1) { \
164 GST_WARNING ("0x%X has unexpected count", tagdata->count); \
168 if (tagdata->tag_type == EXIF_TYPE_SHORT) { \
169 if (exif_reader->byte_order == G_LITTLE_ENDIAN) { \
170 value = GST_READ_UINT16_LE (tagdata->offset_as_data); \
172 value = GST_READ_UINT16_BE (tagdata->offset_as_data); \
174 } else if (tagdata->tag_type == EXIF_TYPE_UNDEFINED) { \
175 value = GST_READ_UINT8 (tagdata->offset_as_data); \
177 GST_WARNING ("0x%X has unexpected type %d", exiftag->exif_tag, \
178 tagdata->tag_type); \
182 str = __exif_tag_## funcname ## _from_exif_value (value); \
184 GST_WARNING ("Invalid value for tag 0x%X: %d", tagdata->tag, value); \
187 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE, \
188 exiftag->gst_tag, str, NULL); \
193 #define EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC(name,funcname) \
194 EXIF_SERIALIZATION_MAP_STRING_TO_INT_FUNC(name,funcname); \
195 EXIF_DESERIALIZATION_MAP_STRING_TO_INT_FUNC(name,funcname);
197 struct _GstExifTagMatch
199 const gchar *gst_tag;
203 /* for tags that need special handling */
204 guint16 complementary_tag;
205 GstExifSerializationFunc serialize;
206 GstExifDeserializationFunc deserialize;
209 struct _GstExifTagData
215 const guint8 *offset_as_data;
219 * Holds the info and variables necessary to write
220 * the exif tags properly
222 struct _GstExifWriter
224 GstByteWriter tagwriter;
225 GstByteWriter datawriter;
231 struct _GstExifReader
234 const GstBuffer *buffer;
238 /* tags waiting for their complementary tags */
239 GSList *pending_tags;
242 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (aperture_value);
243 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (contrast);
244 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (exposure_program);
245 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (exposure_mode);
246 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (flash);
247 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (gain_control);
248 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (geo_coordinate);
249 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (geo_direction);
250 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (geo_elevation);
251 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (metering_mode);
252 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (orientation);
253 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (saturation);
254 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (scene_capture_type);
255 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (scene_type);
256 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (sensitivity_type);
257 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (sharpness);
258 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (shutter_speed);
259 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (source);
260 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (speed);
261 EXIF_SERIALIZATION_DESERIALIZATION_FUNC (white_balance);
263 EXIF_DESERIALIZATION_FUNC (resolution);
264 EXIF_DESERIALIZATION_FUNC (add_to_pending_tags);
266 /* FIXME copyright tag has a weird "artist\0editor\0" format that is
269 /* exif tag numbers */
270 #define EXIF_TAG_GPS_LATITUDE_REF 0x1
271 #define EXIF_TAG_GPS_LATITUDE 0x2
272 #define EXIF_TAG_GPS_LONGITUDE_REF 0x3
273 #define EXIF_TAG_GPS_LONGITUDE 0x4
274 #define EXIF_TAG_GPS_ALTITUDE_REF 0x5
275 #define EXIF_TAG_GPS_ALTITUDE 0x6
276 #define EXIF_TAG_GPS_SPEED_REF 0xC
277 #define EXIF_TAG_GPS_SPEED 0xD
278 #define EXIF_TAG_GPS_TRACK_REF 0xE
279 #define EXIF_TAG_GPS_TRACK 0xF
280 #define EXIF_TAG_GPS_IMAGE_DIRECTION_REF 0x10
281 #define EXIF_TAG_GPS_IMAGE_DIRECTION 0x11
282 #define EXIF_TAG_GPS_HORIZONTAL_POSITIONING_ERROR 0x1F
283 #define EXIF_TAG_IMAGE_DESCRIPTION 0x10E
284 #define EXIF_TAG_MAKE 0x10F
285 #define EXIF_TAG_MODEL 0x110
286 #define EXIF_TAG_ORIENTATION 0x112
287 #define EXIF_TAG_XRESOLUTION 0x11A
288 #define EXIF_TAG_YRESOLUTION 0x11B
289 #define EXIF_TAG_RESOLUTION_UNIT 0x128
290 #define EXIF_TAG_SOFTWARE 0x131
291 #define EXIF_TAG_DATE_TIME 0x132
292 #define EXIF_TAG_ARTIST 0x13B
293 #define EXIF_TAG_COPYRIGHT 0x8298
294 #define EXIF_TAG_EXPOSURE_TIME 0x829A
295 #define EXIF_TAG_F_NUMBER 0x829D
296 #define EXIF_TAG_EXPOSURE_PROGRAM 0x8822
297 #define EXIF_TAG_PHOTOGRAPHIC_SENSITIVITY 0x8827
298 #define EXIF_TAG_SENSITIVITY_TYPE 0x8830
299 #define EXIF_TAG_ISO_SPEED 0x8833
300 #define EXIF_TAG_DATE_TIME_ORIGINAL 0x9003
301 #define EXIF_TAG_DATE_TIME_DIGITIZED 0x9004
302 #define EXIF_TAG_SHUTTER_SPEED_VALUE 0x9201
303 #define EXIF_TAG_APERTURE_VALUE 0x9202
304 #define EXIF_TAG_EXPOSURE_BIAS 0x9204
305 #define EXIF_TAG_METERING_MODE 0x9207
306 #define EXIF_TAG_FLASH 0x9209
307 #define EXIF_TAG_FOCAL_LENGTH 0x920A
308 #define EXIF_TAG_MAKER_NOTE 0x927C
309 #define EXIF_TAG_FILE_SOURCE 0xA300
310 #define EXIF_TAG_SCENE_TYPE 0xA301
311 #define EXIF_TAG_EXPOSURE_MODE 0xA402
312 #define EXIF_TAG_WHITE_BALANCE 0xA403
313 #define EXIF_TAG_DIGITAL_ZOOM_RATIO 0xA404
314 #define EXIF_TAG_SCENE_CAPTURE_TYPE 0xA406
315 #define EXIF_TAG_GAIN_CONTROL 0xA407
316 #define EXIF_TAG_CONTRAST 0xA408
317 #define EXIF_TAG_SATURATION 0xA409
318 #define EXIF_TAG_SHARPNESS 0xA40A
320 /* IFD pointer tags */
321 #define EXIF_IFD_TAG 0x8769
322 #define EXIF_GPS_IFD_TAG 0x8825
325 #define EXIF_VERSION_TAG 0x9000
326 #define EXIF_FLASHPIX_VERSION_TAG 0xA000
328 /* useful macros for speed tag */
329 #define METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR (3.6)
330 #define KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND (1/3.6)
331 #define MILES_PER_HOUR_TO_METERS_PER_SECOND (0.44704)
332 #define KNOTS_TO_METERS_PER_SECOND (0.514444)
335 * Should be kept in ascending id order
337 * {gst-tag, exif-tag, exig-type, complementary-exif-tag, serialization-func,
338 * deserialization-func}
340 static const GstExifTagMatch tag_map_ifd0[] = {
341 {GST_TAG_IMAGE_HORIZONTAL_PPI, EXIF_TAG_XRESOLUTION, EXIF_TYPE_RATIONAL,
342 0, NULL, deserialize_add_to_pending_tags},
343 {GST_TAG_IMAGE_VERTICAL_PPI, EXIF_TAG_YRESOLUTION, EXIF_TYPE_RATIONAL,
344 0, NULL, deserialize_add_to_pending_tags},
345 {NULL, EXIF_TAG_RESOLUTION_UNIT, EXIF_TYPE_SHORT, 0, NULL,
346 deserialize_resolution},
347 {GST_TAG_DESCRIPTION, EXIF_TAG_IMAGE_DESCRIPTION, EXIF_TYPE_ASCII, 0, NULL,
349 {GST_TAG_DEVICE_MANUFACTURER, EXIF_TAG_MAKE, EXIF_TYPE_ASCII, 0, NULL, NULL},
350 {GST_TAG_DEVICE_MODEL, EXIF_TAG_MODEL, EXIF_TYPE_ASCII, 0, NULL, NULL},
351 {GST_TAG_IMAGE_ORIENTATION, EXIF_TAG_ORIENTATION, EXIF_TYPE_SHORT, 0,
352 serialize_orientation,
353 deserialize_orientation},
354 {GST_TAG_APPLICATION_NAME, EXIF_TAG_SOFTWARE, EXIF_TYPE_ASCII, 0, NULL, NULL},
355 {GST_TAG_DATE_TIME, EXIF_TAG_DATE_TIME, EXIF_TYPE_ASCII, 0, NULL, NULL},
356 {GST_TAG_ARTIST, EXIF_TAG_ARTIST, EXIF_TYPE_ASCII, 0, NULL, NULL},
357 {GST_TAG_COPYRIGHT, EXIF_TAG_COPYRIGHT, EXIF_TYPE_ASCII, 0, NULL, NULL},
358 {NULL, EXIF_IFD_TAG, EXIF_TYPE_LONG, 0, NULL, NULL},
359 {NULL, EXIF_GPS_IFD_TAG, EXIF_TYPE_LONG, 0, NULL, NULL},
360 {NULL, 0, 0, 0, NULL, NULL}
363 static const GstExifTagMatch tag_map_exif[] = {
364 {GST_TAG_CAPTURING_SHUTTER_SPEED, EXIF_TAG_EXPOSURE_TIME, EXIF_TYPE_RATIONAL,
367 {GST_TAG_CAPTURING_FOCAL_RATIO, EXIF_TAG_F_NUMBER, EXIF_TYPE_RATIONAL, 0,
370 {GST_TAG_CAPTURING_EXPOSURE_PROGRAM, EXIF_TAG_EXPOSURE_PROGRAM,
371 EXIF_TYPE_SHORT, 0, serialize_exposure_program,
372 deserialize_exposure_program},
374 /* don't need the serializer as we always write the iso speed alone */
375 {GST_TAG_CAPTURING_ISO_SPEED, EXIF_TAG_PHOTOGRAPHIC_SENSITIVITY,
376 EXIF_TYPE_SHORT, 0, NULL,
377 deserialize_add_to_pending_tags},
379 {GST_TAG_CAPTURING_ISO_SPEED, EXIF_TAG_SENSITIVITY_TYPE, EXIF_TYPE_SHORT, 0,
380 serialize_sensitivity_type, deserialize_sensitivity_type},
381 {GST_TAG_CAPTURING_ISO_SPEED, EXIF_TAG_ISO_SPEED, EXIF_TYPE_LONG, 0, NULL,
383 {NULL, EXIF_VERSION_TAG, EXIF_TYPE_UNDEFINED, 0, NULL, NULL},
384 {GST_TAG_DATE_TIME, EXIF_TAG_DATE_TIME_ORIGINAL, EXIF_TYPE_ASCII, 0, NULL,
386 {GST_TAG_CAPTURING_SHUTTER_SPEED, EXIF_TAG_SHUTTER_SPEED_VALUE,
387 EXIF_TYPE_SRATIONAL, 0,
388 serialize_shutter_speed, deserialize_shutter_speed},
389 {GST_TAG_CAPTURING_FOCAL_RATIO, EXIF_TAG_APERTURE_VALUE, EXIF_TYPE_RATIONAL,
391 serialize_aperture_value, deserialize_aperture_value},
392 {GST_TAG_CAPTURING_EXPOSURE_COMPENSATION, EXIF_TAG_EXPOSURE_BIAS,
393 EXIF_TYPE_SRATIONAL, 0, NULL, NULL},
394 {GST_TAG_CAPTURING_METERING_MODE, EXIF_TAG_METERING_MODE, EXIF_TYPE_SHORT, 0,
395 serialize_metering_mode, deserialize_metering_mode},
396 {GST_TAG_CAPTURING_FLASH_FIRED, EXIF_TAG_FLASH, EXIF_TYPE_SHORT, 0,
397 serialize_flash, deserialize_flash},
398 {GST_TAG_CAPTURING_FOCAL_LENGTH, EXIF_TAG_FOCAL_LENGTH, EXIF_TYPE_RATIONAL, 0,
400 {GST_TAG_APPLICATION_DATA, EXIF_TAG_MAKER_NOTE, EXIF_TYPE_UNDEFINED, 0, NULL,
402 {NULL, EXIF_FLASHPIX_VERSION_TAG, EXIF_TYPE_UNDEFINED, 0, NULL, NULL},
403 {GST_TAG_CAPTURING_SOURCE, EXIF_TAG_FILE_SOURCE, EXIF_TYPE_UNDEFINED,
404 0, serialize_source, deserialize_source},
405 {GST_TAG_CAPTURING_SOURCE, EXIF_TAG_SCENE_TYPE, EXIF_TYPE_UNDEFINED,
406 0, serialize_scene_type, deserialize_scene_type},
407 {GST_TAG_CAPTURING_EXPOSURE_MODE, EXIF_TAG_EXPOSURE_MODE, EXIF_TYPE_SHORT,
408 0, serialize_exposure_mode, deserialize_exposure_mode},
409 {GST_TAG_CAPTURING_WHITE_BALANCE, EXIF_TAG_WHITE_BALANCE, EXIF_TYPE_SHORT,
410 0, serialize_white_balance, deserialize_white_balance},
411 {GST_TAG_CAPTURING_DIGITAL_ZOOM_RATIO, EXIF_TAG_DIGITAL_ZOOM_RATIO,
412 EXIF_TYPE_RATIONAL, 0, NULL,
414 {GST_TAG_CAPTURING_SCENE_CAPTURE_TYPE, EXIF_TAG_SCENE_CAPTURE_TYPE,
415 EXIF_TYPE_SHORT, 0, serialize_scene_capture_type,
416 deserialize_scene_capture_type},
417 {GST_TAG_CAPTURING_GAIN_ADJUSTMENT, EXIF_TAG_GAIN_CONTROL,
418 EXIF_TYPE_SHORT, 0, serialize_gain_control,
419 deserialize_gain_control},
420 {GST_TAG_CAPTURING_CONTRAST, EXIF_TAG_CONTRAST, EXIF_TYPE_SHORT, 0,
421 serialize_contrast, deserialize_contrast},
422 {GST_TAG_CAPTURING_SATURATION, EXIF_TAG_SATURATION, EXIF_TYPE_SHORT, 0,
423 serialize_saturation, deserialize_saturation},
424 {GST_TAG_CAPTURING_SHARPNESS, EXIF_TAG_SHARPNESS, EXIF_TYPE_SHORT, 0,
425 serialize_sharpness, deserialize_sharpness},
426 {NULL, 0, 0, 0, NULL, NULL}
429 static const GstExifTagMatch tag_map_gps[] = {
430 {GST_TAG_GEO_LOCATION_LATITUDE, EXIF_TAG_GPS_LATITUDE, EXIF_TYPE_RATIONAL,
431 EXIF_TAG_GPS_LATITUDE_REF,
432 serialize_geo_coordinate, deserialize_geo_coordinate},
433 {GST_TAG_GEO_LOCATION_LONGITUDE, EXIF_TAG_GPS_LONGITUDE, EXIF_TYPE_RATIONAL,
434 EXIF_TAG_GPS_LONGITUDE_REF,
435 serialize_geo_coordinate, deserialize_geo_coordinate},
436 {GST_TAG_GEO_LOCATION_ELEVATION, EXIF_TAG_GPS_ALTITUDE, EXIF_TYPE_RATIONAL,
437 EXIF_TAG_GPS_ALTITUDE_REF,
438 serialize_geo_elevation, deserialize_geo_elevation},
439 {GST_TAG_GEO_LOCATION_MOVEMENT_SPEED, EXIF_TAG_GPS_SPEED, EXIF_TYPE_RATIONAL,
440 EXIF_TAG_GPS_SPEED_REF,
441 serialize_speed, deserialize_speed},
442 {GST_TAG_GEO_LOCATION_MOVEMENT_DIRECTION, EXIF_TAG_GPS_TRACK,
443 EXIF_TYPE_RATIONAL, EXIF_TAG_GPS_TRACK_REF,
444 serialize_geo_direction, deserialize_geo_direction},
445 {GST_TAG_GEO_LOCATION_CAPTURE_DIRECTION, EXIF_TAG_GPS_IMAGE_DIRECTION,
446 EXIF_TYPE_RATIONAL, EXIF_TAG_GPS_IMAGE_DIRECTION_REF,
447 serialize_geo_direction, deserialize_geo_direction},
448 {GST_TAG_GEO_LOCATION_HORIZONTAL_ERROR,
449 EXIF_TAG_GPS_HORIZONTAL_POSITIONING_ERROR,
450 EXIF_TYPE_RATIONAL, 0, NULL, NULL},
451 {NULL, 0, 0, 0, NULL, NULL}
454 /* GstExifReader functions */
456 gst_exif_reader_init (GstExifReader * reader, gint byte_order,
457 const GstBuffer * buf, guint32 base_offset)
461 reader->taglist = gst_tag_list_new ();
462 reader->buffer = buf;
463 reader->base_offset = base_offset;
464 reader->byte_order = byte_order;
465 reader->pending_tags = NULL;
466 if (reader->byte_order != G_LITTLE_ENDIAN &&
467 reader->byte_order != G_BIG_ENDIAN) {
468 GST_WARNING ("Unexpected byte order %d, using system default: %d",
469 reader->byte_order, G_BYTE_ORDER);
470 reader->byte_order = G_BYTE_ORDER;
475 gst_exif_reader_add_pending_tag (GstExifReader * reader, GstExifTagData * data)
477 GstExifTagData *copy;
479 copy = g_slice_new (GstExifTagData);
480 memcpy (copy, data, sizeof (GstExifTagData));
482 reader->pending_tags = g_slist_prepend (reader->pending_tags, copy);
485 static GstExifTagData *
486 gst_exif_reader_get_pending_tag (GstExifReader * reader, gint tagid)
490 for (walker = reader->pending_tags; walker; walker = g_slist_next (walker)) {
491 GstExifTagData *data = (GstExifTagData *) walker->data;
492 if (data->tag == tagid)
500 gst_exif_reader_reset (GstExifReader * reader, gboolean return_taglist)
502 GstTagList *ret = NULL;
505 for (walker = reader->pending_tags; walker; walker = g_slist_next (walker)) {
506 GstExifTagData *data = (GstExifTagData *) walker->data;
508 g_slice_free (GstExifTagData, data);
510 g_slist_free (reader->pending_tags);
512 if (return_taglist) {
513 ret = reader->taglist;
514 reader->taglist = NULL;
517 if (reader->taglist) {
518 gst_tag_list_free (reader->taglist);
524 /* GstExifWriter functions */
527 gst_exif_writer_init (GstExifWriter * writer, gint byte_order)
531 gst_byte_writer_init (&writer->tagwriter);
532 gst_byte_writer_init (&writer->datawriter);
534 writer->byte_order = byte_order;
535 writer->tags_total = 0;
536 if (writer->byte_order != G_LITTLE_ENDIAN &&
537 writer->byte_order != G_BIG_ENDIAN) {
538 GST_WARNING ("Unexpected byte order %d, using system default: %d",
539 writer->byte_order, G_BYTE_ORDER);
540 writer->byte_order = G_BYTE_ORDER;
545 gst_exif_writer_reset_and_get_buffer (GstExifWriter * writer)
550 header = gst_byte_writer_reset_and_get_buffer (&writer->tagwriter);
551 data = gst_byte_writer_reset_and_get_buffer (&writer->datawriter);
553 return gst_buffer_join (header, data);
557 * Given the exif tag with the passed id, returns the map index of the tag
558 * corresponding to it. If use_complementary is true, then the complementary
559 * are also used in the search.
561 * Returns -1 if not found
564 exif_tag_map_find_reverse (guint16 exif_tag, const GstExifTagMatch * tag_map,
565 gboolean use_complementary)
569 for (i = 0; tag_map[i].exif_tag != 0; i++) {
570 if (exif_tag == tag_map[i].exif_tag || (use_complementary &&
571 exif_tag == tag_map[i].complementary_tag)) {
579 gst_tag_list_has_ifd_tags (const GstTagList * taglist,
580 const GstExifTagMatch * tag_map)
584 for (i = 0; tag_map[i].exif_tag != 0; i++) {
585 if (tag_map[i].gst_tag == NULL) {
586 if (tag_map[i].exif_tag == EXIF_GPS_IFD_TAG &&
587 gst_tag_list_has_ifd_tags (taglist, tag_map_gps))
589 if (tag_map[i].exif_tag == EXIF_IFD_TAG &&
590 gst_tag_list_has_ifd_tags (taglist, tag_map_exif))
595 if (gst_tag_list_get_value_index (taglist, tag_map[i].gst_tag, 0)) {
603 * Writes the tag entry.
605 * The tag entry is the tag id, the tag type,
606 * the count and the offset.
608 * The offset is the on the amount of data writen so far, as one
609 * can't predict the total bytes that the tag entries will take.
610 * This means those fields requires being updated later.
613 gst_exif_writer_write_tag_header (GstExifWriter * writer,
614 guint16 exif_tag, guint16 exif_type, guint32 count, guint32 offset,
615 const guint32 * offset_data)
617 GST_DEBUG ("Writing tag entry: id %x, type %u, count %u, offset %u",
618 exif_tag, exif_type, count, offset);
620 if (writer->byte_order == G_LITTLE_ENDIAN) {
621 gst_byte_writer_put_uint16_le (&writer->tagwriter, exif_tag);
622 gst_byte_writer_put_uint16_le (&writer->tagwriter, exif_type);
623 gst_byte_writer_put_uint32_le (&writer->tagwriter, count);
624 if (offset_data != NULL) {
625 gst_byte_writer_put_data (&writer->tagwriter, (guint8 *) offset_data, 4);
627 gst_byte_writer_put_uint32_le (&writer->tagwriter, offset);
629 } else if (writer->byte_order == G_BIG_ENDIAN) {
630 gst_byte_writer_put_uint16_be (&writer->tagwriter, exif_tag);
631 gst_byte_writer_put_uint16_be (&writer->tagwriter, exif_type);
632 gst_byte_writer_put_uint32_be (&writer->tagwriter, count);
633 if (offset_data != NULL) {
634 gst_byte_writer_put_data (&writer->tagwriter, (guint8 *) offset_data, 4);
636 gst_byte_writer_put_uint32_be (&writer->tagwriter, offset);
639 g_assert_not_reached ();
642 writer->tags_total++;
646 gst_exif_writer_write_rational_data (GstExifWriter * writer, guint32 frac_n,
649 if (writer->byte_order == G_LITTLE_ENDIAN) {
650 gst_byte_writer_put_uint32_le (&writer->datawriter, frac_n);
651 gst_byte_writer_put_uint32_le (&writer->datawriter, frac_d);
653 gst_byte_writer_put_uint32_be (&writer->datawriter, frac_n);
654 gst_byte_writer_put_uint32_be (&writer->datawriter, frac_d);
659 gst_exif_writer_write_signed_rational_data (GstExifWriter * writer,
660 gint32 frac_n, gint32 frac_d)
662 if (writer->byte_order == G_LITTLE_ENDIAN) {
663 gst_byte_writer_put_int32_le (&writer->datawriter, frac_n);
664 gst_byte_writer_put_int32_le (&writer->datawriter, frac_d);
666 gst_byte_writer_put_int32_be (&writer->datawriter, frac_n);
667 gst_byte_writer_put_int32_be (&writer->datawriter, frac_d);
672 gst_exif_writer_write_rational_tag (GstExifWriter * writer,
673 guint16 tag, guint32 frac_n, guint32 frac_d)
675 guint32 offset = gst_byte_writer_get_size (&writer->datawriter);
677 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_RATIONAL,
680 gst_exif_writer_write_rational_data (writer, frac_n, frac_d);
684 gst_exif_writer_write_signed_rational_tag (GstExifWriter * writer,
685 guint16 tag, gint32 frac_n, gint32 frac_d)
687 guint32 offset = gst_byte_writer_get_size (&writer->datawriter);
689 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_SRATIONAL,
692 gst_exif_writer_write_signed_rational_data (writer, frac_n, frac_d);
696 gst_exif_writer_write_rational_tag_from_double (GstExifWriter * writer,
697 guint16 tag, gdouble value)
702 gst_util_double_to_fraction (value, &frac_n, &frac_d);
704 gst_exif_writer_write_rational_tag (writer, tag, frac_n, frac_d);
708 gst_exif_writer_write_signed_rational_tag_from_double (GstExifWriter * writer,
709 guint16 tag, gdouble value)
714 gst_util_double_to_fraction (value, &frac_n, &frac_d);
716 gst_exif_writer_write_signed_rational_tag (writer, tag, frac_n, frac_d);
720 gst_exif_writer_write_byte_tag (GstExifWriter * writer, guint16 tag,
725 GST_WRITE_UINT8 ((guint8 *) & offset, value);
726 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_BYTE,
731 gst_exif_writer_write_short_tag (GstExifWriter * writer, guint16 tag,
736 if (writer->byte_order == G_LITTLE_ENDIAN) {
737 GST_WRITE_UINT16_LE ((guint8 *) & offset, value);
739 GST_WRITE_UINT16_BE ((guint8 *) & offset, value);
742 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_SHORT,
747 gst_exif_writer_write_long_tag (GstExifWriter * writer, guint16 tag,
751 if (writer->byte_order == G_LITTLE_ENDIAN) {
752 GST_WRITE_UINT32_LE ((guint8 *) & offset, value);
754 GST_WRITE_UINT32_BE ((guint8 *) & offset, value);
757 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_LONG,
763 write_exif_undefined_tag (GstExifWriter * writer, guint16 tag,
764 const guint8 * data, gint size)
769 /* we only use the data offset here, later we add up the
770 * resulting tag headers offset and the base offset */
771 offset = gst_byte_writer_get_size (&writer->datawriter);
772 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_UNDEFINED,
774 gst_byte_writer_put_data (&writer->datawriter, data, size);
776 /* small enough to go in the offset */
777 memcpy ((guint8 *) & offset, data, size);
778 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_UNDEFINED,
779 size, offset, &offset);
784 write_exif_ascii_tag (GstExifWriter * writer, guint16 tag, const gchar * str)
789 GError *error = NULL;
791 ascii_str = g_convert (str, -1, "latin1", "utf8", NULL, &ascii_size, &error);
794 GST_WARNING ("Failed to convert exif tag to ascii: 0x%x - %s. Error: %s",
795 tag, str, error->message);
796 g_error_free (error);
801 /* add the \0 at the end */
804 if (ascii_size > 4) {
805 /* we only use the data offset here, later we add up the
806 * resulting tag headers offset and the base offset */
807 offset = gst_byte_writer_get_size (&writer->datawriter);
808 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_ASCII,
809 ascii_size, offset, NULL);
810 gst_byte_writer_put_string (&writer->datawriter, ascii_str);
812 /* small enough to go in the offset */
813 memcpy ((guint8 *) & offset, ascii_str, ascii_size);
814 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_ASCII,
815 ascii_size, offset, &offset);
822 write_exif_ascii_tag_from_taglist (GstExifWriter * writer,
823 const GstTagList * taglist, const GstExifTagMatch * exiftag)
826 gboolean cleanup = FALSE;
828 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
831 /* FIXME support this by serializing them with a ','? */
832 GST_WARNING ("Multiple string tags not supported yet");
836 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
838 /* do some conversion if needed */
839 switch (G_VALUE_TYPE (value)) {
841 str = (gchar *) g_value_get_string (value);
844 if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
845 GstDateTime *dt = (GstDateTime *) g_value_get_boxed (value);
848 GST_WARNING ("NULL datetime received");
852 str = g_strdup_printf ("%04d:%02d:%02d %02d:%02d:%02d",
853 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
854 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
855 gst_date_time_get_minute (dt), gst_date_time_get_second (dt));
859 GST_WARNING ("Conversion from %s to ascii string not supported",
860 G_VALUE_TYPE_NAME (value));
868 write_exif_ascii_tag (writer, exiftag->exif_tag, str);
874 write_exif_undefined_tag_from_taglist (GstExifWriter * writer,
875 const GstTagList * taglist, const GstExifTagMatch * exiftag)
878 const guint8 *data = NULL;
880 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
883 GST_WARNING ("Only the first item in the taglist will be serialized");
887 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
889 /* do some conversion if needed */
890 switch (G_VALUE_TYPE (value)) {
892 data = (guint8 *) g_value_get_string (value);
893 size = strlen ((gchar *) data); /* no need to +1, undefined doesn't require it */
896 if (G_VALUE_TYPE (value) == GST_TYPE_BUFFER) {
897 GstBuffer *buf = gst_value_get_buffer (value);
899 data = GST_BUFFER_DATA (buf);
900 size = GST_BUFFER_SIZE (buf);
902 GST_WARNING ("Conversion from %s to raw data not supported",
903 G_VALUE_TYPE_NAME (value));
911 write_exif_undefined_tag (writer, exiftag->exif_tag, data, size);
915 write_exif_rational_tag_from_taglist (GstExifWriter * writer,
916 const GstTagList * taglist, const GstExifTagMatch * exiftag)
920 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
923 GST_WARNING ("Only the first item in the taglist will be serialized");
927 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
929 /* do some conversion if needed */
930 switch (G_VALUE_TYPE (value)) {
932 num = g_value_get_double (value);
933 gst_exif_writer_write_rational_tag_from_double (writer, exiftag->exif_tag,
937 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
938 gst_exif_writer_write_rational_tag (writer, exiftag->exif_tag,
939 gst_value_get_fraction_numerator (value),
940 gst_value_get_fraction_denominator (value));
942 GST_WARNING ("Conversion from %s to rational not supported",
943 G_VALUE_TYPE_NAME (value));
950 write_exif_signed_rational_tag_from_taglist (GstExifWriter * writer,
951 const GstTagList * taglist, const GstExifTagMatch * exiftag)
955 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
958 GST_WARNING ("Only the first item in the taglist will be serialized");
962 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
964 /* do some conversion if needed */
965 switch (G_VALUE_TYPE (value)) {
967 num = g_value_get_double (value);
968 gst_exif_writer_write_signed_rational_tag_from_double (writer,
969 exiftag->exif_tag, num);
972 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
973 gst_exif_writer_write_signed_rational_tag (writer, exiftag->exif_tag,
974 gst_value_get_fraction_numerator (value),
975 gst_value_get_fraction_denominator (value));
977 GST_WARNING ("Conversion from %s to signed rational not supported",
978 G_VALUE_TYPE_NAME (value));
985 write_exif_integer_tag_from_taglist (GstExifWriter * writer,
986 const GstTagList * taglist, const GstExifTagMatch * exiftag)
990 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
993 GST_WARNING ("Only the first item in the taglist will be serialized");
997 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
999 /* do some conversion if needed */
1000 switch (G_VALUE_TYPE (value)) {
1002 num = g_value_get_int (value);
1005 GST_WARNING ("Conversion from %s to int not supported",
1006 G_VALUE_TYPE_NAME (value));
1010 switch (exiftag->exif_type) {
1011 case EXIF_TYPE_LONG:
1012 gst_exif_writer_write_long_tag (writer, exiftag->exif_tag, num);
1014 case EXIF_TYPE_SHORT:
1015 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, num);
1023 write_exif_tag_from_taglist (GstExifWriter * writer, const GstTagList * taglist,
1024 const GstExifTagMatch * exiftag)
1026 GST_DEBUG ("Writing tag %s", exiftag->gst_tag);
1028 /* check for special handling */
1029 if (exiftag->serialize) {
1030 exiftag->serialize (writer, taglist, exiftag);
1034 switch (exiftag->exif_type) {
1035 case EXIF_TYPE_ASCII:
1036 write_exif_ascii_tag_from_taglist (writer, taglist, exiftag);
1038 case EXIF_TYPE_UNDEFINED:
1039 write_exif_undefined_tag_from_taglist (writer, taglist, exiftag);
1041 case EXIF_TYPE_RATIONAL:
1042 write_exif_rational_tag_from_taglist (writer, taglist, exiftag);
1044 case EXIF_TYPE_SRATIONAL:
1045 write_exif_signed_rational_tag_from_taglist (writer, taglist, exiftag);
1047 case EXIF_TYPE_LONG:
1048 case EXIF_TYPE_SHORT:
1049 write_exif_integer_tag_from_taglist (writer, taglist, exiftag);
1052 GST_WARNING ("Unhandled tag type %d", exiftag->exif_type);
1057 tagdata_copy (GstExifTagData * to, const GstExifTagData * from)
1059 to->tag = from->tag;
1060 to->tag_type = from->tag_type;
1061 to->count = from->count;
1062 to->offset = from->offset;
1063 to->offset_as_data = from->offset_as_data;
1067 gst_exif_tag_rewrite_offsets (GstByteWriter * writer, gint byte_order,
1068 guint32 offset, gint num_tags, GstByteWriter * inner_ifds_data)
1070 GstByteReader *reader;
1072 guint16 aux = G_MAXUINT16;
1074 GST_LOG ("Rewriting tag entries offsets");
1076 reader = (GstByteReader *) writer;
1078 if (num_tags == -1) {
1079 if (byte_order == G_LITTLE_ENDIAN) {
1080 gst_byte_reader_get_uint16_le (reader, &aux);
1082 gst_byte_reader_get_uint16_be (reader, &aux);
1084 if (aux == G_MAXUINT16) {
1085 GST_WARNING ("Failed to read number of tags, won't rewrite offsets");
1088 num_tags = (gint) aux;
1091 g_return_if_fail (num_tags != -1);
1093 GST_DEBUG ("number of tags %d", num_tags);
1095 for (i = 0; i < num_tags; i++) {
1097 guint32 cur_offset = 0;
1102 g_assert (gst_byte_writer_get_pos (writer) <
1103 gst_byte_writer_get_size (writer));
1106 if (byte_order == G_LITTLE_ENDIAN) {
1107 if (!gst_byte_reader_get_uint16_le (reader, &tag_id))
1109 if (!gst_byte_reader_get_uint16_le (reader, &type))
1111 if (!gst_byte_reader_get_uint32_le (reader, &count))
1114 if (!gst_byte_reader_get_uint16_be (reader, &tag_id))
1116 if (!gst_byte_reader_get_uint16_be (reader, &type))
1118 if (!gst_byte_reader_get_uint32_be (reader, &count))
1122 GST_LOG ("Parsed tag %x of type %u and count %u", tag_id, type, count);
1125 case EXIF_TYPE_BYTE:
1126 case EXIF_TYPE_ASCII:
1127 case EXIF_TYPE_UNDEFINED:
1130 case EXIF_TYPE_SHORT:
1131 byte_size = count * 2; /* 2 bytes */
1133 case EXIF_TYPE_LONG:
1134 case EXIF_TYPE_SLONG:
1135 byte_size = count * 4; /* 4 bytes */
1137 case EXIF_TYPE_RATIONAL:
1138 case EXIF_TYPE_SRATIONAL:
1139 byte_size = count * 8; /* 8 bytes */
1142 g_assert_not_reached ();
1146 /* adjust the offset if needed */
1147 if (byte_size > 4 || tag_id == EXIF_GPS_IFD_TAG || tag_id == EXIF_IFD_TAG) {
1148 if (byte_order == G_LITTLE_ENDIAN) {
1149 if (gst_byte_reader_peek_uint32_le (reader, &cur_offset)) {
1150 gst_byte_writer_put_uint32_le (writer, cur_offset + offset);
1153 if (gst_byte_reader_peek_uint32_be (reader, &cur_offset)) {
1154 gst_byte_writer_put_uint32_be (writer, cur_offset + offset);
1157 GST_DEBUG ("Rewriting tag offset from %u to (%u + %u) %u",
1158 cur_offset, cur_offset, offset, cur_offset + offset);
1160 if ((tag_id == EXIF_GPS_IFD_TAG || tag_id == EXIF_IFD_TAG) &&
1161 inner_ifds_data != NULL) {
1162 /* needs special handling */
1163 if (!gst_byte_writer_set_pos (inner_ifds_data, cur_offset)) {
1164 GST_WARNING ("Failed to position writer to rewrite inner ifd "
1169 gst_exif_tag_rewrite_offsets (inner_ifds_data, byte_order, offset, -1,
1173 gst_byte_reader_skip (reader, 4);
1174 GST_DEBUG ("No need to rewrite tag offset");
1177 GST_LOG ("Done rewriting offsets");
1181 parse_exif_ascii_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1182 guint32 count, guint32 offset, const guint8 * offset_as_data)
1187 guint32 real_offset;
1188 GError *error = NULL;
1191 if (offset < reader->base_offset) {
1192 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1193 reader->base_offset);
1197 real_offset = offset - reader->base_offset;
1198 if (real_offset >= GST_BUFFER_SIZE (reader->buffer)) {
1199 GST_WARNING ("Invalid offset %u for buffer of size %u, not adding tag %s",
1200 real_offset, GST_BUFFER_SIZE (reader->buffer), tag->gst_tag);
1205 g_strndup ((gchar *) (GST_BUFFER_DATA (reader->buffer) + real_offset),
1208 str = g_strndup ((gchar *) offset_as_data, count);
1211 /* convert from ascii to utf8 */
1212 if (g_utf8_validate (str, -1, NULL)) {
1213 GST_DEBUG ("Exif string is already on utf8: %s", str);
1216 GST_DEBUG ("Exif string isn't utf8, trying to convert from latin1: %s",
1218 utfstr = g_convert (str, count, "utf8", "latin1", NULL, NULL, &error);
1221 GST_WARNING ("Skipping tag %d:%s. Failed to convert ascii string "
1222 "to utf8 : %s - %s", tag->exif_tag, tag->gst_tag, str,
1224 g_error_free (error);
1230 tagtype = gst_tag_get_type (tag->gst_tag);
1231 if (tagtype == GST_TYPE_DATE_TIME) {
1232 gint year = 0, month = 1, day = 1, hour = 0, minute = 0, second = 0;
1234 if (sscanf (utfstr, "%04d:%02d:%02d %02d:%02d:%02d", &year, &month, &day,
1235 &hour, &minute, &second) > 0) {
1238 d = gst_date_time_new_local_time (year, month, day, hour, minute, second);
1239 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE,
1240 tag->gst_tag, d, NULL);
1241 gst_date_time_unref (d);
1243 GST_WARNING ("Failed to parse %s into a datetime tag", str);
1245 } else if (tagtype == G_TYPE_STRING) {
1246 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag,
1249 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1256 parse_exif_long_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1257 guint32 count, guint32 offset, const guint8 * offset_as_data)
1262 GST_WARNING ("Long tags with more than one value are not supported");
1266 tagtype = gst_tag_get_type (tag->gst_tag);
1267 if (tagtype == G_TYPE_INT) {
1268 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag,
1271 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1278 parse_exif_undefined_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1279 guint32 count, guint32 offset, const guint8 * offset_as_data)
1283 guint32 real_offset;
1286 if (offset < reader->base_offset) {
1287 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1288 reader->base_offset);
1292 real_offset = offset - reader->base_offset;
1293 if (real_offset >= GST_BUFFER_SIZE (reader->buffer)) {
1294 GST_WARNING ("Invalid offset %u for buffer of size %u, not adding tag %s",
1295 real_offset, GST_BUFFER_SIZE (reader->buffer), tag->gst_tag);
1299 /* +1 because it could be a string without the \0 */
1300 data = malloc (sizeof (guint8) * count + 1);
1301 memcpy (data, GST_BUFFER_DATA (reader->buffer) + real_offset, count);
1304 data = malloc (sizeof (guint8) * count + 1);
1305 memcpy (data, (guint8 *) offset_as_data, count);
1309 tagtype = gst_tag_get_type (tag->gst_tag);
1310 if (tagtype == GST_TYPE_BUFFER) {
1313 buf = gst_buffer_new ();
1314 GST_BUFFER_DATA (buf) = data;
1315 GST_BUFFER_MALLOCDATA (buf) = data;
1316 GST_BUFFER_SIZE (buf) = count;
1319 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_APPEND, tag->gst_tag,
1322 gst_buffer_unref (buf);
1323 } else if (tagtype == G_TYPE_STRING) {
1324 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag,
1327 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1334 exif_reader_read_rational_tag (GstExifReader * exif_reader,
1335 guint32 count, guint32 offset, gboolean is_signed,
1336 gint32 * _frac_n, gint32 * _frac_d)
1338 GstByteReader data_reader;
1339 guint32 real_offset;
1344 GST_WARNING ("Rationals with multiple entries are not supported");
1346 if (offset < exif_reader->base_offset) {
1347 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1348 exif_reader->base_offset);
1352 real_offset = offset - exif_reader->base_offset;
1353 if (real_offset >= GST_BUFFER_SIZE (exif_reader->buffer)) {
1354 GST_WARNING ("Invalid offset %u for buffer of size %u",
1355 real_offset, GST_BUFFER_SIZE (exif_reader->buffer));
1359 gst_byte_reader_init_from_buffer (&data_reader, exif_reader->buffer);
1360 if (!gst_byte_reader_set_pos (&data_reader, real_offset))
1364 guint32 aux_n = 0, aux_d = 0;
1365 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1366 if (!gst_byte_reader_get_uint32_le (&data_reader, &aux_n) ||
1367 !gst_byte_reader_get_uint32_le (&data_reader, &aux_d))
1370 if (!gst_byte_reader_get_uint32_be (&data_reader, &aux_n) ||
1371 !gst_byte_reader_get_uint32_be (&data_reader, &aux_d))
1374 frac_n = (gint32) aux_n;
1375 frac_d = (gint32) aux_d;
1377 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1378 if (!gst_byte_reader_get_int32_le (&data_reader, &frac_n) ||
1379 !gst_byte_reader_get_int32_le (&data_reader, &frac_d))
1382 if (!gst_byte_reader_get_int32_be (&data_reader, &frac_n) ||
1383 !gst_byte_reader_get_int32_be (&data_reader, &frac_d))
1396 GST_WARNING ("Failed to read from byte reader. (Buffer too short?)");
1401 parse_exif_rational_tag (GstExifReader * exif_reader,
1402 const gchar * gst_tag, guint32 count, guint32 offset, gdouble multiplier,
1410 GST_DEBUG ("Reading fraction for tag %s...", gst_tag);
1411 if (!exif_reader_read_rational_tag (exif_reader, count, offset, is_signed,
1414 GST_DEBUG ("Read fraction for tag %s: %d/%d", gst_tag, frac_n, frac_d);
1416 type = gst_tag_get_type (gst_tag);
1419 gst_util_fraction_to_double (frac_n, frac_d, &value);
1420 value *= multiplier;
1421 GST_DEBUG ("Adding %s tag: %lf", gst_tag, value);
1422 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE, gst_tag,
1426 if (type == GST_TYPE_FRACTION) {
1427 GValue fraction = { 0 };
1429 g_value_init (&fraction, GST_TYPE_FRACTION);
1430 gst_value_set_fraction (&fraction, frac_n * multiplier, frac_d);
1431 gst_tag_list_add_value (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
1432 gst_tag, &fraction);
1433 g_value_unset (&fraction);
1435 GST_WARNING ("Can't convert from fraction into %s", g_type_name (type));
1442 write_exif_ifd (const GstTagList * taglist, guint byte_order,
1443 guint32 base_offset, const GstExifTagMatch * tag_map)
1445 GstExifWriter writer;
1448 GST_DEBUG ("Formatting taglist %p as exif buffer. Byte order: %d, "
1449 "base_offset: %u", taglist, byte_order, base_offset);
1451 g_assert (byte_order == G_LITTLE_ENDIAN || byte_order == G_BIG_ENDIAN);
1453 if (!gst_tag_list_has_ifd_tags (taglist, tag_map)) {
1454 GST_DEBUG ("No tags for this ifd");
1458 gst_exif_writer_init (&writer, byte_order);
1460 /* write tag number as 0 */
1461 gst_byte_writer_put_uint16_le (&writer.tagwriter, 0);
1463 /* write both tag headers and data
1464 * in ascending id order */
1466 for (i = 0; tag_map[i].exif_tag != 0; i++) {
1468 /* special cases have NULL gst tag */
1469 if (tag_map[i].gst_tag == NULL) {
1470 GstBuffer *inner_ifd = NULL;
1471 const GstExifTagMatch *inner_tag_map = NULL;
1473 GST_LOG ("Inner ifd tag: %x", tag_map[i].exif_tag);
1475 if (tag_map[i].exif_tag == EXIF_GPS_IFD_TAG) {
1476 inner_tag_map = tag_map_gps;
1477 } else if (tag_map[i].exif_tag == EXIF_IFD_TAG) {
1478 inner_tag_map = tag_map_exif;
1479 } else if (tag_map[i].exif_tag == EXIF_VERSION_TAG) {
1480 /* special case where we write the exif version */
1481 write_exif_undefined_tag (&writer, EXIF_VERSION_TAG, (guint8 *) "0230",
1483 } else if (tag_map[i].exif_tag == EXIF_FLASHPIX_VERSION_TAG) {
1484 /* special case where we write the flashpix version */
1485 write_exif_undefined_tag (&writer, EXIF_FLASHPIX_VERSION_TAG,
1486 (guint8 *) "0100", 4);
1489 if (inner_tag_map) {
1490 /* base offset and tagheader size are added when rewriting offset */
1491 inner_ifd = write_exif_ifd (taglist, byte_order,
1492 gst_byte_writer_get_size (&writer.datawriter), inner_tag_map);
1496 GST_DEBUG ("Adding inner ifd: %x", tag_map[i].exif_tag);
1497 gst_exif_writer_write_tag_header (&writer, tag_map[i].exif_tag,
1499 gst_byte_writer_get_size (&writer.datawriter), NULL);
1500 gst_byte_writer_put_data (&writer.datawriter,
1501 GST_BUFFER_DATA (inner_ifd), GST_BUFFER_SIZE (inner_ifd));
1502 gst_buffer_unref (inner_ifd);
1507 GST_LOG ("Checking tag %s", tag_map[i].gst_tag);
1508 if (gst_tag_list_get_value_index (taglist, tag_map[i].gst_tag, 0) == NULL)
1511 write_exif_tag_from_taglist (&writer, taglist, &tag_map[i]);
1514 /* Add the next IFD offset, we just set it to 0 because
1515 * there is no easy way to predict what it is going to be.
1516 * The user might rewrite the value if needed */
1517 gst_byte_writer_put_uint32_le (&writer.tagwriter, 0);
1519 /* write the number of tags */
1520 gst_byte_writer_set_pos (&writer.tagwriter, 0);
1521 if (writer.byte_order == G_LITTLE_ENDIAN)
1522 gst_byte_writer_put_uint16_le (&writer.tagwriter, writer.tags_total);
1524 gst_byte_writer_put_uint16_be (&writer.tagwriter, writer.tags_total);
1526 GST_DEBUG ("Number of tags rewritten to %d", writer.tags_total);
1528 /* now that we know the tag headers size, we can add the offsets */
1529 gst_exif_tag_rewrite_offsets (&writer.tagwriter, writer.byte_order,
1530 base_offset + gst_byte_writer_get_size (&writer.tagwriter),
1531 writer.tags_total, &writer.datawriter);
1533 return gst_exif_writer_reset_and_get_buffer (&writer);
1537 parse_exif_tag_header (GstByteReader * reader, gint byte_order,
1538 GstExifTagData * _tagdata)
1540 g_assert (_tagdata);
1542 /* read the fields */
1543 if (byte_order == G_LITTLE_ENDIAN) {
1544 if (!gst_byte_reader_get_uint16_le (reader, &_tagdata->tag) ||
1545 !gst_byte_reader_get_uint16_le (reader, &_tagdata->tag_type) ||
1546 !gst_byte_reader_get_uint32_le (reader, &_tagdata->count) ||
1547 !gst_byte_reader_get_data (reader, 4, &_tagdata->offset_as_data)) {
1550 _tagdata->offset = GST_READ_UINT32_LE (_tagdata->offset_as_data);
1552 if (!gst_byte_reader_get_uint16_be (reader, &_tagdata->tag) ||
1553 !gst_byte_reader_get_uint16_be (reader, &_tagdata->tag_type) ||
1554 !gst_byte_reader_get_uint32_be (reader, &_tagdata->count) ||
1555 !gst_byte_reader_get_data (reader, 4, &_tagdata->offset_as_data)) {
1558 _tagdata->offset = GST_READ_UINT32_BE (_tagdata->offset_as_data);
1565 parse_exif_ifd (GstExifReader * exif_reader, gint buf_offset,
1566 const GstExifTagMatch * tag_map)
1568 GstByteReader reader;
1569 guint16 entries = 0;
1572 g_return_val_if_fail (exif_reader->byte_order == G_LITTLE_ENDIAN
1573 || exif_reader->byte_order == G_BIG_ENDIAN, FALSE);
1575 gst_byte_reader_init_from_buffer (&reader, exif_reader->buffer);
1576 if (!gst_byte_reader_set_pos (&reader, buf_offset)) {
1577 GST_WARNING ("Buffer offset invalid when parsing exif ifd");
1581 /* read the IFD entries number */
1582 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1583 if (!gst_byte_reader_get_uint16_le (&reader, &entries))
1586 if (!gst_byte_reader_get_uint16_be (&reader, &entries))
1589 GST_DEBUG ("Read number of entries: %u", entries);
1591 /* iterate over the buffer and find the tags and stuff */
1592 for (i = 0; i < entries; i++) {
1593 GstExifTagData tagdata;
1596 GST_LOG ("Reading entry: %u", i);
1598 if (!parse_exif_tag_header (&reader, exif_reader->byte_order, &tagdata))
1601 GST_DEBUG ("Parsed tag: id 0x%x, type %u, count %u, offset %u (0x%x)"
1602 ", buf size: %u", tagdata.tag, tagdata.tag_type, tagdata.count,
1603 tagdata.offset, tagdata.offset, gst_byte_reader_get_size (&reader));
1605 map_index = exif_tag_map_find_reverse (tagdata.tag, tag_map, TRUE);
1606 if (map_index == -1) {
1607 GST_WARNING ("Unmapped exif tag: 0x%x", tagdata.tag);
1612 * inner ifd tags handling, errors processing those are being ignored
1613 * and we try to continue the parsing
1615 if (tagdata.tag == EXIF_GPS_IFD_TAG) {
1616 parse_exif_ifd (exif_reader,
1617 tagdata.offset - exif_reader->base_offset, tag_map_gps);
1621 if (tagdata.tag == EXIF_IFD_TAG) {
1622 parse_exif_ifd (exif_reader,
1623 tagdata.offset - exif_reader->base_offset, tag_map_exif);
1627 if (tagdata.tag == EXIF_VERSION_TAG ||
1628 tagdata.tag == EXIF_FLASHPIX_VERSION_TAG) {
1633 /* tags that need specialized deserialization */
1634 if (tag_map[map_index].deserialize) {
1635 i += tag_map[map_index].deserialize (exif_reader, &reader,
1636 &tag_map[map_index], &tagdata);
1640 switch (tagdata.tag_type) {
1641 case EXIF_TYPE_ASCII:
1642 parse_exif_ascii_tag (exif_reader, &tag_map[map_index],
1643 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1645 case EXIF_TYPE_RATIONAL:
1646 parse_exif_rational_tag (exif_reader, tag_map[map_index].gst_tag,
1647 tagdata.count, tagdata.offset, 1, FALSE);
1649 case EXIF_TYPE_SRATIONAL:
1650 parse_exif_rational_tag (exif_reader, tag_map[map_index].gst_tag,
1651 tagdata.count, tagdata.offset, 1, TRUE);
1653 case EXIF_TYPE_UNDEFINED:
1654 parse_exif_undefined_tag (exif_reader, &tag_map[map_index],
1655 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1657 case EXIF_TYPE_LONG:
1658 parse_exif_long_tag (exif_reader, &tag_map[map_index],
1659 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1662 GST_WARNING ("Unhandled tag type: %u", tagdata.tag_type);
1667 /* check if the pending tags have something that can still be added */
1670 GstExifTagData *data;
1672 for (walker = exif_reader->pending_tags; walker;
1673 walker = g_slist_next (walker)) {
1674 data = (GstExifTagData *) walker->data;
1675 switch (data->tag) {
1676 case EXIF_TAG_XRESOLUTION:
1677 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_HORIZONTAL_PPI,
1678 data->count, data->offset, 1, FALSE);
1680 case EXIF_TAG_YRESOLUTION:
1681 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_VERTICAL_PPI,
1682 data->count, data->offset, 1, FALSE);
1695 GST_WARNING ("Failed to parse the exif ifd");
1701 * gst_tag_list_to_exif_buffer:
1702 * @taglist: The taglist
1703 * @byte_order: byte order used in writing (G_LITTLE_ENDIAN or G_BIG_ENDIAN)
1704 * @base_offset: Offset from the tiff header first byte
1706 * Formats the tags in taglist on exif format. The resulting buffer contains
1707 * the tags IFD and is followed by the data pointed by the tag entries.
1709 * Returns: A GstBuffer containing the tag entries followed by the tag data
1714 gst_tag_list_to_exif_buffer (const GstTagList * taglist, gint byte_order,
1715 guint32 base_offset)
1717 return write_exif_ifd (taglist, byte_order, base_offset, tag_map_ifd0);
1721 * gst_tag_list_to_exif_buffer_with_tiff_header:
1722 * @taglist: The taglist
1724 * Formats the tags in taglist into exif structure, a tiff header
1725 * is put in the beginning of the buffer.
1727 * Returns: A GstBuffer containing the data
1732 gst_tag_list_to_exif_buffer_with_tiff_header (const GstTagList * taglist)
1735 GstByteWriter writer;
1738 ifd = gst_tag_list_to_exif_buffer (taglist, G_BYTE_ORDER, 8);
1740 GST_WARNING ("Failed to create exif buffer");
1743 size = TIFF_HEADER_SIZE + GST_BUFFER_SIZE (ifd);
1745 /* TODO what is the correct endianness here? */
1746 gst_byte_writer_init_with_size (&writer, size, FALSE);
1748 if (G_BYTE_ORDER == G_LITTLE_ENDIAN) {
1749 gst_byte_writer_put_uint16_le (&writer, TIFF_LITTLE_ENDIAN);
1750 gst_byte_writer_put_uint16_le (&writer, 42);
1751 gst_byte_writer_put_uint32_le (&writer, 8);
1753 gst_byte_writer_put_uint16_be (&writer, TIFF_BIG_ENDIAN);
1754 gst_byte_writer_put_uint16_be (&writer, 42);
1755 gst_byte_writer_put_uint32_be (&writer, 8);
1757 if (!gst_byte_writer_put_data (&writer, GST_BUFFER_DATA (ifd),
1758 GST_BUFFER_SIZE (ifd))) {
1759 GST_WARNING ("Byte writer size mismatch");
1760 /* reaching here is a programming error because we should have a buffer
1762 g_assert_not_reached ();
1763 gst_buffer_unref (ifd);
1764 gst_byte_writer_reset (&writer);
1767 gst_buffer_unref (ifd);
1768 return gst_byte_writer_reset_and_get_buffer (&writer);
1772 * gst_tag_list_from_exif_buffer:
1773 * @buffer: The exif buffer
1774 * @byte_order: byte order of the data
1775 * @base_offset: Offset from the tiff header to this buffer
1777 * Parses the IFD and IFD tags data contained in the buffer and puts it
1778 * on a taglist. The base_offset is used to subtract from the offset in
1779 * the tag entries and be able to get the offset relative to the buffer
1782 * Returns: The parsed taglist
1787 gst_tag_list_from_exif_buffer (const GstBuffer * buffer, gint byte_order,
1788 guint32 base_offset)
1790 GstExifReader reader;
1791 g_return_val_if_fail (byte_order == G_LITTLE_ENDIAN
1792 || byte_order == G_BIG_ENDIAN, NULL);
1794 gst_exif_reader_init (&reader, byte_order, buffer, base_offset);
1796 if (!parse_exif_ifd (&reader, 0, tag_map_ifd0))
1799 return gst_exif_reader_reset (&reader, TRUE);
1803 gst_exif_reader_reset (&reader, FALSE);
1804 GST_WARNING ("Failed to parse the exif buffer");
1810 * gst_tag_list_from_exif_buffer_with_tiff_header:
1811 * @buffer: The exif buffer
1813 * Parses the exif tags starting with a tiff header structure.
1815 * Returns: The taglist
1820 gst_tag_list_from_exif_buffer_with_tiff_header (const GstBuffer * buffer)
1822 GstByteReader reader;
1823 guint16 fortytwo = 42;
1824 guint16 endianness = 0;
1826 GstTagList *taglist = NULL;
1827 GstBuffer *subbuffer;
1829 GST_LOG ("Parsing exif tags with tiff header of size %u",
1830 GST_BUFFER_SIZE (buffer));
1832 gst_byte_reader_init_from_buffer (&reader, buffer);
1834 GST_LOG ("Parsing the tiff header");
1835 if (!gst_byte_reader_get_uint16_be (&reader, &endianness)) {
1836 goto byte_reader_fail;
1839 if (endianness == TIFF_LITTLE_ENDIAN) {
1840 if (!gst_byte_reader_get_uint16_le (&reader, &fortytwo) ||
1841 !gst_byte_reader_get_uint32_le (&reader, &offset))
1842 goto byte_reader_fail;
1843 } else if (endianness == TIFF_BIG_ENDIAN) {
1844 if (!gst_byte_reader_get_uint16_be (&reader, &fortytwo) ||
1845 !gst_byte_reader_get_uint32_be (&reader, &offset))
1846 goto byte_reader_fail;
1848 GST_WARNING ("Invalid endianness number %u", endianness);
1852 if (fortytwo != 42) {
1853 GST_WARNING ("Invalid magic number %u, should be 42", fortytwo);
1857 subbuffer = gst_buffer_new_and_alloc (GST_BUFFER_SIZE (buffer) -
1858 (TIFF_HEADER_SIZE - 2));
1859 memcpy (GST_BUFFER_DATA (subbuffer),
1860 GST_BUFFER_DATA (buffer) + TIFF_HEADER_SIZE,
1861 GST_BUFFER_SIZE (buffer) - TIFF_HEADER_SIZE);
1863 taglist = gst_tag_list_from_exif_buffer (subbuffer,
1864 endianness == TIFF_LITTLE_ENDIAN ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, 8);
1866 gst_buffer_unref (subbuffer);
1871 GST_WARNING ("Failed to read values from buffer");
1876 /* special serialization functions */
1877 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (contrast,
1878 capturing_contrast);
1879 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (exposure_mode,
1880 capturing_exposure_mode);
1881 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (exposure_program,
1882 capturing_exposure_program);
1883 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (gain_control,
1884 capturing_gain_adjustment);
1885 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (metering_mode,
1886 capturing_metering_mode);
1887 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (orientation,
1889 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (saturation,
1890 capturing_saturation);
1891 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (scene_capture_type,
1892 capturing_scene_capture_type);
1893 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (sharpness,
1894 capturing_sharpness);
1895 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (source,
1897 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (white_balance,
1898 capturing_white_balance);
1901 serialize_geo_coordinate (GstExifWriter * writer, const GstTagList * taglist,
1902 const GstExifTagMatch * exiftag)
1911 latitude = exiftag->exif_tag == EXIF_TAG_GPS_LATITUDE; /* exif tag for latitude */
1912 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
1913 GST_WARNING ("Failed to get double from tag list for tag: %s",
1918 /* first write the Latitude or Longitude Ref */
1921 write_exif_ascii_tag (writer, exiftag->complementary_tag, "N");
1924 write_exif_ascii_tag (writer, exiftag->complementary_tag, "S");
1928 write_exif_ascii_tag (writer, exiftag->complementary_tag, "E");
1931 write_exif_ascii_tag (writer, exiftag->complementary_tag, "W");
1935 /* now write the degrees stuff */
1936 GST_LOG ("Converting geo location %lf to degrees", value);
1937 degrees = (gint) value;
1939 minutes = (gint) (value * 60);
1940 value = (value * 60) - minutes;
1941 seconds = (gint) (value * 60);
1942 GST_LOG ("Converted geo location to %d.%d'%d'' degrees", degrees,
1945 offset = gst_byte_writer_get_size (&writer->datawriter);
1946 gst_exif_writer_write_tag_header (writer, exiftag->exif_tag,
1947 EXIF_TYPE_RATIONAL, 3, offset, NULL);
1948 gst_exif_writer_write_rational_data (writer, degrees, 1);
1949 gst_exif_writer_write_rational_data (writer, minutes, 1);
1950 gst_exif_writer_write_rational_data (writer, seconds, 1);
1954 deserialize_geo_coordinate (GstExifReader * exif_reader,
1955 GstByteReader * reader, const GstExifTagMatch * exiftag,
1956 GstExifTagData * tagdata)
1958 GstByteReader fractions_reader;
1960 GstExifTagData next_tagdata;
1962 /* for the conversion */
1963 guint32 degrees_n = 0;
1964 guint32 degrees_d = 1;
1965 guint32 minutes_n = 0;
1966 guint32 minutes_d = 1;
1967 guint32 seconds_n = 0;
1968 guint32 seconds_d = 1;
1973 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
1976 if (exiftag->complementary_tag != tagdata->tag) {
1977 /* First should come the 'Ref' tags */
1978 GST_WARNING ("Tag %d is not the 'Ref' tag for latitude nor longitude",
1983 if (tagdata->offset_as_data[0] == 'N' || tagdata->offset_as_data[0] == 'E') {
1985 } else if (tagdata->offset_as_data[0] == 'S'
1986 || tagdata->offset_as_data[0] == 'W') {
1989 GST_WARNING ("Invalid LatitudeRef or LongitudeRef %c",
1990 tagdata->offset_as_data[0]);
1994 /* now read the following tag that must be the latitude or longitude */
1995 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1996 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
1999 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2003 if (exiftag->exif_tag != next_tagdata.tag) {
2004 GST_WARNING ("This is not a geo coordinate tag");
2008 /* read the remaining tag entry data */
2009 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2017 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2018 GST_WARNING ("Invalid type %d for geo coordinate (latitude/longitude)",
2019 next_tagdata.tag_type);
2022 if (next_tagdata.count != 3) {
2023 GST_WARNING ("Geo coordinate should use 3 fractions, we have %u",
2024 next_tagdata.count);
2028 /* now parse the fractions */
2029 gst_byte_reader_init_from_buffer (&fractions_reader, exif_reader->buffer);
2030 if (!gst_byte_reader_set_pos (&fractions_reader,
2031 next_tagdata.offset - exif_reader->base_offset))
2034 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2035 if (!gst_byte_reader_get_uint32_le (&fractions_reader, °rees_n) ||
2036 !gst_byte_reader_get_uint32_le (&fractions_reader, °rees_d) ||
2037 !gst_byte_reader_get_uint32_le (&fractions_reader, &minutes_n) ||
2038 !gst_byte_reader_get_uint32_le (&fractions_reader, &minutes_d) ||
2039 !gst_byte_reader_get_uint32_le (&fractions_reader, &seconds_n) ||
2040 !gst_byte_reader_get_uint32_le (&fractions_reader, &seconds_d))
2043 if (!gst_byte_reader_get_uint32_be (&fractions_reader, °rees_n) ||
2044 !gst_byte_reader_get_uint32_be (&fractions_reader, °rees_d) ||
2045 !gst_byte_reader_get_uint32_be (&fractions_reader, &minutes_n) ||
2046 !gst_byte_reader_get_uint32_be (&fractions_reader, &minutes_d) ||
2047 !gst_byte_reader_get_uint32_be (&fractions_reader, &seconds_n) ||
2048 !gst_byte_reader_get_uint32_be (&fractions_reader, &seconds_d))
2052 GST_DEBUG ("Read degrees fraction for tag %s: %u/%u %u/%u %u/%u",
2053 exiftag->gst_tag, degrees_n, degrees_d, minutes_n, minutes_d,
2054 seconds_n, seconds_d);
2056 gst_util_fraction_to_double (degrees_n, degrees_d, °rees);
2057 gst_util_fraction_to_double (minutes_n, minutes_d, &minutes);
2058 gst_util_fraction_to_double (seconds_n, seconds_d, &seconds);
2060 minutes += seconds / 60;
2061 degrees += minutes / 60;
2062 degrees *= multiplier;
2064 GST_DEBUG ("Adding %s tag: %lf", exiftag->gst_tag, degrees);
2065 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2066 exiftag->gst_tag, degrees, NULL);
2071 GST_WARNING ("Failed to read fields from buffer (too short?)");
2077 serialize_geo_direction (GstExifWriter * writer, const GstTagList * taglist,
2078 const GstExifTagMatch * exiftag)
2082 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2083 GST_WARNING ("Failed to get double from tag list for tag: %s",
2088 /* first write the direction ref */
2089 write_exif_ascii_tag (writer, exiftag->complementary_tag, "T");
2090 gst_exif_writer_write_rational_tag_from_double (writer,
2091 exiftag->exif_tag, value);
2095 deserialize_geo_direction (GstExifReader * exif_reader,
2096 GstByteReader * reader, const GstExifTagMatch * exiftag,
2097 GstExifTagData * tagdata)
2099 GstExifTagData next_tagdata = { 0, };
2102 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2105 if (exiftag->complementary_tag == tagdata->tag) {
2106 /* First should come the 'Ref' tags */
2107 if (tagdata->offset_as_data[0] == 'M') {
2108 GST_WARNING ("Magnetic direction is not supported");
2110 } else if (tagdata->offset_as_data[0] == 'T') {
2113 GST_WARNING ("Invalid Ref for direction or track %c",
2114 tagdata->offset_as_data[0]);
2118 GST_DEBUG ("No Direction Ref, using default=T");
2119 if (tagdata->tag == exiftag->exif_tag) {
2120 /* this is the main tag */
2121 tagdata_copy (&next_tagdata, tagdata);
2125 if (next_tagdata.tag == 0) {
2126 /* now read the following tag that must be the exif_tag */
2127 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2128 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2131 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2135 if (exiftag->exif_tag != next_tagdata.tag) {
2136 GST_WARNING ("Unexpected tag");
2140 /* read the remaining tag entry data */
2141 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2149 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2150 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2154 if (next_tagdata.count != 1) {
2155 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2156 next_tagdata.tag_type, next_tagdata.count);
2160 parse_exif_rational_tag (exif_reader,
2161 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, 1, FALSE);
2166 GST_WARNING ("Failed to read fields from buffer (too short?)");
2172 serialize_geo_elevation (GstExifWriter * writer, const GstTagList * taglist,
2173 const GstExifTagMatch * exiftag)
2177 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2178 GST_WARNING ("Failed to get double from tag list for tag: %s",
2183 /* first write the Ref */
2184 gst_exif_writer_write_byte_tag (writer,
2185 exiftag->complementary_tag, value >= 0 ? 0 : 1);
2191 gst_exif_writer_write_rational_tag_from_double (writer,
2192 exiftag->exif_tag, value);
2196 deserialize_geo_elevation (GstExifReader * exif_reader,
2197 GstByteReader * reader, const GstExifTagMatch * exiftag,
2198 GstExifTagData * tagdata)
2200 GstExifTagData next_tagdata = { 0, };
2201 gint multiplier = 1;
2204 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2207 if (exiftag->complementary_tag == tagdata->tag) {
2208 if (tagdata->offset_as_data[0] == 0) {
2210 } else if (tagdata->offset_as_data[0] == 1) {
2213 GST_WARNING ("Invalid GPSAltitudeRef %u", tagdata->offset_as_data[0]);
2217 GST_DEBUG ("No GPSAltitudeRef, using default=0");
2218 if (tagdata->tag == exiftag->exif_tag) {
2219 tagdata_copy (&next_tagdata, tagdata);
2223 /* now read the following tag that must be the exif_tag */
2224 if (next_tagdata.tag == 0) {
2225 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2226 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2229 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2233 if (exiftag->exif_tag != next_tagdata.tag) {
2234 GST_WARNING ("Unexpected tag");
2238 /* read the remaining tag entry data */
2239 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2247 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2248 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2252 if (next_tagdata.count != 1) {
2253 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2254 next_tagdata.tag_type, next_tagdata.count);
2258 parse_exif_rational_tag (exif_reader,
2259 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, multiplier,
2265 GST_WARNING ("Failed to read fields from buffer (too short?)");
2271 serialize_speed (GstExifWriter * writer, const GstTagList * taglist,
2272 const GstExifTagMatch * exiftag)
2276 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2277 GST_WARNING ("Failed to get double from tag list for tag: %s",
2282 /* first write the Ref */
2283 write_exif_ascii_tag (writer, exiftag->complementary_tag, "K");
2286 gst_exif_writer_write_rational_tag_from_double (writer,
2287 exiftag->exif_tag, value * METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
2291 deserialize_speed (GstExifReader * exif_reader,
2292 GstByteReader * reader, const GstExifTagMatch * exiftag,
2293 GstExifTagData * tagdata)
2295 GstExifTagData next_tagdata = { 0, };
2296 gdouble multiplier = 1;
2299 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2302 if (exiftag->complementary_tag == tagdata->tag) {
2303 if (tagdata->offset_as_data[0] == 'K') {
2304 multiplier = KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
2305 } else if (tagdata->offset_as_data[0] == 'M') {
2306 multiplier = MILES_PER_HOUR_TO_METERS_PER_SECOND;
2307 } else if (tagdata->offset_as_data[0] == 'N') {
2308 multiplier = KNOTS_TO_METERS_PER_SECOND;
2310 GST_WARNING ("Invalid GPSSpeedRed %c", tagdata->offset_as_data[0]);
2314 GST_DEBUG ("No GPSSpeedRef, using default=K");
2315 multiplier = KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
2317 if (tagdata->tag == exiftag->exif_tag) {
2318 tagdata_copy (&next_tagdata, tagdata);
2322 /* now read the following tag that must be the exif_tag */
2323 if (next_tagdata.tag == 0) {
2324 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2325 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2328 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2332 if (exiftag->exif_tag != next_tagdata.tag) {
2333 GST_WARNING ("Unexpected tag");
2337 /* read the remaining tag entry data */
2338 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2347 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2348 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2352 if (next_tagdata.count != 1) {
2353 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2354 next_tagdata.tag_type, next_tagdata.count);
2358 parse_exif_rational_tag (exif_reader,
2359 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, multiplier,
2365 GST_WARNING ("Failed to read fields from buffer (too short?)");
2370 serialize_shutter_speed (GstExifWriter * writer, const GstTagList * taglist,
2371 const GstExifTagMatch * exiftag)
2373 const GValue *value = NULL;
2376 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
2378 GST_WARNING ("Failed to get shutter speed from from tag list");
2381 gst_util_fraction_to_double (gst_value_get_fraction_numerator (value),
2382 gst_value_get_fraction_denominator (value), &num);
2387 num = -log (num) / M_LN2;
2391 gst_exif_writer_write_signed_rational_tag_from_double (writer,
2392 exiftag->exif_tag, num);
2396 deserialize_shutter_speed (GstExifReader * exif_reader,
2397 GstByteReader * reader, const GstExifTagMatch * exiftag,
2398 GstExifTagData * tagdata)
2400 gint32 frac_n, frac_d;
2402 GValue value = { 0 };
2404 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2407 if (!exif_reader_read_rational_tag (exif_reader, tagdata->count,
2408 tagdata->offset, TRUE, &frac_n, &frac_d))
2411 gst_util_fraction_to_double (frac_n, frac_d, &d);
2413 gst_util_double_to_fraction (d, &frac_n, &frac_d);
2415 g_value_init (&value, GST_TYPE_FRACTION);
2416 gst_value_set_fraction (&value, frac_n, frac_d);
2417 gst_tag_list_add_value (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2418 exiftag->gst_tag, &value);
2419 g_value_unset (&value);
2425 serialize_aperture_value (GstExifWriter * writer, const GstTagList * taglist,
2426 const GstExifTagMatch * exiftag)
2430 if (!gst_tag_list_get_double_index (taglist, exiftag->gst_tag, 0, &num)) {
2431 GST_WARNING ("Failed to get focal ratio from from tag list");
2435 num = 2 * log2 (num);
2437 num = 2 * (log (num) / M_LN2);
2441 gst_exif_writer_write_rational_tag_from_double (writer,
2442 exiftag->exif_tag, num);
2446 deserialize_aperture_value (GstExifReader * exif_reader,
2447 GstByteReader * reader, const GstExifTagMatch * exiftag,
2448 GstExifTagData * tagdata)
2450 gint32 frac_n, frac_d;
2453 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2456 if (!exif_reader_read_rational_tag (exif_reader, tagdata->count,
2457 tagdata->offset, FALSE, &frac_n, &frac_d))
2460 gst_util_fraction_to_double (frac_n, frac_d, &d);
2463 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2464 exiftag->gst_tag, d, NULL);
2470 serialize_sensitivity_type (GstExifWriter * writer, const GstTagList * taglist,
2471 const GstExifTagMatch * exiftag)
2473 /* we only support ISOSpeed as the sensitivity type (3) */
2474 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, 3);
2478 deserialize_sensitivity_type (GstExifReader * exif_reader,
2479 GstByteReader * reader, const GstExifTagMatch * exiftag,
2480 GstExifTagData * tagdata)
2482 GstExifTagData *sensitivity = NULL;
2485 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2486 type_data = GST_READ_UINT16_LE (tagdata->offset_as_data);
2488 type_data = GST_READ_UINT16_BE (tagdata->offset_as_data);
2491 if (type_data != 3) {
2492 GST_WARNING ("We only support SensitivityType=3");
2496 /* check the pending tags for the PhotographicSensitivity tag */
2498 gst_exif_reader_get_pending_tag (exif_reader,
2499 EXIF_TAG_PHOTOGRAPHIC_SENSITIVITY);
2500 if (sensitivity == NULL) {
2501 GST_WARNING ("PhotographicSensitivity tag not found");
2505 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2508 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2509 GST_TAG_CAPTURING_ISO_SPEED, sensitivity->offset_as_data, NULL);
2515 serialize_flash (GstExifWriter * writer, const GstTagList * taglist,
2516 const GstExifTagMatch * exiftag)
2518 gboolean flash_fired;
2519 const gchar *flash_mode;
2520 guint16 tagvalue = 0;
2522 if (!gst_tag_list_get_boolean_index (taglist, exiftag->gst_tag, 0,
2524 GST_WARNING ("Failed to get flash fired from from tag list");
2531 if (gst_tag_list_peek_string_index (taglist, GST_TAG_CAPTURING_FLASH_MODE, 0,
2534 if (strcmp (flash_mode, "auto") == 0) {
2536 } else if (strcmp (flash_mode, "always") == 0) {
2538 } else if (strcmp (flash_mode, "never") == 0) {
2542 tagvalue = tagvalue | (mode << 3);
2544 GST_DEBUG ("flash-mode not available");
2547 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, tagvalue);
2551 deserialize_flash (GstExifReader * exif_reader,
2552 GstByteReader * reader, const GstExifTagMatch * exiftag,
2553 GstExifTagData * tagdata)
2557 const gchar *mode_str = NULL;
2559 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2562 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2563 value = GST_READ_UINT16_LE (tagdata->offset_as_data);
2565 value = GST_READ_UINT16_BE (tagdata->offset_as_data);
2568 /* check flash fired */
2570 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2571 GST_TAG_CAPTURING_FLASH_FIRED, TRUE, NULL);
2573 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2574 GST_TAG_CAPTURING_FLASH_FIRED, FALSE, NULL);
2577 mode = (value >> 3) & 0x3;
2579 mode_str = "always";
2580 } else if (mode == 2) {
2582 } else if (mode == 3) {
2587 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2588 GST_TAG_CAPTURING_FLASH_MODE, mode_str, NULL);
2594 deserialize_resolution (GstExifReader * exif_reader,
2595 GstByteReader * reader, const GstExifTagMatch * exiftag,
2596 GstExifTagData * tagdata)
2598 GstExifTagData *xres = NULL;
2599 GstExifTagData *yres = NULL;
2603 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2604 unit = GST_READ_UINT16_LE (tagdata->offset_as_data);
2606 unit = GST_READ_UINT16_BE (tagdata->offset_as_data);
2614 multiplier = 1 / 2.54;
2617 GST_WARNING ("Invalid resolution unit, ignoring PPI tags");
2621 xres = gst_exif_reader_get_pending_tag (exif_reader, EXIF_TAG_XRESOLUTION);
2623 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_HORIZONTAL_PPI,
2624 xres->count, xres->offset, multiplier, FALSE);
2626 yres = gst_exif_reader_get_pending_tag (exif_reader, EXIF_TAG_YRESOLUTION);
2628 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_VERTICAL_PPI,
2629 yres->count, yres->offset, multiplier, FALSE);
2636 serialize_scene_type (GstExifWriter * writer, const GstTagList * taglist,
2637 const GstExifTagMatch * exiftag)
2642 if (gst_tag_list_peek_string_index (taglist, GST_TAG_CAPTURING_SOURCE, 0,
2644 if (strcmp (str, "dsc") == 0) {
2650 write_exif_undefined_tag (writer, exiftag->exif_tag, &value, 1);
2654 deserialize_scene_type (GstExifReader * exif_reader,
2655 GstByteReader * reader, const GstExifTagMatch * exiftag,
2656 GstExifTagData * tagdata)
2660 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2663 value = GST_READ_UINT8 (tagdata->offset_as_data);
2666 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2667 GST_TAG_CAPTURING_SOURCE, "dsc", NULL);
2674 deserialize_add_to_pending_tags (GstExifReader * exif_reader,
2675 GstByteReader * reader, const GstExifTagMatch * exiftag,
2676 GstExifTagData * tagdata)
2678 GST_LOG ("Adding %s tag in exif 0x%x to pending tags", exiftag->gst_tag,
2681 /* add it to the pending tags, as we can only parse it when we find the
2682 * SensitivityType tag */
2683 gst_exif_reader_add_pending_tag (exif_reader, tagdata);
2687 #undef EXIF_SERIALIZATION_FUNC
2688 #undef EXIF_DESERIALIZATION_FUNC
2689 #undef EXIF_SERIALIZATION_DESERIALIZATION_FUNC