LCOV - code coverage report
Current view: top level - libavformat - wc3movie.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 109 143 76.2 %
Date: 2017-12-16 01:21:47 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Wing Commander III Movie (.mve) File Demuxer
       3             :  * Copyright (c) 2003 The FFmpeg project
       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             :  * Wing Commander III Movie file demuxer
      25             :  * by Mike Melanson (melanson@pcisys.net)
      26             :  * for more information on the WC3 .mve file format, visit:
      27             :  *   http://www.pcisys.net/~melanson/codecs/
      28             :  */
      29             : 
      30             : #include "libavutil/avstring.h"
      31             : #include "libavutil/channel_layout.h"
      32             : #include "libavutil/intreadwrite.h"
      33             : #include "libavutil/dict.h"
      34             : #include "avformat.h"
      35             : #include "internal.h"
      36             : 
      37             : #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
      38             : #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
      39             : #define  PC__TAG MKTAG('_', 'P', 'C', '_')
      40             : #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
      41             : #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
      42             : #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
      43             : #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
      44             : #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
      45             : #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
      46             : #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
      47             : #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
      48             : #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
      49             : #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
      50             : 
      51             : /* video resolution unless otherwise specified */
      52             : #define WC3_DEFAULT_WIDTH 320
      53             : #define WC3_DEFAULT_HEIGHT 165
      54             : 
      55             : /* always use the same PCM audio parameters */
      56             : #define WC3_SAMPLE_RATE 22050
      57             : #define WC3_AUDIO_CHANNELS 1
      58             : #define WC3_AUDIO_BITS 16
      59             : 
      60             : /* nice, constant framerate */
      61             : #define WC3_FRAME_FPS 15
      62             : 
      63             : #define PALETTE_SIZE (256 * 3)
      64             : 
      65             : typedef struct Wc3DemuxContext {
      66             :     int width;
      67             :     int height;
      68             :     int64_t pts;
      69             :     int video_stream_index;
      70             :     int audio_stream_index;
      71             : 
      72             :     AVPacket vpkt;
      73             : 
      74             : } Wc3DemuxContext;
      75             : 
      76        6130 : static int wc3_probe(AVProbeData *p)
      77             : {
      78        6130 :     if (p->buf_size < 12)
      79           0 :         return 0;
      80             : 
      81        6144 :     if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
      82          14 :         (AV_RL32(&p->buf[8]) != MOVE_TAG))
      83        6129 :         return 0;
      84             : 
      85           1 :     return AVPROBE_SCORE_MAX;
      86             : }
      87             : 
      88           1 : static int wc3_read_header(AVFormatContext *s)
      89             : {
      90           1 :     Wc3DemuxContext *wc3 = s->priv_data;
      91           1 :     AVIOContext *pb = s->pb;
      92             :     unsigned int fourcc_tag;
      93             :     unsigned int size;
      94             :     AVStream *st;
      95           1 :     int ret = 0;
      96             :     char *buffer;
      97             : 
      98             :     /* default context members */
      99           1 :     wc3->width = WC3_DEFAULT_WIDTH;
     100           1 :     wc3->height = WC3_DEFAULT_HEIGHT;
     101           1 :     wc3->pts = 0;
     102           1 :     wc3->video_stream_index = wc3->audio_stream_index = 0;
     103           1 :     av_init_packet(&wc3->vpkt);
     104           1 :     wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
     105             : 
     106             :     /* skip the first 3 32-bit numbers */
     107           1 :     avio_skip(pb, 12);
     108             : 
     109             :     /* traverse through the chunks and load the header information before
     110             :      * the first BRCH tag */
     111           1 :     fourcc_tag = avio_rl32(pb);
     112           1 :     size = (avio_rb32(pb) + 1) & (~1);
     113             : 
     114             :     do {
     115          27 :         switch (fourcc_tag) {
     116             : 
     117           2 :         case SOND_TAG:
     118             :         case INDX_TAG:
     119             :             /* SOND unknown, INDX unnecessary; ignore both */
     120           2 :             avio_skip(pb, size);
     121           2 :             break;
     122             : 
     123           1 :         case PC__TAG:
     124             :             /* number of palettes, unneeded */
     125           1 :             avio_skip(pb, 12);
     126           1 :             break;
     127             : 
     128           0 :         case BNAM_TAG:
     129             :             /* load up the name */
     130           0 :             buffer = av_malloc(size+1);
     131           0 :             if (!buffer)
     132           0 :                 return AVERROR(ENOMEM);
     133           0 :             if ((ret = avio_read(pb, buffer, size)) != size)
     134           0 :                 return AVERROR(EIO);
     135           0 :             buffer[size] = 0;
     136           0 :             av_dict_set(&s->metadata, "title", buffer,
     137             :                                    AV_DICT_DONT_STRDUP_VAL);
     138           0 :             break;
     139             : 
     140           0 :         case SIZE_TAG:
     141             :             /* video resolution override */
     142           0 :             wc3->width  = avio_rl32(pb);
     143           0 :             wc3->height = avio_rl32(pb);
     144           0 :             break;
     145             : 
     146          24 :         case PALT_TAG:
     147             :             /* one of several palettes */
     148          24 :             avio_seek(pb, -8, SEEK_CUR);
     149          24 :             av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
     150          24 :             break;
     151             : 
     152           0 :         default:
     153           0 :             av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
     154           0 :                    av_fourcc2str(fourcc_tag));
     155           0 :             return AVERROR_INVALIDDATA;
     156             :         }
     157             : 
     158          27 :         fourcc_tag = avio_rl32(pb);
     159             :         /* chunk sizes are 16-bit aligned */
     160          27 :         size = (avio_rb32(pb) + 1) & (~1);
     161          27 :         if (avio_feof(pb))
     162           0 :             return AVERROR(EIO);
     163             : 
     164          27 :     } while (fourcc_tag != BRCH_TAG);
     165             : 
     166             :     /* initialize the decoder streams */
     167           1 :     st = avformat_new_stream(s, NULL);
     168           1 :     if (!st)
     169           0 :         return AVERROR(ENOMEM);
     170           1 :     avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
     171           1 :     wc3->video_stream_index = st->index;
     172           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
     173           1 :     st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3;
     174           1 :     st->codecpar->codec_tag = 0;  /* no fourcc */
     175           1 :     st->codecpar->width = wc3->width;
     176           1 :     st->codecpar->height = wc3->height;
     177             : 
     178           1 :     st = avformat_new_stream(s, NULL);
     179           1 :     if (!st)
     180           0 :         return AVERROR(ENOMEM);
     181           1 :     avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
     182           1 :     wc3->audio_stream_index = st->index;
     183           1 :     st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
     184           1 :     st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
     185           1 :     st->codecpar->codec_tag = 1;
     186           1 :     st->codecpar->channels = WC3_AUDIO_CHANNELS;
     187           1 :     st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
     188           1 :     st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS;
     189           1 :     st->codecpar->sample_rate = WC3_SAMPLE_RATE;
     190           2 :     st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
     191           1 :         st->codecpar->bits_per_coded_sample;
     192           1 :     st->codecpar->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
     193             : 
     194           1 :     return 0;
     195             : }
     196             : 
     197          71 : static int wc3_read_packet(AVFormatContext *s,
     198             :                            AVPacket *pkt)
     199             : {
     200          71 :     Wc3DemuxContext *wc3 = s->priv_data;
     201          71 :     AVIOContext *pb = s->pb;
     202             :     unsigned int fourcc_tag;
     203             :     unsigned int size;
     204          71 :     int packet_read = 0;
     205          71 :     int ret = 0;
     206             :     unsigned char text[1024];
     207             : 
     208         214 :     while (!packet_read) {
     209             : 
     210          73 :         fourcc_tag = avio_rl32(pb);
     211             :         /* chunk sizes are 16-bit aligned */
     212          73 :         size = (avio_rb32(pb) + 1) & (~1);
     213          73 :         if (avio_feof(pb))
     214           1 :             return AVERROR(EIO);
     215             : 
     216          72 :         switch (fourcc_tag) {
     217             : 
     218           0 :         case BRCH_TAG:
     219             :             /* no-op */
     220           0 :             break;
     221             : 
     222           1 :         case SHOT_TAG:
     223             :             /* load up new palette */
     224           1 :             avio_seek(pb, -8, SEEK_CUR);
     225           1 :             av_append_packet(pb, &wc3->vpkt, 8 + 4);
     226           1 :             break;
     227             : 
     228          35 :         case VGA__TAG:
     229             :             /* send out video chunk */
     230          35 :             avio_seek(pb, -8, SEEK_CUR);
     231          35 :             ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
     232             :             // ignore error if we have some data
     233          35 :             if (wc3->vpkt.size > 0)
     234          35 :                 ret = 0;
     235          35 :             *pkt = wc3->vpkt;
     236          35 :             wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
     237          35 :             pkt->stream_index = wc3->video_stream_index;
     238          35 :             pkt->pts = wc3->pts;
     239          35 :             packet_read = 1;
     240          35 :             break;
     241             : 
     242           1 :         case TEXT_TAG:
     243             :             /* subtitle chunk */
     244           1 :             if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
     245           0 :                 ret = AVERROR(EIO);
     246             :             else {
     247           1 :                 int i = 0;
     248           1 :                 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
     249           1 :                 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
     250           0 :                     return AVERROR_INVALIDDATA;
     251           1 :                 av_log (s, AV_LOG_DEBUG, "  inglish: %s\n", &text[i + 1]);
     252           1 :                 i += text[i] + 1;
     253           1 :                 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
     254           0 :                     return AVERROR_INVALIDDATA;
     255           1 :                 av_log (s, AV_LOG_DEBUG, "  doytsch: %s\n", &text[i + 1]);
     256           1 :                 i += text[i] + 1;
     257           1 :                 if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
     258           0 :                     return AVERROR_INVALIDDATA;
     259           1 :                 av_log (s, AV_LOG_DEBUG, "  fronsay: %s\n", &text[i + 1]);
     260             :             }
     261           1 :             break;
     262             : 
     263          35 :         case AUDI_TAG:
     264             :             /* send out audio chunk */
     265          35 :             ret= av_get_packet(pb, pkt, size);
     266          35 :             pkt->stream_index = wc3->audio_stream_index;
     267          35 :             pkt->pts = wc3->pts;
     268             : 
     269             :             /* time to advance pts */
     270          35 :             wc3->pts++;
     271             : 
     272          35 :             packet_read = 1;
     273          35 :             break;
     274             : 
     275           0 :         default:
     276           0 :             av_log(s, AV_LOG_ERROR, "unrecognized WC3 chunk: %s\n",
     277           0 :                    av_fourcc2str(fourcc_tag));
     278           0 :             ret = AVERROR_INVALIDDATA;
     279           0 :             packet_read = 1;
     280           0 :             break;
     281             :         }
     282             :     }
     283             : 
     284          70 :     return ret;
     285             : }
     286             : 
     287           1 : static int wc3_read_close(AVFormatContext *s)
     288             : {
     289           1 :     Wc3DemuxContext *wc3 = s->priv_data;
     290             : 
     291           1 :     if (wc3->vpkt.size > 0)
     292           0 :         av_packet_unref(&wc3->vpkt);
     293             : 
     294           1 :     return 0;
     295             : }
     296             : 
     297             : AVInputFormat ff_wc3_demuxer = {
     298             :     .name           = "wc3movie",
     299             :     .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
     300             :     .priv_data_size = sizeof(Wc3DemuxContext),
     301             :     .read_probe     = wc3_probe,
     302             :     .read_header    = wc3_read_header,
     303             :     .read_packet    = wc3_read_packet,
     304             :     .read_close     = wc3_read_close,
     305             : };

Generated by: LCOV version 1.13