diff options
author | Brian Koropoff <brianhk@cs.washington.edu> | 2008-05-20 09:04:48 +0000 |
---|---|---|
committer | Sebastian Dröge <slomo@circular-chaos.org> | 2008-05-20 09:04:48 +0000 |
commit | 61dbbd46cc02f240bd7474c8e867ff67cfa8b4d4 (patch) | |
tree | 4d08d21bd29b53622ea52112d773dfc4706a3933 /ext/spc | |
parent | b641c49a4c4d1cb05b95538d83144a3ad5c1326f (diff) | |
download | gst-plugins-bad-61dbbd46cc02f240bd7474c8e867ff67cfa8b4d4.tar.gz gst-plugins-bad-61dbbd46cc02f240bd7474c8e867ff67cfa8b4d4.tar.bz2 gst-plugins-bad-61dbbd46cc02f240bd7474c8e867ff67cfa8b4d4.zip |
ext/spc/: Add support for some essential features like seeking, reading song duration and extended tags. Fixes bug #4...
Original commit message from CVS:
Patch by: Brian Koropoff <brianhk at cs dot washington dot edu>
* ext/spc/Makefile.am:
* ext/spc/gstspc.c: (gst_spc_dec_class_init),
(gst_spc_dec_src_query_type), (gst_spc_dec_init),
(gst_spc_dec_dispose), (gst_spc_dec_sink_event),
(gst_spc_duration), (gst_spc_fadeout), (gst_spc_dec_src_event),
(gst_spc_dec_src_query), (spc_play), (spc_setup):
* ext/spc/gstspc.h:
* ext/spc/tag.c: (spc_tag_is_extended), (spc_tag_is_text_format),
(spc_tag_is_present), (spc_tag_unpack_date), (spc_tag_clear),
(spc_tag_get_info), (spc_tag_free):
* ext/spc/tag.h:
Add support for some essential features like seeking, reading song
duration and extended tags. Fixes bug #454151.
Diffstat (limited to 'ext/spc')
-rw-r--r-- | ext/spc/Makefile.am | 2 | ||||
-rw-r--r-- | ext/spc/gstspc.c | 313 | ||||
-rw-r--r-- | ext/spc/gstspc.h | 9 | ||||
-rw-r--r-- | ext/spc/tag.c | 353 | ||||
-rw-r--r-- | ext/spc/tag.h | 37 |
5 files changed, 679 insertions, 35 deletions
diff --git a/ext/spc/Makefile.am b/ext/spc/Makefile.am index 18a878b2..656421bc 100644 --- a/ext/spc/Makefile.am +++ b/ext/spc/Makefile.am @@ -1,6 +1,6 @@ plugin_LTLIBRARIES = libgstspc.la -libgstspc_la_SOURCES = gstspc.c +libgstspc_la_SOURCES = gstspc.c tag.c libgstspc_la_CFLAGS = $(GST_CFLAGS) $(SPC_CFLAGS) libgstspc_la_LIBADD = $(GST_LIBS) $(SPC_LIBS) diff --git a/ext/spc/gstspc.c b/ext/spc/gstspc.c index 8c24c650..3eee98f0 100644 --- a/ext/spc/gstspc.c +++ b/ext/spc/gstspc.c @@ -1,5 +1,6 @@ /* Copyright (C) 2004-2005 Michael Pyne <michael dot pyne at kdemail net> * Copyright (C) 2004-2006 Chris Lee <clee at kde org> + * Copyright (C) 2007 Brian Koropoff <bkoropoff at gmail com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -24,12 +25,14 @@ #include "gstspc.h" #include <string.h> +#include <glib/gprintf.h> +#include <glib.h> static const GstElementDetails gst_spc_dec_details = GST_ELEMENT_DETAILS ("OpenSPC SPC decoder", "Codec/Audio/Decoder", "Uses OpenSPC to emulate an SPC processor", - "Chris Lee <clee@kde.org>"); + "Chris Lee <clee@kde.org>, Brian Koropoff <bkoropoff@gmail.com>"); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, @@ -52,6 +55,7 @@ static gboolean gst_spc_dec_src_query (GstPad * pad, GstQuery * query); static GstStateChangeReturn gst_spc_dec_change_state (GstElement * element, GstStateChange transition); static void spc_play (GstPad * pad); +static void gst_spc_dec_dispose (GObject * object); static gboolean spc_setup (GstSpcDec * spc); static gboolean @@ -117,15 +121,30 @@ static void gst_spc_dec_class_init (GstSpcDecClass * klass) { GstElementClass *element_class = (GstElementClass *) klass; + GObjectClass *gobject_class = (GObjectClass *) klass; element_class->change_state = GST_DEBUG_FUNCPTR (gst_spc_dec_change_state); + gobject_class->dispose = gst_spc_dec_dispose; } +static const GstQueryType * +gst_spc_dec_src_query_type (GstPad * pad) +{ + static const GstQueryType query_types[] = { + GST_QUERY_DURATION, + GST_QUERY_POSITION, + (GstQueryType) 0 + }; + + return query_types; +} + + static void gst_spc_dec_init (GstSpcDec * spc, GstSpcDecClass * klass) { spc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink"); - gst_pad_set_query_function (spc->sinkpad, NULL); + /* gst_pad_set_query_function (spc->sinkpad, NULL); */ gst_pad_set_event_function (spc->sinkpad, gst_spc_dec_sink_event); gst_pad_set_chain_function (spc->sinkpad, gst_spc_dec_chain); gst_element_add_pad (GST_ELEMENT (spc), spc->sinkpad); @@ -133,11 +152,26 @@ gst_spc_dec_init (GstSpcDec * spc, GstSpcDecClass * klass) spc->srcpad = gst_pad_new_from_static_template (&src_factory, "src"); gst_pad_set_event_function (spc->srcpad, gst_spc_dec_src_event); gst_pad_set_query_function (spc->srcpad, gst_spc_dec_src_query); + gst_pad_set_query_type_function (spc->srcpad, gst_spc_dec_src_query_type); gst_pad_use_fixed_caps (spc->srcpad); gst_element_add_pad (GST_ELEMENT (spc), spc->srcpad); spc->buf = NULL; spc->initialized = FALSE; + spc_tag_clear (&spc->tag_info); +} + +static void +gst_spc_dec_dispose (GObject * object) +{ + GstSpcDec *spc = GST_SPC_DEC (object); + + if (spc->buf) { + gst_buffer_unref (spc->buf); + spc->buf = NULL; + } + + spc_tag_free (&spc->tag_info); } static GstFlowReturn @@ -160,7 +194,7 @@ static gboolean gst_spc_dec_sink_event (GstPad * pad, GstEvent * event) { GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); - gboolean result; + gboolean result = TRUE; switch (GST_EVENT_TYPE (event)) { case GST_EVENT_EOS: @@ -180,6 +214,38 @@ gst_spc_dec_sink_event (GstPad * pad, GstEvent * event) return result; } +static gint64 +gst_spc_duration (GstSpcDec * spc) +{ + gint64 total_ticks = + spc->tag_info.time_intro + + spc->tag_info.time_loop * spc->tag_info.loop_count + + spc->tag_info.time_end; + if (total_ticks) { + return (gint64) gst_util_uint64_scale (total_ticks, GST_SECOND, 64000); + } else if (spc->tag_info.time_seconds) { + gint64 time = (gint64) spc->tag_info.time_seconds * GST_SECOND; + + return time; + } else { + return (gint64) (3 * 60) * GST_SECOND; + } +} + +static gint64 +gst_spc_fadeout (GstSpcDec * spc) +{ + if (spc->tag_info.time_fade) { + return (gint64) gst_util_uint64_scale ((guint64) spc->tag_info.time_fade, + GST_SECOND, 64000); + } else if (spc->tag_info.time_fade_milliseconds) { + return (gint64) (spc->tag_info.time_fade_milliseconds * GST_MSECOND); + } else { + return 10 * GST_SECOND; + } +} + + static gboolean gst_spc_dec_src_event (GstPad * pad, GstEvent * event) { @@ -187,6 +253,79 @@ gst_spc_dec_src_event (GstPad * pad, GstEvent * event) gboolean result = FALSE; switch (GST_EVENT_TYPE (event)) { + case GST_EVENT_SEEK: + { + gdouble rate; + GstFormat format; + GstSeekFlags flags; + GstSeekType start_type, stop_type; + gint64 start, stop; + gboolean flush; + + gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start, + &stop_type, &stop); + + if (format != GST_FORMAT_TIME) { + GST_DEBUG_OBJECT (spc, "seeking is only supported in TIME format"); + break; + } + + if (start_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE) { + GST_DEBUG_OBJECT (spc, "unsupported seek type"); + break; + } + + if (stop_type == GST_SEEK_TYPE_NONE) + stop = GST_CLOCK_TIME_NONE; + + if (start_type == GST_SEEK_TYPE_SET) { + guint64 cur = + gst_util_uint64_scale (spc->byte_pos, GST_SECOND, 32000 * 2 * 2); + guint64 dest = (guint64) start; + + dest = CLAMP (dest, 0, gst_spc_duration (spc) + gst_spc_fadeout (spc)); + + if (dest == cur) + break; + + flush = (flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH; + + if (flush) { + gst_pad_push_event (spc->srcpad, gst_event_new_flush_start ()); + } else { + gst_pad_stop_task (spc->srcpad); + } + + GST_PAD_STREAM_LOCK (spc->srcpad); + + if (flags & GST_SEEK_FLAG_SEGMENT) { + gst_element_post_message (GST_ELEMENT (spc), + gst_message_new_segment_start (GST_OBJECT (spc), format, cur)); + } + + if (flush) { + gst_pad_push_event (spc->srcpad, gst_event_new_flush_stop ()); + } + + if (stop == GST_CLOCK_TIME_NONE) + stop = (guint64) (gst_spc_duration (spc) + gst_spc_fadeout (spc)); + + gst_pad_push_event (spc->srcpad, gst_event_new_new_segment (FALSE, rate, + GST_FORMAT_TIME, dest, stop, dest)); + + /* spc->byte_pos += OSPC_Run(-1, NULL, (unsigned int) (gst_util_uint64_scale(dest - cur, 32000*2*2, GST_SECOND))); */ + spc->seekpoint = + gst_util_uint64_scale (dest, 32000 * 2 * 2, GST_SECOND); + spc->seeking = TRUE; + + gst_pad_start_task (spc->srcpad, (GstTaskFunction) spc_play, + spc->srcpad); + + GST_PAD_STREAM_UNLOCK (spc->srcpad); + result = TRUE; + } + break; + } default: break; } @@ -204,6 +343,33 @@ gst_spc_dec_src_query (GstPad * pad, GstQuery * query) gboolean result = TRUE; switch (GST_QUERY_TYPE (query)) { + case GST_QUERY_DURATION: + { + GstFormat format; + + gst_query_parse_duration (query, &format, NULL); + if (!spc->initialized || format != GST_FORMAT_TIME) { + result = FALSE; + break; + } + gst_query_set_duration (query, GST_FORMAT_TIME, + gst_spc_duration (spc) + gst_spc_fadeout (spc)); + break; + } + case GST_QUERY_POSITION: + { + GstFormat format; + + gst_query_parse_position (query, &format, NULL); + if (!spc->initialized || format != GST_FORMAT_TIME) { + result = FALSE; + break; + } + gst_query_set_position (query, GST_FORMAT_TIME, + (gint64) gst_util_uint64_scale (spc->byte_pos, GST_SECOND, + 32000 * 2 * 2)); + break; + } default: result = gst_pad_query_default (pad, query); break; @@ -220,11 +386,48 @@ spc_play (GstPad * pad) GstSpcDec *spc = GST_SPC_DEC (gst_pad_get_parent (pad)); GstFlowReturn flow_return; GstBuffer *out; + gboolean seeking = spc->seeking; + gint64 duration, fade, end, position; + + if (!seeking) { + out = gst_buffer_new_and_alloc (1600 * 4); + gst_buffer_set_caps (out, GST_PAD_CAPS (pad)); + GST_BUFFER_TIMESTAMP (out) = + (gint64) gst_util_uint64_scale ((guint64) spc->byte_pos, GST_SECOND, + 32000 * 2 * 2); + spc->byte_pos += OSPC_Run (-1, (short *) GST_BUFFER_DATA (out), 1600 * 4); + } else { + if (spc->seekpoint < spc->byte_pos) { + OSPC_Init (GST_BUFFER_DATA (spc->buf), GST_BUFFER_SIZE (spc->buf)); + spc->byte_pos = 0; + } + spc->byte_pos += OSPC_Run (-1, NULL, 1600 * 4); + if (spc->byte_pos >= spc->seekpoint) { + spc->seeking = FALSE; + } + out = gst_buffer_new (); + gst_buffer_set_caps (out, GST_PAD_CAPS (pad)); + } + + duration = gst_spc_duration (spc); + fade = gst_spc_fadeout (spc); + end = duration + fade; + position = + (gint64) gst_util_uint64_scale ((guint64) spc->byte_pos, GST_SECOND, + 32000 * 2 * 2); + + if (position >= duration) { + gint16 *data = (gint16 *) GST_BUFFER_DATA (out); + guint32 size = GST_BUFFER_SIZE (out) / sizeof (gint16); + unsigned int i; - out = gst_buffer_new_and_alloc (1600 * 4); - gst_buffer_set_caps (out, GST_PAD_CAPS (pad)); + gint64 num = (fade - (position - duration)); - OSPC_Run (-1, (short *) GST_BUFFER_DATA (out), 1600 * 4); + for (i = 0; i < size; i++) { + /* Apply a parabolic volume envelope */ + data[i] = (gint16) (data[i] * num / fade * num / fade); + } + } if ((flow_return = gst_pad_push (spc->srcpad, out)) != GST_FLOW_OK) { GST_DEBUG_OBJECT (spc, "pausing task, reason %s", @@ -237,6 +440,11 @@ spc_play (GstPad * pad) } } + if (position >= end) { + gst_pad_pause_task (pad); + gst_pad_push_event (pad, gst_event_new_eos ()); + } + gst_object_unref (spc); return; @@ -246,38 +454,69 @@ static gboolean spc_setup (GstSpcDec * spc) { guchar *data = GST_BUFFER_DATA (spc->buf); - gboolean text_format = FALSE; if (!spc_negotiate (spc)) { return FALSE; } - if (data[0x23] == 26) { - GstEvent *tagevent; - GstTagList *taglist = gst_tag_list_new (); - - if (data[0xA0] == '/') - text_format = TRUE; - - gchar spctitle[0x21]; - gchar spcartist[0x21]; - gchar spcgame[0x21]; - - strncpy (spctitle, (gchar *) & data[0x2E], 32); - strncpy (spcartist, (gchar *) & data[(text_format ? 0xB1 : 0xB0)], 32); - strncpy (spcgame, (gchar *) & data[0x4E], 32); - - spctitle[0x20] = '\0'; - spcartist[0x20] = '\0'; - spcgame[0x20] = '\0'; - - gst_tag_list_add (taglist, GST_TAG_MERGE_APPEND, - GST_TAG_TITLE, spctitle, - GST_TAG_ARTIST, spcartist, GST_TAG_ALBUM, spcgame, NULL); - - tagevent = gst_event_new_tag (taglist); - gst_element_found_tags_for_pad (GST_ELEMENT (spc), spc->srcpad, taglist); + spc_tag_info *info = &(spc->tag_info); + + spc_tag_get_info (data, GST_BUFFER_SIZE (spc->buf), info); + + GstTagList *taglist = gst_tag_list_new (); + + if (info->title) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_TITLE, + info->title, NULL); + if (info->artist) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ARTIST, + info->artist, NULL); + /* Prefer the name of the official soundtrack over the name of the game (since this is + * how track numbers are derived) + */ + if (info->album) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ALBUM, + info->album, NULL); + else if (info->game) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ALBUM, info->game, + NULL); + if (info->year) { + GDate *date = g_date_new_dmy (1, 1, info->year); + + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_DATE, date, NULL); + g_date_free (date); + } + if (info->track) { + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_TRACK_NUMBER, + info->track, NULL); } + if (info->comment) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_COMMENT, + info->comment, NULL); + if (info->disc) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, + GST_TAG_ALBUM_VOLUME_NUMBER, info->disc, NULL); + if (info->publisher) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ORGANIZATION, + info->publisher, NULL); + if (info->dumper) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_CONTACT, + info->dumper, NULL); + if (info->emulator) + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_ENCODER, + info->emulator == EMU_ZSNES ? "ZSNES" : "Snes9x", NULL); + + guint64 total_duration = + (guint64) (gst_spc_duration (spc) + gst_spc_fadeout (spc)); + + gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, + GST_TAG_DURATION, total_duration, + GST_TAG_GENRE, "Game", GST_TAG_CODEC, "SPC700", NULL); + + gst_element_found_tags_for_pad (GST_ELEMENT (spc), spc->srcpad, taglist); + + /* spc_tag_info_free(&info); */ + if (OSPC_Init (GST_BUFFER_DATA (spc->buf), GST_BUFFER_SIZE (spc->buf)) != 0) { return FALSE; @@ -288,9 +527,15 @@ spc_setup (GstSpcDec * spc) gst_pad_start_task (spc->srcpad, (GstTaskFunction) spc_play, spc->srcpad); - gst_buffer_unref (spc->buf); - spc->buf = NULL; + /* We can't unreference this buffer because we might need to re-initialize + * the emulator with the original data during a reverse seek + * gst_buffer_unref (spc->buf); + * spc->buf = NULL; + */ spc->initialized = TRUE; + spc->seeking = FALSE; + spc->seekpoint = 0; + spc->byte_pos = 0; return spc->initialized; } diff --git a/ext/spc/gstspc.h b/ext/spc/gstspc.h index a1627997..e1740667 100644 --- a/ext/spc/gstspc.h +++ b/ext/spc/gstspc.h @@ -1,5 +1,6 @@ /* Copyright (C) 2004-2005 Michael Pyne <michael dot pyne at kdemail net> * Copyright (C) 2004-2006 Chris Lee <clee at kde org> + * Copyright (C) 2007 Brian Koropoff <bkoropoff at gmail com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -24,6 +25,8 @@ #include <openspc.h> +#include "tag.h" + G_BEGIN_DECLS #define GST_TYPE_SPC_DEC \ @@ -49,6 +52,12 @@ struct _GstSpcDec GstBuffer *buf; gboolean initialized; + gboolean seeking; + guint32 seekpoint; + + spc_tag_info tag_info; + + guint32 byte_pos; }; struct _GstSpcDecClass diff --git a/ext/spc/tag.c b/ext/spc/tag.c new file mode 100644 index 00000000..41ce812f --- /dev/null +++ b/ext/spc/tag.c @@ -0,0 +1,353 @@ +/* Copyright (C) 2007 Brian Koropoff <bkoropoff at gmail com> + * + * 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. + */ + +#include <glib/gstring.h> +#include <glib/gtypes.h> +#include <glib/gmem.h> +#include <glib/gprintf.h> + +#include <tag.h> +#include <string.h> +#include <stdlib.h> + + +#define EXTENDED_OFFSET 0x10200 +#define EXTENDED_MAGIC ((guint32) ('x' << 0 | 'i' << 8 | 'd' << 16 | '6' << 24)) + +#define TYPE_LENGTH 0x0 +#define TYPE_STRING 0x1 +#define TYPE_INTEGER 0x4 + +#define TAG_TITLE 0x01 +#define TAG_GAME 0x02 +#define TAG_ARTIST 0x03 +#define TAG_DUMPER 0x04 +#define TAG_DUMP_DATE 0x05 +#define TAG_EMULATOR 0x06 +#define TAG_COMMENT 0x07 +#define TAG_ALBUM 0x10 +#define TAG_DISC 0x11 +#define TAG_TRACK 0x12 +#define TAG_PUBLISHER 0x13 +#define TAG_YEAR 0x14 +#define TAG_INTRO 0x30 +#define TAG_LOOP 0x31 +#define TAG_END 0x32 +#define TAG_FADE 0x33 +#define TAG_MUTED 0x34 +#define TAG_COUNT 0x35 +#define TAG_AMP 0x36 + +#define READ_INT8(data, offset) (data[offset]) +#define READ_INT16(data, offset) ((data[offset] << 0) + (data[offset+1] << 8)) +#define READ_INT24(data, offset) ((data[offset] << 0) + (data[offset+1] << 8) + (data[offset+2] << 16)) +#define READ_INT32(data, offset) ((data[offset] << 0) + (data[offset+1] << 8) + (data[offset+2] << 16) + (data[offset+3] << 24)) + +static inline gboolean +spc_tag_is_extended (guchar * data, guint length) +{ + // Extended tags come at the end of the file (at a known offset) + // and start with "xid6" + return (length > EXTENDED_OFFSET + 4 + && READ_INT32 (data, EXTENDED_OFFSET) == EXTENDED_MAGIC); +} + +static inline gboolean +spc_tag_is_text_format (guchar * data, guint length) +{ + // Because the id666 format is brain dead, there's + // no definite way to decide if it is in text + // format. This function implements a set of + // heuristics to make a best-effort guess. + + // If the date field contains separators, it is probably text + if (data[0xA0] == '/' || data[0xA0] == '.') + return TRUE; + // If the first byte of the date field is small (but not 0, + // which could indicate an empty string), it's probably binary + if (data[0x9E] >= 1 && data[0x9E] <= 31) + return FALSE; + + // If all previous tests turned up nothing, assume it's text + return TRUE; +} + +static inline gboolean +spc_tag_is_present (guchar * data, guint length) +{ + return data[0x23] == 26; +} + +static inline GDate * +spc_tag_unpack_date (guint32 packed) +{ + guint dump_year = packed / 10000; + guint dump_month = (packed % 10000) / 100; + guint dump_day = packed % 100; + + if (dump_month == 0) + dump_month = 1; + if (dump_day == 0) + dump_day = 1; + + if (dump_year != 0) + return g_date_new_dmy (dump_day, dump_month, dump_year); + else + return NULL; +} + +void +spc_tag_clear (spc_tag_info * info) +{ + info->title = info->game = info->publisher = info->artist = info->album = + info->comment = info->dumper = NULL; + info->dump_date = NULL; + info->time_seconds = 0; + info->time_fade_milliseconds = 0; + info->time_intro = 0; + info->time_end = 0; + info->time_loop = 0; + info->time_fade = 0; + info->loop_count = 0; + info->muted = 0; + info->disc = 0; + info->amplification = 0; +} + +void +spc_tag_get_info (guchar * data, guint length, spc_tag_info * info) +{ + spc_tag_clear (info); + + if (spc_tag_is_present (data, length)) { + gboolean text_format = spc_tag_is_text_format (data, length); + + info->title = g_new0 (gchar, 0x21); + info->game = g_new0 (gchar, 0x21); + info->artist = g_new0 (gchar, 0x21); + info->dumper = g_new0 (gchar, 0x10); + info->comment = g_new0 (gchar, 0x32); + + strncpy (info->title, (gchar *) & data[0x2E], 32); + strncpy (info->artist, (gchar *) & data[(text_format ? 0xB1 : 0xB0)], 32); + strncpy (info->game, (gchar *) & data[0x4E], 32); + strncpy (info->dumper, (gchar *) & data[0x6E], 16); + strncpy (info->comment, (gchar *) & data[0x7E], 32); + + if (text_format) { + gchar time[4]; + gchar fade[6]; + guint dump_year, dump_month, dump_day; + + strncpy (time, (gchar *) data + 0xA9, 3); + strncpy (fade, (gchar *) data + 0xAC, 5); + + time[3] = fade[5] = 0; + + info->time_seconds = atoi (time); + info->time_fade_milliseconds = atoi (fade); + + dump_year = (guint) atoi ((gchar *) data + 0x9E); + dump_month = (guint) atoi ((gchar *) data + 0x9E + 3); + dump_day = (guint) atoi ((gchar *) data + 0x9E + 3 + 3); + + if (dump_month == 0) + dump_month = 1; + if (dump_day == 0) + dump_day = 1; + if (dump_year != 0) + info->dump_date = g_date_new_dmy (dump_day, dump_month, dump_year); + + info->muted = READ_INT8 (data, 0xD1); + info->emulator = READ_INT8 (data, 0xD2); + } else { + info->time_seconds = READ_INT24 (data, 0xA9); + info->time_fade_milliseconds = READ_INT32 (data, 0xAC); + info->dump_date = spc_tag_unpack_date (READ_INT32 (data, 0x9E)); + info->muted = READ_INT8 (data, 0xD0); + info->emulator = READ_INT8 (data, 0xD1); + } + } + + if (spc_tag_is_extended (data, length)) { + guchar *chunk = data + EXTENDED_OFFSET + 8; + guint32 chunk_size = *((guint32 *) (data + EXTENDED_OFFSET + 4)); + + guchar *subchunk, *subchunk_next; + + for (subchunk = chunk; subchunk < chunk + chunk_size; + subchunk = subchunk_next) { + guint8 tag = READ_INT8 (subchunk, 0); + guint8 type = READ_INT8 (subchunk, 1); + guint16 length = READ_INT16 (subchunk, 2); + guchar *value = subchunk + 4; + + switch (type) { + case TYPE_LENGTH: + { + switch (tag) { + case TAG_TRACK: + info->track = READ_INT8 (subchunk, 2 + 1); + break; + case TAG_YEAR: + info->year = READ_INT16 (subchunk, 2); + break; + case TAG_COUNT: + info->loop_count = READ_INT8 (subchunk, 2); + break; + case TAG_EMULATOR: + info->emulator = READ_INT8 (subchunk, 2); + break; + case TAG_DISC: + info->disc = READ_INT8 (subchunk, 2); + break; + case TAG_MUTED: + info->muted = READ_INT8 (subchunk, 2); + break; + default: + break; + } + + subchunk_next = subchunk + 4; + break; + } + case TYPE_STRING: + { + gchar *dest; + + if (length <= 1) + dest = NULL; + else + switch (tag) { + case TAG_TITLE: + dest = info->title = g_renew (gchar, info->title, length); + break; + case TAG_GAME: + dest = info->game = g_renew (gchar, info->game, length); + break; + case TAG_ARTIST: + dest = info->artist = g_renew (gchar, info->artist, length); + break; + case TAG_ALBUM: + dest = info->album = g_renew (gchar, info->album, length); + break; + case TAG_DUMPER: + dest = info->dumper = g_renew (gchar, info->dumper, length); + break; + case TAG_COMMENT: + dest = info->comment = g_renew (gchar, info->comment, length); + break; + case TAG_PUBLISHER: + dest = info->publisher = + g_renew (gchar, info->publisher, length); + break; + default: + dest = NULL; + break; + } + + if (dest) + strncpy (dest, (gchar *) value, length); + + subchunk_next = value + length; + break; + } + case TYPE_INTEGER: + { + switch (tag) { + case TAG_INTRO: + info->time_intro = READ_INT32 (value, 0); + break; + case TAG_END: + info->time_end = READ_INT32 (value, 0); + break; + case TAG_FADE: + info->time_fade = READ_INT32 (value, 0); + break; + case TAG_LOOP: + info->time_loop = READ_INT32 (value, 0); + break; + case TAG_DUMP_DATE: + info->dump_date = spc_tag_unpack_date (READ_INT32 (value, 0)); + break; + case TAG_AMP: + info->amplification = READ_INT32 (value, 0); + break; + default: + break; + } + subchunk_next = value + length; + break; + } + default: + subchunk_next = value + length; + break; + } + } + } + + if (info->title && !*info->title) { + g_free (info->title); + info->title = NULL; + } + if (info->game && !*info->game) { + g_free (info->game); + info->game = NULL; + } + if (info->artist && !*info->artist) { + g_free (info->artist); + info->artist = NULL; + } + if (info->album && !*info->album) { + g_free (info->album); + info->album = NULL; + } + if (info->publisher && !*info->publisher) { + g_free (info->publisher); + info->publisher = NULL; + } + if (info->comment && !*info->comment) { + g_free (info->comment); + info->comment = NULL; + } + if (info->dumper && !*info->dumper) { + g_free (info->dumper); + info->dumper = NULL; + } +} + +void +spc_tag_free (spc_tag_info * info) +{ + if (info->title) + g_free (info->title); + if (info->game) + g_free (info->game); + if (info->artist) + g_free (info->artist); + if (info->album) + g_free (info->album); + if (info->publisher) + g_free (info->publisher); + if (info->comment) + g_free (info->comment); + if (info->dumper) + g_free (info->dumper); + if (info->dump_date) + g_date_free (info->dump_date); +} diff --git a/ext/spc/tag.h b/ext/spc/tag.h new file mode 100644 index 00000000..5b795893 --- /dev/null +++ b/ext/spc/tag.h @@ -0,0 +1,37 @@ +/* Copyright (C) 2007 Brian Koropoff <bkoropoff at gmail com> + * + * 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. + */ + +#include <glib/gtypes.h> +#include <glib.h> + +typedef struct +{ + gchar *title, *game, *artist, *album, *publisher; + gchar *dumper, *comment; + enum { EMU_SNES9X = 2, EMU_ZSNES = 1, EMU_UNKNOWN = 0 } emulator; + guint8 track, disc, muted, loop_count; + guint16 year; + guint32 time_seconds, time_fade_milliseconds; + guint32 time_intro, time_loop, time_end, time_fade; + guint32 amplification; + GDate *dump_date; +} spc_tag_info; + +void spc_tag_clear(spc_tag_info* info); +void spc_tag_get_info(guchar* data, guint length, spc_tag_info* info); +void spc_tag_free(spc_tag_info* info); |