• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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         av_free(st->codec->extradata);
00244         return AVERROR(EIO);
00245     }
00246     st->codec->width = AV_RL16(&header[6]);
00247     st->codec->height = AV_RL16(&header[8]);
00248 
00249     /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
00250     if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
00251         st = avformat_new_stream(s, NULL);
00252         if (!st)
00253             return AVERROR(ENOMEM);
00254         avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
00255         st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00256         if (AV_RL16(&header[0]) == 1)
00257             st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
00258         else
00259             st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
00260         st->codec->codec_tag = 0;  /* no tag */
00261         st->codec->sample_rate = AV_RL16(&header[24]);
00262         if (!st->codec->sample_rate)
00263             st->codec->sample_rate = 22050;
00264         st->codec->channels = header[26];
00265         if (!st->codec->channels)
00266             st->codec->channels = 1;
00267         st->codec->bits_per_coded_sample = 16;
00268         st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00269             st->codec->bits_per_coded_sample / 4;
00270         st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00271 
00272         wsvqa->audio_stream_index = st->index;
00273         wsvqa->audio_samplerate = st->codec->sample_rate;
00274         wsvqa->audio_channels = st->codec->channels;
00275         wsvqa->audio_frame_counter = 0;
00276     }
00277 
00278     /* there are 0 or more chunks before the FINF chunk; iterate until
00279      * FINF has been skipped and the file will be ready to be demuxed */
00280     do {
00281         if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
00282             av_free(st->codec->extradata);
00283             return AVERROR(EIO);
00284         }
00285         chunk_tag = AV_RB32(&scratch[0]);
00286         chunk_size = AV_RB32(&scratch[4]);
00287 
00288         /* catch any unknown header tags, for curiousity */
00289         switch (chunk_tag) {
00290         case CINF_TAG:
00291         case CINH_TAG:
00292         case CIND_TAG:
00293         case PINF_TAG:
00294         case PINH_TAG:
00295         case PIND_TAG:
00296         case FINF_TAG:
00297         case CMDS_TAG:
00298             break;
00299 
00300         default:
00301             av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
00302                 scratch[0], scratch[1],
00303                 scratch[2], scratch[3]);
00304             break;
00305         }
00306 
00307         avio_skip(pb, chunk_size);
00308     } while (chunk_tag != FINF_TAG);
00309 
00310     return 0;
00311 }
00312 
00313 static int wsvqa_read_packet(AVFormatContext *s,
00314                              AVPacket *pkt)
00315 {
00316     WsVqaDemuxContext *wsvqa = s->priv_data;
00317     AVIOContext *pb = s->pb;
00318     int ret = -1;
00319     unsigned char preamble[VQA_PREAMBLE_SIZE];
00320     unsigned int chunk_type;
00321     unsigned int chunk_size;
00322     int skip_byte;
00323 
00324     while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
00325         chunk_type = AV_RB32(&preamble[0]);
00326         chunk_size = AV_RB32(&preamble[4]);
00327         skip_byte = chunk_size & 0x01;
00328 
00329         if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
00330             av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
00331             return AVERROR_INVALIDDATA;
00332         }
00333 
00334         if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
00335 
00336             if (av_new_packet(pkt, chunk_size))
00337                 return AVERROR(EIO);
00338             ret = avio_read(pb, pkt->data, chunk_size);
00339             if (ret != chunk_size) {
00340                 av_free_packet(pkt);
00341                 return AVERROR(EIO);
00342             }
00343 
00344             if (chunk_type == SND2_TAG) {
00345                 pkt->stream_index = wsvqa->audio_stream_index;
00346                 /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
00347                 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
00348             } else if(chunk_type == SND1_TAG) {
00349                 pkt->stream_index = wsvqa->audio_stream_index;
00350                 /* unpacked size is stored in header */
00351                 wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
00352             } else {
00353                 pkt->stream_index = wsvqa->video_stream_index;
00354             }
00355             /* stay on 16-bit alignment */
00356             if (skip_byte)
00357                 avio_skip(pb, 1);
00358 
00359             return ret;
00360         } else {
00361             switch(chunk_type){
00362             case CMDS_TAG:
00363             case SND0_TAG:
00364                 break;
00365             default:
00366                 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
00367             }
00368             avio_skip(pb, chunk_size + skip_byte);
00369         }
00370     }
00371 
00372     return ret;
00373 }
00374 
00375 #if CONFIG_WSAUD_DEMUXER
00376 AVInputFormat ff_wsaud_demuxer = {
00377     .name           = "wsaud",
00378     .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
00379     .priv_data_size = sizeof(WsAudDemuxContext),
00380     .read_probe     = wsaud_probe,
00381     .read_header    = wsaud_read_header,
00382     .read_packet    = wsaud_read_packet,
00383 };
00384 #endif
00385 #if CONFIG_WSVQA_DEMUXER
00386 AVInputFormat ff_wsvqa_demuxer = {
00387     .name           = "wsvqa",
00388     .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
00389     .priv_data_size = sizeof(WsVqaDemuxContext),
00390     .read_probe     = wsvqa_probe,
00391     .read_header    = wsvqa_read_header,
00392     .read_packet    = wsvqa_read_packet,
00393 };
00394 #endif
Generated on Sat Mar 17 2012 12:57:57 for Libav by doxygen 1.7.1