]> git.sesse.net Git - ffmpeg/blob - libavformat/segafilm.c
mpegvideo: Drop a faulty assert
[ffmpeg] / libavformat / segafilm.c
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
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
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 {
49     int video_stream_index;
50     int audio_stream_index;
51
52     enum AVCodecID audio_type;
53     unsigned int audio_samplerate;
54     unsigned int audio_bits;
55     unsigned int audio_channels;
56
57     enum AVCodecID video_type;
58     unsigned int sample_count;
59     film_sample *sample_table;
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;
67     int stereo_buffer_size;
68 } FilmDemuxContext;
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
78 static int film_read_header(AVFormatContext *s)
79 {
80     FilmDemuxContext *film = s->priv_data;
81     AVIOContext *pb = s->pb;
82     AVStream *st;
83     unsigned char scratch[256];
84     int i;
85     unsigned int data_offset;
86     unsigned int audio_frame_counter;
87
88     film->sample_table = NULL;
89     film->stereo_buffer = NULL;
90     film->stereo_buffer_size = 0;
91
92     /* load the main FILM header */
93     if (avio_read(pb, scratch, 16) != 16)
94         return AVERROR(EIO);
95     data_offset = AV_RB32(&scratch[4]);
96     film->version = AV_RB32(&scratch[8]);
97
98     /* load the FDSC chunk */
99     if (film->version == 0) {
100         /* special case for Lemmings .film files; 20-byte header */
101         if (avio_read(pb, scratch, 20) != 20)
102             return AVERROR(EIO);
103         /* make some assumptions about the audio parameters */
104         film->audio_type = AV_CODEC_ID_PCM_S8;
105         film->audio_samplerate = 22050;
106         film->audio_channels = 1;
107         film->audio_bits = 8;
108     } else {
109         /* normal Saturn .cpk files; 32-byte header */
110         if (avio_read(pb, scratch, 32) != 32)
111             return AVERROR(EIO);
112         film->audio_samplerate = AV_RB16(&scratch[24]);
113         film->audio_channels = scratch[21];
114         if (!film->audio_channels || film->audio_channels > 2) {
115             av_log(s, AV_LOG_ERROR,
116                    "Invalid number of channels: %d\n", film->audio_channels);
117             return AVERROR_INVALIDDATA;
118         }
119         film->audio_bits = scratch[22];
120         if (scratch[23] == 2)
121             film->audio_type = AV_CODEC_ID_ADPCM_ADX;
122         else if (film->audio_channels > 0) {
123             if (film->audio_bits == 8)
124                 film->audio_type = AV_CODEC_ID_PCM_S8;
125             else if (film->audio_bits == 16)
126                 film->audio_type = AV_CODEC_ID_PCM_S16BE;
127             else
128                 film->audio_type = AV_CODEC_ID_NONE;
129         } else
130             film->audio_type = AV_CODEC_ID_NONE;
131     }
132
133     if (AV_RB32(&scratch[0]) != FDSC_TAG)
134         return AVERROR_INVALIDDATA;
135
136     if (AV_RB32(&scratch[8]) == CVID_TAG) {
137         film->video_type = AV_CODEC_ID_CINEPAK;
138     } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
139         film->video_type = AV_CODEC_ID_RAWVIDEO;
140     } else {
141         film->video_type = AV_CODEC_ID_NONE;
142     }
143
144     /* initialize the decoder streams */
145     if (film->video_type) {
146         st = avformat_new_stream(s, NULL);
147         if (!st)
148             return AVERROR(ENOMEM);
149         film->video_stream_index = st->index;
150         st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
151         st->codec->codec_id = film->video_type;
152         st->codec->codec_tag = 0;  /* no fourcc */
153         st->codec->width = AV_RB32(&scratch[16]);
154         st->codec->height = AV_RB32(&scratch[12]);
155
156         if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
157             if (scratch[20] == 24) {
158                 st->codec->pix_fmt = AV_PIX_FMT_RGB24;
159             } else {
160                 av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
161                 return -1;
162             }
163         }
164     }
165
166     if (film->audio_type) {
167         st = avformat_new_stream(s, NULL);
168         if (!st)
169             return AVERROR(ENOMEM);
170         film->audio_stream_index = st->index;
171         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
172         st->codec->codec_id = film->audio_type;
173         st->codec->codec_tag = 1;
174         st->codec->channels = film->audio_channels;
175         st->codec->sample_rate = film->audio_samplerate;
176
177         if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
178             st->codec->bits_per_coded_sample = 18 * 8 / 32;
179             st->codec->block_align = st->codec->channels * 18;
180             st->need_parsing = AVSTREAM_PARSE_FULL;
181         } else {
182             st->codec->bits_per_coded_sample = film->audio_bits;
183             st->codec->block_align = st->codec->channels *
184                 st->codec->bits_per_coded_sample / 8;
185         }
186
187         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
188             st->codec->bits_per_coded_sample;
189     }
190
191     /* load the sample table */
192     if (avio_read(pb, scratch, 16) != 16)
193         return AVERROR(EIO);
194     if (AV_RB32(&scratch[0]) != STAB_TAG)
195         return AVERROR_INVALIDDATA;
196     film->base_clock = AV_RB32(&scratch[8]);
197     film->sample_count = AV_RB32(&scratch[12]);
198     if(film->sample_count >= UINT_MAX / sizeof(film_sample))
199         return -1;
200     film->sample_table = av_malloc(film->sample_count * sizeof(film_sample));
201     if (!film->sample_table)
202         return AVERROR(ENOMEM);
203
204     for (i = 0; i < s->nb_streams; i++) {
205         st = s->streams[i];
206         if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
207             avpriv_set_pts_info(st, 33, 1, film->base_clock);
208         else
209             avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
210     }
211
212     audio_frame_counter = 0;
213     for (i = 0; i < film->sample_count; i++) {
214         /* load the next sample record and transfer it to an internal struct */
215         if (avio_read(pb, scratch, 16) != 16) {
216             av_free(film->sample_table);
217             return AVERROR(EIO);
218         }
219         film->sample_table[i].sample_offset =
220             data_offset + AV_RB32(&scratch[0]);
221         film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
222         if (film->sample_table[i].sample_size > INT_MAX / 4)
223             return AVERROR_INVALIDDATA;
224         if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
225             film->sample_table[i].stream = film->audio_stream_index;
226             film->sample_table[i].pts = audio_frame_counter;
227
228             if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
229                 audio_frame_counter += (film->sample_table[i].sample_size * 32 /
230                     (18 * film->audio_channels));
231             else if (film->audio_type != AV_CODEC_ID_NONE)
232                 audio_frame_counter += (film->sample_table[i].sample_size /
233                     (film->audio_channels * film->audio_bits / 8));
234         } else {
235             film->sample_table[i].stream = film->video_stream_index;
236             film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
237             film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : 1;
238         }
239     }
240
241     film->current_sample = 0;
242
243     return 0;
244 }
245
246 static int film_read_packet(AVFormatContext *s,
247                             AVPacket *pkt)
248 {
249     FilmDemuxContext *film = s->priv_data;
250     AVIOContext *pb = s->pb;
251     film_sample *sample;
252     int ret = 0;
253     int i;
254     int left, right;
255
256     if (film->current_sample >= film->sample_count)
257         return AVERROR(EIO);
258
259     sample = &film->sample_table[film->current_sample];
260
261     /* position the stream (will probably be there anyway) */
262     avio_seek(pb, sample->sample_offset, SEEK_SET);
263
264     /* do a special song and dance when loading FILM Cinepak chunks */
265     if ((sample->stream == film->video_stream_index) &&
266         (film->video_type == AV_CODEC_ID_CINEPAK)) {
267         pkt->pos= avio_tell(pb);
268         if (av_new_packet(pkt, sample->sample_size))
269             return AVERROR(ENOMEM);
270         avio_read(pb, pkt->data, sample->sample_size);
271     } else if ((sample->stream == film->audio_stream_index) &&
272         (film->audio_channels == 2) &&
273         (film->audio_type != AV_CODEC_ID_ADPCM_ADX)) {
274         /* stereo PCM needs to be interleaved */
275
276         if (av_new_packet(pkt, sample->sample_size))
277             return AVERROR(ENOMEM);
278
279         /* make sure the interleave buffer is large enough */
280         if (sample->sample_size > film->stereo_buffer_size) {
281             av_free(film->stereo_buffer);
282             film->stereo_buffer_size = sample->sample_size;
283             film->stereo_buffer = av_malloc(film->stereo_buffer_size);
284             if (!film->stereo_buffer) {
285                 film->stereo_buffer_size = 0;
286                 return AVERROR(ENOMEM);
287             }
288         }
289
290         pkt->pos= avio_tell(pb);
291         ret = avio_read(pb, film->stereo_buffer, sample->sample_size);
292         if (ret != sample->sample_size)
293             ret = AVERROR(EIO);
294
295         left = 0;
296         right = sample->sample_size / 2;
297         for (i = 0; i < sample->sample_size; ) {
298             if (film->audio_bits == 8) {
299                 pkt->data[i++] = film->stereo_buffer[left++];
300                 pkt->data[i++] = film->stereo_buffer[right++];
301             } else {
302                 pkt->data[i++] = film->stereo_buffer[left++];
303                 pkt->data[i++] = film->stereo_buffer[left++];
304                 pkt->data[i++] = film->stereo_buffer[right++];
305                 pkt->data[i++] = film->stereo_buffer[right++];
306             }
307         }
308     } else {
309         ret= av_get_packet(pb, pkt, sample->sample_size);
310         if (ret != sample->sample_size)
311             ret = AVERROR(EIO);
312     }
313
314     pkt->stream_index = sample->stream;
315     pkt->pts = sample->pts;
316
317     film->current_sample++;
318
319     return ret;
320 }
321
322 static int film_read_close(AVFormatContext *s)
323 {
324     FilmDemuxContext *film = s->priv_data;
325
326     av_free(film->sample_table);
327     av_free(film->stereo_buffer);
328
329     return 0;
330 }
331
332 AVInputFormat ff_segafilm_demuxer = {
333     .name           = "film_cpk",
334     .long_name      = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
335     .priv_data_size = sizeof(FilmDemuxContext),
336     .read_probe     = film_probe,
337     .read_header    = film_read_header,
338     .read_packet    = film_read_packet,
339     .read_close     = film_read_close,
340 };