1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
2 /* Copyright 2005 Jan Schmidt <thaytan@mad.scientist.com>
3 * Copyright 2002,2003 Scott Wheeler <wheeler@kde.org> (portions from taglib)
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
26 #include <gst/tag/tag.h>
30 #define HANDLE_INVALID_SYNCSAFE
32 static gboolean id3v2_frames_to_tag_list (ID3TagsWorking * work, guint size);
34 #ifndef GST_DISABLE_GST_DEBUG
36 #define GST_CAT_DEFAULT id3v2_ensure_debug_category()
39 id3v2_ensure_debug_category (void)
41 static gsize cat_gonce = 0;
43 if (g_once_init_enter (&cat_gonce)) {
46 cat = (gsize) _gst_debug_category_new ("id3v2", 0, "ID3v2 tag parsing");
48 g_once_init_leave (&cat_gonce, cat);
51 return (GstDebugCategory *) cat_gonce;
54 #endif /* GST_DISABLE_GST_DEBUG */
57 id3v2_read_synch_uint (const guint8 * data, guint size)
66 for (i = 0; i <= size; i++) {
67 invalid |= data[i] & 0x80;
68 result |= (data[i] & 0x7f) << ((size - i) * 7);
71 #ifdef HANDLE_INVALID_SYNCSAFE
73 GST_WARNING ("Invalid synch-safe integer in ID3v2 frame "
74 "- using the actual value instead");
76 for (i = 0; i <= size; i++) {
77 result |= data[i] << ((size - i) * 8);
85 * gst_tag_get_id3v2_tag_size:
86 * @buffer: buffer holding ID3v2 tag (or at least the start of one)
88 * Determines size of an ID3v2 tag on buffer containing at least ID3v2 header,
89 * i.e. at least #GST_TAG_ID3V2_HEADER_SIZE (10) bytes;
91 * Returns: Size of tag, or 0 if header is invalid or too small.
96 gst_tag_get_id3v2_tag_size (GstBuffer * buffer)
102 g_return_val_if_fail (buffer != NULL, 0);
104 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
106 if (size < ID3V2_HDR_SIZE)
109 /* Check for 'ID3' string at start of buffer */
110 if (data[0] != 'I' || data[1] != 'D' || data[2] != '3')
116 /* Read the size from the header */
117 result = id3v2_read_synch_uint (data + 6, 4);
121 result += ID3V2_HDR_SIZE;
123 /* Expand the read size to include a footer if there is one */
124 if ((flags & ID3V2_HDR_FLAG_FOOTER))
127 GST_DEBUG ("ID3v2 tag, size: %u bytes", result);
130 gst_buffer_unmap (buffer, data, size);
136 GST_DEBUG ("size too small");
141 GST_DEBUG ("No ID3v2 tag in data");
146 GST_DEBUG ("Empty tag size");
147 result = ID3V2_HDR_SIZE;
153 id3v2_ununsync_data (const guint8 * unsync_data, guint32 * size)
159 uu = out = g_malloc (*size);
161 for (end = unsync_data + *size; unsync_data < end - 1; ++unsync_data, ++uu) {
163 if (G_UNLIKELY (*unsync_data == 0xff && *(unsync_data + 1) == 0x00))
167 /* take care of last byte (if last two bytes weren't 0xff 0x00) */
168 if (unsync_data < end) {
174 GST_DEBUG ("size after un-unsyncing: %u (before: %u)", out_size, *size);
181 * gst_tag_list_from_id3v2_tag:
182 * @buffer: buffer to convert
184 * Creates a new tag list that contains the information parsed out of a
187 * Returns: A new #GstTagList with all tags that could be extracted from the
188 * given vorbiscomment buffer or NULL on error.
193 gst_tag_list_from_id3v2_tag (GstBuffer * buffer)
195 guint8 *data, *uu_data = NULL;
202 read_size = gst_tag_get_id3v2_tag_size (buffer);
204 /* Ignore tag if it has no frames attached, but skip the header then */
205 if (read_size < ID3V2_HDR_SIZE)
208 data = gst_buffer_map (buffer, &size, NULL, GST_MAP_READ);
210 /* Read the version */
211 version = GST_READ_UINT16_BE (data + 3);
216 /* Validate the version. At the moment, we only support up to 2.4.0 */
217 if (ID3V2_VER_MAJOR (version) > 4 || ID3V2_VER_MINOR (version) > 0)
220 GST_DEBUG ("ID3v2 header flags: %s %s %s %s",
221 (flags & ID3V2_HDR_FLAG_UNSYNC) ? "UNSYNC" : "",
222 (flags & ID3V2_HDR_FLAG_EXTHDR) ? "EXTENDED_HEADER" : "",
223 (flags & ID3V2_HDR_FLAG_EXPERIMENTAL) ? "EXPERIMENTAL" : "",
224 (flags & ID3V2_HDR_FLAG_FOOTER) ? "FOOTER" : "");
226 /* This shouldn't really happen! Caller should have checked first */
227 if (size < read_size)
228 goto not_enough_data;
230 GST_DEBUG ("Reading ID3v2 tag with revision 2.%d.%d of size %u", version >> 8,
231 version & 0xff, read_size);
233 GST_MEMDUMP ("ID3v2 tag", data, read_size);
235 memset (&work, 0, sizeof (ID3TagsWorking));
236 work.buffer = buffer;
237 work.hdr.version = version;
238 work.hdr.size = read_size;
239 work.hdr.flags = flags;
240 work.hdr.frame_data = data + ID3V2_HDR_SIZE;
241 if (flags & ID3V2_HDR_FLAG_FOOTER)
242 work.hdr.frame_data_size = read_size - ID3V2_HDR_SIZE - 10;
244 work.hdr.frame_data_size = read_size - ID3V2_HDR_SIZE;
246 /* in v2.3 the frame sizes are not syncsafe, so the entire tag had to be
247 * unsynced. In v2.4 the frame sizes are syncsafe so it's just the frame
248 * data that needs un-unsyncing, but not the frame headers. */
249 if ((flags & ID3V2_HDR_FLAG_UNSYNC) != 0 && ID3V2_VER_MAJOR (version) <= 3) {
250 GST_DEBUG ("Un-unsyncing entire tag");
251 uu_data = id3v2_ununsync_data (work.hdr.frame_data,
252 &work.hdr.frame_data_size);
253 work.hdr.frame_data = uu_data;
254 GST_MEMDUMP ("ID3v2 tag (un-unsyced)", uu_data, work.hdr.frame_data_size);
257 id3v2_frames_to_tag_list (&work, work.hdr.frame_data_size);
261 gst_buffer_unmap (buffer, data, size);
268 GST_WARNING ("ID3v2 tag is from revision 2.%d.%d, "
269 "but decoder only supports 2.%d.%d. Ignoring as per spec.",
270 version >> 8, version & 0xff, ID3V2_VERSION >> 8, ID3V2_VERSION & 0xff);
271 gst_buffer_unmap (buffer, data, size);
277 ("Found ID3v2 tag with revision 2.%d.%d - need %u more bytes to read",
278 version >> 8, version & 0xff, (guint) (read_size - size));
279 gst_buffer_unmap (buffer, data, size);
285 id3v2_frame_hdr_size (guint id3v2ver)
287 /* ID3v2 < 2.3.0 only had 6 byte header */
288 switch (ID3V2_VER_MAJOR (id3v2ver)) {
300 static const gchar obsolete_frame_ids[][5] = {
301 {"CRM"}, {"EQU"}, {"LNK"}, {"RVA"}, {"TIM"}, {"TSI"}, /* From 2.2 */
302 {"EQUA"}, {"RVAD"}, {"TIME"}, {"TRDA"}, {"TSIZ"} /* From 2.3 */
305 static const struct ID3v2FrameIDConvert
309 } frame_id_conversions[] = {
335 "TDA", "TDAT"}, { /* obsolete, but we need to parse it anyway */
376 convert_fid_to_v240 (gchar * frame_id)
380 for (i = 0; i < G_N_ELEMENTS (obsolete_frame_ids); ++i) {
381 if (strncmp (frame_id, obsolete_frame_ids[i], 5) == 0)
385 for (i = 0; i < G_N_ELEMENTS (frame_id_conversions); ++i) {
386 if (strncmp (frame_id, frame_id_conversions[i].orig, 5) == 0) {
387 strcpy (frame_id, frame_id_conversions[i].new);
395 /* add unknown or unhandled ID3v2 frames to the taglist as binary blobs */
397 id3v2_add_id3v2_frame_blob_to_taglist (ID3TagsWorking * work, guint size)
405 guint frame_size, header_size;
408 switch (ID3V2_VER_MAJOR (work->hdr.version)) {
415 header_size = 4 + 4 + 2;
418 g_return_if_reached ();
421 frame_data = work->hdr.frame_data - header_size;
422 frame_size = size + header_size;
424 blob = gst_buffer_new_and_alloc (frame_size);
425 gst_buffer_fill (blob, 0, frame_data, frame_size);
427 /* Sanitize frame id */
428 for (i = 0; i < 4; i++) {
429 if (!g_ascii_isalnum (frame_data[i]))
434 media_type = g_strdup_printf ("application/x-gst-id3v2-%c%c%c%c-frame",
435 g_ascii_tolower (frame_data[0]), g_ascii_tolower (frame_data[1]),
436 g_ascii_tolower (frame_data[2]), g_ascii_tolower (frame_data[3]));
437 caps = gst_caps_new_simple (media_type, "version", G_TYPE_INT,
438 (gint) ID3V2_VER_MAJOR (work->hdr.version), NULL);
439 gst_buffer_set_caps (blob, caps);
440 gst_caps_unref (caps);
444 /* gst_util_dump_mem (GST_BUFFER_DATA (blob), GST_BUFFER_SIZE (blob)); */
446 gst_tag_list_add (work->tags, GST_TAG_MERGE_APPEND,
447 GST_TAG_ID3V2_FRAME, blob, NULL);
448 gst_buffer_unref (blob);
452 id3v2_frames_to_tag_list (ID3TagsWorking * work, guint size)
454 guint frame_hdr_size;
456 /* Extended header if present */
457 if (work->hdr.flags & ID3V2_HDR_FLAG_EXTHDR) {
458 work->hdr.ext_hdr_size = id3v2_read_synch_uint (work->hdr.frame_data, 4);
459 if (work->hdr.ext_hdr_size < 6 ||
460 (work->hdr.ext_hdr_size) > work->hdr.frame_data_size) {
461 GST_DEBUG ("Invalid extended header. Broken tag");
464 work->hdr.ext_flag_bytes = work->hdr.frame_data[4];
465 if (5 + work->hdr.ext_flag_bytes > work->hdr.frame_data_size) {
467 ("Tag claims extended header, but doesn't have enough bytes. Broken tag");
471 work->hdr.ext_flag_data = work->hdr.frame_data + 5;
472 work->hdr.frame_data += work->hdr.ext_hdr_size;
473 work->hdr.frame_data_size -= work->hdr.ext_hdr_size;
476 frame_hdr_size = id3v2_frame_hdr_size (work->hdr.version);
477 if (work->hdr.frame_data_size <= frame_hdr_size) {
478 GST_DEBUG ("Tag has no data frames. Broken tag");
479 return FALSE; /* Must have at least one frame */
482 work->tags = gst_tag_list_new_empty ();
484 while (work->hdr.frame_data_size > frame_hdr_size) {
485 guint frame_size = 0;
486 gchar frame_id[5] = "";
487 guint16 frame_flags = 0x0;
488 gboolean obsolete_id = FALSE;
489 gboolean read_synch_size = TRUE;
492 /* Read the header */
493 switch (ID3V2_VER_MAJOR (work->hdr.version)) {
497 frame_id[0] = work->hdr.frame_data[0];
498 frame_id[1] = work->hdr.frame_data[1];
499 frame_id[2] = work->hdr.frame_data[2];
502 obsolete_id = convert_fid_to_v240 (frame_id);
504 /* 3 byte non-synchsafe size */
505 frame_size = work->hdr.frame_data[3] << 16 |
506 work->hdr.frame_data[4] << 8 | work->hdr.frame_data[5];
510 read_synch_size = FALSE; /* 2.3 frame size is not synch-safe */
513 frame_id[0] = work->hdr.frame_data[0];
514 frame_id[1] = work->hdr.frame_data[1];
515 frame_id[2] = work->hdr.frame_data[2];
516 frame_id[3] = work->hdr.frame_data[3];
519 frame_size = id3v2_read_synch_uint (work->hdr.frame_data + 4, 4);
521 frame_size = GST_READ_UINT32_BE (work->hdr.frame_data + 4);
523 frame_flags = GST_READ_UINT16_BE (work->hdr.frame_data + 8);
525 if (ID3V2_VER_MAJOR (work->hdr.version) == 3) {
526 frame_flags &= ID3V2_3_FRAME_FLAGS_MASK;
527 obsolete_id = convert_fid_to_v240 (frame_id);
529 GST_DEBUG ("Ignoring v2.3 frame %s", frame_id);
534 work->hdr.frame_data += frame_hdr_size;
535 work->hdr.frame_data_size -= frame_hdr_size;
537 if (frame_size > work->hdr.frame_data_size || strcmp (frame_id, "") == 0)
538 break; /* No more frames to read */
540 /* Sanitize frame id */
541 switch (ID3V2_VER_MAJOR (work->hdr.version)) {
545 for (i = 0; i < 3; i++) {
546 if (!g_ascii_isalnum (frame_id[i]))
551 for (i = 0; i < 4; i++) {
552 if (!g_ascii_isalnum (frame_id[i]))
559 ("Frame @ %ld (0x%02lx) id %s size %u, next=%ld (0x%02lx) obsolete=%d",
560 (glong) (work->hdr.frame_data - start),
561 (glong) (work->hdr.frame_data - start), frame_id, frame_size,
562 (glong) (work->hdr.frame_data + frame_hdr_size + frame_size - start),
563 (glong) (work->hdr.frame_data + frame_hdr_size + frame_size - start),
566 #define flag_string(flag,str) \
567 ((frame_flags & (flag)) ? (str) : "")
568 GST_LOG ("Frame header flags: 0x%04x %s %s %s %s %s %s %s", frame_flags,
569 flag_string (ID3V2_FRAME_STATUS_FRAME_ALTER_PRESERVE, "ALTER_PRESERVE"),
570 flag_string (ID3V2_FRAME_STATUS_READONLY, "READONLY"),
571 flag_string (ID3V2_FRAME_FORMAT_GROUPING_ID, "GROUPING_ID"),
572 flag_string (ID3V2_FRAME_FORMAT_COMPRESSION, "COMPRESSION"),
573 flag_string (ID3V2_FRAME_FORMAT_ENCRYPTION, "ENCRYPTION"),
574 flag_string (ID3V2_FRAME_FORMAT_UNSYNCHRONISATION, "UNSYNC"),
575 flag_string (ID3V2_FRAME_FORMAT_DATA_LENGTH_INDICATOR, "LENGTH_IND"));
580 /* Now, read, decompress etc the contents of the frame
581 * into a TagList entry */
582 work->cur_frame_size = frame_size;
583 work->frame_id = frame_id;
584 work->frame_flags = frame_flags;
586 if (id3v2_parse_frame (work)) {
587 GST_LOG ("Extracted frame with id %s", frame_id);
589 GST_LOG ("Failed to extract frame with id %s", frame_id);
590 id3v2_add_id3v2_frame_blob_to_taglist (work, frame_size);
593 work->hdr.frame_data += frame_size;
594 work->hdr.frame_data_size -= frame_size;
597 if (gst_structure_n_fields (GST_STRUCTURE (work->tags)) == 0) {
598 GST_DEBUG ("Could not extract any frames from tag. Broken or empty tag");
599 gst_tag_list_free (work->tags);
604 /* Set day/month now if they were in a separate (obsolete) TDAT frame */
605 if (work->pending_day != 0 && work->pending_month != 0) {
608 if (gst_tag_list_get_date (work->tags, GST_TAG_DATE, &date)) {
609 g_date_set_day (date, work->pending_day);
610 g_date_set_month (date, work->pending_month);
611 gst_tag_list_add (work->tags, GST_TAG_MERGE_REPLACE, GST_TAG_DATE,