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 AVCodecID video_codec;
00067 AVRational time_base;
00068 int width, height;
00069 int nb_frames;
00070 int video_stream_index;
00071
00072 enum AVCodecID audio_codec;
00073 int audio_stream_index;
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 = AV_CODEC_ID_PCM_S16LE; break;
00179 case 7: ea->audio_codec = AV_CODEC_ID_ADPCM_EA; break;
00180 case -1:
00181 switch (revision) {
00182 case 1: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R1; break;
00183 case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
00184 case 3: ea->audio_codec = AV_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 = AV_CODEC_ID_PCM_S16LE_PLANAR; break;
00192 case 10: ea->audio_codec = AV_CODEC_ID_ADPCM_EA_R2; break;
00193 case 16: ea->audio_codec = AV_CODEC_ID_MP3; break;
00194 case -1: break;
00195 default:
00196 ea->audio_codec = AV_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 ea->num_channels = avio_r8(pb);
00225 compression_type = avio_r8(pb);
00226 avio_skip(pb, 13);
00227
00228 switch (compression_type) {
00229 case 0:
00230 switch (ea->bytes) {
00231 case 1: ea->audio_codec = AV_CODEC_ID_PCM_S8; break;
00232 case 2: ea->audio_codec = AV_CODEC_ID_PCM_S16LE; break;
00233 }
00234 break;
00235 case 1: ea->audio_codec = AV_CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
00236 case 2: ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_EACS; break;
00237 default:
00238 av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
00239 }
00240
00241 return 1;
00242 }
00243
00244
00245
00246
00247
00248 static int process_audio_header_sead(AVFormatContext *s)
00249 {
00250 EaDemuxContext *ea = s->priv_data;
00251 AVIOContext *pb = s->pb;
00252
00253 ea->sample_rate = avio_rl32(pb);
00254 ea->bytes = avio_rl32(pb);
00255 ea->num_channels = avio_rl32(pb);
00256 ea->audio_codec = AV_CODEC_ID_ADPCM_IMA_EA_SEAD;
00257
00258 return 1;
00259 }
00260
00261 static int process_video_header_mdec(AVFormatContext *s)
00262 {
00263 EaDemuxContext *ea = s->priv_data;
00264 AVIOContext *pb = s->pb;
00265 avio_skip(pb, 4);
00266 ea->width = avio_rl16(pb);
00267 ea->height = avio_rl16(pb);
00268 ea->time_base = (AVRational){1,15};
00269 ea->video_codec = AV_CODEC_ID_MDEC;
00270 return 1;
00271 }
00272
00273 static int process_video_header_vp6(AVFormatContext *s)
00274 {
00275 EaDemuxContext *ea = s->priv_data;
00276 AVIOContext *pb = s->pb;
00277
00278 avio_skip(pb, 8);
00279 ea->nb_frames = avio_rl32(pb);
00280 avio_skip(pb, 4);
00281 ea->time_base.den = avio_rl32(pb);
00282 ea->time_base.num = avio_rl32(pb);
00283 ea->video_codec = AV_CODEC_ID_VP6;
00284
00285 return 1;
00286 }
00287
00288 static int process_video_header_cmv(AVFormatContext *s)
00289 {
00290 EaDemuxContext *ea = s->priv_data;
00291 int fps;
00292
00293 avio_skip(s->pb, 10);
00294 fps = avio_rl16(s->pb);
00295 if (fps)
00296 ea->time_base = (AVRational){1, fps};
00297 ea->video_codec = AV_CODEC_ID_CMV;
00298
00299 return 0;
00300 }
00301
00302
00303
00304
00305
00306 static int process_ea_header(AVFormatContext *s) {
00307 uint32_t blockid, size = 0;
00308 EaDemuxContext *ea = s->priv_data;
00309 AVIOContext *pb = s->pb;
00310 int i;
00311
00312 for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
00313 unsigned int startpos = avio_tell(pb);
00314 int err = 0;
00315
00316 blockid = avio_rl32(pb);
00317 size = avio_rl32(pb);
00318 if (i == 0)
00319 ea->big_endian = size > 0x000FFFFF;
00320 if (ea->big_endian)
00321 size = av_bswap32(size);
00322
00323 switch (blockid) {
00324 case ISNh_TAG:
00325 if (avio_rl32(pb) != EACS_TAG) {
00326 av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
00327 return 0;
00328 }
00329 err = process_audio_header_eacs(s);
00330 break;
00331
00332 case SCHl_TAG :
00333 case SHEN_TAG :
00334 blockid = avio_rl32(pb);
00335 if (blockid == GSTR_TAG) {
00336 avio_skip(pb, 4);
00337 } else if ((blockid & 0xFFFF)!=PT00_TAG) {
00338 av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
00339 return 0;
00340 }
00341 err = process_audio_header_elements(s);
00342 break;
00343
00344 case SEAD_TAG:
00345 err = process_audio_header_sead(s);
00346 break;
00347
00348 case MVIh_TAG :
00349 err = process_video_header_cmv(s);
00350 break;
00351
00352 case kVGT_TAG:
00353 ea->video_codec = AV_CODEC_ID_TGV;
00354 break;
00355
00356 case mTCD_TAG :
00357 err = process_video_header_mdec(s);
00358 break;
00359
00360 case MPCh_TAG:
00361 ea->video_codec = AV_CODEC_ID_MPEG2VIDEO;
00362 break;
00363
00364 case pQGT_TAG:
00365 case TGQs_TAG:
00366 ea->video_codec = AV_CODEC_ID_TGQ;
00367 break;
00368
00369 case pIQT_TAG:
00370 ea->video_codec = AV_CODEC_ID_TQI;
00371 break;
00372
00373 case MADk_TAG :
00374 ea->video_codec = AV_CODEC_ID_MAD;
00375 break;
00376
00377 case MVhd_TAG :
00378 err = process_video_header_vp6(s);
00379 break;
00380 }
00381
00382 if (err < 0) {
00383 av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
00384 return err;
00385 }
00386
00387 avio_seek(pb, startpos + size, SEEK_SET);
00388 }
00389
00390 avio_seek(pb, 0, SEEK_SET);
00391
00392 return 1;
00393 }
00394
00395
00396 static int ea_probe(AVProbeData *p)
00397 {
00398 switch (AV_RL32(&p->buf[0])) {
00399 case ISNh_TAG:
00400 case SCHl_TAG:
00401 case SEAD_TAG:
00402 case SHEN_TAG:
00403 case kVGT_TAG:
00404 case MADk_TAG:
00405 case MPCh_TAG:
00406 case MVhd_TAG:
00407 case MVIh_TAG:
00408 break;
00409 default:
00410 return 0;
00411 }
00412 if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
00413 return 0;
00414 return AVPROBE_SCORE_MAX;
00415 }
00416
00417 static int ea_read_header(AVFormatContext *s)
00418 {
00419 EaDemuxContext *ea = s->priv_data;
00420 AVStream *st;
00421
00422 if (process_ea_header(s)<=0)
00423 return AVERROR(EIO);
00424
00425 if (ea->video_codec) {
00426
00427 st = avformat_new_stream(s, NULL);
00428 if (!st)
00429 return AVERROR(ENOMEM);
00430 ea->video_stream_index = st->index;
00431 st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
00432 st->codec->codec_id = ea->video_codec;
00433
00434 if (st->codec->codec_id == AV_CODEC_ID_MPEG2VIDEO)
00435 st->need_parsing = AVSTREAM_PARSE_HEADERS;
00436 st->codec->codec_tag = 0;
00437 st->codec->width = ea->width;
00438 st->codec->height = ea->height;
00439 st->duration = st->nb_frames = ea->nb_frames;
00440 if (ea->time_base.num)
00441 avpriv_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
00442 st->r_frame_rate =
00443 st->avg_frame_rate = av_inv_q(ea->time_base);
00444 }
00445
00446 if (ea->audio_codec) {
00447 if (ea->num_channels <= 0) {
00448 av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
00449 ea->audio_codec = 0;
00450 return 1;
00451 }
00452 if (ea->sample_rate <= 0) {
00453 av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
00454 ea->audio_codec = 0;
00455 return 1;
00456 }
00457 if (ea->bytes <= 0) {
00458 av_log(s, AV_LOG_ERROR, "Invalid number of bytes per sample: %d\n", ea->bytes);
00459 ea->audio_codec = AV_CODEC_ID_NONE;
00460 return 1;
00461 }
00462
00463
00464 st = avformat_new_stream(s, NULL);
00465 if (!st)
00466 return AVERROR(ENOMEM);
00467 avpriv_set_pts_info(st, 33, 1, ea->sample_rate);
00468 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
00469 st->codec->codec_id = ea->audio_codec;
00470 st->codec->codec_tag = 0;
00471 st->codec->channels = ea->num_channels;
00472 st->codec->sample_rate = ea->sample_rate;
00473 st->codec->bits_per_coded_sample = ea->bytes * 8;
00474 st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
00475 st->codec->bits_per_coded_sample / 4;
00476 st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
00477 ea->audio_stream_index = st->index;
00478 st->start_time = 0;
00479 }
00480
00481 return 1;
00482 }
00483
00484 static int ea_read_packet(AVFormatContext *s,
00485 AVPacket *pkt)
00486 {
00487 EaDemuxContext *ea = s->priv_data;
00488 AVIOContext *pb = s->pb;
00489 int ret = 0;
00490 int packet_read = 0;
00491 int partial_packet = 0;
00492 unsigned int chunk_type, chunk_size;
00493 int key = 0;
00494 int av_uninit(num_samples);
00495
00496 while (!packet_read || partial_packet) {
00497 chunk_type = avio_rl32(pb);
00498 chunk_size = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
00499 if (chunk_size <= 8)
00500 return AVERROR_INVALIDDATA;
00501 chunk_size -= 8;
00502
00503 switch (chunk_type) {
00504
00505 case ISNh_TAG:
00506
00507 if (chunk_size < 32)
00508 return AVERROR_INVALIDDATA;
00509 avio_skip(pb, 32);
00510 chunk_size -= 32;
00511 case ISNd_TAG:
00512 case SCDl_TAG:
00513 case SNDC_TAG:
00514 case SDEN_TAG:
00515 if (!ea->audio_codec) {
00516 avio_skip(pb, chunk_size);
00517 break;
00518 } else if (ea->audio_codec == AV_CODEC_ID_PCM_S16LE_PLANAR ||
00519 ea->audio_codec == AV_CODEC_ID_MP3) {
00520 num_samples = avio_rl32(pb);
00521 avio_skip(pb, 8);
00522 chunk_size -= 12;
00523 }
00524 if (partial_packet) {
00525 av_log_ask_for_sample(s, "video header followed by audio packet not supported.\n");
00526 av_free_packet(pkt);
00527 partial_packet = 0;
00528 }
00529 ret = av_get_packet(pb, pkt, chunk_size);
00530 if (ret < 0)
00531 return ret;
00532 pkt->stream_index = ea->audio_stream_index;
00533
00534 switch (ea->audio_codec) {
00535 case AV_CODEC_ID_ADPCM_EA:
00536 case AV_CODEC_ID_ADPCM_EA_R1:
00537 case AV_CODEC_ID_ADPCM_EA_R2:
00538 case AV_CODEC_ID_ADPCM_IMA_EA_EACS:
00539 pkt->duration = AV_RL32(pkt->data);
00540 break;
00541 case AV_CODEC_ID_ADPCM_EA_R3:
00542 pkt->duration = AV_RB32(pkt->data);
00543 break;
00544 case AV_CODEC_ID_ADPCM_IMA_EA_SEAD:
00545 pkt->duration = ret * 2 / ea->num_channels;
00546 break;
00547 case AV_CODEC_ID_PCM_S16LE_PLANAR:
00548 case AV_CODEC_ID_MP3:
00549 pkt->duration = num_samples;
00550 break;
00551 default:
00552 pkt->duration = chunk_size / (ea->bytes * ea->num_channels);
00553 }
00554
00555 packet_read = 1;
00556 break;
00557
00558
00559 case 0:
00560 case ISNe_TAG:
00561 case SCEl_TAG:
00562 case SEND_TAG:
00563 case SEEN_TAG:
00564 ret = AVERROR(EIO);
00565 packet_read = 1;
00566 break;
00567
00568 case MVIh_TAG:
00569 case kVGT_TAG:
00570 case pQGT_TAG:
00571 case TGQs_TAG:
00572 case MADk_TAG:
00573 key = AV_PKT_FLAG_KEY;
00574 case MVIf_TAG:
00575 case fVGT_TAG:
00576 case MADm_TAG:
00577 case MADe_TAG:
00578 avio_seek(pb, -8, SEEK_CUR);
00579 chunk_size += 8;
00580 goto get_video_packet;
00581
00582 case mTCD_TAG:
00583 avio_skip(pb, 8);
00584 chunk_size -= 8;
00585 goto get_video_packet;
00586
00587 case MV0K_TAG:
00588 case MPCh_TAG:
00589 case pIQT_TAG:
00590 key = AV_PKT_FLAG_KEY;
00591 case MV0F_TAG:
00592 get_video_packet:
00593 if (partial_packet) {
00594 ret = av_append_packet(pb, pkt, chunk_size);
00595 } else
00596 ret = av_get_packet(pb, pkt, chunk_size);
00597 if (ret < 0) {
00598 packet_read = 1;
00599 break;
00600 }
00601 partial_packet = chunk_type == MVIh_TAG;
00602 pkt->stream_index = ea->video_stream_index;
00603 pkt->flags |= key;
00604 packet_read = 1;
00605 break;
00606
00607 default:
00608 avio_skip(pb, chunk_size);
00609 break;
00610 }
00611 }
00612
00613 if (ret < 0 && partial_packet)
00614 av_free_packet(pkt);
00615 return ret;
00616 }
00617
00618 AVInputFormat ff_ea_demuxer = {
00619 .name = "ea",
00620 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia"),
00621 .priv_data_size = sizeof(EaDemuxContext),
00622 .read_probe = ea_probe,
00623 .read_header = ea_read_header,
00624 .read_packet = ea_read_packet,
00625 };