LCOV - code coverage report
Current view: top level - libavformat - supdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 7 48 14.6 %
Date: 2017-12-15 02:19:58 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*
       2             :  * This file is part of FFmpeg.
       3             :  *
       4             :  * FFmpeg is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Lesser General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2.1 of the License, or (at your option) any later version.
       8             :  *
       9             :  * FFmpeg is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Lesser General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public
      15             :  * License along with FFmpeg; if not, write to the Free Software
      16             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      17             :  */
      18             : 
      19             : #include "avformat.h"
      20             : #include "internal.h"
      21             : #include "libavutil/intreadwrite.h"
      22             : 
      23             : #define SUP_PGS_MAGIC 0x5047 /* "PG", big endian */
      24             : 
      25           0 : static int sup_read_header(AVFormatContext *s)
      26             : {
      27           0 :     AVStream *st = avformat_new_stream(s, NULL);
      28           0 :     if (!st)
      29           0 :         return AVERROR(ENOMEM);
      30           0 :     st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
      31           0 :     st->codecpar->codec_id = AV_CODEC_ID_HDMV_PGS_SUBTITLE;
      32           0 :     avpriv_set_pts_info(st, 32, 1, 90000);
      33             : 
      34           0 :     return 0;
      35             : }
      36             : 
      37           0 : static int sup_read_packet(AVFormatContext *s, AVPacket *pkt)
      38             : {
      39             :     int64_t pts, dts, pos;
      40             :     int ret;
      41             : 
      42           0 :     pos = avio_tell(s->pb);
      43             : 
      44           0 :     if (avio_rb16(s->pb) != SUP_PGS_MAGIC)
      45           0 :         return avio_feof(s->pb) ? AVERROR_EOF : AVERROR_INVALIDDATA;
      46             : 
      47           0 :     pts = avio_rb32(s->pb);
      48           0 :     dts = avio_rb32(s->pb);
      49             : 
      50           0 :     if ((ret = av_get_packet(s->pb, pkt, 3)) < 0)
      51           0 :         return ret;
      52             : 
      53           0 :     pkt->stream_index = 0;
      54           0 :     pkt->flags |= AV_PKT_FLAG_KEY;
      55           0 :     pkt->pos = pos;
      56           0 :     pkt->pts = pts;
      57             :     // Many files have DTS set to 0 for all packets, so assume 0 means unset.
      58           0 :     pkt->dts = dts ? dts : AV_NOPTS_VALUE;
      59             : 
      60           0 :     if (pkt->size >= 3) {
      61             :         // The full packet size is stored as part of the packet.
      62           0 :         size_t len = AV_RB16(pkt->data + 1);
      63             : 
      64           0 :         if ((ret = av_append_packet(s->pb, pkt, len)) < 0)
      65           0 :             return ret;
      66             :     }
      67             : 
      68           0 :     return 0;
      69             : }
      70             : 
      71        6130 : static int sup_probe(AVProbeData *p)
      72             : {
      73        6130 :     unsigned char *buf = p->buf;
      74        6130 :     size_t buf_size = p->buf_size;
      75             :     int nb_packets;
      76             : 
      77        6130 :     for (nb_packets = 0; nb_packets < 10; nb_packets++) {
      78             :         size_t full_packet_size;
      79        6130 :         if (buf_size < 10 + 3)
      80           0 :             break;
      81        6130 :         if (AV_RB16(buf) != SUP_PGS_MAGIC)
      82        6130 :             return 0;
      83           0 :         full_packet_size = AV_RB16(buf + 10 + 1) + 10 + 3;
      84           0 :         if (buf_size < full_packet_size)
      85           0 :             break;
      86           0 :         buf += full_packet_size;
      87           0 :         buf_size -= full_packet_size;
      88             :     }
      89           0 :     if (!nb_packets)
      90           0 :         return 0;
      91           0 :     if (nb_packets < 2)
      92           0 :         return AVPROBE_SCORE_RETRY / 2;
      93           0 :     if (nb_packets < 4)
      94           0 :         return AVPROBE_SCORE_RETRY;
      95           0 :     if (nb_packets < 10)
      96           0 :         return AVPROBE_SCORE_EXTENSION;
      97           0 :     return AVPROBE_SCORE_MAX;
      98             : }
      99             : 
     100             : AVInputFormat ff_sup_demuxer = {
     101             :     .name           = "sup",
     102             :     .long_name      = NULL_IF_CONFIG_SMALL("raw HDMV Presentation Graphic Stream subtitles"),
     103             :     .extensions     = "sup",
     104             :     .mime_type      = "application/x-pgs",
     105             :     .read_probe     = sup_probe,
     106             :     .read_header    = sup_read_header,
     107             :     .read_packet    = sup_read_packet,
     108             :     .flags          = AVFMT_GENERIC_INDEX,
     109             : };

Generated by: LCOV version 1.13