#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 (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);
-
-typedef struct smf_track_t
-{
- int64_t offset; /* Read offset in the file (stream_Tell) */
- int64_t end; /* End offset in the file */
- uint64_t next; /* Time of next message (in term of pulses) */
- uint8_t running_event; /* Running (previous) event */
-} mtrk_t;
-
-static int ReadDeltaTime (stream_t *s, mtrk_t *track);
-
-struct demux_sys_t
-{
- es_out_id_t *es;
- date_t pts;
- uint64_t pulse; /* Pulses counter */
-
- unsigned ppqn; /* Pulses Per Quarter Note */
- /* by the way, "quarter note" is "noire" in French */
-
- unsigned trackc; /* Number of tracks */
- mtrk_t trackv[]; /* Track states */
-};
-
-/*****************************************************************************
- * Open: check file and initializes structures
- *****************************************************************************/
-static int Open (vlc_object_t * p_this)
-{
- demux_t *p_demux = (demux_t *)p_this;
- stream_t *stream = p_demux->s;
- demux_sys_t *p_sys;
- const uint8_t *peek;
- unsigned tracks, ppqn;
- 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;
-
- /* First word: SMF type */
- switch (GetWBE (peek))
- {
- case 0:
- multitrack = false;
- break;
- case 1:
- multitrack = true;
- break;
- default:
- /* We don't implement SMF2 (as do many) */
- msg_Err (p_this, "unsupported SMF file type %u", GetWBE (peek));
- return VLC_EGENERIC;
- }
- peek += 2;
-
- /* Second word: number of tracks */
- tracks = GetWBE (peek);
- peek += 2;
- if (!multitrack && (tracks != 1))
- {
- msg_Err (p_this, "invalid SMF type 0 file");
- return VLC_EGENERIC;
- }
-
- msg_Dbg (p_this, "detected Standard MIDI File (type %u) with %u track(s)",
- multitrack, tracks);
-
- /* Third/last word: timing */
- ppqn = GetWBE (peek);
- if (ppqn & 0x8000)
- {
- /* FIXME */
- msg_Err (p_this, "SMPTE timestamps not implemented");
- return VLC_EGENERIC;
- }
- else
- {
- msg_Dbg (p_this, " %u pulses per quarter note", ppqn);
- }
-
- p_sys = malloc (sizeof (*p_sys) + (sizeof (mtrk_t) * tracks));
- 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;
-
- /* 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, 0);
- p_sys->pulse = 0;
- p_sys->ppqn = ppqn;
-
- p_sys->trackc = tracks;
- /* Prefetch track offsets */
- for (unsigned i = 0; i < tracks; i++)
- {
- uint8_t head[8];
-
- if (i > 0)
- {
- /* 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))
- {
- msg_Err (p_this, "cannot build SMF index (corrupted file?)");
- goto error;
- }
- }
-
- for (;;)
- {
- 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;
-
- msg_Dbg (p_this, "skipping unknown SMF chunk");
- stream_Read (stream, NULL, GetDWBE (head + 4));
- }
-
- p_sys->trackv[i].offset = stream_Tell (stream);
- p_sys->trackv[i].end = p_sys->trackv[i].offset + GetDWBE (head + 4);
- p_sys->trackv[i].next = 0;
- ReadDeltaTime (stream, p_sys->trackv + i);
- p_sys->trackv[i].running_event = 0xF6;
- /* Why 0xF6 (Tuning Calibration)?
- * Because it has zero bytes of data, so the parser will detect the
- * error if the first event uses running status. */
- }
-
- es_format_t fmt;
- es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MIDI);
- fmt.audio.i_channels = 2;
- 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 (p_sys);
- return VLC_EGENERIC;
-}
-
-/**
- * Releases allocate resources.
- */
-static void Close (vlc_object_t * p_this)
-{
- demux_t *p_demux = (demux_t *)p_this;
- demux_sys_t *p_sys = p_demux->p_sys;
-
- free (p_sys);
-}
-
/**
* Reads MIDI variable length (7, 14, 21 or 28 bits) integer.
* @return read value, or -1 on EOF/error.
return -1;
}
+typedef struct smf_track_t
+{
+ uint64_t next; /*< Time of next message (in term of pulses) */
+ int64_t start; /*< Start offset in the file */
+ uint32_t length; /*< Bytes length */
+ uint32_t offset; /*< Read offset relative to the start offset */
+ uint8_t running_event; /*< Running (previous) event */
+} mtrk_t;
/**
* Reads (delta) time from the next event of a given track.
{
int32_t delta_time;
- assert (stream_Tell (s) == track->offset);
+ assert (stream_Tell (s) == track->start + track->offset);
- if (track->offset >= track->end)
+ if (track->offset >= track->length)
{
/* This track is done */
track->next = UINT64_MAX;
return -1;
track->next += delta_time;
- track->offset = stream_Tell (s);
+ track->offset = stream_Tell (s) - track->start;
return 0;
}
+struct demux_sys_t
+{
+ es_out_id_t *es;
+ date_t pts; /*< Play timestamp */
+ uint64_t pulse; /*< Pulses counter */
+ mtime_t tick; /*< Last tick timestamp */
+
+ mtime_t duration; /*< Total duration */
+ unsigned ppqn; /*< Pulses Per Quarter Note */
+ /* by the way, "quarter note" is "noire" in French */
+
+ unsigned trackc; /*< Number of tracks */
+ mtrk_t trackv[]; /*< Track states */
+};
/**
* Non-MIDI Meta events handler
break;
case 0x2F: /* End of track */
- if (tr->end != stream_Tell (s))
+ if (tr->start + tr->length != stream_Tell (s))
{
msg_Err (p_demux, "misplaced end of track");
ret = -1;
return ret;
}
-
-
static
-int HandleMessage (demux_t *p_demux, mtrk_t *tr)
+int HandleMessage (demux_t *p_demux, mtrk_t *tr, es_out_t *out)
{
stream_t *s = p_demux->s;
block_t *block;
uint8_t first, event;
unsigned datalen;
- if (stream_Seek (s, tr->offset)
+ if (stream_Seek (s, tr->start + tr->offset)
|| (stream_Read (s, &first, 1) != 1))
return -1;
}
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);
+ block->i_dts = block->i_pts = date_Get (&p_demux->p_sys->pts);
+ if (out != NULL)
+ es_out_Send (out, p_demux->p_sys->es, block);
+ else
+ block_Release (block);
skip:
if (event < 0xF8)
/* If event is not real-time, update running status */
tr->running_event = event;
- tr->offset = stream_Tell (s);
+ tr->offset = stream_Tell (s) - tr->start;
return 0;
}
-/*****************************************************************************
- * Demux: read chunks and send them to the synthesizer
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
- *****************************************************************************/
-static int Demux (demux_t *p_demux)
+static int SeekSet0 (demux_t *demux)
{
- stream_t *s = p_demux->s;
- demux_sys_t *p_sys = p_demux->p_sys;
- uint64_t pulse = p_sys->pulse, next_pulse = UINT64_MAX;
+ stream_t *stream = demux->s;
+ demux_sys_t *sys = demux->p_sys;
- if (pulse == UINT64_MAX)
- return 0; /* all tracks are done */
+ /* Default SMF tempo is 120BPM, i.e. half a second per quarter note */
+ date_Init (&sys->pts, sys->ppqn * 2, 1);
+ date_Set (&sys->pts, VLC_TS_0);
+ sys->pulse = 0;
+ sys->tick = VLC_TS_0;
+
+ for (unsigned i = 0; i < sys->trackc; i++)
+ {
+ mtrk_t *tr = sys->trackv + i;
+
+ tr->offset = 0;
+ tr->next = 0;
+ /* Why 0xF6 (Tuning Calibration)?
+ * Because it has zero bytes of data, so the parser will detect the
+ * error if the first event uses running status. */
+ tr->running_event = 0xF6;
+
+ if (stream_Seek (stream, tr->start)
+ || ReadDeltaTime (stream, tr))
+ {
+ msg_Err (demux, "fatal parsing error");
+ return -1;
+ }
+ }
- es_out_Control (p_demux->out, ES_OUT_SET_PCR,
- VLC_TS_0 + date_Get (&p_sys->pts));
+ return 0;
+}
- for (unsigned i = 0; i < p_sys->trackc; i++)
+static int ReadEvents (demux_t *demux, uint64_t *restrict pulse,
+ es_out_t *out)
+{
+ uint64_t cur_pulse = *pulse, next_pulse = UINT64_MAX;
+ demux_sys_t *sys = demux->p_sys;
+
+ for (unsigned i = 0; i < sys->trackc; i++)
{
- mtrk_t *track = p_sys->trackv + i;
+ mtrk_t *track = sys->trackv + i;
- while (track->next == pulse)
+ while (track->next <= cur_pulse)
{
- if (HandleMessage (p_demux, track)
- || ReadDeltaTime (s, track))
+ if (HandleMessage (demux, track, out)
+ || ReadDeltaTime (demux->s, track))
{
- msg_Err (p_demux, "fatal parsing error");
- return VLC_EGENERIC;
+ msg_Err (demux, "fatal parsing error");
+ return -1;
}
}
- if (track->next < next_pulse)
+ if (next_pulse > track->next)
next_pulse = track->next;
}
- mtime_t cur_tick = (date_Get (&p_sys->pts) + 9999) / 10000, last_tick;
if (next_pulse != UINT64_MAX)
- last_tick = date_Increment (&p_sys->pts, next_pulse - pulse) / 10000;
- else
- last_tick = cur_tick + 1;
+ date_Increment (&sys->pts, next_pulse - cur_pulse);
+ *pulse = next_pulse;
+ return 0;
+}
+
+#define TICK (CLOCK_FREQ / 100)
+
+/*****************************************************************************
+ * Demux: read chunks and send them to the synthesizer
+ *****************************************************************************
+ * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
+ *****************************************************************************/
+static int Demux (demux_t *demux)
+{
+ demux_sys_t *sys = demux->p_sys;
/* MIDI Tick emulation (ping the decoder every 10ms) */
- while (cur_tick < last_tick)
+ if (sys->tick <= date_Get (&sys->pts))
{
block_t *tick = block_Alloc (1);
- if (tick == NULL)
- break;
+ if (unlikely(tick == NULL))
+ return VLC_ENOMEM;
tick->p_buffer[0] = 0xF9;
- tick->i_dts = tick->i_pts = VLC_TS_0 + cur_tick++ * 10000;
- es_out_Send (p_demux->out, p_sys->es, tick);
+ tick->i_dts = tick->i_pts = sys->tick;
+
+ es_out_Send (demux->out, sys->es, tick);
+ es_out_Control (demux->out, ES_OUT_SET_PCR, sys->tick);
+
+ sys->tick += TICK;
+ return 1;
}
- p_sys->pulse = next_pulse;
+ /* MIDI events in chronological order across all tracks */
+ uint64_t pulse = sys->pulse;
+
+ if (ReadEvents (demux, &pulse, demux->out))
+ return VLC_EGENERIC;
+
+ if (pulse == UINT64_MAX)
+ return 0; /* all tracks are done */
+ sys->pulse = pulse;
return 1;
}
+static int Seek (demux_t *demux, mtime_t pts)
+{
+ demux_sys_t *sys = demux->p_sys;
+
+ /* Rewind if needed */
+ if (pts < date_Get (&sys->pts) && SeekSet0 (demux))
+ return VLC_EGENERIC;
+
+ /* Fast forward */
+ uint64_t pulse = sys->pulse;
+
+ while (pts > date_Get (&sys->pts))
+ {
+ if (pulse == UINT64_MAX)
+ return VLC_SUCCESS; /* premature end */
+ if (ReadEvents (demux, &pulse, NULL))
+ return VLC_EGENERIC;
+ }
+
+ sys->pulse = pulse;
+ sys->tick = ((date_Get (&sys->pts) - VLC_TS_0) / TICK) * TICK + VLC_TS_0;
+ return VLC_SUCCESS;
+}
/*****************************************************************************
* Control:
*****************************************************************************/
-static int Control (demux_t *p_demux, int i_query, va_list args)
+static int Control (demux_t *demux, int i_query, va_list args)
{
- demux_sys_t *p_sys = p_demux->p_sys;
+ demux_sys_t *sys = 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:
+ if (!sys->duration)
+ return VLC_EGENERIC;
+ *va_arg (args, double *) = (sys->tick - (double)VLC_TS_0)
+ / sys->duration;
+ break;
case DEMUX_SET_POSITION:
+ return Seek (demux, va_arg (args, double) * sys->duration);
case DEMUX_GET_LENGTH:
-#endif
+ *va_arg (args, int64_t *) = sys->duration;
+ break;
+ case DEMUX_GET_TIME:
+ *va_arg (args, int64_t *) = sys->tick - VLC_TS_0;
+ break;
+ case DEMUX_SET_TIME:
+ return Seek (demux, va_arg (args, int64_t));
+ default:
+ return VLC_EGENERIC;
+ }
+ return VLC_SUCCESS;
+}
+
+/**
+ * Probes file format and starts demuxing.
+ */
+static int Open (vlc_object_t *obj)
+{
+ demux_t *demux = (demux_t *)obj;
+ stream_t *stream = demux->s;
+ const uint8_t *peek;
+ 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 (demux, "detected RIFF MIDI file (%"PRIu32" bytes)", 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;
+
+ /* First word: SMF type */
+ switch (GetWBE (peek))
+ {
+ case 0:
+ multitrack = false;
+ break;
+ case 1:
+ multitrack = true;
+ break;
+ default:
+ /* We don't implement SMF2 (as do many) */
+ msg_Err (demux, "unsupported SMF file type %u", GetWBE (peek));
+ return VLC_EGENERIC;
+ }
+ peek += 2;
+
+ /* Second word: number of tracks */
+ unsigned tracks = GetWBE (peek);
+ peek += 2;
+ if (!multitrack && (tracks != 1))
+ {
+ msg_Err (demux, "invalid SMF type 0 file");
+ return VLC_EGENERIC;
+ }
+
+ msg_Dbg (demux, "detected Standard MIDI File (type %u) with %u track(s)",
+ multitrack, tracks);
+
+ /* Third/last word: timing */
+ unsigned ppqn = GetWBE (peek);
+ if (ppqn & 0x8000)
+ { /* FIXME */
+ msg_Err (demux, "SMPTE timestamps not implemented");
+ return VLC_EGENERIC;
+ }
+ else
+ {
+ msg_Dbg (demux, " %u pulses per quarter note", ppqn);
+ }
+
+ demux_sys_t *sys = malloc (sizeof (*sys) + (sizeof (mtrk_t) * tracks));
+ if (unlikely(sys == NULL))
+ return VLC_ENOMEM;
+
+ /* We've had a valid SMF header - now skip it*/
+ if (stream_Read (stream, NULL, 14) < 14)
+ goto error;
+
+ demux->p_sys = sys;
+ sys->duration = 0;
+ sys->ppqn = ppqn;
+ sys->trackc = tracks;
+
+ /* Prefetch track offsets */
+ for (unsigned i = 0; i < tracks; i++)
+ {
+ mtrk_t *tr = sys->trackv + i;
+ uint8_t head[8];
+
+ /* 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 (i > 0 && stream_Seek (stream, tr[-1].start + tr[-1].length))
+ {
+ msg_Err (demux, "cannot build SMF index (corrupted file?)");
+ goto error;
+ }
+
+ for (;;)
+ {
+ if (stream_Read (stream, head, 8) < 8)
+ {
+ /* FIXME: don't give up if we have at least one valid track */
+ msg_Err (demux, "incomplete SMF chunk, file is corrupted");
+ goto error;
+ }
+
+ if (memcmp (head, "MTrk", 4) == 0)
+ break;
+
+ msg_Dbg (demux, "skipping unknown SMF chunk");
+ stream_Read (stream, NULL, GetDWBE (head + 4));
+ }
+
+ tr->start = stream_Tell (stream);
+ tr->length = GetDWBE (head + 4);
+ }
+
+ bool b;
+ if (stream_Control (stream, STREAM_CAN_FASTSEEK, &b) == 0 && b)
+ {
+ if (SeekSet0 (demux))
+ goto error;
+
+ for (uint64_t pulse = 0; pulse != UINT64_MAX;)
+ if (ReadEvents (demux, &pulse, NULL))
+ break;
+
+ sys->duration = date_Get (&sys->pts);
}
+
+ if (SeekSet0 (demux))
+ goto error;
+
+ es_format_t fmt;
+ es_format_Init (&fmt, AUDIO_ES, VLC_CODEC_MIDI);
+ fmt.audio.i_channels = 2;
+ fmt.audio.i_rate = 44100; /* dummy value */
+ sys->es = es_out_Add (demux->out, &fmt);
+
+ demux->pf_demux = Demux;
+ demux->pf_control = Control;
+ return VLC_SUCCESS;
+
+error:
+ free (sys);
return VLC_EGENERIC;
}
+
+/**
+ * Releases allocate resources.
+ */
+static void Close (vlc_object_t * p_this)
+{
+ demux_t *p_demux = (demux_t *)p_this;
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ free (p_sys);
+}
+
+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 ()