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