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.

452 lines
13KB

  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 MVhd_TAG MKTAG('M', 'V', 'h', 'd')
  39. #define MV0K_TAG MKTAG('M', 'V', '0', 'K')
  40. #define MV0F_TAG MKTAG('M', 'V', '0', 'F')
  41. typedef struct EaDemuxContext {
  42. int big_endian;
  43. int video_codec;
  44. AVRational time_base;
  45. int video_stream_index;
  46. int audio_codec;
  47. int audio_stream_index;
  48. int audio_frame_counter;
  49. int64_t audio_pts;
  50. int bytes;
  51. int sample_rate;
  52. int num_channels;
  53. int num_samples;
  54. } EaDemuxContext;
  55. static uint32_t read_arbitary(ByteIOContext *pb) {
  56. uint8_t size, byte;
  57. int i;
  58. uint32_t word;
  59. size = get_byte(pb);
  60. word = 0;
  61. for (i = 0; i < size; i++) {
  62. byte = get_byte(pb);
  63. word <<= 8;
  64. word |= byte;
  65. }
  66. return word;
  67. }
  68. /*
  69. * Process PT/GSTR sound header
  70. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  71. */
  72. static int process_audio_header_elements(AVFormatContext *s)
  73. {
  74. int inHeader = 1;
  75. EaDemuxContext *ea = s->priv_data;
  76. ByteIOContext *pb = &s->pb;
  77. int compression_type = -1, revision = -1;
  78. ea->bytes = 2;
  79. ea->sample_rate = -1;
  80. ea->num_channels = 1;
  81. while (inHeader) {
  82. int inSubheader;
  83. uint8_t byte;
  84. byte = get_byte(pb);
  85. switch (byte) {
  86. case 0xFD:
  87. av_log (s, AV_LOG_INFO, "entered audio subheader\n");
  88. inSubheader = 1;
  89. while (inSubheader) {
  90. uint8_t subbyte;
  91. subbyte = get_byte(pb);
  92. switch (subbyte) {
  93. case 0x80:
  94. revision = read_arbitary(pb);
  95. av_log (s, AV_LOG_INFO, "revision (element 0x80) set to 0x%08x\n", revision);
  96. break;
  97. case 0x82:
  98. ea->num_channels = read_arbitary(pb);
  99. av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
  100. break;
  101. case 0x83:
  102. compression_type = read_arbitary(pb);
  103. av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", compression_type);
  104. break;
  105. case 0x84:
  106. ea->sample_rate = read_arbitary(pb);
  107. av_log (s, AV_LOG_INFO, "sample_rate (element 0x84) set to %i\n", ea->sample_rate);
  108. break;
  109. case 0x85:
  110. ea->num_samples = read_arbitary(pb);
  111. av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
  112. break;
  113. case 0x8A:
  114. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  115. av_log (s, AV_LOG_INFO, "exited audio subheader\n");
  116. inSubheader = 0;
  117. break;
  118. case 0xFF:
  119. av_log (s, AV_LOG_INFO, "end of header block reached (within audio subheader)\n");
  120. inSubheader = 0;
  121. inHeader = 0;
  122. break;
  123. default:
  124. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  125. break;
  126. }
  127. }
  128. break;
  129. case 0xFF:
  130. av_log (s, AV_LOG_INFO, "end of header block reached\n");
  131. inHeader = 0;
  132. break;
  133. default:
  134. av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
  135. break;
  136. }
  137. }
  138. switch (compression_type) {
  139. case 0: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  140. case 7: ea->audio_codec = CODEC_ID_ADPCM_EA; break;
  141. case -1:
  142. switch (revision) {
  143. case 1: ea->audio_codec = CODEC_ID_ADPCM_EA_R1; break;
  144. case 2: ea->audio_codec = CODEC_ID_ADPCM_EA_R2; break;
  145. case 3: ea->audio_codec = CODEC_ID_ADPCM_EA_R3; break;
  146. default:
  147. av_log(s, AV_LOG_ERROR, "unsupported stream type; revision=%i\n", revision);
  148. return 0;
  149. }
  150. break;
  151. default:
  152. av_log(s, AV_LOG_ERROR, "unsupported stream type; compression_type=%i\n", compression_type);
  153. return 0;
  154. }
  155. if (ea->sample_rate == -1)
  156. ea->sample_rate = revision==3 ? 48000 : 22050;
  157. return 1;
  158. }
  159. /*
  160. * Process EACS sound header
  161. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  162. */
  163. static int process_audio_header_eacs(AVFormatContext *s)
  164. {
  165. EaDemuxContext *ea = s->priv_data;
  166. ByteIOContext *pb = &s->pb;
  167. int compression_type;
  168. ea->sample_rate = ea->big_endian ? get_be32(pb) : get_le32(pb);
  169. ea->bytes = get_byte(pb); /* 1=8-bit, 2=16-bit */
  170. ea->num_channels = get_byte(pb);
  171. compression_type = get_byte(pb);
  172. url_fskip(pb, 13);
  173. switch (compression_type) {
  174. case 0:
  175. switch (ea->bytes) {
  176. case 1: ea->audio_codec = CODEC_ID_PCM_S8; break;
  177. case 2: ea->audio_codec = CODEC_ID_PCM_S16LE; break;
  178. }
  179. break;
  180. case 1: ea->audio_codec = CODEC_ID_PCM_MULAW; ea->bytes = 1; break;
  181. case 2: ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_EACS; break;
  182. default:
  183. av_log (s, AV_LOG_ERROR, "unsupported stream type; audio compression_type=%i\n", compression_type);
  184. }
  185. return 1;
  186. }
  187. /*
  188. * Process SEAD sound header
  189. * return 1 if success, 0 if invalid format, otherwise AVERROR_xxx
  190. */
  191. static int process_audio_header_sead(AVFormatContext *s)
  192. {
  193. EaDemuxContext *ea = s->priv_data;
  194. ByteIOContext *pb = &s->pb;
  195. ea->sample_rate = get_le32(pb);
  196. ea->bytes = get_le32(pb); /* 1=8-bit, 2=16-bit */
  197. ea->num_channels = get_le32(pb);
  198. ea->audio_codec = CODEC_ID_ADPCM_IMA_EA_SEAD;
  199. return 1;
  200. }
  201. static int process_video_header_vp6(AVFormatContext *s)
  202. {
  203. EaDemuxContext *ea = s->priv_data;
  204. ByteIOContext *pb = &s->pb;
  205. url_fskip(pb, 16);
  206. ea->time_base.den = get_le32(pb);
  207. ea->time_base.num = get_le32(pb);
  208. ea->video_codec = CODEC_ID_VP6;
  209. return 1;
  210. }
  211. /*
  212. * Process EA file header
  213. * Returns 1 if the EA file is valid and successfully opened, 0 otherwise
  214. */
  215. static int process_ea_header(AVFormatContext *s) {
  216. uint32_t blockid, size = 0;
  217. EaDemuxContext *ea = s->priv_data;
  218. ByteIOContext *pb = &s->pb;
  219. int i;
  220. for (i=0; i<5 && (!ea->audio_codec || !ea->video_codec); i++) {
  221. unsigned int startpos = url_ftell(pb);
  222. int err = 0;
  223. blockid = get_le32(pb);
  224. size = get_le32(pb);
  225. if (i == 0)
  226. ea->big_endian = size > 0x000FFFFF;
  227. if (ea->big_endian)
  228. size = bswap_32(size);
  229. switch (blockid) {
  230. case ISNh_TAG:
  231. if (get_le32(pb) != EACS_TAG) {
  232. av_log (s, AV_LOG_ERROR, "unknown 1SNh headerid\n");
  233. return 0;
  234. }
  235. err = process_audio_header_eacs(s);
  236. break;
  237. case SCHl_TAG :
  238. blockid = get_le32(pb);
  239. if (blockid == GSTR_TAG) {
  240. url_fskip(pb, 4);
  241. } else if (blockid != PT00_TAG) {
  242. av_log (s, AV_LOG_ERROR, "unknown SCHl headerid\n");
  243. return 0;
  244. }
  245. err = process_audio_header_elements(s);
  246. break;
  247. case SEAD_TAG:
  248. err = process_audio_header_sead(s);
  249. break;
  250. case MVhd_TAG :
  251. err = process_video_header_vp6(s);
  252. break;
  253. }
  254. if (err < 0) {
  255. av_log(s, AV_LOG_ERROR, "error parsing header: %i\n", err);
  256. return err;
  257. }
  258. url_fseek(pb, startpos + size, SEEK_SET);
  259. }
  260. url_fseek(pb, 0, SEEK_SET);
  261. return 1;
  262. }
  263. static int ea_probe(AVProbeData *p)
  264. {
  265. switch (AV_RL32(&p->buf[0])) {
  266. case ISNh_TAG:
  267. case SCHl_TAG:
  268. case SEAD_TAG:
  269. case MVhd_TAG:
  270. return AVPROBE_SCORE_MAX;
  271. }
  272. return 0;
  273. }
  274. static int ea_read_header(AVFormatContext *s,
  275. AVFormatParameters *ap)
  276. {
  277. EaDemuxContext *ea = s->priv_data;
  278. AVStream *st;
  279. if (!process_ea_header(s))
  280. return AVERROR(EIO);
  281. if (ea->video_codec) {
  282. /* initialize the video decoder stream */
  283. st = av_new_stream(s, 0);
  284. if (!st)
  285. return AVERROR(ENOMEM);
  286. ea->video_stream_index = st->index;
  287. st->codec->codec_type = CODEC_TYPE_VIDEO;
  288. st->codec->codec_id = ea->video_codec;
  289. st->codec->codec_tag = 0; /* no fourcc */
  290. st->codec->time_base = ea->time_base;
  291. }
  292. if (ea->audio_codec) {
  293. /* initialize the audio decoder stream */
  294. st = av_new_stream(s, 0);
  295. if (!st)
  296. return AVERROR(ENOMEM);
  297. av_set_pts_info(st, 33, 1, ea->sample_rate);
  298. st->codec->codec_type = CODEC_TYPE_AUDIO;
  299. st->codec->codec_id = ea->audio_codec;
  300. st->codec->codec_tag = 0; /* no tag */
  301. st->codec->channels = ea->num_channels;
  302. st->codec->sample_rate = ea->sample_rate;
  303. st->codec->bits_per_sample = ea->bytes * 8;
  304. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  305. st->codec->bits_per_sample / 4;
  306. st->codec->block_align = st->codec->channels*st->codec->bits_per_sample;
  307. ea->audio_stream_index = st->index;
  308. ea->audio_frame_counter = 0;
  309. }
  310. return 1;
  311. }
  312. static int ea_read_packet(AVFormatContext *s,
  313. AVPacket *pkt)
  314. {
  315. EaDemuxContext *ea = s->priv_data;
  316. ByteIOContext *pb = &s->pb;
  317. int ret = 0;
  318. int packet_read = 0;
  319. unsigned int chunk_type, chunk_size;
  320. int key = 0;
  321. while (!packet_read) {
  322. chunk_type = get_le32(pb);
  323. chunk_size = (ea->big_endian ? get_be32(pb) : get_le32(pb)) - 8;
  324. switch (chunk_type) {
  325. /* audio data */
  326. case ISNh_TAG:
  327. /* header chunk also contains data; skip over the header portion*/
  328. url_fskip(pb, 32);
  329. chunk_size -= 32;
  330. case ISNd_TAG:
  331. case SCDl_TAG:
  332. case SNDC_TAG:
  333. if (!ea->audio_codec) {
  334. url_fskip(pb, chunk_size);
  335. break;
  336. }
  337. ret = av_get_packet(pb, pkt, chunk_size);
  338. if (ret != chunk_size)
  339. ret = AVERROR(EIO);
  340. else {
  341. pkt->stream_index = ea->audio_stream_index;
  342. pkt->pts = 90000;
  343. pkt->pts *= ea->audio_frame_counter;
  344. pkt->pts /= ea->sample_rate;
  345. switch (ea->audio_codec) {
  346. case CODEC_ID_ADPCM_EA:
  347. /* 2 samples/byte, 1 or 2 samples per frame depending
  348. * on stereo; chunk also has 12-byte header */
  349. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  350. ea->num_channels;
  351. break;
  352. default:
  353. ea->audio_frame_counter += chunk_size /
  354. (ea->bytes * ea->num_channels);
  355. }
  356. }
  357. packet_read = 1;
  358. break;
  359. /* ending tag */
  360. case 0:
  361. case ISNe_TAG:
  362. case SCEl_TAG:
  363. case SEND_TAG:
  364. ret = AVERROR(EIO);
  365. packet_read = 1;
  366. break;
  367. case MV0K_TAG:
  368. key = PKT_FLAG_KEY;
  369. case MV0F_TAG:
  370. ret = av_get_packet(pb, pkt, chunk_size);
  371. if (ret != chunk_size)
  372. ret = AVERROR_IO;
  373. else {
  374. pkt->stream_index = ea->video_stream_index;
  375. pkt->flags |= key;
  376. }
  377. packet_read = 1;
  378. break;
  379. default:
  380. url_fseek(pb, chunk_size, SEEK_CUR);
  381. break;
  382. }
  383. }
  384. return ret;
  385. }
  386. AVInputFormat ea_demuxer = {
  387. "ea",
  388. "Electronic Arts Multimedia Format",
  389. sizeof(EaDemuxContext),
  390. ea_probe,
  391. ea_read_header,
  392. ea_read_packet,
  393. };