LCOV - code coverage report
Current view: top level - src/libavformat - 4xm.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 141 180 78.3 %
Date: 2017-01-24 04:42:20 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :  * 4X Technologies .4xm File Demuxer (no muxer)
       3             :  * Copyright (c) 2003  The FFmpeg project
       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             :  * 4X Technologies file demuxer
      25             :  * by Mike Melanson (melanson@pcisys.net)
      26             :  * for more information on the .4xm file format, visit:
      27             :  *   http://www.pcisys.net/~melanson/codecs/
      28             :  */
      29             : 
      30             : #include "libavutil/intreadwrite.h"
      31             : #include "libavutil/intfloat.h"
      32             : #include "avformat.h"
      33             : #include "internal.h"
      34             : 
      35             : #define     RIFF_TAG MKTAG('R', 'I', 'F', 'F')
      36             : #define  FOURXMV_TAG MKTAG('4', 'X', 'M', 'V')
      37             : #define     LIST_TAG MKTAG('L', 'I', 'S', 'T')
      38             : #define     HEAD_TAG MKTAG('H', 'E', 'A', 'D')
      39             : #define     TRK__TAG MKTAG('T', 'R', 'K', '_')
      40             : #define     MOVI_TAG MKTAG('M', 'O', 'V', 'I')
      41             : #define     VTRK_TAG MKTAG('V', 'T', 'R', 'K')
      42             : #define     STRK_TAG MKTAG('S', 'T', 'R', 'K')
      43             : #define     std__TAG MKTAG('s', 't', 'd', '_')
      44             : #define     name_TAG MKTAG('n', 'a', 'm', 'e')
      45             : #define     vtrk_TAG MKTAG('v', 't', 'r', 'k')
      46             : #define     strk_TAG MKTAG('s', 't', 'r', 'k')
      47             : #define     ifrm_TAG MKTAG('i', 'f', 'r', 'm')
      48             : #define     pfrm_TAG MKTAG('p', 'f', 'r', 'm')
      49             : #define     cfrm_TAG MKTAG('c', 'f', 'r', 'm')
      50             : #define     ifr2_TAG MKTAG('i', 'f', 'r', '2')
      51             : #define     pfr2_TAG MKTAG('p', 'f', 'r', '2')
      52             : #define     cfr2_TAG MKTAG('c', 'f', 'r', '2')
      53             : #define     snd__TAG MKTAG('s', 'n', 'd', '_')
      54             : 
      55             : #define vtrk_SIZE 0x44
      56             : #define strk_SIZE 0x28
      57             : 
      58             : #define GET_LIST_HEADER() \
      59             :     fourcc_tag = avio_rl32(pb); \
      60             :     size       = avio_rl32(pb); \
      61             :     if (fourcc_tag != LIST_TAG) \
      62             :         return AVERROR_INVALIDDATA; \
      63             :     fourcc_tag = avio_rl32(pb);
      64             : 
      65             : typedef struct AudioTrack {
      66             :     int sample_rate;
      67             :     int bits;
      68             :     int channels;
      69             :     int stream_index;
      70             :     int adpcm;
      71             :     int64_t audio_pts;
      72             : } AudioTrack;
      73             : 
      74             : typedef struct FourxmDemuxContext {
      75             :     int video_stream_index;
      76             :     int track_count;
      77             :     AudioTrack *tracks;
      78             : 
      79             :     int64_t video_pts;
      80             :     AVRational fps;
      81             : } FourxmDemuxContext;
      82             : 
      83        5563 : static int fourxm_probe(AVProbeData *p)
      84             : {
      85        6377 :     if ((AV_RL32(&p->buf[0]) != RIFF_TAG) ||
      86         814 :         (AV_RL32(&p->buf[8]) != FOURXMV_TAG))
      87        5560 :         return 0;
      88             : 
      89           3 :     return AVPROBE_SCORE_MAX;
      90             : }
      91             : 
      92           3 : static int parse_vtrk(AVFormatContext *s,
      93             :                       FourxmDemuxContext *fourxm, uint8_t *buf, int size,
      94             :                       int left)
      95             : {
      96             :     AVStream *st;
      97             :     /* check that there is enough data */
      98           3 :     if (size != vtrk_SIZE || left < size + 8) {
      99           0 :         return AVERROR_INVALIDDATA;
     100             :     }
     101             : 
     102             :     /* allocate a new AVStream */
     103           3 :     st = avformat_new_stream(s, NULL);
     104           3 :     if (!st)
     105           0 :         return AVERROR(ENOMEM);
     106             : 
     107           3 :     avpriv_set_pts_info(st, 60, fourxm->fps.den, fourxm->fps.num);
     108             : 
     109           3 :     fourxm->video_stream_index = st->index;
     110             : 
     111           3 :     st->codecpar->codec_type     = AVMEDIA_TYPE_VIDEO;
     112           3 :     st->codecpar->codec_id       = AV_CODEC_ID_4XM;
     113             : 
     114           3 :     st->codecpar->extradata      = av_mallocz(4 + AV_INPUT_BUFFER_PADDING_SIZE);
     115           3 :     if (!st->codecpar->extradata)
     116           0 :         return AVERROR(ENOMEM);
     117           3 :     st->codecpar->extradata_size = 4;
     118           3 :     AV_WL32(st->codecpar->extradata, AV_RL32(buf + 16));
     119           3 :     st->codecpar->width  = AV_RL32(buf + 36);
     120           3 :     st->codecpar->height = AV_RL32(buf + 40);
     121             : 
     122           3 :     return 0;
     123             : }
     124             : 
     125             : 
     126           8 : static int parse_strk(AVFormatContext *s,
     127             :                       FourxmDemuxContext *fourxm, uint8_t *buf, int size,
     128             :                       int left)
     129             : {
     130             :     AVStream *st;
     131             :     int track;
     132             :     /* check that there is enough data */
     133           8 :     if (size != strk_SIZE || left < size + 8)
     134           0 :         return AVERROR_INVALIDDATA;
     135             : 
     136           8 :     track = AV_RL32(buf + 8);
     137           8 :     if ((unsigned)track >= UINT_MAX / sizeof(AudioTrack) - 1) {
     138           0 :         av_log(s, AV_LOG_ERROR, "current_track too large\n");
     139           0 :         return AVERROR_INVALIDDATA;
     140             :     }
     141             : 
     142           8 :     if (track + 1 > fourxm->track_count) {
     143           8 :         if (av_reallocp_array(&fourxm->tracks, track + 1, sizeof(AudioTrack)))
     144           0 :             return AVERROR(ENOMEM);
     145           8 :         memset(&fourxm->tracks[fourxm->track_count], 0,
     146           8 :                sizeof(AudioTrack) * (track + 1 - fourxm->track_count));
     147           8 :         fourxm->track_count = track + 1;
     148             :     }
     149           8 :     fourxm->tracks[track].adpcm       = AV_RL32(buf + 12);
     150           8 :     fourxm->tracks[track].channels    = AV_RL32(buf + 36);
     151           8 :     fourxm->tracks[track].sample_rate = AV_RL32(buf + 40);
     152           8 :     fourxm->tracks[track].bits        = AV_RL32(buf + 44);
     153           8 :     fourxm->tracks[track].audio_pts   = 0;
     154             : 
     155          16 :     if (fourxm->tracks[track].channels    <= 0 ||
     156          16 :         fourxm->tracks[track].sample_rate <= 0 ||
     157           8 :         fourxm->tracks[track].bits        <= 0) {
     158           0 :         av_log(s, AV_LOG_ERROR, "audio header invalid\n");
     159           0 :         return AVERROR_INVALIDDATA;
     160             :     }
     161           8 :     if (!fourxm->tracks[track].adpcm && fourxm->tracks[track].bits<8) {
     162           0 :         av_log(s, AV_LOG_ERROR, "bits unspecified for non ADPCM\n");
     163           0 :         return AVERROR_INVALIDDATA;
     164             :     }
     165             : 
     166           8 :     if (fourxm->tracks[track].sample_rate > INT64_MAX / fourxm->tracks[track].bits / fourxm->tracks[track].channels) {
     167           0 :         av_log(s, AV_LOG_ERROR, "Overflow during bit rate calculation %d * %d * %d\n",
     168           0 :                fourxm->tracks[track].sample_rate, fourxm->tracks[track].bits, fourxm->tracks[track].channels);
     169           0 :         return AVERROR_INVALIDDATA;
     170             :     }
     171             : 
     172             :     /* allocate a new AVStream */
     173           8 :     st = avformat_new_stream(s, NULL);
     174           8 :     if (!st)
     175           0 :         return AVERROR(ENOMEM);
     176             : 
     177           8 :     st->id = track;
     178           8 :     avpriv_set_pts_info(st, 60, 1, fourxm->tracks[track].sample_rate);
     179             : 
     180           8 :     fourxm->tracks[track].stream_index = st->index;
     181             : 
     182           8 :     st->codecpar->codec_type            = AVMEDIA_TYPE_AUDIO;
     183           8 :     st->codecpar->codec_tag             = 0;
     184           8 :     st->codecpar->channels              = fourxm->tracks[track].channels;
     185           8 :     st->codecpar->sample_rate           = fourxm->tracks[track].sample_rate;
     186           8 :     st->codecpar->bits_per_coded_sample = fourxm->tracks[track].bits;
     187          24 :     st->codecpar->bit_rate              = (int64_t)st->codecpar->channels *
     188          16 :                                           st->codecpar->sample_rate *
     189           8 :                                           st->codecpar->bits_per_coded_sample;
     190          16 :     st->codecpar->block_align           = st->codecpar->channels *
     191           8 :                                           st->codecpar->bits_per_coded_sample;
     192             : 
     193           8 :     if (fourxm->tracks[track].adpcm){
     194           7 :         st->codecpar->codec_id = AV_CODEC_ID_ADPCM_4XM;
     195           1 :     } else if (st->codecpar->bits_per_coded_sample == 8) {
     196           0 :         st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
     197             :     } else
     198           1 :         st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
     199             : 
     200           8 :     return 0;
     201             : }
     202             : 
     203           3 : static int fourxm_read_header(AVFormatContext *s)
     204             : {
     205           3 :     AVIOContext *pb = s->pb;
     206             :     unsigned int fourcc_tag;
     207             :     unsigned int size;
     208             :     int header_size;
     209           3 :     FourxmDemuxContext *fourxm = s->priv_data;
     210             :     unsigned char *header;
     211             :     int i, ret;
     212             : 
     213           3 :     fourxm->track_count = 0;
     214           3 :     fourxm->tracks      = NULL;
     215           3 :     fourxm->fps         = (AVRational){1,1};
     216             : 
     217             :     /* skip the first 3 32-bit numbers */
     218           3 :     avio_skip(pb, 12);
     219             : 
     220             :     /* check for LIST-HEAD */
     221           3 :     GET_LIST_HEADER();
     222           3 :     header_size = size - 4;
     223           3 :     if (fourcc_tag != HEAD_TAG || header_size < 0)
     224           0 :         return AVERROR_INVALIDDATA;
     225             : 
     226             :     /* allocate space for the header and load the whole thing */
     227           3 :     header = av_malloc(header_size);
     228           3 :     if (!header)
     229           0 :         return AVERROR(ENOMEM);
     230           3 :     if (avio_read(pb, header, header_size) != header_size) {
     231           0 :         av_free(header);
     232           0 :         return AVERROR(EIO);
     233             :     }
     234             : 
     235             :     /* take the lazy approach and search for any and all vtrk and strk chunks */
     236         896 :     for (i = 0; i < header_size - 8; i++) {
     237         893 :         fourcc_tag = AV_RL32(&header[i]);
     238         893 :         size       = AV_RL32(&header[i + 4]);
     239         893 :         if (size > header_size - i - 8 && (fourcc_tag == vtrk_TAG || fourcc_tag == strk_TAG)) {
     240           0 :             av_log(s, AV_LOG_ERROR, "chunk larger than array %d>%d\n", size, header_size - i - 8);
     241           0 :             return AVERROR_INVALIDDATA;
     242             :         }
     243             : 
     244         893 :         if (fourcc_tag == std__TAG) {
     245           3 :             if (header_size - i < 16) {
     246           0 :                 av_log(s, AV_LOG_ERROR, "std TAG truncated\n");
     247           0 :                 ret = AVERROR_INVALIDDATA;
     248           0 :                 goto fail;
     249             :             }
     250           3 :             fourxm->fps = av_d2q(av_int2float(AV_RL32(&header[i + 12])), 10000);
     251         890 :         } else if (fourcc_tag == vtrk_TAG) {
     252           3 :             if ((ret = parse_vtrk(s, fourxm, header + i, size,
     253             :                                   header_size - i)) < 0)
     254           0 :                 goto fail;
     255             : 
     256           3 :             i += 8 + size;
     257         887 :         } else if (fourcc_tag == strk_TAG) {
     258           8 :             if ((ret = parse_strk(s, fourxm, header + i, size,
     259             :                                   header_size - i)) < 0)
     260           0 :                 goto fail;
     261             : 
     262           8 :             i += 8 + size;
     263             :         }
     264             :     }
     265             : 
     266             :     /* skip over the LIST-MOVI chunk (which is where the stream should be */
     267           3 :     GET_LIST_HEADER();
     268           3 :     if (fourcc_tag != MOVI_TAG) {
     269           0 :         ret = AVERROR_INVALIDDATA;
     270           0 :         goto fail;
     271             :     }
     272             : 
     273           3 :     av_free(header);
     274             :     /* initialize context members */
     275           3 :     fourxm->video_pts = -1;  /* first frame will push to 0 */
     276             : 
     277           3 :     return 0;
     278             : fail:
     279           0 :     av_freep(&fourxm->tracks);
     280           0 :     av_free(header);
     281           0 :     return ret;
     282             : }
     283             : 
     284         602 : static int fourxm_read_packet(AVFormatContext *s,
     285             :                               AVPacket *pkt)
     286             : {
     287         602 :     FourxmDemuxContext *fourxm = s->priv_data;
     288         602 :     AVIOContext *pb            = s->pb;
     289             :     unsigned int fourcc_tag;
     290             :     unsigned int size;
     291         602 :     int ret = 0;
     292             :     unsigned int track_number;
     293         602 :     int packet_read = 0;
     294             :     unsigned char header[8];
     295             :     int audio_frame_count;
     296             : 
     297        2019 :     while (!packet_read) {
     298         818 :         if ((ret = avio_read(s->pb, header, 8)) < 0)
     299           3 :             return ret;
     300         815 :         fourcc_tag = AV_RL32(&header[0]);
     301         815 :         size       = AV_RL32(&header[4]);
     302         815 :         if (avio_feof(pb))
     303           0 :             return AVERROR(EIO);
     304         815 :         switch (fourcc_tag) {
     305             :         case LIST_TAG:
     306             :             /* this is a good time to bump the video pts */
     307         216 :             fourxm->video_pts++;
     308             : 
     309             :             /* skip the LIST-* tag and move on to the next fourcc */
     310         216 :             avio_rl32(pb);
     311         216 :             break;
     312             : 
     313             :         case ifrm_TAG:
     314             :         case pfrm_TAG:
     315             :         case cfrm_TAG:
     316             :         case ifr2_TAG:
     317             :         case pfr2_TAG:
     318             :         case cfr2_TAG:
     319             :             /* allocate 8 more bytes than 'size' to account for fourcc
     320             :              * and size */
     321         251 :             if (size + 8 < size || av_new_packet(pkt, size + 8))
     322           0 :                 return AVERROR(EIO);
     323         251 :             pkt->stream_index = fourxm->video_stream_index;
     324         251 :             pkt->pts          = fourxm->video_pts;
     325         251 :             pkt->pos          = avio_tell(s->pb);
     326         251 :             memcpy(pkt->data, header, 8);
     327         251 :             ret = avio_read(s->pb, &pkt->data[8], size);
     328             : 
     329         251 :             if (ret < 0) {
     330           0 :                 av_packet_unref(pkt);
     331             :             } else {
     332         251 :                 packet_read = 1;
     333         251 :                 av_shrink_packet(pkt, ret + 8);
     334             :             }
     335         251 :             break;
     336             : 
     337             :         case snd__TAG:
     338         348 :             track_number = avio_rl32(pb);
     339         348 :             avio_skip(pb, 4);
     340         348 :             size -= 8;
     341             : 
     342         696 :             if (track_number < fourxm->track_count &&
     343         348 :                 fourxm->tracks[track_number].channels > 0) {
     344         348 :                 ret = av_get_packet(s->pb, pkt, size);
     345         348 :                 if (ret < 0)
     346           0 :                     return AVERROR(EIO);
     347         348 :                 pkt->stream_index =
     348         348 :                     fourxm->tracks[track_number].stream_index;
     349         348 :                 pkt->pts    = fourxm->tracks[track_number].audio_pts;
     350         348 :                 packet_read = 1;
     351             : 
     352             :                 /* pts accounting */
     353         348 :                 audio_frame_count = size;
     354         348 :                 if (fourxm->tracks[track_number].adpcm)
     355         333 :                     audio_frame_count -= 2 * (fourxm->tracks[track_number].channels);
     356         348 :                 audio_frame_count /= fourxm->tracks[track_number].channels;
     357         348 :                 if (fourxm->tracks[track_number].adpcm) {
     358         333 :                     audio_frame_count *= 2;
     359             :                 } else
     360          15 :                     audio_frame_count /=
     361          15 :                         (fourxm->tracks[track_number].bits / 8);
     362         348 :                 fourxm->tracks[track_number].audio_pts += audio_frame_count;
     363             :             } else {
     364           0 :                 avio_skip(pb, size);
     365             :             }
     366         348 :             break;
     367             : 
     368             :         default:
     369           0 :             avio_skip(pb, size);
     370           0 :             break;
     371             :         }
     372             :     }
     373         599 :     return ret;
     374             : }
     375             : 
     376           3 : static int fourxm_read_close(AVFormatContext *s)
     377             : {
     378           3 :     FourxmDemuxContext *fourxm = s->priv_data;
     379             : 
     380           3 :     av_freep(&fourxm->tracks);
     381             : 
     382           3 :     return 0;
     383             : }
     384             : 
     385             : AVInputFormat ff_fourxm_demuxer = {
     386             :     .name           = "4xm",
     387             :     .long_name      = NULL_IF_CONFIG_SMALL("4X Technologies"),
     388             :     .priv_data_size = sizeof(FourxmDemuxContext),
     389             :     .read_probe     = fourxm_probe,
     390             :     .read_header    = fourxm_read_header,
     391             :     .read_packet    = fourxm_read_packet,
     392             :     .read_close     = fourxm_read_close,
     393             : };

Generated by: LCOV version 1.12