LCOV - code coverage report
Current view: top level - libavformat - thp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 88 99 88.9 %
Date: 2017-12-17 23:02:56 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * THP Demuxer
       3             :  * Copyright (c) 2007 Marco Gerards
       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 "libavutil/intreadwrite.h"
      23             : #include "libavutil/intfloat.h"
      24             : #include "avformat.h"
      25             : #include "internal.h"
      26             : 
      27             : typedef struct ThpDemuxContext {
      28             :     int              version;
      29             :     unsigned         first_frame;
      30             :     unsigned         first_framesz;
      31             :     unsigned         last_frame;
      32             :     int              compoff;
      33             :     unsigned         framecnt;
      34             :     AVRational       fps;
      35             :     unsigned         frame;
      36             :     int64_t          next_frame;
      37             :     unsigned         next_framesz;
      38             :     int              video_stream_index;
      39             :     int              audio_stream_index;
      40             :     int              compcount;
      41             :     unsigned char    components[16];
      42             :     AVStream*        vst;
      43             :     int              has_audio;
      44             :     unsigned         audiosize;
      45             : } ThpDemuxContext;
      46             : 
      47             : 
      48        6130 : static int thp_probe(AVProbeData *p)
      49             : {
      50             :     double d;
      51             :     /* check file header */
      52        6130 :     if (AV_RL32(p->buf) != MKTAG('T', 'H', 'P', '\0'))
      53        6128 :         return 0;
      54             : 
      55           2 :     d = av_int2float(AV_RB32(p->buf + 16));
      56           2 :     if (d < 0.1 || d > 1000 || isnan(d))
      57           0 :         return AVPROBE_SCORE_MAX/4;
      58             : 
      59           2 :     return AVPROBE_SCORE_MAX;
      60             : }
      61             : 
      62           2 : static int thp_read_header(AVFormatContext *s)
      63             : {
      64           2 :     ThpDemuxContext *thp = s->priv_data;
      65             :     AVStream *st;
      66           2 :     AVIOContext *pb = s->pb;
      67           2 :     int64_t fsize= avio_size(pb);
      68             :     int i;
      69             : 
      70             :     /* Read the file header.  */
      71           2 :                            avio_rb32(pb); /* Skip Magic.  */
      72           2 :     thp->version         = avio_rb32(pb);
      73             : 
      74           2 :                            avio_rb32(pb); /* Max buf size.  */
      75           2 :                            avio_rb32(pb); /* Max samples.  */
      76             : 
      77           2 :     thp->fps             = av_d2q(av_int2float(avio_rb32(pb)), INT_MAX);
      78           2 :     thp->framecnt        = avio_rb32(pb);
      79           2 :     thp->first_framesz   = avio_rb32(pb);
      80           2 :     pb->maxsize          = avio_rb32(pb);
      81           2 :     if(fsize>0 && (!pb->maxsize || fsize < pb->maxsize))
      82           2 :         pb->maxsize= fsize;
      83             : 
      84           2 :     thp->compoff         = avio_rb32(pb);
      85           2 :                            avio_rb32(pb); /* offsetDataOffset.  */
      86           2 :     thp->first_frame     = avio_rb32(pb);
      87           2 :     thp->last_frame      = avio_rb32(pb);
      88             : 
      89           2 :     thp->next_framesz    = thp->first_framesz;
      90           2 :     thp->next_frame      = thp->first_frame;
      91             : 
      92             :     /* Read the component structure.  */
      93           2 :     avio_seek (pb, thp->compoff, SEEK_SET);
      94           2 :     thp->compcount       = avio_rb32(pb);
      95             : 
      96             :     /* Read the list of component types.  */
      97           2 :     avio_read(pb, thp->components, 16);
      98             : 
      99           6 :     for (i = 0; i < thp->compcount; i++) {
     100           4 :         if (thp->components[i] == 0) {
     101           2 :             if (thp->vst)
     102           0 :                 break;
     103             : 
     104             :             /* Video component.  */
     105           2 :             st = avformat_new_stream(s, NULL);
     106           2 :             if (!st)
     107           0 :                 return AVERROR(ENOMEM);
     108             : 
     109             :             /* The denominator and numerator are switched because 1/fps
     110             :                is required.  */
     111           2 :             avpriv_set_pts_info(st, 64, thp->fps.den, thp->fps.num);
     112           2 :             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     113           2 :             st->codecpar->codec_id = AV_CODEC_ID_THP;
     114           2 :             st->codecpar->codec_tag = 0;  /* no fourcc */
     115           2 :             st->codecpar->width = avio_rb32(pb);
     116           2 :             st->codecpar->height = avio_rb32(pb);
     117           2 :             st->codecpar->sample_rate = av_q2d(thp->fps);
     118           2 :             st->nb_frames =
     119           2 :             st->duration = thp->framecnt;
     120           2 :             thp->vst = st;
     121           2 :             thp->video_stream_index = st->index;
     122             : 
     123           2 :             if (thp->version == 0x11000)
     124           2 :                 avio_rb32(pb); /* Unknown.  */
     125           2 :         } else if (thp->components[i] == 1) {
     126           2 :             if (thp->has_audio != 0)
     127           0 :                 break;
     128             : 
     129             :             /* Audio component.  */
     130           2 :             st = avformat_new_stream(s, NULL);
     131           2 :             if (!st)
     132           0 :                 return AVERROR(ENOMEM);
     133             : 
     134           2 :             st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     135           2 :             st->codecpar->codec_id = AV_CODEC_ID_ADPCM_THP;
     136           2 :             st->codecpar->codec_tag = 0;  /* no fourcc */
     137           2 :             st->codecpar->channels    = avio_rb32(pb); /* numChannels.  */
     138           2 :             st->codecpar->sample_rate = avio_rb32(pb); /* Frequency.  */
     139           2 :             st->duration           = avio_rb32(pb);
     140             : 
     141           2 :             avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
     142             : 
     143           2 :             thp->audio_stream_index = st->index;
     144           2 :             thp->has_audio = 1;
     145             :         }
     146             :     }
     147             : 
     148           2 :     return 0;
     149             : }
     150             : 
     151         288 : static int thp_read_packet(AVFormatContext *s,
     152             :                             AVPacket *pkt)
     153             : {
     154         288 :     ThpDemuxContext *thp = s->priv_data;
     155         288 :     AVIOContext *pb = s->pb;
     156             :     unsigned int size;
     157             :     int ret;
     158             : 
     159         288 :     if (thp->audiosize == 0) {
     160             :         /* Terminate when last frame is reached.  */
     161         144 :         if (thp->frame >= thp->framecnt)
     162           0 :             return AVERROR_EOF;
     163             : 
     164         144 :         avio_seek(pb, thp->next_frame, SEEK_SET);
     165             : 
     166             :         /* Locate the next frame and read out its size.  */
     167         144 :         thp->next_frame += FFMAX(thp->next_framesz, 1);
     168         144 :         thp->next_framesz = avio_rb32(pb);
     169             : 
     170         144 :                         avio_rb32(pb); /* Previous total size.  */
     171         144 :         size          = avio_rb32(pb); /* Total size of this frame.  */
     172             : 
     173             :         /* Store the audiosize so the next time this function is called,
     174             :            the audio can be read.  */
     175         144 :         if (thp->has_audio)
     176         144 :             thp->audiosize = avio_rb32(pb); /* Audio size.  */
     177             :         else
     178           0 :             thp->frame++;
     179             : 
     180         144 :         ret = av_get_packet(pb, pkt, size);
     181         144 :         if (ret < 0)
     182           0 :             return ret;
     183         144 :         if (ret != size) {
     184           0 :             av_packet_unref(pkt);
     185           0 :             return AVERROR(EIO);
     186             :         }
     187             : 
     188         144 :         pkt->stream_index = thp->video_stream_index;
     189             :     } else {
     190         144 :         ret = av_get_packet(pb, pkt, thp->audiosize);
     191         144 :         if (ret < 0)
     192           0 :             return ret;
     193         144 :         if (ret != thp->audiosize) {
     194           2 :             av_packet_unref(pkt);
     195           2 :             return AVERROR(EIO);
     196             :         }
     197             : 
     198         142 :         pkt->stream_index = thp->audio_stream_index;
     199         142 :         if (thp->audiosize >= 8)
     200         142 :             pkt->duration = AV_RB32(&pkt->data[4]);
     201             : 
     202         142 :         thp->audiosize = 0;
     203         142 :         thp->frame++;
     204             :     }
     205             : 
     206         286 :     return 0;
     207             : }
     208             : 
     209             : AVInputFormat ff_thp_demuxer = {
     210             :     .name           = "thp",
     211             :     .long_name      = NULL_IF_CONFIG_SMALL("THP"),
     212             :     .priv_data_size = sizeof(ThpDemuxContext),
     213             :     .read_probe     = thp_probe,
     214             :     .read_header    = thp_read_header,
     215             :     .read_packet    = thp_read_packet
     216             : };

Generated by: LCOV version 1.13