* 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 ();
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;
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) */
case 0x00: /* Sequence Number */
break;
- case 0x01: /* Test (comment) */
+ case 0x01: /* Text (comment) */
EnsureUTF8 ((char *)payload);
msg_Info (p_demux, "Text : %s", (char *)payload);
break;
msg_Dbg (p_demux, "tempo: %uus/qn -> %u BPM",
(unsigned)uspqn, tempo);
- if (tempo < 20)
+ if (tempo < TEMPO_MIN)
{
- msg_Warn (p_demux, "tempo too slow -> 20 BPM");
- tempo = 20;
+ msg_Warn (p_demux, "tempo too slow -> %u BPM", TEMPO_MIN);
+ tempo = TEMPO_MIN;
}
else
- if (tempo > 240)
+ if (tempo > TEMPO_MAX)
{
- msg_Warn (p_demux, "tempo too fast -> 240 BPM");
- tempo = 240;
+ msg_Warn (p_demux, "tempo too fast -> %u BPM", TEMPO_MAX);
+ tempo = TEMPO_MAX;
}
date_Change (&p_sys->pts, p_sys->ppqn * tempo, 60);
}
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 */
- msg_Warn (p_demux, "unimplemented SMF Meta Event type 0x%02X (%d bytes)",
- type, length);
+ 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:
if (c == 0xF7)
goto skip;
}
+ /* never reached */
}
case 0xFF: /* SMF Meta Event */
if (HandleMeta (p_demux, tr))
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:
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;
*****************************************************************************/
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;
}