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

Generated on Tue Nov 4 2014 12:59:24 for ffmpeg by  doxygen 1.7.1