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.

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