LCOV - code coverage report
Current view: top level - src/libavformat - cdxl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 95 119 79.8 %
Date: 2017-09-22 00:04:37 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * CDXL demuxer
       3             :  * Copyright (c) 2011-2012 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/channel_layout.h"
      23             : #include "libavutil/intreadwrite.h"
      24             : #include "libavutil/parseutils.h"
      25             : #include "libavutil/opt.h"
      26             : #include "avformat.h"
      27             : #include "internal.h"
      28             : 
      29             : #define CDXL_HEADER_SIZE 32
      30             : 
      31             : typedef struct CDXLDemuxContext {
      32             :     AVClass     *class;
      33             :     int         sample_rate;
      34             :     char        *framerate;
      35             :     AVRational  fps;
      36             :     int         read_chunk;
      37             :     uint8_t     header[CDXL_HEADER_SIZE];
      38             :     int         video_stream_index;
      39             :     int         audio_stream_index;
      40             :     int64_t     filesize;
      41             : } CDXLDemuxContext;
      42             : 
      43        6034 : static int cdxl_read_probe(AVProbeData *p)
      44             : {
      45        6034 :     int score = AVPROBE_SCORE_EXTENSION + 10;
      46             : 
      47        6034 :     if (p->buf_size < CDXL_HEADER_SIZE)
      48           0 :         return 0;
      49             : 
      50             :     /* reserved bytes should always be set to 0 */
      51        6034 :     if (AV_RN64(&p->buf[24]) || AV_RN16(&p->buf[10]))
      52        5939 :         return 0;
      53             : 
      54             :     /* check type */
      55          95 :     if (p->buf[0] != 1)
      56          89 :         return 0;
      57             : 
      58             :     /* check palette size */
      59           6 :     if (AV_RB16(&p->buf[20]) > 512)
      60           0 :         return 0;
      61             : 
      62             :     /* check number of planes */
      63           6 :     if (p->buf[18] || !p->buf[19])
      64           0 :         return 0;
      65             : 
      66             :     /* check widh and height */
      67           6 :     if (!AV_RN16(&p->buf[14]) || !AV_RN16(&p->buf[16]))
      68           0 :         return 0;
      69             : 
      70             :     /* chunk size */
      71           6 :     if (AV_RB32(&p->buf[2]) < AV_RB16(&p->buf[22]) + AV_RB16(&p->buf[20]) + CDXL_HEADER_SIZE)
      72           0 :         return 0;
      73             : 
      74             :     /* previous chunk size */
      75           6 :     if (AV_RN32(&p->buf[6]))
      76           0 :         score /= 2;
      77             : 
      78             :     /* current frame number, usually starts from 1 */
      79           6 :     if (AV_RB16(&p->buf[12]) != 1)
      80           1 :         score /= 2;
      81             : 
      82           6 :     return score;
      83             : }
      84             : 
      85           6 : static int cdxl_read_header(AVFormatContext *s)
      86             : {
      87           6 :     CDXLDemuxContext *cdxl = s->priv_data;
      88             :     int ret;
      89             : 
      90           6 :     if (cdxl->framerate && (ret = av_parse_video_rate(&cdxl->fps, cdxl->framerate)) < 0) {
      91           0 :         av_log(s, AV_LOG_ERROR,
      92             :                "Could not parse framerate: %s.\n", cdxl->framerate);
      93           0 :         return ret;
      94             :     }
      95             : 
      96           6 :     cdxl->read_chunk         =  0;
      97           6 :     cdxl->video_stream_index = -1;
      98           6 :     cdxl->audio_stream_index = -1;
      99             : 
     100           6 :     cdxl->filesize = avio_size(s->pb);
     101             : 
     102           6 :     s->ctx_flags |= AVFMTCTX_NOHEADER;
     103             : 
     104           6 :     return 0;
     105             : }
     106             : 
     107         193 : static int cdxl_read_packet(AVFormatContext *s, AVPacket *pkt)
     108             : {
     109         193 :     CDXLDemuxContext *cdxl = s->priv_data;
     110         193 :     AVIOContext *pb = s->pb;
     111             :     uint32_t current_size, video_size, image_size;
     112             :     uint16_t audio_size, palette_size, width, height;
     113             :     int64_t  pos;
     114             :     int      format, frames, ret;
     115             : 
     116         193 :     if (avio_feof(pb))
     117           6 :         return AVERROR_EOF;
     118             : 
     119         187 :     pos = avio_tell(pb);
     120         294 :     if (!cdxl->read_chunk &&
     121         107 :         avio_read(pb, cdxl->header, CDXL_HEADER_SIZE) != CDXL_HEADER_SIZE)
     122           1 :         return AVERROR_EOF;
     123         186 :     if (cdxl->header[0] != 1) {
     124           0 :         av_log(s, AV_LOG_ERROR, "non-standard cdxl file\n");
     125           0 :         return AVERROR_INVALIDDATA;
     126             :     }
     127             : 
     128         186 :     format       = cdxl->header[1] & 0xE0;
     129         186 :     current_size = AV_RB32(&cdxl->header[2]);
     130         186 :     width        = AV_RB16(&cdxl->header[14]);
     131         186 :     height       = AV_RB16(&cdxl->header[16]);
     132         186 :     palette_size = AV_RB16(&cdxl->header[20]);
     133         186 :     audio_size   = AV_RB16(&cdxl->header[22]);
     134         186 :     if (FFALIGN(width, 16) * (uint64_t)height * cdxl->header[19] > INT_MAX)
     135           0 :         return AVERROR_INVALIDDATA;
     136         186 :     if (format == 0x20)
     137           0 :         image_size = width * height * cdxl->header[19] / 8;
     138             :     else
     139         186 :         image_size = FFALIGN(width, 16) * height * cdxl->header[19] / 8;
     140         186 :     video_size   = palette_size + image_size;
     141             : 
     142         186 :     if (palette_size > 512)
     143           0 :         return AVERROR_INVALIDDATA;
     144         186 :     if (current_size < (uint64_t)audio_size + video_size + CDXL_HEADER_SIZE)
     145           0 :         return AVERROR_INVALIDDATA;
     146             : 
     147         186 :     if (cdxl->read_chunk && audio_size) {
     148          80 :         if (cdxl->audio_stream_index == -1) {
     149           4 :             AVStream *st = avformat_new_stream(s, NULL);
     150           4 :             if (!st)
     151           0 :                 return AVERROR(ENOMEM);
     152             : 
     153           4 :             st->codecpar->codec_type    = AVMEDIA_TYPE_AUDIO;
     154           4 :             st->codecpar->codec_tag     = 0;
     155           4 :             st->codecpar->codec_id      = AV_CODEC_ID_PCM_S8;
     156           4 :             if (cdxl->header[1] & 0x10) {
     157           0 :                 st->codecpar->channels       = 2;
     158           0 :                 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
     159             :             } else {
     160           4 :                 st->codecpar->channels       = 1;
     161           4 :                 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     162             :             }
     163           4 :             st->codecpar->sample_rate   = cdxl->sample_rate;
     164           4 :             st->start_time           = 0;
     165           4 :             cdxl->audio_stream_index = st->index;
     166           4 :             avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
     167             :         }
     168             : 
     169          80 :         ret = av_get_packet(pb, pkt, audio_size);
     170          80 :         if (ret < 0)
     171           0 :             return ret;
     172          80 :         pkt->stream_index = cdxl->audio_stream_index;
     173          80 :         pkt->pos          = pos;
     174          80 :         pkt->duration     = audio_size;
     175          80 :         cdxl->read_chunk  = 0;
     176             :     } else {
     177         106 :         if (cdxl->video_stream_index == -1) {
     178           6 :             AVStream *st = avformat_new_stream(s, NULL);
     179           6 :             if (!st)
     180           0 :                 return AVERROR(ENOMEM);
     181             : 
     182           6 :             st->codecpar->codec_type    = AVMEDIA_TYPE_VIDEO;
     183           6 :             st->codecpar->codec_tag     = 0;
     184           6 :             st->codecpar->codec_id      = AV_CODEC_ID_CDXL;
     185           6 :             st->codecpar->width         = width;
     186           6 :             st->codecpar->height        = height;
     187             : 
     188           6 :             if (audio_size + video_size && cdxl->filesize > 0) {
     189           6 :                 frames = cdxl->filesize / (audio_size + video_size);
     190             : 
     191           6 :                 if(cdxl->framerate)
     192           0 :                     st->duration = frames;
     193             :                 else
     194           6 :                     st->duration = frames * (int64_t)audio_size;
     195             :             }
     196           6 :             st->start_time           = 0;
     197           6 :             cdxl->video_stream_index = st->index;
     198           6 :             if (cdxl->framerate)
     199           0 :                 avpriv_set_pts_info(st, 64, cdxl->fps.den, cdxl->fps.num);
     200             :             else
     201           6 :                 avpriv_set_pts_info(st, 64, 1, cdxl->sample_rate);
     202             :         }
     203             : 
     204         106 :         if (av_new_packet(pkt, video_size + CDXL_HEADER_SIZE) < 0)
     205           0 :             return AVERROR(ENOMEM);
     206         106 :         memcpy(pkt->data, cdxl->header, CDXL_HEADER_SIZE);
     207         106 :         ret = avio_read(pb, pkt->data + CDXL_HEADER_SIZE, video_size);
     208         106 :         if (ret < 0) {
     209           0 :             av_packet_unref(pkt);
     210           0 :             return ret;
     211             :         }
     212         106 :         av_shrink_packet(pkt, CDXL_HEADER_SIZE + ret);
     213         106 :         pkt->stream_index  = cdxl->video_stream_index;
     214         106 :         pkt->flags        |= AV_PKT_FLAG_KEY;
     215         106 :         pkt->pos           = pos;
     216         106 :         pkt->duration      = cdxl->framerate ? 1 : audio_size ? audio_size : 220;
     217         106 :         cdxl->read_chunk   = audio_size;
     218             :     }
     219             : 
     220         186 :     if (!cdxl->read_chunk)
     221         102 :         avio_skip(pb, current_size - audio_size - video_size - CDXL_HEADER_SIZE);
     222         186 :     return ret;
     223             : }
     224             : 
     225             : #define OFFSET(x) offsetof(CDXLDemuxContext, x)
     226             : static const AVOption cdxl_options[] = {
     227             :     { "sample_rate", "", OFFSET(sample_rate), AV_OPT_TYPE_INT,    { .i64 = 11025 }, 1, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
     228             :     { "framerate",   "", OFFSET(framerate),   AV_OPT_TYPE_STRING, { .str = NULL },  0, 0,       AV_OPT_FLAG_DECODING_PARAM },
     229             :     { NULL },
     230             : };
     231             : 
     232             : static const AVClass cdxl_demuxer_class = {
     233             :     .class_name = "CDXL demuxer",
     234             :     .item_name  = av_default_item_name,
     235             :     .option     = cdxl_options,
     236             :     .version    = LIBAVUTIL_VERSION_INT,
     237             : };
     238             : 
     239             : AVInputFormat ff_cdxl_demuxer = {
     240             :     .name           = "cdxl",
     241             :     .long_name      = NULL_IF_CONFIG_SMALL("Commodore CDXL video"),
     242             :     .priv_data_size = sizeof(CDXLDemuxContext),
     243             :     .read_probe     = cdxl_read_probe,
     244             :     .read_header    = cdxl_read_header,
     245             :     .read_packet    = cdxl_read_packet,
     246             :     .extensions     = "cdxl,xl",
     247             :     .flags          = AVFMT_GENERIC_INDEX,
     248             :     .priv_class     = &cdxl_demuxer_class,
     249             : };

Generated by: LCOV version 1.13