FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
segafilm.c
Go to the documentation of this file.
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
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 "libavutil/intreadwrite.h"
31 #include "avformat.h"
32 #include "internal.h"
33 #include "avio_internal.h"
34 
35 #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
36 #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
37 #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
38 #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
39 #define RAW_TAG MKBETAG('r', 'a', 'w', ' ')
40 
41 typedef struct {
42  int stream;
43  int64_t sample_offset;
44  unsigned int sample_size;
45  int64_t pts;
46  int keyframe;
47 } film_sample;
48 
49 typedef struct FilmDemuxContext {
52 
54  unsigned int audio_samplerate;
55  unsigned int audio_bits;
56  unsigned int audio_channels;
57 
59  unsigned int sample_count;
61  unsigned int current_sample;
62 
63  unsigned int base_clock;
64  unsigned int version;
65 
66  /* buffer used for interleaving stereo PCM data */
67  unsigned char *stereo_buffer;
70 
71 static int film_probe(AVProbeData *p)
72 {
73  if (AV_RB32(&p->buf[0]) != FILM_TAG)
74  return 0;
75 
76  return AVPROBE_SCORE_MAX;
77 }
78 
80 {
81  FilmDemuxContext *film = s->priv_data;
82  AVIOContext *pb = s->pb;
83  AVStream *st;
84  unsigned char scratch[256];
85  int i;
86  unsigned int data_offset;
87  unsigned int audio_frame_counter;
88 
89  film->sample_table = NULL;
90  film->stereo_buffer = NULL;
91  film->stereo_buffer_size = 0;
92 
93  /* load the main FILM header */
94  if (avio_read(pb, scratch, 16) != 16)
95  return AVERROR(EIO);
96  data_offset = AV_RB32(&scratch[4]);
97  film->version = AV_RB32(&scratch[8]);
98 
99  /* load the FDSC chunk */
100  if (film->version == 0) {
101  /* special case for Lemmings .film files; 20-byte header */
102  if (avio_read(pb, scratch, 20) != 20)
103  return AVERROR(EIO);
104  /* make some assumptions about the audio parameters */
106  film->audio_samplerate = 22050;
107  film->audio_channels = 1;
108  film->audio_bits = 8;
109  } else {
110  /* normal Saturn .cpk files; 32-byte header */
111  if (avio_read(pb, scratch, 32) != 32)
112  return AVERROR(EIO);
113  film->audio_samplerate = AV_RB16(&scratch[24]);
114  film->audio_channels = scratch[21];
115  film->audio_bits = scratch[22];
116  if (scratch[23] == 2 && film->audio_channels > 0)
118  else if (film->audio_channels > 0) {
119  if (film->audio_bits == 8)
121  else if (film->audio_bits == 16)
123  else
125  } else
127  }
128 
129  if (AV_RB32(&scratch[0]) != FDSC_TAG)
130  return AVERROR_INVALIDDATA;
131 
132  if (AV_RB32(&scratch[8]) == CVID_TAG) {
134  } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
136  } else {
138  }
139 
140  /* initialize the decoder streams */
141  if (film->video_type) {
142  st = avformat_new_stream(s, NULL);
143  if (!st)
144  return AVERROR(ENOMEM);
145  film->video_stream_index = st->index;
147  st->codec->codec_id = film->video_type;
148  st->codec->codec_tag = 0; /* no fourcc */
149  st->codec->width = AV_RB32(&scratch[16]);
150  st->codec->height = AV_RB32(&scratch[12]);
151 
152  if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
153  if (scratch[20] == 24) {
155  } else {
156  av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
157  return -1;
158  }
159  }
160  }
161 
162  if (film->audio_type) {
163  st = avformat_new_stream(s, NULL);
164  if (!st)
165  return AVERROR(ENOMEM);
166  film->audio_stream_index = st->index;
168  st->codec->codec_id = film->audio_type;
169  st->codec->codec_tag = 1;
170  st->codec->channels = film->audio_channels;
171  st->codec->sample_rate = film->audio_samplerate;
172 
173  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
174  st->codec->bits_per_coded_sample = 18 * 8 / 32;
175  st->codec->block_align = st->codec->channels * 18;
177  } else {
179  st->codec->block_align = st->codec->channels *
180  st->codec->bits_per_coded_sample / 8;
181  }
182 
183  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
185  }
186 
187  /* load the sample table */
188  if (avio_read(pb, scratch, 16) != 16)
189  return AVERROR(EIO);
190  if (AV_RB32(&scratch[0]) != STAB_TAG)
191  return AVERROR_INVALIDDATA;
192  film->base_clock = AV_RB32(&scratch[8]);
193  film->sample_count = AV_RB32(&scratch[12]);
194  if(film->sample_count >= UINT_MAX / sizeof(film_sample))
195  return -1;
196  film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
197  if (!film->sample_table)
198  return AVERROR(ENOMEM);
199 
200  for (i = 0; i < s->nb_streams; i++) {
201  st = s->streams[i];
202  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
203  avpriv_set_pts_info(st, 33, 1, film->base_clock);
204  else
205  avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
206  }
207 
208  audio_frame_counter = 0;
209  for (i = 0; i < film->sample_count; i++) {
210  /* load the next sample record and transfer it to an internal struct */
211  if (avio_read(pb, scratch, 16) != 16) {
212  av_free(film->sample_table);
213  return AVERROR(EIO);
214  }
215  film->sample_table[i].sample_offset =
216  data_offset + AV_RB32(&scratch[0]);
217  film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
218  if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
219  film->sample_table[i].stream = film->audio_stream_index;
220  film->sample_table[i].pts = audio_frame_counter;
221 
222  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
223  audio_frame_counter += (film->sample_table[i].sample_size * 32 /
224  (18 * film->audio_channels));
225  else if (film->audio_type != AV_CODEC_ID_NONE)
226  audio_frame_counter += (film->sample_table[i].sample_size /
227  (film->audio_channels * film->audio_bits / 8));
228  } else {
229  film->sample_table[i].stream = film->video_stream_index;
230  film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
231  film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
232  }
233  }
234 
235  film->current_sample = 0;
236 
237  return 0;
238 }
239 
241  AVPacket *pkt)
242 {
243  FilmDemuxContext *film = s->priv_data;
244  AVIOContext *pb = s->pb;
246  int ret = 0;
247  int i;
248  int left, right;
249 
250  if (film->current_sample >= film->sample_count)
251  return AVERROR_EOF;
252 
253  sample = &film->sample_table[film->current_sample];
254 
255  /* position the stream (will probably be there anyway) */
256  avio_seek(pb, sample->sample_offset, SEEK_SET);
257 
258  /* do a special song and dance when loading FILM Cinepak chunks */
259  if ((sample->stream == film->video_stream_index) &&
260  (film->video_type == AV_CODEC_ID_CINEPAK)) {
261  pkt->pos= avio_tell(pb);
262  if (av_new_packet(pkt, sample->sample_size))
263  return AVERROR(ENOMEM);
264  avio_read(pb, pkt->data, sample->sample_size);
265  } else if ((sample->stream == film->audio_stream_index) &&
266  (film->audio_channels == 2) &&
267  (film->audio_type != AV_CODEC_ID_ADPCM_ADX)) {
268  /* stereo PCM needs to be interleaved */
269 
270  if (ffio_limit(pb, sample->sample_size) != sample->sample_size)
271  return AVERROR(EIO);
272  if (av_new_packet(pkt, sample->sample_size))
273  return AVERROR(ENOMEM);
274 
275  /* make sure the interleave buffer is large enough */
276  if (sample->sample_size > film->stereo_buffer_size) {
277  av_free(film->stereo_buffer);
278  film->stereo_buffer_size = sample->sample_size;
280  if (!film->stereo_buffer) {
281  film->stereo_buffer_size = 0;
282  return AVERROR(ENOMEM);
283  }
284  }
285 
286  pkt->pos= avio_tell(pb);
287  ret = avio_read(pb, film->stereo_buffer, sample->sample_size);
288  if (ret != sample->sample_size)
289  ret = AVERROR(EIO);
290 
291  left = 0;
292  right = sample->sample_size / 2;
293  for (i = 0; i + 1 + 2*(film->audio_bits != 8) < sample->sample_size; ) {
294  if (film->audio_bits == 8) {
295  pkt->data[i++] = film->stereo_buffer[left++];
296  pkt->data[i++] = film->stereo_buffer[right++];
297  } else {
298  pkt->data[i++] = film->stereo_buffer[left++];
299  pkt->data[i++] = film->stereo_buffer[left++];
300  pkt->data[i++] = film->stereo_buffer[right++];
301  pkt->data[i++] = film->stereo_buffer[right++];
302  }
303  }
304  } else {
305  ret= av_get_packet(pb, pkt, sample->sample_size);
306  if (ret != sample->sample_size)
307  ret = AVERROR(EIO);
308  }
309 
310  pkt->stream_index = sample->stream;
311  pkt->pts = sample->pts;
312 
313  film->current_sample++;
314 
315  return ret;
316 }
317 
319 {
320  FilmDemuxContext *film = s->priv_data;
321 
322  av_free(film->sample_table);
323  av_free(film->stereo_buffer);
324 
325  return 0;
326 }
327 
329  .name = "film_cpk",
330  .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
331  .priv_data_size = sizeof(FilmDemuxContext),
336 };