1 /*****************************************************************************
2 * smf.c : Standard MIDI File (.mid) demux module for vlc
3 *****************************************************************************
4 * Copyright © 2007 Rémi Denis-Courmont
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
20 *****************************************************************************/
23 #include <vlc_demux.h>
25 #include <vlc_codecs.h>
28 static int Open (vlc_object_t *);
29 static void Close (vlc_object_t *);
32 set_description (_("SMF demuxer"));
33 set_category (CAT_INPUT);
34 set_subcategory (SUBCAT_INPUT_DEMUX);
35 set_capability ("demux2", 20);
36 set_callbacks (Open, Close);
39 static int Demux (demux_t *);
40 static int Control (demux_t *, int i_query, va_list args);
42 typedef struct smf_track_t
44 int64_t offset; /* Read offset in the file (stream_Tell) */
45 int64_t end; /* End offset in the file */
46 uint64_t next; /* Time of next message (in term of pulses) */
47 uint8_t running_event; /* Running (previous) event */
50 static int ReadDeltaTime (stream_t *s, mtrk_t *track);
56 uint64_t pulse; /* Pulses counter */
58 unsigned ppqn; /* Pulses Per Quarter Note */
59 /* by the way, "quarter note" is "noire" in French */
61 unsigned trackc; /* Number of tracks */
62 mtrk_t trackv[0]; /* Track states */
65 /*****************************************************************************
66 * Open: check file and initializes structures
67 *****************************************************************************/
68 static int Open (vlc_object_t * p_this)
70 demux_t *p_demux = (demux_t *)p_this;
71 stream_t *stream = p_demux->s;
74 unsigned tracks, ppqn;
75 vlc_bool_t multitrack;
77 /* (Try to) parse the SMF header */
78 /* Header chunk always has 6 bytes payload */
79 if (stream_Peek (stream, &peek, 14) < 14)
82 if (memcmp (peek, "MThd\x00\x00\x00\x06", 8))
86 /* First word: SMF type */
87 switch (GetWBE (peek))
90 multitrack = VLC_FALSE;
93 multitrack = VLC_TRUE;
96 /* We don't implement SMF2 (as do many) */
97 msg_Err (p_this, "unsupported SMF file type %u", GetWBE (peek));
102 /* Second word: number of tracks */
103 tracks = GetWBE (peek);
105 if (!multitrack && (tracks != 1))
107 msg_Err (p_this, "invalid SMF type 0 file");
111 msg_Dbg (p_this, "detected Standard MIDI File (type %u) with %u track(s)",
114 /* Third/last word: timing */
115 ppqn = GetWBE (peek);
119 msg_Err (p_this, "SMPTE timestamps not implemented");
124 msg_Dbg (p_this, " %u pulses per quarter note", ppqn);
127 p_sys = malloc (sizeof (*p_sys) + (sizeof (mtrk_t) * tracks));
131 /* We've had a valid SMF header - now skip it*/
132 if (stream_Read (stream, NULL, 14) < 14)
135 p_demux->pf_demux = Demux;
136 p_demux->pf_control = Control;
137 p_demux->p_sys = p_sys;
139 /* SMF expresses tempo in Beats-Per-Minute, default is 120 */
140 date_Init (&p_sys->pts, ppqn * 120, 60);
141 date_Set (&p_sys->pts, 1);
145 p_sys->trackc = tracks;
146 /* Prefetch track offsets */
147 for (unsigned i = 0; i < tracks; i++)
153 /* Seeking screws streaming up, but there is no way around this,
154 * as SMF1 tracks are performed simultaneously.
155 * Not a big deal as SMF1 are usually only a few kbytes anyway. */
156 if (stream_Seek (stream, p_sys->trackv[i-1].end))
158 msg_Err (p_this, "cannot build SMF index (corrupted file?)");
165 stream_Read (stream, head, 8);
166 if (memcmp (head, "MTrk", 4) == 0)
169 msg_Dbg (p_this, "skipping unknown SMF chunk");
170 stream_Read (stream, NULL, GetDWBE (head + 4));
173 p_sys->trackv[i].offset = stream_Tell (stream);
174 p_sys->trackv[i].end = p_sys->trackv[i].offset + GetDWBE (head + 4);
175 msg_Dbg (p_this, "track %u: 0x"I64Fx"-0x"I64Fx, i,
176 p_sys->trackv[i].offset, p_sys->trackv[i].end);
177 p_sys->trackv[i].next = 0;
178 ReadDeltaTime (stream, p_sys->trackv + i);
179 p_sys->trackv[i].running_event = 0xF6;
180 /* Why 0xF6 (Tuning Calibration)?
181 * Because it has zero bytes of data, so the parser will detect the
182 * error if the first event uses running status. */
186 es_format_Init (&fmt, AUDIO_ES, VLC_FOURCC('M', 'I', 'D', 'I'));
187 p_sys->es = es_out_Add (p_demux->out, &fmt);
197 * Releases allocate resources.
199 static void Close (vlc_object_t * p_this)
201 demux_t *p_demux = (demux_t *)p_this;
202 demux_sys_t *p_sys = p_demux->p_sys;
208 * Reads MIDI variable length (7, 14, 21 or 28 bits) integer.
209 * @return read value, or -1 on EOF/error.
211 static int32_t ReadVarInt (stream_t *s)
216 for (unsigned i = 0; i < 4; i++)
218 if (stream_Read (s, &byte, 1) < 1)
221 val = (val << 7) | (byte & 0x7f);
222 if ((byte & 0x80) == 0)
231 * Reads (delta) time from the next event of a given track.
232 * @param s stream to read data from (must be positioned at the right offset)
234 static int ReadDeltaTime (stream_t *s, mtrk_t *track)
238 assert (stream_Tell (s) == track->offset);
240 if (track->offset >= track->end)
242 /* This track is done */
243 track->next = UINT64_MAX;
247 delta_time = ReadVarInt (s);
251 track->next += delta_time;
252 track->offset = stream_Tell (s);
258 int HandleMessage (demux_t *p_demux, mtrk_t *tr)
260 stream_t *s = p_demux->s;
262 uint8_t first, event;
265 if (stream_Seek (s, tr->offset)
266 || (stream_Read (s, &first, 1) != 1))
269 event = (first & 0x80) ? first : tr->running_event;
271 switch (event & 0xf0)
273 case 0xF0: /* System Exclusive */
276 case 0xF0: /* System Specific */
278 /* TODO: don't skip these */
279 stream_Read (s, NULL, 1); /* Manuf ID */
283 if (stream_Read (s, &c, 1) != 1)
289 case 0xFF: /* SMF Meta Event */
294 if (stream_Read (s, &type, 1) != 1)
296 length = ReadVarInt (s);
301 "unknown SMF Meta Event type 0x%02X (%d bytes)",
303 /* TODO: parse these */
304 if (stream_Read (s, NULL, length) != length)
307 /* We MUST NOT pass this event to forward. It would be
308 * confused as a MIDI Reset real-time event. */
320 /* We cannot handle undefined "common" (non-real-time)
321 * events inside SMF, as we cannot differentiate a
322 * one byte delta-time (< 0x80) from event data. */
323 case 0xF7: /* End of sysex -> should never happen(?) */
324 msg_Err (p_demux, "unknown MIDI event 0x%02X", event);
325 return -1; /* undefined events */
339 /* FIXME: one message per block is very inefficient */
340 block = block_New (p_demux, 1 + datalen);
344 block->p_buffer[0] = event;
347 stream_Read (s, block->p_buffer + 1, datalen);
353 msg_Err (p_demux, "malformatted MIDI event");
354 return -1; /* can't use implicit running status with empty payload! */
357 block->p_buffer[1] = first;
359 stream_Read (s, block->p_buffer + 2, datalen - 1);
362 block->i_dts = block->i_pts = date_Get (&p_demux->p_sys->pts);
363 es_out_Send (p_demux->out, p_demux->p_sys->es, block);
367 /* If event is not real-time, update running status */
368 tr->running_event = event;
370 tr->offset = stream_Tell (s);
374 /*****************************************************************************
375 * Demux: read chunks and send them to the synthetizer
376 *****************************************************************************
377 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
378 *****************************************************************************/
379 static int Demux (demux_t *p_demux)
381 stream_t *s = p_demux->s;
382 demux_sys_t *p_sys = p_demux->p_sys;
383 uint64_t pulse = p_sys->pulse, next_pulse = UINT64_MAX;
385 if (pulse == UINT64_MAX)
386 return 0; /* all tracks are done */
388 es_out_Control (p_demux->out, ES_OUT_SET_PCR, date_Get (&p_sys->pts));
390 for (unsigned i = 0; i < p_sys->trackc; i++)
392 mtrk_t *track = p_sys->trackv + i;
394 while (track->next == pulse)
396 msg_Dbg (p_demux, "event on track %u", i);
397 if (HandleMessage (p_demux, track)
398 || ReadDeltaTime (s, track))
400 msg_Err (p_demux, "fatal parsing error");
405 if (track->next < next_pulse)
406 next_pulse = track->next;
409 if (next_pulse != UINT64_MAX)
410 date_Increment (&p_sys->pts, next_pulse - pulse);
411 p_sys->pulse = next_pulse;
417 /*****************************************************************************
419 *****************************************************************************/
420 static int Control (demux_t *p_demux, int i_query, va_list args)
422 return demux2_vaControlHelper (p_demux->s, 0, -1, 0, 1, i_query, args);