electronicarts.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /* Electronic Arts Multimedia File Demuxer
  2. * Copyright (c) 2004 The ffmpeg Project
  3. * Copyright (c) 2006-2008 Peter Ross
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
  24. * by Robin Kay (komadori at gekkou.co.uk)
  25. */
  26. #include "libavutil/intreadwrite.h"
  27. #include "avformat.h"
  28. #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
  29. #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
  30. #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
  31. #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
  32. #define SHEN_TAG MKTAG('S', 'H', 'E', 'N') /* SxEN header */
  33. #define SDEN_TAG MKTAG('S', 'D', 'E', 'N') /* SxEN data */
  34. #define SEEN_TAG MKTAG('S', 'E', 'E', 'N') /* SxEN end */
  35. #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
  36. #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
  37. #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
  38. #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
  39. #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
  40. #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
  41. #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
  42. #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
  43. #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
  44. #define fVGT_TAG MKTAG('f', 'V', 'G', 'T') /* TGV p-frame */
  45. #define mTCD_TAG MKTAG('m', 'T', 'C', 'D') /* MDEC */
  46. #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
  47. #define MADm_TAG MKTAG('M', 'A', 'D', 'm') /* MAD p-frame */
  48. #define MADe_TAG MKTAG('M', 'A', 'D', 'e') /* MAD lqp-frame */
  49. #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
  50. #define TGQs_TAG MKTAG('T', 'G', 'Q', 's') /* TGQ i-frame (appears in .TGQ files) */
  51. #define pQGT_TAG MKTAG('p', 'Q', 'G', 'T') /* TGQ i-frame (appears in .UV files) */
  52. #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T') /* TQI/UV2 i-frame (.UV2/.WVE) */
  53. #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
  54. #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
  55. #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
  56. #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
  57. #define MVIf_TAG MKTAG('M', 'V', 'I', 'f') /* CMV i-frame */
  58. typedef struct EaDemuxContext {
  59. int big_endian;
  60. enum CodecID video_codec;
  61. AVRational time_base;
  62. int width, height;
  63. int video_stream_index;
  64. enum CodecID audio_codec;
  65. int audio_stream_index;
  66. int audio_frame_counter;
  67. int bytes;
  68. int sample_rate;
  69. int num_channels;
  70. int num_samples;
  71. } EaDemuxContext;
  72. static uint32_t read_arbitary(AVIOContext *pb) {
  73. uint8_t size, byte;
  74. int i;
  75. uint32_t word;
  76. size = avio_r8(pb);
  77. word = 0;
  78. for (i = 0; i < size; i++) {
  79. byte = avio_r8(pb);
  80. word <<= 8;
  81. word |= byte;
  82. }
  83. return word;
  84. }
  85. /*
  86. * Process PT/GSTR sound header
  87. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  88. */
  89. static int process_audio_header_elements(AVFormatContext *s)
  90. {
  91. int inHeader = 1;
  92. EaDemuxContext *ea = s->priv_data;
  93. AVIOContext *pb = s->pb;
  94. int compression_type = -1, revision = -1, revision2 = -1;
  95. ea->bytes = 2;
  96. ea->sample_rate = -1;
  97. ea->num_channels = 1;
  98. while (!url_feof(pb) && inHeader) {
  99. int inSubheader;
  100. uint8_t byte;
  101. byte = avio_r8(pb);
  102. switch (byte) {
  103. case 0xFD:
  104. av_log (s, AV_LOG_DEBUG, "entered audio subheader\n");
  105. inSubheader = 1;
  106. while (!url_feof(pb) && inSubheader) {
  107. uint8_t subbyte;
  108. subbyte = avio_r8(pb);
  109. switch (subbyte) {
  110. case 0x80:
  111. revision = read_arbitary(pb);
  112. av_log (s, AV_LOG_DEBUG, "revision (element 0x80) set to 0x%08x\n", revision);
  113. break;
  114. case 0x82:
  115. ea->num_channels = read_arbitary(pb);
  116. av_log (s, AV_LOG_DEBUG, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
  117. break;
  118. case 0x83:
  119. compression_type = read_arbitary(pb);
  120. av_log (s, AV_LOG_DEBUG, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
  121. break;
  122. case 0x84:
  123. ea->sample_rate = read_arbitary(pb);
  124. av_log (s, AV_LOG_DEBUG, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
  125. break;
  126. case 0x85:
  127. ea->num_samples = read_arbitary(pb);
  128. av_log (s, AV_LOG_DEBUG, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
  129. break;
  130. case 0x8A:
  131. av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  132. av_log (s, AV_LOG_DEBUG, "exited audio subheader\n");
  133. inSubheader = 0;
  134. break;
  135. case 0xA0:
  136. revision2 = read_arbitary(pb);
  137. av_log (s, AV_LOG_DEBUG, "revision2 (element 0xA0) set to 0x%08x\n", revision2);
  138. break;
  139. case 0xFF:
  140. av_log (s, AV_LOG_DEBUG, "end of header block reached (within audio subheader)\n");
  141. inSubheader = 0;
  142. inHeader = 0;
  143. break;
  144. default:
  145. av_log (s, AV_LOG_DEBUG, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  146. break;
  147. }
  148. }
  149. break;
  150. case 0xFF:
  151. av_log (s, AV_LOG_DEBUG, "end of header block reached\n");
  152. inHeader = 0;
  153. break;
  154. default:
  155. av_log (s, AV_LOG_DEBUG, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
  156. break;
  157. }
  158. }
  159. switch (compression_type) {
  160. case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  161. case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
  162. case -1:
  163. switch (revision) {
  164. case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
  165. case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
  166. case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
  167. case -1: break;
  168. default:
  169. av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
  170. return 0;
  171. }
  172. switch (revision2) {
  173. case 8: ea->audio_codec = CODEC_ID_PCM_S16LE_PLANAR; break;
  174. case 10: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
  175. case 16: ea->audio_codec = CODEC_ID_MP3; break;
  176. case -1: break;
  177. default:
  178. ea->audio_codec = CODEC_ID_NONE;
  179. av_log(s, AV_LOG_ERROR, "unsupported stream type; revision2=%i\n", revision2);
  180. return 0;
  181. }
  182. break;
  183. default:
  184. av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
  185. return 0;
  186. }
  187. if (ea->sample_rate == -1)
  188. ea->sample_rate = revision==3 ? 48000 : 22050;
  189. return 1;
  190. }
  191. /*
  192. * Process EACS sound header
  193. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  194. */
  195. static int process_audio_header_eacs(AVFormatContext *s)
  196. {
  197. EaDemuxContext *ea = s->priv_data;
  198. AVIOContext *pb = s->pb;
  199. int compression_type;
  200. ea->sample_rate = ea->big_endian ? avio_rb32(pb) : avio_rl32(pb);
  201. ea->bytes = avio_r8(pb); /* 1=8-bit, 2=16-bit */
  202. ea->num_channels = avio_r8(pb);
  203. compression_type = avio_r8(pb);
  204. avio_skip(pb, 13);
  205. switch (compression_type) {
  206. case 0:
  207. switch (ea->bytes) {
  208. case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
  209. case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  210. }
  211. break;
  212. case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
  213. case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
  214. default:
  215. av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
  216. }
  217. return 1;
  218. }
  219. /*
  220. * Process SEAD sound header
  221. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  222. */
  223. static int process_audio_header_sead(AVFormatContext *s)
  224. {
  225. EaDemuxContext *ea = s->priv_data;
  226. AVIOContext *pb = s->pb;
  227. ea->sample_rate = avio_rl32(pb);
  228. ea->bytes = avio_rl32(pb); /* 1=8-bit, 2=16-bit */
  229. ea->num_channels = avio_rl32(pb);
  230. ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
  231. return 1;
  232. }
  233. static int process_video_header_mdec(AVFormatContext *s)
  234. {
  235. EaDemuxContext *ea = s->priv_data;
  236. AVIOContext *pb = s->pb;
  237. avio_skip(pb, 4);
  238. ea->width = avio_rl16(pb);
  239. ea->height = avio_rl16(pb);
  240. ea->time_base = (AVRational){1,15};
  241. ea->video_codec = CODEC_ID_MDEC;
  242. return 1;
  243. }
  244. static int process_video_header_vp6(AVFormatContext *s)
  245. {
  246. EaDemuxContext *ea = s->priv_data;
  247. AVIOContext *pb = s->pb;
  248. avio_skip(pb, 16);
  249. ea->time_base.den = avio_rl32(pb);
  250. ea->time_base.num = avio_rl32(pb);
  251. ea->video_codec = CODEC_ID_VP6;
  252. return 1;
  253. }
  254. /*
  255. * Process EA file header
  256. * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
  257. */
  258. static int process_ea_header(AVFormatContext *s) {
  259. uint32_t blockid, size = 0;
  260. EaDemuxContext *ea = s->priv_data;
  261. AVIOContext *pb = s->pb;
  262. int i;
  263. for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
  264. unsigned int startpos = avio_tell(pb);
  265. int err = 0;
  266. blockid = avio_rl32(pb);
  267. size = avio_rl32(pb);
  268. if (i == 0)
  269. ea->big_endian = size > 0x000FFFFF;
  270. if (ea->big_endian)
  271. size = av_bswap32(size);
  272. switch (blockid) {
  273. case ISNh_TAG:
  274. if (avio_rl32(pb) != EACS_TAG) {
  275. av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
  276. return 0;
  277. }
  278. err = process_audio_header_eacs(s);
  279. break;
  280. case SCHl_TAG :
  281. case SHEN_TAG :
  282. blockid = avio_rl32(pb);
  283. if (blockid == GSTR_TAG) {
  284. avio_skip(pb, 4);
  285. } else if ((blockid & 0xFFFF)!=PT00_TAG) {
  286. av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
  287. return 0;
  288. }
  289. err = process_audio_header_elements(s);
  290. break;
  291. case SEAD_TAG:
  292. err = process_audio_header_sead(s);
  293. break;
  294. case MVIh_TAG :
  295. ea->video_codec = CODEC_ID_CMV;
  296. break;
  297. case kVGT_TAG:
  298. ea->video_codec = CODEC_ID_TGV;
  299. break;
  300. case mTCD_TAG :
  301. err = process_video_header_mdec(s);
  302. break;
  303. case MPCh_TAG:
  304. ea->video_codec = CODEC_ID_MPEG2VIDEO;
  305. break;
  306. case pQGT_TAG:
  307. case TGQs_TAG:
  308. ea->video_codec = CODEC_ID_TGQ;
  309. break;
  310. case pIQT_TAG:
  311. ea->video_codec = CODEC_ID_TQI;
  312. break;
  313. case MADk_TAG :
  314. ea->video_codec = CODEC_ID_MAD;
  315. break;
  316. case MVhd_TAG :
  317. err = process_video_header_vp6(s);
  318. break;
  319. }
  320. if (err < 0) {
  321. av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
  322. return err;
  323. }
  324. avio_seek(pb, startpos + size, SEEK_SET);
  325. }
  326. avio_seek(pb, 0, SEEK_SET);
  327. return 1;
  328. }
  329. static int ea_probe(AVProbeData *p)
  330. {
  331. switch (AV_RL32(&p->buf[0])) {
  332. case ISNh_TAG:
  333. case SCHl_TAG:
  334. case SEAD_TAG:
  335. case SHEN_TAG:
  336. case kVGT_TAG:
  337. case MADk_TAG:
  338. case MPCh_TAG:
  339. case MVhd_TAG:
  340. case MVIh_TAG:
  341. break;
  342. default:
  343. return 0;
  344. }
  345. if (AV_RL32(&p->buf[4]) > 0xfffff && AV_RB32(&p->buf[4]) > 0xfffff)
  346. return 0;
  347. return AVPROBE_SCORE_MAX;
  348. }
  349. static int ea_read_header(AVFormatContext *s,
  350. AVFormatParameters *ap)
  351. {
  352. EaDemuxContext *ea = s->priv_data;
  353. AVStream *st;
  354. if (!process_ea_header(s))
  355. return AVERROR(EIO);
  356. if (ea->video_codec) {
  357. /* initialize the video decoder stream */
  358. st = av_new_stream(s, 0);
  359. if (!st)
  360. return AVERROR(ENOMEM);
  361. ea->video_stream_index = st->index;
  362. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  363. st->codec->codec_id = ea->video_codec;
  364. // parsing is necessary to make FFmpeg generate correct timestamps
  365. if (st->codec->codec_id == CODEC_ID_MPEG2VIDEO)
  366. st->need_parsing = AVSTREAM_PARSE_HEADERS;
  367. st->codec->codec_tag = 0; /* no fourcc */
  368. if (ea->time_base.num)
  369. av_set_pts_info(st, 64, ea->time_base.num, ea->time_base.den);
  370. st->codec->width = ea->width;
  371. st->codec->height = ea->height;
  372. }
  373. if (ea->audio_codec) {
  374. if (ea->num_channels <= 0) {
  375. av_log(s, AV_LOG_WARNING, "Unsupported number of channels: %d\n", ea->num_channels);
  376. ea->audio_codec = 0;
  377. return 1;
  378. }
  379. if (ea->sample_rate <= 0) {
  380. av_log(s, AV_LOG_ERROR, "Unsupported sample rate: %d\n", ea->sample_rate);
  381. ea->audio_codec = 0;
  382. return 1;
  383. }
  384. /* initialize the audio decoder stream */
  385. st = av_new_stream(s, 0);
  386. if (!st)
  387. return AVERROR(ENOMEM);
  388. av_set_pts_info(st, 33, 1, ea->sample_rate);
  389. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  390. st->codec->codec_id = ea->audio_codec;
  391. st->codec->codec_tag = 0; /* no tag */
  392. st->codec->channels = ea->num_channels;
  393. st->codec->sample_rate = ea->sample_rate;
  394. st->codec->bits_per_coded_sample = ea->bytes * 8;
  395. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  396. st->codec->bits_per_coded_sample / 4;
  397. st->codec->block_align = st->codec->channels*st->codec->bits_per_coded_sample;
  398. ea->audio_stream_index = st->index;
  399. ea->audio_frame_counter = 0;
  400. }
  401. return 1;
  402. }
  403. static int ea_read_packet(AVFormatContext *s,
  404. AVPacket *pkt)
  405. {
  406. EaDemuxContext *ea = s->priv_data;
  407. AVIOContext *pb = s->pb;
  408. int ret = 0;
  409. int packet_read = 0;
  410. unsigned int chunk_type, chunk_size;
  411. int key = 0;
  412. int av_uninit(num_samples);
  413. while (!packet_read) {
  414. chunk_type = avio_rl32(pb);
  415. chunk_size = (ea->big_endian ? avio_rb32(pb) : avio_rl32(pb)) - 8;
  416. switch (chunk_type) {
  417. /* audio data */
  418. case ISNh_TAG:
  419. /* header chunk also contains data; skip over the header portion*/
  420. avio_skip(pb, 32);
  421. chunk_size -= 32;
  422. case ISNd_TAG:
  423. case SCDl_TAG:
  424. case SNDC_TAG:
  425. case SDEN_TAG:
  426. if (!ea->audio_codec) {
  427. avio_skip(pb, chunk_size);
  428. break;
  429. } else if (ea->audio_codec == CODEC_ID_PCM_S16LE_PLANAR ||
  430. ea->audio_codec == CODEC_ID_MP3) {
  431. num_samples = avio_rl32(pb);
  432. avio_skip(pb, 8);
  433. chunk_size -= 12;
  434. }
  435. ret = av_get_packet(pb, pkt, chunk_size);
  436. if (ret < 0)
  437. return ret;
  438. pkt->stream_index = ea->audio_stream_index;
  439. pkt->pts = 90000;
  440. pkt->pts *= ea->audio_frame_counter;
  441. pkt->pts /= ea->sample_rate;
  442. switch (ea->audio_codec) {
  443. case CODEC_ID_ADPCM_EA:
  444. /* 2 samples/byte, 1 or 2 samples per frame depending
  445. * on stereo; chunk also has 12-byte header */
  446. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  447. ea->num_channels;
  448. break;
  449. case CODEC_ID_PCM_S16LE_PLANAR:
  450. case CODEC_ID_MP3:
  451. ea->audio_frame_counter += num_samples;
  452. break;
  453. default:
  454. ea->audio_frame_counter += chunk_size /
  455. (ea->bytes * ea->num_channels);
  456. }
  457. packet_read = 1;
  458. break;
  459. /* ending tag */
  460. case 0:
  461. case ISNe_TAG:
  462. case SCEl_TAG:
  463. case SEND_TAG:
  464. case SEEN_TAG:
  465. ret = AVERROR(EIO);
  466. packet_read = 1;
  467. break;
  468. case MVIh_TAG:
  469. case kVGT_TAG:
  470. case pQGT_TAG:
  471. case TGQs_TAG:
  472. case MADk_TAG:
  473. key = AV_PKT_FLAG_KEY;
  474. case MVIf_TAG:
  475. case fVGT_TAG:
  476. case MADm_TAG:
  477. case MADe_TAG:
  478. avio_seek(pb, -8, SEEK_CUR); // include chunk preamble
  479. chunk_size += 8;
  480. goto get_video_packet;
  481. case mTCD_TAG:
  482. avio_skip(pb, 8); // skip ea dct header
  483. chunk_size -= 8;
  484. goto get_video_packet;
  485. case MV0K_TAG:
  486. case MPCh_TAG:
  487. case pIQT_TAG:
  488. key = AV_PKT_FLAG_KEY;
  489. case MV0F_TAG:
  490. get_video_packet:
  491. ret = av_get_packet(pb, pkt, chunk_size);
  492. if (ret < 0)
  493. return ret;
  494. pkt->stream_index = ea->video_stream_index;
  495. pkt->flags |= key;
  496. packet_read = 1;
  497. break;
  498. default:
  499. avio_skip(pb, chunk_size);
  500. break;
  501. }
  502. }
  503. return ret;
  504. }
  505. AVInputFormat ff_ea_demuxer = {
  506. .name = "ea",
  507. .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Multimedia Format"),
  508. .priv_data_size = sizeof(EaDemuxContext),
  509. .read_probe = ea_probe,
  510. .read_header = ea_read_header,
  511. .read_packet = ea_read_packet,
  512. };