LCOV - code coverage report
Current view: top level - libavformat - fsb.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3 125 2.4 %
Date: 2017-12-14 01:15:32 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*
       2             :  * FSB demuxer
       3             :  * Copyright (c) 2015 Paul B Mahol
       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/avassert.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "avformat.h"
      25             : #include "avio.h"
      26             : #include "internal.h"
      27             : 
      28        6130 : static int fsb_probe(AVProbeData *p)
      29             : {
      30        6130 :     if (memcmp(p->buf, "FSB", 3) || p->buf[3] - '0' < 1 || p->buf[3] - '0' > 5)
      31        6130 :         return 0;
      32           0 :     if (AV_RL32(p->buf + 4) != 1)
      33           0 :         return 0;
      34           0 :     return AVPROBE_SCORE_MAX;
      35             : }
      36             : 
      37           0 : static int fsb_read_header(AVFormatContext *s)
      38             : {
      39           0 :     AVIOContext *pb = s->pb;
      40             :     unsigned format, version, c;
      41             :     int64_t offset;
      42             :     AVCodecParameters *par;
      43           0 :     AVStream *st = avformat_new_stream(s, NULL);
      44             : 
      45           0 :     avio_skip(pb, 3); // "FSB"
      46           0 :     version = avio_r8(pb) - '0';
      47           0 :     if (version != 4 && version != 3) {
      48           0 :         avpriv_request_sample(s, "version %d", version);
      49           0 :         return AVERROR_PATCHWELCOME;
      50             :     }
      51             : 
      52           0 :     avio_skip(pb, 4);
      53             : 
      54           0 :     if (!st)
      55           0 :         return AVERROR(ENOMEM);
      56           0 :     par = st->codecpar;
      57           0 :     par->codec_type  = AVMEDIA_TYPE_AUDIO;
      58           0 :     par->codec_tag   = 0;
      59             : 
      60           0 :     if (version == 3) {
      61           0 :         offset = avio_rl32(pb) + 0x18;
      62           0 :         avio_skip(pb, 44);
      63           0 :         st->duration = avio_rl32(pb);
      64           0 :         avio_skip(pb, 12);
      65           0 :         format = avio_rl32(pb);
      66           0 :         par->sample_rate = avio_rl32(pb);
      67           0 :         if (par->sample_rate <= 0)
      68           0 :             return AVERROR_INVALIDDATA;
      69           0 :         avio_skip(pb, 6);
      70           0 :         par->channels    = avio_rl16(pb);
      71           0 :         if (!par->channels)
      72           0 :             return AVERROR_INVALIDDATA;
      73             : 
      74           0 :         if (format & 0x00000100) {
      75           0 :             par->codec_id    = AV_CODEC_ID_PCM_S16LE;
      76           0 :             par->block_align = 4096 * par->channels;
      77           0 :         } else if (format & 0x00400000) {
      78           0 :             par->bits_per_coded_sample = 4;
      79           0 :             par->codec_id    = AV_CODEC_ID_ADPCM_IMA_WAV;
      80           0 :             par->block_align = 36 * par->channels;
      81           0 :         } else if (format & 0x00800000) {
      82           0 :             par->codec_id    = AV_CODEC_ID_ADPCM_PSX;
      83           0 :             par->block_align = 16 * par->channels;
      84           0 :         } else if (format & 0x02000000) {
      85           0 :             par->codec_id    = AV_CODEC_ID_ADPCM_THP;
      86           0 :             par->block_align = 8 * par->channels;
      87           0 :             if (par->channels > INT_MAX / 32)
      88           0 :                 return AVERROR_INVALIDDATA;
      89           0 :             ff_alloc_extradata(par, 32 * par->channels);
      90           0 :             if (!par->extradata)
      91           0 :                 return AVERROR(ENOMEM);
      92           0 :             avio_seek(pb, 0x68, SEEK_SET);
      93           0 :             for (c = 0; c < par->channels; c++) {
      94           0 :                 avio_read(pb, par->extradata + 32 * c, 32);
      95           0 :                 avio_skip(pb, 14);
      96             :             }
      97             :         } else {
      98           0 :             avpriv_request_sample(s, "format 0x%X", format);
      99           0 :             return AVERROR_PATCHWELCOME;
     100             :         }
     101           0 :     } else if (version == 4) {
     102           0 :         offset = avio_rl32(pb) + 0x30;
     103           0 :         avio_skip(pb, 80);
     104           0 :         st->duration = avio_rl32(pb);
     105             : 
     106           0 :         format = avio_rb32(pb);
     107           0 :         switch(format) {
     108           0 :         case 0x40001001:
     109             :         case 0x00001005:
     110             :         case 0x40001081:
     111             :         case 0x40200001:
     112           0 :             par->codec_id = AV_CODEC_ID_XMA2;
     113           0 :             break;
     114           0 :         case 0x40000802:
     115           0 :             par->codec_id = AV_CODEC_ID_ADPCM_THP;
     116           0 :             break;
     117           0 :         default:
     118           0 :             avpriv_request_sample(s, "format 0x%X", format);
     119           0 :             return AVERROR_PATCHWELCOME;
     120             :         }
     121             : 
     122           0 :         par->sample_rate = avio_rl32(pb);
     123           0 :         if (par->sample_rate <= 0)
     124           0 :             return AVERROR_INVALIDDATA;
     125           0 :         avio_skip(pb, 6);
     126             : 
     127           0 :         par->channels    = avio_rl16(pb);
     128           0 :         if (!par->channels)
     129           0 :             return AVERROR_INVALIDDATA;
     130             : 
     131           0 :         switch (par->codec_id) {
     132           0 :         case AV_CODEC_ID_XMA2:
     133           0 :             ff_alloc_extradata(par, 34);
     134           0 :             if (!par->extradata)
     135           0 :                 return AVERROR(ENOMEM);
     136           0 :             memset(par->extradata, 0, 34);
     137           0 :             par->block_align = 2048;
     138           0 :             break;
     139           0 :         case AV_CODEC_ID_ADPCM_THP:
     140           0 :             if (par->channels > INT_MAX / 32)
     141           0 :                 return AVERROR_INVALIDDATA;
     142           0 :             ff_alloc_extradata(par, 32 * par->channels);
     143           0 :             if (!par->extradata)
     144           0 :                 return AVERROR(ENOMEM);
     145           0 :             avio_seek(pb, 0x80, SEEK_SET);
     146           0 :             for (c = 0; c < par->channels; c++) {
     147           0 :                 avio_read(pb, par->extradata + 32 * c, 32);
     148           0 :                 avio_skip(pb, 14);
     149             :             }
     150           0 :             par->block_align = 8 * par->channels;
     151           0 :             break;
     152             :         }
     153             :     } else {
     154           0 :         av_assert0(0);
     155             :     }
     156             : 
     157           0 :     avio_skip(pb, offset - avio_tell(pb));
     158           0 :     s->internal->data_offset = avio_tell(pb);
     159             : 
     160           0 :     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
     161             : 
     162           0 :     return 0;
     163             : }
     164             : 
     165           0 : static int fsb_read_packet(AVFormatContext *s, AVPacket *pkt)
     166             : {
     167           0 :     AVCodecParameters *par = s->streams[0]->codecpar;
     168             :     int64_t pos;
     169             :     int ret;
     170             : 
     171           0 :     if (avio_feof(s->pb))
     172           0 :         return AVERROR_EOF;
     173             : 
     174           0 :     pos = avio_tell(s->pb);
     175           0 :     if (par->codec_id == AV_CODEC_ID_ADPCM_THP &&
     176           0 :                par->channels > 1) {
     177             :         int i, ch;
     178             : 
     179           0 :         ret = av_new_packet(pkt, par->block_align);
     180           0 :         if (ret < 0)
     181           0 :             return ret;
     182           0 :         for (i = 0; i < 4; i++) {
     183           0 :             for (ch = 0; ch < par->channels; ch++) {
     184           0 :                 pkt->data[ch * 8 + i * 2 + 0] = avio_r8(s->pb);
     185           0 :                 pkt->data[ch * 8 + i * 2 + 1] = avio_r8(s->pb);
     186             :             }
     187             :         }
     188           0 :         ret = 0;
     189             :     } else {
     190           0 :         ret = av_get_packet(s->pb, pkt, par->block_align);
     191             :     }
     192             : 
     193           0 :     if (par->codec_id == AV_CODEC_ID_XMA2 && pkt->size >= 1)
     194           0 :         pkt->duration = (pkt->data[0] >> 2) * 512;
     195             : 
     196           0 :     pkt->pos = pos;
     197           0 :     pkt->stream_index = 0;
     198             : 
     199           0 :     return ret;
     200             : }
     201             : 
     202             : AVInputFormat ff_fsb_demuxer = {
     203             :     .name        = "fsb",
     204             :     .long_name   = NULL_IF_CONFIG_SMALL("FMOD Sample Bank"),
     205             :     .read_probe  = fsb_probe,
     206             :     .read_header = fsb_read_header,
     207             :     .read_packet = fsb_read_packet,
     208             :     .extensions  = "fsb",
     209             :     .flags       = AVFMT_GENERIC_INDEX,
     210             : };

Generated by: LCOV version 1.13