808
|
1 /*
|
|
2 * Sega FILM Format (CPK) Demuxer
|
|
3 * Copyright (c) 2003 The ffmpeg Project
|
|
4 *
|
|
5 * This file is part of FFmpeg.
|
|
6 *
|
|
7 * FFmpeg is free software; you can redistribute it and/or
|
|
8 * modify it under the terms of the GNU Lesser General Public
|
|
9 * License as published by the Free Software Foundation; either
|
|
10 * version 2.1 of the License, or (at your option) any later version.
|
|
11 *
|
|
12 * FFmpeg 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 GNU
|
|
15 * Lesser General Public License for more details.
|
|
16 *
|
|
17 * You should have received a copy of the GNU Lesser General Public
|
|
18 * License along with FFmpeg; if not, write to the Free Software
|
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * @file segafilm.c
|
|
24 * Sega FILM (.cpk) file demuxer
|
|
25 * by Mike Melanson (melanson@pcisys.net)
|
|
26 * For more information regarding the Sega FILM file format, visit:
|
|
27 * http://www.pcisys.net/~melanson/codecs/
|
|
28 */
|
|
29
|
|
30 #include "avformat.h"
|
|
31
|
|
32 #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
|
|
33 #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
|
|
34 #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
|
|
35 #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
|
|
36
|
|
37 typedef struct {
|
|
38 int stream;
|
|
39 offset_t sample_offset;
|
|
40 unsigned int sample_size;
|
|
41 int64_t pts;
|
|
42 int keyframe;
|
|
43 } film_sample_t;
|
|
44
|
|
45 typedef struct FilmDemuxContext {
|
|
46 int video_stream_index;
|
|
47 int audio_stream_index;
|
|
48
|
|
49 unsigned int audio_type;
|
|
50 unsigned int audio_samplerate;
|
|
51 unsigned int audio_bits;
|
|
52 unsigned int audio_channels;
|
|
53
|
|
54 unsigned int video_type;
|
|
55 unsigned int sample_count;
|
|
56 film_sample_t *sample_table;
|
|
57 unsigned int current_sample;
|
|
58
|
|
59 unsigned int base_clock;
|
|
60 unsigned int version;
|
|
61 int cvid_extra_bytes; /* the number of bytes thrown into the Cinepak
|
|
62 * chunk header to throw off decoders */
|
|
63
|
|
64 /* buffer used for interleaving stereo PCM data */
|
|
65 unsigned char *stereo_buffer;
|
|
66 int stereo_buffer_size;
|
|
67 } FilmDemuxContext;
|
|
68
|
|
69 static int film_probe(AVProbeData *p)
|
|
70 {
|
|
71 if (p->buf_size < 4)
|
|
72 return 0;
|
|
73
|
|
74 if (BE_32(&p->buf[0]) != FILM_TAG)
|
|
75 return 0;
|
|
76
|
|
77 return AVPROBE_SCORE_MAX;
|
|
78 }
|
|
79
|
|
80 static int film_read_header(AVFormatContext *s,
|
|
81 AVFormatParameters *ap)
|
|
82 {
|
|
83 FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
|
|
84 ByteIOContext *pb = &s->pb;
|
|
85 AVStream *st;
|
|
86 unsigned char scratch[256];
|
|
87 int i;
|
|
88 unsigned int data_offset;
|
|
89 unsigned int audio_frame_counter;
|
|
90
|
|
91 film->sample_table = NULL;
|
|
92 film->stereo_buffer = NULL;
|
|
93 film->stereo_buffer_size = 0;
|
|
94
|
|
95 /* load the main FILM header */
|
|
96 if (get_buffer(pb, scratch, 16) != 16)
|
|
97 return AVERROR_IO;
|
|
98 data_offset = BE_32(&scratch[4]);
|
|
99 film->version = BE_32(&scratch[8]);
|
|
100
|
|
101 /* load the FDSC chunk */
|
|
102 if (film->version == 0) {
|
|
103 /* special case for Lemmings .film files; 20-byte header */
|
|
104 if (get_buffer(pb, scratch, 20) != 20)
|
|
105 return AVERROR_IO;
|
|
106 /* make some assumptions about the audio parameters */
|
|
107 film->audio_type = CODEC_ID_PCM_S8;
|
|
108 film->audio_samplerate = 22050;
|
|
109 film->audio_channels = 1;
|
|
110 film->audio_bits = 8;
|
|
111 } else {
|
|
112 /* normal Saturn .cpk files; 32-byte header */
|
|
113 if (get_buffer(pb, scratch, 32) != 32)
|
|
114 return AVERROR_IO;
|
|
115 film->audio_samplerate = BE_16(&scratch[24]);;
|
|
116 film->audio_channels = scratch[21];
|
|
117 film->audio_bits = scratch[22];
|
|
118 if (film->audio_bits == 8)
|
|
119 film->audio_type = CODEC_ID_PCM_S8;
|
|
120 else if (film->audio_bits == 16)
|
|
121 film->audio_type = CODEC_ID_PCM_S16BE;
|
|
122 else
|
|
123 film->audio_type = 0;
|
|
124 }
|
|
125
|
|
126 if (BE_32(&scratch[0]) != FDSC_TAG)
|
|
127 return AVERROR_INVALIDDATA;
|
|
128
|
|
129 film->cvid_extra_bytes = 0;
|
|
130 if (BE_32(&scratch[8]) == CVID_TAG) {
|
|
131 film->video_type = CODEC_ID_CINEPAK;
|
|
132 if (film->version)
|
|
133 film->cvid_extra_bytes = 2;
|
|
134 else
|
|
135 film->cvid_extra_bytes = 6; /* Lemmings 3DO case */
|
|
136 } else
|
|
137 film->video_type = 0;
|
|
138
|
|
139 /* initialize the decoder streams */
|
|
140 if (film->video_type) {
|
|
141 st = av_new_stream(s, 0);
|
|
142 if (!st)
|
|
143 return AVERROR_NOMEM;
|
|
144 film->video_stream_index = st->index;
|
|
145 st->codec->codec_type = CODEC_TYPE_VIDEO;
|
|
146 st->codec->codec_id = film->video_type;
|
|
147 st->codec->codec_tag = 0; /* no fourcc */
|
|
148 st->codec->width = BE_32(&scratch[16]);
|
|
149 st->codec->height = BE_32(&scratch[12]);
|
|
150 }
|
|
151
|
|
152 if (film->audio_type) {
|
|
153 st = av_new_stream(s, 0);
|
|
154 if (!st)
|
|
155 return AVERROR_NOMEM;
|
|
156 film->audio_stream_index = st->index;
|
|
157 st->codec->codec_type = CODEC_TYPE_AUDIO;
|
|
158 st->codec->codec_id = film->audio_type;
|
|
159 st->codec->codec_tag = 1;
|
|
160 st->codec->channels = film->audio_channels;
|
|
161 st->codec->bits_per_sample = film->audio_bits;
|
|
162 st->codec->sample_rate = film->audio_samplerate;
|
|
163 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
|
|
164 st->codec->bits_per_sample;
|
|
165 st->codec->block_align = st->codec->channels *
|
|
166 st->codec->bits_per_sample / 8;
|
|
167 }
|
|
168
|
|
169 /* load the sample table */
|
|
170 if (get_buffer(pb, scratch, 16) != 16)
|
|
171 return AVERROR_IO;
|
|
172 if (BE_32(&scratch[0]) != STAB_TAG)
|
|
173 return AVERROR_INVALIDDATA;
|
|
174 film->base_clock = BE_32(&scratch[8]);
|
|
175 film->sample_count = BE_32(&scratch[12]);
|
|
176 if(film->sample_count >= UINT_MAX / sizeof(film_sample_t))
|
|
177 return -1;
|
|
178 film->sample_table = av_malloc(film->sample_count * sizeof(film_sample_t));
|
|
179
|
|
180 for(i=0; i<s->nb_streams; i++)
|
|
181 av_set_pts_info(s->streams[i], 33, 1, film->base_clock);
|
|
182
|
|
183 audio_frame_counter = 0;
|
|
184 for (i = 0; i < film->sample_count; i++) {
|
|
185 /* load the next sample record and transfer it to an internal struct */
|
|
186 if (get_buffer(pb, scratch, 16) != 16) {
|
|
187 av_free(film->sample_table);
|
|
188 return AVERROR_IO;
|
|
189 }
|
|
190 film->sample_table[i].sample_offset =
|
|
191 data_offset + BE_32(&scratch[0]);
|
|
192 film->sample_table[i].sample_size = BE_32(&scratch[4]);
|
|
193 if (BE_32(&scratch[8]) == 0xFFFFFFFF) {
|
|
194 film->sample_table[i].stream = film->audio_stream_index;
|
|
195 film->sample_table[i].pts = audio_frame_counter;
|
|
196 film->sample_table[i].pts *= film->base_clock;
|
|
197 film->sample_table[i].pts /= film->audio_samplerate;
|
|
198
|
|
199 audio_frame_counter += (film->sample_table[i].sample_size /
|
|
200 (film->audio_channels * film->audio_bits / 8));
|
|
201 } else {
|
|
202 film->sample_table[i].stream = film->video_stream_index;
|
|
203 film->sample_table[i].pts = BE_32(&scratch[8]) & 0x7FFFFFFF;
|
|
204 film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
|
|
205 }
|
|
206 }
|
|
207
|
|
208 film->current_sample = 0;
|
|
209
|
|
210 return 0;
|
|
211 }
|
|
212
|
|
213 static int film_read_packet(AVFormatContext *s,
|
|
214 AVPacket *pkt)
|
|
215 {
|
|
216 FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
|
|
217 ByteIOContext *pb = &s->pb;
|
|
218 film_sample_t *sample;
|
|
219 int ret = 0;
|
|
220 int i;
|
|
221 int left, right;
|
|
222
|
|
223 if (film->current_sample >= film->sample_count)
|
|
224 return AVERROR_IO;
|
|
225
|
|
226 sample = &film->sample_table[film->current_sample];
|
|
227
|
|
228 /* position the stream (will probably be there anyway) */
|
|
229 url_fseek(pb, sample->sample_offset, SEEK_SET);
|
|
230
|
|
231 /* do a special song and dance when loading FILM Cinepak chunks */
|
|
232 if ((sample->stream == film->video_stream_index) &&
|
|
233 (film->video_type == CODEC_ID_CINEPAK)) {
|
|
234 if (av_new_packet(pkt, sample->sample_size - film->cvid_extra_bytes))
|
|
235 return AVERROR_NOMEM;
|
|
236 if(pkt->size < 10)
|
|
237 return -1;
|
|
238 pkt->pos= url_ftell(pb);
|
|
239 ret = get_buffer(pb, pkt->data, 10);
|
|
240 /* skip the non-spec CVID bytes */
|
|
241 url_fseek(pb, film->cvid_extra_bytes, SEEK_CUR);
|
|
242 ret += get_buffer(pb, pkt->data + 10,
|
|
243 sample->sample_size - 10 - film->cvid_extra_bytes);
|
|
244 if (ret != sample->sample_size - film->cvid_extra_bytes)
|
|
245 ret = AVERROR_IO;
|
|
246 } else if ((sample->stream == film->audio_stream_index) &&
|
|
247 (film->audio_channels == 2)) {
|
|
248 /* stereo PCM needs to be interleaved */
|
|
249
|
|
250 if (av_new_packet(pkt, sample->sample_size))
|
|
251 return AVERROR_NOMEM;
|
|
252
|
|
253 /* make sure the interleave buffer is large enough */
|
|
254 if (sample->sample_size > film->stereo_buffer_size) {
|
|
255 av_free(film->stereo_buffer);
|
|
256 film->stereo_buffer_size = sample->sample_size;
|
|
257 film->stereo_buffer = av_malloc(film->stereo_buffer_size);
|
|
258 }
|
|
259
|
|
260 pkt->pos= url_ftell(pb);
|
|
261 ret = get_buffer(pb, film->stereo_buffer, sample->sample_size);
|
|
262 if (ret != sample->sample_size)
|
|
263 ret = AVERROR_IO;
|
|
264
|
|
265 left = 0;
|
|
266 right = sample->sample_size / 2;
|
|
267 for (i = 0; i < sample->sample_size; ) {
|
|
268 if (film->audio_bits == 8) {
|
|
269 pkt->data[i++] = film->stereo_buffer[left++];
|
|
270 pkt->data[i++] = film->stereo_buffer[right++];
|
|
271 } else {
|
|
272 pkt->data[i++] = film->stereo_buffer[left++];
|
|
273 pkt->data[i++] = film->stereo_buffer[left++];
|
|
274 pkt->data[i++] = film->stereo_buffer[right++];
|
|
275 pkt->data[i++] = film->stereo_buffer[right++];
|
|
276 }
|
|
277 }
|
|
278 } else {
|
|
279 ret= av_get_packet(pb, pkt, sample->sample_size);
|
|
280 if (ret != sample->sample_size)
|
|
281 ret = AVERROR_IO;
|
|
282 }
|
|
283
|
|
284 pkt->stream_index = sample->stream;
|
|
285 pkt->pts = sample->pts;
|
|
286
|
|
287 film->current_sample++;
|
|
288
|
|
289 return ret;
|
|
290 }
|
|
291
|
|
292 static int film_read_close(AVFormatContext *s)
|
|
293 {
|
|
294 FilmDemuxContext *film = (FilmDemuxContext *)s->priv_data;
|
|
295
|
|
296 av_free(film->sample_table);
|
|
297 av_free(film->stereo_buffer);
|
|
298
|
|
299 return 0;
|
|
300 }
|
|
301
|
|
302 AVInputFormat segafilm_demuxer = {
|
|
303 "film_cpk",
|
|
304 "Sega FILM/CPK format",
|
|
305 sizeof(FilmDemuxContext),
|
|
306 film_probe,
|
|
307 film_read_header,
|
|
308 film_read_packet,
|
|
309 film_read_close,
|
|
310 };
|