sd_ass: initialize structs for external tracks properly
[mplayer.git] / libmpdemux / demux_film.c
blob713c9ea26e27fae5559ba379544993cf910514a5
1 /*
2 * FILM file parser
3 * Copyright (C) 2002 Mike Melanson
5 * This demuxer handles FILM (a.k.a. CPK) files commonly found on Sega
6 * Saturn CD-ROM games. FILM files have also been found on 3DO games.
8 * details of the FILM file format can be found at:
9 * http://www.pcisys.net/~melanson/codecs/
11 * This file is part of MPlayer.
13 * MPlayer is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * MPlayer is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
32 #include "config.h"
33 #include "mp_msg.h"
35 #include "stream/stream.h"
36 #include "demuxer.h"
37 #include "stheader.h"
39 // chunk types found in a FILM file
40 #define CHUNK_FILM mmioFOURCC('F', 'I', 'L', 'M')
41 #define CHUNK_FDSC mmioFOURCC('F', 'D', 'S', 'C')
42 #define CHUNK_STAB mmioFOURCC('S', 'T', 'A', 'B')
44 typedef struct film_chunk_t
46 off_t chunk_offset;
47 int chunk_size;
48 unsigned int syncinfo1;
49 unsigned int syncinfo2;
51 float pts;
52 } film_chunk_t;
54 typedef struct film_data_t
56 unsigned int total_chunks;
57 unsigned int current_chunk;
58 film_chunk_t *chunks;
59 unsigned int chunks_per_second;
60 unsigned int film_version;
61 } film_data_t;
63 static void demux_seek_film(demuxer_t *demuxer, float rel_seek_secs, float audio_delay, int flags)
65 film_data_t *film_data = (film_data_t *)demuxer->priv;
66 int new_current_chunk=(flags&SEEK_ABSOLUTE)?0:film_data->current_chunk;
68 if(flags&SEEK_FACTOR)
69 new_current_chunk += rel_seek_secs * film_data->total_chunks; // 0..1
70 else
71 new_current_chunk += rel_seek_secs * film_data->chunks_per_second; // secs
74 mp_msg(MSGT_DECVIDEO, MSGL_INFO,"current, total chunks = %d, %d; seek %5.3f sec, new chunk guess = %d\n",
75 film_data->current_chunk, film_data->total_chunks,
76 rel_seek_secs, new_current_chunk);
78 // check if the new chunk number is valid
79 if (new_current_chunk < 0)
80 new_current_chunk = 0;
81 if ((unsigned int)new_current_chunk > film_data->total_chunks)
82 new_current_chunk = film_data->total_chunks - 1;
84 while (((film_data->chunks[new_current_chunk].syncinfo1 == 0xFFFFFFFF) ||
85 (film_data->chunks[new_current_chunk].syncinfo1 & 0x80000000)) &&
86 (new_current_chunk > 0))
87 new_current_chunk--;
89 film_data->current_chunk = new_current_chunk;
91 mp_msg(MSGT_DECVIDEO, MSGL_INFO," (flags = %X) actual new chunk = %d (syncinfo1 = %08X)\n",
92 flags, film_data->current_chunk, film_data->chunks[film_data->current_chunk].syncinfo1);
93 demuxer->video->pts=film_data->chunks[film_data->current_chunk].pts;
97 // return value:
98 // 0 = EOF or no stream found
99 // 1 = successfully read a packet
100 static int demux_film_fill_buffer(demuxer_t *demuxer, demux_stream_t *ds)
102 int i;
103 unsigned char byte_swap;
104 int cvid_size;
105 sh_video_t *sh_video = demuxer->video->sh;
106 sh_audio_t *sh_audio = demuxer->audio->sh;
107 film_data_t *film_data = (film_data_t *)demuxer->priv;
108 film_chunk_t film_chunk;
109 int length_fix_bytes;
110 demux_packet_t* dp;
112 // see if the end has been reached
113 if (film_data->current_chunk >= film_data->total_chunks)
114 return 0;
116 film_chunk = film_data->chunks[film_data->current_chunk];
118 // position stream and fetch chunk
119 stream_seek(demuxer->stream, film_chunk.chunk_offset);
121 // load the chunks manually (instead of using ds_read_packet()), since
122 // they require some adjustment
123 // (all ones in syncinfo1 indicates an audio chunk)
124 if (film_chunk.syncinfo1 == 0xFFFFFFFF)
126 if(demuxer->audio->id>=-1){ // audio not disabled
127 dp = new_demux_packet(film_chunk.chunk_size);
128 if (stream_read(demuxer->stream, dp->buffer, film_chunk.chunk_size) !=
129 film_chunk.chunk_size)
130 return 0;
131 dp->pts = film_chunk.pts;
132 dp->pos = film_chunk.chunk_offset;
134 // adjust the data before queuing it:
135 // 8-bit: signed -> unsigned
136 // 16-bit: big-endian -> little-endian
137 if (sh_audio->wf->wBitsPerSample == 8)
138 for (i = 0; i < film_chunk.chunk_size; i++)
139 dp->buffer[i] += 128;
140 else
141 for (i = 0; i < film_chunk.chunk_size; i += 2)
143 byte_swap = dp->buffer[i];
144 dp->buffer[i] = dp->buffer[i + 1];
145 dp->buffer[i + 1] = byte_swap;
148 /* for SegaSaturn .cpk file, translate audio data if stereo */
149 if (sh_audio->wf->nChannels == 2) {
150 if (sh_audio->wf->wBitsPerSample == 8) {
151 unsigned char* tmp = dp->buffer;
152 unsigned char buf[film_chunk.chunk_size];
153 for(i = 0; i < film_chunk.chunk_size/2; i++) {
154 buf[i*2] = tmp[i];
155 buf[i*2+1] = tmp[film_chunk.chunk_size/2+i];
157 memcpy( tmp, buf, film_chunk.chunk_size );
159 else {/* for 16bit */
160 unsigned short *tmp = (unsigned short *)dp->buffer;
161 unsigned short buf[film_chunk.chunk_size/2];
162 for(i = 0; i < film_chunk.chunk_size/4; i++) {
163 buf[i*2] = tmp[i];
164 buf[i*2+1] = tmp[film_chunk.chunk_size/4+i];
166 memcpy( tmp, buf, film_chunk.chunk_size );
170 // append packet to DS stream
171 ds_add_packet(demuxer->audio, dp);
174 else
176 // if the demuxer is dealing with CVID data, deal with it a special way
177 if (sh_video->format == mmioFOURCC('c', 'v', 'i', 'd'))
179 if (film_data->film_version)
180 length_fix_bytes = 2;
181 else
182 length_fix_bytes = 6;
184 // account for the fix bytes when allocating the buffer
185 dp = new_demux_packet(film_chunk.chunk_size - length_fix_bytes);
187 // these CVID data chunks have a few extra bytes; skip them
188 if (stream_read(demuxer->stream, dp->buffer, 10) != 10)
189 return 0;
190 stream_skip(demuxer->stream, length_fix_bytes);
192 if (stream_read(demuxer->stream, dp->buffer + 10,
193 film_chunk.chunk_size - (10 + length_fix_bytes)) !=
194 (film_chunk.chunk_size - (10 + length_fix_bytes)))
195 return 0;
197 dp->pts = film_chunk.pts;
198 dp->pos = film_chunk.chunk_offset;
199 dp->keyframe = film_chunk.syncinfo1 & 0x80000000;
201 // fix the CVID chunk size
202 cvid_size = film_chunk.chunk_size - length_fix_bytes;
203 dp->buffer[1] = (cvid_size >> 16) & 0xFF;
204 dp->buffer[2] = (cvid_size >> 8) & 0xFF;
205 dp->buffer[3] = (cvid_size >> 0) & 0xFF;
207 // append packet to DS stream
208 ds_add_packet(demuxer->video, dp);
210 else
212 ds_read_packet(demuxer->video, demuxer->stream, film_chunk.chunk_size,
213 film_chunk.pts,
214 film_chunk.chunk_offset, (film_chunk.syncinfo1 & 0x80000000) ? 1 : 0);
217 film_data->current_chunk++;
219 return 1;
222 static demuxer_t* demux_open_film(demuxer_t* demuxer)
224 sh_video_t *sh_video = NULL;
225 sh_audio_t *sh_audio = NULL;
226 film_data_t *film_data;
227 film_chunk_t film_chunk;
228 int header_size;
229 unsigned int chunk_type;
230 unsigned int chunk_size;
231 unsigned int i;
232 unsigned int video_format;
233 int audio_channels;
234 int counting_chunks;
235 unsigned int total_audio_bytes = 0;
237 film_data = malloc(sizeof(film_data_t));
238 film_data->total_chunks = 0;
239 film_data->current_chunk = 0;
240 film_data->chunks = NULL;
241 film_data->chunks_per_second = 0;
243 // go back to the beginning
244 stream_reset(demuxer->stream);
245 stream_seek(demuxer->stream, 0);
247 // read the master chunk type
248 chunk_type = stream_read_fourcc(demuxer->stream);
249 // validate the chunk type
250 if (chunk_type != CHUNK_FILM)
252 mp_msg(MSGT_DEMUX, MSGL_ERR, "Not a FILM file\n");
253 free(film_data);
254 return NULL;
257 // get the header size, which implicitly points past the header and
258 // to the start of the data
259 header_size = stream_read_dword(demuxer->stream);
260 film_data->film_version = stream_read_fourcc(demuxer->stream);
261 demuxer->movi_start = header_size;
262 demuxer->movi_end = demuxer->stream->end_pos;
263 header_size -= 16;
265 mp_msg(MSGT_DEMUX, MSGL_HINT, "FILM version %.4s\n",
266 (char *)&film_data->film_version);
268 // skip to where the next chunk should be
269 stream_skip(demuxer->stream, 4);
271 // traverse through the header
272 while (header_size > 0)
274 // fetch the chunk type and size
275 chunk_type = stream_read_fourcc(demuxer->stream);
276 chunk_size = stream_read_dword(demuxer->stream);
277 header_size -= chunk_size;
279 switch (chunk_type)
281 case CHUNK_FDSC:
282 mp_msg(MSGT_DECVIDEO, MSGL_V, "parsing FDSC chunk\n");
284 // fetch the video codec fourcc to see if there's any video
285 video_format = stream_read_fourcc(demuxer->stream);
286 if (video_format)
288 // create and initialize the video stream header
289 sh_video = new_sh_video(demuxer, 0);
290 demuxer->video->sh = sh_video;
291 sh_video->ds = demuxer->video;
293 sh_video->format = video_format;
294 sh_video->disp_h = stream_read_dword(demuxer->stream);
295 sh_video->disp_w = stream_read_dword(demuxer->stream);
296 mp_msg(MSGT_DECVIDEO, MSGL_V,
297 " FILM video: %d x %d\n", sh_video->disp_w,
298 sh_video->disp_h);
300 else
301 // skip height and width if no video
302 stream_skip(demuxer->stream, 8);
304 if(demuxer->audio->id<-1){
305 mp_msg(MSGT_DECVIDEO, MSGL_INFO,"chunk size = 0x%X \n",chunk_size);
306 stream_skip(demuxer->stream, chunk_size-12-8);
307 break; // audio disabled (or no soundcard)
310 // skip over unknown byte, but only if file had non-NULL version
311 if (film_data->film_version)
312 stream_skip(demuxer->stream, 1);
314 // fetch the audio channels to see if there's any audio
315 // don't do this if the file is a quirky file with NULL version
316 if (film_data->film_version)
318 audio_channels = stream_read_char(demuxer->stream);
319 if (audio_channels > 0)
321 // create and initialize the audio stream header
322 sh_audio = new_sh_audio(demuxer, 0);
323 demuxer->audio->id = 0;
324 demuxer->audio->sh = sh_audio;
325 sh_audio->ds = demuxer->audio;
327 sh_audio->wf = malloc(sizeof(*sh_audio->wf));
329 // uncompressed PCM format
330 sh_audio->wf->wFormatTag = 1;
331 sh_audio->format = 1;
332 sh_audio->wf->nChannels = audio_channels;
333 sh_audio->wf->wBitsPerSample = stream_read_char(demuxer->stream);
334 stream_skip(demuxer->stream, 1); // skip unknown byte
335 sh_audio->wf->nSamplesPerSec = stream_read_word(demuxer->stream);
336 sh_audio->wf->nAvgBytesPerSec =
337 sh_audio->wf->nSamplesPerSec * sh_audio->wf->wBitsPerSample
338 * sh_audio->wf->nChannels / 8;
339 stream_skip(demuxer->stream, 6); // skip the rest of the unknown
341 mp_msg(MSGT_DECVIDEO, MSGL_V,
342 " FILM audio: %d channels, %d bits, %d Hz\n",
343 sh_audio->wf->nChannels, 8 * sh_audio->wf->wBitsPerSample,
344 sh_audio->wf->nSamplesPerSec);
346 else
347 stream_skip(demuxer->stream, 10);
349 else
351 // otherwise, make some assumptions about the audio
353 // create and initialize the audio stream header
354 sh_audio = new_sh_audio(demuxer, 0);
355 demuxer->audio->sh = sh_audio;
356 sh_audio->ds = demuxer->audio;
358 sh_audio->wf = malloc(sizeof(*sh_audio->wf));
360 // uncompressed PCM format
361 sh_audio->wf->wFormatTag = 1;
362 sh_audio->format = 1;
363 sh_audio->wf->nChannels = 1;
364 sh_audio->wf->wBitsPerSample = 8;
365 sh_audio->wf->nSamplesPerSec = 22050;
366 sh_audio->wf->nAvgBytesPerSec =
367 sh_audio->wf->nSamplesPerSec * sh_audio->wf->wBitsPerSample
368 * sh_audio->wf->nChannels / 8;
370 mp_msg(MSGT_DECVIDEO, MSGL_V,
371 " FILM audio: %d channels, %d bits, %d Hz\n",
372 sh_audio->wf->nChannels, sh_audio->wf->wBitsPerSample,
373 sh_audio->wf->nSamplesPerSec);
375 break;
377 case CHUNK_STAB:
378 mp_msg(MSGT_DECVIDEO, MSGL_V, "parsing STAB chunk\n");
380 if (sh_video)
382 sh_video->fps = stream_read_dword(demuxer->stream);
383 sh_video->frametime = 1.0 / sh_video->fps;
386 // fetch the number of chunks
387 film_data->total_chunks = stream_read_dword(demuxer->stream);
388 film_data->current_chunk = 0;
389 mp_msg(MSGT_DECVIDEO, MSGL_V,
390 " STAB chunk contains %d chunks\n", film_data->total_chunks);
392 // allocate enough entries for the chunk
393 film_data->chunks =
394 calloc(film_data->total_chunks, sizeof(film_chunk_t));
396 // build the chunk index
397 counting_chunks = 1;
398 for (i = 0; i < film_data->total_chunks; i++)
400 film_chunk = film_data->chunks[i];
401 film_chunk.chunk_offset =
402 demuxer->movi_start + stream_read_dword(demuxer->stream);
403 film_chunk.chunk_size = stream_read_dword(demuxer->stream);
404 film_chunk.syncinfo1 = stream_read_dword(demuxer->stream);
405 film_chunk.syncinfo2 = stream_read_dword(demuxer->stream);
407 // count chunks for the purposes of seeking
408 if (counting_chunks)
410 // if we're counting chunks, always count an audio chunk
411 if (film_chunk.syncinfo1 == 0xFFFFFFFF)
412 film_data->chunks_per_second++;
413 // if it's a video chunk, check if it's time to stop counting
414 else if ((film_chunk.syncinfo1 & 0x7FFFFFFF) >= sh_video->fps)
415 counting_chunks = 0;
416 else
417 film_data->chunks_per_second++;
420 // precalculate PTS
421 if (film_chunk.syncinfo1 == 0xFFFFFFFF)
423 if(demuxer->audio->id>=-1)
424 film_chunk.pts =
425 (float)total_audio_bytes / (float)sh_audio->wf->nAvgBytesPerSec;
426 total_audio_bytes += film_chunk.chunk_size;
428 else
429 film_chunk.pts =
430 (film_chunk.syncinfo1 & 0x7FFFFFFF) / sh_video->fps;
432 film_data->chunks[i] = film_chunk;
435 // in some FILM files (notably '1.09'), the length of the FDSC chunk
436 // follows different rules
437 if (chunk_size == (film_data->total_chunks * 16))
438 header_size -= 16;
439 break;
441 default:
442 mp_msg(MSGT_DEMUX, MSGL_ERR, "Unrecognized FILM header chunk: %08X\n",
443 chunk_type);
444 return NULL;
445 break;
449 demuxer->priv = film_data;
451 return demuxer;
454 static void demux_close_film(demuxer_t* demuxer) {
455 film_data_t *film_data = demuxer->priv;
457 if(!film_data)
458 return;
459 free(film_data->chunks);
460 free(film_data);
464 static int film_check_file(demuxer_t* demuxer)
466 int signature=stream_read_fourcc(demuxer->stream);
468 // check for the FILM file magic number
469 if(signature==mmioFOURCC('F', 'I', 'L', 'M'))
470 return DEMUXER_TYPE_FILM;
472 return 0;
476 const demuxer_desc_t demuxer_desc_film = {
477 "FILM/CPK demuxer for Sega Saturn CD-ROM games",
478 "film",
479 "FILM",
480 "Mike Melanson",
482 DEMUXER_TYPE_FILM,
483 0, // unsafe autodetect (short signature)
484 film_check_file,
485 demux_film_fill_buffer,
486 demux_open_film,
487 demux_close_film,
488 demux_seek_film,
489 NULL