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 };
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:70
int64_t pos
byte position in stream, -1 if unknown
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
#define FILM_TAG
Definition: segafilm.c:35
int index
stream index in AVFormatContext
Definition: avformat.h:644
unsigned int sample_count
Definition: segafilm.c:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:199
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
static int film_read_close(AVFormatContext *s)
Definition: segafilm.c:318
#define sample
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Format I/O context.
Definition: avformat.h:944
int stereo_buffer_size
Definition: segafilm.c:68
int ffio_limit(AVIOContext *s, int size)
unsigned int audio_channels
Definition: segafilm.c:56
unsigned int base_clock
Definition: segafilm.c:63
#define AV_RB32
static AVPacket pkt
Definition: demuxing.c:56
enum AVStreamParseType need_parsing
Definition: avformat.h:811
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
#define STAB_TAG
Definition: segafilm.c:37
int stream
Definition: segafilm.c:42
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
unsigned int sample_size
Definition: segafilm.c:44
unsigned int audio_samplerate
Definition: segafilm.c:54
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int video_stream_index
Definition: segafilm.c:50
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
AVCodecID
Identify the syntax and semantics of the bitstream.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
struct FilmDemuxContext FilmDemuxContext
#define AV_RB16
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
unsigned int current_sample
Definition: segafilm.c:61
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
unsigned int version
Definition: segafilm.c:64
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int keyframe
Definition: segafilm.c:46
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
int bit_rate
the average bitrate
static int read_probe(AVProbeData *pd)
ret
Definition: avfilter.c:821
int width
picture width / height.
film_sample * sample_table
Definition: segafilm.c:60
unsigned int audio_bits
Definition: segafilm.c:55
#define RAW_TAG
Definition: segafilm.c:39
#define CVID_TAG
Definition: segafilm.c:38
int64_t pts
Definition: segafilm.c:45
enum AVCodecID video_type
Definition: segafilm.c:58
static int film_read_header(AVFormatContext *s)
Definition: segafilm.c:79
int64_t sample_offset
Definition: segafilm.c:43
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:517
Stream structure.
Definition: avformat.h:643
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
unsigned char * stereo_buffer
Definition: segafilm.c:67
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
synthesis window for stochastic i
static int film_probe(AVProbeData *p)
Definition: segafilm.c:71
#define FDSC_TAG
Definition: segafilm.c:36
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVInputFormat ff_segafilm_demuxer
Definition: segafilm.c:328
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
full parsing and repack
Definition: avformat.h:582
Main libavformat public API header.
int audio_stream_index
Definition: segafilm.c:51
int channels
number of audio channels
static int film_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: segafilm.c:240
void * priv_data
Format private data.
Definition: avformat.h:964
enum AVCodecID audio_type
Definition: segafilm.c:53
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...