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.

296 lines
8.1KB

  1. /* Electronic Arts Multimedia File Demuxer
  2. * Copyright (c) 2004 The ffmpeg Project
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * @file electronicarts.c
  20. * Electronic Arts Multimedia file demuxer (WVE/UV2/etc.)
  21. * by Robin Kay (komadori at gekkou.co.uk)
  22. */
  23. #include "avformat.h"
  24. #define SCHl_TAG MKTAG('S', 'C', 'H', 'l')
  25. #define PT00_TAG MKTAG('P', 'T', 0x0, 0x0)
  26. #define SCDl_TAG MKTAG('S', 'C', 'D', 'l')
  27. #define pIQT_TAG MKTAG('p', 'I', 'Q', 'T')
  28. #define SCEl_TAG MKTAG('S', 'C', 'E', 'l')
  29. #define _TAG MKTAG('', '', '', '')
  30. #define EA_SAMPLE_RATE 22050
  31. #define EA_BITS_PER_SAMPLE 16
  32. #define EA_PREAMBLE_SIZE 8
  33. typedef struct EaDemuxContext {
  34. int width;
  35. int height;
  36. int video_stream_index;
  37. int track_count;
  38. int audio_stream_index;
  39. int audio_frame_counter;
  40. int64_t audio_pts;
  41. int64_t video_pts;
  42. int video_pts_inc;
  43. float fps;
  44. int num_channels;
  45. int num_samples;
  46. int compression_type;
  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 WVE file header
  63. * Returns 1 if the WVE file is valid and successfully opened, 0 otherwise
  64. */
  65. static int process_ea_header(AVFormatContext *s) {
  66. int inHeader;
  67. uint32_t blockid, size;
  68. EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
  69. ByteIOContext *pb = &s->pb;
  70. if (get_buffer(pb, (void*)&blockid, 4) != 4) {
  71. return 0;
  72. }
  73. if (le2me_32(blockid) != SCHl_TAG) {
  74. return 0;
  75. }
  76. if (get_buffer(pb, (void*)&size, 4) != 4) {
  77. return 0;
  78. }
  79. size = le2me_32(size);
  80. if (get_buffer(pb, (void*)&blockid, 4) != 4) {
  81. return 0;
  82. }
  83. if (le2me_32(blockid) != PT00_TAG) {
  84. av_log (s, AV_LOG_ERROR, "PT header missing\n");
  85. return 0;
  86. }
  87. inHeader = 1;
  88. while (inHeader) {
  89. int inSubheader;
  90. uint8_t byte;
  91. byte = get_byte(pb) & 0xFF;
  92. switch (byte) {
  93. case 0xFD:
  94. av_log (s, AV_LOG_INFO, "entered audio subheader\n");
  95. inSubheader = 1;
  96. while (inSubheader) {
  97. uint8_t subbyte;
  98. subbyte = get_byte(pb) & 0xFF;
  99. switch (subbyte) {
  100. case 0x82:
  101. ea->num_channels = read_arbitary(pb);
  102. av_log (s, AV_LOG_INFO, "num_channels (element 0x82) set to 0x%08x\n", ea->num_channels);
  103. break;
  104. case 0x83:
  105. ea->compression_type = read_arbitary(pb);
  106. av_log (s, AV_LOG_INFO, "compression_type (element 0x83) set to 0x%08x\n", ea->compression_type);
  107. break;
  108. case 0x85:
  109. ea->num_samples = read_arbitary(pb);
  110. av_log (s, AV_LOG_INFO, "num_samples (element 0x85) set to 0x%08x\n", ea->num_samples);
  111. break;
  112. case 0x8A:
  113. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  114. av_log (s, AV_LOG_INFO, "exited audio subheader\n");
  115. inSubheader = 0;
  116. break;
  117. default:
  118. av_log (s, AV_LOG_INFO, "element 0x%02x set to 0x%08x\n", subbyte, read_arbitary(pb));
  119. break;
  120. }
  121. }
  122. break;
  123. case 0xFF:
  124. av_log (s, AV_LOG_INFO, "end of header block reached\n");
  125. inHeader = 0;
  126. break;
  127. default:
  128. av_log (s, AV_LOG_INFO, "header element 0x%02x set to 0x%08x\n", byte, read_arbitary(pb));
  129. break;
  130. }
  131. }
  132. if ((ea->num_channels != 2) || (ea->compression_type != 7)) {
  133. av_log (s, AV_LOG_ERROR, "unsupported stream type\n");
  134. return 0;
  135. }
  136. /* skip to the start of the data */
  137. url_fseek(pb, size, SEEK_SET);
  138. return 1;
  139. }
  140. static int ea_probe(AVProbeData *p)
  141. {
  142. if (p->buf_size < 4)
  143. return 0;
  144. if (LE_32(&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 = (EaDemuxContext *)s->priv_data;
  152. AVStream *st;
  153. if (!process_ea_header(s))
  154. return AVERROR_IO;
  155. #if 0
  156. /* initialize the video decoder stream */
  157. st = av_new_stream(s, 0);
  158. if (!st)
  159. return AVERROR_NOMEM;
  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_NOMEM;
  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_IO;
  196. chunk_type = LE_32(&preamble[0]);
  197. chunk_size = LE_32(&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_IO;
  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_IO;
  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 = (EaDemuxContext *)s->priv_data;
  234. return 0;
  235. }
  236. static AVInputFormat ea_iformat = {
  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. };
  245. int ea_init(void)
  246. {
  247. av_register_input_format(&ea_iformat);
  248. return 0;
  249. }