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