X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavformat%2Fwestwood_vqa.c;h=77da375e70c835f3a56e89d3b5d31ca11bae7330;hb=eb8b05a3824a9fa85e20d603595ac8a3b83505d4;hp=4d6397b88c2a64b39dcc474ed272a9ed7fabaa7b;hpb=4340a6363e9ff75dc4e8ce14dc96671623494ed8;p=ffmpeg diff --git a/libavformat/westwood_vqa.c b/libavformat/westwood_vqa.c index 4d6397b88c2..77da375e70c 100644 --- a/libavformat/westwood_vqa.c +++ b/libavformat/westwood_vqa.c @@ -54,7 +54,10 @@ #define VQA_PREAMBLE_SIZE 8 typedef struct WsVqaDemuxContext { - int audio_channels; + int version; + int bps; + int channels; + int sample_rate; int audio_stream_index; int video_stream_index; } WsVqaDemuxContext; @@ -73,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; @@ -83,7 +85,7 @@ static int wsvqa_read_header(AVFormatContext *s, unsigned char scratch[VQA_PREAMBLE_SIZE]; unsigned int chunk_tag; unsigned int chunk_size; - int fps, version, flags, sample_rate, channels; + int fps; /* initialize the video decoder stream */ st = avformat_new_stream(s, NULL); @@ -92,7 +94,7 @@ static int wsvqa_read_header(AVFormatContext *s, 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 */ @@ -104,65 +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]); 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); - /* initialize the audio decoder stream for VQA v1 or nonzero samplerate */ - version = AV_RL16(&header[ 0]); - flags = AV_RL16(&header[ 2]); - sample_rate = AV_RL16(&header[24]); - channels = header[26]; - if (sample_rate || (version == 1 && flags == 1)) { - st = avformat_new_stream(s, NULL); - if (!st) - return AVERROR(ENOMEM); - st->start_time = 0; - st->codec->codec_type = AVMEDIA_TYPE_AUDIO; - - if (!sample_rate) - sample_rate = 22050; - st->codec->sample_rate = sample_rate; - avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate); - - if (!channels) - channels = 1; - st->codec->channels = channels; - - switch (version) { - case 1: - st->codec->codec_id = CODEC_ID_WESTWOOD_SND1; - break; - case 2: - case 3: - st->codec->codec_id = CODEC_ID_ADPCM_IMA_WS; - st->codec->bits_per_coded_sample = 4; - st->codec->bit_rate = channels * sample_rate * 4; - break; - default: - /* NOTE: version 0 is supposedly raw pcm_u8 or pcm_s16le, but we do - not have any samples to validate this */ - av_log_ask_for_sample(s, "VQA version %d audio\n", version); - return AVERROR_PATCHWELCOME; - } + 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; - wsvqa->audio_stream_index = st->index; - wsvqa->audio_channels = st->codec->channels; - } + 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]); @@ -209,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); @@ -224,18 +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 */ - pkt->duration = (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 */ - pkt->duration = 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); @@ -244,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); @@ -258,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,