00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "libavutil/intreadwrite.h"
00029 #include "avformat.h"
00030
00031 #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
00032 #define SEAD_TAG MKTAG('S', 'E', 'A', 'D')
00033 #define SNDC_TAG MKTAG('S', 'N', 'D', 'C')
00034 #define SEND_TAG MKTAG('S', 'E', 'N', 'D')
00035 #define SHEN_TAG MKTAG('S', 'H', 'E', 'N')
00036 #define SDEN_TAG MKTAG('S', 'D', 'E', 'N')
00037 #define SEEN_TAG MKTAG('S', 'E', 'E', 'N')
00038 #define ISNh_TAG MKTAG('1', 'S', 'N', 'h')
00039 #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
00040 #define ISNd_TAG MKTAG('1', 'S', 'N', 'd')
00041 #define ISNe_TAG MKTAG('1', 'S', 'N', 'e')
00042 #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
00043 #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
00044 #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
00045 #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
00046 #define kVGT_TAG MKTAG('k', 'V', 'G', 'T')
00047 #define fVGT_TAG MKTAG('f', 'V', 'G', 'T')
00048 #define mTCD_TAG MKTAG('m', 'T', 'C', 'D')
00049 #define MADk_TAG MKTAG('M', 'A', 'D', 'k')
00050 #define MPCh_TAG MKTAG('M', 'P', 'C', 'h')
00051 #define TGQs_TAG MKTAG('T', 'G', 'Q', 's')
00052 #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T')
00053 #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')
00054 #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
00055 #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
00056 #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
00057 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h')
00058 #define MVIf_TAG MKTAG('M', 'V', 'I', 'f')
00059
00060 typedef struct EaDemuxContext {
00061 int big_endian;
00062
00063 enum CodecID video_codec;
00064 AVRational time_base;
00065 int width, height;
00066 int video_stream_index;
00067
00068 enum CodecID audio_codec;
00069 int audio_stream_index;
00070 int audio_frame_counter;
00071
00072 int64_t audio_pts;
00073
00074 int bytes;
00075 int sample_rate;
00076 int num_channels;
00077 int num_samples;
00078 } EaDemuxContext;
00079
00080 static uint32_t read_arbitary(ByteIOContext *pb) {
00081 uint8_t size, byte;
00082 int i;
00083 uint32_t word;
00084
00085 size = get_byte(pb);
00086
00087 word = 0;
00088 for (i = 0; i < size; i++) {
00089 byte = get_byte(pb);
00090 word <<= 8;
00091 word |= byte;
00092 }
00093
00094 return word;
00095 }
00096
00097
00098
00099
00100
00101 static int process_audio_header_elements(AVFormatContext *s)
00102 {
00103 int inHeader = 1;
00104 EaDemuxContext *ea = s->priv_data;
00105 ByteIOContext *pb = s->pb;
00106 int compression_type = -1, revision = -1, revision2 = -1;
00107
00108 ea->bytes = 2;
00109 ea->sample_rate = -1;
00110 ea->num_channels = 1;
00111
00112 while (inHeader) {
00113 int inSubheader;
00114 uint8_t byte;
00115 byte = get_byte(pb);
00116
00117 switch (byte) {
00118 case 0xFD:
00119 av_log (s, AV_LOG_INFO, "entered audio subheader\n");
00120 inSubheader = 1;
00121 while (inSubheader) {
00122 uint8_t subbyte;
00123 subbyte = get_byte(pb);
00124
00125 switch (subbyte) {
00126 case 0x80:
00127 revision = read_arbitary(pb);
00128 av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
00129 break;
00130 case 0x82:
00131 ea->num_channels = read_arbitary(pb);
00132 av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
00133 break;
00134 case 0x83:
00135 compression_type = read_arbitary(pb);
00136 av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
00137 break;
00138 case 0x84:
00139 ea->sample_rate = read_arbitary(pb);
00140 av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
00141 break;
00142 case 0x85:
00143 ea->num_samples = read_arbitary(pb);
00144 av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
00145 break;
00146 case 0x8A:
00147 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
00148 av_log (s, AV_LOG_INFO, "exited audio subheader\n");
00149 inSubheader = 0;
00150 break;
00151 case 0xA0:
00152 revision2 = read_arbitary(pb);
00153 av_log (s, AV_LOG_INFO, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
00154 break;
00155 case 0xFF:
00156 av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
00157 inSubheader = 0;
00158 inHeader = 0;
00159 break;
00160 default:
00161 av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
00162 break;
00163 }
00164 }
00165 break;
00166 case 0xFF:
00167 av_log (s, AV_LOG_INFO, "end of header block reached\n");
00168 inHeader = 0;
00169 break;
00170 default:
00171 av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
00172 break;
00173 }
00174 }
00175
00176 switch (compression_type) {
00177 case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
00178 case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
00179 case -1:
00180 switch (revision) {
00181 case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
00182 case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
00183 case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
00184 case -1: break;
00185 default:
00186 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
00187 return 0;
00188 }
00189 switch (revision2) {
00190 case 8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
00191 case 10: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
00192 case 16: ea->audio_codec = CODEC_ID_MP3; break;
00193 case -1: break;
00194 default:
00195 av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
00196 return 0;
00197 }
00198 break;
00199 default:
00200 av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
00201 return 0;
00202 }
00203
00204 if (ea->sample_rate == -1)
00205 ea->sample_rate = revision==3 ? 48000 : 22050;
00206
00207 return 1;
00208 }
00209
00210
00211
00212
00213
00214 static int process_audio_header_eacs(AVFormatContext *s)
00215 {
00216 EaDemuxContext *ea = s->priv_data;
00217 ByteIOContext *pb = s->pb;
00218 int compression_type;
00219
00220 ea->sample_rate = ea->big_endian ? get_be32(pb) : get_le32(pb);
00221 ea->bytes = get_byte(pb);
00222 ea->num_channels = get_byte(pb);
00223 compression_type = get_byte(pb);
00224 url_fskip(pb, 13);
00225
00226 switch (compression_type) {
00227 case 0:
00228 switch (ea->bytes) {
00229 case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
00230 case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
00231 }
00232 break;
00233 case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
00234 case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
00235 default:
00236 av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
00237 }
00238
00239 return 1;
00240 }
00241
00242
00243
00244
00245
00246 static int process_audio_header_sead(AVFormatContext *s)
00247 {
00248 EaDemuxContext *ea = s->priv_data;
00249 ByteIOContext *pb = s->pb;
00250
00251 ea->sample_rate = get_le32(pb);
00252 ea->bytes = get_le32(pb);
00253 ea->num_channels = get_le32(pb);
00254 ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
00255
00256 return 1;
00257 }
00258
00259 static int process_video_header_mdec(AVFormatContext *s)
00260 {
00261 EaDemuxContext *ea = s->priv_data;
00262 ByteIOContext *pb = s->pb;
00263 url_fskip(pb, 4);
00264 ea->width = get_le16(pb);
00265 ea->height = get_le16(pb);
00266 ea->time_base = (AVRational){1,15};
00267 ea->video_codec = CODEC_ID_MDEC;
00268 return 1;
00269 }
00270
00271 static int process_video_header_vp6(AVFormatContext *s)
00272 {
00273 EaDemuxContext *ea = s->priv_data;
00274 ByteIOContext *pb = s->pb;
00275
00276 url_fskip(pb, 16);
00277 ea->time_base.den = get_le32(pb);
00278 ea->time_base.num = get_le32(pb);
00279 ea->video_codec = CODEC_ID_VP6;
00280
00281 return 1;
00282 }
00283
00284
00285
00286
00287
00288 static int process_ea_header(AVFormatContext *s) {
00289 uint32_t blockid, size = 0;
00290 EaDemuxContext *ea = s->priv_data;
00291 ByteIOContext *pb = s->pb;
00292 int i;
00293
00294 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
00295 unsigned int startpos = url_ftell(pb);
00296 int err = 0;
00297
00298 blockid = get_le32(pb);
00299 size = get_le32(pb);
00300 if (i == 0)
00301 ea->big_endian = size > 0x000FFFFF;
00302 if (ea->big_endian)
00303 size = bswap_32(size);
00304
00305 switch (blockid) {
00306 case ISNh_TAG:
00307 if (get_le32(pb) != EACS_TAG) {
00308 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
00309 return 0;
00310 }
00311 err = process_audio_header_eacs(s);
00312 break;
00313
00314 case SCHl_TAG :
00315 case SHEN_TAG :
00316 blockid = get_le32(pb);
00317 if (blockid == GSTR_TAG) {
00318 url_fskip(pb, 4);
00319 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
00320 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
00321 return 0;
00322 }
00323 err = process_audio_header_elements(s);
00324 break;
00325
00326 case SEAD_TAG:
00327 err = process_audio_header_sead(s);
00328 break;
00329
00330 case MVIh_TAG :
00331 ea->video_codec = CODEC_ID_CMV;
00332 ea->time_base = (AVRational){0,0};
00333 break;
00334
00335 case kVGT_TAG:
00336 ea->video_codec = CODEC_ID_TGV;
00337 ea->time_base = (AVRational){0,0};
00338 break;
00339
00340 case mTCD_TAG :
00341 err = process_video_header_mdec(s);
00342 break;
00343
00344 case MPCh_TAG:
00345 ea->video_codec = CODEC_ID_MPEG2VIDEO;
00346 break;
00347
00348 case pQGT_TAG:
00349 case TGQs_TAG:
00350 ea->video_codec = CODEC_ID_TGQ;
00351 break;
00352
00353 case pIQT_TAG:
00354 ea->video_codec = CODEC_ID_TQI;
00355 break;
00356
00357 case MVhd_TAG :
00358 err = process_video_header_vp6(s);
00359 break;
00360 }
00361
00362 if (err < 0) {
00363 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
00364 return err;
00365 }
00366
00367 url_fseek(pb, startpos + size, SEEK_SET);
00368 }
00369
00370 url_fseek(pb, 0, SEEK_SET);
00371
00372 return 1;
00373 }
00374
00375
00376 static int ea_probe(AVProbeData *p)
00377 {
00378 switch (AV_RL32(&p->buf[0])) {
00379 case ISNh_TAG:
00380 case SCHl_TAG:
00381 case SEAD_TAG:
00382 case SHEN_TAG:
00383 case kVGT_TAG:
00384 case MADk_TAG:
00385 case MPCh_TAG:
00386 case MVhd_TAG:
00387 case MVIh_TAG:
00388 return AVPROBE_SCORE_MAX;
00389 }
00390 return 0;
00391 }
00392
00393 static int ea_read_header(AVFormatContext *s,
00394 AVFormatParameters *ap)
00395 {
00396 EaDemuxContext *ea = s->priv_data;
00397 AVStream *st;
00398
00399 if (!process_ea_header(s))
00400 return AVERROR(EIO);
00401
00402 if (ea->video_codec) {
00403
00404 st = av_new_stream(s, 0);
00405 if (!st)
00406 return AVERROR(ENOMEM);
00407 ea->video_stream_index = st->index;
00408 st->codec->codec_type = CODEC_TYPE_VIDEO;
00409 st->codec->codec_id = ea->video_codec;
00410 st->codec->codec_tag = 0;
00411 st->codec->time_base = ea->time_base;
00412 st->codec->width = ea->width;
00413 st->codec->height = ea->height;
00414 }
00415
00416 if (ea->audio_codec) {
00417
00418 st = av_new_stream(s, 0);
00419 if (!st)
00420 return AVERROR(ENOMEM);
00421 av_set_pts_info(st, 33, 1, ea->sample_rate);
00422 st->codec->codec_type = CODEC_TYPE_AUDIO;
00423 st->codec->codec_id = ea->audio_codec;
00424 st->codec->codec_tag = 0;
00425 st->codec->channels = ea->num_channels;
00426 st->codec->sample_rate = ea->sample_rate;
00427 st->codec->bits_per_coded_sample = ea->bytes * 8;
00428 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00429 st->codec->bits_per_coded_sample / 4;
00430 st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
00431 ea->audio_stream_index = st->index;
00432 ea->audio_frame_counter = 0;
00433 }
00434
00435 return 1;
00436 }
00437
00438 static int ea_read_packet(AVFormatContext *s,
00439 AVPacket *pkt)
00440 {
00441 EaDemuxContext *ea = s->priv_data;
00442 ByteIOContext *pb = s->pb;
00443 int ret = 0;
00444 int packet_read = 0;
00445 unsigned int chunk_type, chunk_size;
00446 int key = 0;
00447 int av_uninit(num_samples);
00448
00449 while (!packet_read) {
00450 chunk_type = get_le32(pb);
00451 chunk_size = ea->big_endian ? get_be32(pb) : get_le32(pb);
00452 if (chunk_size <= 8)
00453 return AVERROR_INVALIDDATA;
00454 chunk_size -= 8;
00455
00456 switch (chunk_type) {
00457
00458 case ISNh_TAG:
00459
00460 if (chunk_size < 32)
00461 return AVERROR_INVALIDDATA;
00462 url_fskip(pb, 32);
00463 chunk_size -= 32;
00464 case ISNd_TAG:
00465 case SCDl_TAG:
00466 case SNDC_TAG:
00467 case SDEN_TAG:
00468 if (!ea->audio_codec) {
00469 url_fskip(pb, chunk_size);
00470 break;
00471 } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
00472 ea->audio_codec == CODEC_ID_MP3) {
00473 num_samples = get_le32(pb);
00474 url_fskip(pb, 8);
00475 chunk_size -= 12;
00476 }
00477 ret = av_get_packet(pb, pkt, chunk_size);
00478 if (ret != chunk_size)
00479 ret = AVERROR(EIO);
00480 else {
00481 pkt->stream_index = ea->audio_stream_index;
00482 pkt->pts = 90000;
00483 pkt->pts *= ea->audio_frame_counter;
00484 pkt->pts /= ea->sample_rate;
00485
00486 switch (ea->audio_codec) {
00487 case CODEC_ID_ADPCM_EA:
00488
00489
00490 ea->audio_frame_counter += ((chunk_size - 12) * 2) /
00491 ea->num_channels;
00492 break;
00493 case CODEC_ID_PCM_S16LE_PLANAR:
00494 case CODEC_ID_MP3:
00495 ea->audio_frame_counter += num_samples;
00496 break;
00497 default:
00498 ea->audio_frame_counter += chunk_size /
00499 (ea->bytes * ea->num_channels);
00500 }
00501 }
00502
00503 packet_read = 1;
00504 break;
00505
00506
00507 case 0:
00508 case ISNe_TAG:
00509 case SCEl_TAG:
00510 case SEND_TAG:
00511 case SEEN_TAG:
00512 ret = AVERROR(EIO);
00513 packet_read = 1;
00514 break;
00515
00516 case MVIh_TAG:
00517 case kVGT_TAG:
00518 case pQGT_TAG:
00519 case TGQs_TAG:
00520 key = PKT_FLAG_KEY;
00521 case MVIf_TAG:
00522 case fVGT_TAG:
00523 url_fseek(pb, -8, SEEK_CUR);
00524 chunk_size += 8;
00525 goto get_video_packet;
00526
00527 case mTCD_TAG:
00528 url_fseek(pb, 8, SEEK_CUR);
00529 chunk_size -= 8;
00530 goto get_video_packet;
00531
00532 case MV0K_TAG:
00533 case MPCh_TAG:
00534 case pIQT_TAG:
00535 key = PKT_FLAG_KEY;
00536 case MV0F_TAG:
00537 get_video_packet:
00538 ret = av_get_packet(pb, pkt, chunk_size);
00539 if (ret != chunk_size)
00540 ret = AVERROR_IO;
00541 else {
00542 pkt->stream_index = ea->video_stream_index;
00543 pkt->flags |= key;
00544 }
00545 packet_read = 1;
00546 break;
00547
00548 default:
00549 url_fseek(pb, chunk_size, SEEK_CUR);
00550 break;
00551 }
00552 }
00553
00554 return ret;
00555 }
00556
00557 AVInputFormat ea_demuxer = {
00558 "ea",
00559 NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
00560 sizeof(EaDemuxContext),
00561 ea_probe,
00562 ea_read_header,
00563 ea_read_packet,
00564 };