* 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
#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>
static int Open (vlc_object_t *);
static void Close (vlc_object_t *);
-vlc_module_begin ();
- set_description (N_("SMF demuxer"));
- set_category (CAT_INPUT);
- set_subcategory (SUBCAT_INPUT_DEMUX);
- set_capability ("demux", 20);
- set_callbacks (Open, Close);
-vlc_module_end ();
+vlc_module_begin ()
+ set_description (N_("SMF demuxer"))
+ set_category (CAT_INPUT)
+ set_subcategory (SUBCAT_INPUT_DEMUX)
+ set_capability ("demux", 20)
+ set_callbacks (Open, Close)
+vlc_module_end ()
static int Demux (demux_t *);
static int Control (demux_t *, int i_query, va_list args);
/* 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 */
};
/*****************************************************************************
}
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;
/* 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;
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;
}
es_format_t fmt;
- es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC('M', 'I', 'D', 'I'));
+ 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:
}
free (payload);
- return 0;
+ return ret;
}
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))
/* 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;
}
/* 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;
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;
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:
}
/*****************************************************************************
- * 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
*****************************************************************************/
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++)
{
/* 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);
}