LCOV - code coverage report
Current view: top level - src/libavformat - dfa.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 45 62 72.6 %
Date: 2017-03-25 17:02:41 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Chronomaster DFA Format Demuxer
       3             :  * Copyright (c) 2011 Konstantin Shishkov
       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 <inttypes.h>
      23             : 
      24             : #include "libavutil/intreadwrite.h"
      25             : #include "avformat.h"
      26             : #include "internal.h"
      27             : 
      28        5936 : static int dfa_probe(AVProbeData *p)
      29             : {
      30        5936 :     if (p->buf_size < 4 || AV_RL32(p->buf) != MKTAG('D', 'F', 'I', 'A'))
      31        5925 :         return 0;
      32             : 
      33          11 :     if (AV_RL32(p->buf + 16) != 0x80)
      34           0 :         return AVPROBE_SCORE_MAX / 4;
      35             : 
      36          11 :     return AVPROBE_SCORE_MAX;
      37             : }
      38             : 
      39          11 : static int dfa_read_header(AVFormatContext *s)
      40             : {
      41          11 :     AVIOContext *pb = s->pb;
      42             :     AVStream *st;
      43             :     int frames;
      44             :     int version;
      45             :     uint32_t mspf;
      46             : 
      47          11 :     if (avio_rl32(pb) != MKTAG('D', 'F', 'I', 'A')) {
      48           0 :         av_log(s, AV_LOG_ERROR, "Invalid magic for DFA\n");
      49           0 :         return AVERROR_INVALIDDATA;
      50             :     }
      51             : 
      52          11 :     version = avio_rl16(pb);
      53          11 :     frames = avio_rl16(pb);
      54             : 
      55          11 :     st = avformat_new_stream(s, NULL);
      56          11 :     if (!st)
      57           0 :         return AVERROR(ENOMEM);
      58             : 
      59          11 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
      60          11 :     st->codecpar->codec_id   = AV_CODEC_ID_DFA;
      61          11 :     st->codecpar->width      = avio_rl16(pb);
      62          11 :     st->codecpar->height     = avio_rl16(pb);
      63          11 :     mspf = avio_rl32(pb);
      64          11 :     if (!mspf) {
      65           0 :         av_log(s, AV_LOG_WARNING, "Zero FPS reported, defaulting to 10\n");
      66           0 :         mspf = 100;
      67             :     }
      68          11 :     avpriv_set_pts_info(st, 24, mspf, 1000);
      69          11 :     avio_skip(pb, 128 - 16); // padding
      70          11 :     st->duration = frames;
      71             : 
      72          11 :     if (ff_alloc_extradata(st->codecpar, 2))
      73           0 :         return AVERROR(ENOMEM);
      74          11 :     AV_WL16(st->codecpar->extradata, version);
      75          11 :     if (version == 0x100)
      76           0 :         st->sample_aspect_ratio = (AVRational){2, 1};
      77             : 
      78          11 :     return 0;
      79             : }
      80             : 
      81         194 : static int dfa_read_packet(AVFormatContext *s, AVPacket *pkt)
      82             : {
      83         194 :     AVIOContext *pb = s->pb;
      84             :     uint32_t frame_size;
      85         194 :     int ret, first = 1;
      86             : 
      87         194 :     if (avio_feof(pb))
      88          19 :         return AVERROR_EOF;
      89             : 
      90         175 :     if (av_get_packet(pb, pkt, 12) != 12)
      91           3 :         return AVERROR(EIO);
      92         523 :     while (!avio_feof(pb)) {
      93         343 :         if (!first) {
      94         171 :             ret = av_append_packet(pb, pkt, 12);
      95         171 :             if (ret < 0) {
      96           0 :                 av_packet_unref(pkt);
      97           0 :                 return ret;
      98             :             }
      99             :         } else
     100         172 :             first = 0;
     101         343 :         frame_size = AV_RL32(pkt->data + pkt->size - 8);
     102         343 :         if (frame_size > INT_MAX - 4) {
     103           0 :             av_log(s, AV_LOG_ERROR, "Too large chunk size: %"PRIu32"\n", frame_size);
     104           0 :             av_packet_unref(pkt);
     105           0 :             return AVERROR(EIO);
     106             :         }
     107         343 :         if (AV_RL32(pkt->data + pkt->size - 12) == MKTAG('E', 'O', 'F', 'R')) {
     108         164 :             if (frame_size) {
     109           0 :                 av_log(s, AV_LOG_WARNING,
     110             :                        "skipping %"PRIu32" bytes of end-of-frame marker chunk\n",
     111             :                        frame_size);
     112           0 :                 avio_skip(pb, frame_size);
     113             :             }
     114         164 :             return 0;
     115             :         }
     116         179 :         ret = av_append_packet(pb, pkt, frame_size);
     117         179 :         if (ret < 0) {
     118           0 :             av_packet_unref(pkt);
     119           0 :             return ret;
     120             :         }
     121             :     }
     122             : 
     123           8 :     return 0;
     124             : }
     125             : 
     126             : AVInputFormat ff_dfa_demuxer = {
     127             :     .name           = "dfa",
     128             :     .long_name      = NULL_IF_CONFIG_SMALL("Chronomaster DFA"),
     129             :     .read_probe     = dfa_probe,
     130             :     .read_header    = dfa_read_header,
     131             :     .read_packet    = dfa_read_packet,
     132             :     .flags          = AVFMT_GENERIC_INDEX,
     133             : };

Generated by: LCOV version 1.13