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