1 /*
2 * APNG demuxer
3 * Copyright (c) 2014 Benoit Fouet
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 * APNG demuxer.
25 * @see https://wiki.mozilla.org/APNG_Specification
26 * @see http://www.w3.org/TR/PNG
27 */
28
38
39 #define DEFAULT_APNG_FPS 15
40
43
46
49
51
52 /*
53 * loop options
54 */
60
61 /*
62 * To be a valid APNG file, we mandate, in this order:
63 * PNGSIG
64 * IHDR
65 * ...
66 * acTL
67 * ...
68 * IDAT
69 */
71 {
75
77
78 if (bytestream2_get_be64(&gb) !=
PNGSIG)
79 return 0;
80
81 for (;;) {
82 len = bytestream2_get_be32(&gb);
83 if (len > 0x7fffffff)
84 return 0;
85
86 tag = bytestream2_get_le32(&gb);
87 /* we don't check IDAT size, as this is the last tag
88 * we check, and it may be larger than the probe buffer */
89 if (tag !=
MKTAG(
'I',
'D',
'A',
'T') &&
91 return 0;
92
93 switch (tag) {
94 case MKTAG(
'I',
'H',
'D',
'R'):
95 if (len != 13)
96 return 0;
98 return 0;
100 state++;
101 break;
102 case MKTAG(
'a',
'c',
'T',
'L'):
103 if (state != 1 ||
104 len != 8 ||
105 bytestream2_get_be32(&gb) == 0) /* 0 is not a valid value for number of frames */
106 return 0;
108 state++;
109 break;
110 case MKTAG(
'I',
'D',
'A',
'T'):
111 if (state != 2)
112 return 0;
114 default:
115 /* skip other tags */
117 break;
118 }
119 }
120
123 }
124
126 {
130
131 if (previous_size > INT_MAX - len)
133
134 new_size = previous_size +
len;
136 if (!new_extradata)
140
143
144 return previous_size;
145 }
146
148 {
153 int acTL_found = 0;
155
156 /* verify PNGSIG */
159
160 /* parse IHDR (must be first chunk) */
163 if (len != 13 || tag !=
MKTAG(
'I',
'H',
'D',
'R'))
165
167 if (!st)
169
170 /* set the timebase to something large enough (1/100,000 of second)
171 * to hopefully cope with all sane frame durations */
179
180 /* extradata will contain every chunk up to the first fcTL (excluded) */
190 goto fail;
191
193 if (acTL_found && ctx->
num_play != 1) {
196 if (size < 0) {
198 goto fail;
199 } else if (offset < 0) {
201 goto fail;
205 }
206 }
207 if ((ctx->
num_play == 1 || !acTL_found) &&
209 goto fail;
210
212 if (len > 0x7fffffff) {
214 goto fail;
215 }
216
218 switch (tag) {
219 case MKTAG(
'a',
'c',
'T',
'L'):
220 if ((ret =
avio_seek(pb, -8, SEEK_CUR)) < 0 ||
222 goto fail;
223 acTL_found = 1;
228 break;
229 case MKTAG(
'f',
'c',
'T',
'L'):
230 if (!acTL_found) {
232 goto fail;
233 }
234 if ((ret =
avio_seek(pb, -8, SEEK_CUR)) < 0)
235 goto fail;
236 return 0;
237 default:
238 if ((ret =
avio_seek(pb, -8, SEEK_CUR)) < 0 ||
240 goto fail;
241 }
242 }
243
244 fail:
248 }
250 }
251
253 {
254 uint32_t sequence_number,
width,
height, x_offset, y_offset;
255 uint16_t delay_num, delay_den;
257
268
269 /* default is hundredths of seconds */
270 if (!delay_den)
271 delay_den = 100;
272 if (!delay_num || delay_den / delay_num > ctx->
max_fps) {
273 delay_num = 1;
275 }
279
281 "sequence_number: %"PRId32", "
282 "width: %"PRIu32", "
283 "height: %"PRIu32", "
284 "x_offset: %"PRIu32", "
285 "y_offset: %"PRIu32", "
286 "delay_num: %"PRIu16", "
287 "delay_den: %"PRIu16", "
288 "dispose_op: %d, "
289 "blend_op: %d\n",
290 __FUNCTION__,
291 sequence_number,
292 width,
293 height,
294 x_offset,
295 y_offset,
296 delay_num,
297 delay_den,
298 dispose_op,
299 blend_op);
300
303 x_offset != 0 ||
304 y_offset != 0) {
305 if (sequence_number == 0 ||
312 } else {
317 }
318
319 return 0;
320 }
321
323 {
329
330 /*
331 * fcTL chunk length, in bytes:
332 * 4 (length)
333 * 4 (tag)
334 * 26 (actual chunk)
335 * 4 (crc) bytes
336 * and needed next:
337 * 4 (length)
338 * 4 (tag (must be fdAT or IDAT))
339 */
340 /* if num_play is not 1, then the seekback is already guaranteed */
343
346 switch (tag) {
347 case MKTAG(
'f',
'c',
'T',
'L'):
348 if (len != 26)
350
353
354 /* fcTL must precede fdAT or IDAT */
357 if (len > 0x7fffffff ||
358 tag !=
MKTAG(
'f',
'd',
'A',
'T') &&
359 tag !=
MKTAG(
'I',
'D',
'A',
'T'))
361
362 size = 38 /* fcTL */ + 8 /* len, tag */ + len + 4 /* crc */;
363 if (size > INT_MAX)
365
366 if ((ret =
avio_seek(pb, -46, SEEK_CUR)) < 0 ||
369
372
375 while (tag &&
376 tag !=
MKTAG(
'f',
'c',
'T',
'L') &&
377 tag !=
MKTAG(
'I',
'E',
'N',
'D')) {
378 if (len > 0x7fffffff)
380 if ((ret =
avio_seek(pb, -8, SEEK_CUR)) < 0 ||
387 }
388 if ((ret =
avio_seek(pb, -8, SEEK_CUR)) < 0)
390
397 case MKTAG(
'I',
'E',
'N',
'D'):
402 }
405 return 0;
406 default:
407 {
408 char tag_buf[32];
409
413 }
414 }
415
416 /* Handle the unsupported yet cases */
418 }
419
421 {
"ignore_loop",
"ignore loop setting" , offsetof(
APNGDemuxContext, ignore_loop),
423 {
"max_fps" ,
"maximum framerate (0 is no limit)" , offsetof(
APNGDemuxContext, max_fps),
425 {
"default_fps",
"default framerate (0 is as fast as possible)", offsetof(
APNGDemuxContext, default_fps),
428 };
429
436 };
437
446 .priv_class = &demuxer_class,
447 };