LCOV - code coverage report
Current view: top level - libavformat - pva.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 80 119 67.2 %
Date: 2017-12-16 13:57:32 Functions: 5 6 83.3 %

          Line data    Source code
       1             : /*
       2             :  * TechnoTrend PVA (.pva) demuxer
       3             :  * Copyright (c) 2007, 2008 Ivo van Poorten
       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 "avformat.h"
      23             : #include "internal.h"
      24             : #include "mpeg.h"
      25             : 
      26             : #define PVA_MAX_PAYLOAD_LENGTH  0x17f8
      27             : #define PVA_VIDEO_PAYLOAD       0x01
      28             : #define PVA_AUDIO_PAYLOAD       0x02
      29             : #define PVA_MAGIC               (('A' << 8) + 'V')
      30             : 
      31             : typedef struct PVAContext {
      32             :     int continue_pes;
      33             : } PVAContext;
      34             : 
      35        6131 : static int pva_check(const uint8_t *p) {
      36        6131 :     int length = AV_RB16(p + 6);
      37        6135 :     if (AV_RB16(p) != PVA_MAGIC || !p[2] || p[2] > 2 || p[4] != 0x55 ||
      38           8 :         (p[5] & 0xe0) || length > PVA_MAX_PAYLOAD_LENGTH)
      39        6127 :         return -1;
      40           4 :     return length + 8;
      41             : }
      42             : 
      43        6130 : static int pva_probe(AVProbeData * pd) {
      44        6130 :     const unsigned char *buf = pd->buf;
      45        6130 :     int len = pva_check(buf);
      46             : 
      47        6130 :     if (len < 0)
      48        6127 :         return 0;
      49             : 
      50           4 :     if (pd->buf_size >= len + 8 &&
      51           1 :         pva_check(buf + len) >= 0)
      52           1 :         return AVPROBE_SCORE_EXTENSION;
      53             : 
      54           2 :     return AVPROBE_SCORE_MAX / 4;
      55             : }
      56             : 
      57           1 : static int pva_read_header(AVFormatContext *s) {
      58             :     AVStream *st;
      59             : 
      60           1 :     if (!(st = avformat_new_stream(s, NULL)))
      61           0 :         return AVERROR(ENOMEM);
      62           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
      63           1 :     st->codecpar->codec_id   = AV_CODEC_ID_MPEG2VIDEO;
      64           1 :     st->need_parsing      = AVSTREAM_PARSE_FULL;
      65           1 :     avpriv_set_pts_info(st, 32, 1, 90000);
      66           1 :     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
      67             : 
      68           1 :     if (!(st = avformat_new_stream(s, NULL)))
      69           0 :         return AVERROR(ENOMEM);
      70           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
      71           1 :     st->codecpar->codec_id   = AV_CODEC_ID_MP2;
      72           1 :     st->need_parsing      = AVSTREAM_PARSE_FULL;
      73           1 :     avpriv_set_pts_info(st, 33, 1, 90000);
      74           1 :     av_add_index_entry(st, 0, 0, 0, 0, AVINDEX_KEYFRAME);
      75             : 
      76             :     /* the parameters will be extracted from the compressed bitstream */
      77           1 :     return 0;
      78             : }
      79             : 
      80             : #define pva_log if (read_packet) av_log
      81             : 
      82          91 : static int read_part_of_packet(AVFormatContext *s, int64_t *pts,
      83             :                                int *len, int *strid, int read_packet) {
      84          91 :     AVIOContext *pb = s->pb;
      85          91 :     PVAContext *pvactx = s->priv_data;
      86             :     int syncword, streamid, reserved, flags, length, pts_flag;
      87          91 :     int64_t pva_pts = AV_NOPTS_VALUE, startpos;
      88             :     int ret;
      89             : 
      90          91 : recover:
      91          91 :     startpos = avio_tell(pb);
      92             : 
      93          91 :     syncword = avio_rb16(pb);
      94          91 :     streamid = avio_r8(pb);
      95          91 :     avio_r8(pb);               /* counter not used */
      96          91 :     reserved = avio_r8(pb);
      97          91 :     flags    = avio_r8(pb);
      98          91 :     length   = avio_rb16(pb);
      99             : 
     100          91 :     pts_flag = flags & 0x10;
     101             : 
     102          91 :     if (syncword != PVA_MAGIC) {
     103           0 :         pva_log(s, AV_LOG_ERROR, "invalid syncword\n");
     104           0 :         return AVERROR(EIO);
     105             :     }
     106          91 :     if (streamid != PVA_VIDEO_PAYLOAD && streamid != PVA_AUDIO_PAYLOAD) {
     107           0 :         pva_log(s, AV_LOG_ERROR, "invalid streamid\n");
     108           0 :         return AVERROR(EIO);
     109             :     }
     110          91 :     if (reserved != 0x55) {
     111           0 :         pva_log(s, AV_LOG_WARNING, "expected reserved byte to be 0x55\n");
     112             :     }
     113          91 :     if (length > PVA_MAX_PAYLOAD_LENGTH) {
     114           0 :         pva_log(s, AV_LOG_ERROR, "invalid payload length %u\n", length);
     115           0 :         return AVERROR(EIO);
     116             :     }
     117             : 
     118          91 :     if (streamid == PVA_VIDEO_PAYLOAD && pts_flag) {
     119          23 :         pva_pts = avio_rb32(pb);
     120          23 :         length -= 4;
     121          68 :     } else if (streamid == PVA_AUDIO_PAYLOAD) {
     122             :         /* PVA Audio Packets either start with a signaled PES packet or
     123             :          * are a continuation of the previous PES packet. New PES packets
     124             :          * always start at the beginning of a PVA Packet, never somewhere in
     125             :          * the middle. */
     126           9 :         if (!pvactx->continue_pes) {
     127             :             int pes_signal, pes_header_data_length, pes_packet_length,
     128             :                 pes_flags;
     129             :             unsigned char pes_header_data[256];
     130             : 
     131           9 :             pes_signal             = avio_rb24(pb);
     132           9 :             avio_r8(pb);
     133           9 :             pes_packet_length      = avio_rb16(pb);
     134           9 :             pes_flags              = avio_rb16(pb);
     135           9 :             pes_header_data_length = avio_r8(pb);
     136             : 
     137           9 :             if (pes_signal != 1 || pes_header_data_length == 0) {
     138           0 :                 pva_log(s, AV_LOG_WARNING, "expected non empty signaled PES packet, "
     139             :                                           "trying to recover\n");
     140           0 :                 avio_skip(pb, length - 9);
     141           0 :                 if (!read_packet)
     142           0 :                     return AVERROR(EIO);
     143           0 :                 goto recover;
     144             :             }
     145             : 
     146           9 :             ret = avio_read(pb, pes_header_data, pes_header_data_length);
     147           9 :             if (ret != pes_header_data_length)
     148           0 :                 return ret < 0 ? ret : AVERROR_INVALIDDATA;
     149           9 :             length -= 9 + pes_header_data_length;
     150             : 
     151           9 :             pes_packet_length -= 3 + pes_header_data_length;
     152             : 
     153           9 :             pvactx->continue_pes = pes_packet_length;
     154             : 
     155           9 :             if (pes_flags & 0x80 && (pes_header_data[0] & 0xf0) == 0x20) {
     156           9 :                 if (pes_header_data_length < 5) {
     157           0 :                     pva_log(s, AV_LOG_ERROR, "header too short\n");
     158           0 :                     avio_skip(pb, length);
     159           0 :                     return AVERROR_INVALIDDATA;
     160             :                 }
     161           9 :                 pva_pts = ff_parse_pes_pts(pes_header_data);
     162             :             }
     163             :         }
     164             : 
     165           9 :         pvactx->continue_pes -= length;
     166             : 
     167           9 :         if (pvactx->continue_pes < 0) {
     168           0 :             pva_log(s, AV_LOG_WARNING, "audio data corruption\n");
     169           0 :             pvactx->continue_pes = 0;
     170             :         }
     171             :     }
     172             : 
     173          91 :     if (pva_pts != AV_NOPTS_VALUE)
     174          32 :         av_add_index_entry(s->streams[streamid-1], startpos, pva_pts, 0, 0, AVINDEX_KEYFRAME);
     175             : 
     176          91 :     *pts   = pva_pts;
     177          91 :     *len   = length;
     178          91 :     *strid = streamid;
     179          91 :     return 0;
     180             : }
     181             : 
     182          91 : static int pva_read_packet(AVFormatContext *s, AVPacket *pkt) {
     183          91 :     AVIOContext *pb = s->pb;
     184             :     int64_t pva_pts;
     185             :     int ret, length, streamid;
     186             : 
     187         182 :     if (read_part_of_packet(s, &pva_pts, &length, &streamid, 1) < 0 ||
     188          91 :        (ret = av_get_packet(pb, pkt, length)) <= 0)
     189           0 :         return AVERROR(EIO);
     190             : 
     191          91 :     pkt->stream_index = streamid - 1;
     192          91 :     pkt->pts = pva_pts;
     193             : 
     194          91 :     return ret;
     195             : }
     196             : 
     197           0 : static int64_t pva_read_timestamp(struct AVFormatContext *s, int stream_index,
     198             :                                           int64_t *pos, int64_t pos_limit) {
     199           0 :     AVIOContext *pb = s->pb;
     200           0 :     PVAContext *pvactx = s->priv_data;
     201             :     int length, streamid;
     202           0 :     int64_t res = AV_NOPTS_VALUE;
     203             : 
     204           0 :     pos_limit = FFMIN(*pos+PVA_MAX_PAYLOAD_LENGTH*8, (uint64_t)*pos+pos_limit);
     205             : 
     206           0 :     while (*pos < pos_limit) {
     207           0 :         res = AV_NOPTS_VALUE;
     208           0 :         avio_seek(pb, *pos, SEEK_SET);
     209             : 
     210           0 :         pvactx->continue_pes = 0;
     211           0 :         if (read_part_of_packet(s, &res, &length, &streamid, 0)) {
     212           0 :             (*pos)++;
     213           0 :             continue;
     214             :         }
     215           0 :         if (streamid - 1 != stream_index || res == AV_NOPTS_VALUE) {
     216           0 :             *pos = avio_tell(pb) + length;
     217           0 :             continue;
     218             :         }
     219           0 :         break;
     220             :     }
     221             : 
     222           0 :     pvactx->continue_pes = 0;
     223           0 :     return res;
     224             : }
     225             : 
     226             : AVInputFormat ff_pva_demuxer = {
     227             :     .name           = "pva",
     228             :     .long_name      = NULL_IF_CONFIG_SMALL("TechnoTrend PVA"),
     229             :     .priv_data_size = sizeof(PVAContext),
     230             :     .read_probe     = pva_probe,
     231             :     .read_header    = pva_read_header,
     232             :     .read_packet    = pva_read_packet,
     233             :     .read_timestamp = pva_read_timestamp,
     234             : };

Generated by: LCOV version 1.13