libavformat/westwood.c
Go to the documentation of this file.
00001 /*
00002  * Westwood Studios Multimedia Formats Demuxer (VQA, AUD)
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 
00036 #include "libavutil/intreadwrite.h"
00037 #include "avformat.h"
00038 #include "internal.h"
00039 
00040 #define AUD_HEADER_SIZE 12
00041 #define AUD_CHUNK_PREAMBLE_SIZE 8
00042 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
00043 
00044 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
00045 #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
00046 #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
00047 #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
00048 #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
00049 #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
00050 #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
00051 #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
00052 
00053 /* don't know what these tags are for, but acknowledge their existence */
00054 #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
00055 #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
00056 #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
00057 #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
00058 #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
00059 #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
00060 #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
00061 
00062 #define VQA_HEADER_SIZE 0x2A
00063 #define VQA_FRAMERATE 15
00064 #define VQA_PREAMBLE_SIZE 8
00065 
00066 typedef struct WsAudDemuxContext {
00067     int audio_samplerate;
00068     int audio_channels;
00069     int audio_bits;
00070     enum CodecID audio_type;
00071     int audio_stream_index;
00072     int64_t audio_frame_counter;
00073 } WsAudDemuxContext;
00074 
00075 typedef struct WsVqaDemuxContext {
00076     int audio_samplerate;
00077     int audio_channels;
00078     int audio_bits;
00079 
00080     int audio_stream_index;
00081     int video_stream_index;
00082 
00083     int64_t audio_frame_counter;
00084 } WsVqaDemuxContext;
00085 
00086 static int wsaud_probe(AVProbeData *p)
00087 {
00088     int field;
00089 
00090     /* Probabilistic content detection strategy: There is no file signature
00091      * so perform sanity checks on various header parameters:
00092      *   8000 <= sample rate (16 bits) <= 48000  ==> 40001 acceptable numbers
00093      *   flags <= 0x03 (2 LSBs are used)         ==> 4 acceptable numbers
00094      *   compression type (8 bits) = 1 or 99     ==> 2 acceptable numbers
00095      *   first audio chunk signature (32 bits)   ==> 1 acceptable number
00096      * The number space contains 2^64 numbers. There are 40001 * 4 * 2 * 1 =
00097      * 320008 acceptable number combinations.
00098      */
00099 
00100     if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
00101         return 0;
00102 
00103     /* check sample rate */
00104     field = AV_RL16(&p->buf[0]);
00105     if ((field < 8000) || (field > 48000))
00106         return 0;
00107 
00108     /* enforce the rule that the top 6 bits of this flags field are reserved (0);
00109      * this might not be true, but enforce it until deemed unnecessary */
00110     if (p->buf[10] & 0xFC)
00111         return 0;
00112 
00113     /* note: only check for WS IMA (type 99) right now since there is no
00114      * support for type 1 */
00115     if (p->buf[11] != 99)
00116         return 0;
00117 
00118     /* read ahead to the first audio chunk and validate the first header signature */
00119     if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
00120         return 0;
00121 
00122     /* return 1/2 certainty since this file check is a little sketchy */
00123     return AVPROBE_SCORE_MAX / 2;
00124 }
00125 
00126 static int wsaud_read_header(AVFormatContext *s,
00127                              AVFormatParameters *ap)
00128 {
00129     WsAudDemuxContext *wsaud = s->priv_data;
00130     AVIOContext *pb = s->pb;
00131     AVStream *st;
00132     unsigned char header[AUD_HEADER_SIZE];
00133 
00134     if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
00135         return AVERROR(EIO);
00136     wsaud->audio_samplerate = AV_RL16(&header[0]);
00137     if (header[11] == 99)
00138         wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
00139     else
00140         return AVERROR_INVALIDDATA;
00141 
00142     /* flag 0 indicates stereo */
00143     wsaud->audio_channels = (header[10] & 0x1) + 1;
00144     /* flag 1 indicates 16 bit audio */
00145     wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
00146 
00147     /* initialize the audio decoder stream */
00148     st = avformat_new_stream(s, NULL);
00149     if (!st)
00150         return AVERROR(ENOMEM);
00151     avpriv_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
00152     st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00153     st->codec->codec_id = wsaud->audio_type;
00154     st->codec->codec_tag = 0;  /* no tag */
00155     st->codec->channels = wsaud->audio_channels;
00156     st->codec->sample_rate = wsaud->audio_samplerate;
00157     st->codec->bits_per_coded_sample = wsaud->audio_bits;
00158     st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00159         st->codec->bits_per_coded_sample / 4;
00160     st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00161 
00162     wsaud->audio_stream_index = st->index;
00163     wsaud->audio_frame_counter = 0;
00164 
00165     return 0;
00166 }
00167 
00168 static int wsaud_read_packet(AVFormatContext *s,
00169                              AVPacket *pkt)
00170 {
00171     WsAudDemuxContext *wsaud = s->priv_data;
00172     AVIOContext *pb = s->pb;
00173     unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
00174     unsigned int chunk_size;
00175     int ret = 0;
00176 
00177     if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
00178         AUD_CHUNK_PREAMBLE_SIZE)
00179         return AVERROR(EIO);
00180 
00181     /* validate the chunk */
00182     if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
00183         return AVERROR_INVALIDDATA;
00184 
00185     chunk_size = AV_RL16(&preamble[0]);
00186     ret= av_get_packet(pb, pkt, chunk_size);
00187     if (ret != chunk_size)
00188         return AVERROR(EIO);
00189     pkt->stream_index = wsaud->audio_stream_index;
00190     pkt->pts = wsaud->audio_frame_counter;
00191     pkt->pts /= wsaud->audio_samplerate;
00192 
00193     /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
00194     wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
00195 
00196     return ret;
00197 }
00198 
00199 static int wsvqa_probe(AVProbeData *p)
00200 {
00201     /* need 12 bytes to qualify */
00202     if (p->buf_size < 12)
00203         return 0;
00204 
00205     /* check for the VQA signatures */
00206     if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
00207         (AV_RB32(&p->buf[8]) != WVQA_TAG))
00208         return 0;
00209 
00210     return AVPROBE_SCORE_MAX;
00211 }
00212 
00213 static int wsvqa_read_header(AVFormatContext *s,
00214                              AVFormatParameters *ap)
00215 {
00216     WsVqaDemuxContext *wsvqa = s->priv_data;
00217     AVIOContext *pb = s->pb;
00218     AVStream *st;
00219     unsigned char *header;
00220     unsigned char scratch[VQA_PREAMBLE_SIZE];
00221     unsigned int chunk_tag;
00222     unsigned int chunk_size;
00223 
00224     /* initialize the video decoder stream */
00225     st = avformat_new_stream(s, NULL);
00226     if (!st)
00227         return AVERROR(ENOMEM);
00228     avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
00229     wsvqa->video_stream_index = st->index;
00230     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00231     st->codec->codec_id = CODEC_ID_WS_VQA;
00232     st->codec->codec_tag = 0;  /* no fourcc */
00233 
00234     /* skip to the start of the VQA header */
00235     avio_seek(pb, 20, SEEK_SET);
00236 
00237     /* the VQA header needs to go to the decoder */
00238     st->codec->extradata_size = VQA_HEADER_SIZE;
00239     st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00240     header = (unsigned char *)st->codec->extradata;
00241     if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
00242         VQA_HEADER_SIZE) {
00243         return AVERROR(EIO);
00244     }
00245     st->codec->width = AV_RL16(&header[6]);
00246     st->codec->height = AV_RL16(&header[8]);
00247 
00248     /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
00249     if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
00250         st = avformat_new_stream(s, NULL);
00251         if (!st)
00252             return AVERROR(ENOMEM);
00253         avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
00254         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00255         if (AV_RL16(&header[0]) == 1)
00256             st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
00257         else
00258             st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
00259         st->codec->codec_tag = 0;  /* no tag */
00260         st->codec->sample_rate = AV_RL16(&header[24]);
00261         if (!st->codec->sample_rate)
00262             st->codec->sample_rate = 22050;
00263         st->codec->channels = header[26];
00264         if (!st->codec->channels)
00265             st->codec->channels = 1;
00266         st->codec->bits_per_coded_sample = 16;
00267         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00268             st->codec->bits_per_coded_sample / 4;
00269         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00270 
00271         wsvqa->audio_stream_index = st->index;
00272         wsvqa->audio_samplerate = st->codec->sample_rate;
00273         wsvqa->audio_channels = st->codec->channels;
00274         wsvqa->audio_frame_counter = 0;
00275     }
00276 
00277     /* there are 0 or more chunks before the FINF chunk; iterate until
00278      * FINF has been skipped and the file will be ready to be demuxed */
00279     do {
00280         if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
00281             return AVERROR(EIO);
00282         }
00283         chunk_tag = AV_RB32(&scratch[0]);
00284         chunk_size = AV_RB32(&scratch[4]);
00285 
00286         /* catch any unknown header tags, for curiousity */
00287         switch (chunk_tag) {
00288         case CINF_TAG:
00289         case CINH_TAG:
00290         case CIND_TAG:
00291         case PINF_TAG:
00292         case PINH_TAG:
00293         case PIND_TAG:
00294         case FINF_TAG:
00295         case CMDS_TAG:
00296             break;
00297 
00298         default:
00299             av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
00300                 scratch[0], scratch[1],
00301                 scratch[2], scratch[3]);
00302             break;
00303         }
00304 
00305         avio_skip(pb, chunk_size);
00306     } while (chunk_tag != FINF_TAG);
00307 
00308     return 0;
00309 }
00310 
00311 static int wsvqa_read_packet(AVFormatContext *s,
00312                              AVPacket *pkt)
00313 {
00314     WsVqaDemuxContext *wsvqa = s->priv_data;
00315     AVIOContext *pb = s->pb;
00316     int ret = -1;
00317     unsigned char preamble[VQA_PREAMBLE_SIZE];
00318     unsigned int chunk_type;
00319     unsigned int chunk_size;
00320     int skip_byte;
00321 
00322     while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
00323         chunk_type = AV_RB32(&preamble[0]);
00324         chunk_size = AV_RB32(&preamble[4]);
00325         skip_byte = chunk_size & 0x01;
00326 
00327         if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
00328             av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
00329             return AVERROR_INVALIDDATA;
00330         }
00331 
00332         if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
00333 
00334             if (av_new_packet(pkt, chunk_size))
00335                 return AVERROR(EIO);
00336             ret = avio_read(pb, pkt->data, chunk_size);
00337             if (ret != chunk_size) {
00338                 av_free_packet(pkt);
00339                 return AVERROR(EIO);
00340             }
00341 
00342             if (chunk_type == SND2_TAG) {
00343                 pkt->stream_index = wsvqa->audio_stream_index;
00344                 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
00345                 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
00346             } else if(chunk_type == SND1_TAG) {
00347                 pkt->stream_index = wsvqa->audio_stream_index;
00348                 /* unpacked size is stored in header */
00349                 wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
00350             } else {
00351                 pkt->stream_index = wsvqa->video_stream_index;
00352             }
00353             /* stay on 16-bit alignment */
00354             if (skip_byte)
00355                 avio_skip(pb, 1);
00356 
00357             return ret;
00358         } else {
00359             switch(chunk_type){
00360             case CMDS_TAG:
00361             case SND0_TAG:
00362                 break;
00363             default:
00364                 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
00365             }
00366             avio_skip(pb, chunk_size + skip_byte);
00367         }
00368     }
00369 
00370     return ret;
00371 }
00372 
00373 #if CONFIG_WSAUD_DEMUXER
00374 AVInputFormat ff_wsaud_demuxer = {
00375     .name           = "wsaud",
00376     .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
00377     .priv_data_size = sizeof(WsAudDemuxContext),
00378     .read_probe     = wsaud_probe,
00379     .read_header    = wsaud_read_header,
00380     .read_packet    = wsaud_read_packet,
00381 };
00382 #endif
00383 #if CONFIG_WSVQA_DEMUXER
00384 AVInputFormat ff_wsvqa_demuxer = {
00385     .name           = "wsvqa",
00386     .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
00387     .priv_data_size = sizeof(WsVqaDemuxContext),
00388     .read_probe     = wsvqa_probe,
00389     .read_header    = wsvqa_read_header,
00390     .read_packet    = wsvqa_read_packet,
00391 };
00392 #endif