LCOV - code coverage report
Current view: top level - src/libavformat - dsicin.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 93 108 86.1 %
Date: 2017-09-22 10:31:19 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Delphine Software International CIN File Demuxer
       3             :  * Copyright (c) 2006 Gregory Montoir (cyx@users.sourceforge.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             :  * Delphine Software International CIN file demuxer
      25             :  */
      26             : 
      27             : #include "libavutil/channel_layout.h"
      28             : #include "libavutil/intreadwrite.h"
      29             : #include "avformat.h"
      30             : #include "internal.h"
      31             : #include "avio_internal.h"
      32             : 
      33             : 
      34             : typedef struct CinFileHeader {
      35             :     int video_frame_size;
      36             :     int video_frame_width;
      37             :     int video_frame_height;
      38             :     int audio_frequency;
      39             :     int audio_bits;
      40             :     int audio_stereo;
      41             :     int audio_frame_size;
      42             : } CinFileHeader;
      43             : 
      44             : typedef struct CinFrameHeader {
      45             :     int audio_frame_type;
      46             :     int video_frame_type;
      47             :     int pal_colors_count;
      48             :     int audio_frame_size;
      49             :     int video_frame_size;
      50             : } CinFrameHeader;
      51             : 
      52             : typedef struct CinDemuxContext {
      53             :     int audio_stream_index;
      54             :     int video_stream_index;
      55             :     CinFileHeader file_header;
      56             :     int64_t audio_stream_pts;
      57             :     int64_t video_stream_pts;
      58             :     CinFrameHeader frame_header;
      59             :     int audio_buffer_size;
      60             : } CinDemuxContext;
      61             : 
      62             : 
      63        6034 : static int cin_probe(AVProbeData *p)
      64             : {
      65             :     /* header starts with this special marker */
      66        6034 :     if (AV_RL32(&p->buf[0]) != 0x55AA0000)
      67        6032 :         return 0;
      68             : 
      69             :     /* for accuracy, check some header field values */
      70           2 :     if (AV_RL32(&p->buf[12]) != 22050 || p->buf[16] != 16 || p->buf[17] != 0)
      71           0 :         return 0;
      72             : 
      73           2 :     return AVPROBE_SCORE_MAX;
      74             : }
      75             : 
      76           2 : static int cin_read_file_header(CinDemuxContext *cin, AVIOContext *pb) {
      77           2 :     CinFileHeader *hdr = &cin->file_header;
      78             : 
      79           2 :     if (avio_rl32(pb) != 0x55AA0000)
      80           0 :         return AVERROR_INVALIDDATA;
      81             : 
      82           2 :     hdr->video_frame_size   = avio_rl32(pb);
      83           2 :     hdr->video_frame_width  = avio_rl16(pb);
      84           2 :     hdr->video_frame_height = avio_rl16(pb);
      85           2 :     hdr->audio_frequency    = avio_rl32(pb);
      86           2 :     hdr->audio_bits         = avio_r8(pb);
      87           2 :     hdr->audio_stereo       = avio_r8(pb);
      88           2 :     hdr->audio_frame_size   = avio_rl16(pb);
      89             : 
      90           2 :     if (hdr->audio_frequency != 22050 || hdr->audio_bits != 16 || hdr->audio_stereo != 0)
      91           0 :         return AVERROR_INVALIDDATA;
      92             : 
      93           2 :     return 0;
      94             : }
      95             : 
      96           2 : static int cin_read_header(AVFormatContext *s)
      97             : {
      98             :     int rc;
      99           2 :     CinDemuxContext *cin = s->priv_data;
     100           2 :     CinFileHeader *hdr = &cin->file_header;
     101           2 :     AVIOContext *pb = s->pb;
     102             :     AVStream *st;
     103             : 
     104           2 :     rc = cin_read_file_header(cin, pb);
     105           2 :     if (rc)
     106           0 :         return rc;
     107             : 
     108           2 :     cin->video_stream_pts = 0;
     109           2 :     cin->audio_stream_pts = 0;
     110           2 :     cin->audio_buffer_size = 0;
     111             : 
     112             :     /* initialize the video decoder stream */
     113           2 :     st = avformat_new_stream(s, NULL);
     114           2 :     if (!st)
     115           0 :         return AVERROR(ENOMEM);
     116             : 
     117           2 :     avpriv_set_pts_info(st, 32, 1, 12);
     118           2 :     cin->video_stream_index = st->index;
     119           2 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     120           2 :     st->codecpar->codec_id = AV_CODEC_ID_DSICINVIDEO;
     121           2 :     st->codecpar->codec_tag = 0;  /* no fourcc */
     122           2 :     st->codecpar->width = hdr->video_frame_width;
     123           2 :     st->codecpar->height = hdr->video_frame_height;
     124             : 
     125             :     /* initialize the audio decoder stream */
     126           2 :     st = avformat_new_stream(s, NULL);
     127           2 :     if (!st)
     128           0 :         return AVERROR(ENOMEM);
     129             : 
     130           2 :     avpriv_set_pts_info(st, 32, 1, 22050);
     131           2 :     cin->audio_stream_index = st->index;
     132           2 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     133           2 :     st->codecpar->codec_id = AV_CODEC_ID_DSICINAUDIO;
     134           2 :     st->codecpar->codec_tag = 0;  /* no tag */
     135           2 :     st->codecpar->channels = 1;
     136           2 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     137           2 :     st->codecpar->sample_rate = 22050;
     138           2 :     st->codecpar->bits_per_coded_sample = 8;
     139           2 :     st->codecpar->bit_rate = st->codecpar->sample_rate * st->codecpar->bits_per_coded_sample * st->codecpar->channels;
     140             : 
     141           2 :     return 0;
     142             : }
     143             : 
     144         184 : static int cin_read_frame_header(CinDemuxContext *cin, AVIOContext *pb) {
     145         184 :     CinFrameHeader *hdr = &cin->frame_header;
     146             : 
     147         184 :     hdr->video_frame_type = avio_r8(pb);
     148         184 :     hdr->audio_frame_type = avio_r8(pb);
     149         184 :     hdr->pal_colors_count = avio_rl16(pb);
     150         184 :     hdr->video_frame_size = avio_rl32(pb);
     151         184 :     hdr->audio_frame_size = avio_rl32(pb);
     152             : 
     153         184 :     if (avio_feof(pb) || pb->error)
     154           0 :         return AVERROR(EIO);
     155             : 
     156         184 :     if (avio_rl32(pb) != 0xAA55AA55)
     157           0 :         return AVERROR_INVALIDDATA;
     158         184 :     if (hdr->video_frame_size < 0 || hdr->audio_frame_size < 0)
     159           0 :         return AVERROR_INVALIDDATA;
     160             : 
     161         184 :     return 0;
     162             : }
     163             : 
     164         368 : static int cin_read_packet(AVFormatContext *s, AVPacket *pkt)
     165             : {
     166         368 :     CinDemuxContext *cin = s->priv_data;
     167         368 :     AVIOContext *pb = s->pb;
     168         368 :     CinFrameHeader *hdr = &cin->frame_header;
     169             :     int rc, palette_type, pkt_size;
     170             :     int ret;
     171             : 
     172         368 :     if (cin->audio_buffer_size == 0) {
     173         184 :         rc = cin_read_frame_header(cin, pb);
     174         184 :         if (rc)
     175           0 :             return rc;
     176             : 
     177         184 :         if ((int16_t)hdr->pal_colors_count < 0) {
     178           0 :             hdr->pal_colors_count = -(int16_t)hdr->pal_colors_count;
     179           0 :             palette_type = 1;
     180             :         } else {
     181         184 :             palette_type = 0;
     182             :         }
     183             : 
     184             :         /* palette and video packet */
     185         184 :         pkt_size = (palette_type + 3) * hdr->pal_colors_count + hdr->video_frame_size;
     186             : 
     187         184 :         pkt_size = ffio_limit(pb, pkt_size);
     188             : 
     189         184 :         ret = av_new_packet(pkt, 4 + pkt_size);
     190         184 :         if (ret < 0)
     191           0 :             return ret;
     192             : 
     193         184 :         pkt->stream_index = cin->video_stream_index;
     194         184 :         pkt->pts = cin->video_stream_pts++;
     195             : 
     196         184 :         pkt->data[0] = palette_type;
     197         184 :         pkt->data[1] = hdr->pal_colors_count & 0xFF;
     198         184 :         pkt->data[2] = hdr->pal_colors_count >> 8;
     199         184 :         pkt->data[3] = hdr->video_frame_type;
     200             : 
     201         184 :         ret = avio_read(pb, &pkt->data[4], pkt_size);
     202         184 :         if (ret < 0) {
     203           0 :             av_packet_unref(pkt);
     204           0 :             return ret;
     205             :         }
     206         184 :         if (ret < pkt_size)
     207           2 :             av_shrink_packet(pkt, 4 + ret);
     208             : 
     209             :         /* sound buffer will be processed on next read_packet() call */
     210         184 :         cin->audio_buffer_size = hdr->audio_frame_size;
     211         184 :         return 0;
     212             :     }
     213             : 
     214             :     /* audio packet */
     215         184 :     ret = av_get_packet(pb, pkt, cin->audio_buffer_size);
     216         184 :     if (ret < 0)
     217           2 :         return ret;
     218             : 
     219         182 :     pkt->stream_index = cin->audio_stream_index;
     220         182 :     pkt->pts = cin->audio_stream_pts;
     221         182 :     pkt->duration = cin->audio_buffer_size - (pkt->pts == 0);
     222         182 :     cin->audio_stream_pts += pkt->duration;
     223         182 :     cin->audio_buffer_size = 0;
     224         182 :     return 0;
     225             : }
     226             : 
     227             : AVInputFormat ff_dsicin_demuxer = {
     228             :     .name           = "dsicin",
     229             :     .long_name      = NULL_IF_CONFIG_SMALL("Delphine Software International CIN"),
     230             :     .priv_data_size = sizeof(CinDemuxContext),
     231             :     .read_probe     = cin_probe,
     232             :     .read_header    = cin_read_header,
     233             :     .read_packet    = cin_read_packet,
     234             : };

Generated by: LCOV version 1.13