Libav
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 
89 {
90  FilmDemuxContext *film = s->priv_data;
91  AVIOContext *pb = s->pb;
92  AVStream *st;
93  unsigned char scratch[256];
94  int i, ret;
95  unsigned int data_offset;
96  unsigned int audio_frame_counter;
97 
98  film->sample_table = NULL;
99  film->stereo_buffer = NULL;
100  film->stereo_buffer_size = 0;
101 
102  /* load the main FILM header */
103  if (avio_read(pb, scratch, 16) != 16)
104  return AVERROR(EIO);
105  data_offset = AV_RB32(&scratch[4]);
106  film->version = AV_RB32(&scratch[8]);
107 
108  /* load the FDSC chunk */
109  if (film->version == 0) {
110  /* special case for Lemmings .film files; 20-byte header */
111  if (avio_read(pb, scratch, 20) != 20)
112  return AVERROR(EIO);
113  /* make some assumptions about the audio parameters */
115  film->audio_samplerate = 22050;
116  film->audio_channels = 1;
117  film->audio_bits = 8;
118  } else {
119  /* normal Saturn .cpk files; 32-byte header */
120  if (avio_read(pb, scratch, 32) != 32)
121  return AVERROR(EIO);
122  film->audio_samplerate = AV_RB16(&scratch[24]);
123  film->audio_channels = scratch[21];
124  if (!film->audio_channels || film->audio_channels > 2) {
125  av_log(s, AV_LOG_ERROR,
126  "Invalid number of channels: %d\n", film->audio_channels);
127  return AVERROR_INVALIDDATA;
128  }
129  film->audio_bits = scratch[22];
130  if (scratch[23] == 2)
132  else if (film->audio_channels > 0) {
133  if (film->audio_bits == 8)
135  else if (film->audio_bits == 16)
137  else
139  } else
141  }
142 
143  if (AV_RB32(&scratch[0]) != FDSC_TAG)
144  return AVERROR_INVALIDDATA;
145 
146  if (AV_RB32(&scratch[8]) == CVID_TAG) {
148  } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
150  } else {
152  }
153 
154  /* initialize the decoder streams */
155  if (film->video_type) {
156  st = avformat_new_stream(s, NULL);
157  if (!st)
158  return AVERROR(ENOMEM);
159  film->video_stream_index = st->index;
161  st->codec->codec_id = film->video_type;
162  st->codec->codec_tag = 0; /* no fourcc */
163  st->codec->width = AV_RB32(&scratch[16]);
164  st->codec->height = AV_RB32(&scratch[12]);
165 
166  if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
167  if (scratch[20] == 24) {
169  } else {
170  av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
171  return -1;
172  }
173  }
174  }
175 
176  if (film->audio_type) {
177  st = avformat_new_stream(s, NULL);
178  if (!st)
179  return AVERROR(ENOMEM);
180  film->audio_stream_index = st->index;
182  st->codec->codec_id = film->audio_type;
183  st->codec->codec_tag = 1;
184  st->codec->channels = film->audio_channels;
185  st->codec->sample_rate = film->audio_samplerate;
186 
187  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
188  st->codec->bits_per_coded_sample = 18 * 8 / 32;
189  st->codec->block_align = st->codec->channels * 18;
191  } else {
193  st->codec->block_align = st->codec->channels *
194  st->codec->bits_per_coded_sample / 8;
195  }
196 
197  st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
199  }
200 
201  /* load the sample table */
202  if (avio_read(pb, scratch, 16) != 16)
203  return AVERROR(EIO);
204  if (AV_RB32(&scratch[0]) != STAB_TAG)
205  return AVERROR_INVALIDDATA;
206  film->base_clock = AV_RB32(&scratch[8]);
207  film->sample_count = AV_RB32(&scratch[12]);
208  if(film->sample_count >= UINT_MAX / sizeof(film_sample))
209  return -1;
210  film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
211  if (!film->sample_table)
212  return AVERROR(ENOMEM);
213 
214  for (i = 0; i < s->nb_streams; i++) {
215  st = s->streams[i];
216  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
217  avpriv_set_pts_info(st, 33, 1, film->base_clock);
218  else
219  avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
220  }
221 
222  audio_frame_counter = 0;
223  for (i = 0; i < film->sample_count; i++) {
224  /* load the next sample record and transfer it to an internal struct */
225  if (avio_read(pb, scratch, 16) != 16) {
226  ret = AVERROR(EIO);
227  goto fail;
228  }
229  film->sample_table[i].sample_offset =
230  data_offset + AV_RB32(&scratch[0]);
231  film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
232  if (film->sample_table[i].sample_size > INT_MAX / 4) {
233  ret = AVERROR_INVALIDDATA;
234  goto fail;
235  }
236  if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
237  film->sample_table[i].stream = film->audio_stream_index;
238  film->sample_table[i].pts = audio_frame_counter;
239 
240  if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
241  audio_frame_counter += (film->sample_table[i].sample_size * 32 /
242  (18 * film->audio_channels));
243  else if (film->audio_type != AV_CODEC_ID_NONE)
244  audio_frame_counter += (film->sample_table[i].sample_size /
245  (film->audio_channels * film->audio_bits / 8));
246  } else {
247  film->sample_table[i].stream = film->video_stream_index;
248  film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
249  film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
250  }
251  }
252 
253  film->current_sample = 0;
254 
255  return 0;
256 fail:
257  film_read_close(s);
258  return ret;
259 }
260 
262  AVPacket *pkt)
263 {
264  FilmDemuxContext *film = s->priv_data;
265  AVIOContext *pb = s->pb;
267  int ret = 0;
268  int i;
269  int left, right;
270 
271  if (film->current_sample >= film->sample_count)
272  return AVERROR(EIO);
273 
274  sample = &film->sample_table[film->current_sample];
275 
276  /* position the stream (will probably be there anyway) */
277  avio_seek(pb, sample->sample_offset, SEEK_SET);
278 
279  /* do a special song and dance when loading FILM Cinepak chunks */
280  if ((sample->stream == film->video_stream_index) &&
281  (film->video_type == AV_CODEC_ID_CINEPAK)) {
282  pkt->pos= avio_tell(pb);
283  if (av_new_packet(pkt, sample->sample_size))
284  return AVERROR(ENOMEM);
285  avio_read(pb, pkt->data, sample->sample_size);
286  } else if ((sample->stream == film->audio_stream_index) &&
287  (film->audio_channels == 2) &&
288  (film->audio_type != AV_CODEC_ID_ADPCM_ADX)) {
289  /* stereo PCM needs to be interleaved */
290 
291  if (av_new_packet(pkt, sample->sample_size))
292  return AVERROR(ENOMEM);
293 
294  /* make sure the interleave buffer is large enough */
295  if (sample->sample_size > film->stereo_buffer_size) {
296  av_free(film->stereo_buffer);
297  film->stereo_buffer_size = sample->sample_size;
299  if (!film->stereo_buffer) {
300  film->stereo_buffer_size = 0;
301  return AVERROR(ENOMEM);
302  }
303  }
304 
305  pkt->pos= avio_tell(pb);
306  ret = avio_read(pb, film->stereo_buffer, sample->sample_size);
307  if (ret != sample->sample_size)
308  ret = AVERROR(EIO);
309 
310  left = 0;
311  right = sample->sample_size / 2;
312  for (i = 0; i < sample->sample_size; ) {
313  if (film->audio_bits == 8) {
314  pkt->data[i++] = film->stereo_buffer[left++];
315  pkt->data[i++] = film->stereo_buffer[right++];
316  } else {
317  pkt->data[i++] = film->stereo_buffer[left++];
318  pkt->data[i++] = film->stereo_buffer[left++];
319  pkt->data[i++] = film->stereo_buffer[right++];
320  pkt->data[i++] = film->stereo_buffer[right++];
321  }
322  }
323  } else {
324  ret= av_get_packet(pb, pkt, sample->sample_size);
325  if (ret != sample->sample_size)
326  ret = AVERROR(EIO);
327  }
328 
329  pkt->stream_index = sample->stream;
330  pkt->pts = sample->pts;
331 
332  film->current_sample++;
333 
334  return ret;
335 }
336 
338  .name = "film_cpk",
339  .long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
340  .priv_data_size = sizeof(FilmDemuxContext),
345 };
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:62
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
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.
Definition: utils.c:2829
#define FILM_TAG
Definition: segafilm.c:34
int index
stream index in AVFormatContext
Definition: avformat.h:700
unsigned int sample_count
Definition: segafilm.c:58
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
static int film_read_close(AVFormatContext *s)
Definition: segafilm.c:78
#define sample
int block_align
number of bytes per packet if constant and known or 0 Used by some WAV based audio codecs...
Definition: avcodec.h:1844
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
Format I/O context.
Definition: avformat.h:922
int stereo_buffer_size
Definition: segafilm.c:67
unsigned int audio_channels
Definition: segafilm.c:55
unsigned int base_clock
Definition: segafilm.c:62
#define AV_RB32
Definition: intreadwrite.h:130
enum AVStreamParseType need_parsing
Definition: avformat.h:867
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
uint8_t * data
Definition: avcodec.h:973
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.
Definition: utils.c:117
#define STAB_TAG
Definition: segafilm.c:36
int stream
Definition: segafilm.c:41
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2523
unsigned int sample_size
Definition: segafilm.c:43
unsigned int audio_samplerate
Definition: segafilm.c:53
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int video_stream_index
Definition: segafilm.c:49
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:105
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AV_RB16
Definition: intreadwrite.h:53
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
unsigned int current_sample
Definition: segafilm.c:60
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
unsigned int version
Definition: segafilm.c:63
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
int keyframe
Definition: segafilm.c:45
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:978
int bit_rate
the average bitrate
Definition: avcodec.h:1114
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
int width
picture width / height.
Definition: avcodec.h:1229
film_sample * sample_table
Definition: segafilm.c:59
unsigned int audio_bits
Definition: segafilm.c:54
#define RAW_TAG
Definition: segafilm.c:38
#define CVID_TAG
Definition: segafilm.c:37
int64_t pts
Definition: segafilm.c:44
enum AVCodecID video_type
Definition: segafilm.c:57
static int film_read_header(AVFormatContext *s)
Definition: segafilm.c:88
int64_t sample_offset
Definition: segafilm.c:42
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1807
AVIOContext * pb
I/O context.
Definition: avformat.h:964
unsigned char * stereo_buffer
Definition: segafilm.c:66
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> (&#39;D&#39;<<24) + (&#39;C&#39;<<16) + (&#39;B&#39;<<8) + &#39;A&#39;).
Definition: avcodec.h:1082
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
static int film_probe(AVProbeData *p)
Definition: segafilm.c:70
#define FDSC_TAG
Definition: segafilm.c:35
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
AVInputFormat ff_segafilm_demuxer
Definition: segafilm.c:337
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
full parsing and repack
Definition: avformat.h:653
Main libavformat public API header.
int audio_stream_index
Definition: segafilm.c:50
int channels
number of audio channels
Definition: avcodec.h:1808
static int film_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: segafilm.c:261
void * priv_data
Format private data.
Definition: avformat.h:950
enum AVCodecID audio_type
Definition: segafilm.c:52
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966