electronicarts.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 = (EaDemuxContext *)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 (p->buf_size < 4)
  145. return 0;
  146. if (AV_RL32(&p->buf[0]) != SCHl_TAG)
  147. return 0;
  148. return AVPROBE_SCORE_MAX;
  149. }
  150. static int ea_read_header(AVFormatContext *s,
  151. AVFormatParameters *ap)
  152. {
  153. EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
  154. AVStream *st;
  155. if (!process_ea_header(s))
  156. return AVERROR_IO;
  157. #if 0
  158. /* initialize the video decoder stream */
  159. st = av_new_stream(s, 0);
  160. if (!st)
  161. return AVERROR_NOMEM;
  162. av_set_pts_info(st, 33, 1, 90000);
  163. ea->video_stream_index = st->index;
  164. st->codec->codec_type = CODEC_TYPE_VIDEO;
  165. st->codec->codec_id = CODEC_ID_EA_MJPEG;
  166. st->codec->codec_tag = 0; /* no fourcc */
  167. #endif
  168. /* initialize the audio decoder stream */
  169. st = av_new_stream(s, 0);
  170. if (!st)
  171. return AVERROR_NOMEM;
  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. while (!packet_read) {
  196. if (get_buffer(pb, preamble, EA_PREAMBLE_SIZE) != EA_PREAMBLE_SIZE)
  197. return AVERROR_IO;
  198. chunk_type = AV_RL32(&preamble[0]);
  199. chunk_size = AV_RL32(&preamble[4]) - EA_PREAMBLE_SIZE;
  200. switch (chunk_type) {
  201. /* audio data */
  202. case SCDl_TAG:
  203. ret = av_get_packet(pb, pkt, chunk_size);
  204. if (ret != chunk_size)
  205. ret = AVERROR_IO;
  206. else {
  207. pkt->stream_index = ea->audio_stream_index;
  208. pkt->pts = 90000;
  209. pkt->pts *= ea->audio_frame_counter;
  210. pkt->pts /= EA_SAMPLE_RATE;
  211. /* 2 samples/byte, 1 or 2 samples per frame depending
  212. * on stereo; chunk also has 12-byte header */
  213. ea->audio_frame_counter += ((chunk_size - 12) * 2) /
  214. ea->num_channels;
  215. }
  216. packet_read = 1;
  217. break;
  218. /* ending tag */
  219. case SCEl_TAG:
  220. ret = AVERROR_IO;
  221. packet_read = 1;
  222. break;
  223. default:
  224. url_fseek(pb, chunk_size, SEEK_CUR);
  225. break;
  226. }
  227. /* ending packet */
  228. if (chunk_type == SCEl_TAG) {
  229. }
  230. }
  231. return ret;
  232. }
  233. static int ea_read_close(AVFormatContext *s)
  234. {
  235. // EaDemuxContext *ea = (EaDemuxContext *)s->priv_data;
  236. return 0;
  237. }
  238. AVInputFormat ea_demuxer = {
  239. "ea",
  240. "Electronic Arts Multimedia Format",
  241. sizeof(EaDemuxContext),
  242. ea_probe,
  243. ea_read_header,
  244. ea_read_packet,
  245. ea_read_close,
  246. };