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.

289 lines
7.9KB

  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 SCDl_TAG MKTAG('S', 'C', 'D', 'l')
  29. #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')
  30. #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
  31. #define _TAG MKTAG('', '', '', '')
  32. #define EA_SAMPLE_RATE 22050
  33. #define EA_BITS_PER_SAMPLE 16
  34. #define EA_PREAMBLE_SIZE 8
  35. typedef struct EaDemuxContext {
  36. int width;
  37. int height;
  38. int video_stream_index;
  39. int track_count;
  40. int audio_stream_index;
  41. int audio_frame_counter;
  42. int64_t audio_pts;
  43. int64_t video_pts;
  44. int video_pts_inc;
  45. float fps;
  46. int num_channels;
  47. int num_samples;
  48. int compression_type;
  49. } EaDemuxContext;
  50. static uint32_t read_arbitary(ByteIOContext *pb) {
  51. uint8_t size, byte;
  52. int i;
  53. uint32_t word;
  54. size = get_byte(pb);
  55. word = 0;
  56. for (i = 0; i < size; i++) {
  57. byte = get_byte(pb);
  58. word <<= 8;
  59. word |= byte;
  60. }
  61. return word;
  62. }
  63. /*
  64. * Process WVE file header
  65. * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
  66. */
  67. static int process_ea_header(AVFormatContext *s) {
  68. int inHeader;
  69. uint32_t blockid, size;
  70. EaDemuxContext *ea = s->priv_data;
  71. ByteIOContext *pb = &s->pb;
  72. if (get_buffer(pb, (void*)&blockid, 4) != 4) {
  73. return 0;
  74. }
  75. if (le2me_32(blockid) != SCHl_TAG) {
  76. return 0;
  77. }
  78. if (get_buffer(pb, (void*)&size, 4) != 4) {
  79. return 0;
  80. }
  81. size = le2me_32(size);
  82. if (get_buffer(pb, (void*)&blockid, 4) != 4) {
  83. return 0;
  84. }
  85. if (le2me_32(blockid) != PT00_TAG) {
  86. av_log (s, AV_LOG_ERROR, "PT header missing\n");
  87. return 0;
  88. }
  89. inHeader = 1;
  90. while (inHeader) {
  91. int inSubheader;
  92. uint8_t byte;
  93. byte = get_byte(pb) & 0xFF;
  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) & 0xFF;
  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. default:
  120. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  121. break;
  122. }
  123. }
  124. break;
  125. case 0xFF:
  126. av_log (s, AV_LOG_INFO, "end of header block reached\n");
  127. inHeader = 0;
  128. break;
  129. default:
  130. av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
  131. break;
  132. }
  133. }
  134. if ((ea->num_channels != 2) || (ea->compression_type != 7)) {
  135. av_log (s, AV_LOG_ERROR, "unsupported stream type\n");
  136. return 0;
  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. if (AV_RL32(&p->buf[0]) != SCHl_TAG)
  145. return 0;
  146. return AVPROBE_SCORE_MAX;
  147. }
  148. static int ea_read_header(AVFormatContext *s,
  149. AVFormatParameters *ap)
  150. {
  151. EaDemuxContext *ea = s->priv_data;
  152. AVStream *st;
  153. if (!process_ea_header(s))
  154. return AVERROR(EIO);
  155. #if 0
  156. /* initialize the video decoder stream */
  157. st = av_new_stream(s, 0);
  158. if (!st)
  159. return AVERROR(ENOMEM);
  160. av_set_pts_info(st, 33, 1, 90000);
  161. ea->video_stream_index = st->index;
  162. st->codec->codec_type = CODEC_TYPE_VIDEO;
  163. st->codec->codec_id = CODEC_ID_EA_MJPEG;
  164. st->codec->codec_tag = 0; /* no fourcc */
  165. #endif
  166. /* initialize the audio decoder stream */
  167. st = av_new_stream(s, 0);
  168. if (!st)
  169. return AVERROR(ENOMEM);
  170. av_set_pts_info(st, 33, 1, EA_SAMPLE_RATE);
  171. st->codec->codec_type = CODEC_TYPE_AUDIO;
  172. st->codec->codec_id = CODEC_ID_ADPCM_EA;
  173. st->codec->codec_tag = 0; /* no tag */
  174. st->codec->channels = ea->num_channels;
  175. st->codec->sample_rate = EA_SAMPLE_RATE;
  176. st->codec->bits_per_sample = EA_BITS_PER_SAMPLE;
  177. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  178. st->codec->bits_per_sample / 4;
  179. st->codec->block_align = st->codec->channels * st->codec->bits_per_sample;
  180. ea->audio_stream_index = st->index;
  181. ea->audio_frame_counter = 0;
  182. return 1;
  183. }
  184. static int ea_read_packet(AVFormatContext *s,
  185. AVPacket *pkt)
  186. {
  187. EaDemuxContext *ea = s->priv_data;
  188. ByteIOContext *pb = &s->pb;
  189. int ret = 0;
  190. int packet_read = 0;
  191. unsigned char preamble[EA_PREAMBLE_SIZE];
  192. unsigned int chunk_type, chunk_size;
  193. while (!packet_read) {
  194. if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
  195. return AVERROR(EIO);
  196. chunk_type = AV_RL32(&preamble[0]);
  197. chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
  198. switch (chunk_type) {
  199. /* audio data */
  200. case SCDl_TAG:
  201. ret = av_get_packet(pb, pkt, chunk_size);
  202. if (ret != chunk_size)
  203. ret = AVERROR(EIO);
  204. else {
  205. pkt->stream_index = ea->audio_stream_index;
  206. pkt->pts = 90000;
  207. pkt->pts *= ea->audio_frame_counter;
  208. pkt->pts /= EA_SAMPLE_RATE;
  209. /* 2 samples/byte, 1 or 2 samples per frame depending
  210. * on stereo; chunk also has 12-byte header */
  211. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  212. ea->num_channels;
  213. }
  214. packet_read = 1;
  215. break;
  216. /* ending tag */
  217. case SCEl_TAG:
  218. ret = AVERROR(EIO);
  219. packet_read = 1;
  220. break;
  221. default:
  222. url_fseek(pb, chunk_size, SEEK_CUR);
  223. break;
  224. }
  225. /* ending packet */
  226. if (chunk_type == SCEl_TAG) {
  227. }
  228. }
  229. return ret;
  230. }
  231. static int ea_read_close(AVFormatContext *s)
  232. {
  233. // EaDemuxContext *ea = s->priv_data;
  234. return 0;
  235. }
  236. AVInputFormat ea_demuxer = {
  237. "ea",
  238. "Electronic Arts Multimedia Format",
  239. sizeof(EaDemuxContext),
  240. ea_probe,
  241. ea_read_header,
  242. ea_read_packet,
  243. ea_read_close,
  244. };