00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00036 #include "avformat.h"
00037
00038 #define AUD_HEADER_SIZE 12
00039 #define AUD_CHUNK_PREAMBLE_SIZE 8
00040 #define AUD_CHUNK_SIGNATURE 0x0000DEAF
00041
00042 #define FORM_TAG MKBETAG('F', 'O', 'R', 'M')
00043 #define WVQA_TAG MKBETAG('W', 'V', 'Q', 'A')
00044 #define VQHD_TAG MKBETAG('V', 'Q', 'H', 'D')
00045 #define FINF_TAG MKBETAG('F', 'I', 'N', 'F')
00046 #define SND0_TAG MKBETAG('S', 'N', 'D', '0')
00047 #define SND1_TAG MKBETAG('S', 'N', 'D', '1')
00048 #define SND2_TAG MKBETAG('S', 'N', 'D', '2')
00049 #define VQFR_TAG MKBETAG('V', 'Q', 'F', 'R')
00050
00051
00052 #define CINF_TAG MKBETAG('C', 'I', 'N', 'F')
00053 #define CINH_TAG MKBETAG('C', 'I', 'N', 'H')
00054 #define CIND_TAG MKBETAG('C', 'I', 'N', 'D')
00055 #define PINF_TAG MKBETAG('P', 'I', 'N', 'F')
00056 #define PINH_TAG MKBETAG('P', 'I', 'N', 'H')
00057 #define PIND_TAG MKBETAG('P', 'I', 'N', 'D')
00058 #define CMDS_TAG MKBETAG('C', 'M', 'D', 'S')
00059
00060 #define VQA_HEADER_SIZE 0x2A
00061 #define VQA_FRAMERATE 15
00062 #define VQA_VIDEO_PTS_INC (90000 / VQA_FRAMERATE)
00063 #define VQA_PREAMBLE_SIZE 8
00064
00065 typedef struct WsAudDemuxContext {
00066 int audio_samplerate;
00067 int audio_channels;
00068 int audio_bits;
00069 int 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 int64_t video_pts;
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)
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[11] != 99)
00111 return 0;
00112
00113
00114 return AVPROBE_SCORE_MAX / 2;
00115 }
00116
00117 static int wsaud_read_header(AVFormatContext *s,
00118 AVFormatParameters *ap)
00119 {
00120 WsAudDemuxContext *wsaud = s->priv_data;
00121 ByteIOContext *pb = s->pb;
00122 AVStream *st;
00123 unsigned char header[AUD_HEADER_SIZE];
00124
00125 if (get_buffer(pb, header, AUD_HEADER_SIZE) != AUD_HEADER_SIZE)
00126 return AVERROR(EIO);
00127 wsaud->audio_samplerate = AV_RL16(&header[0]);
00128 if (header[11] == 99)
00129 wsaud->audio_type = CODEC_ID_ADPCM_IMA_WS;
00130 else
00131 return AVERROR_INVALIDDATA;
00132
00133
00134 wsaud->audio_channels = (header[10] & 0x1) + 1;
00135
00136 wsaud->audio_bits = (((header[10] & 0x2) >> 1) + 1) * 8;
00137
00138
00139 st = av_new_stream(s, 0);
00140 if (!st)
00141 return AVERROR(ENOMEM);
00142 av_set_pts_info(st, 33, 1, wsaud->audio_samplerate);
00143 st->codec->codec_type = CODEC_TYPE_AUDIO;
00144 st->codec->codec_id = wsaud->audio_type;
00145 st->codec->codec_tag = 0;
00146 st->codec->channels = wsaud->audio_channels;
00147 st->codec->sample_rate = wsaud->audio_samplerate;
00148 st->codec->bits_per_sample = wsaud->audio_bits;
00149 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00150 st->codec->bits_per_sample / 4;
00151 st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
00152
00153 wsaud->audio_stream_index = st->index;
00154 wsaud->audio_frame_counter = 0;
00155
00156 return 0;
00157 }
00158
00159 static int wsaud_read_packet(AVFormatContext *s,
00160 AVPacket *pkt)
00161 {
00162 WsAudDemuxContext *wsaud = s->priv_data;
00163 ByteIOContext *pb = s->pb;
00164 unsigned char preamble[AUD_CHUNK_PREAMBLE_SIZE];
00165 unsigned int chunk_size;
00166 int ret = 0;
00167
00168 if (get_buffer(pb, preamble, AUD_CHUNK_PREAMBLE_SIZE) !=
00169 AUD_CHUNK_PREAMBLE_SIZE)
00170 return AVERROR(EIO);
00171
00172
00173 if (AV_RL32(&preamble[4]) != AUD_CHUNK_SIGNATURE)
00174 return AVERROR_INVALIDDATA;
00175
00176 chunk_size = AV_RL16(&preamble[0]);
00177 ret= av_get_packet(pb, pkt, chunk_size);
00178 if (ret != chunk_size)
00179 return AVERROR(EIO);
00180 pkt->stream_index = wsaud->audio_stream_index;
00181 pkt->pts = wsaud->audio_frame_counter;
00182 pkt->pts /= wsaud->audio_samplerate;
00183
00184
00185 wsaud->audio_frame_counter += (chunk_size * 2) / wsaud->audio_channels;
00186
00187 return ret;
00188 }
00189
00190 static int wsaud_read_close(AVFormatContext *s)
00191 {
00192
00193
00194 return 0;
00195 }
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 ByteIOContext *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 = CODEC_TYPE_VIDEO;
00230 st->codec->codec_id = CODEC_ID_WS_VQA;
00231 st->codec->codec_tag = 0;
00232
00233
00234 url_fseek(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 (get_buffer(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 = CODEC_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_sample = 16;
00267 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00268 st->codec->bits_per_sample / 4;
00269 st->codec->block_align = st->codec->channels * st->codec->bits_per_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 (get_buffer(pb, scratch, VQA_PREAMBLE_SIZE) != VQA_PREAMBLE_SIZE) {
00281 av_free(st->codec->extradata);
00282 return AVERROR(EIO);
00283 }
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 static int wsvqa_read_close(AVFormatContext *s)
00373 {
00374
00375
00376 return 0;
00377 }
00378
00379 #ifdef CONFIG_WSAUD_DEMUXER
00380 AVInputFormat wsaud_demuxer = {
00381 "wsaud",
00382 "Westwood Studios audio format",
00383 sizeof(WsAudDemuxContext),
00384 wsaud_probe,
00385 wsaud_read_header,
00386 wsaud_read_packet,
00387 wsaud_read_close,
00388 };
00389 #endif
00390 #ifdef CONFIG_WSVQA_DEMUXER
00391 AVInputFormat wsvqa_demuxer = {
00392 "wsvqa",
00393 "Westwood Studios VQA format",
00394 sizeof(WsVqaDemuxContext),
00395 wsvqa_probe,
00396 wsvqa_read_header,
00397 wsvqa_read_packet,
00398 wsvqa_read_close,
00399 };
00400 #endif