~ubuntu-branches/ubuntu/precise/gst-plugins-bad0.10/precise-proposed

« back to all changes in this revision

Viewing changes to ext/metadata/metadata.c

Tags: upstream-0.10.5.3
Import upstream version 0.10.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * GStreamer
 
3
 * Copyright 2007 Edgard Lima <edgard.lima@indt.org.br>
 
4
 * 
 
5
 * Permission is hereby granted, free of charge, to any person obtaining a
 
6
 * copy of this software and associated documentation files (the "Software"),
 
7
 * to deal in the Software without restriction, including without limitation
 
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
9
 * and/or sell copies of the Software, and to permit persons to whom the
 
10
 * Software is furnished to do so, subject to the following conditions:
 
11
 *
 
12
 * The above copyright notice and this permission notice shall be included in
 
13
 * all copies or substantial portions of the Software.
 
14
 *
 
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
21
 * DEALINGS IN THE SOFTWARE.
 
22
 *
 
23
 * Alternatively, the contents of this file may be used under the
 
24
 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
 
25
 * which case the following provisions apply instead of the ones
 
26
 * mentioned above:
 
27
 *
 
28
 * This library is free software; you can redistribute it and/or
 
29
 * modify it under the terms of the GNU Library General Public
 
30
 * License as published by the Free Software Foundation; either
 
31
 * version 2 of the License, or (at your option) any later version.
 
32
 *
 
33
 * This library is distributed in the hope that it will be useful,
 
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
36
 * Library General Public License for more details.
 
37
 *
 
38
 * You should have received a copy of the GNU Library General Public
 
39
 * License along with this library; if not, write to the
 
40
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
41
 * Boston, MA 02111-1307, USA.
 
42
 */
 
43
 
 
44
/*
 
45
 * SECTION: metadata
 
46
 * @short_description: This module provides high-level functions to parse files
 
47
 *
 
48
 * This module find out the stream type (JPEG or PNG), and provide functions to
 
49
 * the caller to know where are the metadata chunks and where should it be
 
50
 * written, as well, it gives the caller the metedata chunk to be written and
 
51
 * also gets a metadata chunk and wraps it according the strem type
 
52
 * specification.
 
53
 *
 
54
 * <refsect2>
 
55
 * <para>
 
56
 * #metadata_init must be called before any other function in this module and
 
57
 * must be paired with a call to #metadata_dispose. #metadata_parse is used to
 
58
 * parse the stream (find the metadata chunks and the place it should be
 
59
 * written to. And #metadata_lazy_update is used by muxers to wrap the metadata
 
60
 * chunk according the stream type specification. Actually after indentify the
 
61
 * stream type, the real jog of parsing is delivered to speciallized module.
 
62
 * See, #metadata[mux/parse][jpeg/png].[c/h] files.
 
63
 * </para>
 
64
 * </refsect2>
 
65
 *
 
66
 * Last reviewed on 2008-01-24 (0.10.15)
 
67
 */
 
68
 
 
69
/*
 
70
 * includes
 
71
 */
 
72
 
 
73
#include <string.h>
 
74
 
 
75
#include "metadata.h"
 
76
 
 
77
/*
 
78
 * static helper functions declaration
 
79
 */
 
80
 
 
81
static MetadataParsingReturn
 
82
metadata_parse_none (MetaData * meta_data, const guint8 * data,
 
83
    guint32 * data_size, guint8 ** next_start, guint32 * next_size);
 
84
 
 
85
/*
 
86
 * extern functions implementations
 
87
 */
 
88
 
 
89
/*
 
90
 * metadata_init:
 
91
 * @meta_data: [in] metadata handler to be inited
 
92
 * @options: [in] which types of metadata will be processed (EXIF, IPTC and/or
 
93
 * XMP) and how it will be handled (DEMUXING or MUXING). Look at #MetaOptions
 
94
 * to see the available options.
 
95
 *
 
96
 * Init metadata handle.
 
97
 * This function must be called before any other function from this module.
 
98
 * This function must not be called twice without call to #metadata_dispose
 
99
 * beteween them.
 
100
 * @see_also: #metadata_dispose #metadata_parse
 
101
 *
 
102
 * Returns: nothing
 
103
 */
 
104
 
 
105
void
 
106
metadata_init (MetaData ** meta_data, const MetaOptions options)
 
107
{
 
108
 
 
109
  if (meta_data == NULL)
 
110
    return;
 
111
  if ((*meta_data))
 
112
    metadata_dispose (meta_data);
 
113
 
 
114
  (*meta_data) = g_new (MetaData, 1);
 
115
 
 
116
  (*meta_data)->state = STATE_NULL;
 
117
  (*meta_data)->img_type = IMG_NONE;
 
118
  (*meta_data)->options = options;
 
119
  (*meta_data)->offset_orig = 0;
 
120
  (*meta_data)->exif_adapter = NULL;
 
121
  (*meta_data)->iptc_adapter = NULL;
 
122
  (*meta_data)->xmp_adapter = NULL;
 
123
 
 
124
  if ((*meta_data)->options & META_OPT_DEMUX) {
 
125
    /* when parsing we will probably strip only 3 chunk (exif, iptc and xmp)
 
126
       so we use 4 just in case there is more than one chunk of them.
 
127
       But this is just for convinience, 'cause the chunk_array increases
 
128
       dinamically */
 
129
    metadata_chunk_array_init (&(*meta_data)->strip_chunks, 4);
 
130
    /* at most 1 chunk will be injected (JPEG JFIF) */
 
131
    metadata_chunk_array_init (&(*meta_data)->inject_chunks, 1);
 
132
  } else {
 
133
    /* at most 1 chunk will be striped (JPEG JFIF) */
 
134
    metadata_chunk_array_init (&(*meta_data)->strip_chunks, 1);
 
135
    /* at most 3 chunk will be injected (EXIF, IPTC, XMP) */
 
136
    metadata_chunk_array_init (&(*meta_data)->inject_chunks, 3);
 
137
  }
 
138
 
 
139
}
 
140
 
 
141
/*
 
142
 * metadata_dispose:
 
143
 * @meta_data: [in] metadata handler to be freed
 
144
 *
 
145
 * Call this function to free any resource allocated by #metadata_init
 
146
 * @see_also: #metadata_init
 
147
 *
 
148
 * Returns: nothing
 
149
 */
 
150
 
 
151
void
 
152
metadata_dispose (MetaData ** meta_data)
 
153
{
 
154
 
 
155
  if (meta_data == NULL || (*meta_data) == NULL)
 
156
    return;
 
157
 
 
158
  switch ((*meta_data)->img_type) {
 
159
    case IMG_JPEG:
 
160
      if (G_LIKELY ((*meta_data)->options & META_OPT_DEMUX))
 
161
        metadataparse_jpeg_dispose (&(*meta_data)->format_data.jpeg_parse);
 
162
      else
 
163
        metadatamux_jpeg_dispose (&(*meta_data)->format_data.jpeg_mux);
 
164
      break;
 
165
    case IMG_PNG:
 
166
      if (G_LIKELY ((*meta_data)->options & META_OPT_DEMUX))
 
167
        metadataparse_png_dispose (&(*meta_data)->format_data.png_parse);
 
168
      else
 
169
        metadatamux_png_dispose (&(*meta_data)->format_data.png_mux);
 
170
      break;
 
171
    case IMG_NONE:
 
172
    default:
 
173
      break;
 
174
  }
 
175
 
 
176
  metadata_chunk_array_free (&(*meta_data)->strip_chunks);
 
177
  metadata_chunk_array_free (&(*meta_data)->inject_chunks);
 
178
 
 
179
  if ((*meta_data)->xmp_adapter) {
 
180
    gst_object_unref ((*meta_data)->xmp_adapter);
 
181
    (*meta_data)->xmp_adapter = NULL;
 
182
  }
 
183
 
 
184
  if ((*meta_data)->iptc_adapter) {
 
185
    gst_object_unref ((*meta_data)->iptc_adapter);
 
186
    (*meta_data)->iptc_adapter = NULL;
 
187
  }
 
188
 
 
189
  if ((*meta_data)->exif_adapter) {
 
190
    gst_object_unref ((*meta_data)->exif_adapter);
 
191
    (*meta_data)->exif_adapter = NULL;
 
192
  }
 
193
 
 
194
  g_free (*meta_data);
 
195
  *meta_data = NULL;
 
196
 
 
197
}
 
198
 
 
199
 
 
200
/*
 
201
 * metadata_parse:
 
202
 * @meta_data: [in] metadata handle
 
203
 * @buf: [in] data to be parsed
 
204
 * @buf_size: [in] size of @buf in bytes
 
205
 * @next_offset: [out] number of bytes to jump from the begining of @buf in
 
206
 * the next call. i.e, 0 (zero) mean that in the next call this function @buf
 
207
 * must have the same data (probably resized, see @next_size)
 
208
 * @next_size: [out] number of minimal bytes in @buf for the next call to this
 
209
 * function
 
210
 *
 
211
 * This function is used to parse the stream step-by-step incrementaly, which
 
212
 * means, discover the stream type and find the metadata chunks
 
213
 * (#META_OPT_DEMUX),  or point out where metadata chunks should be written 
 
214
 * (#META_OPT_MUX). It is important to notice that there could be both strip
 
215
 * and inject chunks in both demuxing and muxing modes.
 
216
 * @see_also: #metadata_init #META_DATA_STRIP_CHUNKS #META_DATA_INJECT_CHUNKS
 
217
 *
 
218
 * Returns:
 
219
 * <itemizedlist>
 
220
 * <listitem><para>%META_PARSING_ERROR
 
221
 * </para></listitem>
 
222
 * <listitem><para>%META_PARSING_DONE if parse has finished. Now strip and
 
223
 * inject chunks has been found
 
224
 * </para></listitem>
 
225
 * <listitem><para>%META_PARSING_NEED_MORE_DATA if this function should be
 
226
 * called again (look @next_offset and @next_size)
 
227
 * </para></listitem>
 
228
 * </itemizedlist>
 
229
 */
 
230
 
 
231
MetadataParsingReturn
 
232
metadata_parse (MetaData * meta_data, const guint8 * buf,
 
233
    guint32 buf_size, guint32 * next_offset, guint32 * next_size)
 
234
{
 
235
 
 
236
  int ret = META_PARSING_DONE;
 
237
 
 
238
  guint8 *next_start = (guint8 *) buf;
 
239
 
 
240
  if (meta_data->state == STATE_NULL) {
 
241
    ret =
 
242
        metadata_parse_none (meta_data, buf, &buf_size, &next_start, next_size);
 
243
    if (ret == META_PARSING_DONE)
 
244
      meta_data->state = STATE_READING;
 
245
    else
 
246
      goto done;
 
247
  }
 
248
 
 
249
  switch (meta_data->img_type) {
 
250
    case IMG_JPEG:
 
251
      if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
252
        ret =
 
253
            metadataparse_jpeg_parse (&meta_data->format_data.jpeg_parse,
 
254
            (guint8 *) buf, &buf_size, meta_data->offset_orig, &next_start,
 
255
            next_size);
 
256
      else
 
257
        ret =
 
258
            metadatamux_jpeg_parse (&meta_data->format_data.jpeg_mux,
 
259
            (guint8 *) buf, &buf_size, meta_data->offset_orig, &next_start,
 
260
            next_size);
 
261
      break;
 
262
    case IMG_PNG:
 
263
      if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
264
        ret =
 
265
            metadataparse_png_parse (&meta_data->format_data.png_parse,
 
266
            (guint8 *) buf, &buf_size, meta_data->offset_orig, &next_start,
 
267
            next_size);
 
268
      else
 
269
        ret =
 
270
            metadatamux_png_parse (&meta_data->format_data.png_mux,
 
271
            (guint8 *) buf, &buf_size, meta_data->offset_orig, &next_start,
 
272
            next_size);
 
273
      break;
 
274
    default:
 
275
      /* unexpected */
 
276
      ret = META_PARSING_ERROR;
 
277
      goto done;
 
278
      break;
 
279
  }
 
280
 
 
281
  *next_offset = next_start - buf;
 
282
  meta_data->offset_orig += *next_offset;
 
283
 
 
284
done:
 
285
 
 
286
  if (ret == META_PARSING_DONE) {
 
287
    meta_data->state = STATE_DONE;
 
288
  }
 
289
 
 
290
  return ret;
 
291
}
 
292
 
 
293
/*
 
294
 * metadata_lazy_update:
 
295
 * @meta_data: [in] metata handle
 
296
 *
 
297
 * This function must be called after #metadata_parse and after the element
 
298
 * has modified the segments (chunks)
 
299
 * Data written to #META_DATA_INJECT_CHUNKS will be properly wrapped
 
300
 * This function is really importante in case o muxing 'cause:
 
301
 * 1- 'cause gives the oportunity to muxers to wrapper new segments with
 
302
 * apropriate bytes
 
303
 *   ex: in case of JPEG it can wrap the EXIF chunk (created using tags) with
 
304
 * chunk id and chunk size
 
305
 * 2- 'cause gives the oportunity to muxer to decide if some chunks should
 
306
 * still be striped/injected
 
307
 *   ex: if there is no EXIF chunk to be inserted, the muxer decides to not
 
308
 * strip JFIF anymore
 
309
 * @see_also: #metadata_parse #META_DATA_INJECT_CHUNKS
 
310
 *
 
311
 * Returns: nothing
 
312
 */
 
313
 
 
314
void
 
315
metadata_lazy_update (MetaData * meta_data)
 
316
{
 
317
  switch (meta_data->img_type) {
 
318
    case IMG_JPEG:
 
319
      if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
320
        metadataparse_jpeg_lazy_update (&meta_data->format_data.jpeg_parse);
 
321
      else
 
322
        metadatamux_jpeg_lazy_update (&meta_data->format_data.jpeg_mux);
 
323
      break;
 
324
    case IMG_PNG:
 
325
      if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
326
        metadataparse_png_lazy_update (&meta_data->format_data.png_parse);
 
327
      else
 
328
        metadatamux_png_lazy_update (&meta_data->format_data.png_mux);
 
329
      break;
 
330
    default:
 
331
      /* unexpected */
 
332
      break;
 
333
  }
 
334
 
 
335
}
 
336
 
 
337
 
 
338
/*
 
339
 * static helper functions implementation
 
340
 */
 
341
 
 
342
/*
 
343
 * metadata_parse_none:
 
344
 * @meta_data: [in] metata handle
 
345
 * @buf: [in] data to be parsed
 
346
 * @buf_size: [in] size of @buf in bytes
 
347
 * @next_offset: [out] number of bytes to jump from the begining of @buf in
 
348
 * the next call. i.e, 0 (zero) mean that in the next call this function @buf
 
349
 * must have the same data (probably resized, see @next_size)
 
350
 * @next_size: [out] number of minimal bytes in @buf for the next call to this
 
351
 * function
 
352
 *
 
353
 * Parse the fisrt bytes of the stream to identify the stream type
 
354
 * @see_also: metadata_parse
 
355
 *
 
356
 * Returns:
 
357
 * <itemizedlist>
 
358
 * <listitem><para>%META_PARSING_ERROR none of the alloed strem types (JPEG,
 
359
 * PNG) has been identified
 
360
 * </para></listitem>
 
361
 * <listitem><para>%META_PARSING_DONE if the stream type has been identified
 
362
 * </para></listitem>
 
363
 * <listitem><para>%META_PARSING_NEED_MORE_DATA if this function should be
 
364
 * called again (look @next_offset and @next_size)
 
365
 * </para></listitem>
 
366
 * </itemizedlist>
 
367
 */
 
368
static MetadataParsingReturn
 
369
metadata_parse_none (MetaData * meta_data, const guint8 * buf,
 
370
    guint32 * buf_size, guint8 ** next_start, guint32 * next_size)
 
371
{
 
372
 
 
373
  int ret = META_PARSING_ERROR;
 
374
  GstAdapter **exif = NULL;
 
375
  GstAdapter **iptc = NULL;
 
376
  GstAdapter **xmp = NULL;
 
377
 
 
378
  *next_start = (guint8 *) buf;
 
379
 
 
380
  meta_data->img_type = IMG_NONE;
 
381
 
 
382
  /*
 
383
   * Be sure of add checking for more types in order from the
 
384
   * less to more bytes need to detect the stream type.
 
385
   */
 
386
 
 
387
  /* we need at least 3 bytes to see if it is JPEG */
 
388
  if (*buf_size < 3) {
 
389
    *next_size = 3;
 
390
    ret = META_PARSING_NEED_MORE_DATA;
 
391
    goto done;
 
392
  }
 
393
 
 
394
  if (meta_data->options & META_OPT_EXIF)
 
395
    exif = &meta_data->exif_adapter;
 
396
  if (meta_data->options & META_OPT_IPTC)
 
397
    iptc = &meta_data->iptc_adapter;
 
398
  if (meta_data->options & META_OPT_XMP)
 
399
    xmp = &meta_data->xmp_adapter;
 
400
 
 
401
  if (buf[0] == 0xFF && buf[1] == 0xD8 && buf[2] == 0xFF) {
 
402
    if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
403
      metadataparse_jpeg_init (&meta_data->format_data.jpeg_parse, exif, iptc,
 
404
          xmp, &meta_data->strip_chunks, &meta_data->inject_chunks,
 
405
          meta_data->options & META_OPT_PARSE_ONLY);
 
406
    else
 
407
      metadatamux_jpeg_init (&meta_data->format_data.jpeg_mux,
 
408
          &meta_data->strip_chunks, &meta_data->inject_chunks);
 
409
    ret = META_PARSING_DONE;
 
410
    meta_data->img_type = IMG_JPEG;
 
411
    goto done;
 
412
  }
 
413
 
 
414
  /* we need at least 8 bytes to see if it is PNG */
 
415
  if (*buf_size < 8) {
 
416
    *next_size = 8;
 
417
    ret = META_PARSING_NEED_MORE_DATA;
 
418
    goto done;
 
419
  }
 
420
 
 
421
  if (buf[0] == 0x89 && buf[1] == 0x50 && buf[2] == 0x4E && buf[3] == 0x47 &&
 
422
      buf[4] == 0x0D && buf[5] == 0x0A && buf[6] == 0x1A && buf[7] == 0x0A) {
 
423
    if (G_LIKELY (meta_data->options & META_OPT_DEMUX))
 
424
      metadataparse_png_init (&meta_data->format_data.png_parse, exif, iptc,
 
425
          xmp, &meta_data->strip_chunks, &meta_data->inject_chunks,
 
426
          meta_data->options & META_OPT_PARSE_ONLY);
 
427
    else
 
428
      metadatamux_png_init (&meta_data->format_data.png_mux,
 
429
          &meta_data->strip_chunks, &meta_data->inject_chunks);
 
430
    ret = META_PARSING_DONE;
 
431
    meta_data->img_type = IMG_PNG;
 
432
    goto done;
 
433
  }
 
434
 
 
435
done:
 
436
 
 
437
  return ret;
 
438
}