LCOV - code coverage report
Current view: top level - libavformat - idroqdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 95 113 84.1 %
Date: 2017-12-17 04:34:43 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * id RoQ (.roq) File Demuxer
       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             :  * id RoQ format file demuxer
      25             :  * by Mike Melanson (melanson@pcisys.net)
      26             :  * for more information on the .roq file format, visit:
      27             :  *   http://www.csse.monash.edu.au/~timf/
      28             :  */
      29             : 
      30             : #include "libavutil/channel_layout.h"
      31             : #include "libavutil/intreadwrite.h"
      32             : #include "avformat.h"
      33             : #include "internal.h"
      34             : #include "avio_internal.h"
      35             : 
      36             : #define RoQ_MAGIC_NUMBER 0x1084
      37             : #define RoQ_CHUNK_PREAMBLE_SIZE 8
      38             : #define RoQ_AUDIO_SAMPLE_RATE 22050
      39             : #define RoQ_CHUNKS_TO_SCAN 30
      40             : 
      41             : #define RoQ_INFO           0x1001
      42             : #define RoQ_QUAD_CODEBOOK  0x1002
      43             : #define RoQ_QUAD_VQ        0x1011
      44             : #define RoQ_SOUND_MONO     0x1020
      45             : #define RoQ_SOUND_STEREO   0x1021
      46             : 
      47             : typedef struct RoqDemuxContext {
      48             : 
      49             :     int frame_rate;
      50             :     int width;
      51             :     int height;
      52             :     int audio_channels;
      53             : 
      54             :     int video_stream_index;
      55             :     int audio_stream_index;
      56             : 
      57             :     int64_t video_pts;
      58             :     unsigned int audio_frame_count;
      59             : 
      60             : } RoqDemuxContext;
      61             : 
      62        6130 : static int roq_probe(AVProbeData *p)
      63             : {
      64        6137 :     if ((AV_RL16(&p->buf[0]) != RoQ_MAGIC_NUMBER) ||
      65           7 :         (AV_RL32(&p->buf[2]) != 0xFFFFFFFF))
      66        6123 :         return 0;
      67             : 
      68           7 :     return AVPROBE_SCORE_MAX;
      69             : }
      70             : 
      71           7 : static int roq_read_header(AVFormatContext *s)
      72             : {
      73           7 :     RoqDemuxContext *roq = s->priv_data;
      74           7 :     AVIOContext *pb = s->pb;
      75             :     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
      76             : 
      77             :     /* get the main header */
      78           7 :     if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
      79             :         RoQ_CHUNK_PREAMBLE_SIZE)
      80           0 :         return AVERROR(EIO);
      81           7 :     roq->frame_rate = AV_RL16(&preamble[6]);
      82             : 
      83             :     /* init private context parameters */
      84           7 :     roq->width = roq->height = roq->audio_channels = roq->video_pts =
      85           7 :     roq->audio_frame_count = 0;
      86           7 :     roq->audio_stream_index = -1;
      87           7 :     roq->video_stream_index = -1;
      88             : 
      89           7 :     s->ctx_flags |= AVFMTCTX_NOHEADER;
      90             : 
      91           7 :     return 0;
      92             : }
      93             : 
      94        1113 : static int roq_read_packet(AVFormatContext *s,
      95             :                            AVPacket *pkt)
      96             : {
      97        1113 :     RoqDemuxContext *roq = s->priv_data;
      98        1113 :     AVIOContext *pb = s->pb;
      99        1113 :     int ret = 0;
     100             :     unsigned int chunk_size;
     101             :     unsigned int chunk_type;
     102             :     unsigned int codebook_size;
     103             :     unsigned char preamble[RoQ_CHUNK_PREAMBLE_SIZE];
     104        1113 :     int packet_read = 0;
     105             :     int64_t codebook_offset;
     106             : 
     107        3335 :     while (!packet_read) {
     108             : 
     109        1145 :         if (avio_feof(s->pb))
     110           3 :             return AVERROR(EIO);
     111             : 
     112             :         /* get the next chunk preamble */
     113        1142 :         if ((ret = avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE)) !=
     114             :             RoQ_CHUNK_PREAMBLE_SIZE)
     115          33 :             return AVERROR(EIO);
     116             : 
     117        1109 :         chunk_type = AV_RL16(&preamble[0]);
     118        1109 :         chunk_size = AV_RL32(&preamble[2]);
     119        1109 :         if(chunk_size > INT_MAX)
     120           0 :             return AVERROR_INVALIDDATA;
     121             : 
     122        1109 :         chunk_size = ffio_limit(pb, chunk_size);
     123             : 
     124        1109 :         switch (chunk_type) {
     125             : 
     126          32 :         case RoQ_INFO:
     127          32 :             if (roq->video_stream_index == -1) {
     128           6 :                 AVStream *st = avformat_new_stream(s, NULL);
     129           6 :                 if (!st)
     130           0 :                     return AVERROR(ENOMEM);
     131           6 :                 avpriv_set_pts_info(st, 63, 1, roq->frame_rate);
     132           6 :                 roq->video_stream_index = st->index;
     133           6 :                 st->codecpar->codec_type   = AVMEDIA_TYPE_VIDEO;
     134           6 :                 st->codecpar->codec_id     = AV_CODEC_ID_ROQ;
     135           6 :                 st->codecpar->codec_tag    = 0;  /* no fourcc */
     136             : 
     137           6 :                 if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) != RoQ_CHUNK_PREAMBLE_SIZE)
     138           0 :                     return AVERROR(EIO);
     139           6 :                 st->codecpar->width  = roq->width  = AV_RL16(preamble);
     140           6 :                 st->codecpar->height = roq->height = AV_RL16(preamble + 2);
     141           6 :                 break;
     142             :             }
     143             :             /* don't care about this chunk anymore */
     144          26 :             avio_skip(pb, RoQ_CHUNK_PREAMBLE_SIZE);
     145          26 :             break;
     146             : 
     147         526 :         case RoQ_QUAD_CODEBOOK:
     148         526 :             if (roq->video_stream_index < 0)
     149           0 :                 return AVERROR_INVALIDDATA;
     150             :             /* packet needs to contain both this codebook and next VQ chunk */
     151         526 :             codebook_offset = avio_tell(pb) - RoQ_CHUNK_PREAMBLE_SIZE;
     152         526 :             codebook_size = chunk_size;
     153         526 :             avio_skip(pb, codebook_size);
     154         526 :             if (avio_read(pb, preamble, RoQ_CHUNK_PREAMBLE_SIZE) !=
     155             :                 RoQ_CHUNK_PREAMBLE_SIZE)
     156           0 :                 return AVERROR(EIO);
     157         526 :             chunk_size = AV_RL32(&preamble[2]) + RoQ_CHUNK_PREAMBLE_SIZE * 2 +
     158             :                 codebook_size;
     159             : 
     160         526 :             if (chunk_size > INT_MAX)
     161           0 :                 return AVERROR_INVALIDDATA;
     162             : 
     163             :             /* rewind */
     164         526 :             avio_seek(pb, codebook_offset, SEEK_SET);
     165             : 
     166             :             /* load up the packet */
     167         526 :             ret= av_get_packet(pb, pkt, chunk_size);
     168         526 :             if (ret != chunk_size)
     169           0 :                 return AVERROR(EIO);
     170         526 :             pkt->stream_index = roq->video_stream_index;
     171         526 :             pkt->pts = roq->video_pts++;
     172             : 
     173         526 :             packet_read = 1;
     174         526 :             break;
     175             : 
     176         507 :         case RoQ_SOUND_MONO:
     177             :         case RoQ_SOUND_STEREO:
     178         507 :             if (roq->audio_stream_index == -1) {
     179           3 :                 AVStream *st = avformat_new_stream(s, NULL);
     180           3 :                 if (!st)
     181           0 :                     return AVERROR(ENOMEM);
     182           3 :                 avpriv_set_pts_info(st, 32, 1, RoQ_AUDIO_SAMPLE_RATE);
     183           3 :                 roq->audio_stream_index = st->index;
     184           3 :                 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     185           3 :                 st->codecpar->codec_id = AV_CODEC_ID_ROQ_DPCM;
     186           3 :                 st->codecpar->codec_tag = 0;  /* no tag */
     187           3 :                 if (chunk_type == RoQ_SOUND_STEREO) {
     188           3 :                     st->codecpar->channels       = 2;
     189           3 :                     st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
     190             :                 } else {
     191           0 :                     st->codecpar->channels       = 1;
     192           0 :                     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     193             :                 }
     194           3 :                 roq->audio_channels    = st->codecpar->channels;
     195           3 :                 st->codecpar->sample_rate = RoQ_AUDIO_SAMPLE_RATE;
     196           3 :                 st->codecpar->bits_per_coded_sample = 16;
     197           6 :                 st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
     198           3 :                     st->codecpar->bits_per_coded_sample;
     199           3 :                 st->codecpar->block_align = st->codecpar->channels * st->codecpar->bits_per_coded_sample;
     200             :             }
     201             :         case RoQ_QUAD_VQ:
     202         551 :             if (chunk_type == RoQ_QUAD_VQ) {
     203          44 :                 if (roq->video_stream_index < 0)
     204           0 :                     return AVERROR_INVALIDDATA;
     205             :             }
     206             : 
     207             :             /* load up the packet */
     208         551 :             if (av_new_packet(pkt, chunk_size + RoQ_CHUNK_PREAMBLE_SIZE))
     209           0 :                 return AVERROR(EIO);
     210             :             /* copy over preamble */
     211         551 :             memcpy(pkt->data, preamble, RoQ_CHUNK_PREAMBLE_SIZE);
     212             : 
     213         551 :             if (chunk_type == RoQ_QUAD_VQ) {
     214          44 :                 pkt->stream_index = roq->video_stream_index;
     215          44 :                 pkt->pts = roq->video_pts++;
     216             :             } else {
     217         507 :                 pkt->stream_index = roq->audio_stream_index;
     218         507 :                 pkt->pts = roq->audio_frame_count;
     219         507 :                 roq->audio_frame_count += (chunk_size / roq->audio_channels);
     220             :             }
     221             : 
     222         551 :             pkt->pos= avio_tell(pb);
     223         551 :             ret = avio_read(pb, pkt->data + RoQ_CHUNK_PREAMBLE_SIZE,
     224             :                 chunk_size);
     225         551 :             if (ret != chunk_size) {
     226           0 :                 av_packet_unref(pkt);
     227           0 :                 ret = AVERROR(EIO);
     228             :             }
     229             : 
     230         551 :             packet_read = 1;
     231         551 :             break;
     232             : 
     233           0 :         default:
     234           0 :             av_log(s, AV_LOG_ERROR, "  unknown RoQ chunk (%04X)\n", chunk_type);
     235           0 :             return AVERROR_INVALIDDATA;
     236             :         }
     237             :     }
     238             : 
     239        1077 :     return ret;
     240             : }
     241             : 
     242             : AVInputFormat ff_roq_demuxer = {
     243             :     .name           = "roq",
     244             :     .long_name      = NULL_IF_CONFIG_SMALL("id RoQ"),
     245             :     .priv_data_size = sizeof(RoqDemuxContext),
     246             :     .read_probe     = roq_probe,
     247             :     .read_header    = roq_read_header,
     248             :     .read_packet    = roq_read_packet,
     249             : };

Generated by: LCOV version 1.13