LCOV - code coverage report
Current view: top level - libavformat - icodec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4 114 3.5 %
Date: 2017-12-17 16:07:53 Functions: 1 4 25.0 %

          Line data    Source code
       1             : /*
       2             :  * Microsoft Windows ICO demuxer
       3             :  * Copyright (c) 2011 Peter Ross (pross@xvid.org)
       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             :  * Microsoft Windows ICO demuxer
      25             :  */
      26             : 
      27             : #include "libavutil/intreadwrite.h"
      28             : #include "libavcodec/bytestream.h"
      29             : #include "libavcodec/bmp.h"
      30             : #include "libavcodec/png.h"
      31             : #include "avformat.h"
      32             : #include "internal.h"
      33             : 
      34             : typedef struct {
      35             :     int offset;
      36             :     int size;
      37             :     int nb_pal;
      38             : } IcoImage;
      39             : 
      40             : typedef struct {
      41             :     int current_image;
      42             :     int nb_images;
      43             :     IcoImage * images;
      44             : } IcoDemuxContext;
      45             : 
      46        6130 : static int probe(AVProbeData *p)
      47             : {
      48        6130 :     unsigned i, frames, checked = 0;
      49             : 
      50        6130 :     if (p->buf_size < 22 || AV_RL16(p->buf) || AV_RL16(p->buf + 2) != 1)
      51        6130 :         return 0;
      52           0 :     frames = AV_RL16(p->buf + 4);
      53           0 :     if (!frames)
      54           0 :         return 0;
      55           0 :     for (i = 0; i < frames && i * 16 + 22 <= p->buf_size; i++) {
      56             :         unsigned offset;
      57           0 :         if (AV_RL16(p->buf + 10 + i * 16) & ~1)
      58           0 :             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
      59           0 :         if (p->buf[13 + i * 16])
      60           0 :             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
      61           0 :         if (AV_RL32(p->buf + 14 + i * 16) < 40)
      62           0 :             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
      63           0 :         offset = AV_RL32(p->buf + 18 + i * 16);
      64           0 :         if (offset < 22)
      65           0 :             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
      66           0 :         if (offset > p->buf_size - 8)
      67           0 :             continue;
      68           0 :         if (p->buf[offset] != 40 && AV_RB64(p->buf + offset) != PNGSIG)
      69           0 :             return FFMIN(i, AVPROBE_SCORE_MAX / 4);
      70           0 :         checked++;
      71             :     }
      72             : 
      73           0 :     if (checked < frames)
      74           0 :         return AVPROBE_SCORE_MAX / 4 + FFMIN(checked, 1);
      75           0 :     return AVPROBE_SCORE_MAX / 2 + 1;
      76             : }
      77             : 
      78           0 : static int read_header(AVFormatContext *s)
      79             : {
      80           0 :     IcoDemuxContext *ico = s->priv_data;
      81           0 :     AVIOContext *pb = s->pb;
      82             :     int i, codec;
      83             : 
      84           0 :     avio_skip(pb, 4);
      85           0 :     ico->nb_images = avio_rl16(pb);
      86             : 
      87           0 :     ico->images = av_malloc_array(ico->nb_images, sizeof(IcoImage));
      88           0 :     if (!ico->images)
      89           0 :         return AVERROR(ENOMEM);
      90             : 
      91           0 :     for (i = 0; i < ico->nb_images; i++) {
      92             :         AVStream *st;
      93             :         int tmp;
      94             : 
      95           0 :         if (avio_seek(pb, 6 + i * 16, SEEK_SET) < 0)
      96           0 :             break;
      97             : 
      98           0 :         st = avformat_new_stream(s, NULL);
      99           0 :         if (!st)
     100           0 :             return AVERROR(ENOMEM);
     101             : 
     102           0 :         st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     103           0 :         st->codecpar->width      = avio_r8(pb);
     104           0 :         st->codecpar->height     = avio_r8(pb);
     105           0 :         ico->images[i].nb_pal = avio_r8(pb);
     106           0 :         if (ico->images[i].nb_pal == 255)
     107           0 :             ico->images[i].nb_pal = 0;
     108             : 
     109           0 :         avio_skip(pb, 5);
     110             : 
     111           0 :         ico->images[i].size   = avio_rl32(pb);
     112           0 :         if (ico->images[i].size <= 0) {
     113           0 :             av_log(s, AV_LOG_ERROR, "Invalid image size %d\n", ico->images[i].size);
     114           0 :             return AVERROR_INVALIDDATA;
     115             :         }
     116           0 :         ico->images[i].offset = avio_rl32(pb);
     117             : 
     118           0 :         if (avio_seek(pb, ico->images[i].offset, SEEK_SET) < 0)
     119           0 :             break;
     120             : 
     121           0 :         codec = avio_rl32(pb);
     122           0 :         switch (codec) {
     123           0 :         case MKTAG(0x89, 'P', 'N', 'G'):
     124           0 :             st->codecpar->codec_id = AV_CODEC_ID_PNG;
     125           0 :             st->codecpar->width    = 0;
     126           0 :             st->codecpar->height   = 0;
     127           0 :             break;
     128           0 :         case 40:
     129           0 :             if (ico->images[i].size < 40)
     130           0 :                 return AVERROR_INVALIDDATA;
     131           0 :             st->codecpar->codec_id = AV_CODEC_ID_BMP;
     132           0 :             tmp = avio_rl32(pb);
     133           0 :             if (tmp)
     134           0 :                 st->codecpar->width = tmp;
     135           0 :             tmp = avio_rl32(pb);
     136           0 :             if (tmp)
     137           0 :                 st->codecpar->height = tmp / 2;
     138           0 :             break;
     139           0 :         default:
     140           0 :             avpriv_request_sample(s, "codec %d", codec);
     141           0 :             return AVERROR_INVALIDDATA;
     142             :         }
     143             :     }
     144             : 
     145           0 :     return 0;
     146             : }
     147             : 
     148           0 : static int read_packet(AVFormatContext *s, AVPacket *pkt)
     149             : {
     150           0 :     IcoDemuxContext *ico = s->priv_data;
     151             :     IcoImage *image;
     152           0 :     AVIOContext *pb = s->pb;
     153           0 :     AVStream *st = s->streams[0];
     154             :     int ret;
     155             : 
     156           0 :     if (ico->current_image >= ico->nb_images)
     157           0 :         return AVERROR_EOF;
     158             : 
     159           0 :     image = &ico->images[ico->current_image];
     160             : 
     161           0 :     if ((ret = avio_seek(pb, image->offset, SEEK_SET)) < 0)
     162           0 :         return ret;
     163             : 
     164           0 :     if (s->streams[ico->current_image]->codecpar->codec_id == AV_CODEC_ID_PNG) {
     165           0 :         if ((ret = av_get_packet(pb, pkt, image->size)) < 0)
     166           0 :             return ret;
     167             :     } else {
     168             :         uint8_t *buf;
     169           0 :         if ((ret = av_new_packet(pkt, 14 + image->size)) < 0)
     170           0 :             return ret;
     171           0 :         buf = pkt->data;
     172             : 
     173             :         /* add BMP header */
     174           0 :         bytestream_put_byte(&buf, 'B');
     175           0 :         bytestream_put_byte(&buf, 'M');
     176           0 :         bytestream_put_le32(&buf, pkt->size);
     177           0 :         bytestream_put_le16(&buf, 0);
     178           0 :         bytestream_put_le16(&buf, 0);
     179           0 :         bytestream_put_le32(&buf, 0);
     180             : 
     181           0 :         if ((ret = avio_read(pb, buf, image->size)) != image->size) {
     182           0 :             av_packet_unref(pkt);
     183           0 :             return ret < 0 ? ret : AVERROR_INVALIDDATA;
     184             :         }
     185             : 
     186           0 :         st->codecpar->bits_per_coded_sample = AV_RL16(buf + 14);
     187             : 
     188           0 :         if (AV_RL32(buf + 32))
     189           0 :             image->nb_pal = AV_RL32(buf + 32);
     190             : 
     191           0 :         if (st->codecpar->bits_per_coded_sample <= 8 && !image->nb_pal) {
     192           0 :             image->nb_pal = 1 << st->codecpar->bits_per_coded_sample;
     193           0 :             AV_WL32(buf + 32, image->nb_pal);
     194             :         }
     195             : 
     196           0 :         AV_WL32(buf - 4, 14 + 40 + image->nb_pal * 4);
     197           0 :         AV_WL32(buf + 8, AV_RL32(buf + 8) / 2);
     198             :     }
     199             : 
     200           0 :     pkt->stream_index = ico->current_image++;
     201           0 :     pkt->flags |= AV_PKT_FLAG_KEY;
     202             : 
     203           0 :     return 0;
     204             : }
     205             : 
     206           0 : static int ico_read_close(AVFormatContext * s)
     207             : {
     208           0 :     IcoDemuxContext *ico = s->priv_data;
     209           0 :     av_freep(&ico->images);
     210           0 :     return 0;
     211             : }
     212             : 
     213             : AVInputFormat ff_ico_demuxer = {
     214             :     .name           = "ico",
     215             :     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft Windows ICO"),
     216             :     .priv_data_size = sizeof(IcoDemuxContext),
     217             :     .read_probe     = probe,
     218             :     .read_header    = read_header,
     219             :     .read_packet    = read_packet,
     220             :     .read_close     = ico_read_close,
     221             :     .flags          = AVFMT_NOTIMESTAMPS,
     222             : };

Generated by: LCOV version 1.13