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

« back to all changes in this revision

Viewing changes to ext/metadata/metadataiptc.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: metadataiptc
 
46
 * @short_description: This module provides functions to extract tags from
 
47
 * IPTC metadata chunks and create IPTC chunks from metadata tags.
 
48
 * @see_also: #metadatatags.[c/h]
 
49
 *
 
50
 * If libiptcdata isn't available at compilation time, only the whole chunk
 
51
 * (#METADATA_TAG_MAP_WHOLECHUNK) tags is created. It means that individual
 
52
 * tags aren't mapped.
 
53
 *
 
54
 * Last reviewed on 2008-01-24 (0.10.15)
 
55
 */
 
56
 
 
57
/*
 
58
 * includes
 
59
 */
 
60
 
 
61
#include "metadataiptc.h"
 
62
#include "metadataparseutil.h"
 
63
#include "metadatatags.h"
 
64
 
 
65
/*
 
66
 * defines
 
67
 */
 
68
 
 
69
GST_DEBUG_CATEGORY (gst_metadata_iptc_debug);
 
70
#define GST_CAT_DEFAULT gst_metadata_iptc_debug
 
71
 
 
72
/*
 
73
 * Implementation when libiptcdata isn't available at compilation time
 
74
 */
 
75
 
 
76
#ifndef HAVE_IPTC
 
77
 
 
78
/*
 
79
 * extern functions implementations
 
80
 */
 
81
 
 
82
void
 
83
metadataparse_iptc_tag_list_add (GstTagList * taglist, GstTagMergeMode mode,
 
84
    GstAdapter * adapter, MetadataTagMapping mapping)
 
85
{
 
86
 
 
87
  if (mapping & METADATA_TAG_MAP_WHOLECHUNK) {
 
88
    GST_LOG ("IPTC not defined, sending just one tag as whole chunk");
 
89
    metadataparse_util_tag_list_add_chunk (taglist, mode, GST_TAG_IPTC,
 
90
        adapter);
 
91
  }
 
92
 
 
93
}
 
94
 
 
95
 
 
96
void
 
97
metadatamux_iptc_create_chunk_from_tag_list (guint8 ** buf, guint32 * size,
 
98
    const GstTagList * taglist)
 
99
{
 
100
  /* do nothing */
 
101
}
 
102
 
 
103
#else /* ifndef HAVE_IPTC */
 
104
 
 
105
/*
 
106
 * Implementation when libiptcdata is available at compilation time
 
107
 */
 
108
 
 
109
/*
 
110
 * includes
 
111
 */
 
112
 
 
113
#include <iptc-data.h>
 
114
#include <iptc-tag.h>
 
115
#include <string.h>
 
116
#include <gst/gsttaglist.h>
 
117
 
 
118
/*
 
119
 * enum and types
 
120
 */
 
121
 
 
122
typedef struct _tag_MEUserData
 
123
{
 
124
  GstTagList *taglist;
 
125
  GstTagMergeMode mode;
 
126
} MEUserData;
 
127
 
 
128
typedef struct _tag_MapIntStr
 
129
{
 
130
  IptcRecord record;
 
131
  IptcTag iptc;
 
132
  const gchar *str;
 
133
} MapIntStr;
 
134
 
 
135
/*
 
136
 * defines and static global vars
 
137
 */
 
138
 
 
139
/* *INDENT-OFF* */
 
140
/* When changing this table, update 'metadata_mapping.htm' file too. */
 
141
static MapIntStr mappedTags[] = {
 
142
  {IPTC_RECORD_APP_2, IPTC_TAG_OBJECT_NAME,      /*ASCII*/
 
143
   GST_TAG_TITLE      /*STRING*/},
 
144
  {IPTC_RECORD_APP_2, IPTC_TAG_BYLINE,           /*ASCII*/
 
145
   GST_TAG_COMPOSER    /*STRING*/},
 
146
  {IPTC_RECORD_APP_2, IPTC_TAG_CAPTION,          /*ASCII*/
 
147
   GST_TAG_DESCRIPTION /*STRING*/},
 
148
  {IPTC_RECORD_APP_2, IPTC_TAG_COPYRIGHT_NOTICE, /*ASCII*/
 
149
   GST_TAG_COPYRIGHT   /*STRING*/},
 
150
  {0, 0, NULL}
 
151
};
 
152
/* *INDENT-ON* */
 
153
 
 
154
/*
 
155
 * static helper functions declaration
 
156
 */
 
157
 
 
158
static const gchar *metadataparse_iptc_get_tag_from_iptc (IptcTag iptc,
 
159
    GType * type, IptcRecord * record);
 
160
 
 
161
 
 
162
static IptcTag
 
163
metadatamux_iptc_get_iptc_from_tag (const gchar * tag, GType * type,
 
164
    IptcRecord * record);
 
165
 
 
166
static void
 
167
metadataparse_iptc_data_foreach_dataset_func (IptcDataSet * dataset,
 
168
    void *user_data);
 
169
 
 
170
static void
 
171
metadatamux_iptc_for_each_tag_in_list (const GstTagList * list,
 
172
    const gchar * tag, gpointer user_data);
 
173
 
 
174
/*
 
175
 * extern functions implementations
 
176
 */
 
177
 
 
178
/*
 
179
 * metadataparse_iptc_tag_list_add:
 
180
 * @taglist: tag list in which extracted tags will be added
 
181
 * @mode: tag list merge mode
 
182
 * @adapter: contains the IPTC metadata chunk
 
183
 * @mapping: if is to extract individual tags and/or the whole chunk.
 
184
 * 
 
185
 * This function gets a IPTC chunk (@adapter) and extract tags form it 
 
186
 * and then add to @taglist.
 
187
 * Note: The IPTC chunk (@adapetr) must NOT be wrapped by any bytes specific
 
188
 * to any file format
 
189
 *
 
190
 * Returns: nothing
 
191
 */
 
192
 
 
193
void
 
194
metadataparse_iptc_tag_list_add (GstTagList * taglist, GstTagMergeMode mode,
 
195
    GstAdapter * adapter, MetadataTagMapping mapping)
 
196
{
 
197
  const guint8 *buf;
 
198
  guint32 size;
 
199
  IptcData *iptc = NULL;
 
200
  MEUserData user_data = { taglist, mode };
 
201
 
 
202
  if (adapter == NULL || (size = gst_adapter_available (adapter)) == 0) {
 
203
    goto done;
 
204
  }
 
205
 
 
206
  /* add chunk tag */
 
207
  if (mapping & METADATA_TAG_MAP_WHOLECHUNK)
 
208
    metadataparse_util_tag_list_add_chunk (taglist, mode, GST_TAG_IPTC,
 
209
        adapter);
 
210
 
 
211
  if (!(mapping & METADATA_TAG_MAP_INDIVIDUALS))
 
212
    goto done;
 
213
 
 
214
  buf = gst_adapter_peek (adapter, size);
 
215
 
 
216
  iptc = iptc_data_new_from_data (buf, size);
 
217
  if (iptc == NULL) {
 
218
    goto done;
 
219
  }
 
220
 
 
221
  iptc_data_foreach_dataset (iptc,
 
222
      metadataparse_iptc_data_foreach_dataset_func, (void *) &user_data);
 
223
 
 
224
done:
 
225
 
 
226
  if (iptc)
 
227
    iptc_data_unref (iptc);
 
228
 
 
229
  return;
 
230
 
 
231
}
 
232
 
 
233
/*
 
234
 * metadatamux_iptc_create_chunk_from_tag_list:
 
235
 * @buf: buffer that will have the created IPTC chunk
 
236
 * @size: size of the buffer that will be created
 
237
 * @taglist: list of tags to be added to IPTC chunk
 
238
 *
 
239
 * Get tags from @taglist, create a IPTC chunk based on it and save to @buf.
 
240
 * Note: The IPTC chunk is NOT wrapped by any bytes specific to any file format
 
241
 *
 
242
 * Returns: nothing
 
243
 */
 
244
 
 
245
void
 
246
metadatamux_iptc_create_chunk_from_tag_list (guint8 ** buf, guint32 * size,
 
247
    const GstTagList * taglist)
 
248
{
 
249
  IptcData *iptc = NULL;
 
250
  GstBuffer *iptc_chunk = NULL;
 
251
  const GValue *val = NULL;
 
252
 
 
253
  if (!(buf && size))
 
254
    goto done;
 
255
  if (*buf) {
 
256
    g_free (*buf);
 
257
    *buf = NULL;
 
258
  }
 
259
  *size = 0;
 
260
 
 
261
  val = gst_tag_list_get_value_index (taglist, GST_TAG_IPTC, 0);
 
262
  if (val) {
 
263
    iptc_chunk = gst_value_get_buffer (val);
 
264
    if (iptc_chunk) {
 
265
      iptc = iptc_data_new_from_data (GST_BUFFER_DATA (iptc_chunk),
 
266
          GST_BUFFER_SIZE (iptc_chunk));
 
267
    }
 
268
  }
 
269
 
 
270
  if (!iptc) {
 
271
    iptc = iptc_data_new ();
 
272
  }
 
273
 
 
274
  gst_tag_list_foreach (taglist, metadatamux_iptc_for_each_tag_in_list, iptc);
 
275
 
 
276
  iptc_data_save (iptc, buf, size);
 
277
 
 
278
 
 
279
done:
 
280
 
 
281
  if (iptc)
 
282
    iptc_data_unref (iptc);
 
283
 
 
284
  return;
 
285
}
 
286
 
 
287
/*
 
288
 * static helper functions implementation
 
289
 */
 
290
 
 
291
/*
 
292
 * metadataparse_iptc_get_tag_from_iptc:
 
293
 * @iptc: IPTC tag to look for
 
294
 * @type: the type of the GStreamer tag mapped to @iptc
 
295
 * @record: the place into IPTC chunk @iptc belongs to.
 
296
 *
 
297
 * This returns the GStreamer tag mapped to an IPTC tag.
 
298
 *
 
299
 * Returns:
 
300
 * <itemizedlist>
 
301
 * <listitem><para>The GStreamer tag mapped to the @iptc
 
302
 * </para></listitem>
 
303
 * <listitem><para>%NULL if there is no mapped GST tag for @iptc
 
304
 * </para></listitem>
 
305
 * </itemizedlist>
 
306
 */
 
307
 
 
308
static const gchar *
 
309
metadataparse_iptc_get_tag_from_iptc (IptcTag iptc, GType * type,
 
310
    IptcRecord * record)
 
311
{
 
312
  int i = 0;
 
313
 
 
314
  while (mappedTags[i].iptc) {
 
315
    if (iptc == mappedTags[i].iptc) {
 
316
      *type = gst_tag_get_type (mappedTags[i].str);
 
317
      *record = mappedTags[i].record;
 
318
      break;
 
319
    }
 
320
    ++i;
 
321
  }
 
322
 
 
323
  return mappedTags[i].str;
 
324
 
 
325
}
 
326
 
 
327
/*
 
328
 * metadatamux_iptc_get_iptc_from_tag:
 
329
 * @tag: GST tag to look for
 
330
 * @type: the type of the GStreamer @tag
 
331
 * @record: the place into IPTC chunk @iptc belongs to.
 
332
 *
 
333
 * This returns thet IPTC tag mapped to an GStreamer @tag.
 
334
 *
 
335
 * Returns:
 
336
 * <itemizedlist>
 
337
 * <listitem><para>The IPTC tag mapped to the GST @tag
 
338
 * </para></listitem>
 
339
 * <listitem><para>0 if there is no mapped IPTC tag for GST @tag
 
340
 * </para></listitem>
 
341
 * </itemizedlist>
 
342
 */
 
343
 
 
344
static IptcTag
 
345
metadatamux_iptc_get_iptc_from_tag (const gchar * tag, GType * type,
 
346
    IptcRecord * record)
 
347
{
 
348
  int i = 0;
 
349
 
 
350
  while (mappedTags[i].iptc) {
 
351
    if (0 == strcmp (mappedTags[i].str, tag)) {
 
352
      *type = gst_tag_get_type (tag);
 
353
      *record = mappedTags[i].record;
 
354
      break;
 
355
    }
 
356
    ++i;
 
357
  }
 
358
 
 
359
  return mappedTags[i].iptc;
 
360
 
 
361
}
 
362
 
 
363
/*
 
364
 * metadataparse_iptc_data_foreach_dataset_func:
 
365
 * @dataset: IPTC structure from libiptcdata having IPTC tag
 
366
 * @user_data: pointer to #MEUserData
 
367
 *
 
368
 * This function designed to be called for each IPTC tag in a IPTC chunk. This
 
369
 * function gets the IPTC tag from @dataset and then add to the tag list
 
370
 * in @user_data by using a merge mode also specified in @user_data
 
371
 * @see_also: #metadataparse_iptc_tag_list_add
 
372
 *
 
373
 * Returns: nothing
 
374
 */
 
375
 
 
376
static void
 
377
metadataparse_iptc_data_foreach_dataset_func (IptcDataSet * dataset,
 
378
    void *user_data)
 
379
{
 
380
 
 
381
  char buf[1024];
 
382
  MEUserData *meudata = (MEUserData *) user_data;
 
383
  GType type;
 
384
  IptcRecord record;
 
385
  const gchar *tag =
 
386
      metadataparse_iptc_get_tag_from_iptc (dataset->tag, &type, &record);
 
387
  const gchar *value = iptc_dataset_get_as_str (dataset, buf, 1024);
 
388
 
 
389
  if (!tag)
 
390
    goto done;
 
391
 
 
392
  gst_tag_list_add (meudata->taglist, meudata->mode, tag, value, NULL);
 
393
 
 
394
done:
 
395
 
 
396
  GST_LOG ("name -> %s", iptc_tag_get_name (dataset->record, dataset->tag));
 
397
  GST_LOG ("title -> %s", iptc_tag_get_title (dataset->record, dataset->tag));
 
398
  GST_LOG ("description -> %s", iptc_tag_get_description (dataset->record,
 
399
          dataset->tag));
 
400
  GST_LOG ("value = %s", value);
 
401
  GST_LOG ("record = %d", dataset->record);
 
402
 
 
403
  return;
 
404
 
 
405
}
 
406
 
 
407
/*
 
408
 * metadatamux_iptc_for_each_tag_in_list:
 
409
 * @list: GStreamer tag list from which @tag belongs to
 
410
 * @tag: GStreamer tag to be added to the IPTC chunk
 
411
 * @user_data: pointer to #IptcData in which the tag will be added
 
412
 *
 
413
 * This function designed to be called for each tag in GST tag list. This
 
414
 * function adds get the tag value from tag @list and then add it to the IPTC
 
415
 * chunk by using #IptcData and related functions from libiptcdata
 
416
 * @see_also: #metadatamux_iptc_create_chunk_from_tag_list
 
417
 *
 
418
 * Returns: nothing
 
419
 */
 
420
 
 
421
static void
 
422
metadatamux_iptc_for_each_tag_in_list (const GstTagList * list,
 
423
    const gchar * tag, gpointer user_data)
 
424
{
 
425
  IptcData *iptc = (IptcData *) user_data;
 
426
  IptcTag iptc_tag;
 
427
  IptcRecord record;
 
428
  GType type;
 
429
  IptcDataSet *dataset = NULL;
 
430
  gboolean new_dataset = FALSE;
 
431
  gchar *tag_value = NULL;
 
432
 
 
433
  iptc_tag = metadatamux_iptc_get_iptc_from_tag (tag, &type, &record);
 
434
 
 
435
  if (!iptc_tag)
 
436
    goto done;
 
437
 
 
438
  dataset = iptc_data_get_dataset (iptc, record, iptc_tag);
 
439
 
 
440
  if (!dataset) {
 
441
    dataset = iptc_dataset_new ();
 
442
    new_dataset = TRUE;
 
443
  }
 
444
 
 
445
  iptc_dataset_set_tag (dataset, record, iptc_tag);
 
446
 
 
447
  if (gst_tag_list_get_string (list, tag, &tag_value)) {
 
448
    iptc_dataset_set_data (dataset, (guint8 *) tag_value, strlen (tag_value),
 
449
        IPTC_DONT_VALIDATE);
 
450
    g_free (tag_value);
 
451
    tag_value = NULL;
 
452
  }
 
453
 
 
454
 
 
455
  if (new_dataset)
 
456
    iptc_data_add_dataset (iptc, dataset);
 
457
 
 
458
done:
 
459
 
 
460
  if (dataset)
 
461
    iptc_dataset_unref (dataset);
 
462
}
 
463
 
 
464
 
 
465
#endif /* else (ifndef HAVE_IPTC) */