LCOV - code coverage report
Current view: top level - libavformat - iss.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 59 70 84.3 %
Date: 2017-12-16 13:57:32 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * ISS (.iss) file demuxer
       3             :  * Copyright (c) 2008 Jaikrishnan Menon <realityman@gmx.net>
       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             :  * Funcom ISS file demuxer
      25             :  * @author Jaikrishnan Menon
      26             :  * @see http://wiki.multimedia.cx/index.php?title=FunCom_ISS
      27             :  */
      28             : 
      29             : #include "libavutil/channel_layout.h"
      30             : #include "avformat.h"
      31             : #include "internal.h"
      32             : #include "libavutil/avstring.h"
      33             : 
      34             : #define ISS_SIG "IMA_ADPCM_Sound"
      35             : #define ISS_SIG_LEN 15
      36             : #define MAX_TOKEN_SIZE 20
      37             : 
      38             : typedef struct IssDemuxContext {
      39             :     int packet_size;
      40             :     int sample_start_pos;
      41             : } IssDemuxContext;
      42             : 
      43          10 : static void get_token(AVIOContext *s, char *buf, int maxlen)
      44             : {
      45          10 :     int i = 0;
      46             :     char c;
      47             : 
      48          68 :     while ((c = avio_r8(s))) {
      49          57 :         if(c == ' ')
      50           9 :             break;
      51          48 :         if (i < maxlen-1)
      52          48 :             buf[i++] = c;
      53             :     }
      54             : 
      55          10 :     if(!c)
      56           1 :         avio_r8(s);
      57             : 
      58          10 :     buf[i] = 0; /* Ensure null terminated, but may be truncated */
      59          10 : }
      60             : 
      61        6130 : static int iss_probe(AVProbeData *p)
      62             : {
      63        6130 :     if (strncmp(p->buf, ISS_SIG, ISS_SIG_LEN))
      64        6129 :         return 0;
      65             : 
      66           1 :     return AVPROBE_SCORE_MAX;
      67             : }
      68             : 
      69           1 : static av_cold int iss_read_header(AVFormatContext *s)
      70             : {
      71           1 :     IssDemuxContext *iss = s->priv_data;
      72           1 :     AVIOContext *pb = s->pb;
      73             :     AVStream *st;
      74             :     char token[MAX_TOKEN_SIZE];
      75             :     int stereo, rate_divisor;
      76             : 
      77           1 :     get_token(pb, token, sizeof(token)); //"IMA_ADPCM_Sound"
      78           1 :     get_token(pb, token, sizeof(token)); //packet size
      79           1 :     if (sscanf(token, "%d", &iss->packet_size) != 1) {
      80           0 :         av_log(s, AV_LOG_ERROR, "Failed parsing packet size\n");
      81           0 :         return AVERROR_INVALIDDATA;
      82             :     }
      83           1 :     get_token(pb, token, sizeof(token)); //File ID
      84           1 :     get_token(pb, token, sizeof(token)); //out size
      85           1 :     get_token(pb, token, sizeof(token)); //stereo
      86           1 :     if (sscanf(token, "%d", &stereo) != 1) {
      87           0 :         av_log(s, AV_LOG_ERROR, "Failed parsing stereo flag\n");
      88           0 :         return AVERROR_INVALIDDATA;
      89             :     }
      90           1 :     get_token(pb, token, sizeof(token)); //Unknown1
      91           1 :     get_token(pb, token, sizeof(token)); //RateDivisor
      92           1 :     if (sscanf(token, "%d", &rate_divisor) != 1) {
      93           0 :         av_log(s, AV_LOG_ERROR, "Failed parsing rate_divisor\n");
      94           0 :         return AVERROR_INVALIDDATA;
      95             :     }
      96           1 :     get_token(pb, token, sizeof(token)); //Unknown2
      97           1 :     get_token(pb, token, sizeof(token)); //Version ID
      98           1 :     get_token(pb, token, sizeof(token)); //Size
      99             : 
     100           1 :     if (iss->packet_size <= 0) {
     101           0 :         av_log(s, AV_LOG_ERROR, "packet_size %d is invalid\n", iss->packet_size);
     102           0 :         return AVERROR_INVALIDDATA;
     103             :     }
     104             : 
     105           1 :     iss->sample_start_pos = avio_tell(pb);
     106             : 
     107           1 :     st = avformat_new_stream(s, NULL);
     108           1 :     if (!st)
     109           0 :         return AVERROR(ENOMEM);
     110           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     111           1 :     st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_ISS;
     112           1 :     if (stereo) {
     113           0 :         st->codecpar->channels       = 2;
     114           0 :         st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
     115             :     } else {
     116           1 :         st->codecpar->channels       = 1;
     117           1 :         st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     118             :     }
     119           1 :     st->codecpar->sample_rate = 44100;
     120           1 :     if(rate_divisor > 0)
     121           1 :          st->codecpar->sample_rate /= rate_divisor;
     122           1 :     st->codecpar->bits_per_coded_sample = 4;
     123           3 :     st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate
     124           2 :                                       * st->codecpar->bits_per_coded_sample;
     125           1 :     st->codecpar->block_align = iss->packet_size;
     126           1 :     avpriv_set_pts_info(st, 32, 1, st->codecpar->sample_rate);
     127             : 
     128           1 :     return 0;
     129             : }
     130             : 
     131         120 : static int iss_read_packet(AVFormatContext *s, AVPacket *pkt)
     132             : {
     133         120 :     IssDemuxContext *iss = s->priv_data;
     134         120 :     int ret = av_get_packet(s->pb, pkt, iss->packet_size);
     135             : 
     136         120 :     if(ret != iss->packet_size)
     137           1 :         return AVERROR(EIO);
     138             : 
     139         119 :     pkt->stream_index = 0;
     140         119 :     pkt->pts = avio_tell(s->pb) - iss->sample_start_pos;
     141         119 :     if(s->streams[0]->codecpar->channels > 0)
     142         119 :         pkt->pts /= s->streams[0]->codecpar->channels*2;
     143         119 :     return 0;
     144             : }
     145             : 
     146             : AVInputFormat ff_iss_demuxer = {
     147             :     .name           = "iss",
     148             :     .long_name      = NULL_IF_CONFIG_SMALL("Funcom ISS"),
     149             :     .priv_data_size = sizeof(IssDemuxContext),
     150             :     .read_probe     = iss_probe,
     151             :     .read_header    = iss_read_header,
     152             :     .read_packet    = iss_read_packet,
     153             : };

Generated by: LCOV version 1.13