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;
66 
67 static int film_probe(AVProbeData *p)
68 {
69  if (AV_RB32(&p->buf[0]) != FILM_TAG)
70  return 0;
71 
72  return AVPROBE_SCORE_MAX;
73 }
74 
76 {
77  FilmDemuxContext *film = s->priv_data;
78 
79  av_freep(&film->sample_table);
80 
81  return 0;
82 }
83 
85 {
86  FilmDemuxContext *film = s->priv_data;
87  AVIOContext *pb = s->pb;
88  AVStream *st;
89  unsigned char scratch[256];
90  int i, ret;
91  unsigned int data_offset;
92  unsigned int audio_frame_counter;
93 
94  film->sample_table = NULL;
95 
96  /* load the main FILM header */
97  if (avio_read(pb, scratch, 16) != 16)
98  return AVERROR(EIO);
99  data_offset = AV_RB32(&scratch[4]);
100  film->version = AV_RB32(&scratch[8]);
101 
102  /* load the FDSC chunk */
103  if (film->version == 0) {
104  /* special case for Lemmings .film files; 20-byte header */
105  if (avio_read(pb, scratch, 20) != 20)
106  return AVERROR(EIO);
107  /* make some assumptions about the audio parameters */
109  film->audio_samplerate = 22050;
110  film->audio_channels = 1;
111  film->audio_bits = 8;
112  } else {
113  /* normal Saturn .cpk files; 32-byte header */
114  if (avio_read(pb, scratch, 32) != 32)
115  return AVERROR(EIO);
116  film->audio_samplerate = AV_RB16(&scratch[24]);
117  film->audio_channels = scratch[21];
118  film->audio_bits = scratch[22];
119  if (scratch[23] == 2 && film->audio_channels > 0)
121  else if (film->audio_channels > 0) {
122  if (film->audio_bits == 8)
124  else if (film->audio_bits == 16)
126  else
128  } else
130  }
131 
132  if (AV_RB32(&scratch[0]) != FDSC_TAG)
133  return AVERROR_INVALIDDATA;
134 
135  if (AV_RB32(&scratch[8]) == CVID_TAG) {
137  } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
139  } else {
141  }
142 
143  /* initialize the decoder streams */
144  if (film->video_type) {
145  st = avformat_new_stream(s, NULL);
146  if (!st)
147  return AVERROR(ENOMEM);
148  film->video_stream_index = st->index;
150  st->codec->codec_id = film->video_type;
151  st->codec->codec_tag = 0; /* no fourcc */
152  st->codec->width = AV_RB32(&scratch[16]);
153  st->codec->height = AV_RB32(&scratch[12]);
154 
155  if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
156  if (scratch[20] == 24) {
158  } else {
159  av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
160  return -1;
161  }
162  }
163  }
164 
165  if (film->audio_type) {
166  st = avformat_new_stream(s, NULL);
167  if (!st)
168  return AVERROR(ENOMEM);
169  film->audio_stream_index = st->index;
171  st->codec->codec_id = film->audio_type;
172  st->codec->codec_tag = 1;
173  st->codec->channels = film->audio_channels;
174  st->codec->sample_rate = film->audio_samplerate;
175 
176  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
177  st->codec->bits_per_coded_sample = 18 * 8 / 32;
178  st->codec->block_align = st->codec->channels * 18;
180  } else {
182  st->codec->block_align = st->codec->channels *
183  st->codec->bits_per_coded_sample / 8;
184  }
185 
186  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
188  }
189 
190  /* load the sample table */
191  if (avio_read(pb, scratch, 16) != 16)
192  return AVERROR(EIO);
193  if (AV_RB32(&scratch[0]) != STAB_TAG)
194  return AVERROR_INVALIDDATA;
195  film->base_clock = AV_RB32(&scratch[8]);
196  film->sample_count = AV_RB32(&scratch[12]);
197  if(film->sample_count >= UINT_MAX / sizeof(film_sample))
198  return -1;
199  film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
200  if (!film->sample_table)
201  return AVERROR(ENOMEM);
202 
203  for (i = 0; i < s->nb_streams; i++) {
204  st = s->streams[i];
205  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
206  avpriv_set_pts_info(st, 33, 1, film->base_clock);
207  else
208  avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
209  }
210 
211  audio_frame_counter = 0;
212  for (i = 0; i < film->sample_count; i++) {
213  /* load the next sample record and transfer it to an internal struct */
214  if (avio_read(pb, scratch, 16) != 16) {
215  ret = AVERROR(EIO);
216  goto fail;
217  }
218  film->sample_table[i].sample_offset =
219  data_offset + AV_RB32(&scratch[0]);
220  film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
221  if (film->sample_table[i].sample_size > INT_MAX / 4) {
222  ret = AVERROR_INVALIDDATA;
223  goto fail;
224  }
225  if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
226  film->sample_table[i].stream = film->audio_stream_index;
227  film->sample_table[i].pts = audio_frame_counter;
228 
229  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
230  audio_frame_counter += (film->sample_table[i].sample_size * 32 /
231  (18 * film->audio_channels));
232  else if (film->audio_type != AV_CODEC_ID_NONE)
233  audio_frame_counter += (film->sample_table[i].sample_size /
234  (film->audio_channels * film->audio_bits / 8));
235  } else {
236  film->sample_table[i].stream = film->video_stream_index;
237  film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
238  film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
239  }
240  }
241 
242  film->current_sample = 0;
243 
244  return 0;
245 fail:
246  film_read_close(s);
247  return ret;
248 }
249 
251  AVPacket *pkt)
252 {
253  FilmDemuxContext *film = s->priv_data;
254  AVIOContext *pb = s->pb;
256  int ret = 0;
257 
258  if (film->current_sample >= film->sample_count)
259  return AVERROR_EOF;
260 
261  sample = &film->sample_table[film->current_sample];
262 
263  /* position the stream (will probably be there anyway) */
264  avio_seek(pb, sample->sample_offset, SEEK_SET);
265 
266  /* do a special song and dance when loading FILM Cinepak chunks */
267  if ((sample->stream == film->video_stream_index) &&
268  (film->video_type == AV_CODEC_ID_CINEPAK)) {
269  pkt->pos= avio_tell(pb);
270  if (av_new_packet(pkt, sample->sample_size))
271  return AVERROR(ENOMEM);
272  avio_read(pb, pkt->data, sample->sample_size);
273  } else {
274  ret= av_get_packet(pb, pkt, sample->sample_size);
275  if (ret != sample->sample_size)
276  ret = AVERROR(EIO);
277  }
278 
279  pkt->stream_index = sample->stream;
280  pkt->pts = sample->pts;
281 
282  film->current_sample++;
283 
284  return ret;
285 }
286 
288  .name = "film_cpk",
289  .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
290  .priv_data_size = sizeof(FilmDemuxContext),
295 };