LCOV - code coverage report
Current view: top level - src/libavformat - dxa.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 112 136 82.4 %
Date: 2017-07-21 15:41:20 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * DXA demuxer
       3             :  * Copyright (c) 2007 Konstantin Shishkov
       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 <inttypes.h>
      23             : 
      24             : #include "libavutil/intreadwrite.h"
      25             : #include "avformat.h"
      26             : #include "internal.h"
      27             : #include "riff.h"
      28             : 
      29             : #define DXA_EXTRA_SIZE  9
      30             : 
      31             : typedef struct DXAContext {
      32             :     int frames;
      33             :     int has_sound;
      34             :     int bpc;
      35             :     uint32_t bytes_left;
      36             :     int64_t wavpos, vidpos;
      37             :     int readvid;
      38             : }DXAContext;
      39             : 
      40        5967 : static int dxa_probe(AVProbeData *p)
      41             : {
      42             :     int w, h;
      43        5967 :     if (p->buf_size < 15)
      44           0 :         return 0;
      45        5967 :     w = AV_RB16(p->buf + 11);
      46        5967 :     h = AV_RB16(p->buf + 13);
      47             :     /* check file header */
      48        5970 :     if (p->buf[0] == 'D' && p->buf[1] == 'E' &&
      49           6 :         p->buf[2] == 'X' && p->buf[3] == 'A' &&
      50           3 :         w && w <= 2048 && h && h <= 2048)
      51           3 :         return AVPROBE_SCORE_MAX;
      52             :     else
      53        5964 :         return 0;
      54             : }
      55             : 
      56           3 : static int dxa_read_header(AVFormatContext *s)
      57             : {
      58           3 :     AVIOContext *pb = s->pb;
      59           3 :     DXAContext *c = s->priv_data;
      60             :     AVStream *st, *ast;
      61             :     uint32_t tag;
      62             :     int32_t fps;
      63             :     int w, h;
      64             :     int num, den;
      65             :     int flags;
      66             :     int ret;
      67             : 
      68           3 :     tag = avio_rl32(pb);
      69           3 :     if (tag != MKTAG('D', 'E', 'X', 'A'))
      70           0 :         return AVERROR_INVALIDDATA;
      71           3 :     flags = avio_r8(pb);
      72           3 :     c->frames = avio_rb16(pb);
      73           3 :     if(!c->frames){
      74           0 :         av_log(s, AV_LOG_ERROR, "File contains no frames ???\n");
      75           0 :         return AVERROR_INVALIDDATA;
      76             :     }
      77             : 
      78           3 :     fps = avio_rb32(pb);
      79           3 :     if(fps > 0){
      80           0 :         den = 1000;
      81           0 :         num = fps;
      82           3 :     }else if (fps < 0){
      83           3 :         den = 100000;
      84           3 :         num = -fps;
      85             :     }else{
      86           0 :         den = 10;
      87           0 :         num = 1;
      88             :     }
      89           3 :     w = avio_rb16(pb);
      90           3 :     h = avio_rb16(pb);
      91           3 :     c->has_sound = 0;
      92             : 
      93           3 :     st = avformat_new_stream(s, NULL);
      94           3 :     if (!st)
      95           0 :         return AVERROR(ENOMEM);
      96             : 
      97             :     // Parse WAV data header
      98           3 :     if(avio_rl32(pb) == MKTAG('W', 'A', 'V', 'E')){
      99             :         uint32_t size, fsize;
     100           2 :         c->has_sound = 1;
     101           2 :         size = avio_rb32(pb);
     102           2 :         c->vidpos = avio_tell(pb) + size;
     103           2 :         avio_skip(pb, 16);
     104           2 :         fsize = avio_rl32(pb);
     105             : 
     106           2 :         ast = avformat_new_stream(s, NULL);
     107           2 :         if (!ast)
     108           0 :             return AVERROR(ENOMEM);
     109           2 :         ret = ff_get_wav_header(s, pb, ast->codecpar, fsize, 0);
     110           2 :         if (ret < 0)
     111           0 :             return ret;
     112           2 :         if (ast->codecpar->sample_rate > 0)
     113           2 :             avpriv_set_pts_info(ast, 64, 1, ast->codecpar->sample_rate);
     114             :         // find 'data' chunk
     115           6 :         while(avio_tell(pb) < c->vidpos && !avio_feof(pb)){
     116           4 :             tag = avio_rl32(pb);
     117           4 :             fsize = avio_rl32(pb);
     118           4 :             if(tag == MKTAG('d', 'a', 't', 'a')) break;
     119           2 :             avio_skip(pb, fsize);
     120             :         }
     121           2 :         c->bpc = (fsize + c->frames - 1) / c->frames;
     122           2 :         if(ast->codecpar->block_align)
     123           2 :             c->bpc = ((c->bpc + ast->codecpar->block_align - 1) / ast->codecpar->block_align) * ast->codecpar->block_align;
     124           2 :         c->bytes_left = fsize;
     125           2 :         c->wavpos = avio_tell(pb);
     126           2 :         avio_seek(pb, c->vidpos, SEEK_SET);
     127             :     }
     128             : 
     129             :     /* now we are ready: build format streams */
     130           3 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     131           3 :     st->codecpar->codec_id   = AV_CODEC_ID_DXA;
     132           3 :     st->codecpar->width      = w;
     133           3 :     st->codecpar->height     = h;
     134           3 :     av_reduce(&den, &num, den, num, (1UL<<31)-1);
     135           3 :     avpriv_set_pts_info(st, 33, num, den);
     136             :     /* flags & 0x80 means that image is interlaced,
     137             :      * flags & 0x40 means that image has double height
     138             :      * either way set true height
     139             :      */
     140           3 :     if(flags & 0xC0){
     141           1 :         st->codecpar->height >>= 1;
     142             :     }
     143           3 :     c->readvid = !c->has_sound;
     144           3 :     c->vidpos  = avio_tell(pb);
     145           3 :     s->start_time = 0;
     146           3 :     s->duration = (int64_t)c->frames * AV_TIME_BASE * num / den;
     147           3 :     av_log(s, AV_LOG_DEBUG, "%d frame(s)\n",c->frames);
     148             : 
     149           3 :     return 0;
     150             : }
     151             : 
     152         172 : static int dxa_read_packet(AVFormatContext *s, AVPacket *pkt)
     153             : {
     154         172 :     DXAContext *c = s->priv_data;
     155             :     int ret;
     156             :     uint32_t size;
     157             :     uint8_t buf[DXA_EXTRA_SIZE], pal[768+4];
     158         172 :     int pal_size = 0;
     159             : 
     160         172 :     if(!c->readvid && c->has_sound && c->bytes_left){
     161          80 :         c->readvid = 1;
     162          80 :         avio_seek(s->pb, c->wavpos, SEEK_SET);
     163          80 :         size = FFMIN(c->bytes_left, c->bpc);
     164          80 :         ret = av_get_packet(s->pb, pkt, size);
     165          80 :         pkt->stream_index = 1;
     166          80 :         if(ret != size)
     167           0 :             return AVERROR(EIO);
     168          80 :         c->bytes_left -= size;
     169          80 :         c->wavpos = avio_tell(s->pb);
     170          80 :         return 0;
     171             :     }
     172          92 :     avio_seek(s->pb, c->vidpos, SEEK_SET);
     173          92 :     while(!avio_feof(s->pb) && c->frames){
     174             :         uint32_t tag;
     175          95 :         if ((ret = avio_read(s->pb, buf, 4)) != 4) {
     176           0 :             av_log(s, AV_LOG_ERROR, "failed reading chunk type\n");
     177           0 :             return ret < 0 ? ret : AVERROR_INVALIDDATA;
     178             :         }
     179          95 :         tag = AV_RL32(buf);
     180          95 :         switch (tag) {
     181          44 :         case MKTAG('N', 'U', 'L', 'L'):
     182          44 :             if(av_new_packet(pkt, 4 + pal_size) < 0)
     183           0 :                 return AVERROR(ENOMEM);
     184          44 :             pkt->stream_index = 0;
     185          44 :             if(pal_size) memcpy(pkt->data, pal, pal_size);
     186          44 :             memcpy(pkt->data + pal_size, buf, 4);
     187          44 :             c->frames--;
     188          44 :             c->vidpos = avio_tell(s->pb);
     189          44 :             c->readvid = 0;
     190          44 :             return 0;
     191           3 :         case MKTAG('C', 'M', 'A', 'P'):
     192           3 :             pal_size = 768+4;
     193           3 :             memcpy(pal, buf, 4);
     194           3 :             avio_read(s->pb, pal + 4, 768);
     195           3 :             break;
     196          48 :         case MKTAG('F', 'R', 'A', 'M'):
     197          48 :             if ((ret = avio_read(s->pb, buf + 4, DXA_EXTRA_SIZE - 4)) != DXA_EXTRA_SIZE - 4) {
     198           0 :                 av_log(s, AV_LOG_ERROR, "failed reading dxa_extra\n");
     199           0 :                 return ret < 0 ? ret : AVERROR_INVALIDDATA;
     200             :             }
     201          48 :             size = AV_RB32(buf + 5);
     202          48 :             if(size > 0xFFFFFF){
     203           0 :                 av_log(s, AV_LOG_ERROR, "Frame size is too big: %"PRIu32"\n",
     204             :                        size);
     205           0 :                 return AVERROR_INVALIDDATA;
     206             :             }
     207          48 :             if(av_new_packet(pkt, size + DXA_EXTRA_SIZE + pal_size) < 0)
     208           0 :                 return AVERROR(ENOMEM);
     209          48 :             memcpy(pkt->data + pal_size, buf, DXA_EXTRA_SIZE);
     210          48 :             ret = avio_read(s->pb, pkt->data + DXA_EXTRA_SIZE + pal_size, size);
     211          48 :             if(ret != size){
     212           2 :                 av_packet_unref(pkt);
     213           2 :                 return AVERROR(EIO);
     214             :             }
     215          46 :             if(pal_size) memcpy(pkt->data, pal, pal_size);
     216          46 :             pkt->stream_index = 0;
     217          46 :             c->frames--;
     218          46 :             c->vidpos = avio_tell(s->pb);
     219          46 :             c->readvid = 0;
     220          46 :             return 0;
     221           0 :         default:
     222           0 :             av_log(s, AV_LOG_ERROR, "Unknown tag %s\n", av_fourcc2str(tag));
     223           0 :             return AVERROR_INVALIDDATA;
     224             :         }
     225             :     }
     226           0 :     return AVERROR_EOF;
     227             : }
     228             : 
     229             : AVInputFormat ff_dxa_demuxer = {
     230             :     .name           = "dxa",
     231             :     .long_name      = NULL_IF_CONFIG_SMALL("DXA"),
     232             :     .priv_data_size = sizeof(DXAContext),
     233             :     .read_probe     = dxa_probe,
     234             :     .read_header    = dxa_read_header,
     235             :     .read_packet    = dxa_read_packet,
     236             : };

Generated by: LCOV version 1.13