libavformat/wc3movie.c
Go to the documentation of this file.
00001 /*
00002  * Wing Commander III Movie (.mve) File Demuxer
00003  * Copyright (c) 2003 The ffmpeg Project
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #include "libavutil/intreadwrite.h"
00031 #include "libavutil/dict.h"
00032 #include "avformat.h"
00033 #include "internal.h"
00034 
00035 #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
00036 #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
00037 #define  PC__TAG MKTAG('_', 'P', 'C', '_')
00038 #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
00039 #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
00040 #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
00041 #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
00042 #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
00043 #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
00044 #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
00045 #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
00046 #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
00047 #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
00048 
00049 /* video resolution unless otherwise specified */
00050 #define WC3_DEFAULT_WIDTH 320
00051 #define WC3_DEFAULT_HEIGHT 165
00052 
00053 /* always use the same PCM audio parameters */
00054 #define WC3_SAMPLE_RATE 22050
00055 #define WC3_AUDIO_CHANNELS 1
00056 #define WC3_AUDIO_BITS 16
00057 
00058 /* nice, constant framerate */
00059 #define WC3_FRAME_FPS 15
00060 
00061 #define PALETTE_SIZE (256 * 3)
00062 
00063 typedef struct Wc3DemuxContext {
00064     int width;
00065     int height;
00066     int64_t pts;
00067     int video_stream_index;
00068     int audio_stream_index;
00069 
00070     AVPacket vpkt;
00071 
00072 } Wc3DemuxContext;
00073 
00074 static int wc3_probe(AVProbeData *p)
00075 {
00076     if (p->buf_size < 12)
00077         return 0;
00078 
00079     if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
00080         (AV_RL32(&p->buf[8]) != MOVE_TAG))
00081         return 0;
00082 
00083     return AVPROBE_SCORE_MAX;
00084 }
00085 
00086 static int wc3_read_header(AVFormatContext *s,
00087                            AVFormatParameters *ap)
00088 {
00089     Wc3DemuxContext *wc3 = s->priv_data;
00090     AVIOContext *pb = s->pb;
00091     unsigned int fourcc_tag;
00092     unsigned int size;
00093     AVStream *st;
00094     int ret = 0;
00095     char *buffer;
00096 
00097     /* default context members */
00098     wc3->width = WC3_DEFAULT_WIDTH;
00099     wc3->height = WC3_DEFAULT_HEIGHT;
00100     wc3->pts = 0;
00101     wc3->video_stream_index = wc3->audio_stream_index = 0;
00102     av_init_packet(&wc3->vpkt);
00103     wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00104 
00105     /* skip the first 3 32-bit numbers */
00106     avio_skip(pb, 12);
00107 
00108     /* traverse through the chunks and load the header information before
00109      * the first BRCH tag */
00110     fourcc_tag = avio_rl32(pb);
00111     size = (avio_rb32(pb) + 1) & (~1);
00112 
00113     do {
00114         switch (fourcc_tag) {
00115 
00116         case SOND_TAG:
00117         case INDX_TAG:
00118             /* SOND unknown, INDX unnecessary; ignore both */
00119             avio_skip(pb, size);
00120             break;
00121 
00122         case PC__TAG:
00123             /* number of palettes, unneeded */
00124             avio_skip(pb, 12);
00125             break;
00126 
00127         case BNAM_TAG:
00128             /* load up the name */
00129             buffer = av_malloc(size+1);
00130             if (!buffer)
00131                 return AVERROR(ENOMEM);
00132             if ((ret = avio_read(pb, buffer, size)) != size)
00133                 return AVERROR(EIO);
00134             buffer[size] = 0;
00135             av_dict_set(&s->metadata, "title", buffer,
00136                                    AV_DICT_DONT_STRDUP_VAL);
00137             break;
00138 
00139         case SIZE_TAG:
00140             /* video resolution override */
00141             wc3->width  = avio_rl32(pb);
00142             wc3->height = avio_rl32(pb);
00143             break;
00144 
00145         case PALT_TAG:
00146             /* one of several palettes */
00147             avio_seek(pb, -8, SEEK_CUR);
00148             av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
00149             break;
00150 
00151         default:
00152             av_log(s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00153                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00154                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00155             return AVERROR_INVALIDDATA;
00156         }
00157 
00158         fourcc_tag = avio_rl32(pb);
00159         /* chunk sizes are 16-bit aligned */
00160         size = (avio_rb32(pb) + 1) & (~1);
00161         if (pb->eof_reached)
00162             return AVERROR(EIO);
00163 
00164     } while (fourcc_tag != BRCH_TAG);
00165 
00166     /* initialize the decoder streams */
00167     st = avformat_new_stream(s, NULL);
00168     if (!st)
00169         return AVERROR(ENOMEM);
00170     avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00171     wc3->video_stream_index = st->index;
00172     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00173     st->codec->codec_id = CODEC_ID_XAN_WC3;
00174     st->codec->codec_tag = 0;  /* no fourcc */
00175     st->codec->width = wc3->width;
00176     st->codec->height = wc3->height;
00177 
00178     st = avformat_new_stream(s, NULL);
00179     if (!st)
00180         return AVERROR(ENOMEM);
00181     avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
00182     wc3->audio_stream_index = st->index;
00183     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00184     st->codec->codec_id = CODEC_ID_PCM_S16LE;
00185     st->codec->codec_tag = 1;
00186     st->codec->channels = WC3_AUDIO_CHANNELS;
00187     st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
00188     st->codec->sample_rate = WC3_SAMPLE_RATE;
00189     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00190         st->codec->bits_per_coded_sample;
00191     st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
00192 
00193     return 0;
00194 }
00195 
00196 static int wc3_read_packet(AVFormatContext *s,
00197                            AVPacket *pkt)
00198 {
00199     Wc3DemuxContext *wc3 = s->priv_data;
00200     AVIOContext *pb = s->pb;
00201     unsigned int fourcc_tag;
00202     unsigned int size;
00203     int packet_read = 0;
00204     int ret = 0;
00205     unsigned char text[1024];
00206 
00207     while (!packet_read) {
00208 
00209         fourcc_tag = avio_rl32(pb);
00210         /* chunk sizes are 16-bit aligned */
00211         size = (avio_rb32(pb) + 1) & (~1);
00212         if (pb->eof_reached)
00213             return AVERROR(EIO);
00214 
00215         switch (fourcc_tag) {
00216 
00217         case BRCH_TAG:
00218             /* no-op */
00219             break;
00220 
00221         case SHOT_TAG:
00222             /* load up new palette */
00223             avio_seek(pb, -8, SEEK_CUR);
00224             av_append_packet(pb, &wc3->vpkt, 8 + 4);
00225             break;
00226 
00227         case VGA__TAG:
00228             /* send out video chunk */
00229             avio_seek(pb, -8, SEEK_CUR);
00230             ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
00231             // ignore error if we have some data
00232             if (wc3->vpkt.size > 0)
00233                 ret = 0;
00234             *pkt = wc3->vpkt;
00235             wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
00236             pkt->stream_index = wc3->video_stream_index;
00237             pkt->pts = wc3->pts;
00238             packet_read = 1;
00239             break;
00240 
00241         case TEXT_TAG:
00242             /* subtitle chunk */
00243 #if 0
00244             avio_skip(pb, size);
00245 #else
00246             if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
00247                 ret = AVERROR(EIO);
00248             else {
00249                 int i = 0;
00250                 av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
00251                 av_log (s, AV_LOG_DEBUG, "  inglish: %s\n", &text[i + 1]);
00252                 i += text[i] + 1;
00253                 av_log (s, AV_LOG_DEBUG, "  doytsch: %s\n", &text[i + 1]);
00254                 i += text[i] + 1;
00255                 av_log (s, AV_LOG_DEBUG, "  fronsay: %s\n", &text[i + 1]);
00256             }
00257 #endif
00258             break;
00259 
00260         case AUDI_TAG:
00261             /* send out audio chunk */
00262             ret= av_get_packet(pb, pkt, size);
00263             pkt->stream_index = wc3->audio_stream_index;
00264             pkt->pts = wc3->pts;
00265 
00266             /* time to advance pts */
00267             wc3->pts++;
00268 
00269             packet_read = 1;
00270             break;
00271 
00272         default:
00273             av_log (s, AV_LOG_ERROR, "  unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
00274                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
00275                 (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
00276             ret = AVERROR_INVALIDDATA;
00277             packet_read = 1;
00278             break;
00279         }
00280     }
00281 
00282     return ret;
00283 }
00284 
00285 static int wc3_read_close(AVFormatContext *s)
00286 {
00287     Wc3DemuxContext *wc3 = s->priv_data;
00288 
00289     if (wc3->vpkt.size > 0)
00290         av_free_packet(&wc3->vpkt);
00291 
00292     return 0;
00293 }
00294 
00295 AVInputFormat ff_wc3_demuxer = {
00296     .name           = "wc3movie",
00297     .long_name      = NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
00298     .priv_data_size = sizeof(Wc3DemuxContext),
00299     .read_probe     = wc3_probe,
00300     .read_header    = wc3_read_header,
00301     .read_packet    = wc3_read_packet,
00302     .read_close     = wc3_read_close,
00303 };