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 #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
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
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 if (p->buf_size < AUD_HEADER_SIZE + AUD_CHUNK_PREAMBLE_SIZE)
00101 return 0;
00102
00103
00104 field = AV_RL16(&p->buf[0]);
00105 if ((field < 8000) || (field > 48000))
00106 return 0;
00107
00108
00109
00110 if (p->buf[10] & 0xFC)
00111 return 0;
00112
00113
00114
00115 if (p->buf[11] != 99)
00116 return 0;
00117
00118
00119 if (AV_RL32(&p->buf[16]) != AUD_CHUNK_SIGNATURE)
00120 return 0;
00121
00122
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
00143 wsaud->audio_channels = (header[10] & 0x1) + 1;
00144
00145 wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
00146
00147
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;
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
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
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
00202 if (p->buf_size < 12)
00203 return 0;
00204
00205
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
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;
00233
00234
00235 avio_seek(pb, 20, SEEK_SET);
00236
00237
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
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;
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
00279
00280 do {
00281 if (avio_read(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE)
00282 return AVERROR(EIO);
00283 chunk_tag = AV_RB32(&scratch[0]);
00284 chunk_size = AV_RB32(&scratch[4]);
00285
00286
00287 switch (chunk_tag) {
00288 case CINF_TAG:
00289 case CINH_TAG:
00290 case CIND_TAG:
00291 case PINF_TAG:
00292 case PINH_TAG:
00293 case PIND_TAG:
00294 case FINF_TAG:
00295 case CMDS_TAG:
00296 break;
00297
00298 default:
00299 av_log (s, AV_LOG_ERROR, " note: unknown chunk seen (%c%c%c%c)\n",
00300 scratch[0], scratch[1],
00301 scratch[2], scratch[3]);
00302 break;
00303 }
00304
00305 avio_skip(pb, chunk_size);
00306 } while (chunk_tag != FINF_TAG);
00307
00308 return 0;
00309 }
00310
00311 static int wsvqa_read_packet(AVFormatContext *s,
00312 AVPacket *pkt)
00313 {
00314 WsVqaDemuxContext *wsvqa = s->priv_data;
00315 AVIOContext *pb = s->pb;
00316 int ret = -1;
00317 unsigned char preamble[VQA_PREAMBLE_SIZE];
00318 unsigned int chunk_type;
00319 unsigned int chunk_size;
00320 int skip_byte;
00321
00322 while (avio_read(pb, preamble, VQA_PREAMBLE_SIZE) == VQA_PREAMBLE_SIZE) {
00323 chunk_type = AV_RB32(&preamble[0]);
00324 chunk_size = AV_RB32(&preamble[4]);
00325
00326 skip_byte = chunk_size & 0x01;
00327
00328 if ((chunk_type == SND2_TAG || chunk_type == SND1_TAG) && wsvqa->audio_channels == 0) {
00329 av_log(s, AV_LOG_ERROR, "audio chunk without any audio header information found\n");
00330 return AVERROR_INVALIDDATA;
00331 }
00332
00333 if ((chunk_type == SND1_TAG) || (chunk_type == SND2_TAG) || (chunk_type == VQFR_TAG)) {
00334
00335 ret= av_get_packet(pb, pkt, chunk_size);
00336 if (ret<0)
00337 return AVERROR(EIO);
00338
00339 if (chunk_type == SND2_TAG) {
00340 pkt->stream_index = wsvqa->audio_stream_index;
00341
00342 wsvqa->audio_frame_counter += (chunk_size * 2) / wsvqa->audio_channels;
00343 } else if(chunk_type == SND1_TAG) {
00344 pkt->stream_index = wsvqa->audio_stream_index;
00345
00346 wsvqa->audio_frame_counter += AV_RL16(pkt->data) / wsvqa->audio_channels;
00347 } else {
00348 pkt->stream_index = wsvqa->video_stream_index;
00349 }
00350
00351 if (skip_byte)
00352 avio_skip(pb, 1);
00353
00354 return ret;
00355 } else {
00356 switch(chunk_type){
00357 case CMDS_TAG:
00358 case SND0_TAG:
00359 break;
00360 default:
00361 av_log(s, AV_LOG_INFO, "Skipping unknown chunk 0x%08X\n", chunk_type);
00362 }
00363 avio_skip(pb, chunk_size + skip_byte);
00364 }
00365 }
00366
00367 return ret;
00368 }
00369
00370 #if CONFIG_WSAUD_DEMUXER
00371 AVInputFormat ff_wsaud_demuxer = {
00372 .name = "wsaud",
00373 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios audio format"),
00374 .priv_data_size = sizeof(WsAudDemuxContext),
00375 .read_probe = wsaud_probe,
00376 .read_header = wsaud_read_header,
00377 .read_packet = wsaud_read_packet,
00378 };
00379 #endif
00380 #if CONFIG_WSVQA_DEMUXER
00381 AVInputFormat ff_wsvqa_demuxer = {
00382 .name = "wsvqa",
00383 .long_name = NULL_IF_CONFIG_SMALL("Westwood Studios VQA format"),
00384 .priv_data_size = sizeof(WsVqaDemuxContext),
00385 .read_probe = wsvqa_probe,
00386 .read_header = wsvqa_read_header,
00387 .read_packet = wsvqa_read_packet,
00388 };
00389 #endif