LCOV - code coverage report
Current view: top level - libavformat - bfi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 77 88 87.5 %
Date: 2017-12-16 01:21:47 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Brute Force & Ignorance (BFI) demuxer
       3             :  * Copyright (c) 2008 Sisir Koppaka
       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             :  * @brief Brute Force & Ignorance (.bfi) file demuxer
      25             :  * @author Sisir Koppaka ( sisir.koppaka at gmail dot com )
      26             :  * @see http://wiki.multimedia.cx/index.php?title=BFI
      27             :  */
      28             : 
      29             : #include "libavutil/channel_layout.h"
      30             : #include "libavutil/intreadwrite.h"
      31             : #include "avformat.h"
      32             : #include "internal.h"
      33             : 
      34             : typedef struct BFIContext {
      35             :     int nframes;
      36             :     int audio_frame;
      37             :     int video_frame;
      38             :     int video_size;
      39             :     int avflag;
      40             : } BFIContext;
      41             : 
      42        6130 : static int bfi_probe(AVProbeData * p)
      43             : {
      44             :     /* Check file header */
      45        6130 :     if (AV_RL32(p->buf) == MKTAG('B', 'F', '&', 'I'))
      46           1 :         return AVPROBE_SCORE_MAX;
      47             :     else
      48        6129 :         return 0;
      49             : }
      50             : 
      51           1 : static int bfi_read_header(AVFormatContext * s)
      52             : {
      53           1 :     BFIContext *bfi = s->priv_data;
      54           1 :     AVIOContext *pb = s->pb;
      55             :     AVStream *vstream;
      56             :     AVStream *astream;
      57             :     int fps, chunk_header;
      58             : 
      59             :     /* Initialize the video codec... */
      60           1 :     vstream = avformat_new_stream(s, NULL);
      61           1 :     if (!vstream)
      62           0 :         return AVERROR(ENOMEM);
      63             : 
      64             :     /* Initialize the audio codec... */
      65           1 :     astream = avformat_new_stream(s, NULL);
      66           1 :     if (!astream)
      67           0 :         return AVERROR(ENOMEM);
      68             : 
      69             :     /* Set the total number of frames. */
      70           1 :     avio_skip(pb, 8);
      71           1 :     chunk_header           = avio_rl32(pb);
      72           1 :     bfi->nframes           = avio_rl32(pb);
      73           1 :     avio_rl32(pb);
      74           1 :     avio_rl32(pb);
      75           1 :     avio_rl32(pb);
      76           1 :     fps                    = avio_rl32(pb);
      77           1 :     avio_skip(pb, 12);
      78           1 :     vstream->codecpar->width  = avio_rl32(pb);
      79           1 :     vstream->codecpar->height = avio_rl32(pb);
      80             : 
      81             :     /*Load the palette to extradata */
      82           1 :     avio_skip(pb, 8);
      83           1 :     vstream->codecpar->extradata      = av_malloc(768);
      84           1 :     if (!vstream->codecpar->extradata)
      85           0 :         return AVERROR(ENOMEM);
      86           1 :     vstream->codecpar->extradata_size = 768;
      87           1 :     avio_read(pb, vstream->codecpar->extradata,
      88           1 :                vstream->codecpar->extradata_size);
      89             : 
      90           1 :     astream->codecpar->sample_rate = avio_rl32(pb);
      91           1 :     if (astream->codecpar->sample_rate <= 0) {
      92           0 :         av_log(s, AV_LOG_ERROR, "Invalid sample rate %d\n", astream->codecpar->sample_rate);
      93           0 :         return AVERROR_INVALIDDATA;
      94             :     }
      95             : 
      96             :     /* Set up the video codec... */
      97           1 :     avpriv_set_pts_info(vstream, 32, 1, fps);
      98           1 :     vstream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
      99           1 :     vstream->codecpar->codec_id   = AV_CODEC_ID_BFI;
     100           1 :     vstream->codecpar->format     = AV_PIX_FMT_PAL8;
     101           1 :     vstream->nb_frames            =
     102           1 :     vstream->duration             = bfi->nframes;
     103             : 
     104             :     /* Set up the audio codec now... */
     105           1 :     astream->codecpar->codec_type      = AVMEDIA_TYPE_AUDIO;
     106           1 :     astream->codecpar->codec_id        = AV_CODEC_ID_PCM_U8;
     107           1 :     astream->codecpar->channels        = 1;
     108           1 :     astream->codecpar->channel_layout  = AV_CH_LAYOUT_MONO;
     109           1 :     astream->codecpar->bits_per_coded_sample = 8;
     110           2 :     astream->codecpar->bit_rate        =
     111           2 :         (int64_t)astream->codecpar->sample_rate * astream->codecpar->bits_per_coded_sample;
     112           1 :     avio_seek(pb, chunk_header - 3, SEEK_SET);
     113           1 :     avpriv_set_pts_info(astream, 64, 1, astream->codecpar->sample_rate);
     114           1 :     return 0;
     115             : }
     116             : 
     117             : 
     118         115 : static int bfi_read_packet(AVFormatContext * s, AVPacket * pkt)
     119             : {
     120         115 :     BFIContext *bfi = s->priv_data;
     121         115 :     AVIOContext *pb = s->pb;
     122         115 :     int ret, audio_offset, video_offset, chunk_size, audio_size = 0;
     123         115 :     if (bfi->nframes == 0 || avio_feof(pb)) {
     124           1 :         return AVERROR_EOF;
     125             :     }
     126             : 
     127             :     /* If all previous chunks were completely read, then find a new one... */
     128         114 :     if (!bfi->avflag) {
     129          57 :         uint32_t state = 0;
     130         345 :         while(state != MKTAG('S','A','V','I')){
     131         231 :             if (avio_feof(pb))
     132           0 :                 return AVERROR(EIO);
     133         231 :             state = 256*state + avio_r8(pb);
     134             :         }
     135             :         /* Now that the chunk's location is confirmed, we proceed... */
     136          57 :         chunk_size      = avio_rl32(pb);
     137          57 :         avio_rl32(pb);
     138          57 :         audio_offset    = avio_rl32(pb);
     139          57 :         avio_rl32(pb);
     140          57 :         video_offset    = avio_rl32(pb);
     141          57 :         audio_size      = video_offset - audio_offset;
     142          57 :         bfi->video_size = chunk_size - video_offset;
     143          57 :         if (audio_size < 0 || bfi->video_size < 0) {
     144           0 :             av_log(s, AV_LOG_ERROR, "Invalid audio/video offsets or chunk size\n");
     145           0 :             return AVERROR_INVALIDDATA;
     146             :         }
     147             : 
     148             :         //Tossing an audio packet at the audio decoder.
     149          57 :         ret = av_get_packet(pb, pkt, audio_size);
     150          57 :         if (ret < 0)
     151           0 :             return ret;
     152             : 
     153          57 :         pkt->pts          = bfi->audio_frame;
     154          57 :         bfi->audio_frame += ret;
     155          57 :     } else if (bfi->video_size > 0) {
     156             : 
     157             :         //Tossing a video packet at the video decoder.
     158          57 :         ret = av_get_packet(pb, pkt, bfi->video_size);
     159          57 :         if (ret < 0)
     160           0 :             return ret;
     161             : 
     162          57 :         pkt->pts          = bfi->video_frame;
     163          57 :         bfi->video_frame += ret / bfi->video_size;
     164             : 
     165             :         /* One less frame to read. A cursory decrement. */
     166          57 :         bfi->nframes--;
     167             :     } else {
     168             :         /* Empty video packet */
     169           0 :         ret = AVERROR(EAGAIN);
     170             :     }
     171             : 
     172         114 :     bfi->avflag       = !bfi->avflag;
     173         114 :     pkt->stream_index = bfi->avflag;
     174         114 :     return ret;
     175             : }
     176             : 
     177             : AVInputFormat ff_bfi_demuxer = {
     178             :     .name           = "bfi",
     179             :     .long_name      = NULL_IF_CONFIG_SMALL("Brute Force & Ignorance"),
     180             :     .priv_data_size = sizeof(BFIContext),
     181             :     .read_probe     = bfi_probe,
     182             :     .read_header    = bfi_read_header,
     183             :     .read_packet    = bfi_read_packet,
     184             : };

Generated by: LCOV version 1.13