wc3movie.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Wing Commander III Movie (.mve) File Demuxer
  3. * Copyright (c) 2003 The ffmpeg Project
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Wing Commander III Movie file demuxer
  24. * by Mike Melanson (melanson@pcisys.net)
  25. * for more information on the WC3 .mve file format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. */
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/dict.h"
  30. #include "avformat.h"
  31. #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
  32. #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
  33. #define PC__TAG MKTAG('_', 'P', 'C', '_')
  34. #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
  35. #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
  36. #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
  37. #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
  38. #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
  39. #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
  40. #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
  41. #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
  42. #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
  43. #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
  44. /* video resolution unless otherwise specified */
  45. #define WC3_DEFAULT_WIDTH 320
  46. #define WC3_DEFAULT_HEIGHT 165
  47. /* always use the same PCM audio parameters */
  48. #define WC3_SAMPLE_RATE 22050
  49. #define WC3_AUDIO_CHANNELS 1
  50. #define WC3_AUDIO_BITS 16
  51. /* nice, constant framerate */
  52. #define WC3_FRAME_FPS 15
  53. #define PALETTE_SIZE (256 * 3)
  54. typedef struct Wc3DemuxContext {
  55. int width;
  56. int height;
  57. int64_t pts;
  58. int video_stream_index;
  59. int audio_stream_index;
  60. AVPacket vpkt;
  61. } Wc3DemuxContext;
  62. static int wc3_probe(AVProbeData *p)
  63. {
  64. if (p->buf_size < 12)
  65. return 0;
  66. if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
  67. (AV_RL32(&p->buf[8]) != MOVE_TAG))
  68. return 0;
  69. return AVPROBE_SCORE_MAX;
  70. }
  71. static int wc3_read_header(AVFormatContext *s,
  72. AVFormatParameters *ap)
  73. {
  74. Wc3DemuxContext *wc3 = s->priv_data;
  75. AVIOContext *pb = s->pb;
  76. unsigned int fourcc_tag;
  77. unsigned int size;
  78. AVStream *st;
  79. int ret = 0;
  80. char *buffer;
  81. /* default context members */
  82. wc3->width = WC3_DEFAULT_WIDTH;
  83. wc3->height = WC3_DEFAULT_HEIGHT;
  84. wc3->pts = 0;
  85. wc3->video_stream_index = wc3->audio_stream_index = 0;
  86. av_init_packet(&wc3->vpkt);
  87. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  88. /* skip the first 3 32-bit numbers */
  89. avio_skip(pb, 12);
  90. /* traverse through the chunks and load the header information before
  91. * the first BRCH tag */
  92. fourcc_tag = avio_rl32(pb);
  93. size = (avio_rb32(pb) + 1) & (~1);
  94. do {
  95. switch (fourcc_tag) {
  96. case SOND_TAG:
  97. case INDX_TAG:
  98. /* SOND unknown, INDX unnecessary; ignore both */
  99. avio_skip(pb, size);
  100. break;
  101. case PC__TAG:
  102. /* number of palettes, unneeded */
  103. avio_skip(pb, 12);
  104. break;
  105. case BNAM_TAG:
  106. /* load up the name */
  107. buffer = av_malloc(size+1);
  108. if (!buffer)
  109. return AVERROR(ENOMEM);
  110. if ((ret = avio_read(pb, buffer, size)) != size)
  111. return AVERROR(EIO);
  112. buffer[size] = 0;
  113. av_dict_set(&s->metadata, "title", buffer,
  114. AV_DICT_DONT_STRDUP_VAL);
  115. break;
  116. case SIZE_TAG:
  117. /* video resolution override */
  118. wc3->width = avio_rl32(pb);
  119. wc3->height = avio_rl32(pb);
  120. break;
  121. case PALT_TAG:
  122. /* one of several palettes */
  123. avio_seek(pb, -8, SEEK_CUR);
  124. av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
  125. break;
  126. default:
  127. av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  128. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  129. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  130. return AVERROR_INVALIDDATA;
  131. break;
  132. }
  133. fourcc_tag = avio_rl32(pb);
  134. /* chunk sizes are 16-bit aligned */
  135. size = (avio_rb32(pb) + 1) & (~1);
  136. if (url_feof(pb))
  137. return AVERROR(EIO);
  138. } while (fourcc_tag != BRCH_TAG);
  139. /* initialize the decoder streams */
  140. st = av_new_stream(s, 0);
  141. if (!st)
  142. return AVERROR(ENOMEM);
  143. av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  144. wc3->video_stream_index = st->index;
  145. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  146. st->codec->codec_id = CODEC_ID_XAN_WC3;
  147. st->codec->codec_tag = 0; /* no fourcc */
  148. st->codec->width = wc3->width;
  149. st->codec->height = wc3->height;
  150. st = av_new_stream(s, 0);
  151. if (!st)
  152. return AVERROR(ENOMEM);
  153. av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  154. wc3->audio_stream_index = st->index;
  155. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  156. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  157. st->codec->codec_tag = 1;
  158. st->codec->channels = WC3_AUDIO_CHANNELS;
  159. st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
  160. st->codec->sample_rate = WC3_SAMPLE_RATE;
  161. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  162. st->codec->bits_per_coded_sample;
  163. st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
  164. return 0;
  165. }
  166. static int wc3_read_packet(AVFormatContext *s,
  167. AVPacket *pkt)
  168. {
  169. Wc3DemuxContext *wc3 = s->priv_data;
  170. AVIOContext *pb = s->pb;
  171. unsigned int fourcc_tag;
  172. unsigned int size;
  173. int packet_read = 0;
  174. int ret = 0;
  175. unsigned char text[1024];
  176. while (!packet_read) {
  177. fourcc_tag = avio_rl32(pb);
  178. /* chunk sizes are 16-bit aligned */
  179. size = (avio_rb32(pb) + 1) & (~1);
  180. if (url_feof(pb))
  181. return AVERROR(EIO);
  182. switch (fourcc_tag) {
  183. case BRCH_TAG:
  184. /* no-op */
  185. break;
  186. case SHOT_TAG:
  187. /* load up new palette */
  188. avio_seek(pb, -8, SEEK_CUR);
  189. av_append_packet(pb, &wc3->vpkt, 8 + 4);
  190. break;
  191. case VGA__TAG:
  192. /* send out video chunk */
  193. avio_seek(pb, -8, SEEK_CUR);
  194. ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
  195. // ignore error if we have some data
  196. if (wc3->vpkt.size > 0)
  197. ret = 0;
  198. *pkt = wc3->vpkt;
  199. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  200. pkt->stream_index = wc3->video_stream_index;
  201. pkt->pts = wc3->pts;
  202. packet_read = 1;
  203. break;
  204. case TEXT_TAG:
  205. /* subtitle chunk */
  206. #if 0
  207. avio_skip(pb, size);
  208. #else
  209. if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
  210. ret = AVERROR(EIO);
  211. else {
  212. int i = 0;
  213. av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
  214. av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
  215. i += text[i] + 1;
  216. av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
  217. i += text[i] + 1;
  218. av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
  219. }
  220. #endif
  221. break;
  222. case AUDI_TAG:
  223. /* send out audio chunk */
  224. ret= av_get_packet(pb, pkt, size);
  225. pkt->stream_index = wc3->audio_stream_index;
  226. pkt->pts = wc3->pts;
  227. /* time to advance pts */
  228. wc3->pts++;
  229. packet_read = 1;
  230. break;
  231. default:
  232. av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  233. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  234. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  235. ret = AVERROR_INVALIDDATA;
  236. packet_read = 1;
  237. break;
  238. }
  239. }
  240. return ret;
  241. }
  242. static int wc3_read_close(AVFormatContext *s)
  243. {
  244. Wc3DemuxContext *wc3 = s->priv_data;
  245. if (wc3->vpkt.size > 0)
  246. av_free_packet(&wc3->vpkt);
  247. return 0;
  248. }
  249. AVInputFormat ff_wc3_demuxer = {
  250. "wc3movie",
  251. NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
  252. sizeof(Wc3DemuxContext),
  253. wc3_probe,
  254. wc3_read_header,
  255. wc3_read_packet,
  256. wc3_read_close,
  257. };