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.

291 lines
8.3KB

  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. #define EA_SAMPLE_RATE 22050
  35. #define EA_BITS_PER_SAMPLE 16
  36. #define EA_PREAMBLE_SIZE 8
  37. typedef struct EaDemuxContext {
  38. AVRational time_base;
  39. int video_stream_index;
  40. int audio_stream_index;
  41. int audio_frame_counter;
  42. int64_t audio_pts;
  43. int num_channels;
  44. int num_samples;
  45. int compression_type;
  46. } EaDemuxContext;
  47. static uint32_t read_arbitary(ByteIOContext *pb) {
  48. uint8_t size, byte;
  49. int i;
  50. uint32_t word;
  51. size = get_byte(pb);
  52. word = 0;
  53. for (i = 0; i < size; i++) {
  54. byte = get_byte(pb);
  55. word <<= 8;
  56. word |= byte;
  57. }
  58. return word;
  59. }
  60. /*
  61. * Process WVE file header
  62. * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
  63. */
  64. static int process_ea_header(AVFormatContext *s) {
  65. int inHeader = 1;
  66. uint32_t blockid, size = 0;
  67. int num, den;
  68. EaDemuxContext *ea = s->priv_data;
  69. ByteIOContext *pb = &s->pb;
  70. blockid = get_le32(pb);
  71. if (blockid == MVhd_TAG) {
  72. size = get_le32(pb);
  73. url_fskip(pb, 16);
  74. den = get_le32(pb);
  75. num = get_le32(pb);
  76. ea->time_base = (AVRational) {num, den};
  77. url_fskip(pb, size-32);
  78. blockid = get_le32(pb);
  79. }
  80. if (blockid != SCHl_TAG)
  81. return 0;
  82. size += get_le32(pb);
  83. blockid = get_le32(pb);
  84. if (blockid == GSTR_TAG) {
  85. url_fskip(pb, 4);
  86. } else if (blockid != PT00_TAG) {
  87. av_log (s, AV_LOG_ERROR, "PT header missing\n");
  88. return 0;
  89. }
  90. while (inHeader) {
  91. int inSubheader;
  92. uint8_t byte;
  93. byte = get_byte(pb);
  94. switch (byte) {
  95. case 0xFD:
  96. av_log (s, AV_LOG_INFO, "entered audio subheader\n");
  97. inSubheader = 1;
  98. while (inSubheader) {
  99. uint8_t subbyte;
  100. subbyte = get_byte(pb);
  101. switch (subbyte) {
  102. case 0x82:
  103. ea->num_channels = read_arbitary(pb);
  104. av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
  105. break;
  106. case 0x83:
  107. ea->compression_type = read_arbitary(pb);
  108. av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
  109. break;
  110. case 0x85:
  111. ea->num_samples = read_arbitary(pb);
  112. av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
  113. break;
  114. case 0x8A:
  115. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  116. av_log (s, AV_LOG_INFO, "exited audio subheader\n");
  117. inSubheader = 0;
  118. break;
  119. case 0xFF:
  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. /* skip to the start of the data */
  139. url_fseek(pb, size, SEEK_SET);
  140. return 1;
  141. }
  142. static int ea_probe(AVProbeData *p)
  143. {
  144. uint32_t tag;
  145. tag = AV_RL32(&p->buf[0]);
  146. if (tag == SCHl_TAG || tag == MVhd_TAG)
  147. return AVPROBE_SCORE_MAX;
  148. return 0;
  149. }
  150. static int ea_read_header(AVFormatContext *s,
  151. AVFormatParameters *ap)
  152. {
  153. EaDemuxContext *ea = s->priv_data;
  154. AVStream *st;
  155. if (!process_ea_header(s))
  156. return AVERROR(EIO);
  157. if (ea->time_base.num && ea->time_base.den) {
  158. /* initialize the video decoder stream */
  159. st = av_new_stream(s, 0);
  160. if (!st)
  161. return AVERROR(ENOMEM);
  162. ea->video_stream_index = st->index;
  163. st->codec->codec_type = CODEC_TYPE_VIDEO;
  164. st->codec->codec_id = CODEC_ID_VP6;
  165. st->codec->codec_tag = 0; /* no fourcc */
  166. st->codec->time_base = ea->time_base;
  167. }
  168. /* initialize the audio decoder stream */
  169. st = av_new_stream(s, 0);
  170. if (!st)
  171. return AVERROR(ENOMEM);
  172. av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
  173. st->codec->codec_type = CODEC_TYPE_AUDIO;
  174. st->codec->codec_id = CODEC_ID_ADPCM_EA;
  175. st->codec->codec_tag = 0; /* no tag */
  176. st->codec->channels = ea->num_channels;
  177. st->codec->sample_rate = EA_SAMPLE_RATE;
  178. st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
  179. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  180. st->codec->bits_per_sample / 4;
  181. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  182. ea->audio_stream_index = st->index;
  183. ea->audio_frame_counter = 0;
  184. return 1;
  185. }
  186. static int ea_read_packet(AVFormatContext *s,
  187. AVPacket *pkt)
  188. {
  189. EaDemuxContext *ea = s->priv_data;
  190. ByteIOContext *pb = &s->pb;
  191. int ret = 0;
  192. int packet_read = 0;
  193. unsigned char preamble[EA_PREAMBLE_SIZE];
  194. unsigned int chunk_type, chunk_size;
  195. int key = 0;
  196. while (!packet_read) {
  197. if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
  198. return AVERROR(EIO);
  199. chunk_type = AV_RL32(&preamble[0]);
  200. chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
  201. switch (chunk_type) {
  202. /* audio data */
  203. case SCDl_TAG:
  204. ret = av_get_packet(pb, pkt, chunk_size);
  205. if (ret != chunk_size)
  206. ret = AVERROR(EIO);
  207. else {
  208. pkt->stream_index = ea->audio_stream_index;
  209. pkt->pts = 90000;
  210. pkt->pts *= ea->audio_frame_counter;
  211. pkt->pts /= EA_SAMPLE_RATE;
  212. /* 2 samples/byte, 1 or 2 samples per frame depending
  213. * on stereo; chunk also has 12-byte header */
  214. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  215. ea->num_channels;
  216. }
  217. packet_read = 1;
  218. break;
  219. /* ending tag */
  220. case SCEl_TAG:
  221. ret = AVERROR(EIO);
  222. packet_read = 1;
  223. break;
  224. case MV0K_TAG:
  225. key = PKT_FLAG_KEY;
  226. case MV0F_TAG:
  227. ret = av_get_packet(pb, pkt, chunk_size);
  228. if (ret != chunk_size)
  229. ret = AVERROR_IO;
  230. else {
  231. pkt->stream_index = ea->video_stream_index;
  232. pkt->flags |= key;
  233. }
  234. packet_read = 1;
  235. break;
  236. default:
  237. url_fseek(pb, chunk_size, SEEK_CUR);
  238. break;
  239. }
  240. }
  241. return ret;
  242. }
  243. AVInputFormat ea_demuxer = {
  244. "ea",
  245. "Electronic Arts Multimedia Format",
  246. sizeof(EaDemuxContext),
  247. ea_probe,
  248. ea_read_header,
  249. ea_read_packet,
  250. };