diff options
author | Edgard Lima <edgard.lima@indt.org.br> | 2007-11-18 21:06:51 +0000 |
---|---|---|
committer | Edgard Lima <edgard.lima@indt.org.br> | 2007-11-18 21:06:51 +0000 |
commit | a7a580dd585e9d7185b75505fa6749d96c6aff6a (patch) | |
tree | e6e849a14c11f2ce73fa27350bb633e4ccc3633c /ext | |
parent | 822d35c31bcda063be9e209612f5c753ad8e0bf2 (diff) | |
download | gst-plugins-bad-a7a580dd585e9d7185b75505fa6749d96c6aff6a.tar.gz gst-plugins-bad-a7a580dd585e9d7185b75505fa6749d96c6aff6a.tar.bz2 gst-plugins-bad-a7a580dd585e9d7185b75505fa6749d96c6aff6a.zip |
Strip out metadata chunks.
Original commit message from CVS:
Strip out metadata chunks.
Diffstat (limited to 'ext')
-rw-r--r-- | ext/metadata/gstmetadataparse.c | 299 | ||||
-rw-r--r-- | ext/metadata/gstmetadataparse.h | 11 | ||||
-rw-r--r-- | ext/metadata/metadataparse.c | 65 | ||||
-rw-r--r-- | ext/metadata/metadataparse.h | 13 | ||||
-rw-r--r-- | ext/metadata/metadataparsejpeg.c | 106 | ||||
-rw-r--r-- | ext/metadata/metadataparsejpeg.h | 15 | ||||
-rw-r--r-- | ext/metadata/metadataparsepng.c | 30 | ||||
-rw-r--r-- | ext/metadata/metadataparsepng.h | 10 | ||||
-rw-r--r-- | ext/metadata/metadataparsetypes.h | 64 |
9 files changed, 527 insertions, 86 deletions
diff --git a/ext/metadata/gstmetadataparse.c b/ext/metadata/gstmetadataparse.c index be1e0d43..43dbe6ad 100644 --- a/ext/metadata/gstmetadataparse.c +++ b/ext/metadata/gstmetadataparse.c @@ -161,6 +161,13 @@ gst_metadata_parse_parse (GstMetadataParse * filter, const guint8 * buf, static void gst_metadata_parse_send_tags (GstMetadataParse * filter); +static gboolean gst_metadata_parse_activate (GstPad * pad); + +static gboolean +gst_metadata_parse_get_strip_range (gint64 * boffset, guint32 * bsize, + const gint64 seg_offset, const guint32 seg_size, + gint64 * toffset, guint32 * tsize, gint64 * ioffset); + static void gst_metadata_parse_base_init (gpointer gclass) { @@ -492,11 +499,24 @@ gst_metadata_parse_init_members (GstMetadataParse * filter) filter->exif = TRUE; filter->iptc = TRUE; filter->xmp = TRUE; + + memset (filter->seg_offset, 0x00, sizeof (filter->seg_offset[0]) * 3); + memset (filter->seg_size, 0x00, sizeof (filter->seg_size[0]) * 3); + + memset (filter->seg_inject_data, 0x00, + sizeof (filter->seg_inject_data[0]) * 3); + memset (filter->seg_inject_size, 0x00, + sizeof (filter->seg_inject_size[0]) * 3); + + filter->num_segs = 0; + filter->taglist = NULL; filter->adapter = NULL; filter->next_offset = 0; filter->next_size = 0; filter->img_type = IMG_NONE; + filter->duration = -1; + filter->offset = 0; filter->state = MT_STATE_NULL; filter->need_more_data = FALSE; metadataparse_init (&filter->parse_data); @@ -654,13 +674,13 @@ gst_metadata_parse_send_tags (GstMetadataParse * filter) if (PARSE_DATA_OPTION (filter->parse_data) & PARSE_OPT_EXIF) metadataparse_exif_tag_list_add (filter->taglist, GST_TAG_MERGE_KEEP, - filter->parse_data.adpt_exif); + filter->parse_data.exif.adapter); if (PARSE_DATA_OPTION (filter->parse_data) & PARSE_OPT_IPTC) metadataparse_iptc_tag_list_add (filter->taglist, GST_TAG_MERGE_KEEP, - filter->parse_data.adpt_iptc); + filter->parse_data.iptc.adapter); if (PARSE_DATA_OPTION (filter->parse_data) & PARSE_OPT_XMP) metadataparse_xmp_tag_list_add (filter->taglist, GST_TAG_MERGE_KEEP, - filter->parse_data.adpt_xmp); + filter->parse_data.xmp.adapter); if (!gst_tag_list_is_empty (filter->taglist)) { @@ -708,6 +728,70 @@ gst_metadata_parse_parse (GstMetadataParse * filter, const guint8 * buf, } else if (ret > 0) { filter->need_more_data = TRUE; } else { + int i, j; + + filter->num_segs = 0; + + /* FIXME: better design for segments */ + + filter->seg_size[0] = filter->parse_data.exif.size; + filter->seg_offset[0] = filter->parse_data.exif.offset; + filter->seg_inject_size[0] = filter->parse_data.exif.size_inject; + filter->seg_inject_data[0] = filter->parse_data.exif.buffer; + + filter->seg_size[1] = filter->parse_data.iptc.size; + filter->seg_offset[1] = filter->parse_data.iptc.offset; + filter->seg_inject_size[1] = filter->parse_data.iptc.size_inject; + filter->seg_inject_data[1] = filter->parse_data.iptc.buffer; + + filter->seg_size[2] = filter->parse_data.xmp.size; + filter->seg_offset[2] = filter->parse_data.xmp.offset; + filter->seg_inject_size[2] = filter->parse_data.xmp.size_inject; + filter->seg_inject_data[2] = filter->parse_data.xmp.buffer; + + for (i = 0; i < 3; ++i) { + if (filter->seg_size[i]) { + filter->num_segs++; + } + } + + if (filter->num_segs) { + + for (i = 0; i < 3; ++i) { + int j, min = i; + + for (j = i + 1; j < 3; ++j) { + if (filter->seg_size[j]) + if (filter->seg_size[min] == 0 + || filter->seg_offset[j] < filter->seg_offset[min]) + min = j; + } + if (min != i) { + gint64 aux_offset; + guint32 aux_size; + guint8 *aux_data; + + aux_offset = filter->seg_offset[i]; + aux_size = filter->seg_size[i]; + + filter->seg_offset[i] = filter->seg_offset[min]; + filter->seg_size[i] = filter->seg_size[min]; + + filter->seg_offset[min] = aux_offset; + filter->seg_size[min] = aux_size; + + aux_size = filter->seg_inject_size[i]; + filter->seg_inject_size[i] = filter->seg_inject_size[min]; + filter->seg_inject_size[min] = aux_size; + + aux_data = filter->seg_inject_data[i]; + filter->seg_inject_data[i] = filter->seg_inject_data[min]; + filter->seg_inject_data[min] = aux_data; + } + } + + } + filter->state = MT_STATE_PARSED; filter->need_more_data = FALSE; filter->need_send_tag = TRUE; @@ -739,9 +823,12 @@ gst_metadata_parse_chain (GstPad * pad, GstBuffer * buf) { GstMetadataParse *filter = NULL; GstFlowReturn ret = GST_FLOW_ERROR; + guint32 buf_size; filter = GST_METADATA_PARSE (gst_pad_get_parent (pad)); + /* commented until I figure out how to strip if it wasn't parsed yet */ +#if 0 if (filter->state != MT_STATE_PARSED) { guint32 adpt_size = gst_adapter_available (filter->adapter); @@ -785,15 +872,24 @@ gst_metadata_parse_chain (GstPad * pad, GstBuffer * buf) goto done; } } +#endif if (filter->need_send_tag) { gst_metadata_parse_send_tags (filter); } - gst_buffer_set_caps (buf, GST_PAD_CAPS (filter->srcpad)); + buf_size = GST_BUFFER_SIZE (buf); + gst_metadata_parse_strip_buffer (filter, &buf); + filter->offset += buf_size; + + if (buf) { /* may be all buffer has been striped */ + gst_buffer_set_caps (buf, GST_PAD_CAPS (filter->srcpad)); - ret = gst_pad_push (filter->srcpad, buf); - buf = NULL; /* this function don't owner it anymore */ + ret = gst_pad_push (filter->srcpad, buf); + buf = NULL; /* this function don't owner it anymore */ + } else { + ret = GST_FLOW_OK; + } done: @@ -905,6 +1001,197 @@ done: } +/* + * Returns FALSE if nothing has stripped, TRUE if otherwise + */ + +gboolean +gst_metadata_parse_get_strip_range (gint64 * boffset, guint32 * bsize, + const gint64 seg_offset, const guint32 seg_size, + gint64 * toffset, guint32 * tsize, gint64 * ioffset) +{ + + gboolean ret = FALSE; + + if (seg_size == 0 || *bsize == 0) + goto done; + + /* segment after buffer */ + if (seg_offset > *boffset + *bsize) { + *bsize = 0; + goto done; + } + + if (seg_offset < *boffset) { + /* segment start somewhere before buffer */ + guint32 cut_size; + + /* all segment before buffer */ + if (seg_offset + seg_size <= *boffset) { + *tsize = *bsize; + *bsize = 0; + *toffset = *boffset; + goto done; + } + + cut_size = seg_size - (*boffset - seg_offset); + + if (cut_size >= *bsize) { + /* all buffer striped out */ + *bsize = 0; + *tsize = 0; + } else { + /* just beginning buffers striped */ + *toffset = *boffset + cut_size; + *tsize = *bsize - cut_size; + *bsize = 0; + } + + } else { + /* segment start somewhere into buffer */ + *ioffset = seg_offset; + + if (seg_offset + seg_size >= *boffset + *bsize) { + /* strip just tail */ + *bsize = seg_offset - *boffset; + } else { + /* strip the middle */ + *toffset = seg_offset + seg_size; + *tsize = *boffset + *bsize - seg_offset - seg_size; + *bsize = seg_offset - *boffset; + } + + } + + ret = TRUE; + +done: + + return ret; + +} + +/* + * TRUE -> buffer striped (or injeted) + * FALSE -> buffer unmodified + */ + +gboolean +gst_metadata_parse_strip_buffer (GstMetadataParse * filter, GstBuffer ** buf) +{ + + gint64 boffset[3]; + guint32 bsize[3]; + + gint64 toffset[3]; + guint32 tsize[3]; + + gint64 ioffset[3]; + + const gint64 *seg_offset = filter->seg_offset; + + const guint32 *seg_size = filter->seg_size; + + int i; + gboolean ret = FALSE; + guint32 new_size = 0; + const gint64 offset = filter->offset; + + if (filter->num_segs == 0) + goto done; + + memset (bsize, 0x00, sizeof (bsize)); + memset (tsize, 0x00, sizeof (tsize)); + + for (i = 0; i < filter->num_segs; ++i) { + ioffset[i] = -1; + } + + boffset[0] = offset; + new_size = bsize[0] = GST_BUFFER_SIZE (*buf); + i = 0; + + for (i = 0; i < filter->num_segs; ++i) { + guint32 striped_size; + + striped_size = bsize[i]; + if (gst_metadata_parse_get_strip_range (&boffset[i], &bsize[i], + seg_offset[i], seg_size[i], &toffset[i], &tsize[i], &ioffset[i])) { + ret = TRUE; + new_size -= (striped_size - (bsize[i] + tsize[i])); + } + if (i + 1 < filter->num_segs) { + if (tsize[i]) { + boffset[i + 1] = toffset[i]; + bsize[i + 1] = GST_BUFFER_SIZE (*buf) - (toffset[i] - offset); + tsize[i] = 0; + } else { + /* buffer already consumed */ + break; + } + } + if (new_size == 0) { + /* all buffer striped already */ + break; + } + + } + + /* segments must be sorted by offset to make this copy */ + if (ret) { + if (new_size == 0) { + gst_buffer_unref (*buf); + *buf = NULL; + } else { + GstBuffer *new_buf; + guint8 *data; + + for (i = 0; i < filter->num_segs; ++i) { + if (filter->seg_inject_size[i] == 0) + ioffset[i] = -1; + else + new_size += filter->seg_inject_size[i]; + } + + if (GST_BUFFER_SIZE (new_buf) < new_size) { + /* FIXME: alloc more memory here */ + } + + /* FIXME: try to use buffer data in place */ + new_buf = gst_buffer_copy (*buf); + data = GST_BUFFER_DATA (new_buf); + + GST_BUFFER_SIZE (new_buf) = new_size; + + for (i = 0; i < filter->num_segs; ++i) { + if (bsize[i]) { + memcpy (data, + GST_BUFFER_DATA (*buf) + (boffset[i] - offset), bsize[i]); + data += bsize[i]; + } + if (ioffset[i] >= 0) { + memcpy (data, filter->seg_inject_data[i], filter->seg_inject_size[i]); + data += filter->seg_inject_size[i]; + } + if (tsize[i]) { + memcpy (data, + GST_BUFFER_DATA (*buf) + (toffset[i] - offset), tsize[i]); + data += tsize[i]; + } + } + gst_buffer_unref (*buf); + *buf = new_buf; + GST_BUFFER_FLAG_UNSET (*buf, GST_BUFFER_FLAG_READONLY); + } + } + + +done: + + return ret; + +} + static gboolean gst_metadata_parse_get_range (GstPad * pad, guint64 offset, guint size, GstBuffer ** buf) diff --git a/ext/metadata/gstmetadataparse.h b/ext/metadata/gstmetadataparse.h index eb1f802d..dd5a031c 100644 --- a/ext/metadata/gstmetadataparse.h +++ b/ext/metadata/gstmetadataparse.h @@ -79,6 +79,14 @@ struct _GstMetadataParse gboolean iptc; gboolean xmp; + gint64 seg_offset[3]; + guint32 seg_size[3]; + + guint32 seg_inject_size[3]; + guint8 * seg_inject_data[3]; + + gint8 num_segs; + gboolean need_send_tag; GstTagList *taglist; @@ -88,6 +96,9 @@ struct _GstMetadataParse guint32 next_size; ImageType img_type; + gint64 duration; + gint64 offset; + MetadataState state; gboolean need_more_data; diff --git a/ext/metadata/metadataparse.c b/ext/metadata/metadataparse.c index 8a16bcf3..410ae847 100644 --- a/ext/metadata/metadataparse.c +++ b/ext/metadata/metadataparse.c @@ -41,6 +41,8 @@ * Boston, MA 02111-1307, USA. */ +#include <string.h> + #include "metadataparse.h" /* @@ -61,9 +63,10 @@ metadataparse_init (ParseData * parse_data) parse_data->state = STATE_NULL; parse_data->img_type = IMG_NONE; parse_data->option = PARSE_OPT_ALL; - parse_data->adpt_exif = NULL; - parse_data->adpt_iptc = NULL; - parse_data->adpt_xmp = NULL; + parse_data->offset = 0; + memset (&parse_data->exif, 0x00, sizeof (MetadataChunk)); + memset (&parse_data->iptc, 0x00, sizeof (MetadataChunk)); + memset (&parse_data->xmp, 0x00, sizeof (MetadataChunk)); } /* @@ -97,12 +100,12 @@ metadataparse_parse (ParseData * parse_data, const guint8 * buf, case IMG_JPEG: ret = metadataparse_jpeg_parse (&parse_data->format_data.jpeg, - (guint8 *) buf, &bufsize, &next_start, next_size); + (guint8 *) buf, &bufsize, parse_data->offset, &next_start, next_size); break; case IMG_PNG: ret = metadataparse_png_parse (&parse_data->format_data.png, - (guint8 *) buf, &bufsize, &next_start, next_size); + (guint8 *) buf, &bufsize, parse_data->offset, &next_start, next_size); break; default: /* unexpected */ @@ -112,6 +115,7 @@ metadataparse_parse (ParseData * parse_data, const guint8 * buf, } *next_offset = next_start - buf; + parse_data->offset += *next_offset; done: @@ -135,21 +139,34 @@ metadataparse_dispose (ParseData * parse_data) break; } - if (parse_data->adpt_xmp) { - gst_object_unref (parse_data->adpt_xmp); - parse_data->adpt_xmp = NULL; + if (parse_data->xmp.adapter) { + gst_object_unref (parse_data->xmp.adapter); + } + + if (parse_data->iptc.adapter) { + gst_object_unref (parse_data->iptc.adapter); + } + + if (parse_data->exif.adapter) { + gst_object_unref (parse_data->exif.adapter); + } + + if (parse_data->xmp.buffer) { + free (parse_data->xmp.buffer); } - if (parse_data->adpt_iptc) { - gst_object_unref (parse_data->adpt_iptc); - parse_data->adpt_iptc = NULL; + if (parse_data->iptc.buffer) { + free (parse_data->iptc.buffer); } - if (parse_data->adpt_exif) { - gst_object_unref (parse_data->adpt_exif); - parse_data->adpt_exif = NULL; + if (parse_data->exif.buffer) { + free (parse_data->exif.buffer); } + memset (&parse_data->xmp, 0x00, sizeof (MetadataChunk)); + memset (&parse_data->iptc, 0x00, sizeof (MetadataChunk)); + memset (&parse_data->exif, 0x00, sizeof (MetadataChunk)); + } /* @@ -163,11 +180,11 @@ metadataparse_parse_none (ParseData * parse_data, const guint8 * buf, { int ret = -1; - GstAdapter **adpt_exif = NULL; - GstAdapter **adpt_iptc = NULL; - GstAdapter **adpt_xmp = NULL; + MetadataChunk *exif = NULL; + MetadataChunk *iptc = NULL; + MetadataChunk *xmp = NULL; - *next_start = buf; + *next_start = (guint8 *) buf; parse_data->img_type = IMG_NONE; @@ -178,15 +195,14 @@ metadataparse_parse_none (ParseData * parse_data, const guint8 * buf, } if (parse_data->option & PARSE_OPT_EXIF) - adpt_exif = &parse_data->adpt_exif; + exif = &parse_data->exif; if (parse_data->option & PARSE_OPT_IPTC) - adpt_iptc = &parse_data->adpt_iptc; + iptc = &parse_data->iptc; if (parse_data->option & PARSE_OPT_XMP) - adpt_xmp = &parse_data->adpt_xmp; + xmp = &parse_data->xmp; if (buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF) { - metadataparse_jpeg_init (&parse_data->format_data.jpeg, adpt_exif, - adpt_iptc, adpt_xmp); + metadataparse_jpeg_init (&parse_data->format_data.jpeg, exif, iptc, xmp); ret = 0; parse_data->img_type = IMG_JPEG; goto done; @@ -200,8 +216,7 @@ metadataparse_parse_none (ParseData * parse_data, const guint8 * buf, if (buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 && buf[4] == 0x0D && buf[5] == 0x0A && buf[6] == 0x1A && buf[7] == 0x0A) { - metadataparse_png_init (&parse_data->format_data.png, adpt_exif, - adpt_iptc, adpt_xmp); + metadataparse_png_init (&parse_data->format_data.png, exif, iptc, xmp); ret = 0; parse_data->img_type = IMG_PNG; goto done; diff --git a/ext/metadata/metadataparse.h b/ext/metadata/metadataparse.h index 4534f12a..98c1714d 100644 --- a/ext/metadata/metadataparse.h +++ b/ext/metadata/metadataparse.h @@ -45,6 +45,7 @@ #define __METADATAPARSE_H__ #include <gst/base/gstadapter.h> +#include "metadataparsetypes.h" #include "metadataparsejpeg.h" #include "metadataparsepng.h" @@ -78,14 +79,15 @@ typedef struct _tag_ParseData ParseState state; ImageType img_type; ParseOption option; + guint32 offset; /* offset since begining of stream */ union { JpegData jpeg; PngData png; } format_data; - GstAdapter *adpt_exif; - GstAdapter *adpt_iptc; - GstAdapter *adpt_xmp; + MetadataChunk exif; + MetadataChunk iptc; + MetadataChunk xmp; } ParseData; #define PARSE_DATA_IMG_TYPE(p) (p).img_type @@ -96,7 +98,10 @@ typedef struct _tag_ParseData extern void metadataparse_init (ParseData * parse_data); /* - * offset: number of bytes to jump (just a hint to jump a chunk) + * offset: number of bytes that MUST be jumped after current "buf" pointer + * next_size: number of minimum amount of bytes required on next step. + * if less than this is provided, the return will be 1 for sure. + * and the offset will be 0 (zero) * return: * -1 -> error * 0 -> done diff --git a/ext/metadata/metadataparsejpeg.c b/ext/metadata/metadataparsejpeg.c index 1a3acb4b..daf7ad27 100644 --- a/ext/metadata/metadataparsejpeg.c +++ b/ext/metadata/metadataparsejpeg.c @@ -49,7 +49,8 @@ static int metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size); + guint32 * bufsize, const guint32 offset, const guint8 * step_buf, + guint8 ** next_start, guint32 * next_size); static int metadataparse_jpeg_exif (JpegData * jpeg_data, guint8 ** buf, @@ -70,14 +71,15 @@ metadataparse_jpeg_jump (JpegData * jpeg_data, guint8 ** buf, #define READ(buf, size) ( (size)--, *((buf)++) ) void -metadataparse_jpeg_init (JpegData * jpeg_data, GstAdapter ** adpt_exif, - GstAdapter ** adpt_iptc, GstAdapter ** adpt_xmp) +metadataparse_jpeg_init (JpegData * jpeg_data, MetadataChunk * exif, + MetadataChunk * iptc, MetadataChunk * xmp) { jpeg_data->state = JPEG_NULL; - jpeg_data->adpt_exif = adpt_exif; - jpeg_data->adpt_iptc = adpt_iptc; - jpeg_data->adpt_xmp = adpt_xmp; + jpeg_data->exif = exif; + jpeg_data->iptc = iptc; + jpeg_data->xmp = xmp; jpeg_data->read = 0; + jpeg_data->jfif_found = FALSE; metadataparse_xmp_init (); } @@ -87,18 +89,20 @@ metadataparse_jpeg_dispose (JpegData * jpeg_data) { metadataparse_xmp_dispose (); - jpeg_data->adpt_exif = NULL; - jpeg_data->adpt_iptc = NULL; - jpeg_data->adpt_xmp = NULL; + jpeg_data->exif = NULL; + jpeg_data->iptc = NULL; + jpeg_data->xmp = NULL; } int metadataparse_jpeg_parse (JpegData * jpeg_data, guint8 * buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size) + guint32 * bufsize, const guint32 offset, guint8 ** next_start, + guint32 * next_size) { int ret = 0; guint8 mark[2] = { 0x00, 0x00 }; + const guint8 *step_buf = buf; *next_start = buf; @@ -126,8 +130,8 @@ metadataparse_jpeg_parse (JpegData * jpeg_data, guint8 * buf, switch (jpeg_data->state) { case JPEG_READING: ret = - metadataparse_jpeg_reading (jpeg_data, &buf, bufsize, next_start, - next_size); + metadataparse_jpeg_reading (jpeg_data, &buf, bufsize, + offset, step_buf, next_start, next_size); break; case JPEG_JUMPING: ret = @@ -168,13 +172,15 @@ done: /* look for markers */ static int metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size) + guint32 * bufsize, const guint32 offset, const guint8 * step_buf, + guint8 ** next_start, guint32 * next_size) { int ret = -1; guint8 mark[2] = { 0x00, 0x00 }; guint16 chunk_size = 0; + static const char JfifHeader[] = "JFIF"; static const unsigned char ExifHeader[] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 }; static const char IptcHeader[] = "Photoshop 3.0"; @@ -211,7 +217,21 @@ metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, chunk_size = READ (*buf, *bufsize) << 8; chunk_size += READ (*buf, *bufsize); - if (mark[1] == 0xE1) { /* may be it is Exif or XMP */ + if (mark[1] == 0xE0) { /* may be JFIF */ + + if (chunk_size >= 16) { + if (*bufsize < 14) { + *next_size = (*buf - *next_start) + 14; + ret = 1; + goto done; + } + + if (0 == memcmp (JfifHeader, *buf, 5)) { + jpeg_data->jfif_found = TRUE; + } + } + + } else if (mark[1] == 0xE1) { /* may be it is Exif or XMP */ if (chunk_size >= 8) { /* size2 'EXIF' 0x00 0x00 */ guint8 ch; @@ -222,10 +242,27 @@ metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, goto done; } - if (jpeg_data->adpt_exif) { + if (jpeg_data->exif) { if (0 == memcmp (ExifHeader, *buf, 6)) { + jpeg_data->exif->offset = (*buf - step_buf) + offset - 4; /* maker + size */ + jpeg_data->exif->size = chunk_size + 2; /* chunk size plus app marker */ jpeg_data->read = chunk_size - 2; ret = 0; + + if (!jpeg_data->jfif_found) { + static const guint8 chunk[] = { 0xff, 0xe0, 0x00, 0x10, + 0x4a, 0x46, 0x49, 0x46, 0x00, + 0x01, 0x02, + 0x00, 0x00, 0x01, 0x00, 0x01, + 0x00, 0x00 + }; + + /* only inject if no JFIF has been found */ + jpeg_data->exif->size_inject = 18; + jpeg_data->exif->buffer = malloc (18); + memcpy (jpeg_data->exif->buffer, chunk, 18); + } + jpeg_data->state = JPEG_EXIF; goto done; } @@ -237,8 +274,10 @@ metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, goto done; } - if (jpeg_data->adpt_xmp) { + if (jpeg_data->xmp) { if (0 == memcmp (XmpHeader, *buf, 29)) { + jpeg_data->xmp->offset = (*buf - step_buf) + offset - 4; /* maker + size */ + jpeg_data->xmp->size = chunk_size + 2; /* chunk size plus app marker */ *buf += 29; *bufsize -= 29; jpeg_data->read = chunk_size - 2 - 29; @@ -258,8 +297,10 @@ metadataparse_jpeg_reading (JpegData * jpeg_data, guint8 ** buf, goto done; } - if (jpeg_data->adpt_iptc) { + if (jpeg_data->iptc) { if (0 == memcmp (IptcHeader, *buf, 14)) { + jpeg_data->iptc->offset = (*buf - step_buf) + offset - 4; /* maker + size */ + jpeg_data->iptc->size = chunk_size + 2; /* chunk size plus app marker */ jpeg_data->read = chunk_size - 2; ret = 0; jpeg_data->state = JPEG_IPTC; @@ -294,9 +335,13 @@ metadataparse_jpeg_exif (JpegData * jpeg_data, guint8 ** buf, int ret; ret = metadataparse_util_hold_chunk (&jpeg_data->read, buf, - bufsize, next_start, next_size, jpeg_data->adpt_exif); + bufsize, next_start, next_size, &jpeg_data->exif->adapter); if (ret == 0) { + jpeg_data->state = JPEG_READING; + + /* if there is a second Exif chunk in the file it will be jumped */ + jpeg_data->exif = NULL; } return ret; @@ -310,7 +355,7 @@ metadataparse_jpeg_iptc (JpegData * jpeg_data, guint8 ** buf, int ret; ret = metadataparse_util_hold_chunk (&jpeg_data->read, buf, - bufsize, next_start, next_size, jpeg_data->adpt_iptc); + bufsize, next_start, next_size, &jpeg_data->iptc->adapter); if (ret == 0) { @@ -322,8 +367,8 @@ metadataparse_jpeg_iptc (JpegData * jpeg_data, guint8 ** buf, jpeg_data->state = JPEG_READING; - size = gst_adapter_available (*jpeg_data->adpt_iptc); - buf = gst_adapter_peek (*jpeg_data->adpt_iptc, size); + size = gst_adapter_available (jpeg_data->iptc->adapter); + buf = gst_adapter_peek (jpeg_data->iptc->adapter, size); res = iptc_jpeg_ps3_find_iptc (buf, size, &iptc_len); @@ -332,19 +377,22 @@ metadataparse_jpeg_iptc (JpegData * jpeg_data, guint8 ** buf, ret = -1; } else if (res == 0) { /* no iptc data found */ - gst_adapter_clear (*jpeg_data->adpt_iptc); + gst_adapter_clear (jpeg_data->iptc->adapter); } else { - gst_adapter_flush (*jpeg_data->adpt_iptc, res); - size = gst_adapter_available (*jpeg_data->adpt_iptc); + gst_adapter_flush (jpeg_data->iptc->adapter, res); + size = gst_adapter_available (jpeg_data->iptc->adapter); if (size > iptc_len) { GstBuffer *buf; - buf = gst_adapter_take_buffer (*jpeg_data->adpt_iptc, iptc_len); - gst_adapter_clear (*jpeg_data->adpt_iptc); - gst_adapter_push (*jpeg_data->adpt_iptc, buf); + buf = gst_adapter_take_buffer (jpeg_data->iptc->adapter, iptc_len); + gst_adapter_clear (jpeg_data->iptc->adapter); + gst_adapter_push (jpeg_data->iptc->adapter, buf); } } + /* if there is a second Iptc chunk in the file it will be jumped */ + jpeg_data->iptc = NULL; + } return ret; @@ -358,10 +406,12 @@ metadataparse_jpeg_xmp (JpegData * jpeg_data, guint8 ** buf, int ret; ret = metadataparse_util_hold_chunk (&jpeg_data->read, buf, - bufsize, next_start, next_size, jpeg_data->adpt_xmp); + bufsize, next_start, next_size, &jpeg_data->xmp->adapter); if (ret == 0) { jpeg_data->state = JPEG_READING; + /* if there is a second XMP chunk in the file it will be jumped */ + jpeg_data->xmp = NULL; } return ret; } diff --git a/ext/metadata/metadataparsejpeg.h b/ext/metadata/metadataparsejpeg.h index d7f1660d..cc7cd6e2 100644 --- a/ext/metadata/metadataparsejpeg.h +++ b/ext/metadata/metadataparsejpeg.h @@ -44,7 +44,7 @@ #ifndef __METADATAPARSE_JPEG_H__ #define __METADATAPARSE_JPEG_H__ -#include <gst/base/gstadapter.h> +#include "metadataparsetypes.h" G_BEGIN_DECLS @@ -63,23 +63,24 @@ typedef enum _tag_JpegState typedef struct _tag_JpegData { JpegState state; - GstAdapter **adpt_exif; - GstAdapter **adpt_iptc; - GstAdapter **adpt_xmp; + MetadataChunk *exif; + MetadataChunk *iptc; + MetadataChunk *xmp; guint32 read; + gboolean jfif_found; } JpegData; extern void -metadataparse_jpeg_init (JpegData * jpeg_data, GstAdapter ** adpt_exif, - GstAdapter ** adpt_iptc, GstAdapter ** adpt_xmp); +metadataparse_jpeg_init (JpegData * jpeg_data, MetadataChunk * adpt_exif, + MetadataChunk * adpt_iptc, MetadataChunk * adpt_xmp); extern void metadataparse_jpeg_dispose (JpegData * jpeg_data); int metadataparse_jpeg_parse (JpegData * jpeg_data, guint8 * buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size); + guint32 * bufsize, const guint32 offset, guint8 ** next_start, guint32 * next_size); G_END_DECLS #endif /* __METADATAPARSE_JPEG_H__ */ diff --git a/ext/metadata/metadataparsepng.c b/ext/metadata/metadataparsepng.c index 9c3241cf..829ad65e 100644 --- a/ext/metadata/metadataparsepng.c +++ b/ext/metadata/metadataparsepng.c @@ -47,7 +47,8 @@ static int metadataparse_png_reading (PngData * png_data, guint8 ** buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size); + guint32 * bufsize, const guint32 offset, const guint8 * step_buf, + guint8 ** next_start, guint32 * next_size); static int metadataparse_png_xmp (PngData * png_data, guint8 ** buf, @@ -60,11 +61,11 @@ metadataparse_png_jump (PngData * png_data, guint8 ** buf, #define READ(buf, size) ( (size)--, *((buf)++) ) void -metadataparse_png_init (PngData * png_data, GstAdapter ** adpt_exif, - GstAdapter ** adpt_iptc, GstAdapter ** adpt_xmp) +metadataparse_png_init (PngData * png_data, MetadataChunk * exif, + MetadataChunk * iptc, MetadataChunk * xmp) { png_data->state = PNG_NULL; - png_data->adpt_xmp = adpt_xmp; + png_data->xmp = xmp; png_data->read = 0; metadataparse_xmp_init (); @@ -75,16 +76,18 @@ metadataparse_png_dispose (PngData * png_data) { metadataparse_xmp_dispose (); - png_data->adpt_xmp = NULL; + png_data->xmp = NULL; } int metadataparse_png_parse (PngData * png_data, guint8 * buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size) + guint32 * bufsize, const guint32 offset, guint8 ** next_start, + guint32 * next_size) { int ret = 0; guint8 mark[8]; + const guint8 *step_buf = buf; *next_start = buf; @@ -120,8 +123,8 @@ metadataparse_png_parse (PngData * png_data, guint8 * buf, switch (png_data->state) { case PNG_READING: ret = - metadataparse_png_reading (png_data, &buf, bufsize, next_start, - next_size); + metadataparse_png_reading (png_data, &buf, bufsize, + offset, step_buf, next_start, next_size); break; case PNG_JUMPING: ret = @@ -152,7 +155,8 @@ done: /* look for markers */ static int metadataparse_png_reading (PngData * png_data, guint8 ** buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size) + guint32 * bufsize, const guint32 offset, const guint8 * step_buf, + guint8 ** next_start, guint32 * next_size) { int ret = -1; @@ -193,8 +197,10 @@ metadataparse_png_reading (PngData * png_data, guint8 ** buf, goto done; } - if (png_data->adpt_xmp) { + if (png_data->xmp) { if (0 == memcmp (XmpHeader, *buf, 18)) { + png_data->xmp->offset = (*buf - step_buf) + offset - 8; /* maker + size */ + png_data->xmp->size = chunk_size + 12; /* chunk size plus app marker plus crc */ *buf += 22; /* jump "XML:com.adobe.xmp" plus some flags */ *bufsize -= 22; png_data->read = chunk_size - 22; /* four CRC bytes at the end will be jumped after */ @@ -234,11 +240,13 @@ metadataparse_png_xmp (PngData * png_data, guint8 ** buf, int ret; ret = metadataparse_util_hold_chunk (&png_data->read, buf, - bufsize, next_start, next_size, png_data->adpt_xmp); + bufsize, next_start, next_size, &png_data->xmp->adapter); if (ret == 0) { /* jump four CRC bytes at the end of chunk */ png_data->read = 4; png_data->state = PNG_JUMPING; + /* if there is a second XMP chunk in the file it will be jumped */ + png_data->xmp = NULL; } return ret; diff --git a/ext/metadata/metadataparsepng.h b/ext/metadata/metadataparsepng.h index 99f0f7ea..c059450f 100644 --- a/ext/metadata/metadataparsepng.h +++ b/ext/metadata/metadataparsepng.h @@ -44,7 +44,7 @@ #ifndef __METADATAPARSE_PNG_H__ #define __METADATAPARSE_PNG_H__ -#include <gst/base/gstadapter.h> +#include "metadataparsetypes.h" G_BEGIN_DECLS @@ -61,21 +61,21 @@ typedef enum _tag_PngState typedef struct _tag_PngData { PngState state; - GstAdapter **adpt_xmp; + MetadataChunk *xmp; guint32 read; } PngData; extern void -metadataparse_png_init (PngData * png_data, GstAdapter ** adpt_exif, - GstAdapter ** adpt_iptc, GstAdapter ** adpt_xmp); +metadataparse_png_init (PngData * png_data, MetadataChunk *exif, + MetadataChunk * iptc, MetadataChunk *xmp); extern void metadataparse_png_dispose (PngData * png_data); int metadataparse_png_parse (PngData * png_data, guint8 * buf, - guint32 * bufsize, guint8 ** next_start, guint32 * next_size); + guint32 * bufsize, const guint32 offset, guint8 ** next_start, guint32 * next_size); G_END_DECLS #endif /* __METADATAPARSE_PNG_H__ */ diff --git a/ext/metadata/metadataparsetypes.h b/ext/metadata/metadataparsetypes.h new file mode 100644 index 00000000..7fd74fb4 --- /dev/null +++ b/ext/metadata/metadataparsetypes.h @@ -0,0 +1,64 @@ +/* + * GStreamer + * Copyright 2007 Edgard Lima <edgard.lima@indt.org.br> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Alternatively, the contents of this file may be used under the + * GNU Lesser General Public License Version 2.1 (the "LGPL"), in + * which case the following provisions apply instead of the ones + * mentioned above: + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __METADATAPARSETYPES_H__ +#define __METADATAPARSETYPES_H__ + +#include <gst/base/gstadapter.h> + +G_BEGIN_DECLS + +typedef struct _tag_MetadataChunk +{ + gint64 offset; /* from the beginning of file */ + guint32 size; /* size of bytes in chunk (same as adapter size) */ + GstAdapter *adapter; + /* in some cases, when there is JPEG/EXIF, and no JIF as first chunk + is is need to add a chunk data back to the file */ + guint32 size_inject; /* same as buffer size */ + guint8 * buffer; +} MetadataChunk; + + +G_END_DECLS +#endif /* __METADATAPARSETYPES_H__ */ |