]> git.sesse.net Git - vlc/blobdiff - modules/demux/smf.c
macosx: move fullscreen-related method to VideoWindowCommon class
[vlc] / modules / demux / smf.c
index 2f8e93aaf9c7f3952a1ce7d0ebce68584d6a0858..6b999cd37ce725b1a09d1c9895864df226f0a3da 100644 (file)
@@ -4,19 +4,19 @@
  * Copyright © 2007 Rémi Denis-Courmont
  * $Id$
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program 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 General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
@@ -26,8 +26,6 @@
 #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>
 
@@ -70,7 +68,7 @@ struct demux_sys_t
     /* by the way, "quarter note" is "noire" in French */
 
     unsigned     trackc; /* Number of tracks */
-    mtrk_t       trackv[0]; /* Track states */
+    mtrk_t       trackv[]; /* Track states */
 };
 
 /*****************************************************************************
@@ -174,20 +172,16 @@ static int Open (vlc_object_t * p_this)
     }
 
     p_sys = malloc (sizeof (*p_sys) + (sizeof (mtrk_t) * tracks));
-    if (p_sys == NULL)
+    if (unlikely(p_sys == NULL))
         return VLC_ENOMEM;
 
     /* We've had a valid SMF header - now skip it*/
     if (stream_Read (stream, NULL, 14) < 14)
         goto error;
 
-    p_demux->pf_demux   = Demux;
-    p_demux->pf_control = Control;
-    p_demux->p_sys      = p_sys;
-
     /* 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);
+    date_Set (&p_sys->pts, 0);
     p_sys->pulse        = 0;
     p_sys->ppqn         = ppqn;
 
@@ -202,7 +196,7 @@ static int Open (vlc_object_t * p_this)
             /* Seeking screws streaming up, but there is no way around this,
              * as SMF1 tracks are performed simultaneously.
              * Not a big deal as SMF1 are usually only a few kbytes anyway. */
-            if (stream_Seek (stream,  p_sys->trackv[i-1].end))
+            if (stream_Seek (stream, p_sys->trackv[i - 1].end))
             {
                 msg_Err (p_this, "cannot build SMF index (corrupted file?)");
                 goto error;
@@ -211,7 +205,13 @@ static int Open (vlc_object_t * p_this)
 
         for (;;)
         {
-            stream_Read (stream, head, 8);
+            if (stream_Read (stream, head, 8) < 8)
+            {
+                /* FIXME: don't give up if we have at least one valid track */
+                msg_Err (p_this, "incomplete SMF chunk, file is corrupted");
+                goto error;
+            }
+
             if (memcmp (head, "MTrk", 4) == 0)
                 break;
 
@@ -232,11 +232,12 @@ static int Open (vlc_object_t * p_this)
     es_format_t  fmt;
     es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MIDI);
     fmt.audio.i_channels = 2;
-    fmt.audio.i_original_channels = fmt.audio.i_physical_channels =
-        AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
     fmt.audio.i_rate = 44100; /* dummy value */
     p_sys->es = es_out_Add (p_demux->out, &fmt);
 
+    p_demux->pf_demux   = Demux;
+    p_demux->pf_control = Control;
+    p_demux->p_sys      = p_sys;
     return VLC_SUCCESS;
 
 error:
@@ -449,7 +450,7 @@ int HandleMeta (demux_t *p_demux, mtrk_t *tr)
     }
 
     free (payload);
-    return 0;
+    return ret;
 }
 
 
@@ -473,19 +474,22 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
         case 0xF0: /* System Exclusive */
             switch (event)
             {
-                case 0xF0: /* System Specific */
+                case 0xF0: /* System Specific start */
+                case 0xF7: /* System Specific continuation */
                 {
-                    /* TODO: don't skip these */
-                    stream_Read (s, NULL, 1); /* Manuf ID */
-                    for (;;)
-                    {
-                        uint8_t c;
-                        if (stream_Read (s, &c, 1) != 1)
-                            return -1;
-                        if (c == 0xF7)
-                            goto skip;
-                    }
-                    /* never reached */
+                    /* Variable length followed by SysEx event data */
+                    int32_t len = ReadVarInt (s);
+                    if (len == -1)
+                        return -1;
+
+                    block = stream_Block (s, len);
+                    if (block == NULL)
+                        return -1;
+                    block = block_Realloc (block, 1, len);
+                    if (block == NULL)
+                        return -1;
+                    block->p_buffer[0] = event;
+                    goto send;
                 }
                 case 0xFF: /* SMF Meta Event */
                     if (HandleMeta (p_demux, tr))
@@ -505,13 +509,11 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
                     /* We cannot handle undefined "common" (non-real-time)
                      * events inside SMF, as we cannot differentiate a
                      * one byte delta-time (< 0x80) from event data. */
-                case 0xF7: /* End of sysex -> should never happen(?) */
-                    msg_Err (p_demux, "unknown MIDI event 0x%02X", event);
-                    return -1; /* undefined events */
                 default:
                     datalen = 0;
                     break;
             }
+            break;
         case 0xC0:
         case 0xD0:
             datalen = 1;
@@ -522,7 +524,7 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
     }
 
     /* FIXME: one message per block is very inefficient */
-    block = block_New (p_demux, 1 + datalen);
+    block = block_Alloc (1 + datalen);
     if (block == NULL)
         goto skip;
 
@@ -536,7 +538,7 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
         if (datalen == 0)
         {
             msg_Err (p_demux, "malformatted MIDI event");
-            return -1; /* can't use implicit running status with empty payload! */
+            return -1; /* implicit running status requires non-empty payload */
         }
 
         block->p_buffer[1] = first;
@@ -544,7 +546,8 @@ int HandleMessage (demux_t *p_demux, mtrk_t *tr)
             stream_Read (s, block->p_buffer + 2, datalen - 1);
     }
 
-    block->i_dts = block->i_pts = date_Get (&p_demux->p_sys->pts);
+send:
+    block->i_dts = block->i_pts = VLC_TS_0 + date_Get (&p_demux->p_sys->pts);
     es_out_Send (p_demux->out, p_demux->p_sys->es, block);
 
 skip:
@@ -557,7 +560,7 @@ skip:
 }
 
 /*****************************************************************************
- * Demux: read chunks and send them to the synthetizer
+ * Demux: read chunks and send them to the synthesizer
  *****************************************************************************
  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
  *****************************************************************************/
@@ -570,7 +573,8 @@ static int Demux (demux_t *p_demux)
     if (pulse == UINT64_MAX)
         return 0; /* all tracks are done */
 
-    es_out_Control (p_demux->out, ES_OUT_SET_PCR, date_Get (&p_sys->pts));
+    es_out_Control (p_demux->out, ES_OUT_SET_PCR,
+                    VLC_TS_0 + date_Get (&p_sys->pts));
 
     for (unsigned i = 0; i < p_sys->trackc; i++)
     {
@@ -599,12 +603,12 @@ static int Demux (demux_t *p_demux)
     /* MIDI Tick emulation (ping the decoder every 10ms) */
     while (cur_tick < last_tick)
     {
-        block_t *tick = block_New (p_demux, 1);
+        block_t *tick = block_Alloc (1);
         if (tick == NULL)
             break;
 
         tick->p_buffer[0] = 0xF9;
-        tick->i_dts = tick->i_pts = cur_tick++ * 10000;
+        tick->i_dts = tick->i_pts = VLC_TS_0 + cur_tick++ * 10000;
         es_out_Send (p_demux->out, p_sys->es, tick);
     }