LCOV - code coverage report
Current view: top level - libavformat - rl2.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 105 137 76.6 %
Date: 2017-12-15 18:13:28 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /*
       2             :  * RL2 Format Demuxer
       3             :  * Copyright (c) 2008 Sascha Sommer (saschasommer@freenet.de)
       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             :  * RL2 file demuxer
      24             :  * @file
      25             :  * @author Sascha Sommer (saschasommer@freenet.de)
      26             :  * @see http://wiki.multimedia.cx/index.php?title=RL2
      27             :  *
      28             :  * extradata:
      29             :  * 2 byte le initial drawing offset within 320x200 viewport
      30             :  * 4 byte le number of used colors
      31             :  * 256 * 3 bytes rgb palette
      32             :  * optional background_frame
      33             :  */
      34             : 
      35             : #include <stdint.h>
      36             : 
      37             : #include "libavutil/intreadwrite.h"
      38             : #include "libavutil/mathematics.h"
      39             : #include "avformat.h"
      40             : #include "internal.h"
      41             : 
      42             : #define EXTRADATA1_SIZE (6 + 256 * 3) ///< video base, clr, palette
      43             : 
      44             : #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
      45             : #define RLV2_TAG MKBETAG('R', 'L', 'V', '2')
      46             : #define RLV3_TAG MKBETAG('R', 'L', 'V', '3')
      47             : 
      48             : typedef struct Rl2DemuxContext {
      49             :     unsigned int index_pos[2];   ///< indexes in the sample tables
      50             : } Rl2DemuxContext;
      51             : 
      52             : 
      53             : /**
      54             :  * check if the file is in rl2 format
      55             :  * @param p probe buffer
      56             :  * @return 0 when the probe buffer does not contain rl2 data, > 0 otherwise
      57             :  */
      58        6130 : static int rl2_probe(AVProbeData *p)
      59             : {
      60             : 
      61        6130 :     if(AV_RB32(&p->buf[0]) != FORM_TAG)
      62        6116 :         return 0;
      63             : 
      64          28 :     if(AV_RB32(&p->buf[8]) != RLV2_TAG &&
      65          14 :         AV_RB32(&p->buf[8]) != RLV3_TAG)
      66          13 :         return 0;
      67             : 
      68           1 :     return AVPROBE_SCORE_MAX;
      69             : }
      70             : 
      71             : /**
      72             :  * read rl2 header data and setup the avstreams
      73             :  * @param s demuxer context
      74             :  * @return 0 on success, AVERROR otherwise
      75             :  */
      76           1 : static av_cold int rl2_read_header(AVFormatContext *s)
      77             : {
      78           1 :     AVIOContext *pb = s->pb;
      79             :     AVStream *st;
      80             :     unsigned int frame_count;
      81           1 :     unsigned int audio_frame_counter = 0;
      82           1 :     unsigned int video_frame_counter = 0;
      83             :     unsigned int back_size;
      84             :     unsigned short sound_rate;
      85             :     unsigned short rate;
      86             :     unsigned short channels;
      87             :     unsigned short def_sound_size;
      88             :     unsigned int signature;
      89           1 :     unsigned int pts_den = 11025; /* video only case */
      90           1 :     unsigned int pts_num = 1103;
      91           1 :     unsigned int* chunk_offset = NULL;
      92           1 :     int* chunk_size = NULL;
      93           1 :     int* audio_size = NULL;
      94             :     int i;
      95           1 :     int ret = 0;
      96             : 
      97           1 :     avio_skip(pb,4);          /* skip FORM tag */
      98           1 :     back_size = avio_rl32(pb); /**< get size of the background frame */
      99           1 :     signature = avio_rb32(pb);
     100           1 :     avio_skip(pb, 4);         /* data size */
     101           1 :     frame_count = avio_rl32(pb);
     102             : 
     103             :     /* disallow back_sizes and frame_counts that may lead to overflows later */
     104           1 :     if(back_size > INT_MAX/2  || frame_count > INT_MAX / sizeof(uint32_t))
     105           0 :         return AVERROR_INVALIDDATA;
     106             : 
     107           1 :     avio_skip(pb, 2);         /* encoding method */
     108           1 :     sound_rate = avio_rl16(pb);
     109           1 :     rate = avio_rl16(pb);
     110           1 :     channels = avio_rl16(pb);
     111           1 :     def_sound_size = avio_rl16(pb);
     112             : 
     113             :     /** setup video stream */
     114           1 :     st = avformat_new_stream(s, NULL);
     115           1 :     if(!st)
     116           0 :          return AVERROR(ENOMEM);
     117             : 
     118           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     119           1 :     st->codecpar->codec_id = AV_CODEC_ID_RL2;
     120           1 :     st->codecpar->codec_tag = 0;  /* no fourcc */
     121           1 :     st->codecpar->width = 320;
     122           1 :     st->codecpar->height = 200;
     123             : 
     124             :     /** allocate and fill extradata */
     125           1 :     st->codecpar->extradata_size = EXTRADATA1_SIZE;
     126             : 
     127           1 :     if(signature == RLV3_TAG && back_size > 0)
     128           1 :         st->codecpar->extradata_size += back_size;
     129             : 
     130           1 :     if(ff_get_extradata(s, st->codecpar, pb, st->codecpar->extradata_size) < 0)
     131           0 :         return AVERROR(ENOMEM);
     132             : 
     133             :     /** setup audio stream if present */
     134           1 :     if(sound_rate){
     135           1 :         if (!channels || channels > 42) {
     136           0 :             av_log(s, AV_LOG_ERROR, "Invalid number of channels: %d\n", channels);
     137           0 :             return AVERROR_INVALIDDATA;
     138             :         }
     139             : 
     140           1 :         pts_num = def_sound_size;
     141           1 :         pts_den = rate;
     142             : 
     143           1 :         st = avformat_new_stream(s, NULL);
     144           1 :         if (!st)
     145           0 :             return AVERROR(ENOMEM);
     146           1 :         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     147           1 :         st->codecpar->codec_id = AV_CODEC_ID_PCM_U8;
     148           1 :         st->codecpar->codec_tag = 1;
     149           1 :         st->codecpar->channels = channels;
     150           1 :         st->codecpar->bits_per_coded_sample = 8;
     151           1 :         st->codecpar->sample_rate = rate;
     152           2 :         st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
     153           1 :             st->codecpar->bits_per_coded_sample;
     154           3 :         st->codecpar->block_align = st->codecpar->channels *
     155           2 :             st->codecpar->bits_per_coded_sample / 8;
     156           1 :         avpriv_set_pts_info(st,32,1,rate);
     157             :     }
     158             : 
     159           1 :     avpriv_set_pts_info(s->streams[0], 32, pts_num, pts_den);
     160             : 
     161           1 :     chunk_size =   av_malloc(frame_count * sizeof(uint32_t));
     162           1 :     audio_size =   av_malloc(frame_count * sizeof(uint32_t));
     163           1 :     chunk_offset = av_malloc(frame_count * sizeof(uint32_t));
     164             : 
     165           1 :     if(!chunk_size || !audio_size || !chunk_offset){
     166           0 :         av_free(chunk_size);
     167           0 :         av_free(audio_size);
     168           0 :         av_free(chunk_offset);
     169           0 :         return AVERROR(ENOMEM);
     170             :     }
     171             : 
     172             :     /** read offset and size tables */
     173         109 :     for(i=0; i < frame_count;i++) {
     174         108 :         if (avio_feof(pb))
     175           0 :             return AVERROR_INVALIDDATA;
     176         108 :         chunk_size[i] = avio_rl32(pb);
     177             :     }
     178         109 :     for(i=0; i < frame_count;i++) {
     179         108 :         if (avio_feof(pb))
     180           0 :             return AVERROR_INVALIDDATA;
     181         108 :         chunk_offset[i] = avio_rl32(pb);
     182             :     }
     183         109 :     for(i=0; i < frame_count;i++) {
     184         108 :         if (avio_feof(pb))
     185           0 :             return AVERROR_INVALIDDATA;
     186         108 :         audio_size[i] = avio_rl32(pb) & 0xFFFF;
     187             :     }
     188             : 
     189             :     /** build the sample index */
     190         109 :     for(i=0;i<frame_count;i++){
     191         108 :         if(chunk_size[i] < 0 || audio_size[i] > chunk_size[i]){
     192           0 :             ret = AVERROR_INVALIDDATA;
     193           0 :             break;
     194             :         }
     195             : 
     196         108 :         if(sound_rate && audio_size[i]){
     197         108 :             av_add_index_entry(s->streams[1], chunk_offset[i],
     198         108 :                 audio_frame_counter,audio_size[i], 0, AVINDEX_KEYFRAME);
     199         108 :             audio_frame_counter += audio_size[i] / channels;
     200             :         }
     201         108 :         av_add_index_entry(s->streams[0], chunk_offset[i] + audio_size[i],
     202         108 :             video_frame_counter,chunk_size[i]-audio_size[i],0,AVINDEX_KEYFRAME);
     203         108 :         ++video_frame_counter;
     204             :     }
     205             : 
     206             : 
     207           1 :     av_free(chunk_size);
     208           1 :     av_free(audio_size);
     209           1 :     av_free(chunk_offset);
     210             : 
     211           1 :     return ret;
     212             : }
     213             : 
     214             : /**
     215             :  * read a single audio or video packet
     216             :  * @param s demuxer context
     217             :  * @param pkt the packet to be filled
     218             :  * @return 0 on success, AVERROR otherwise
     219             :  */
     220         217 : static int rl2_read_packet(AVFormatContext *s,
     221             :                             AVPacket *pkt)
     222             : {
     223         217 :     Rl2DemuxContext *rl2 = s->priv_data;
     224         217 :     AVIOContext *pb = s->pb;
     225         217 :     AVIndexEntry *sample = NULL;
     226             :     int i;
     227         217 :     int ret = 0;
     228         217 :     int stream_id = -1;
     229         217 :     int64_t pos = INT64_MAX;
     230             : 
     231             :     /** check if there is a valid video or audio entry that can be used */
     232         651 :     for(i=0; i<s->nb_streams; i++){
     233         434 :         if(rl2->index_pos[i] < s->streams[i]->nb_index_entries
     234         431 :               && s->streams[i]->index_entries[ rl2->index_pos[i] ].pos < pos){
     235         324 :             sample = &s->streams[i]->index_entries[ rl2->index_pos[i] ];
     236         324 :             pos= sample->pos;
     237         324 :             stream_id= i;
     238             :         }
     239             :     }
     240             : 
     241         217 :     if(stream_id == -1)
     242           1 :         return AVERROR_EOF;
     243             : 
     244         216 :     ++rl2->index_pos[stream_id];
     245             : 
     246             :     /** position the stream (will probably be there anyway) */
     247         216 :     avio_seek(pb, sample->pos, SEEK_SET);
     248             : 
     249             :     /** fill the packet */
     250         216 :     ret = av_get_packet(pb, pkt, sample->size);
     251         216 :     if(ret != sample->size){
     252           0 :         av_packet_unref(pkt);
     253           0 :         return AVERROR(EIO);
     254             :     }
     255             : 
     256         216 :     pkt->stream_index = stream_id;
     257         216 :     pkt->pts = sample->timestamp;
     258             : 
     259         216 :     return ret;
     260             : }
     261             : 
     262             : /**
     263             :  * seek to a new timestamp
     264             :  * @param s demuxer context
     265             :  * @param stream_index index of the stream that should be seeked
     266             :  * @param timestamp wanted timestamp
     267             :  * @param flags direction and seeking mode
     268             :  * @return 0 on success, -1 otherwise
     269             :  */
     270           0 : static int rl2_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
     271             : {
     272           0 :     AVStream *st = s->streams[stream_index];
     273           0 :     Rl2DemuxContext *rl2 = s->priv_data;
     274             :     int i;
     275           0 :     int index = av_index_search_timestamp(st, timestamp, flags);
     276           0 :     if(index < 0)
     277           0 :         return -1;
     278             : 
     279           0 :     rl2->index_pos[stream_index] = index;
     280           0 :     timestamp = st->index_entries[index].timestamp;
     281             : 
     282           0 :     for(i=0; i < s->nb_streams; i++){
     283           0 :         AVStream *st2 = s->streams[i];
     284           0 :         index = av_index_search_timestamp(st2,
     285             :                     av_rescale_q(timestamp, st->time_base, st2->time_base),
     286             :                     flags | AVSEEK_FLAG_BACKWARD);
     287             : 
     288           0 :         if(index < 0)
     289           0 :             index = 0;
     290             : 
     291           0 :         rl2->index_pos[i] = index;
     292             :     }
     293             : 
     294           0 :     return 0;
     295             : }
     296             : 
     297             : AVInputFormat ff_rl2_demuxer = {
     298             :     .name           = "rl2",
     299             :     .long_name      = NULL_IF_CONFIG_SMALL("RL2"),
     300             :     .priv_data_size = sizeof(Rl2DemuxContext),
     301             :     .read_probe     = rl2_probe,
     302             :     .read_header    = rl2_read_header,
     303             :     .read_packet    = rl2_read_packet,
     304             :     .read_seek      = rl2_read_seek,
     305             : };

Generated by: LCOV version 1.13