]> git.sesse.net Git - ffmpeg/blobdiff - libavformat/westwood_vqa.c
librtmp: append the correct field to the string
[ffmpeg] / libavformat / westwood_vqa.c
index 2eff4cc0a5eec0e4c0c92dbfb9dd18bd03d340ba..77da375e70c835f3a56e89d3b5d31ca11bae7330 100644 (file)
 #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
 
 #define VQA_HEADER_SIZE 0x2A
-#define VQA_FRAMERATE 15
 #define VQA_PREAMBLE_SIZE 8
 
 typedef struct WsVqaDemuxContext {
-    int audio_samplerate;
-    int audio_channels;
-    int audio_bits;
-
+    int version;
+    int bps;
+    int channels;
+    int sample_rate;
     int audio_stream_index;
     int video_stream_index;
-
-    int64_t audio_frame_counter;
 } WsVqaDemuxContext;
 
 static int wsvqa_probe(AVProbeData *p)
@@ -79,8 +76,7 @@ static int wsvqa_probe(AVProbeData *p)
     return AVPROBE_SCORE_MAX;
 }
 
-static int wsvqa_read_header(AVFormatContext *s,
-                             AVFormatParameters *ap)
+static int wsvqa_read_header(AVFormatContext *s)
 {
     WsVqaDemuxContext *wsvqa = s->priv_data;
     AVIOContext *pb = s->pb;
@@ -89,15 +85,16 @@ static int wsvqa_read_header(AVFormatContext *s,
     unsigned char scratch[VQA_PREAMBLE_SIZE];
     unsigned int chunk_tag;
     unsigned int chunk_size;
+    int fps;
 
     /* initialize the video decoder stream */
     st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
-    avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
+    st->start_time = 0;
     wsvqa->video_stream_index = st->index;
     st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
-    st->codec->codec_id = CODEC_ID_WS_VQA;
+    st->codec->codec_id = AV_CODEC_ID_WS_VQA;
     st->codec->codec_tag = 0;  /* no fourcc */
 
     /* skip to the start of the VQA header */
@@ -109,46 +106,31 @@ static int wsvqa_read_header(AVFormatContext *s,
     header = (unsigned char *)st->codec->extradata;
     if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
         VQA_HEADER_SIZE) {
-        av_free(st->codec->extradata);
         return AVERROR(EIO);
     }
     st->codec->width = AV_RL16(&header[6]);
     st->codec->height = AV_RL16(&header[8]);
-
-    /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */
-    if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
-        st = avformat_new_stream(s, NULL);
-        if (!st)
-            return AVERROR(ENOMEM);
-        avpriv_set_pts_info(st, 33, 1, VQA_FRAMERATE);
-        st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
-        if (AV_RL16(&header[0]) == 1)
-            st->codec->codec_id = CODEC_ID_WESTWOOD_SND1;
-        else
-            st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS;
-        st->codec->codec_tag = 0;  /* no tag */
-        st->codec->sample_rate = AV_RL16(&header[24]);
-        if (!st->codec->sample_rate)
-            st->codec->sample_rate = 22050;
-        st->codec->channels = header[26];
-        if (!st->codec->channels)
-            st->codec->channels = 1;
-        st->codec->bits_per_coded_sample = 16;
-        st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
-            st->codec->bits_per_coded_sample / 4;
-        st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
-
-        wsvqa->audio_stream_index = st->index;
-        wsvqa->audio_samplerate = st->codec->sample_rate;
-        wsvqa->audio_channels = st->codec->channels;
-        wsvqa->audio_frame_counter = 0;
+    fps = header[12];
+    st->nb_frames =
+    st->duration  = AV_RL16(&header[4]);
+    if (fps < 1 || fps > 30) {
+        av_log(s, AV_LOG_ERROR, "invalid fps: %d\n", fps);
+        return AVERROR_INVALIDDATA;
     }
+    avpriv_set_pts_info(st, 64, 1, fps);
+
+    wsvqa->version      = AV_RL16(&header[ 0]);
+    wsvqa->sample_rate  = AV_RL16(&header[24]);
+    wsvqa->channels     = header[26];
+    wsvqa->bps          = header[27];
+    wsvqa->audio_stream_index = -1;
+
+    s->ctx_flags |= AVFMTCTX_NOHEADER;
 
     /* there are 0 or more chunks before the FINF chunk; iterate until
      * FINF has been skipped and the file will be ready to be demuxed */
     do {
         if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
-            av_free(st->codec->extradata);
             return AVERROR(EIO);
         }
         chunk_tag = AV_RB32(&scratch[0]);
@@ -195,12 +177,8 @@ static int wsvqa_read_packet(AVFormatContext *s,
         chunk_size = AV_RB32(&preamble[4]);
         skip_byte = chunk_size & 0x01;
 
-        if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
-            av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
-            return AVERROR_INVALIDDATA;
-        }
-
-        if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
+        if ((chunk_type == SND0_TAG) || (chunk_type == SND1_TAG) ||
+            (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
 
             if (av_new_packet(pkt, chunk_size))
                 return AVERROR(EIO);
@@ -210,17 +188,68 @@ static int wsvqa_read_packet(AVFormatContext *s,
                 return AVERROR(EIO);
             }
 
-            if (chunk_type == SND2_TAG) {
-                pkt->stream_index = wsvqa->audio_stream_index;
-                /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
-                wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
-            } else if(chunk_type == SND1_TAG) {
+            switch (chunk_type) {
+            case SND0_TAG:
+            case SND1_TAG:
+            case SND2_TAG:
+                if (wsvqa->audio_stream_index == -1) {
+                    AVStream *st = avformat_new_stream(s, NULL);
+                    if (!st)
+                        return AVERROR(ENOMEM);
+
+                    wsvqa->audio_stream_index = st->index;
+                    if (!wsvqa->sample_rate)
+                        wsvqa->sample_rate = 22050;
+                    if (!wsvqa->channels)
+                        wsvqa->channels = 1;
+                    if (!wsvqa->bps)
+                        wsvqa->bps = 8;
+                    st->codec->sample_rate = wsvqa->sample_rate;
+                    st->codec->bits_per_coded_sample = wsvqa->bps;
+                    st->codec->channels = wsvqa->channels;
+                    st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+
+                    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
+
+                    switch (chunk_type) {
+                    case SND0_TAG:
+                        if (wsvqa->bps == 16)
+                            st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
+                        else
+                            st->codec->codec_id = AV_CODEC_ID_PCM_U8;
+                        break;
+                    case SND1_TAG:
+                        st->codec->codec_id = AV_CODEC_ID_WESTWOOD_SND1;
+                        break;
+                    case SND2_TAG:
+                        st->codec->codec_id = AV_CODEC_ID_ADPCM_IMA_WS;
+                        st->codec->extradata_size = 2;
+                        st->codec->extradata = av_mallocz(2 + FF_INPUT_BUFFER_PADDING_SIZE);
+                        if (!st->codec->extradata)
+                            return AVERROR(ENOMEM);
+                        AV_WL16(st->codec->extradata, wsvqa->version);
+                        break;
+                    }
+                }
+
                 pkt->stream_index = wsvqa->audio_stream_index;
-                /* unpacked size is stored in header */
-                wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
-            } else {
+                switch (chunk_type) {
+                case SND1_TAG:
+                    /* unpacked size is stored in header */
+                    pkt->duration = AV_RL16(pkt->data) / wsvqa->channels;
+                    break;
+                case SND2_TAG:
+                    /* 2 samples/byte, 1 or 2 samples per frame depending on stereo */
+                    pkt->duration = (chunk_size * 2) / wsvqa->channels;
+                    break;
+                }
+                break;
+            case VQFR_TAG:
                 pkt->stream_index = wsvqa->video_stream_index;
+                pkt->duration = 1;
+                break;
             }
+
             /* stay on 16-bit alignment */
             if (skip_byte)
                 avio_skip(pb, 1);
@@ -229,7 +258,6 @@ static int wsvqa_read_packet(AVFormatContext *s,
         } else {
             switch(chunk_type){
             case CMDS_TAG:
-            case SND0_TAG:
                 break;
             default:
                 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
@@ -243,7 +271,7 @@ static int wsvqa_read_packet(AVFormatContext *s,
 
 AVInputFormat ff_wsvqa_demuxer = {
     .name           = "wsvqa",
-    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
+    .long_name      = NULL_IF_CONFIG_SMALL("Westwood Studios VQA"),
     .priv_data_size = sizeof(WsVqaDemuxContext),
     .read_probe     = wsvqa_probe,
     .read_header    = wsvqa_read_header,