wc3movie.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. }
  132. fourcc_tag = avio_rl32(pb);
  133. /* chunk sizes are 16-bit aligned */
  134. size = (avio_rb32(pb) + 1) & (~1);
  135. if (url_feof(pb))
  136. return AVERROR(EIO);
  137. } while (fourcc_tag != BRCH_TAG);
  138. /* initialize the decoder streams */
  139. st = av_new_stream(s, 0);
  140. if (!st)
  141. return AVERROR(ENOMEM);
  142. av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  143. wc3->video_stream_index = st->index;
  144. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  145. st->codec->codec_id = CODEC_ID_XAN_WC3;
  146. st->codec->codec_tag = 0; /* no fourcc */
  147. st->codec->width = wc3->width;
  148. st->codec->height = wc3->height;
  149. st = av_new_stream(s, 0);
  150. if (!st)
  151. return AVERROR(ENOMEM);
  152. av_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  153. wc3->audio_stream_index = st->index;
  154. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  155. st->codec->codec_id = CODEC_ID_PCM_S16LE;
  156. st->codec->codec_tag = 1;
  157. st->codec->channels = WC3_AUDIO_CHANNELS;
  158. st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
  159. st->codec->sample_rate = WC3_SAMPLE_RATE;
  160. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  161. st->codec->bits_per_coded_sample;
  162. st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
  163. return 0;
  164. }
  165. static int wc3_read_packet(AVFormatContext *s,
  166. AVPacket *pkt)
  167. {
  168. Wc3DemuxContext *wc3 = s->priv_data;
  169. AVIOContext *pb = s->pb;
  170. unsigned int fourcc_tag;
  171. unsigned int size;
  172. int packet_read = 0;
  173. int ret = 0;
  174. unsigned char text[1024];
  175. while (!packet_read) {
  176. fourcc_tag = avio_rl32(pb);
  177. /* chunk sizes are 16-bit aligned */
  178. size = (avio_rb32(pb) + 1) & (~1);
  179. if (url_feof(pb))
  180. return AVERROR(EIO);
  181. switch (fourcc_tag) {
  182. case BRCH_TAG:
  183. /* no-op */
  184. break;
  185. case SHOT_TAG:
  186. /* load up new palette */
  187. avio_seek(pb, -8, SEEK_CUR);
  188. av_append_packet(pb, &wc3->vpkt, 8 + 4);
  189. break;
  190. case VGA__TAG:
  191. /* send out video chunk */
  192. avio_seek(pb, -8, SEEK_CUR);
  193. ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
  194. // ignore error if we have some data
  195. if (wc3->vpkt.size > 0)
  196. ret = 0;
  197. *pkt = wc3->vpkt;
  198. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  199. pkt->stream_index = wc3->video_stream_index;
  200. pkt->pts = wc3->pts;
  201. packet_read = 1;
  202. break;
  203. case TEXT_TAG:
  204. /* subtitle chunk */
  205. #if 0
  206. avio_skip(pb, size);
  207. #else
  208. if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
  209. ret = AVERROR(EIO);
  210. else {
  211. int i = 0;
  212. av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
  213. av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
  214. i += text[i] + 1;
  215. av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
  216. i += text[i] + 1;
  217. av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
  218. }
  219. #endif
  220. break;
  221. case AUDI_TAG:
  222. /* send out audio chunk */
  223. ret= av_get_packet(pb, pkt, size);
  224. pkt->stream_index = wc3->audio_stream_index;
  225. pkt->pts = wc3->pts;
  226. /* time to advance pts */
  227. wc3->pts++;
  228. packet_read = 1;
  229. break;
  230. default:
  231. av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  232. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  233. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  234. ret = AVERROR_INVALIDDATA;
  235. packet_read = 1;
  236. break;
  237. }
  238. }
  239. return ret;
  240. }
  241. static int wc3_read_close(AVFormatContext *s)
  242. {
  243. Wc3DemuxContext *wc3 = s->priv_data;
  244. if (wc3->vpkt.size > 0)
  245. av_free_packet(&wc3->vpkt);
  246. return 0;
  247. }
  248. AVInputFormat ff_wc3_demuxer = {
  249. .name = "wc3movie",
  250. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
  251. .priv_data_size = sizeof(Wc3DemuxContext),
  252. .read_probe = wc3_probe,
  253. .read_header = wc3_read_header,
  254. .read_packet = wc3_read_packet,
  255. .read_close = wc3_read_close,
  256. };