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.

301 lines
9.1KB

  1. /*
  2. * Wing Commander III Movie (.mve) File Demuxer
  3. * Copyright (c) 2003 The FFmpeg project
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/channel_layout.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/dict.h"
  31. #include "avformat.h"
  32. #include "internal.h"
  33. #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
  34. #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
  35. #define PC__TAG MKTAG('_', 'P', 'C', '_')
  36. #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
  37. #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
  38. #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
  39. #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
  40. #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
  41. #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
  42. #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
  43. #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
  44. #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
  45. #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
  46. /* video resolution unless otherwise specified */
  47. #define WC3_DEFAULT_WIDTH 320
  48. #define WC3_DEFAULT_HEIGHT 165
  49. /* always use the same PCM audio parameters */
  50. #define WC3_SAMPLE_RATE 22050
  51. #define WC3_AUDIO_CHANNELS 1
  52. #define WC3_AUDIO_BITS 16
  53. /* nice, constant framerate */
  54. #define WC3_FRAME_FPS 15
  55. #define PALETTE_SIZE (256 * 3)
  56. typedef struct Wc3DemuxContext {
  57. int width;
  58. int height;
  59. int64_t pts;
  60. int video_stream_index;
  61. int audio_stream_index;
  62. AVPacket vpkt;
  63. } Wc3DemuxContext;
  64. static int wc3_probe(AVProbeData *p)
  65. {
  66. if (p->buf_size < 12)
  67. return 0;
  68. if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
  69. (AV_RL32(&p->buf[8]) != MOVE_TAG))
  70. return 0;
  71. return AVPROBE_SCORE_MAX;
  72. }
  73. static int wc3_read_header(AVFormatContext *s)
  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 (pb->eof_reached)
  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->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
  146. st->codecpar->codec_id = AV_CODEC_ID_XAN_WC3;
  147. st->codecpar->codec_tag = 0; /* no fourcc */
  148. st->codecpar->width = wc3->width;
  149. st->codecpar->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->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
  156. st->codecpar->codec_id = AV_CODEC_ID_PCM_S16LE;
  157. st->codecpar->codec_tag = 1;
  158. st->codecpar->channels = WC3_AUDIO_CHANNELS;
  159. st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
  160. st->codecpar->bits_per_coded_sample = WC3_AUDIO_BITS;
  161. st->codecpar->sample_rate = WC3_SAMPLE_RATE;
  162. st->codecpar->bit_rate = st->codecpar->channels * st->codecpar->sample_rate *
  163. st->codecpar->bits_per_coded_sample;
  164. st->codecpar->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
  165. return 0;
  166. }
  167. static int wc3_read_packet(AVFormatContext *s,
  168. AVPacket *pkt)
  169. {
  170. Wc3DemuxContext *wc3 = s->priv_data;
  171. AVIOContext *pb = s->pb;
  172. unsigned int fourcc_tag;
  173. unsigned int size;
  174. int packet_read = 0;
  175. int ret = 0;
  176. unsigned char text[1024];
  177. while (!packet_read) {
  178. fourcc_tag = avio_rl32(pb);
  179. /* chunk sizes are 16-bit aligned */
  180. size = (avio_rb32(pb) + 1) & (~1);
  181. if (pb->eof_reached)
  182. return AVERROR(EIO);
  183. switch (fourcc_tag) {
  184. case BRCH_TAG:
  185. /* no-op */
  186. break;
  187. case SHOT_TAG:
  188. /* load up new palette */
  189. avio_seek(pb, -8, SEEK_CUR);
  190. av_append_packet(pb, &wc3->vpkt, 8 + 4);
  191. break;
  192. case VGA__TAG:
  193. /* send out video chunk */
  194. avio_seek(pb, -8, SEEK_CUR);
  195. ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
  196. // ignore error if we have some data
  197. if (wc3->vpkt.size > 0)
  198. ret = 0;
  199. *pkt = wc3->vpkt;
  200. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  201. pkt->stream_index = wc3->video_stream_index;
  202. pkt->pts = wc3->pts;
  203. packet_read = 1;
  204. break;
  205. case TEXT_TAG:
  206. /* subtitle chunk */
  207. if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
  208. ret = AVERROR(EIO);
  209. else {
  210. int i = 0;
  211. av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
  212. av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
  213. i += text[i] + 1;
  214. av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
  215. i += text[i] + 1;
  216. av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
  217. }
  218. break;
  219. case AUDI_TAG:
  220. /* send out audio chunk */
  221. ret= av_get_packet(pb, pkt, size);
  222. pkt->stream_index = wc3->audio_stream_index;
  223. pkt->pts = wc3->pts;
  224. /* time to advance pts */
  225. wc3->pts++;
  226. packet_read = 1;
  227. break;
  228. default:
  229. av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  230. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  231. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  232. ret = AVERROR_INVALIDDATA;
  233. packet_read = 1;
  234. break;
  235. }
  236. }
  237. return ret;
  238. }
  239. static int wc3_read_close(AVFormatContext *s)
  240. {
  241. Wc3DemuxContext *wc3 = s->priv_data;
  242. if (wc3->vpkt.size > 0)
  243. av_packet_unref(&wc3->vpkt);
  244. return 0;
  245. }
  246. AVInputFormat ff_wc3_demuxer = {
  247. .name = "wc3movie",
  248. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
  249. .priv_data_size = sizeof(Wc3DemuxContext),
  250. .read_probe = wc3_probe,
  251. .read_header = wc3_read_header,
  252. .read_packet = wc3_read_packet,
  253. .read_close = wc3_read_close,
  254. };