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
40
41 #define DEFAULT_APNG_FPS 15
42
45
48
50
52
53 /*
54 * loop options
55 */
61
62 /*
63 * To be a valid APNG file, we mandate, in this order:
64 * PNGSIG
65 * IHDR
66 * ...
67 * acTL
68 * ...
69 * IDAT
70 */
72 {
76
78
79 if (bytestream2_get_be64(&gb) !=
PNGSIG)
80 return 0;
81
82 for (;;) {
83 len = bytestream2_get_be32(&gb);
85 return 0;
86
87 tag = bytestream2_get_le32(&gb);
88 /* we don't check IDAT size, as this is the last tag
89 * we check, and it may be larger than the probe buffer */
90 if (
tag !=
MKTAG(
'I',
'D',
'A',
'T') &&
92 return 0;
93
95 case MKTAG(
'I',
'H',
'D',
'R'):
97 return 0;
99 return 0;
102 break;
103 case MKTAG(
'a',
'c',
'T',
'L'):
106 bytestream2_get_be32(&gb) == 0) /* 0 is not a valid value for number of frames */
107 return 0;
110 break;
111 case MKTAG(
'I',
'D',
'A',
'T'):
113 return 0;
114 goto end;
115 default:
116 /* skip other tags */
118 break;
119 }
120 }
121
122 end:
124 }
125
127 {
130 uint8_t *new_extradata;
131
134
135 new_size = previous_size +
len;
137 if (!new_extradata)
142
145
146 return previous_size;
147 }
148
150 {
155 int acTL_found = 0;
157
158 /* verify PNGSIG */
161
162 /* parse IHDR (must be first chunk) */
167
169 if (!st)
171
172 /* set the timebase to something large enough (1/100,000 of second)
173 * to hopefully cope with all sane frame durations */
181
182 /* extradata will contain every chunk up to the first fcTL (excluded) */
192
193 while (1) {
194 if (acTL_found &&
ctx->num_play != 1) {
204 }
205 }
206 if ((
ctx->num_play == 1 || !acTL_found) &&
209
211 if (
len > INT_MAX - 12)
213
216 case MKTAG(
'a',
'c',
'T',
'L'):
220 acTL_found = 1;
224 ctx->num_frames,
ctx->num_play);
225 break;
226 case MKTAG(
'f',
'c',
'T',
'L'):
229 }
232 return 0;
233 default:
237 }
238 }
239 }
240
242 {
243 uint32_t sequence_number,
width,
height, x_offset, y_offset;
244 uint16_t delay_num, delay_den;
245 uint8_t dispose_op, blend_op;
246
257
258 /* default is hundredths of seconds */
259 if (!delay_den)
260 delay_den = 100;
261 if (!delay_num || (
ctx->max_fps && delay_den / delay_num >
ctx->max_fps)) {
262 delay_num = 1;
263 delay_den =
ctx->default_fps;
264 }
267 s->streams[0]->time_base);
268
270 "sequence_number: %"PRId32", "
271 "width: %"PRIu32", "
272 "height: %"PRIu32", "
273 "x_offset: %"PRIu32", "
274 "y_offset: %"PRIu32", "
275 "delay_num: %"PRIu16", "
276 "delay_den: %"PRIu16", "
277 "dispose_op: %d, "
278 "blend_op: %d\n",
279 __func__,
280 sequence_number,
283 x_offset,
284 y_offset,
285 delay_num,
286 delay_den,
287 dispose_op,
288 blend_op);
289
290 if (
width !=
s->streams[0]->codecpar->width ||
291 height !=
s->streams[0]->codecpar->height ||
292 x_offset != 0 ||
293 y_offset != 0) {
294 if (sequence_number == 0 ||
295 x_offset >=
s->streams[0]->codecpar->width ||
296 width >
s->streams[0]->codecpar->width - x_offset ||
297 y_offset >=
s->streams[0]->codecpar->height ||
298 height >
s->streams[0]->codecpar->height - y_offset)
300 ctx->is_key_frame = 0;
301 } else {
306 }
307
308 return 0;
309 }
310
312 {
318
319 /*
320 * fcTL chunk length, in bytes:
321 * 4 (length)
322 * 4 (tag)
323 * 26 (actual chunk)
324 * 4 (crc) bytes
325 * and needed next:
326 * 4 (length)
327 * 4 (tag (must be fdAT or IDAT))
328 */
329 /* if num_play is not 1, then the seekback is already guaranteed */
332
335
338
340 case MKTAG(
'f',
'c',
'T',
'L'):
343
346
347 /* fcTL must precede fdAT or IDAT */
350 if (
len > 0x7fffffff ||
354
355 size = 38
/* fcTL */ + 8
/* len, tag */ +
len + 4
/* crc */;
358
362
365
371 if (
len > 0x7fffffff)
380 }
383
384 if (
ctx->is_key_frame)
389 case MKTAG(
'I',
'E',
'N',
'D'):
391 if (
ctx->ignore_loop ||
ctx->num_play >= 1 &&
ctx->cur_loop ==
ctx->num_play) {
394 }
395 if ((
ret =
avio_seek(pb,
s->streams[0]->codecpar->extradata_size + 8, SEEK_SET)) < 0)
397 return 0;
398 default:
402 }
403
404 /* Handle the unsupported yet cases */
406 }
407
409 {
"ignore_loop",
"ignore loop setting" , offsetof(
APNGDemuxContext, ignore_loop),
411 {
"max_fps" ,
"maximum framerate (0 is no limit)" , offsetof(
APNGDemuxContext, max_fps),
413 {
"default_fps",
"default framerate (0 is as fast as possible)", offsetof(
APNGDemuxContext, default_fps),
416 };
417
424 };
425
435 };