FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/segafilm.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 137 170 80.6%
Functions: 4 5 80.0%
Branches: 61 90 67.8%

Line Branch Exec Source
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 "libavutil/mem.h"
32 #include "avformat.h"
33 #include "demux.h"
34 #include "internal.h"
35
36 #define FILM_TAG MKBETAG('F', 'I', 'L', 'M')
37 #define FDSC_TAG MKBETAG('F', 'D', 'S', 'C')
38 #define STAB_TAG MKBETAG('S', 'T', 'A', 'B')
39 #define CVID_TAG MKBETAG('c', 'v', 'i', 'd')
40 #define RAW_TAG MKBETAG('r', 'a', 'w', ' ')
41
42 typedef struct film_sample {
43 int stream;
44 unsigned int sample_size;
45 int64_t sample_offset;
46 int64_t pts;
47 int keyframe;
48 } film_sample;
49
50 typedef struct FilmDemuxContext {
51 int video_stream_index;
52 int audio_stream_index;
53
54 enum AVCodecID audio_type;
55 unsigned int audio_samplerate;
56 unsigned int audio_bits;
57 unsigned int audio_channels;
58
59 enum AVCodecID video_type;
60 unsigned int sample_count;
61 film_sample *sample_table;
62 unsigned int current_sample;
63
64 unsigned int base_clock;
65 unsigned int version;
66 } FilmDemuxContext;
67
68 7186 static int film_probe(const AVProbeData *p)
69 {
70
2/2
✓ Branch 0 taken 7181 times.
✓ Branch 1 taken 5 times.
7186 if (AV_RB32(&p->buf[0]) != FILM_TAG)
71 7181 return 0;
72
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (AV_RB32(&p->buf[16]) != FDSC_TAG)
74 return 0;
75
76 5 return AVPROBE_SCORE_MAX;
77 }
78
79 7 static int film_read_close(AVFormatContext *s)
80 {
81 7 FilmDemuxContext *film = s->priv_data;
82
83 7 av_freep(&film->sample_table);
84
85 7 return 0;
86 }
87
88 7 static int film_read_header(AVFormatContext *s)
89 {
90 7 FilmDemuxContext *film = s->priv_data;
91 7 AVIOContext *pb = s->pb;
92 AVStream *st;
93 unsigned char scratch[256];
94 int i;
95 unsigned int data_offset;
96 unsigned int audio_frame_counter;
97 unsigned int video_frame_counter;
98
99 7 film->sample_table = NULL;
100
101 /* load the main FILM header */
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (avio_read(pb, scratch, 16) != 16)
103 return AVERROR(EIO);
104 7 data_offset = AV_RB32(&scratch[4]);
105 7 film->version = AV_RB32(&scratch[8]);
106
107 /* load the FDSC chunk */
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (film->version == 0) {
109 /* special case for Lemmings .film files; 20-byte header */
110 if (avio_read(pb, scratch, 20) != 20)
111 return AVERROR(EIO);
112 /* make some assumptions about the audio parameters */
113 film->audio_type = AV_CODEC_ID_PCM_S8;
114 film->audio_samplerate = 22050;
115 film->audio_channels = 1;
116 film->audio_bits = 8;
117 } else {
118 /* normal Saturn .cpk files; 32-byte header */
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (avio_read(pb, scratch, 32) != 32)
120 return AVERROR(EIO);
121 7 film->audio_samplerate = AV_RB16(&scratch[24]);
122 7 film->audio_channels = scratch[21];
123 7 film->audio_bits = scratch[22];
124
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
7 if (scratch[23] == 2 && film->audio_channels > 0)
125 2 film->audio_type = AV_CODEC_ID_ADPCM_ADX;
126
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 else if (film->audio_channels > 0) {
127
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (film->audio_bits == 8)
128 3 film->audio_type = AV_CODEC_ID_PCM_S8_PLANAR;
129
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (film->audio_bits == 16)
130 1 film->audio_type = AV_CODEC_ID_PCM_S16BE_PLANAR;
131 else
132 film->audio_type = AV_CODEC_ID_NONE;
133 } else
134 1 film->audio_type = AV_CODEC_ID_NONE;
135 }
136
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (AV_RB32(&scratch[0]) != FDSC_TAG)
138 return AVERROR_INVALIDDATA;
139
140
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (AV_RB32(&scratch[8]) == CVID_TAG) {
141 6 film->video_type = AV_CODEC_ID_CINEPAK;
142
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (AV_RB32(&scratch[8]) == RAW_TAG) {
143 1 film->video_type = AV_CODEC_ID_RAWVIDEO;
144 } else {
145 film->video_type = AV_CODEC_ID_NONE;
146 }
147
148
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7 if (film->video_type == AV_CODEC_ID_NONE && film->audio_type == AV_CODEC_ID_NONE)
149 return AVERROR_INVALIDDATA;
150
151 /* initialize the decoder streams */
152
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (film->video_type != AV_CODEC_ID_NONE) {
153 7 st = avformat_new_stream(s, NULL);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!st)
155 return AVERROR(ENOMEM);
156 7 film->video_stream_index = st->index;
157 7 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
158 7 st->codecpar->codec_id = film->video_type;
159 7 st->codecpar->codec_tag = 0; /* no fourcc */
160 7 st->codecpar->width = AV_RB32(&scratch[16]);
161 7 st->codecpar->height = AV_RB32(&scratch[12]);
162
163
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (film->video_type == AV_CODEC_ID_RAWVIDEO) {
164
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (scratch[20] == 24) {
165 1 st->codecpar->format = AV_PIX_FMT_RGB24;
166 } else {
167 av_log(s, AV_LOG_ERROR, "raw video is using unhandled %dbpp\n", scratch[20]);
168 return -1;
169 }
170 }
171 }
172
173
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (film->audio_type != AV_CODEC_ID_NONE) {
174 6 st = avformat_new_stream(s, NULL);
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!st)
176 return AVERROR(ENOMEM);
177 6 film->audio_stream_index = st->index;
178 6 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
179 6 st->codecpar->codec_id = film->audio_type;
180 6 st->codecpar->codec_tag = 1;
181 6 st->codecpar->ch_layout.nb_channels = film->audio_channels;
182 6 st->codecpar->sample_rate = film->audio_samplerate;
183
184
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (film->audio_type == AV_CODEC_ID_ADPCM_ADX) {
185 2 st->codecpar->bits_per_coded_sample = 18 * 8 / 32;
186 2 st->codecpar->block_align = film->audio_channels * 18;
187 2 ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
188 } else {
189 4 st->codecpar->bits_per_coded_sample = film->audio_bits;
190 4 st->codecpar->block_align = film->audio_channels *
191 4 st->codecpar->bits_per_coded_sample / 8;
192 }
193
194 6 st->codecpar->bit_rate = film->audio_channels * st->codecpar->sample_rate *
195 6 st->codecpar->bits_per_coded_sample;
196 }
197
198 /* load the sample table */
199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (avio_read(pb, scratch, 16) != 16)
200 return AVERROR(EIO);
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (AV_RB32(&scratch[0]) != STAB_TAG)
202 return AVERROR_INVALIDDATA;
203 7 film->base_clock = AV_RB32(&scratch[8]);
204 7 film->sample_count = AV_RB32(&scratch[12]);
205 7 film->sample_table = av_malloc_array(film->sample_count, sizeof(film_sample));
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!film->sample_table)
207 return AVERROR(ENOMEM);
208
209
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 7 times.
20 for (i = 0; i < s->nb_streams; i++) {
210 13 st = s->streams[i];
211
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
13 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
212 7 avpriv_set_pts_info(st, 33, 1, film->base_clock);
213 else
214 6 avpriv_set_pts_info(st, 64, 1, film->audio_samplerate);
215 }
216
217 7 audio_frame_counter = video_frame_counter = 0;
218
2/2
✓ Branch 0 taken 4048 times.
✓ Branch 1 taken 7 times.
4055 for (i = 0; i < film->sample_count; i++) {
219 /* load the next sample record and transfer it to an internal struct */
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4048 times.
4048 if (avio_read(pb, scratch, 16) != 16)
221 return AVERROR(EIO);
222 4048 film->sample_table[i].sample_offset =
223 4048 data_offset + AV_RB32(&scratch[0]);
224 4048 film->sample_table[i].sample_size = AV_RB32(&scratch[4]);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4048 times.
4048 if (film->sample_table[i].sample_size > INT_MAX / 4)
226 return AVERROR_INVALIDDATA;
227
2/2
✓ Branch 0 taken 1463 times.
✓ Branch 1 taken 2585 times.
4048 if (AV_RB32(&scratch[8]) == 0xFFFFFFFF) {
228 1463 film->sample_table[i].stream = film->audio_stream_index;
229 1463 film->sample_table[i].pts = audio_frame_counter;
230
231
2/2
✓ Branch 0 taken 1369 times.
✓ Branch 1 taken 94 times.
1463 if (film->audio_type == AV_CODEC_ID_ADPCM_ADX)
232 1369 audio_frame_counter += (film->sample_table[i].sample_size * 32 /
233 1369 (18 * film->audio_channels));
234
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 else if (film->audio_type != AV_CODEC_ID_NONE)
235 94 audio_frame_counter += (film->sample_table[i].sample_size /
236 94 (film->audio_channels * film->audio_bits / 8));
237 1463 film->sample_table[i].keyframe = 1;
238 } else {
239 2585 film->sample_table[i].stream = film->video_stream_index;
240 2585 film->sample_table[i].pts = AV_RB32(&scratch[8]) & 0x7FFFFFFF;
241 2585 film->sample_table[i].keyframe = (scratch[8] & 0x80) ? 0 : AVINDEX_KEYFRAME;
242 2585 video_frame_counter++;
243
1/2
✓ Branch 0 taken 2585 times.
✗ Branch 1 not taken.
2585 if (film->video_type != AV_CODEC_ID_NONE)
244 2585 av_add_index_entry(s->streams[film->video_stream_index],
245 2585 film->sample_table[i].sample_offset,
246 2585 film->sample_table[i].pts,
247 2585 film->sample_table[i].sample_size, 0,
248 2585 film->sample_table[i].keyframe);
249 }
250 }
251
252
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (film->audio_type != AV_CODEC_ID_NONE)
253 6 s->streams[film->audio_stream_index]->duration = audio_frame_counter;
254
255
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (film->video_type != AV_CODEC_ID_NONE)
256 7 s->streams[film->video_stream_index]->duration = video_frame_counter;
257
258 7 film->current_sample = 0;
259
260 7 return 0;
261 }
262
263 1718 static int film_read_packet(AVFormatContext *s,
264 AVPacket *pkt)
265 {
266 1718 FilmDemuxContext *film = s->priv_data;
267 1718 AVIOContext *pb = s->pb;
268 film_sample *sample;
269 1718 film_sample *next_sample = NULL;
270 int next_sample_id;
271 1718 int ret = 0;
272
273
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1711 times.
1718 if (film->current_sample >= film->sample_count)
274 7 return AVERROR_EOF;
275
276 1711 sample = &film->sample_table[film->current_sample];
277
278 /* Find the next sample from the same stream, assuming there is one;
279 * this is used to calculate the duration below */
280 1711 next_sample_id = film->current_sample + 1;
281
2/2
✓ Branch 0 taken 3270 times.
✓ Branch 1 taken 1700 times.
6681 while (next_sample == NULL) {
282
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3259 times.
3270 if (next_sample_id >= film->sample_count)
283 11 break;
284
285 3259 next_sample = &film->sample_table[next_sample_id];
286
2/2
✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 1559 times.
3259 if (next_sample->stream != sample->stream) {
287 1559 next_sample = NULL;
288 1559 next_sample_id++;
289 }
290 }
291
292 /* position the stream (will probably be there anyway) */
293 1711 avio_seek(pb, sample->sample_offset, SEEK_SET);
294
295 1711 ret = av_get_packet(pb, pkt, sample->sample_size);
296
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1709 times.
1711 if (ret != sample->sample_size)
297 2 ret = AVERROR(EIO);
298
299 1711 pkt->stream_index = sample->stream;
300 1711 pkt->dts = sample->pts;
301 1711 pkt->pts = sample->pts;
302 1711 pkt->flags |= sample->keyframe ? AV_PKT_FLAG_KEY : 0;
303
2/2
✓ Branch 0 taken 1700 times.
✓ Branch 1 taken 11 times.
1711 if (next_sample != NULL)
304 1700 pkt->duration = next_sample->pts - sample->pts;
305
306 1711 film->current_sample++;
307
308 1711 return ret;
309 }
310
311 static int film_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
312 {
313 FilmDemuxContext *film = s->priv_data;
314 AVStream *st = s->streams[stream_index];
315 int64_t pos;
316 int ret = av_index_search_timestamp(st, timestamp, flags);
317 if (ret < 0)
318 return ret;
319
320 pos = avio_seek(s->pb, ffstream(st)->index_entries[ret].pos, SEEK_SET);
321 if (pos < 0)
322 return pos;
323
324 film->current_sample = ret;
325
326 return 0;
327 }
328
329 const FFInputFormat ff_segafilm_demuxer = {
330 .p.name = "film_cpk",
331 .p.long_name = NULL_IF_CONFIG_SMALL("Sega FILM / CPK"),
332 .priv_data_size = sizeof(FilmDemuxContext),
333 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
334 .read_probe = film_probe,
335 .read_header = film_read_header,
336 .read_packet = film_read_packet,
337 .read_close = film_read_close,
338 .read_seek = film_read_seek,
339 };
340