LCOV - code coverage report
Current view: top level - src/libavformat - apngdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 159 209 76.1 %
Date: 2017-01-28 02:43:52 Functions: 5 5 100.0 %

          Line data    Source code
       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             : 
      29             : #include "avformat.h"
      30             : #include "avio_internal.h"
      31             : #include "internal.h"
      32             : #include "libavutil/imgutils.h"
      33             : #include "libavutil/intreadwrite.h"
      34             : #include "libavutil/opt.h"
      35             : #include "libavcodec/apng.h"
      36             : #include "libavcodec/png.h"
      37             : #include "libavcodec/bytestream.h"
      38             : 
      39             : #define DEFAULT_APNG_FPS 15
      40             : 
      41             : typedef struct APNGDemuxContext {
      42             :     const AVClass *class;
      43             : 
      44             :     int max_fps;
      45             :     int default_fps;
      46             : 
      47             :     int64_t pkt_pts;
      48             :     int pkt_duration;
      49             : 
      50             :     int is_key_frame;
      51             : 
      52             :     /*
      53             :      * loop options
      54             :      */
      55             :     int ignore_loop;
      56             :     uint32_t num_frames;
      57             :     uint32_t num_play;
      58             :     uint32_t cur_loop;
      59             : } APNGDemuxContext;
      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             :  */
      70        5565 : static int apng_probe(AVProbeData *p)
      71             : {
      72             :     GetByteContext gb;
      73        5565 :     int state = 0;
      74             :     uint32_t len, tag;
      75             : 
      76        5565 :     bytestream2_init(&gb, p->buf, p->buf_size);
      77             : 
      78        5565 :     if (bytestream2_get_be64(&gb) != PNGSIG)
      79        5544 :         return 0;
      80             : 
      81             :     for (;;) {
      82         151 :         len = bytestream2_get_be32(&gb);
      83          86 :         if (len > 0x7fffffff)
      84           0 :             return 0;
      85             : 
      86          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         152 :         if (tag != MKTAG('I', 'D', 'A', 'T') &&
      90          66 :             len + 4 > bytestream2_get_bytes_left(&gb))
      91           1 :             return 0;
      92             : 
      93          85 :         switch (tag) {
      94             :         case MKTAG('I', 'H', 'D', 'R'):
      95          21 :             if (len != 13)
      96           0 :                 return 0;
      97          21 :             if (av_image_check_size(bytestream2_get_be32(&gb), bytestream2_get_be32(&gb), 0, NULL))
      98           0 :                 return 0;
      99          21 :             bytestream2_skip(&gb, 9);
     100          21 :             state++;
     101          21 :             break;
     102             :         case MKTAG('a', 'c', 'T', 'L'):
     103           5 :             if (state != 1 ||
     104           5 :                 len != 8 ||
     105           5 :                 bytestream2_get_be32(&gb) == 0) /* 0 is not a valid value for number of frames */
     106           0 :                 return 0;
     107           5 :             bytestream2_skip(&gb, 8);
     108           5 :             state++;
     109           5 :             break;
     110             :         case MKTAG('I', 'D', 'A', 'T'):
     111          20 :             if (state != 2)
     112          15 :                 return 0;
     113           5 :             goto end;
     114             :         default:
     115             :             /* skip other tags */
     116          39 :             bytestream2_skip(&gb, len + 4);
     117          39 :             break;
     118             :         }
     119             :     }
     120             : 
     121             : end:
     122           5 :     return AVPROBE_SCORE_MAX;
     123             : }
     124             : 
     125          10 : static int append_extradata(AVCodecParameters *par, AVIOContext *pb, int len)
     126             : {
     127          10 :     int previous_size = par->extradata_size;
     128             :     int new_size, ret;
     129             :     uint8_t *new_extradata;
     130             : 
     131          10 :     if (previous_size > INT_MAX - len)
     132           0 :         return AVERROR_INVALIDDATA;
     133             : 
     134          10 :     new_size = previous_size + len;
     135          10 :     new_extradata = av_realloc(par->extradata, new_size + AV_INPUT_BUFFER_PADDING_SIZE);
     136          10 :     if (!new_extradata)
     137           0 :         return AVERROR(ENOMEM);
     138          10 :     par->extradata = new_extradata;
     139          10 :     par->extradata_size = new_size;
     140             : 
     141          10 :     if ((ret = avio_read(pb, par->extradata + previous_size, len)) < 0)
     142           0 :         return ret;
     143             : 
     144          10 :     return previous_size;
     145             : }
     146             : 
     147           5 : static int apng_read_header(AVFormatContext *s)
     148             : {
     149           5 :     APNGDemuxContext *ctx = s->priv_data;
     150           5 :     AVIOContext *pb = s->pb;
     151             :     uint32_t len, tag;
     152             :     AVStream *st;
     153           5 :     int acTL_found = 0;
     154           5 :     int64_t ret = AVERROR_INVALIDDATA;
     155             : 
     156             :     /* verify PNGSIG */
     157           5 :     if (avio_rb64(pb) != PNGSIG)
     158           0 :         return ret;
     159             : 
     160             :     /* parse IHDR (must be first chunk) */
     161           5 :     len = avio_rb32(pb);
     162           5 :     tag = avio_rl32(pb);
     163           5 :     if (len != 13 || tag != MKTAG('I', 'H', 'D', 'R'))
     164           0 :         return ret;
     165             : 
     166           5 :     st = avformat_new_stream(s, NULL);
     167           5 :     if (!st)
     168           0 :         return AVERROR(ENOMEM);
     169             : 
     170             :     /* set the timebase to something large enough (1/100,000 of second)
     171             :      * to hopefully cope with all sane frame durations */
     172           5 :     avpriv_set_pts_info(st, 64, 1, 100000);
     173           5 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     174           5 :     st->codecpar->codec_id   = AV_CODEC_ID_APNG;
     175           5 :     st->codecpar->width      = avio_rb32(pb);
     176           5 :     st->codecpar->height     = avio_rb32(pb);
     177           5 :     if ((ret = av_image_check_size(st->codecpar->width, st->codecpar->height, 0, s)) < 0)
     178           0 :         return ret;
     179             : 
     180             :     /* extradata will contain every chunk up to the first fcTL (excluded) */
     181           5 :     st->codecpar->extradata = av_malloc(len + 12 + AV_INPUT_BUFFER_PADDING_SIZE);
     182           5 :     if (!st->codecpar->extradata)
     183           0 :         return AVERROR(ENOMEM);
     184           5 :     st->codecpar->extradata_size = len + 12;
     185           5 :     AV_WB32(st->codecpar->extradata,    len);
     186           5 :     AV_WL32(st->codecpar->extradata+4,  tag);
     187           5 :     AV_WB32(st->codecpar->extradata+8,  st->codecpar->width);
     188           5 :     AV_WB32(st->codecpar->extradata+12, st->codecpar->height);
     189           5 :     if ((ret = avio_read(pb, st->codecpar->extradata+16, 9)) < 0)
     190           0 :         goto fail;
     191             : 
     192          20 :     while (!avio_feof(pb)) {
     193          15 :         if (acTL_found && ctx->num_play != 1) {
     194           4 :             int64_t size   = avio_size(pb);
     195           4 :             int64_t offset = avio_tell(pb);
     196           4 :             if (size < 0) {
     197           0 :                 ret = size;
     198           0 :                 goto fail;
     199           4 :             } else if (offset < 0) {
     200           0 :                 ret = offset;
     201           0 :                 goto fail;
     202           4 :             } else if ((ret = ffio_ensure_seekback(pb, size - offset)) < 0) {
     203           0 :                 av_log(s, AV_LOG_WARNING, "Could not ensure seekback, will not loop\n");
     204           0 :                 ctx->num_play = 1;
     205             :             }
     206             :         }
     207          26 :         if ((ctx->num_play == 1 || !acTL_found) &&
     208          11 :             ((ret = ffio_ensure_seekback(pb, 4 /* len */ + 4 /* tag */)) < 0))
     209           0 :             goto fail;
     210             : 
     211          15 :         len = avio_rb32(pb);
     212          15 :         if (len > 0x7fffffff) {
     213           0 :             ret = AVERROR_INVALIDDATA;
     214           0 :             goto fail;
     215             :         }
     216             : 
     217          15 :         tag = avio_rl32(pb);
     218          15 :         switch (tag) {
     219             :         case MKTAG('a', 'c', 'T', 'L'):
     220          10 :             if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
     221           5 :                 (ret = append_extradata(st->codecpar, pb, len + 12)) < 0)
     222             :                 goto fail;
     223           5 :             acTL_found = 1;
     224           5 :             ctx->num_frames = AV_RB32(st->codecpar->extradata + ret + 8);
     225           5 :             ctx->num_play   = AV_RB32(st->codecpar->extradata + ret + 12);
     226           5 :             av_log(s, AV_LOG_DEBUG, "num_frames: %"PRIu32", num_play: %"PRIu32"\n",
     227             :                                     ctx->num_frames, ctx->num_play);
     228           5 :             break;
     229             :         case MKTAG('f', 'c', 'T', 'L'):
     230           5 :             if (!acTL_found) {
     231           0 :                ret = AVERROR_INVALIDDATA;
     232           0 :                goto fail;
     233             :             }
     234           5 :             if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
     235           0 :                 goto fail;
     236           5 :             return 0;
     237             :         default:
     238          10 :             if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
     239           5 :                 (ret = append_extradata(st->codecpar, pb, len + 12)) < 0)
     240             :                 goto fail;
     241             :         }
     242             :     }
     243             : 
     244             : fail:
     245           0 :     if (st->codecpar->extradata_size) {
     246           0 :         av_freep(&st->codecpar->extradata);
     247           0 :         st->codecpar->extradata_size = 0;
     248             :     }
     249           0 :     return ret;
     250             : }
     251             : 
     252         121 : static int decode_fctl_chunk(AVFormatContext *s, APNGDemuxContext *ctx, AVPacket *pkt)
     253             : {
     254             :     uint32_t sequence_number, width, height, x_offset, y_offset;
     255             :     uint16_t delay_num, delay_den;
     256             :     uint8_t dispose_op, blend_op;
     257             : 
     258         121 :     sequence_number = avio_rb32(s->pb);
     259         121 :     width           = avio_rb32(s->pb);
     260         121 :     height          = avio_rb32(s->pb);
     261         121 :     x_offset        = avio_rb32(s->pb);
     262         121 :     y_offset        = avio_rb32(s->pb);
     263         121 :     delay_num       = avio_rb16(s->pb);
     264         121 :     delay_den       = avio_rb16(s->pb);
     265         121 :     dispose_op      = avio_r8(s->pb);
     266         121 :     blend_op        = avio_r8(s->pb);
     267         121 :     avio_skip(s->pb, 4); /* crc */
     268             : 
     269             :     /* default is hundredths of seconds */
     270         121 :     if (!delay_den)
     271           0 :         delay_den = 100;
     272         121 :     if (!delay_num || delay_den / delay_num > ctx->max_fps) {
     273         115 :         delay_num = 1;
     274         115 :         delay_den = ctx->default_fps;
     275             :     }
     276         121 :     ctx->pkt_duration = av_rescale_q(delay_num,
     277         121 :                                      (AVRational){ 1, delay_den },
     278         121 :                                      s->streams[0]->time_base);
     279             : 
     280         121 :     av_log(s, AV_LOG_DEBUG, "%s: "
     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             : 
     301         203 :     if (width != s->streams[0]->codecpar->width ||
     302         164 :         height != s->streams[0]->codecpar->height ||
     303          82 :         x_offset != 0 ||
     304             :         y_offset != 0) {
     305          78 :         if (sequence_number == 0 ||
     306          78 :             x_offset >= s->streams[0]->codecpar->width ||
     307          78 :             width > s->streams[0]->codecpar->width - x_offset ||
     308          78 :             y_offset >= s->streams[0]->codecpar->height ||
     309          39 :             height > s->streams[0]->codecpar->height - y_offset)
     310           0 :             return AVERROR_INVALIDDATA;
     311          39 :         ctx->is_key_frame = 0;
     312             :     } else {
     313          82 :         if (sequence_number == 0 && dispose_op == APNG_DISPOSE_OP_PREVIOUS)
     314           0 :             dispose_op = APNG_DISPOSE_OP_BACKGROUND;
     315          82 :         ctx->is_key_frame = dispose_op == APNG_DISPOSE_OP_BACKGROUND ||
     316             :                             blend_op   == APNG_BLEND_OP_SOURCE;
     317             :     }
     318             : 
     319         121 :     return 0;
     320             : }
     321             : 
     322         127 : static int apng_read_packet(AVFormatContext *s, AVPacket *pkt)
     323             : {
     324         127 :     APNGDemuxContext *ctx = s->priv_data;
     325             :     int64_t ret;
     326             :     int64_t size;
     327         127 :     AVIOContext *pb = s->pb;
     328             :     uint32_t len, tag;
     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 */
     341         127 :     if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 46)) < 0)
     342           0 :         return ret;
     343             : 
     344         127 :     len = avio_rb32(pb);
     345         127 :     tag = avio_rl32(pb);
     346         127 :     switch (tag) {
     347             :     case MKTAG('f', 'c', 'T', 'L'):
     348         121 :         if (len != 26)
     349           0 :             return AVERROR_INVALIDDATA;
     350             : 
     351         121 :         if ((ret = decode_fctl_chunk(s, ctx, pkt)) < 0)
     352           0 :             return ret;
     353             : 
     354             :         /* fcTL must precede fdAT or IDAT */
     355         121 :         len = avio_rb32(pb);
     356         121 :         tag = avio_rl32(pb);
     357         121 :         if (len > 0x7fffffff ||
     358           5 :             tag != MKTAG('f', 'd', 'A', 'T') &&
     359             :             tag != MKTAG('I', 'D', 'A', 'T'))
     360           0 :             return AVERROR_INVALIDDATA;
     361             : 
     362         121 :         size = 38 /* fcTL */ + 8 /* len, tag */ + len + 4 /* crc */;
     363         121 :         if (size > INT_MAX)
     364           0 :             return AVERROR(EINVAL);
     365             : 
     366         242 :         if ((ret = avio_seek(pb, -46, SEEK_CUR)) < 0 ||
     367         121 :             (ret = av_append_packet(pb, pkt, size)) < 0)
     368           0 :             return ret;
     369             : 
     370         121 :         if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
     371           0 :             return ret;
     372             : 
     373         121 :         len = avio_rb32(pb);
     374         121 :         tag = avio_rl32(pb);
     375        4733 :         while (tag &&
     376        4496 :                tag != MKTAG('f', 'c', 'T', 'L') &&
     377             :                tag != MKTAG('I', 'E', 'N', 'D')) {
     378        4491 :             if (len > 0x7fffffff)
     379           0 :                 return AVERROR_INVALIDDATA;
     380        8982 :             if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0 ||
     381        4491 :                 (ret = av_append_packet(pb, pkt, len + 12)) < 0)
     382           0 :                 return ret;
     383        4491 :             if (ctx->num_play == 1 && (ret = ffio_ensure_seekback(pb, 8)) < 0)
     384           0 :                 return ret;
     385        4491 :             len = avio_rb32(pb);
     386        4491 :             tag = avio_rl32(pb);
     387             :         }
     388         121 :         if ((ret = avio_seek(pb, -8, SEEK_CUR)) < 0)
     389           0 :             return ret;
     390             : 
     391         121 :         if (ctx->is_key_frame)
     392          82 :             pkt->flags |= AV_PKT_FLAG_KEY;
     393         121 :         pkt->pts = ctx->pkt_pts;
     394         121 :         pkt->duration = ctx->pkt_duration;
     395         121 :         ctx->pkt_pts += ctx->pkt_duration;
     396         121 :         return ret;
     397             :     case MKTAG('I', 'E', 'N', 'D'):
     398           6 :         ctx->cur_loop++;
     399           6 :         if (ctx->ignore_loop || ctx->num_play >= 1 && ctx->cur_loop == ctx->num_play) {
     400           6 :             avio_seek(pb, -8, SEEK_CUR);
     401           6 :             return AVERROR_EOF;
     402             :         }
     403           0 :         if ((ret = avio_seek(pb, s->streams[0]->codecpar->extradata_size + 8, SEEK_SET)) < 0)
     404           0 :             return ret;
     405           0 :         return 0;
     406             :     default:
     407             :         {
     408             :         char tag_buf[32];
     409             : 
     410           0 :         av_get_codec_tag_string(tag_buf, sizeof(tag_buf), tag);
     411           0 :         avpriv_request_sample(s, "In-stream tag=%s (0x%08X) len=%"PRIu32, tag_buf, tag, len);
     412           0 :         avio_skip(pb, len + 4);
     413             :         }
     414             :     }
     415             : 
     416             :     /* Handle the unsupported yet cases */
     417           0 :     return AVERROR_PATCHWELCOME;
     418             : }
     419             : 
     420             : static const AVOption options[] = {
     421             :     { "ignore_loop", "ignore loop setting"                         , offsetof(APNGDemuxContext, ignore_loop),
     422             :       AV_OPT_TYPE_BOOL, { .i64 = 1 }              , 0, 1      , AV_OPT_FLAG_DECODING_PARAM },
     423             :     { "max_fps"    , "maximum framerate (0 is no limit)"           , offsetof(APNGDemuxContext, max_fps),
     424             :       AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
     425             :     { "default_fps", "default framerate (0 is as fast as possible)", offsetof(APNGDemuxContext, default_fps),
     426             :       AV_OPT_TYPE_INT, { .i64 = DEFAULT_APNG_FPS }, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
     427             :     { NULL },
     428             : };
     429             : 
     430             : static const AVClass demuxer_class = {
     431             :     .class_name = "APNG demuxer",
     432             :     .item_name  = av_default_item_name,
     433             :     .option     = options,
     434             :     .version    = LIBAVUTIL_VERSION_INT,
     435             :     .category   = AV_CLASS_CATEGORY_DEMUXER,
     436             : };
     437             : 
     438             : AVInputFormat ff_apng_demuxer = {
     439             :     .name           = "apng",
     440             :     .long_name      = NULL_IF_CONFIG_SMALL("Animated Portable Network Graphics"),
     441             :     .priv_data_size = sizeof(APNGDemuxContext),
     442             :     .read_probe     = apng_probe,
     443             :     .read_header    = apng_read_header,
     444             :     .read_packet    = apng_read_packet,
     445             :     .flags          = AVFMT_GENERIC_INDEX,
     446             :     .priv_class     = &demuxer_class,
     447             : };

Generated by: LCOV version 1.12