wc3movie.c 9.1 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. #include "internal.h"
  32. #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
  33. #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
  34. #define PC__TAG MKTAG('_', 'P', 'C', '_')
  35. #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
  36. #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
  37. #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
  38. #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
  39. #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
  40. #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
  41. #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
  42. #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
  43. #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
  44. #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
  45. /* video resolution unless otherwise specified */
  46. #define WC3_DEFAULT_WIDTH 320
  47. #define WC3_DEFAULT_HEIGHT 165
  48. /* always use the same PCM audio parameters */
  49. #define WC3_SAMPLE_RATE 22050
  50. #define WC3_AUDIO_CHANNELS 1
  51. #define WC3_AUDIO_BITS 16
  52. /* nice, constant framerate */
  53. #define WC3_FRAME_FPS 15
  54. #define PALETTE_SIZE (256 * 3)
  55. typedef struct Wc3DemuxContext {
  56. int width;
  57. int height;
  58. int64_t pts;
  59. int video_stream_index;
  60. int audio_stream_index;
  61. AVPacket vpkt;
  62. } Wc3DemuxContext;
  63. static int wc3_probe(AVProbeData *p)
  64. {
  65. if (p->buf_size < 12)
  66. return 0;
  67. if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
  68. (AV_RL32(&p->buf[8]) != MOVE_TAG))
  69. return 0;
  70. return AVPROBE_SCORE_MAX;
  71. }
  72. static int wc3_read_header(AVFormatContext *s,
  73. AVFormatParameters *ap)
  74. {
  75. Wc3DemuxContext *wc3 = s->priv_data;
  76. AVIOContext *pb = s->pb;
  77. unsigned int fourcc_tag;
  78. unsigned int size;
  79. AVStream *st;
  80. int ret = 0;
  81. char *buffer;
  82. /* default context members */
  83. wc3->width = WC3_DEFAULT_WIDTH;
  84. wc3->height = WC3_DEFAULT_HEIGHT;
  85. wc3->pts = 0;
  86. wc3->video_stream_index = wc3->audio_stream_index = 0;
  87. av_init_packet(&wc3->vpkt);
  88. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  89. /* skip the first 3 32-bit numbers */
  90. avio_skip(pb, 12);
  91. /* traverse through the chunks and load the header information before
  92. * the first BRCH tag */
  93. fourcc_tag = avio_rl32(pb);
  94. size = (avio_rb32(pb) + 1) & (~1);
  95. do {
  96. switch (fourcc_tag) {
  97. case SOND_TAG:
  98. case INDX_TAG:
  99. /* SOND unknown, INDX unnecessary; ignore both */
  100. avio_skip(pb, size);
  101. break;
  102. case PC__TAG:
  103. /* number of palettes, unneeded */
  104. avio_skip(pb, 12);
  105. break;
  106. case BNAM_TAG:
  107. /* load up the name */
  108. buffer = av_malloc(size+1);
  109. if (!buffer)
  110. return AVERROR(ENOMEM);
  111. if ((ret = avio_read(pb, buffer, size)) != size)
  112. return AVERROR(EIO);
  113. buffer[size] = 0;
  114. av_dict_set(&s->metadata, "title", buffer,
  115. AV_DICT_DONT_STRDUP_VAL);
  116. break;
  117. case SIZE_TAG:
  118. /* video resolution override */
  119. wc3->width = avio_rl32(pb);
  120. wc3->height = avio_rl32(pb);
  121. break;
  122. case PALT_TAG:
  123. /* one of several palettes */
  124. avio_seek(pb, -8, SEEK_CUR);
  125. av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
  126. break;
  127. default:
  128. av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  129. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  130. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  131. return AVERROR_INVALIDDATA;
  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 = avformat_new_stream(s, NULL);
  141. if (!st)
  142. return AVERROR(ENOMEM);
  143. avpriv_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 = avformat_new_stream(s, NULL);
  151. if (!st)
  152. return AVERROR(ENOMEM);
  153. avpriv_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. .name = "wc3movie",
  251. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie format"),
  252. .priv_data_size = sizeof(Wc3DemuxContext),
  253. .read_probe = wc3_probe,
  254. .read_header = wc3_read_header,
  255. .read_packet = wc3_read_packet,
  256. .read_close = wc3_read_close,
  257. };