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.

364 lines
11KB

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