Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3d8280]: src / demuxers / demux_4xm.c Maximize Restore History

Download this file

demux_4xm.c    520 lines (427 with data), 15.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* Copyright (C) 2000-2003 the xine project
*
* This file is part of xine, a free video player.
*
* xine 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
* (at your option) any later version.
*
* xine 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.
*
* 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, USA
*/
/*
* 4X Technologies (.4xm) File Demuxer by Mike Melanson (melanson@pcisys.net)
* For more information on the 4xm file format, visit:
* http://www.pcisys.net/~melanson/codecs/
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#define LOG_MODULE "demux_4xm"
#define LOG_VERBOSE
/*
#define LOG
*/
#include <xine/xine_internal.h>
#include <xine/xineutils.h>
#include <xine/compat.h>
#include <xine/demux.h>
#include "bswap.h"
#include "group_games.h"
#define FOURCC_TAG LE_FOURCC
#define RIFF_TAG FOURCC_TAG('R', 'I', 'F', 'F')
#define _4XMV_TAG FOURCC_TAG('4', 'X', 'M', 'V')
#define LIST_TAG FOURCC_TAG('L', 'I', 'S', 'T')
#define HEAD_TAG FOURCC_TAG('H', 'E', 'A', 'D')
#define TRK__TAG FOURCC_TAG('T', 'R', 'K', '_')
#define MOVI_TAG FOURCC_TAG('M', 'O', 'V', 'I')
#define VTRK_TAG FOURCC_TAG('V', 'T', 'R', 'K')
#define STRK_TAG FOURCC_TAG('S', 'T', 'R', 'K')
#define std__TAG FOURCC_TAG('s', 't', 'd', '_')
#define name_TAG FOURCC_TAG('n', 'a', 'm', 'e')
#define vtrk_TAG FOURCC_TAG('v', 't', 'r', 'k')
#define strk_TAG FOURCC_TAG('s', 't', 'r', 'k')
#define ifrm_TAG FOURCC_TAG('i', 'f', 'r', 'm')
#define pfrm_TAG FOURCC_TAG('p', 'f', 'r', 'm')
#define cfrm_TAG FOURCC_TAG('c', 'f', 'r', 'm')
#define snd__TAG FOURCC_TAG('s', 'n', 'd', '_')
#define vtrk_SIZE 0x44
#define strk_SIZE 0x28
typedef struct AudioTrack {
unsigned int audio_type;
int sample_rate;
int bits;
int channels;
} audio_track_t;
typedef struct {
demux_plugin_t demux_plugin;
xine_stream_t *stream;
fifo_buffer_t *video_fifo;
fifo_buffer_t *audio_fifo;
input_plugin_t *input;
int status;
unsigned int filesize;
xine_bmiheader bih;
unsigned int track_count;
audio_track_t *tracks;
int64_t video_pts;
int64_t video_pts_inc;
int64_t duration_in_ms;
} demux_fourxm_t;
typedef struct {
demux_class_t demux_class;
} demux_fourxm_class_t;
static float get_le_float(unsigned char *buffer)
{
float f;
unsigned char *float_buffer = (unsigned char *)&f;
#ifdef WORDS_BIGENDIAN
float_buffer[0] = buffer[3];
float_buffer[1] = buffer[2];
float_buffer[2] = buffer[1];
float_buffer[3] = buffer[0];
#else
float_buffer[0] = buffer[0];
float_buffer[1] = buffer[1];
float_buffer[2] = buffer[2];
float_buffer[3] = buffer[3];
#endif
return f;
}
/* Open a 4xm file
* This function is called from the _open() function of this demuxer.
* It returns 1 if 4xm file was opened successfully. */
static int open_fourxm_file(demux_fourxm_t *fourxm) {
unsigned char preview[12];
int header_size;
unsigned char *header;
int i;
unsigned int fourcc_tag;
unsigned int size;
unsigned int current_track;
unsigned int audio_type;
unsigned int total_frames;
float fps;
/* the file signature will be in the first 12 bytes */
if (_x_demux_read_header(fourxm->input, preview, 12) != 12)
return 0;
/* check for the signature tags */
if ((_X_LE_32(&preview[0]) != RIFF_TAG) ||
(_X_LE_32(&preview[8]) != _4XMV_TAG))
return 0;
/* file is qualified; skip over the header bytes in the stream */
fourxm->input->seek(fourxm->input, 12, SEEK_SET);
/* fetch the LIST-HEAD header */
if (fourxm->input->read(fourxm->input, preview, 12) != 12)
return 0;
if ((_X_LE_32(&preview[0]) != LIST_TAG) ||
(_X_LE_32(&preview[8]) != HEAD_TAG))
return 0;
/* read the whole header */
header_size = _X_LE_32(&preview[4]) - 4;
header = xine_xmalloc(header_size);
if (!header || fourxm->input->read(fourxm->input, header, header_size) != header_size) {
free(header);
return 0;
}
fourxm->bih.biWidth = 0;
fourxm->bih.biHeight = 0;
fourxm->track_count = 0;
fourxm->tracks = NULL;
fourxm->video_pts_inc = 0;
/* take the lazy approach and search for any and all vtrk and strk chunks */
for (i = 0; i < header_size - 8; i++) {
fourcc_tag = _X_LE_32(&header[i]);
size = _X_LE_32(&header[i + 4]);
if (fourcc_tag == std__TAG) {
fps = get_le_float(&header[i + 12]);
fourxm->video_pts_inc = (int64_t)(90000.0 / fps);
} else if (fourcc_tag == vtrk_TAG) {
/* check that there is enough data */
if (size != vtrk_SIZE) {
free(header);
return 0;
}
total_frames = _X_LE_32(&header[i + 24]);
fourxm->duration_in_ms = total_frames;
fourxm->duration_in_ms *= fourxm->video_pts_inc;
fourxm->duration_in_ms /= 90000;
fourxm->duration_in_ms *= 1000;
fourxm->bih.biWidth = _X_LE_32(&header[i + 36]);
fourxm->bih.biHeight = _X_LE_32(&header[i + 40]);
i += 8 + size;
} else if (fourcc_tag == strk_TAG) {
/* check that there is enough data */
if (size != strk_SIZE) {
free(header);
return 0;
}
current_track = _X_LE_32(&header[i + 8]);
if (current_track + 1 > fourxm->track_count) {
fourxm->track_count = current_track + 1;
fourxm->tracks = realloc(fourxm->tracks,
fourxm->track_count * sizeof(audio_track_t));
if (!fourxm->tracks) {
free(header);
return 0;
}
}
fourxm->tracks[current_track].channels = _X_LE_32(&header[i + 36]);
fourxm->tracks[current_track].sample_rate = _X_LE_32(&header[i + 40]);
fourxm->tracks[current_track].bits = _X_LE_32(&header[i + 44]);
audio_type = _X_LE_32(&header[i + 12]);
if (audio_type == 0)
fourxm->tracks[current_track].audio_type = BUF_AUDIO_LPCM_LE;
else if (audio_type == 1)
fourxm->tracks[current_track].audio_type = BUF_AUDIO_4X_ADPCM;
fourxm->tracks[current_track].audio_type += (current_track & 0x0000FFFF);
i += 8 + size;
}
}
fourxm->filesize = fourxm->input->get_length(fourxm->input);
/* this will get bumped to 0 on the first iteration */
fourxm->video_pts = -fourxm->video_pts_inc;
/* skip the data body LIST header */
fourxm->input->seek(fourxm->input, 12, SEEK_CUR);
free(header);
return 1;
}
static int demux_fourxm_send_chunk(demux_plugin_t *this_gen) {
demux_fourxm_t *this = (demux_fourxm_t *) this_gen;
buf_element_t *buf = NULL;
unsigned int fourcc_tag;
unsigned int size;
unsigned char header[8];
unsigned int remaining_bytes;
unsigned int current_track;
/* read the next header */
if (this->input->read(this->input, header, 8) != 8) {
this->status = DEMUX_FINISHED;
return this->status;
}
fourcc_tag = _X_LE_32(&header[0]);
size = _X_LE_32(&header[4]);
switch (fourcc_tag) {
case ifrm_TAG:
case pfrm_TAG:
case cfrm_TAG:
/* send the 8-byte chunk header first */
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
buf->type = BUF_VIDEO_4XM;
if( this->filesize )
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 / this->filesize );
buf->extra_info->input_time = this->video_pts / 90;
buf->pts = this->video_pts;
buf->size = 8;
memcpy(buf->content, header, 8);
if (fourcc_tag == ifrm_TAG)
buf->decoder_flags |= BUF_FLAG_KEYFRAME;
this->video_fifo->put(this->video_fifo, buf);
remaining_bytes = size;
while (remaining_bytes) {
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
buf->type = BUF_VIDEO_4XM;
if( this->filesize )
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 / this->filesize );
buf->extra_info->input_time = this->video_pts / 90;
buf->pts = this->video_pts;
if (remaining_bytes > buf->max_size)
buf->size = buf->max_size;
else
buf->size = remaining_bytes;
remaining_bytes -= buf->size;
if (this->input->read(this->input, buf->content, buf->size) !=
buf->size) {
buf->free_buffer(buf);
this->status = DEMUX_FINISHED;
break;
}
if (!remaining_bytes)
buf->decoder_flags |= BUF_FLAG_FRAME_END;
if (fourcc_tag == ifrm_TAG)
buf->decoder_flags |= BUF_FLAG_KEYFRAME;
this->video_fifo->put(this->video_fifo, buf);
}
break;
case snd__TAG:
/* fetch the track number and audio chunk size */
if (this->input->read(this->input, header, 8) != 8) {
this->status = DEMUX_FINISHED;
return this->status;
}
current_track = _X_LE_32(&header[0]);
// size = _X_LE_32(&header[4]);
if (current_track >= this->track_count) {
lprintf ("bad audio track number (%d >= %d)\n",
current_track, this->track_count);
this->status = DEMUX_FINISHED;
return this->status;
}
remaining_bytes = size - 8;
while (remaining_bytes) {
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
buf->type = this->tracks[current_track].audio_type;
if( this->filesize )
buf->extra_info->input_normpos = (int)( (double) this->input->get_current_pos (this->input) *
65535 / this->filesize );
/* let the engine sort it out */
buf->extra_info->input_time = 0;
buf->pts = 0;
if (remaining_bytes > buf->max_size)
buf->size = buf->max_size;
else
buf->size = remaining_bytes;
remaining_bytes -= buf->size;
if (this->input->read(this->input, buf->content, buf->size) !=
buf->size) {
buf->free_buffer(buf);
this->status = DEMUX_FINISHED;
break;
}
if (!remaining_bytes)
buf->decoder_flags |= BUF_FLAG_FRAME_END;
this->audio_fifo->put(this->audio_fifo, buf);
}
break;
case LIST_TAG:
/* skip LIST header */
this->input->seek(this->input, 4, SEEK_CUR);
/* take this opportunity to bump the video pts */
this->video_pts += this->video_pts_inc;
break;
default:
lprintf("bad chunk: %c%c%c%c (%02X%02X%02X%02X)\n",
header[0], header[1], header[2], header[3],
header[0], header[1], header[2], header[3]);
this->status = DEMUX_FINISHED;
break;
}
return this->status;
}
static void demux_fourxm_send_headers(demux_plugin_t *this_gen) {
demux_fourxm_t *this = (demux_fourxm_t *) this_gen;
buf_element_t *buf;
this->video_fifo = this->stream->video_fifo;
this->audio_fifo = this->stream->audio_fifo;
this->status = DEMUX_OK;
/* load stream information */
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_VIDEO, 1);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_HAS_AUDIO,
(this->track_count > 0) ? 1 : 0);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_WIDTH,
this->bih.biWidth);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_VIDEO_HEIGHT,
this->bih.biHeight);
if (this->track_count > 0) {
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_CHANNELS,
this->tracks[0].channels);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_SAMPLERATE,
this->tracks[0].sample_rate);
_x_stream_info_set(this->stream, XINE_STREAM_INFO_AUDIO_BITS,
this->tracks[0].bits);
}
/* send start buffers */
_x_demux_control_start(this->stream);
/* send init info to decoders */
buf = this->video_fifo->buffer_pool_alloc (this->video_fifo);
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAMERATE|
BUF_FLAG_FRAME_END;
buf->decoder_info[0] = this->video_pts_inc; /* initial video_step */
memcpy(buf->content, &this->bih, sizeof(this->bih));
buf->size = sizeof(this->bih);
buf->type = BUF_VIDEO_4XM;
this->video_fifo->put (this->video_fifo, buf);
if (this->audio_fifo && this->track_count > 0) {
buf = this->audio_fifo->buffer_pool_alloc (this->audio_fifo);
buf->type = this->tracks[0].audio_type;
buf->decoder_flags = BUF_FLAG_HEADER|BUF_FLAG_STDHEADER|BUF_FLAG_FRAME_END;
buf->decoder_info[0] = 0;
buf->decoder_info[1] = this->tracks[0].sample_rate;
buf->decoder_info[2] = this->tracks[0].bits;
buf->decoder_info[3] = this->tracks[0].channels;
this->audio_fifo->put (this->audio_fifo, buf);
}
}
static int demux_fourxm_seek (demux_plugin_t *this_gen,
off_t start_pos, int start_time, int playing) {
demux_fourxm_t *this = (demux_fourxm_t *) this_gen;
/* if thread is not running, initialize demuxer */
if( !playing ) {
/* send new pts */
_x_demux_control_newpts(this->stream, 0, 0);
this->status = DEMUX_OK;
}
return this->status;
}
static int demux_fourxm_get_status (demux_plugin_t *this_gen) {
demux_fourxm_t *this = (demux_fourxm_t *) this_gen;
return this->status;
}
static int demux_fourxm_get_stream_length (demux_plugin_t *this_gen) {
demux_fourxm_t *this = (demux_fourxm_t *) this_gen;
return this->duration_in_ms;
}
static uint32_t demux_fourxm_get_capabilities(demux_plugin_t *this_gen) {
return DEMUX_CAP_NOCAP;
}
static int demux_fourxm_get_optional_data(demux_plugin_t *this_gen,
void *data, int data_type) {
return DEMUX_OPTIONAL_UNSUPPORTED;
}
static demux_plugin_t *open_plugin (demux_class_t *class_gen, xine_stream_t *stream,
input_plugin_t *input) {
demux_fourxm_t *this;
this = xine_xmalloc (sizeof (demux_fourxm_t));
this->stream = stream;
this->input = input;
this->demux_plugin.send_headers = demux_fourxm_send_headers;
this->demux_plugin.send_chunk = demux_fourxm_send_chunk;
this->demux_plugin.seek = demux_fourxm_seek;
this->demux_plugin.dispose = default_demux_plugin_dispose;
this->demux_plugin.get_status = demux_fourxm_get_status;
this->demux_plugin.get_stream_length = demux_fourxm_get_stream_length;
this->demux_plugin.get_capabilities = demux_fourxm_get_capabilities;
this->demux_plugin.get_optional_data = demux_fourxm_get_optional_data;
this->demux_plugin.demux_class = class_gen;
this->status = DEMUX_FINISHED;
switch (stream->content_detection_method) {
case METHOD_BY_MRL:
case METHOD_BY_CONTENT:
case METHOD_EXPLICIT:
if (!open_fourxm_file(this)) {
free (this);
return NULL;
}
break;
default:
free (this);
return NULL;
}
return &this->demux_plugin;
}
void *demux_fourxm_init_plugin (xine_t *xine, void *data) {
demux_fourxm_class_t *this;
this = xine_xmalloc (sizeof (demux_fourxm_class_t));
this->demux_class.open_plugin = open_plugin;
this->demux_class.description = N_("4X Technologies (4xm) demux plugin");
this->demux_class.identifier = "4X Technologies";
this->demux_class.mimetypes = NULL;
this->demux_class.extensions = "4xm";
this->demux_class.dispose = default_demux_class_dispose;
return this;
}