]> git.sesse.net Git - vlc/blobdiff - modules/demux/smf.c
Revert "Prefer setenv to putenv (evenmore with local variables)."
[vlc] / modules / demux / smf.c
index a362ecb524822139e152bf04f377e066bb1793bb..baaf5aebb475e9b04ee17546a9c4ffe7401dd70f 100644 (file)
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_demux.h>
 #include <vlc_aout.h>
 #include <vlc_codecs.h>
+#include <vlc_charset.h>
 #include <limits.h>
 
+#include <assert.h>
+
+#define TEMPO_MIN  20
+#define TEMPO_MAX 250 /* Beats per minute */
+
 static int  Open  (vlc_object_t *);
 static void Close (vlc_object_t *);
 
 vlc_module_begin ();
-    set_description (_("SMF demuxer"));
+    set_description (N_("SMF demuxer"));
     set_category (CAT_INPUT);
     set_subcategory (SUBCAT_INPUT_DEMUX);
-    set_capability ("demux2", 20);
+    set_capability ("demux", 20);
     set_callbacks (Open, Close);
 vlc_module_end ();
 
@@ -72,13 +83,51 @@ static int Open (vlc_object_t * p_this)
     demux_sys_t   *p_sys;
     const uint8_t *peek;
     unsigned       tracks, ppqn;
-    vlc_bool_t     multitrack;
+    bool     multitrack;
 
     /* (Try to) parse the SMF header */
     /* Header chunk always has 6 bytes payload */
     if (stream_Peek (stream, &peek, 14) < 14)
         return VLC_EGENERIC;
 
+    /* Skip RIFF MIDI header if present */
+    if (!memcmp (peek, "RIFF", 4) && !memcmp (peek + 8, "RMID", 4))
+    {
+        uint32_t riff_len = GetDWLE (peek + 4);
+
+        msg_Dbg (p_this, "detected RIFF MIDI file (%u bytes)",
+                 (unsigned)riff_len);
+        if ((stream_Read (stream, NULL, 12) < 12))
+            return VLC_EGENERIC;
+
+        /* Look for the RIFF data chunk */
+        for (;;)
+        {
+            char chnk_hdr[8];
+            uint32_t chnk_len;
+
+            if ((riff_len < 8)
+             || (stream_Read (stream, chnk_hdr, 8) < 8))
+                return VLC_EGENERIC;
+
+            riff_len -= 8;
+            chnk_len = GetDWLE (chnk_hdr + 4);
+            if (riff_len < chnk_len)
+                return VLC_EGENERIC;
+            riff_len -= chnk_len;
+
+            if (!memcmp (chnk_hdr, "data", 4))
+                break; /* found! */
+
+            if (stream_Read (stream, NULL, chnk_len) < (ssize_t)chnk_len)
+                return VLC_EGENERIC;
+        }
+
+        /* Read real SMF header. Assume RIFF data chunk length is proper. */
+        if (stream_Peek (stream, &peek, 14) < 14)
+            return VLC_EGENERIC;
+    }
+
     if (memcmp (peek, "MThd\x00\x00\x00\x06", 8))
         return VLC_EGENERIC;
     peek += 8;
@@ -87,10 +136,10 @@ static int Open (vlc_object_t * p_this)
     switch (GetWBE (peek))
     {
         case 0:
-            multitrack = VLC_FALSE;
+            multitrack = false;
             break;
         case 1:
-            multitrack = VLC_TRUE;
+            multitrack = true;
             break;
         default:
             /* We don't implement SMF2 (as do many) */
@@ -136,8 +185,9 @@ static int Open (vlc_object_t * p_this)
     p_demux->pf_control = Control;
     p_demux->p_sys      = p_sys;
 
-    /* SMF expresses tempo in Beats-Per-Minute, default is 120 */
-    date_Init (&p_sys->pts, ppqn * 120, 60);
+    /* Default SMF tempo is 120BPM, i.e. half a second per quarter note */
+    date_Init (&p_sys->pts, ppqn * 2, 1);
+    date_Set (&p_sys->pts, 1);
     p_sys->pulse        = 0;
     p_sys->ppqn         = ppqn;
 
@@ -171,8 +221,6 @@ static int Open (vlc_object_t * p_this)
 
         p_sys->trackv[i].offset = stream_Tell (stream);
         p_sys->trackv[i].end = p_sys->trackv[i].offset + GetDWBE (head + 4);
-        msg_Dbg (p_this, "track %u: 0x"I64Fx"-0x"I64Fx, i,
-                 p_sys->trackv[i].offset, p_sys->trackv[i].end);
         p_sys->trackv[i].next = 0;
         ReadDeltaTime (stream, p_sys->trackv + i);
         p_sys->trackv[i].running_event = 0xF6;
@@ -253,6 +301,155 @@ static int ReadDeltaTime (stream_t *s, mtrk_t *track)
 }
 
 
+/**
+ * Non-MIDI Meta events handler
+ */
+static
+int HandleMeta (demux_t *p_demux, mtrk_t *tr)
+{
+    stream_t *s = p_demux->s;
+    demux_sys_t *p_sys = p_demux->p_sys;
+    uint8_t *payload;
+    uint8_t type;
+    int32_t length;
+    int ret = 0;
+
+    if (stream_Read (s, &type, 1) != 1)
+        return -1;
+
+    length = ReadVarInt (s);
+    if (length < 0)
+        return -1;
+
+    payload = malloc (length + 1);
+    if ((payload == NULL)
+     || (stream_Read (s, payload, length) != length))
+    {
+        free (payload);
+        return -1;
+    }
+
+    payload[length] = '\0';
+
+    switch (type)
+    {
+        case 0x00: /* Sequence Number */
+            break;
+
+        case 0x01: /* Text (comment) */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Text      : %s", (char *)payload);
+            break;
+
+        case 0x02: /* Copyright */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Copyright : %s", (char *)payload);
+            break;
+
+        case 0x03: /* Track name */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Track name: %s", (char *)payload);
+            break;
+
+        case 0x04: /* Instrument name */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Instrument: %s", (char *)payload);
+            break;
+
+        case 0x05: /* Lyric (one syllable) */
+            /*EnsureUTF8 ((char *)payload);*/
+            break;
+
+        case 0x06: /* Marker text */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Marker    : %s", (char *)payload);
+
+        case 0x07: /* Cue point (WAVE filename) */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Cue point : %s", (char *)payload);
+            break;
+
+        case 0x08: /* Program/Patch name */
+            EnsureUTF8 ((char *)payload);
+            msg_Info (p_demux, "Patch name: %s", (char *)payload);
+            break;
+
+        case 0x09: /* MIDI port name */
+            EnsureUTF8 ((char *)payload);
+            msg_Dbg (p_demux, "MIDI port : %s", (char *)payload);
+            break;
+
+        case 0x2F: /* End of track */
+            if (tr->end != stream_Tell (s))
+            {
+                msg_Err (p_demux, "misplaced end of track");
+                ret = -1;
+            }
+            break;
+
+        case 0x51: /* Tempo */
+            if (length == 3)
+            {
+                uint32_t uspqn = (payload[0] << 16)
+                               | (payload[1] << 8) | payload[2];
+                unsigned tempo = 60 * 1000000 / (uspqn ? uspqn : 1);
+                msg_Dbg (p_demux, "tempo: %uus/qn -> %u BPM",
+                         (unsigned)uspqn, tempo);
+
+                if (tempo < TEMPO_MIN)
+                {
+                    msg_Warn (p_demux, "tempo too slow -> %u BPM", TEMPO_MIN);
+                    tempo = TEMPO_MIN;
+                }
+                else
+                if (tempo > TEMPO_MAX)
+                {
+                    msg_Warn (p_demux, "tempo too fast -> %u BPM", TEMPO_MAX);
+                    tempo = TEMPO_MAX;
+                }
+                date_Change (&p_sys->pts, p_sys->ppqn * tempo, 60);
+            }
+            else
+                ret = -1;
+            break;
+
+        case 0x54: /* SMPTE offset */
+            if (length == 5)
+                msg_Warn (p_demux, "SMPTE offset not implemented");
+            else
+                ret = -1;
+            break;
+
+        case 0x58: /* Time signature */
+            if (length == 4)
+                ;
+            else
+                ret = -1;
+            break;
+
+        case 0x59: /* Key signature */
+            if (length == 2)
+                ;
+            else
+                ret = -1;
+            break;
+
+        case 0x7f: /* Proprietary event */
+            msg_Dbg (p_demux, "ignored proprietary SMF Meta Event (%d bytes)",
+                     length);
+            break;
+
+        default:
+            msg_Warn (p_demux, "unknown SMF Meta Event type 0x%02X (%d bytes)",
+                      type, length);
+    }
+
+    free (payload);
+    return 0;
+}
+
+
+
 static
 int HandleMessage (demux_t *p_demux, mtrk_t *tr)
 {
@@ -266,7 +463,6 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
         return -1;
 
     event = (first & 0x80) ? first : tr->running_event;
-    msg_Dbg (p_demux, "MIDI event 0x%02X", event);
 
     switch (event & 0xf0)
     {
@@ -285,29 +481,14 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
                         if (c == 0xF7)
                             goto skip;
                     }
+                    /* never reached */
                 }
                 case 0xFF: /* SMF Meta Event */
-                {
-                    uint8_t type;
-                    int32_t length;
-
-                    if (stream_Read (s, &type, 1) != 1)
+                    if (HandleMeta (p_demux, tr))
                         return -1;
-                    length = ReadVarInt (s);
-                    if (length < 0)
-                        return -1;
-
-                    msg_Warn (p_demux,
-                              "unknown SMF Meta Event type 0x%02X (%d bytes)",
-                              type, length);
-                    /* TODO: parse these */
-                    if (stream_Read (s, NULL, length) != length)
-                        return -1;
-
-                    /* We MUST NOT pass this event to forward. It would be
+                    /* We MUST NOT pass this event forward. It would be
                      * confused as a MIDI Reset real-time event. */
                     goto skip;
-                }
                 case 0xF1:
                 case 0xF3:
                     datalen = 1;
@@ -387,14 +568,12 @@ static int Demux (demux_t *p_demux)
 
     es_out_Control (p_demux->out, ES_OUT_SET_PCR, date_Get (&p_sys->pts));
 
-    msg_Dbg (p_demux, "pulse %llu", pulse);
     for (unsigned i = 0; i < p_sys->trackc; i++)
     {
         mtrk_t *track = p_sys->trackv + i;
 
         while (track->next == pulse)
         {
-            msg_Dbg (p_demux, "event on track %u", i);
             if (HandleMessage (p_demux, track)
              || ReadDeltaTime (s, track))
             {
@@ -407,8 +586,24 @@ static int Demux (demux_t *p_demux)
             next_pulse = track->next;
     }
 
+    mtime_t cur_tick = (date_Get (&p_sys->pts) + 9999) / 10000, last_tick;
     if (next_pulse != UINT64_MAX)
-        date_Increment (&p_sys->pts, next_pulse - pulse);
+        last_tick = date_Increment (&p_sys->pts, next_pulse - pulse) / 10000;
+    else
+        last_tick = cur_tick + 1;
+
+    /* MIDI Tick emulation (ping the decoder every 10ms) */
+    while (cur_tick < last_tick)
+    {
+        block_t *tick = block_New (p_demux, 1);
+        if (tick == NULL)
+            break;
+
+        tick->p_buffer[0] = 0xF9;
+        tick->i_dts = tick->i_pts = cur_tick++ * 10000;
+        es_out_Send (p_demux->out, p_sys->es, tick);
+    }
+
     p_sys->pulse = next_pulse;
 
     return 1;
@@ -420,5 +615,22 @@ static int Demux (demux_t *p_demux)
  *****************************************************************************/
 static int Control (demux_t *p_demux, int i_query, va_list args)
 {
-    return demux2_vaControlHelper (p_demux->s, 0, -1, 0, 1, i_query, args);
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    switch (i_query)
+    {
+        case DEMUX_GET_TIME:
+        {
+            *(va_arg (args, int64_t *)) = date_Get (&p_sys->pts);
+            return 0;
+        }
+#if 0
+        /* TODO: */
+        case DEMUX_SET_TIME:
+        case DEMUX_GET_POSITION:
+        case DEMUX_SET_POSITION:
+        case DEMUX_GET_LENGTH:
+#endif
+    }
+    return VLC_EGENERIC;
 }