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