You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

460 lines
14KB

  1. /* Electronic Arts Multimedia File Demuxer
  2. * Copyright (c) 2004 The ffmpeg Project
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file electronicarts.c
  22. * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
  23. * by Robin Kay (komadori at gekkou.co.uk)
  24. */
  25. #include "avformat.h"
  26. #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
  27. #define SEAD_TAG MKTAG('S', 'E', 'A', 'D') /* Sxxx header */
  28. #define SNDC_TAG MKTAG('S', 'N', 'D', 'C') /* Sxxx data */
  29. #define SEND_TAG MKTAG('S', 'E', 'N', 'D') /* Sxxx end */
  30. #define ISNh_TAG MKTAG('1', 'S', 'N', 'h') /* 1SNx header */
  31. #define EACS_TAG MKTAG('E', 'A', 'C', 'S')
  32. #define ISNd_TAG MKTAG('1', 'S', 'N', 'd') /* 1SNx data */
  33. #define ISNe_TAG MKTAG('1', 'S', 'N', 'e') /* 1SNx end */
  34. #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
  35. #define GSTR_TAG MKTAG('G', 'S', 'T', 'R')
  36. #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
  37. #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
  38. #define kVGT_TAG MKTAG('k', 'V', 'G', 'T') /* TGV i-frame */
  39. #define MADk_TAG MKTAG('M', 'A', 'D', 'k') /* MAD i-frame */
  40. #define MPCh_TAG MKTAG('M', 'P', 'C', 'h') /* MPEG2 */
  41. #define MVhd_TAG MKTAG('M', 'V', 'h', 'd')
  42. #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
  43. #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
  44. #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') /* CMV header */
  45. typedef struct EaDemuxContext {
  46. int big_endian;
  47. int video_codec;
  48. AVRational time_base;
  49. int video_stream_index;
  50. int audio_codec;
  51. int audio_stream_index;
  52. int audio_frame_counter;
  53. int64_t audio_pts;
  54. int bytes;
  55. int sample_rate;
  56. int num_channels;
  57. int num_samples;
  58. } EaDemuxContext;
  59. static uint32_t read_arbitary(ByteIOContext *pb) {
  60. uint8_t size, byte;
  61. int i;
  62. uint32_t word;
  63. size = get_byte(pb);
  64. word = 0;
  65. for (i = 0; i < size; i++) {
  66. byte = get_byte(pb);
  67. word <<= 8;
  68. word |= byte;
  69. }
  70. return word;
  71. }
  72. /*
  73. * Process PT/GSTR sound header
  74. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  75. */
  76. static int process_audio_header_elements(AVFormatContext *s)
  77. {
  78. int inHeader = 1;
  79. EaDemuxContext *ea = s->priv_data;
  80. ByteIOContext *pb = &s->pb;
  81. int compression_type = -1, revision = -1;
  82. ea->bytes = 2;
  83. ea->sample_rate = -1;
  84. ea->num_channels = 1;
  85. while (inHeader) {
  86. int inSubheader;
  87. uint8_t byte;
  88. byte = get_byte(pb);
  89. switch (byte) {
  90. case 0xFD:
  91. av_log (s, AV_LOG_INFO, "entered audio subheader\n");
  92. inSubheader = 1;
  93. while (inSubheader) {
  94. uint8_t subbyte;
  95. subbyte = get_byte(pb);
  96. switch (subbyte) {
  97. case 0x80:
  98. revision = read_arbitary(pb);
  99. av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
  100. break;
  101. case 0x82:
  102. ea->num_channels = read_arbitary(pb);
  103. av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
  104. break;
  105. case 0x83:
  106. compression_type = read_arbitary(pb);
  107. av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
  108. break;
  109. case 0x84:
  110. ea->sample_rate = read_arbitary(pb);
  111. av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
  112. break;
  113. case 0x85:
  114. ea->num_samples = read_arbitary(pb);
  115. av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
  116. break;
  117. case 0x8A:
  118. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  119. av_log (s, AV_LOG_INFO, "exited audio subheader\n");
  120. inSubheader = 0;
  121. break;
  122. case 0xFF:
  123. av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
  124. inSubheader = 0;
  125. inHeader = 0;
  126. break;
  127. default:
  128. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  129. break;
  130. }
  131. }
  132. break;
  133. case 0xFF:
  134. av_log (s, AV_LOG_INFO, "end of header block reached\n");
  135. inHeader = 0;
  136. break;
  137. default:
  138. av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
  139. break;
  140. }
  141. }
  142. switch (compression_type) {
  143. case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  144. case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
  145. case -1:
  146. switch (revision) {
  147. case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
  148. case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
  149. case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
  150. default:
  151. av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
  152. return 0;
  153. }
  154. break;
  155. default:
  156. av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
  157. return 0;
  158. }
  159. if (ea->sample_rate == -1)
  160. ea->sample_rate = revision==3 ? 48000 : 22050;
  161. return 1;
  162. }
  163. /*
  164. * Process EACS sound header
  165. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  166. */
  167. static int process_audio_header_eacs(AVFormatContext *s)
  168. {
  169. EaDemuxContext *ea = s->priv_data;
  170. ByteIOContext *pb = &s->pb;
  171. int compression_type;
  172. ea->sample_rate = ea->big_endian ? get_be32(pb) : get_le32(pb);
  173. ea->bytes = get_byte(pb); /* 1=8-bit, 2=16-bit */
  174. ea->num_channels = get_byte(pb);
  175. compression_type = get_byte(pb);
  176. url_fskip(pb, 13);
  177. switch (compression_type) {
  178. case 0:
  179. switch (ea->bytes) {
  180. case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
  181. case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  182. }
  183. break;
  184. case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
  185. case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
  186. default:
  187. av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
  188. }
  189. return 1;
  190. }
  191. /*
  192. * Process SEAD sound header
  193. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  194. */
  195. static int process_audio_header_sead(AVFormatContext *s)
  196. {
  197. EaDemuxContext *ea = s->priv_data;
  198. ByteIOContext *pb = &s->pb;
  199. ea->sample_rate = get_le32(pb);
  200. ea->bytes = get_le32(pb); /* 1=8-bit, 2=16-bit */
  201. ea->num_channels = get_le32(pb);
  202. ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
  203. return 1;
  204. }
  205. static int process_video_header_vp6(AVFormatContext *s)
  206. {
  207. EaDemuxContext *ea = s->priv_data;
  208. ByteIOContext *pb = &s->pb;
  209. url_fskip(pb, 16);
  210. ea->time_base.den = get_le32(pb);
  211. ea->time_base.num = get_le32(pb);
  212. ea->video_codec = CODEC_ID_VP6;
  213. return 1;
  214. }
  215. /*
  216. * Process EA file header
  217. * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
  218. */
  219. static int process_ea_header(AVFormatContext *s) {
  220. uint32_t blockid, size = 0;
  221. EaDemuxContext *ea = s->priv_data;
  222. ByteIOContext *pb = &s->pb;
  223. int i;
  224. for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
  225. unsigned int startpos = url_ftell(pb);
  226. int err = 0;
  227. blockid = get_le32(pb);
  228. size = get_le32(pb);
  229. if (i == 0)
  230. ea->big_endian = size > 0x000FFFFF;
  231. if (ea->big_endian)
  232. size = bswap_32(size);
  233. switch (blockid) {
  234. case ISNh_TAG:
  235. if (get_le32(pb) != EACS_TAG) {
  236. av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
  237. return 0;
  238. }
  239. err = process_audio_header_eacs(s);
  240. break;
  241. case SCHl_TAG :
  242. blockid = get_le32(pb);
  243. if (blockid == GSTR_TAG) {
  244. url_fskip(pb, 4);
  245. } else if (blockid != PT00_TAG) {
  246. av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
  247. return 0;
  248. }
  249. err = process_audio_header_elements(s);
  250. break;
  251. case SEAD_TAG:
  252. err = process_audio_header_sead(s);
  253. break;
  254. case MVhd_TAG :
  255. err = process_video_header_vp6(s);
  256. break;
  257. }
  258. if (err < 0) {
  259. av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
  260. return err;
  261. }
  262. url_fseek(pb, startpos + size, SEEK_SET);
  263. }
  264. url_fseek(pb, 0, SEEK_SET);
  265. return 1;
  266. }
  267. static int ea_probe(AVProbeData *p)
  268. {
  269. switch (AV_RL32(&p->buf[0])) {
  270. case ISNh_TAG:
  271. case SCHl_TAG:
  272. case SEAD_TAG:
  273. case kVGT_TAG:
  274. case MADk_TAG:
  275. case MPCh_TAG:
  276. case MVhd_TAG:
  277. case MVIh_TAG:
  278. return AVPROBE_SCORE_MAX;
  279. }
  280. return 0;
  281. }
  282. static int ea_read_header(AVFormatContext *s,
  283. AVFormatParameters *ap)
  284. {
  285. EaDemuxContext *ea = s->priv_data;
  286. AVStream *st;
  287. if (!process_ea_header(s))
  288. return AVERROR(EIO);
  289. if (ea->video_codec) {
  290. /* initialize the video decoder stream */
  291. st = av_new_stream(s, 0);
  292. if (!st)
  293. return AVERROR(ENOMEM);
  294. ea->video_stream_index = st->index;
  295. st->codec->codec_type = CODEC_TYPE_VIDEO;
  296. st->codec->codec_id = ea->video_codec;
  297. st->codec->codec_tag = 0; /* no fourcc */
  298. st->codec->time_base = ea->time_base;
  299. }
  300. if (ea->audio_codec) {
  301. /* initialize the audio decoder stream */
  302. st = av_new_stream(s, 0);
  303. if (!st)
  304. return AVERROR(ENOMEM);
  305. av_set_pts_info(st, 33, 1, ea->sample_rate);
  306. st->codec->codec_type = CODEC_TYPE_AUDIO;
  307. st->codec->codec_id = ea->audio_codec;
  308. st->codec->codec_tag = 0; /* no tag */
  309. st->codec->channels = ea->num_channels;
  310. st->codec->sample_rate = ea->sample_rate;
  311. st->codec->bits_per_sample = ea->bytes * 8;
  312. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  313. st->codec->bits_per_sample / 4;
  314. st->codec->block_align = st->codec->channels*st->codec->bits_per_sample;
  315. ea->audio_stream_index = st->index;
  316. ea->audio_frame_counter = 0;
  317. }
  318. return 1;
  319. }
  320. static int ea_read_packet(AVFormatContext *s,
  321. AVPacket *pkt)
  322. {
  323. EaDemuxContext *ea = s->priv_data;
  324. ByteIOContext *pb = &s->pb;
  325. int ret = 0;
  326. int packet_read = 0;
  327. unsigned int chunk_type, chunk_size;
  328. int key = 0;
  329. while (!packet_read) {
  330. chunk_type = get_le32(pb);
  331. chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
  332. switch (chunk_type) {
  333. /* audio data */
  334. case ISNh_TAG:
  335. /* header chunk also contains data; skip over the header portion*/
  336. url_fskip(pb, 32);
  337. chunk_size -= 32;
  338. case ISNd_TAG:
  339. case SCDl_TAG:
  340. case SNDC_TAG:
  341. if (!ea->audio_codec) {
  342. url_fskip(pb, chunk_size);
  343. break;
  344. }
  345. ret = av_get_packet(pb, pkt, chunk_size);
  346. if (ret != chunk_size)
  347. ret = AVERROR(EIO);
  348. else {
  349. pkt->stream_index = ea->audio_stream_index;
  350. pkt->pts = 90000;
  351. pkt->pts *= ea->audio_frame_counter;
  352. pkt->pts /= ea->sample_rate;
  353. switch (ea->audio_codec) {
  354. case CODEC_ID_ADPCM_EA:
  355. /* 2 samples/byte, 1 or 2 samples per frame depending
  356. * on stereo; chunk also has 12-byte header */
  357. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  358. ea->num_channels;
  359. break;
  360. default:
  361. ea->audio_frame_counter += chunk_size /
  362. (ea->bytes * ea->num_channels);
  363. }
  364. }
  365. packet_read = 1;
  366. break;
  367. /* ending tag */
  368. case 0:
  369. case ISNe_TAG:
  370. case SCEl_TAG:
  371. case SEND_TAG:
  372. ret = AVERROR(EIO);
  373. packet_read = 1;
  374. break;
  375. case MV0K_TAG:
  376. key = PKT_FLAG_KEY;
  377. case MV0F_TAG:
  378. ret = av_get_packet(pb, pkt, chunk_size);
  379. if (ret != chunk_size)
  380. ret = AVERROR_IO;
  381. else {
  382. pkt->stream_index = ea->video_stream_index;
  383. pkt->flags |= key;
  384. }
  385. packet_read = 1;
  386. break;
  387. default:
  388. url_fseek(pb, chunk_size, SEEK_CUR);
  389. break;
  390. }
  391. }
  392. return ret;
  393. }
  394. AVInputFormat ea_demuxer = {
  395. "ea",
  396. "Electronic Arts Multimedia Format",
  397. sizeof(EaDemuxContext),
  398. ea_probe,
  399. ea_read_header,
  400. ea_read_packet,
  401. };