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
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 GstBuffer * buf, guint32 base_offset)
459 reader->taglist = gst_tag_list_new ();
460 reader->buffer = buf;
461 reader->base_offset = base_offset;
462 reader->byte_order = byte_order;
463 reader->pending_tags = NULL;
464 if (reader->byte_order != G_LITTLE_ENDIAN &&
465 reader->byte_order != G_BIG_ENDIAN) {
466 GST_WARNING ("Unexpected byte order %d, using system default: %d",
467 reader->byte_order, G_BYTE_ORDER);
468 reader->byte_order = G_BYTE_ORDER;
473 gst_exif_reader_add_pending_tag (GstExifReader * reader, GstExifTagData * data)
475 GstExifTagData *copy;
477 copy = g_slice_new (GstExifTagData);
478 memcpy (copy, data, sizeof (GstExifTagData));
480 reader->pending_tags = g_slist_prepend (reader->pending_tags, copy);
483 static GstExifTagData *
484 gst_exif_reader_get_pending_tag (GstExifReader * reader, gint tagid)
488 for (walker = reader->pending_tags; walker; walker = g_slist_next (walker)) {
489 GstExifTagData *data = (GstExifTagData *) walker->data;
490 if (data->tag == tagid)
498 gst_exif_reader_reset (GstExifReader * reader, gboolean return_taglist)
500 GstTagList *ret = NULL;
503 for (walker = reader->pending_tags; walker; walker = g_slist_next (walker)) {
504 GstExifTagData *data = (GstExifTagData *) walker->data;
506 g_slice_free (GstExifTagData, data);
508 g_slist_free (reader->pending_tags);
510 if (return_taglist) {
511 ret = reader->taglist;
512 reader->taglist = NULL;
515 if (reader->taglist) {
516 gst_tag_list_free (reader->taglist);
522 /* GstExifWriter functions */
525 gst_exif_writer_init (GstExifWriter * writer, gint byte_order)
527 gst_byte_writer_init (&writer->tagwriter);
528 gst_byte_writer_init (&writer->datawriter);
530 writer->byte_order = byte_order;
531 writer->tags_total = 0;
532 if (writer->byte_order != G_LITTLE_ENDIAN &&
533 writer->byte_order != G_BIG_ENDIAN) {
534 GST_WARNING ("Unexpected byte order %d, using system default: %d",
535 writer->byte_order, G_BYTE_ORDER);
536 writer->byte_order = G_BYTE_ORDER;
541 gst_exif_writer_reset_and_get_buffer (GstExifWriter * writer)
546 header = gst_byte_writer_reset_and_get_buffer (&writer->tagwriter);
547 data = gst_byte_writer_reset_and_get_buffer (&writer->datawriter);
549 return gst_buffer_join (header, data);
553 * Given the exif tag with the passed id, returns the map index of the tag
554 * corresponding to it. If use_complementary is true, then the complementary
555 * are also used in the search.
557 * Returns -1 if not found
560 exif_tag_map_find_reverse (guint16 exif_tag, const GstExifTagMatch * tag_map,
561 gboolean use_complementary)
565 for (i = 0; tag_map[i].exif_tag != 0; i++) {
566 if (exif_tag == tag_map[i].exif_tag || (use_complementary &&
567 exif_tag == tag_map[i].complementary_tag)) {
575 gst_tag_list_has_ifd_tags (const GstTagList * taglist,
576 const GstExifTagMatch * tag_map)
580 for (i = 0; tag_map[i].exif_tag != 0; i++) {
581 if (tag_map[i].gst_tag == NULL) {
582 if (tag_map[i].exif_tag == EXIF_GPS_IFD_TAG &&
583 gst_tag_list_has_ifd_tags (taglist, tag_map_gps))
585 if (tag_map[i].exif_tag == EXIF_IFD_TAG &&
586 gst_tag_list_has_ifd_tags (taglist, tag_map_exif))
591 if (gst_tag_list_get_value_index (taglist, tag_map[i].gst_tag, 0)) {
599 * Writes the tag entry.
601 * The tag entry is the tag id, the tag type,
602 * the count and the offset.
604 * The offset is the on the amount of data writen so far, as one
605 * can't predict the total bytes that the tag entries will take.
606 * This means those fields requires being updated later.
609 gst_exif_writer_write_tag_header (GstExifWriter * writer,
610 guint16 exif_tag, guint16 exif_type, guint32 count, guint32 offset,
613 GST_DEBUG ("Writing tag entry: id %x, type %u, count %u, offset %u",
614 exif_tag, exif_type, count, offset);
616 if (writer->byte_order == G_LITTLE_ENDIAN) {
617 gst_byte_writer_put_uint16_le (&writer->tagwriter, exif_tag);
618 gst_byte_writer_put_uint16_le (&writer->tagwriter, exif_type);
619 gst_byte_writer_put_uint32_le (&writer->tagwriter, count);
620 gst_byte_writer_put_uint32_le (&writer->tagwriter, offset);
621 } else if (writer->byte_order == G_BIG_ENDIAN) {
622 gst_byte_writer_put_uint16_be (&writer->tagwriter, exif_tag);
623 gst_byte_writer_put_uint16_be (&writer->tagwriter, exif_type);
624 gst_byte_writer_put_uint32_be (&writer->tagwriter, count);
626 gst_byte_writer_put_uint32_le (&writer->tagwriter, offset);
628 gst_byte_writer_put_uint32_be (&writer->tagwriter, offset);
631 g_assert_not_reached ();
634 writer->tags_total++;
638 gst_exif_writer_write_rational_data (GstExifWriter * writer, guint32 frac_n,
641 if (writer->byte_order == G_LITTLE_ENDIAN) {
642 gst_byte_writer_put_uint32_le (&writer->datawriter, frac_n);
643 gst_byte_writer_put_uint32_le (&writer->datawriter, frac_d);
645 gst_byte_writer_put_uint32_be (&writer->datawriter, frac_n);
646 gst_byte_writer_put_uint32_be (&writer->datawriter, frac_d);
651 gst_exif_writer_write_signed_rational_data (GstExifWriter * writer,
652 gint32 frac_n, gint32 frac_d)
654 if (writer->byte_order == G_LITTLE_ENDIAN) {
655 gst_byte_writer_put_int32_le (&writer->datawriter, frac_n);
656 gst_byte_writer_put_int32_le (&writer->datawriter, frac_d);
658 gst_byte_writer_put_int32_be (&writer->datawriter, frac_n);
659 gst_byte_writer_put_int32_be (&writer->datawriter, frac_d);
664 gst_exif_writer_write_rational_tag (GstExifWriter * writer,
665 guint16 tag, guint32 frac_n, guint32 frac_d)
667 guint32 offset = gst_byte_writer_get_size (&writer->datawriter);
669 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_RATIONAL,
672 gst_exif_writer_write_rational_data (writer, frac_n, frac_d);
676 gst_exif_writer_write_signed_rational_tag (GstExifWriter * writer,
677 guint16 tag, gint32 frac_n, gint32 frac_d)
679 guint32 offset = gst_byte_writer_get_size (&writer->datawriter);
681 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_SRATIONAL,
684 gst_exif_writer_write_signed_rational_data (writer, frac_n, frac_d);
688 gst_exif_writer_write_rational_tag_from_double (GstExifWriter * writer,
689 guint16 tag, gdouble value)
694 gst_util_double_to_fraction (value, &frac_n, &frac_d);
696 gst_exif_writer_write_rational_tag (writer, tag, frac_n, frac_d);
700 gst_exif_writer_write_signed_rational_tag_from_double (GstExifWriter * writer,
701 guint16 tag, gdouble value)
706 gst_util_double_to_fraction (value, &frac_n, &frac_d);
708 gst_exif_writer_write_signed_rational_tag (writer, tag, frac_n, frac_d);
712 gst_exif_writer_write_byte_tag (GstExifWriter * writer, guint16 tag,
717 GST_WRITE_UINT8 ((guint8 *) & offset, value);
718 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_BYTE,
723 gst_exif_writer_write_short_tag (GstExifWriter * writer, guint16 tag,
728 if (writer->byte_order == G_LITTLE_ENDIAN) {
729 GST_WRITE_UINT16_LE ((guint8 *) & offset, value);
731 GST_WRITE_UINT16_BE ((guint8 *) & offset, value);
734 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_SHORT,
739 gst_exif_writer_write_long_tag (GstExifWriter * writer, guint16 tag,
743 if (writer->byte_order == G_LITTLE_ENDIAN) {
744 GST_WRITE_UINT32_LE ((guint8 *) & offset, value);
746 GST_WRITE_UINT32_BE ((guint8 *) & offset, value);
749 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_LONG,
755 write_exif_undefined_tag (GstExifWriter * writer, guint16 tag,
756 const guint8 * data, gint size)
761 /* we only use the data offset here, later we add up the
762 * resulting tag headers offset and the base offset */
763 offset = gst_byte_writer_get_size (&writer->datawriter);
764 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_UNDEFINED,
765 size, offset, FALSE);
766 gst_byte_writer_put_data (&writer->datawriter, data, size);
768 /* small enough to go in the offset */
769 memcpy ((guint8 *) & offset, data, size);
770 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_UNDEFINED,
776 write_exif_ascii_tag (GstExifWriter * writer, guint16 tag, const gchar * str)
781 size = strlen (str) + 1;
784 /* we only use the data offset here, later we add up the
785 * resulting tag headers offset and the base offset */
786 offset = gst_byte_writer_get_size (&writer->datawriter);
787 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_ASCII,
788 size, offset, FALSE);
789 gst_byte_writer_put_string (&writer->datawriter, str);
791 /* small enough to go in the offset */
792 memcpy ((guint8 *) & offset, str, size);
793 gst_exif_writer_write_tag_header (writer, tag, EXIF_TYPE_ASCII,
799 write_exif_ascii_tag_from_taglist (GstExifWriter * writer,
800 const GstTagList * taglist, const GstExifTagMatch * exiftag)
803 gboolean cleanup = FALSE;
805 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
808 /* FIXME support this by serializing them with a ','? */
809 GST_WARNING ("Multiple string tags not supported yet");
813 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
815 /* do some conversion if needed */
816 switch (G_VALUE_TYPE (value)) {
818 str = (gchar *) g_value_get_string (value);
821 if (G_VALUE_TYPE (value) == GST_TYPE_DATE_TIME) {
822 GstDateTime *dt = (GstDateTime *) g_value_get_boxed (value);
825 GST_WARNING ("NULL datetime received");
829 str = g_strdup_printf ("%04d:%02d:%02d %02d:%02d:%02d",
830 gst_date_time_get_year (dt), gst_date_time_get_month (dt),
831 gst_date_time_get_day (dt), gst_date_time_get_hour (dt),
832 gst_date_time_get_minute (dt), gst_date_time_get_second (dt));
836 GST_WARNING ("Conversion from %s to ascii string not supported",
837 G_VALUE_TYPE_NAME (value));
845 write_exif_ascii_tag (writer, exiftag->exif_tag, str);
851 write_exif_undefined_tag_from_taglist (GstExifWriter * writer,
852 const GstTagList * taglist, const GstExifTagMatch * exiftag)
857 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
858 GstBuffer *buf = NULL;
861 GST_WARNING ("Only the first item in the taglist will be serialized");
865 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
867 /* do some conversion if needed */
868 switch (G_VALUE_TYPE (value)) {
870 data = (guint8 *) g_value_get_string (value);
871 size = strlen ((gchar *) data); /* no need to +1, undefined doesn't require it */
874 if (G_VALUE_TYPE (value) == GST_TYPE_BUFFER) {
875 buf = gst_value_get_buffer (value);
876 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
878 GST_WARNING ("Conversion from %s to raw data not supported",
879 G_VALUE_TYPE_NAME (value));
885 write_exif_undefined_tag (writer, exiftag->exif_tag, data, size);
888 gst_buffer_unmap (buf, data, size);
892 write_exif_rational_tag_from_taglist (GstExifWriter * writer,
893 const GstTagList * taglist, const GstExifTagMatch * exiftag)
897 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
900 GST_WARNING ("Only the first item in the taglist will be serialized");
904 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
906 /* do some conversion if needed */
907 switch (G_VALUE_TYPE (value)) {
909 num = g_value_get_double (value);
910 gst_exif_writer_write_rational_tag_from_double (writer, exiftag->exif_tag,
914 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
915 gst_exif_writer_write_rational_tag (writer, exiftag->exif_tag,
916 gst_value_get_fraction_numerator (value),
917 gst_value_get_fraction_denominator (value));
919 GST_WARNING ("Conversion from %s to rational not supported",
920 G_VALUE_TYPE_NAME (value));
927 write_exif_signed_rational_tag_from_taglist (GstExifWriter * writer,
928 const GstTagList * taglist, const GstExifTagMatch * exiftag)
932 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
935 GST_WARNING ("Only the first item in the taglist will be serialized");
939 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
941 /* do some conversion if needed */
942 switch (G_VALUE_TYPE (value)) {
944 num = g_value_get_double (value);
945 gst_exif_writer_write_signed_rational_tag_from_double (writer,
946 exiftag->exif_tag, num);
949 if (G_VALUE_TYPE (value) == GST_TYPE_FRACTION) {
950 gst_exif_writer_write_signed_rational_tag (writer, exiftag->exif_tag,
951 gst_value_get_fraction_numerator (value),
952 gst_value_get_fraction_denominator (value));
954 GST_WARNING ("Conversion from %s to signed rational not supported",
955 G_VALUE_TYPE_NAME (value));
962 write_exif_integer_tag_from_taglist (GstExifWriter * writer,
963 const GstTagList * taglist, const GstExifTagMatch * exiftag)
967 gint tag_size = gst_tag_list_get_tag_size (taglist, exiftag->gst_tag);
970 GST_WARNING ("Only the first item in the taglist will be serialized");
974 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
976 /* do some conversion if needed */
977 switch (G_VALUE_TYPE (value)) {
979 num = g_value_get_int (value);
982 GST_WARNING ("Conversion from %s to int not supported",
983 G_VALUE_TYPE_NAME (value));
987 switch (exiftag->exif_type) {
989 gst_exif_writer_write_long_tag (writer, exiftag->exif_tag, num);
991 case EXIF_TYPE_SHORT:
992 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, num);
1000 write_exif_tag_from_taglist (GstExifWriter * writer, const GstTagList * taglist,
1001 const GstExifTagMatch * exiftag)
1003 GST_DEBUG ("Writing tag %s", exiftag->gst_tag);
1005 /* check for special handling */
1006 if (exiftag->serialize) {
1007 exiftag->serialize (writer, taglist, exiftag);
1011 switch (exiftag->exif_type) {
1012 case EXIF_TYPE_ASCII:
1013 write_exif_ascii_tag_from_taglist (writer, taglist, exiftag);
1015 case EXIF_TYPE_UNDEFINED:
1016 write_exif_undefined_tag_from_taglist (writer, taglist, exiftag);
1018 case EXIF_TYPE_RATIONAL:
1019 write_exif_rational_tag_from_taglist (writer, taglist, exiftag);
1021 case EXIF_TYPE_SRATIONAL:
1022 write_exif_signed_rational_tag_from_taglist (writer, taglist, exiftag);
1024 case EXIF_TYPE_LONG:
1025 case EXIF_TYPE_SHORT:
1026 write_exif_integer_tag_from_taglist (writer, taglist, exiftag);
1029 GST_WARNING ("Unhandled tag type %d", exiftag->exif_type);
1034 tagdata_copy (GstExifTagData * to, const GstExifTagData * from)
1036 to->tag = from->tag;
1037 to->tag_type = from->tag_type;
1038 to->count = from->count;
1039 to->offset = from->offset;
1040 to->offset_as_data = from->offset_as_data;
1044 gst_exif_tag_rewrite_offsets (GstByteWriter * writer, gint byte_order,
1045 guint32 offset, gint num_tags, GstByteWriter * inner_ifds_data)
1047 GstByteReader *reader;
1049 guint16 aux = G_MAXUINT16;
1051 GST_LOG ("Rewriting tag entries offsets");
1053 reader = (GstByteReader *) writer;
1055 if (num_tags == -1) {
1056 if (byte_order == G_LITTLE_ENDIAN) {
1057 gst_byte_reader_get_uint16_le (reader, &aux);
1059 gst_byte_reader_get_uint16_be (reader, &aux);
1061 if (aux == G_MAXUINT16) {
1062 GST_WARNING ("Failed to read number of tags, won't rewrite offsets");
1065 num_tags = (gint) aux;
1068 g_return_if_fail (num_tags != -1);
1070 GST_DEBUG ("number of tags %d", num_tags);
1072 for (i = 0; i < num_tags; i++) {
1074 guint32 cur_offset = 0;
1079 g_assert (gst_byte_writer_get_pos (writer) <
1080 gst_byte_writer_get_size (writer));
1083 if (byte_order == G_LITTLE_ENDIAN) {
1084 if (!gst_byte_reader_get_uint16_le (reader, &tag_id))
1086 if (!gst_byte_reader_get_uint16_le (reader, &type))
1088 if (!gst_byte_reader_get_uint32_le (reader, &count))
1091 if (!gst_byte_reader_get_uint16_be (reader, &tag_id))
1093 if (!gst_byte_reader_get_uint16_be (reader, &type))
1095 if (!gst_byte_reader_get_uint32_be (reader, &count))
1099 GST_LOG ("Parsed tag %x of type %u and count %u", tag_id, type, count);
1102 case EXIF_TYPE_BYTE:
1103 case EXIF_TYPE_ASCII:
1104 case EXIF_TYPE_UNDEFINED:
1107 case EXIF_TYPE_SHORT:
1108 byte_size = count * 2; /* 2 bytes */
1110 case EXIF_TYPE_LONG:
1111 case EXIF_TYPE_SLONG:
1112 byte_size = count * 4; /* 4 bytes */
1114 case EXIF_TYPE_RATIONAL:
1115 case EXIF_TYPE_SRATIONAL:
1116 byte_size = count * 8; /* 8 bytes */
1119 g_assert_not_reached ();
1123 /* adjust the offset if needed */
1124 if (byte_size > 4 || tag_id == EXIF_GPS_IFD_TAG || tag_id == EXIF_IFD_TAG) {
1125 if (byte_order == G_LITTLE_ENDIAN) {
1126 if (gst_byte_reader_peek_uint32_le (reader, &cur_offset)) {
1127 gst_byte_writer_put_uint32_le (writer, cur_offset + offset);
1130 if (gst_byte_reader_peek_uint32_be (reader, &cur_offset)) {
1131 gst_byte_writer_put_uint32_be (writer, cur_offset + offset);
1134 GST_DEBUG ("Rewriting tag offset from %u to (%u + %u) %u",
1135 cur_offset, cur_offset, offset, cur_offset + offset);
1137 if ((tag_id == EXIF_GPS_IFD_TAG || tag_id == EXIF_IFD_TAG) &&
1138 inner_ifds_data != NULL) {
1139 /* needs special handling */
1140 if (!gst_byte_writer_set_pos (inner_ifds_data, cur_offset)) {
1141 GST_WARNING ("Failed to position writer to rewrite inner ifd "
1146 gst_exif_tag_rewrite_offsets (inner_ifds_data, byte_order, offset, -1,
1150 gst_byte_reader_skip (reader, 4);
1151 GST_DEBUG ("No need to rewrite tag offset");
1154 GST_LOG ("Done rewriting offsets");
1158 parse_exif_ascii_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1159 guint32 count, guint32 offset, const guint8 * offset_as_data)
1163 guint32 real_offset;
1169 if (offset < reader->base_offset) {
1170 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1171 reader->base_offset);
1175 real_offset = offset - reader->base_offset;
1177 data = gst_buffer_map (reader->buffer, &size, NULL, GST_MAP_READ);
1178 if (real_offset >= size) {
1179 GST_WARNING ("Invalid offset %u for buffer of size %u, not adding tag %s",
1180 real_offset, size, tag->gst_tag);
1181 gst_buffer_unmap (reader->buffer, data, size);
1185 str = g_strndup ((gchar *) (data + real_offset), count);
1186 gst_buffer_unmap (reader->buffer, data, size);
1188 str = g_strndup ((gchar *) offset_as_data, count);
1191 tagtype = gst_tag_get_type (tag->gst_tag);
1192 if (tagtype == GST_TYPE_DATE_TIME) {
1193 gint year = 0, month = 1, day = 1, hour = 0, minute = 0, second = 0;
1195 if (sscanf (str, "%04d:%02d:%02d %02d:%02d:%02d", &year, &month, &day,
1196 &hour, &minute, &second) > 0) {
1199 d = gst_date_time_new_local_time (year, month, day, hour, minute, second);
1200 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE,
1201 tag->gst_tag, d, NULL);
1202 gst_date_time_unref (d);
1204 GST_WARNING ("Failed to parse %s into a datetime tag", str);
1206 } else if (tagtype == G_TYPE_STRING) {
1207 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag, str,
1210 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1217 parse_exif_long_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1218 guint32 count, guint32 offset, const guint8 * offset_as_data)
1223 GST_WARNING ("Long tags with more than one value are not supported");
1227 tagtype = gst_tag_get_type (tag->gst_tag);
1228 if (tagtype == G_TYPE_INT) {
1229 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag,
1232 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1239 parse_exif_undefined_tag (GstExifReader * reader, const GstExifTagMatch * tag,
1240 guint32 count, guint32 offset, const guint8 * offset_as_data)
1244 guint32 real_offset;
1250 if (offset < reader->base_offset) {
1251 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1252 reader->base_offset);
1256 real_offset = offset - reader->base_offset;
1258 bdata = gst_buffer_map (reader->buffer, &bsize, NULL, GST_MAP_READ);
1260 if (real_offset >= bsize) {
1261 GST_WARNING ("Invalid offset %u for buffer of size %u, not adding tag %s",
1262 real_offset, bsize, tag->gst_tag);
1263 gst_buffer_unmap (reader->buffer, bdata, bsize);
1267 /* +1 because it could be a string without the \0 */
1268 data = malloc (sizeof (guint8) * count + 1);
1269 memcpy (data, bdata + real_offset, count);
1272 gst_buffer_unmap (reader->buffer, bdata, bsize);
1274 data = malloc (sizeof (guint8) * count + 1);
1275 memcpy (data, (guint8 *) offset_as_data, count);
1279 tagtype = gst_tag_get_type (tag->gst_tag);
1280 if (tagtype == GST_TYPE_BUFFER) {
1283 buf = gst_buffer_new ();
1284 gst_buffer_take_memory (buf,
1285 gst_memory_new_wrapped (0, data, g_free, count, 0, count));
1288 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_APPEND, tag->gst_tag,
1291 gst_buffer_unref (buf);
1292 } else if (tagtype == G_TYPE_STRING) {
1293 gst_tag_list_add (reader->taglist, GST_TAG_MERGE_REPLACE, tag->gst_tag,
1296 GST_WARNING ("No parsing function associated to %x(%s)", tag->exif_tag,
1303 exif_reader_read_rational_tag (GstExifReader * exif_reader,
1304 guint32 count, guint32 offset, gboolean is_signed,
1305 gint32 * _frac_n, gint32 * _frac_d)
1307 GstByteReader data_reader;
1308 guint32 real_offset;
1315 GST_WARNING ("Rationals with multiple entries are not supported");
1317 if (offset < exif_reader->base_offset) {
1318 GST_WARNING ("Offset is smaller (%u) than base offset (%u)", offset,
1319 exif_reader->base_offset);
1323 real_offset = offset - exif_reader->base_offset;
1325 data = gst_buffer_map (exif_reader->buffer, &size, NULL, GST_MAP_READ);
1327 if (real_offset >= size) {
1328 GST_WARNING ("Invalid offset %u for buffer of size %u", real_offset, size);
1332 gst_byte_reader_init (&data_reader, data, size);
1333 if (!gst_byte_reader_set_pos (&data_reader, real_offset))
1337 guint32 aux_n = 0, aux_d = 0;
1338 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1339 if (!gst_byte_reader_get_uint32_le (&data_reader, &aux_n) ||
1340 !gst_byte_reader_get_uint32_le (&data_reader, &aux_d))
1343 if (!gst_byte_reader_get_uint32_be (&data_reader, &aux_n) ||
1344 !gst_byte_reader_get_uint32_be (&data_reader, &aux_d))
1347 frac_n = (gint32) aux_n;
1348 frac_d = (gint32) aux_d;
1350 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1351 if (!gst_byte_reader_get_int32_le (&data_reader, &frac_n) ||
1352 !gst_byte_reader_get_int32_le (&data_reader, &frac_d))
1355 if (!gst_byte_reader_get_int32_be (&data_reader, &frac_n) ||
1356 !gst_byte_reader_get_int32_be (&data_reader, &frac_d))
1366 gst_buffer_unmap (exif_reader->buffer, data, size);
1371 GST_WARNING ("Failed to read from byte reader. (Buffer too short?)");
1372 gst_buffer_unmap (exif_reader->buffer, data, size);
1377 parse_exif_rational_tag (GstExifReader * exif_reader,
1378 const gchar * gst_tag, guint32 count, guint32 offset, gdouble multiplier,
1386 GST_DEBUG ("Reading fraction for tag %s...", gst_tag);
1387 if (!exif_reader_read_rational_tag (exif_reader, count, offset, is_signed,
1390 GST_DEBUG ("Read fraction for tag %s: %d/%d", gst_tag, frac_n, frac_d);
1392 type = gst_tag_get_type (gst_tag);
1395 gst_util_fraction_to_double (frac_n, frac_d, &value);
1396 value *= multiplier;
1397 GST_DEBUG ("Adding %s tag: %lf", gst_tag, value);
1398 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE, gst_tag,
1402 if (type == GST_TYPE_FRACTION) {
1403 GValue fraction = { 0 };
1405 g_value_init (&fraction, GST_TYPE_FRACTION);
1406 gst_value_set_fraction (&fraction, frac_n * multiplier, frac_d);
1407 gst_tag_list_add_value (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
1408 gst_tag, &fraction);
1409 g_value_unset (&fraction);
1411 GST_WARNING ("Can't convert from fraction into %s", g_type_name (type));
1418 write_exif_ifd (const GstTagList * taglist, gboolean byte_order,
1419 guint32 base_offset, const GstExifTagMatch * tag_map)
1421 GstExifWriter writer;
1424 GST_DEBUG ("Formatting taglist %p as exif buffer. Byte order: %d, "
1425 "base_offset: %u", taglist, byte_order, base_offset);
1427 g_assert (byte_order == G_LITTLE_ENDIAN || byte_order == G_BIG_ENDIAN);
1429 if (!gst_tag_list_has_ifd_tags (taglist, tag_map)) {
1430 GST_DEBUG ("No tags for this ifd");
1434 gst_exif_writer_init (&writer, byte_order);
1436 /* write tag number as 0 */
1437 gst_byte_writer_put_uint16_le (&writer.tagwriter, 0);
1439 /* write both tag headers and data
1440 * in ascending id order */
1442 for (i = 0; tag_map[i].exif_tag != 0; i++) {
1444 /* special cases have NULL gst tag */
1445 if (tag_map[i].gst_tag == NULL) {
1446 GstBuffer *inner_ifd = NULL;
1447 const GstExifTagMatch *inner_tag_map = NULL;
1449 GST_LOG ("Inner ifd tag: %x", tag_map[i].exif_tag);
1451 if (tag_map[i].exif_tag == EXIF_GPS_IFD_TAG) {
1452 inner_tag_map = tag_map_gps;
1453 } else if (tag_map[i].exif_tag == EXIF_IFD_TAG) {
1454 inner_tag_map = tag_map_exif;
1455 } else if (tag_map[i].exif_tag == EXIF_VERSION_TAG) {
1456 /* special case where we write the exif version */
1457 write_exif_undefined_tag (&writer, EXIF_VERSION_TAG, (guint8 *) "0230",
1459 } else if (tag_map[i].exif_tag == EXIF_FLASHPIX_VERSION_TAG) {
1460 /* special case where we write the flashpix version */
1461 write_exif_undefined_tag (&writer, EXIF_FLASHPIX_VERSION_TAG,
1462 (guint8 *) "0100", 4);
1465 if (inner_tag_map) {
1466 /* base offset and tagheader size are added when rewriting offset */
1467 inner_ifd = write_exif_ifd (taglist, byte_order,
1468 gst_byte_writer_get_size (&writer.datawriter), inner_tag_map);
1475 GST_DEBUG ("Adding inner ifd: %x", tag_map[i].exif_tag);
1476 gst_exif_writer_write_tag_header (&writer, tag_map[i].exif_tag,
1478 gst_byte_writer_get_size (&writer.datawriter), FALSE);
1480 data = gst_buffer_map (inner_ifd, &size, NULL, GST_MAP_READ);
1481 gst_byte_writer_put_data (&writer.datawriter, data, size);
1482 gst_buffer_unmap (inner_ifd, data, size);
1483 gst_buffer_unref (inner_ifd);
1488 GST_LOG ("Checking tag %s", tag_map[i].gst_tag);
1489 if (gst_tag_list_get_value_index (taglist, tag_map[i].gst_tag, 0) == NULL)
1492 write_exif_tag_from_taglist (&writer, taglist, &tag_map[i]);
1495 /* Add the next IFD offset, we just set it to 0 because
1496 * there is no easy way to predict what it is going to be.
1497 * The user might rewrite the value if needed */
1498 gst_byte_writer_put_uint32_le (&writer.tagwriter, 0);
1500 /* write the number of tags */
1501 gst_byte_writer_set_pos (&writer.tagwriter, 0);
1502 if (writer.byte_order == G_LITTLE_ENDIAN)
1503 gst_byte_writer_put_uint16_le (&writer.tagwriter, writer.tags_total);
1505 gst_byte_writer_put_uint16_be (&writer.tagwriter, writer.tags_total);
1507 GST_DEBUG ("Number of tags rewriten to %d", writer.tags_total);
1509 /* now that we know the tag headers size, we can add the offsets */
1510 gst_exif_tag_rewrite_offsets (&writer.tagwriter, writer.byte_order,
1511 base_offset + gst_byte_writer_get_size (&writer.tagwriter),
1512 writer.tags_total, &writer.datawriter);
1514 return gst_exif_writer_reset_and_get_buffer (&writer);
1518 parse_exif_tag_header (GstByteReader * reader, gint byte_order,
1519 GstExifTagData * _tagdata)
1521 g_assert (_tagdata);
1523 /* read the fields */
1524 if (byte_order == G_LITTLE_ENDIAN) {
1525 if (!gst_byte_reader_get_uint16_le (reader, &_tagdata->tag) ||
1526 !gst_byte_reader_get_uint16_le (reader, &_tagdata->tag_type) ||
1527 !gst_byte_reader_get_uint32_le (reader, &_tagdata->count) ||
1528 !gst_byte_reader_get_data (reader, 4, &_tagdata->offset_as_data)) {
1531 _tagdata->offset = GST_READ_UINT32_LE (_tagdata->offset_as_data);
1533 if (!gst_byte_reader_get_uint16_be (reader, &_tagdata->tag) ||
1534 !gst_byte_reader_get_uint16_be (reader, &_tagdata->tag_type) ||
1535 !gst_byte_reader_get_uint32_be (reader, &_tagdata->count) ||
1536 !gst_byte_reader_get_data (reader, 4, &_tagdata->offset_as_data)) {
1539 _tagdata->offset = GST_READ_UINT32_BE (_tagdata->offset_as_data);
1546 parse_exif_ifd (GstExifReader * exif_reader, gint buf_offset,
1547 const GstExifTagMatch * tag_map)
1549 GstByteReader reader;
1550 guint16 entries = 0;
1555 g_return_val_if_fail (exif_reader->byte_order == G_LITTLE_ENDIAN
1556 || exif_reader->byte_order == G_BIG_ENDIAN, FALSE);
1558 data = gst_buffer_map (exif_reader->buffer, &size, NULL, GST_MAP_READ);
1560 gst_byte_reader_init (&reader, data, size);
1561 if (!gst_byte_reader_set_pos (&reader, buf_offset))
1562 goto invalid_offset;
1564 /* read the IFD entries number */
1565 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
1566 if (!gst_byte_reader_get_uint16_le (&reader, &entries))
1569 if (!gst_byte_reader_get_uint16_be (&reader, &entries))
1572 GST_DEBUG ("Read number of entries: %u", entries);
1574 /* iterate over the buffer and find the tags and stuff */
1575 for (i = 0; i < entries; i++) {
1576 GstExifTagData tagdata;
1579 GST_LOG ("Reading entry: %u", i);
1581 if (!parse_exif_tag_header (&reader, exif_reader->byte_order, &tagdata))
1584 GST_DEBUG ("Parsed tag: id 0x%x, type %u, count %u, offset %u (0x%x)"
1585 ", buf size: %u", tagdata.tag, tagdata.tag_type, tagdata.count,
1586 tagdata.offset, tagdata.offset, gst_byte_reader_get_size (&reader));
1588 map_index = exif_tag_map_find_reverse (tagdata.tag, tag_map, TRUE);
1589 if (map_index == -1) {
1590 GST_WARNING ("Unmapped exif tag: 0x%x", tagdata.tag);
1595 * inner ifd tags handling, errors processing those are being ignored
1596 * and we try to continue the parsing
1598 if (tagdata.tag == EXIF_GPS_IFD_TAG) {
1599 parse_exif_ifd (exif_reader,
1600 tagdata.offset - exif_reader->base_offset, tag_map_gps);
1604 if (tagdata.tag == EXIF_IFD_TAG) {
1605 parse_exif_ifd (exif_reader,
1606 tagdata.offset - exif_reader->base_offset, tag_map_exif);
1610 if (tagdata.tag == EXIF_VERSION_TAG ||
1611 tagdata.tag == EXIF_FLASHPIX_VERSION_TAG) {
1616 /* tags that need specialized deserialization */
1617 if (tag_map[map_index].deserialize) {
1618 i += tag_map[map_index].deserialize (exif_reader, &reader,
1619 &tag_map[map_index], &tagdata);
1623 switch (tagdata.tag_type) {
1624 case EXIF_TYPE_ASCII:
1625 parse_exif_ascii_tag (exif_reader, &tag_map[map_index],
1626 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1628 case EXIF_TYPE_RATIONAL:
1629 parse_exif_rational_tag (exif_reader, tag_map[map_index].gst_tag,
1630 tagdata.count, tagdata.offset, 1, FALSE);
1632 case EXIF_TYPE_SRATIONAL:
1633 parse_exif_rational_tag (exif_reader, tag_map[map_index].gst_tag,
1634 tagdata.count, tagdata.offset, 1, TRUE);
1636 case EXIF_TYPE_UNDEFINED:
1637 parse_exif_undefined_tag (exif_reader, &tag_map[map_index],
1638 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1639 case EXIF_TYPE_LONG:
1640 parse_exif_long_tag (exif_reader, &tag_map[map_index],
1641 tagdata.count, tagdata.offset, tagdata.offset_as_data);
1644 GST_WARNING ("Unhandled tag type: %u", tagdata.tag_type);
1649 /* check if the pending tags have something that can still be added */
1652 GstExifTagData *data;
1654 for (walker = exif_reader->pending_tags; walker;
1655 walker = g_slist_next (walker)) {
1656 data = (GstExifTagData *) walker->data;
1657 switch (data->tag) {
1658 case EXIF_TAG_XRESOLUTION:
1659 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_HORIZONTAL_PPI,
1660 data->count, data->offset, 1, FALSE);
1662 case EXIF_TAG_YRESOLUTION:
1663 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_VERTICAL_PPI,
1664 data->count, data->offset, 1, FALSE);
1672 gst_buffer_unmap (exif_reader->buffer, data, size);
1678 GST_WARNING ("Buffer offset invalid when parsing exif ifd");
1679 gst_buffer_unmap (exif_reader->buffer, data, size);
1684 GST_WARNING ("Failed to parse the exif ifd");
1685 gst_buffer_unmap (exif_reader->buffer, data, size);
1691 * gst_tag_list_to_exif_buffer:
1692 * @taglist: The taglist
1693 * @byte_order: byte order used in writing (G_LITTLE_ENDIAN or G_BIG_ENDIAN)
1694 * @base_offset: Offset from the tiff header first byte
1696 * Formats the tags in taglist on exif format. The resulting buffer contains
1697 * the tags IFD and is followed by the data pointed by the tag entries.
1699 * Returns: A GstBuffer containing the tag entries followed by the tag data
1704 gst_tag_list_to_exif_buffer (const GstTagList * taglist, gint byte_order,
1705 guint32 base_offset)
1707 return write_exif_ifd (taglist, byte_order, base_offset, tag_map_ifd0);
1711 * gst_tag_list_to_exif_buffer_with_tiff_header:
1712 * @taglist: The taglist
1714 * Formats the tags in taglist into exif structure, a tiff header
1715 * is put in the beginning of the buffer.
1717 * Returns: A GstBuffer containing the data
1722 gst_tag_list_to_exif_buffer_with_tiff_header (const GstTagList * taglist)
1725 GstByteWriter writer;
1729 ifd = gst_tag_list_to_exif_buffer (taglist, G_BYTE_ORDER, 8);
1731 GST_WARNING ("Failed to create exif buffer");
1735 data = gst_buffer_map (ifd, &size, NULL, GST_MAP_READ);
1737 size += TIFF_HEADER_SIZE;
1739 /* TODO what is the correct endianness here? */
1740 gst_byte_writer_init_with_size (&writer, size, FALSE);
1742 if (G_BYTE_ORDER == G_LITTLE_ENDIAN) {
1743 gst_byte_writer_put_uint16_le (&writer, TIFF_LITTLE_ENDIAN);
1744 gst_byte_writer_put_uint16_le (&writer, 42);
1745 gst_byte_writer_put_uint32_le (&writer, 8);
1747 gst_byte_writer_put_uint16_be (&writer, TIFF_BIG_ENDIAN);
1748 gst_byte_writer_put_uint16_be (&writer, 42);
1749 gst_byte_writer_put_uint32_be (&writer, 8);
1751 if (!gst_byte_writer_put_data (&writer, data, size)) {
1752 GST_WARNING ("Byte writer size mismatch");
1753 /* reaching here is a programming error because we should have a buffer
1755 g_assert_not_reached ();
1756 gst_buffer_unmap (ifd, data, size);
1757 gst_buffer_unref (ifd);
1758 gst_byte_writer_reset (&writer);
1761 gst_buffer_unmap (ifd, data, size);
1762 gst_buffer_unref (ifd);
1764 return gst_byte_writer_reset_and_get_buffer (&writer);
1768 * gst_tag_list_from_exif_buffer:
1769 * @buffer: The exif buffer
1770 * @byte_order: byte order of the data
1771 * @base_offset: Offset from the tiff header to this buffer
1773 * Parses the IFD and IFD tags data contained in the buffer and puts it
1774 * on a taglist. The base_offset is used to subtract from the offset in
1775 * the tag entries and be able to get the offset relative to the buffer
1778 * Returns: The parsed taglist
1783 gst_tag_list_from_exif_buffer (GstBuffer * buffer, gint byte_order,
1784 guint32 base_offset)
1786 GstExifReader reader;
1787 g_return_val_if_fail (byte_order == G_LITTLE_ENDIAN
1788 || byte_order == G_BIG_ENDIAN, NULL);
1790 gst_exif_reader_init (&reader, byte_order, buffer, base_offset);
1792 if (!parse_exif_ifd (&reader, 0, tag_map_ifd0))
1795 return gst_exif_reader_reset (&reader, TRUE);
1799 gst_exif_reader_reset (&reader, FALSE);
1800 GST_WARNING ("Failed to parse the exif buffer");
1806 * gst_tag_list_from_exif_buffer_with_tiff_header:
1807 * @buffer: The exif buffer
1809 * Parses the exif tags starting with a tiff header structure.
1811 * Returns: The taglist
1816 gst_tag_list_from_exif_buffer_with_tiff_header (GstBuffer * buffer)
1818 GstByteReader reader;
1819 guint16 fortytwo = 42;
1820 guint16 endianness = 0;
1822 GstTagList *taglist = NULL;
1823 GstBuffer *subbuffer;
1824 guint8 *data, *sdata;
1827 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
1829 GST_LOG ("Parsing exif tags with tiff header of size %u", size);
1831 gst_byte_reader_init (&reader, data, size);
1833 GST_LOG ("Parsing the tiff header");
1834 if (!gst_byte_reader_get_uint16_be (&reader, &endianness)) {
1835 goto byte_reader_fail;
1838 if (endianness == TIFF_LITTLE_ENDIAN) {
1839 if (!gst_byte_reader_get_uint16_le (&reader, &fortytwo) ||
1840 !gst_byte_reader_get_uint32_le (&reader, &offset))
1841 goto byte_reader_fail;
1842 } else if (endianness == TIFF_BIG_ENDIAN) {
1843 if (!gst_byte_reader_get_uint16_be (&reader, &fortytwo) ||
1844 !gst_byte_reader_get_uint32_be (&reader, &offset))
1845 goto byte_reader_fail;
1847 goto invalid_endianness;
1852 subbuffer = gst_buffer_new_and_alloc (size - (TIFF_HEADER_SIZE - 2));
1854 sdata = gst_buffer_map (subbuffer, &ssize, NULL, GST_MAP_WRITE);
1855 memcpy (sdata, data + TIFF_HEADER_SIZE, size - TIFF_HEADER_SIZE);
1856 gst_buffer_unmap (subbuffer, sdata, ssize);
1858 taglist = gst_tag_list_from_exif_buffer (subbuffer,
1859 endianness == TIFF_LITTLE_ENDIAN ? G_LITTLE_ENDIAN : G_BIG_ENDIAN, 8);
1861 gst_buffer_unref (subbuffer);
1864 gst_buffer_unmap (buffer, data, size);
1870 GST_WARNING ("Failed to read values from buffer");
1875 GST_WARNING ("Invalid endianness number %u", endianness);
1880 GST_WARNING ("Invalid magic number %u, should be 42", fortytwo);
1885 /* special serialization functions */
1886 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (contrast,
1887 capturing_contrast);
1888 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (exposure_mode,
1889 capturing_exposure_mode);
1890 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (exposure_program,
1891 capturing_exposure_program);
1892 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (gain_control,
1893 capturing_gain_adjustment);
1894 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (metering_mode,
1895 capturing_metering_mode);
1896 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (orientation,
1898 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (saturation,
1899 capturing_saturation);
1900 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (scene_capture_type,
1901 capturing_scene_capture_type);
1902 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (sharpness,
1903 capturing_sharpness);
1904 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (source,
1906 EXIF_SERIALIZATION_DESERIALIZATION_MAP_STRING_TO_INT_FUNC (white_balance,
1907 capturing_white_balance);
1910 serialize_geo_coordinate (GstExifWriter * writer, const GstTagList * taglist,
1911 const GstExifTagMatch * exiftag)
1920 latitude = exiftag->exif_tag == EXIF_TAG_GPS_LATITUDE; /* exif tag for latitude */
1921 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
1922 GST_WARNING ("Failed to get double from tag list for tag: %s",
1927 /* first write the Latitude or Longitude Ref */
1930 write_exif_ascii_tag (writer, exiftag->complementary_tag, "N");
1933 write_exif_ascii_tag (writer, exiftag->complementary_tag, "S");
1937 write_exif_ascii_tag (writer, exiftag->complementary_tag, "E");
1940 write_exif_ascii_tag (writer, exiftag->complementary_tag, "W");
1944 /* now write the degrees stuff */
1945 GST_LOG ("Converting geo location %lf to degrees", value);
1946 degrees = (gint) value;
1948 minutes = (gint) (value * 60);
1949 value = (value * 60) - minutes;
1950 seconds = (gint) (value * 60);
1951 GST_LOG ("Converted geo location to %d.%d'%d'' degrees", degrees,
1954 offset = gst_byte_writer_get_size (&writer->datawriter);
1955 gst_exif_writer_write_tag_header (writer, exiftag->exif_tag,
1956 EXIF_TYPE_RATIONAL, 3, offset, FALSE);
1957 gst_exif_writer_write_rational_data (writer, degrees, 1);
1958 gst_exif_writer_write_rational_data (writer, minutes, 1);
1959 gst_exif_writer_write_rational_data (writer, seconds, 1);
1963 deserialize_geo_coordinate (GstExifReader * exif_reader,
1964 GstByteReader * reader, const GstExifTagMatch * exiftag,
1965 GstExifTagData * tagdata)
1967 GstByteReader fractions_reader;
1969 GstExifTagData next_tagdata;
1971 /* for the conversion */
1972 guint32 degrees_n = 0;
1973 guint32 degrees_d = 1;
1974 guint32 minutes_n = 0;
1975 guint32 minutes_d = 1;
1976 guint32 seconds_n = 0;
1977 guint32 seconds_d = 1;
1981 guint8 *data = NULL;
1984 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
1987 if (exiftag->complementary_tag != tagdata->tag) {
1988 /* First should come the 'Ref' tags */
1989 GST_WARNING ("Tag %d is not the 'Ref' tag for latitude nor longitude",
1994 if (tagdata->offset_as_data[0] == 'N' || tagdata->offset_as_data[0] == 'E') {
1996 } else if (tagdata->offset_as_data[0] == 'S'
1997 || tagdata->offset_as_data[0] == 'W') {
2000 GST_WARNING ("Invalid LatitudeRef or LongitudeRef %c",
2001 tagdata->offset_as_data[0]);
2005 /* now read the following tag that must be the latitude or longitude */
2006 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2007 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2010 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2014 if (exiftag->exif_tag != next_tagdata.tag) {
2015 GST_WARNING ("This is not a geo cordinate tag");
2019 /* read the remaining tag entry data */
2020 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2028 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2029 GST_WARNING ("Invalid type %d for geo coordinate (latitude/longitude)",
2030 next_tagdata.tag_type);
2033 if (next_tagdata.count != 3) {
2034 GST_WARNING ("Geo coordinate should use 3 fractions, we have %u",
2035 next_tagdata.count);
2039 data = gst_buffer_map (exif_reader->buffer, &size, NULL, GST_MAP_READ);
2041 /* now parse the fractions */
2042 gst_byte_reader_init (&fractions_reader, data, size);
2044 if (!gst_byte_reader_set_pos (&fractions_reader,
2045 next_tagdata.offset - exif_reader->base_offset))
2048 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2049 if (!gst_byte_reader_get_uint32_le (&fractions_reader, °rees_n) ||
2050 !gst_byte_reader_get_uint32_le (&fractions_reader, °rees_d) ||
2051 !gst_byte_reader_get_uint32_le (&fractions_reader, &minutes_n) ||
2052 !gst_byte_reader_get_uint32_le (&fractions_reader, &minutes_d) ||
2053 !gst_byte_reader_get_uint32_le (&fractions_reader, &seconds_n) ||
2054 !gst_byte_reader_get_uint32_le (&fractions_reader, &seconds_d))
2057 if (!gst_byte_reader_get_uint32_be (&fractions_reader, °rees_n) ||
2058 !gst_byte_reader_get_uint32_be (&fractions_reader, °rees_d) ||
2059 !gst_byte_reader_get_uint32_be (&fractions_reader, &minutes_n) ||
2060 !gst_byte_reader_get_uint32_be (&fractions_reader, &minutes_d) ||
2061 !gst_byte_reader_get_uint32_be (&fractions_reader, &seconds_n) ||
2062 !gst_byte_reader_get_uint32_be (&fractions_reader, &seconds_d))
2065 gst_buffer_unmap (exif_reader->buffer, data, size);
2067 GST_DEBUG ("Read degrees fraction for tag %s: %u/%u %u/%u %u/%u",
2068 exiftag->gst_tag, degrees_n, degrees_d, minutes_n, minutes_d,
2069 seconds_n, seconds_d);
2071 gst_util_fraction_to_double (degrees_n, degrees_d, °rees);
2072 gst_util_fraction_to_double (minutes_n, minutes_d, &minutes);
2073 gst_util_fraction_to_double (seconds_n, seconds_d, &seconds);
2075 minutes += seconds / 60;
2076 degrees += minutes / 60;
2077 degrees *= multiplier;
2079 GST_DEBUG ("Adding %s tag: %lf", exiftag->gst_tag, degrees);
2080 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2081 exiftag->gst_tag, degrees, NULL);
2086 GST_WARNING ("Failed to read fields from buffer (too short?)");
2088 gst_buffer_unmap (exif_reader->buffer, data, size);
2094 serialize_geo_direction (GstExifWriter * writer, const GstTagList * taglist,
2095 const GstExifTagMatch * exiftag)
2099 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2100 GST_WARNING ("Failed to get double from tag list for tag: %s",
2105 /* first write the direction ref */
2106 write_exif_ascii_tag (writer, exiftag->complementary_tag, "T");
2107 gst_exif_writer_write_rational_tag_from_double (writer,
2108 exiftag->exif_tag, value);
2112 deserialize_geo_direction (GstExifReader * exif_reader,
2113 GstByteReader * reader, const GstExifTagMatch * exiftag,
2114 GstExifTagData * tagdata)
2116 GstExifTagData next_tagdata = { 0, };
2119 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2122 if (exiftag->complementary_tag == tagdata->tag) {
2123 /* First should come the 'Ref' tags */
2124 if (tagdata->offset_as_data[0] == 'M') {
2125 GST_WARNING ("Magnetic direction is not supported");
2127 } else if (tagdata->offset_as_data[0] == 'T') {
2130 GST_WARNING ("Invalid Ref for direction or track %c",
2131 tagdata->offset_as_data[0]);
2135 GST_DEBUG ("No Direction Ref, using default=T");
2136 if (tagdata->tag == exiftag->exif_tag) {
2137 /* this is the main tag */
2138 tagdata_copy (&next_tagdata, tagdata);
2142 if (next_tagdata.tag == 0) {
2143 /* now read the following tag that must be the exif_tag */
2144 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2145 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2148 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2152 if (exiftag->exif_tag != next_tagdata.tag) {
2153 GST_WARNING ("Unexpected tag");
2157 /* read the remaining tag entry data */
2158 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2166 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2167 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2171 if (next_tagdata.count != 1) {
2172 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2173 next_tagdata.tag_type, next_tagdata.count);
2177 parse_exif_rational_tag (exif_reader,
2178 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, 1, FALSE);
2183 GST_WARNING ("Failed to read fields from buffer (too short?)");
2189 serialize_geo_elevation (GstExifWriter * writer, const GstTagList * taglist,
2190 const GstExifTagMatch * exiftag)
2194 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2195 GST_WARNING ("Failed to get double from tag list for tag: %s",
2200 /* first write the Ref */
2201 gst_exif_writer_write_byte_tag (writer,
2202 exiftag->complementary_tag, value >= 0 ? 0 : 1);
2208 gst_exif_writer_write_rational_tag_from_double (writer,
2209 exiftag->exif_tag, value);
2213 deserialize_geo_elevation (GstExifReader * exif_reader,
2214 GstByteReader * reader, const GstExifTagMatch * exiftag,
2215 GstExifTagData * tagdata)
2217 GstExifTagData next_tagdata = { 0, };
2218 gint multiplier = 1;
2221 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2224 if (exiftag->complementary_tag == tagdata->tag) {
2225 if (tagdata->offset_as_data[0] == 0) {
2227 } else if (tagdata->offset_as_data[0] == 1) {
2230 GST_WARNING ("Invalid GPSAltitudeRef %u", tagdata->offset_as_data[0]);
2234 GST_DEBUG ("No GPSAltitudeRef, using default=0");
2235 if (tagdata->tag == exiftag->exif_tag) {
2236 tagdata_copy (&next_tagdata, tagdata);
2240 /* now read the following tag that must be the exif_tag */
2241 if (next_tagdata.tag == 0) {
2242 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2243 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2246 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2250 if (exiftag->exif_tag != next_tagdata.tag) {
2251 GST_WARNING ("Unexpected tag");
2255 /* read the remaining tag entry data */
2256 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2264 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2265 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2269 if (next_tagdata.count != 1) {
2270 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2271 next_tagdata.tag_type, next_tagdata.count);
2275 parse_exif_rational_tag (exif_reader,
2276 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, multiplier,
2282 GST_WARNING ("Failed to read fields from buffer (too short?)");
2288 serialize_speed (GstExifWriter * writer, const GstTagList * taglist,
2289 const GstExifTagMatch * exiftag)
2293 if (!gst_tag_list_get_double (taglist, exiftag->gst_tag, &value)) {
2294 GST_WARNING ("Failed to get double from tag list for tag: %s",
2299 /* first write the Ref */
2300 write_exif_ascii_tag (writer, exiftag->complementary_tag, "K");
2303 gst_exif_writer_write_rational_tag_from_double (writer,
2304 exiftag->exif_tag, value * METERS_PER_SECOND_TO_KILOMETERS_PER_HOUR);
2308 deserialize_speed (GstExifReader * exif_reader,
2309 GstByteReader * reader, const GstExifTagMatch * exiftag,
2310 GstExifTagData * tagdata)
2312 GstExifTagData next_tagdata = { 0, };
2313 gdouble multiplier = 1;
2316 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2319 if (exiftag->complementary_tag == tagdata->tag) {
2320 if (tagdata->offset_as_data[0] == 'K') {
2321 multiplier = KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
2322 } else if (tagdata->offset_as_data[0] == 'M') {
2323 multiplier = MILES_PER_HOUR_TO_METERS_PER_SECOND;
2324 } else if (tagdata->offset_as_data[0] == 'N') {
2325 multiplier = KNOTS_TO_METERS_PER_SECOND;
2327 GST_WARNING ("Invalid GPSSpeedRed %c", tagdata->offset_as_data[0]);
2331 GST_DEBUG ("No GPSSpeedRef, using default=K");
2332 multiplier = KILOMETERS_PER_HOUR_TO_METERS_PER_SECOND;
2334 if (tagdata->tag == exiftag->exif_tag) {
2335 tagdata_copy (&next_tagdata, tagdata);
2339 /* now read the following tag that must be the exif_tag */
2340 if (next_tagdata.tag == 0) {
2341 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2342 if (!gst_byte_reader_peek_uint16_le (reader, &next_tagdata.tag))
2345 if (!gst_byte_reader_peek_uint16_be (reader, &next_tagdata.tag))
2349 if (exiftag->exif_tag != next_tagdata.tag) {
2350 GST_WARNING ("Unexpected tag");
2354 /* read the remaining tag entry data */
2355 if (!parse_exif_tag_header (reader, exif_reader->byte_order, &next_tagdata)) {
2364 if (next_tagdata.tag_type != EXIF_TYPE_RATIONAL) {
2365 GST_WARNING ("Invalid type %d for 0x%x", next_tagdata.tag_type,
2369 if (next_tagdata.count != 1) {
2370 GST_WARNING ("0x%x tag must have a single fraction, we have %u",
2371 next_tagdata.tag_type, next_tagdata.count);
2375 parse_exif_rational_tag (exif_reader,
2376 exiftag->gst_tag, next_tagdata.count, next_tagdata.offset, multiplier,
2382 GST_WARNING ("Failed to read fields from buffer (too short?)");
2387 serialize_shutter_speed (GstExifWriter * writer, const GstTagList * taglist,
2388 const GstExifTagMatch * exiftag)
2390 const GValue *value = NULL;
2393 value = gst_tag_list_get_value_index (taglist, exiftag->gst_tag, 0);
2395 GST_WARNING ("Failed to get shutter speed from from tag list");
2398 gst_util_fraction_to_double (gst_value_get_fraction_numerator (value),
2399 gst_value_get_fraction_denominator (value), &num);
2404 num = -log (num) / M_LN2;
2408 gst_exif_writer_write_signed_rational_tag_from_double (writer,
2409 exiftag->exif_tag, num);
2413 deserialize_shutter_speed (GstExifReader * exif_reader,
2414 GstByteReader * reader, const GstExifTagMatch * exiftag,
2415 GstExifTagData * tagdata)
2417 gint32 frac_n, frac_d;
2419 GValue value = { 0 };
2421 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2424 if (!exif_reader_read_rational_tag (exif_reader, tagdata->count,
2425 tagdata->offset, TRUE, &frac_n, &frac_d))
2428 gst_util_fraction_to_double (frac_n, frac_d, &d);
2430 gst_util_double_to_fraction (d, &frac_n, &frac_d);
2432 g_value_init (&value, GST_TYPE_FRACTION);
2433 gst_value_set_fraction (&value, frac_n, frac_d);
2434 gst_tag_list_add_value (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2435 exiftag->gst_tag, &value);
2436 g_value_unset (&value);
2442 serialize_aperture_value (GstExifWriter * writer, const GstTagList * taglist,
2443 const GstExifTagMatch * exiftag)
2447 if (!gst_tag_list_get_double_index (taglist, exiftag->gst_tag, 0, &num)) {
2448 GST_WARNING ("Failed to get focal ratio from from tag list");
2452 num = 2 * log2 (num);
2454 num = 2 * (log (num) / M_LN2);
2458 gst_exif_writer_write_rational_tag_from_double (writer,
2459 exiftag->exif_tag, num);
2463 deserialize_aperture_value (GstExifReader * exif_reader,
2464 GstByteReader * reader, const GstExifTagMatch * exiftag,
2465 GstExifTagData * tagdata)
2467 gint32 frac_n, frac_d;
2470 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2473 if (!exif_reader_read_rational_tag (exif_reader, tagdata->count,
2474 tagdata->offset, FALSE, &frac_n, &frac_d))
2477 gst_util_fraction_to_double (frac_n, frac_d, &d);
2480 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2481 exiftag->gst_tag, d, NULL);
2487 serialize_sensitivity_type (GstExifWriter * writer, const GstTagList * taglist,
2488 const GstExifTagMatch * exiftag)
2490 /* we only support ISOSpeed as the sensitivity type (3) */
2491 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, 3);
2495 deserialize_sensitivity_type (GstExifReader * exif_reader,
2496 GstByteReader * reader, const GstExifTagMatch * exiftag,
2497 GstExifTagData * tagdata)
2499 GstExifTagData *sensitivity = NULL;
2502 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2503 type_data = GST_READ_UINT16_LE (tagdata->offset_as_data);
2505 type_data = GST_READ_UINT16_BE (tagdata->offset_as_data);
2508 if (type_data != 3) {
2509 GST_WARNING ("We only support SensitivityType=3");
2513 /* check the pending tags for the PhotographicSensitivity tag */
2515 gst_exif_reader_get_pending_tag (exif_reader,
2516 EXIF_TAG_PHOTOGRAPHIC_SENSITIVITY);
2517 if (sensitivity == NULL) {
2518 GST_WARNING ("PhotographicSensitivity tag not found");
2522 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2525 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2526 GST_TAG_CAPTURING_ISO_SPEED, sensitivity->offset_as_data, NULL);
2532 serialize_flash (GstExifWriter * writer, const GstTagList * taglist,
2533 const GstExifTagMatch * exiftag)
2535 gboolean flash_fired;
2536 const gchar *flash_mode;
2537 guint16 tagvalue = 0;
2539 if (!gst_tag_list_get_boolean_index (taglist, exiftag->gst_tag, 0,
2541 GST_WARNING ("Failed to get flash fired from from tag list");
2548 if (gst_tag_list_peek_string_index (taglist, GST_TAG_CAPTURING_FLASH_MODE, 0,
2551 if (strcmp (flash_mode, "auto") == 0) {
2553 } else if (strcmp (flash_mode, "always") == 0) {
2555 } else if (strcmp (flash_mode, "never") == 0) {
2559 tagvalue = tagvalue | (mode << 3);
2561 GST_DEBUG ("flash-mode not available");
2564 gst_exif_writer_write_short_tag (writer, exiftag->exif_tag, tagvalue);
2568 deserialize_flash (GstExifReader * exif_reader,
2569 GstByteReader * reader, const GstExifTagMatch * exiftag,
2570 GstExifTagData * tagdata)
2574 const gchar *mode_str = NULL;
2576 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2579 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2580 value = GST_READ_UINT16_LE (tagdata->offset_as_data);
2582 value = GST_READ_UINT16_BE (tagdata->offset_as_data);
2585 /* check flash fired */
2587 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2588 GST_TAG_CAPTURING_FLASH_FIRED, TRUE, NULL);
2590 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2591 GST_TAG_CAPTURING_FLASH_FIRED, FALSE, NULL);
2594 mode = (value >> 3) & 0x3;
2596 mode_str = "always";
2597 } else if (mode == 2) {
2599 } else if (mode == 3) {
2604 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_REPLACE,
2605 GST_TAG_CAPTURING_FLASH_MODE, mode_str, NULL);
2611 deserialize_resolution (GstExifReader * exif_reader,
2612 GstByteReader * reader, const GstExifTagMatch * exiftag,
2613 GstExifTagData * tagdata)
2615 GstExifTagData *xres = NULL;
2616 GstExifTagData *yres = NULL;
2620 if (exif_reader->byte_order == G_LITTLE_ENDIAN) {
2621 unit = GST_READ_UINT16_LE (tagdata->offset_as_data);
2623 unit = GST_READ_UINT16_BE (tagdata->offset_as_data);
2626 if (unit != 2 && unit != 3) {
2627 GST_WARNING ("Invalid resolution unit, ignoring PPI tags");
2631 xres = gst_exif_reader_get_pending_tag (exif_reader, EXIF_TAG_XRESOLUTION);
2632 yres = gst_exif_reader_get_pending_tag (exif_reader, EXIF_TAG_YRESOLUTION);
2639 multiplier = 1 / 2.54;
2643 g_assert_not_reached ();
2648 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_HORIZONTAL_PPI,
2649 xres->count, xres->offset, multiplier, FALSE);
2652 parse_exif_rational_tag (exif_reader, GST_TAG_IMAGE_VERTICAL_PPI,
2653 yres->count, yres->offset, multiplier, FALSE);
2660 serialize_scene_type (GstExifWriter * writer, const GstTagList * taglist,
2661 const GstExifTagMatch * exiftag)
2666 if (gst_tag_list_peek_string_index (taglist, GST_TAG_CAPTURING_SOURCE, 0,
2668 if (strcmp (str, "dsc") == 0) {
2674 write_exif_undefined_tag (writer, exiftag->exif_tag, &value, 1);
2678 deserialize_scene_type (GstExifReader * exif_reader,
2679 GstByteReader * reader, const GstExifTagMatch * exiftag,
2680 GstExifTagData * tagdata)
2684 GST_LOG ("Starting to parse %s tag in exif 0x%x", exiftag->gst_tag,
2687 value = GST_READ_UINT8 (tagdata->offset_as_data);
2690 gst_tag_list_add (exif_reader->taglist, GST_TAG_MERGE_KEEP,
2691 GST_TAG_CAPTURING_SOURCE, "dsc", NULL);
2698 deserialize_add_to_pending_tags (GstExifReader * exif_reader,
2699 GstByteReader * reader, const GstExifTagMatch * exiftag,
2700 GstExifTagData * tagdata)
2702 GST_LOG ("Adding %s tag in exif 0x%x to pending tags", exiftag->gst_tag,
2705 /* add it to the pending tags, as we can only parse it when we find the
2706 * SensitivityType tag */
2707 gst_exif_reader_add_pending_tag (exif_reader, tagdata);
2711 #undef EXIF_SERIALIZATION_FUNC
2712 #undef EXIF_DESERIALIZATION_FUNC
2713 #undef EXIF_SERIALIZATION_DESERIALIZATION_FUNC