Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Copyright (C) 2001-2005 the xine project |
3 |
* |
4 |
* This file is part of xine, a free video player. |
5 |
* |
6 |
* xine is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* xine is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
/* |
22 |
* Midi File Demuxer by Jian Shi (jianshi@redflag-linux.com) |
23 |
* based on Midi and WAV specs that are available far and wide |
24 |
* require libtimidity to convert Midi sequence to WAV samples |
25 |
* |
26 |
*/ |
27 |
|
28 |
#ifdef HAVE_CONFIG_H |
29 |
#include "config.h" |
30 |
#endif |
31 |
|
32 |
#ifdef HAVE_TIMIDITY |
33 |
|
34 |
#include <stdio.h> |
35 |
#include <fcntl.h> |
36 |
#include <unistd.h> |
37 |
#include <string.h> |
38 |
#include <stdlib.h> |
39 |
#include <ctype.h> |
40 |
|
41 |
#include <timidity.h> |
42 |
|
43 |
#include "xine_internal.h" |
44 |
#include "xineutils.h" |
45 |
#include "demux.h" |
46 |
#include "buffer.h" |
47 |
#include "bswap.h" |
48 |
#include "group_audio.h" |
49 |
|
50 |
#define MIDI_SIGNATURE_SIZE 14 |
51 |
#define RIFF_SIGNATURE_SIZE 20 |
52 |
#define RMI_SIGNATURE_SIZE (RIFF_SIGNATURE_SIZE + MIDI_SIGNATURE_SIZE) |
53 |
#define PREFERED_FRAME_SIZE 4096 |
54 |
|
55 |
#define CONVERT_WAVE_RATE 44100 |
56 |
#define CONVERT_WAVE_BITPERSAMPLE 16 |
57 |
#define CONVERT_WAVE_CHANNEL 2 |
58 |
|
59 |
typedef struct { |
60 |
demux_plugin_t demux_plugin; |
61 |
xine_stream_t *stream; |
62 |
fifo_buffer_t *video_fifo; |
63 |
fifo_buffer_t *audio_fifo; |
64 |
input_plugin_t *input; |
65 |
int status; |
66 |
|
67 |
int start_pos; /* current position ,presented by time(ms) */ |
68 |
|
69 |
xine_waveformatex *wave_header; |
70 |
char *wave_frame_buf; /* wave samples for midi */ |
71 |
int wave_frame_size; /* the frame buf size ,presented by byte */ |
72 |
int wave_length; /* wave length presented by time(ms) */ |
73 |
|
74 |
MidSong *midsong; /* the mid-stream from libtimidity */ |
75 |
MidSongOptions convert_opts; /* convertion params */ |
76 |
|
77 |
int audio_type; /* the type of the instance of buf_element_t */ |
78 |
int seek_flag; /* this is set true when a seek occurred */ |
79 |
} demux_midi_t; |
80 |
|
81 |
typedef struct { |
82 |
demux_class_t demux_class; |
83 |
} demux_midi_class_t; |
84 |
|
85 |
static int demux_midi_get_stream_length(demux_plugin_t * this_gen); |
86 |
|
87 |
/* returns 1 if the MIDI file was opened successfully, 0 otherwise */ |
88 |
static int |
89 |
open_midi_file(demux_midi_t * this) |
90 |
{ |
91 |
int count = 0; |
92 |
char *rawbuf = NULL; |
93 |
char* title = NULL; |
94 |
char* comment = NULL; |
95 |
uint32_t rawbuf_len = 0; |
96 |
uint16_t mtrk_count = 0; |
97 |
uint32_t mtrk_len = 0; |
98 |
uint32_t header_size = 0; |
99 |
|
100 |
header_size = RMI_SIGNATURE_SIZE; |
101 |
rawbuf = (char *) malloc(header_size); |
102 |
if (rawbuf == NULL) |
103 |
return 0; |
104 |
|
105 |
/* strikly check the signature and the consistance for the MIDI file */ |
106 |
if (_x_demux_read_header(this->input, rawbuf, header_size) != |
107 |
header_size) { |
108 |
free(rawbuf); |
109 |
return 0; |
110 |
} |
111 |
|
112 |
if (rawbuf[0] == 'M' && rawbuf[1] == 'T' && |
113 |
rawbuf[2] == 'h' && rawbuf[3] == 'd'){ |
114 |
this->input->seek(this->input, MIDI_SIGNATURE_SIZE, SEEK_SET); |
115 |
} |
116 |
else if(rawbuf[0] == 'R' && rawbuf[1] == 'I' && |
117 |
rawbuf[2] == 'F' && rawbuf[3] == 'F' && |
118 |
rawbuf[8] == 'R' && rawbuf[9] == 'M' && |
119 |
rawbuf[10] == 'I' && rawbuf[11] == 'D' && |
120 |
rawbuf[12] == 'd' && rawbuf[13] == 'a' && |
121 |
rawbuf[14] == 't' && rawbuf[15] == 'a' && |
122 |
rawbuf[20] == 'M' && rawbuf[21] == 'T' && |
123 |
rawbuf[22] == 'h' && rawbuf[23] == 'd'){ |
124 |
char* tmp_buf = (char*)malloc(MIDI_SIGNATURE_SIZE); |
125 |
memcpy(tmp_buf, rawbuf + RIFF_SIGNATURE_SIZE, MIDI_SIGNATURE_SIZE); |
126 |
free(rawbuf); |
127 |
rawbuf = tmp_buf; |
128 |
this->input->seek(this->input, RMI_SIGNATURE_SIZE, SEEK_SET); |
129 |
} |
130 |
else{ |
131 |
free(rawbuf); |
132 |
return 0; |
133 |
} |
134 |
|
135 |
rawbuf_len = MIDI_SIGNATURE_SIZE; |
136 |
mtrk_count = *(uint16_t *) (rawbuf + 10); |
137 |
mtrk_count = mtrk_count >> 8 | mtrk_count << 8; |
138 |
while (count < mtrk_count) { |
139 |
char mtrk_header[8]; |
140 |
|
141 |
if (this->input->read(this->input, mtrk_header, 8) != 8) { |
142 |
free(rawbuf); |
143 |
return 0; |
144 |
} |
145 |
|
146 |
if (mtrk_header[0] != 'M' || mtrk_header[1] != 'T' || |
147 |
mtrk_header[2] != 'r' || mtrk_header[3] != 'k'){ |
148 |
free(rawbuf); |
149 |
return 0; |
150 |
} |
151 |
mtrk_len = *(uint32_t *) (mtrk_header + 4); |
152 |
mtrk_len = (mtrk_len >> 24 & 0x000000FF) | |
153 |
(mtrk_len >> 8 & 0x0000FF00) | |
154 |
(mtrk_len << 8 & 0x00FF0000) | |
155 |
(mtrk_len << 24 & 0xFF000000); |
156 |
|
157 |
rawbuf = (char *) realloc(rawbuf, rawbuf_len + mtrk_len + 8); |
158 |
if(rawbuf == NULL){ |
159 |
free(rawbuf); |
160 |
return 0; |
161 |
} |
162 |
|
163 |
memcpy(rawbuf + rawbuf_len, mtrk_header, 8); |
164 |
rawbuf_len += 8; |
165 |
if (this->input-> |
166 |
read(this->input, rawbuf + rawbuf_len, |
167 |
mtrk_len) != mtrk_len) { |
168 |
free(rawbuf); |
169 |
return 0; |
170 |
} |
171 |
|
172 |
rawbuf_len += mtrk_len; |
173 |
count++; |
174 |
} |
175 |
|
176 |
this->wave_header = |
177 |
(xine_waveformatex *) malloc(sizeof (xine_waveformatex)); |
178 |
this->wave_header->wFormatTag = 0x0001; |
179 |
this->wave_header->nChannels = CONVERT_WAVE_CHANNEL; |
180 |
this->wave_header->nSamplesPerSec = CONVERT_WAVE_RATE; |
181 |
this->wave_header->nAvgBytesPerSec = |
182 |
CONVERT_WAVE_RATE * CONVERT_WAVE_CHANNEL * |
183 |
CONVERT_WAVE_BITPERSAMPLE / 8; |
184 |
this->wave_header->nBlockAlign = |
185 |
CONVERT_WAVE_BITPERSAMPLE * CONVERT_WAVE_CHANNEL / 8; |
186 |
this->wave_header->wBitsPerSample = CONVERT_WAVE_BITPERSAMPLE; |
187 |
this->wave_header->cbSize = 0; |
188 |
|
189 |
this->convert_opts.rate = CONVERT_WAVE_RATE; |
190 |
this->convert_opts.format = MID_AUDIO_S16LSB; |
191 |
this->convert_opts.channels = CONVERT_WAVE_CHANNEL; |
192 |
this->convert_opts.buffer_size = 4096; |
193 |
|
194 |
mid_init(NULL); |
195 |
MidIStream *midi_stream = |
196 |
mid_istream_open_mem((void *) rawbuf, rawbuf_len, 0); |
197 |
|
198 |
this->midsong = mid_song_load(midi_stream, &this->convert_opts); |
199 |
mid_istream_close(midi_stream); |
200 |
|
201 |
free(rawbuf); |
202 |
|
203 |
if (this->midsong == NULL){ |
204 |
mid_exit(); |
205 |
return 0; |
206 |
} |
207 |
|
208 |
title = mid_song_get_meta(this->midsong, MID_SONG_TEXT); |
209 |
comment = mid_song_get_meta(this->midsong, MID_SONG_COPYRIGHT); |
210 |
if (title) { |
211 |
_x_meta_info_n_set(this->stream, XINE_META_INFO_TITLE, |
212 |
title, strlen(title)); |
213 |
} |
214 |
if(comment){ |
215 |
_x_meta_info_n_set(this->stream, XINE_META_INFO_COMMENT, |
216 |
comment, strlen(comment)); |
217 |
} |
218 |
|
219 |
this->wave_length = mid_song_get_total_time(this->midsong); |
220 |
if (this->wave_length == 0){ |
221 |
mid_exit(); |
222 |
return 0; |
223 |
} |
224 |
|
225 |
this->wave_frame_size = PREFERED_FRAME_SIZE / this->wave_header-> |
226 |
nBlockAlign * this->wave_header->nBlockAlign; |
227 |
this->wave_frame_buf = (char *) malloc(this->wave_frame_size); |
228 |
if (this->wave_frame_buf == NULL){ |
229 |
mid_exit(); |
230 |
return 0; |
231 |
} |
232 |
|
233 |
mid_song_start(this->midsong); |
234 |
|
235 |
this->start_pos = 0; |
236 |
this->audio_type = |
237 |
_x_formattag_to_buf_audio(0x0001 /* PCM WAVE FORMAT */ ); |
238 |
|
239 |
return 1; |
240 |
} |
241 |
|
242 |
static int |
243 |
demux_midi_send_chunk(demux_plugin_t * this_gen) |
244 |
{ |
245 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
246 |
|
247 |
buf_element_t *buf = NULL; |
248 |
unsigned int remaining_bytes = 0; |
249 |
unsigned int local_start_pos = 0; |
250 |
|
251 |
remaining_bytes = |
252 |
mid_song_read_wave(this->midsong, this->wave_frame_buf, |
253 |
this->wave_frame_size); |
254 |
if (remaining_bytes == 0){ |
255 |
this->status = DEMUX_FINISHED; |
256 |
return this->status; |
257 |
} |
258 |
|
259 |
if (this->seek_flag) { |
260 |
_x_demux_control_newpts(this->stream, this->start_pos * 90, |
261 |
BUF_FLAG_SEEK); |
262 |
this->seek_flag = 0; |
263 |
} |
264 |
|
265 |
while (remaining_bytes) { |
266 |
local_start_pos = this->start_pos; |
267 |
if (!this->audio_fifo) { |
268 |
this->status = DEMUX_FINISHED; |
269 |
break; |
270 |
} |
271 |
|
272 |
buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo); |
273 |
bzero(buf->content, buf->max_size); |
274 |
if (this->wave_length != 0) |
275 |
buf->extra_info->input_normpos = |
276 |
(int) ((double) local_start_pos * 65535 / |
277 |
this->wave_length); |
278 |
buf->extra_info->input_time = local_start_pos; |
279 |
buf->pts = local_start_pos * 90; |
280 |
|
281 |
if (remaining_bytes > buf->max_size) |
282 |
buf->size = buf->max_size; |
283 |
else |
284 |
buf->size = remaining_bytes; |
285 |
|
286 |
if (memcpy |
287 |
(buf->content, |
288 |
this->wave_frame_buf + (buf->size - remaining_bytes), |
289 |
buf->size) != buf->content) { |
290 |
buf->free_buffer(buf); |
291 |
this->status = DEMUX_FINISHED; |
292 |
break; |
293 |
} |
294 |
|
295 |
|
296 |
remaining_bytes -= buf->size; |
297 |
if (remaining_bytes == 0){ |
298 |
buf->decoder_flags |= BUF_FLAG_FRAME_END; |
299 |
} |
300 |
|
301 |
buf->type = this->audio_type; |
302 |
this->audio_fifo->put(this->audio_fifo, buf); |
303 |
|
304 |
} |
305 |
this->start_pos = mid_song_get_time(this->midsong); |
306 |
|
307 |
return this->status; |
308 |
} |
309 |
|
310 |
static void |
311 |
demux_midi_send_headers(demux_plugin_t * this_gen) |
312 |
{ |
313 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
314 |
buf_element_t *buf; |
315 |
|
316 |
this->video_fifo = this->stream->video_fifo; |
317 |
this->audio_fifo = this->stream->audio_fifo; |
318 |
|
319 |
this->status = DEMUX_OK; |
320 |
|
321 |
/* load stream information */ |
322 |
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 0); |
323 |
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO, 1); |
324 |
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_CHANNELS, |
325 |
this->wave_header->nChannels); |
326 |
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE, |
327 |
this->wave_header->nSamplesPerSec); |
328 |
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITS, |
329 |
this->wave_header->wBitsPerSample); |
330 |
/* send start buffers */ |
331 |
_x_demux_control_start(this->stream); |
332 |
|
333 |
/* send init info to decoders */ |
334 |
if (this->audio_fifo) { |
335 |
buf = this->audio_fifo->buffer_pool_alloc(this->audio_fifo); |
336 |
buf->type = this->audio_type; |
337 |
buf->decoder_flags = |
338 |
BUF_FLAG_HEADER | BUF_FLAG_STDHEADER | BUF_FLAG_FRAME_END; |
339 |
buf->decoder_info[0] = 0; |
340 |
buf->decoder_info[1] = this->wave_header->nSamplesPerSec; |
341 |
buf->decoder_info[2] = this->wave_header->wBitsPerSample; |
342 |
buf->decoder_info[3] = this->wave_header->nChannels; |
343 |
buf->content = (void *) this->wave_header; |
344 |
buf->size = 16; /* the size of WAV fmt header */ |
345 |
this->audio_fifo->put(this->audio_fifo, buf); |
346 |
} |
347 |
} |
348 |
|
349 |
static int |
350 |
demux_midi_seek(demux_plugin_t * this_gen, |
351 |
off_t start_pos, int start_time, int playing) |
352 |
{ |
353 |
|
354 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
355 |
start_pos = (off_t) ((double) start_pos / 65535 * this->wave_length); |
356 |
|
357 |
this->seek_flag = 1; |
358 |
this->status = DEMUX_OK; |
359 |
_x_demux_flush_engine(this->stream); |
360 |
|
361 |
if (start_time != 0) { |
362 |
start_pos = start_time; |
363 |
} |
364 |
|
365 |
/* check the boundary offsets */ |
366 |
if (start_pos <= 0) |
367 |
this->start_pos = 0; |
368 |
else if (start_pos >= this->wave_length) { |
369 |
this->status = DEMUX_FINISHED; |
370 |
return this->status; |
371 |
} else { |
372 |
this->start_pos = start_pos; |
373 |
} |
374 |
mid_song_seek(this->midsong, start_pos); |
375 |
|
376 |
return this->status; |
377 |
} |
378 |
|
379 |
static void |
380 |
demux_midi_dispose(demux_plugin_t * this_gen) |
381 |
{ |
382 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
383 |
free(this->wave_header); |
384 |
free(this->wave_frame_buf); |
385 |
mid_song_free(this->midsong); |
386 |
free(this); |
387 |
} |
388 |
|
389 |
static int |
390 |
demux_midi_get_status(demux_plugin_t * this_gen) |
391 |
{ |
392 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
393 |
|
394 |
return this->status; |
395 |
} |
396 |
|
397 |
static int |
398 |
demux_midi_get_stream_length(demux_plugin_t * this_gen) |
399 |
{ |
400 |
demux_midi_t *this = (demux_midi_t *) this_gen; |
401 |
|
402 |
return this->wave_length; |
403 |
} |
404 |
|
405 |
static uint32_t |
406 |
demux_midi_get_capabilities(demux_plugin_t * this_gen) |
407 |
{ |
408 |
return DEMUX_CAP_NOCAP; |
409 |
} |
410 |
|
411 |
static int |
412 |
demux_midi_get_optional_data(demux_plugin_t * this_gen, |
413 |
void *data, int data_type) |
414 |
{ |
415 |
return DEMUX_OPTIONAL_UNSUPPORTED; |
416 |
} |
417 |
|
418 |
static demux_plugin_t * |
419 |
open_plugin(demux_class_t * class_gen, xine_stream_t * stream, |
420 |
input_plugin_t * input) |
421 |
{ |
422 |
|
423 |
demux_midi_t *this; |
424 |
|
425 |
this = xine_xmalloc(sizeof (demux_midi_t)); |
426 |
this->stream = stream; |
427 |
this->input = input; |
428 |
|
429 |
this->demux_plugin.send_headers = demux_midi_send_headers; |
430 |
this->demux_plugin.send_chunk = demux_midi_send_chunk; |
431 |
this->demux_plugin.seek = demux_midi_seek; |
432 |
this->demux_plugin.dispose = demux_midi_dispose; |
433 |
this->demux_plugin.get_status = demux_midi_get_status; |
434 |
this->demux_plugin.get_stream_length = demux_midi_get_stream_length; |
435 |
this->demux_plugin.get_capabilities = demux_midi_get_capabilities; |
436 |
this->demux_plugin.get_optional_data = demux_midi_get_optional_data; |
437 |
this->demux_plugin.demux_class = class_gen; |
438 |
|
439 |
this->status = DEMUX_FINISHED; |
440 |
|
441 |
switch (stream->content_detection_method) { |
442 |
|
443 |
case METHOD_BY_EXTENSION:{ |
444 |
const char *extensions, *mrl; |
445 |
|
446 |
mrl = input->get_mrl(input); |
447 |
extensions = class_gen->get_extensions(class_gen); |
448 |
|
449 |
if (!_x_demux_check_extension(mrl, extensions)) { |
450 |
free(this); |
451 |
return NULL; |
452 |
} |
453 |
break; |
454 |
} |
455 |
|
456 |
case METHOD_BY_CONTENT: |
457 |
case METHOD_EXPLICIT: |
458 |
break; |
459 |
|
460 |
default: |
461 |
free(this); |
462 |
return NULL; |
463 |
} |
464 |
|
465 |
if (open_midi_file(this) == 0) { |
466 |
free(this); |
467 |
return NULL; |
468 |
} |
469 |
|
470 |
return &this->demux_plugin; |
471 |
} |
472 |
|
473 |
static const char * |
474 |
get_description(demux_class_t * this_gen) |
475 |
{ |
476 |
return "MIDI file demux plugin"; |
477 |
} |
478 |
|
479 |
static const char * |
480 |
get_identifier(demux_class_t * this_gen) |
481 |
{ |
482 |
return "MIDI"; |
483 |
} |
484 |
|
485 |
static const char * |
486 |
get_extensions(demux_class_t * this_gen) |
487 |
{ |
488 |
return "mid"; |
489 |
} |
490 |
|
491 |
static const char * |
492 |
get_mimetypes(demux_class_t * this_gen) |
493 |
{ |
494 |
return "audio/x-midi: midi: MIDI audio;" |
495 |
"audio/x-midi: mid: MIDI audio;"; |
496 |
} |
497 |
|
498 |
static void |
499 |
class_dispose(demux_class_t * this_gen) |
500 |
{ |
501 |
demux_midi_class_t *this = (demux_midi_class_t *) this_gen; |
502 |
free(this); |
503 |
} |
504 |
|
505 |
void * |
506 |
demux_midi_init_plugin(xine_t * xine, void *data) |
507 |
{ |
508 |
demux_midi_class_t *this; |
509 |
|
510 |
this = xine_xmalloc(sizeof (demux_midi_class_t)); |
511 |
|
512 |
this->demux_class.open_plugin = open_plugin; |
513 |
this->demux_class.get_description = get_description; |
514 |
this->demux_class.get_identifier = get_identifier; |
515 |
this->demux_class.get_mimetypes = get_mimetypes; |
516 |
this->demux_class.get_extensions = get_extensions; |
517 |
this->demux_class.dispose = class_dispose; |
518 |
|
519 |
return this; |
520 |
} |
521 |
|
522 |
#endif /* HAVE_TIMIDITY*/ |
523 |
|