FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/paf.c
Date: 2024-04-25 05:10:44
Exec Total Coverage
Lines: 131 148 88.5%
Functions: 5 5 100.0%
Branches: 52 84 61.9%

Line Branch Exec Source
1 /*
2 * Packed Animation File demuxer
3 * Copyright (c) 2012 Paul B Mahol
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 #include "libavutil/channel_layout.h"
23 #include "libavutil/mem.h"
24 #include "libavcodec/paf.h"
25 #include "avformat.h"
26 #include "demux.h"
27 #include "internal.h"
28
29 #define MAGIC "Packed Animation File V1.0\n(c) 1992-96 Amazing Studio\x0a\x1a"
30
31 typedef struct PAFDemuxContext {
32 uint32_t buffer_size;
33 uint32_t frame_blks;
34 uint32_t nb_frames;
35 uint32_t start_offset;
36 uint32_t preload_count;
37 uint32_t max_video_blks;
38 uint32_t max_audio_blks;
39
40 uint32_t current_frame;
41 uint32_t current_frame_count;
42 uint32_t current_frame_block;
43
44 uint32_t *blocks_count_table;
45 uint32_t *frames_offset_table;
46 uint32_t *blocks_offset_table;
47
48 uint8_t *video_frame;
49 int video_size;
50
51 uint8_t *audio_frame;
52 uint8_t *temp_audio_frame;
53 int audio_size;
54
55 int got_audio;
56 } PAFDemuxContext;
57
58 7128 static int read_probe(const AVProbeData *p)
59 {
60
2/2
✓ Branch 0 taken 7126 times.
✓ Branch 1 taken 2 times.
7128 if ((p->buf_size >= strlen(MAGIC)) &&
61
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 7123 times.
7126 !memcmp(p->buf, MAGIC, strlen(MAGIC)))
62 3 return AVPROBE_SCORE_MAX;
63 7125 return 0;
64 }
65
66 3 static int read_close(AVFormatContext *s)
67 {
68 3 PAFDemuxContext *p = s->priv_data;
69
70 3 av_freep(&p->blocks_count_table);
71 3 av_freep(&p->frames_offset_table);
72 3 av_freep(&p->blocks_offset_table);
73 3 av_freep(&p->video_frame);
74 3 av_freep(&p->audio_frame);
75 3 av_freep(&p->temp_audio_frame);
76
77 3 return 0;
78 }
79
80 9 static int read_table(AVFormatContext *s, uint32_t *table, uint32_t count)
81 {
82 int i;
83
84
2/2
✓ Branch 0 taken 152565 times.
✓ Branch 1 taken 9 times.
152574 for (i = 0; i < count; i++) {
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 152565 times.
152565 if (avio_feof(s->pb))
86 return AVERROR_INVALIDDATA;
87 152565 table[i] = avio_rl32(s->pb);
88 }
89
90 9 avio_skip(s->pb, 4 * (FFALIGN(count, 512) - count));
91 9 return 0;
92 }
93
94 3 static int read_header(AVFormatContext *s)
95 {
96 3 PAFDemuxContext *p = s->priv_data;
97 3 AVIOContext *pb = s->pb;
98 AVStream *ast, *vst;
99 3 int frame_ms, ret = 0;
100
101 3 avio_skip(pb, 132);
102
103 3 vst = avformat_new_stream(s, 0);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!vst)
105 return AVERROR(ENOMEM);
106
107 3 vst->start_time = 0;
108 3 vst->nb_frames =
109 3 vst->duration =
110 3 p->nb_frames = avio_rl32(pb);
111 3 frame_ms = avio_rl32(pb);
112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (frame_ms < 1)
113 return AVERROR_INVALIDDATA;
114
115 3 vst->codecpar->width = avio_rl32(pb);
116 3 vst->codecpar->height = avio_rl32(pb);
117 3 avio_skip(pb, 4);
118
119 3 vst->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
120 3 vst->codecpar->codec_tag = 0;
121 3 vst->codecpar->codec_id = AV_CODEC_ID_PAF_VIDEO;
122 3 avpriv_set_pts_info(vst, 64, frame_ms, 1000);
123
124 3 ast = avformat_new_stream(s, 0);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!ast)
126 return AVERROR(ENOMEM);
127
128 3 ast->start_time = 0;
129 3 ast->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
130 3 ast->codecpar->codec_tag = 0;
131 3 ast->codecpar->codec_id = AV_CODEC_ID_PAF_AUDIO;
132 3 ast->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
133 3 ast->codecpar->sample_rate = 22050;
134 3 avpriv_set_pts_info(ast, 64, 1, 22050);
135
136 3 p->buffer_size = avio_rl32(pb);
137 3 p->preload_count = avio_rl32(pb);
138 3 p->frame_blks = avio_rl32(pb);
139 3 p->start_offset = avio_rl32(pb);
140 3 p->max_video_blks = avio_rl32(pb);
141 3 p->max_audio_blks = avio_rl32(pb);
142
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_feof(pb))
144 return AVERROR_INVALIDDATA;
145
146
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (p->buffer_size < 175 ||
147
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->max_audio_blks < 2 ||
148
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->max_video_blks < 1 ||
149
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->frame_blks < 1 ||
150
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->nb_frames < 1 ||
151
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->preload_count < 1 ||
152
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->buffer_size > 2048 ||
153
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->max_video_blks > 2048 ||
154
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->max_audio_blks > 2048 ||
155
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 p->nb_frames > INT_MAX / sizeof(uint32_t) ||
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 p->frame_blks > INT_MAX / sizeof(uint32_t))
157 return AVERROR_INVALIDDATA;
158
159 3 p->blocks_count_table = av_malloc_array(p->nb_frames,
160 sizeof(*p->blocks_count_table));
161 3 p->frames_offset_table = av_malloc_array(p->nb_frames,
162 sizeof(*p->frames_offset_table));
163 3 p->blocks_offset_table = av_malloc_array(p->frame_blks,
164 sizeof(*p->blocks_offset_table));
165
166 3 p->video_size = p->max_video_blks * p->buffer_size;
167 3 p->video_frame = av_mallocz(p->video_size);
168
169 3 p->audio_size = p->max_audio_blks * p->buffer_size;
170 3 p->audio_frame = av_mallocz(p->audio_size);
171 3 p->temp_audio_frame = av_mallocz(p->audio_size);
172
173
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!p->blocks_count_table ||
174
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !p->frames_offset_table ||
175
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !p->blocks_offset_table ||
176
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !p->video_frame ||
177
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 !p->audio_frame ||
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 !p->temp_audio_frame)
179 return AVERROR(ENOMEM);
180
181 3 avio_seek(pb, p->buffer_size, SEEK_SET);
182
183 3 ret = read_table(s, p->blocks_count_table, p->nb_frames);
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
185 return ret;
186 3 ret = read_table(s, p->frames_offset_table, p->nb_frames);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
188 return ret;
189 3 ret = read_table(s, p->blocks_offset_table, p->frame_blks);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ret < 0)
191 return ret;
192
193 3 p->got_audio = 0;
194 3 p->current_frame = 0;
195 3 p->current_frame_block = 0;
196
197 3 avio_seek(pb, p->start_offset, SEEK_SET);
198
199 3 return 0;
200 }
201
202 477 static int read_packet(AVFormatContext *s, AVPacket *pkt)
203 {
204 477 PAFDemuxContext *p = s->priv_data;
205 477 AVIOContext *pb = s->pb;
206 uint32_t count, offset;
207 int size, i, ret;
208
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 477 times.
477 if (p->current_frame >= p->nb_frames)
210 return AVERROR_EOF;
211
212
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 474 times.
477 if (avio_feof(pb))
213 3 return AVERROR_EOF;
214
215
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 456 times.
474 if (p->got_audio) {
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 if ((ret = av_new_packet(pkt, p->audio_size)) < 0)
217 return ret;
218
219 18 memcpy(pkt->data, p->temp_audio_frame, p->audio_size);
220 18 pkt->duration = PAF_SOUND_SAMPLES * (p->audio_size / PAF_SOUND_FRAME_SIZE);
221 18 pkt->flags |= AV_PKT_FLAG_KEY;
222 18 pkt->stream_index = 1;
223 18 p->got_audio = 0;
224 18 return pkt->size;
225 }
226
227 912 count = (p->current_frame == 0) ? p->preload_count
228
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 453 times.
456 : p->blocks_count_table[p->current_frame - 1];
229
2/2
✓ Branch 0 taken 2772 times.
✓ Branch 1 taken 456 times.
3228 for (i = 0; i < count; i++) {
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2772 times.
2772 if (p->current_frame_block >= p->frame_blks)
231 return AVERROR_INVALIDDATA;
232
233 2772 offset = p->blocks_offset_table[p->current_frame_block] & ~(1U << 31);
234
2/2
✓ Branch 0 taken 1245 times.
✓ Branch 1 taken 1527 times.
2772 if (p->blocks_offset_table[p->current_frame_block] & (1U << 31)) {
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1245 times.
1245 if (offset > p->audio_size - p->buffer_size)
236 return AVERROR_INVALIDDATA;
237
238 1245 avio_read(pb, p->audio_frame + offset, p->buffer_size);
239
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 1227 times.
1245 if (offset == (p->max_audio_blks - 2) * p->buffer_size) {
240 18 memcpy(p->temp_audio_frame, p->audio_frame, p->audio_size);
241 18 p->got_audio = 1;
242 }
243 } else {
244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1527 times.
1527 if (offset > p->video_size - p->buffer_size)
245 return AVERROR_INVALIDDATA;
246
247 1527 avio_read(pb, p->video_frame + offset, p->buffer_size);
248 }
249 2772 p->current_frame_block++;
250 }
251
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 456 times.
456 if (p->frames_offset_table[p->current_frame] >= p->video_size)
253 return AVERROR_INVALIDDATA;
254
255 456 size = p->video_size - p->frames_offset_table[p->current_frame];
256
257
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 456 times.
456 if ((ret = av_new_packet(pkt, size)) < 0)
258 return ret;
259
260 456 pkt->stream_index = 0;
261 456 pkt->duration = 1;
262 456 memcpy(pkt->data, p->video_frame + p->frames_offset_table[p->current_frame], size);
263
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 450 times.
456 if (pkt->data[0] & 0x20)
264 6 pkt->flags |= AV_PKT_FLAG_KEY;
265 456 p->current_frame++;
266
267 456 return pkt->size;
268 }
269
270 const FFInputFormat ff_paf_demuxer = {
271 .p.name = "paf",
272 .p.long_name = NULL_IF_CONFIG_SMALL("Amazing Studio Packed Animation File"),
273 .priv_data_size = sizeof(PAFDemuxContext),
274 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
275 .read_probe = read_probe,
276 .read_header = read_header,
277 .read_packet = read_packet,
278 .read_close = read_close,
279 };
280