00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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_PREAMBLE_SIZE 8
00064
00065 typedef struct WsAudDemuxContext {
00066 int audio_samplerate;
00067 int audio_channels;
00068 int audio_bits;
00069 enum CodecID audio_type;
00070 int audio_stream_index;
00071 int64_t audio_frame_counter;
00072 } WsAudDemuxContext;
00073
00074 typedef struct WsVqaDemuxContext {
00075 int audio_samplerate;
00076 int audio_channels;
00077 int audio_bits;
00078
00079 int audio_stream_index;
00080 int video_stream_index;
00081
00082 int64_t audio_frame_counter;
00083 } WsVqaDemuxContext;
00084
00085 static int wsaud_probe(AVProbeData *p)
00086 {
00087 int field;
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
00100 return 0;
00101
00102
00103 field = AV_RL16(&p->buf[0]);
00104 if ((field < 8000) || (field > 48000))
00105 return 0;
00106
00107
00108
00109 if (p->buf[10] & 0xFC)
00110 return 0;
00111
00112
00113
00114 if (p->buf[11] != 99)
00115 return 0;
00116
00117
00118 if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
00119 return 0;
00120
00121
00122 return AVPROBE_SCORE_MAX / 2;
00123 }
00124
00125 static int wsaud_read_header(AVFormatContext *s,
00126 AVFormatParameters *ap)
00127 {
00128 WsAudDemuxContext *wsaud = s->priv_data;
00129 AVIOContext *pb = s->pb;
00130 AVStream *st;
00131 unsigned char header[AUD_HEADER_SIZE];
00132
00133 if (avio_read(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
00134 return AVERROR(EIO);
00135 wsaud->audio_samplerate = AV_RL16(&header[0]);
00136 if (header[11] == 99)
00137 wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
00138 else
00139 return AVERROR_INVALIDDATA;
00140
00141
00142 wsaud->audio_channels = (header[10] & 0x1) + 1;
00143
00144 wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
00145
00146
00147 st = av_new_stream(s, 0);
00148 if (!st)
00149 return AVERROR(ENOMEM);
00150 av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
00151 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00152 st->codec->codec_id = wsaud->audio_type;
00153 st->codec->codec_tag = 0;
00154 st->codec->channels = wsaud->audio_channels;
00155 st->codec->sample_rate = wsaud->audio_samplerate;
00156 st->codec->bits_per_coded_sample = wsaud->audio_bits;
00157 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00158 st->codec->bits_per_coded_sample / 4;
00159 st->codec->block_align = st->codec->channels * st->codec->bits_per_coded_sample;
00160
00161 wsaud->audio_stream_index = st->index;
00162 wsaud->audio_frame_counter = 0;
00163
00164 return 0;
00165 }
00166
00167 static int wsaud_read_packet(AVFormatContext *s,
00168 AVPacket *pkt)
00169 {
00170 WsAudDemuxContext *wsaud = s->priv_data;
00171 AVIOContext *pb = s->pb;
00172 unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
00173 unsigned int chunk_size;
00174 int ret = 0;
00175
00176 if (avio_read(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
00177 AUD_CHUNK_PREAMBLE_SIZE)
00178 return AVERROR(EIO);
00179
00180
00181 if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
00182 return AVERROR_INVALIDDATA;
00183
00184 chunk_size = AV_RL16(&preamble[0]);
00185 ret= av_get_packet(pb, pkt, chunk_size);
00186 if (ret != chunk_size)
00187 return AVERROR(EIO);
00188 pkt->stream_index = wsaud->audio_stream_index;
00189 pkt->pts = wsaud->audio_frame_counter;
00190 pkt->pts /= wsaud->audio_samplerate;
00191
00192
00193 wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
00194
00195 return ret;
00196 }
00197
00198 static int wsvqa_probe(AVProbeData *p)
00199 {
00200
00201 if (p->buf_size < 12)
00202 return 0;
00203
00204
00205 if ((AV_RB32(&p->buf[0]) != FORM_TAG) ||
00206 (AV_RB32(&p->buf[8]) != WVQA_TAG))
00207 return 0;
00208
00209 return AVPROBE_SCORE_MAX;
00210 }
00211
00212 static int wsvqa_read_header(AVFormatContext *s,
00213 AVFormatParameters *ap)
00214 {
00215 WsVqaDemuxContext *wsvqa = s->priv_data;
00216 AVIOContext *pb = s->pb;
00217 AVStream *st;
00218 unsigned char *header;
00219 unsigned char scratch[VQA_PREAMBLE_SIZE];
00220 unsigned int chunk_tag;
00221 unsigned int chunk_size;
00222
00223
00224 st = av_new_stream(s, 0);
00225 if (!st)
00226 return AVERROR(ENOMEM);
00227 av_set_pts_info(st, 33, 1, VQA_FRAMERATE);
00228 wsvqa->video_stream_index = st->index;
00229 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00230 st->codec->codec_id = CODEC_ID_WS_VQA;
00231 st->codec->codec_tag = 0;
00232
00233
00234 avio_seek(pb, 20, SEEK_SET);
00235
00236
00237 st->codec->extradata_size = VQA_HEADER_SIZE;
00238 st->codec->extradata = av_mallocz(VQA_HEADER_SIZE + FF_INPUT_BUFFER_PADDING_SIZE);
00239 header = (unsigned char *)st->codec->extradata;
00240 if (avio_read(pb, st->codec->extradata, VQA_HEADER_SIZE) !=
00241 VQA_HEADER_SIZE) {
00242 av_free(st->codec->extradata);
00243 return AVERROR(EIO);
00244 }
00245 st->codec->width = AV_RL16(&header[6]);
00246 st->codec->height = AV_RL16(&header[8]);
00247
00248
00249 if (AV_RL16(&header[24]) || (AV_RL16(&header[0]) == 1 && AV_RL16(&header[2]) == 1)) {
00250 st = av_new_stream(s, 0);
00251 if (!st)
00252 return AVERROR(ENOMEM);
00253 av_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;
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
00278
00279 do {
00280 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
00281 return AVERROR(EIO);
00282 chunk_tag = AV_RB32(&scratch[0]);
00283 chunk_size = AV_RB32(&scratch[4]);
00284
00285
00286 switch (chunk_tag) {
00287 case CINF_TAG:
00288 case CINH_TAG:
00289 case CIND_TAG:
00290 case PINF_TAG:
00291 case PINH_TAG:
00292 case PIND_TAG:
00293 case FINF_TAG:
00294 case CMDS_TAG:
00295 break;
00296
00297 default:
00298 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
00299 scratch[0], scratch[1],
00300 scratch[2], scratch[3]);
00301 break;
00302 }
00303
00304 avio_skip(pb, chunk_size);
00305 } while (chunk_tag != FINF_TAG);
00306
00307 return 0;
00308 }
00309
00310 static int wsvqa_read_packet(AVFormatContext *s,
00311 AVPacket *pkt)
00312 {
00313 WsVqaDemuxContext *wsvqa = s->priv_data;
00314 AVIOContext *pb = s->pb;
00315 int ret = -1;
00316 unsigned char preamble[VQA_PREAMBLE_SIZE];
00317 unsigned int chunk_type;
00318 unsigned int chunk_size;
00319 int skip_byte;
00320
00321 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
00322 chunk_type = AV_RB32(&preamble[0]);
00323 chunk_size = AV_RB32(&preamble[4]);
00324 skip_byte = chunk_size & 0x01;
00325
00326 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
00327
00328 if (av_new_packet(pkt, chunk_size))
00329 return AVERROR(EIO);
00330 ret = avio_read(pb, pkt->data, chunk_size);
00331 if (ret != chunk_size) {
00332 av_free_packet(pkt);
00333 return AVERROR(EIO);
00334 }
00335
00336 if (chunk_type == SND2_TAG) {
00337 pkt->stream_index = wsvqa->audio_stream_index;
00338
00339 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
00340 } else if(chunk_type == SND1_TAG) {
00341 pkt->stream_index = wsvqa->audio_stream_index;
00342
00343 wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
00344 } else {
00345 pkt->stream_index = wsvqa->video_stream_index;
00346 }
00347
00348 if (skip_byte)
00349 avio_skip(pb, 1);
00350
00351 return ret;
00352 } else {
00353 switch(chunk_type){
00354 case CMDS_TAG:
00355 case SND0_TAG:
00356 break;
00357 default:
00358 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
00359 }
00360 avio_skip(pb, chunk_size + skip_byte);
00361 }
00362 }
00363
00364 return ret;
00365 }
00366
00367 #if CONFIG_WSAUD_DEMUXER
00368 AVInputFormat ff_wsaud_demuxer = {
00369 "wsaud",
00370 NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
00371 sizeof(WsAudDemuxContext),
00372 wsaud_probe,
00373 wsaud_read_header,
00374 wsaud_read_packet,
00375 };
00376 #endif
00377 #if CONFIG_WSVQA_DEMUXER
00378 AVInputFormat ff_wsvqa_demuxer = {
00379 "wsvqa",
00380 NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
00381 sizeof(WsVqaDemuxContext),
00382 wsvqa_probe,
00383 wsvqa_read_header,
00384 wsvqa_read_packet,
00385 };
00386 #endif