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.

312 lines
9.6KB

  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/avstring.h"
  29. #include "libavutil/channel_layout.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/dict.h"
  32. #include "avformat.h"
  33. #include "internal.h"
  34. #define FORM_TAG MKTAG('F', 'O', 'R', 'M')
  35. #define MOVE_TAG MKTAG('M', 'O', 'V', 'E')
  36. #define PC__TAG MKTAG('_', 'P', 'C', '_')
  37. #define SOND_TAG MKTAG('S', 'O', 'N', 'D')
  38. #define BNAM_TAG MKTAG('B', 'N', 'A', 'M')
  39. #define SIZE_TAG MKTAG('S', 'I', 'Z', 'E')
  40. #define PALT_TAG MKTAG('P', 'A', 'L', 'T')
  41. #define INDX_TAG MKTAG('I', 'N', 'D', 'X')
  42. #define BRCH_TAG MKTAG('B', 'R', 'C', 'H')
  43. #define SHOT_TAG MKTAG('S', 'H', 'O', 'T')
  44. #define VGA__TAG MKTAG('V', 'G', 'A', ' ')
  45. #define TEXT_TAG MKTAG('T', 'E', 'X', 'T')
  46. #define AUDI_TAG MKTAG('A', 'U', 'D', 'I')
  47. /* video resolution unless otherwise specified */
  48. #define WC3_DEFAULT_WIDTH 320
  49. #define WC3_DEFAULT_HEIGHT 165
  50. /* always use the same PCM audio parameters */
  51. #define WC3_SAMPLE_RATE 22050
  52. #define WC3_AUDIO_CHANNELS 1
  53. #define WC3_AUDIO_BITS 16
  54. /* nice, constant framerate */
  55. #define WC3_FRAME_FPS 15
  56. #define PALETTE_SIZE (256 * 3)
  57. typedef struct Wc3DemuxContext {
  58. int width;
  59. int height;
  60. int64_t pts;
  61. int video_stream_index;
  62. int audio_stream_index;
  63. AVPacket vpkt;
  64. } Wc3DemuxContext;
  65. static int wc3_probe(AVProbeData *p)
  66. {
  67. if (p->buf_size < 12)
  68. return 0;
  69. if ((AV_RL32(&p->buf[0]) != FORM_TAG) ||
  70. (AV_RL32(&p->buf[8]) != MOVE_TAG))
  71. return 0;
  72. return AVPROBE_SCORE_MAX;
  73. }
  74. static int wc3_read_header(AVFormatContext *s)
  75. {
  76. Wc3DemuxContext *wc3 = s->priv_data;
  77. AVIOContext *pb = s->pb;
  78. unsigned int fourcc_tag;
  79. unsigned int size;
  80. AVStream *st;
  81. int ret = 0;
  82. char *buffer;
  83. /* default context members */
  84. wc3->width = WC3_DEFAULT_WIDTH;
  85. wc3->height = WC3_DEFAULT_HEIGHT;
  86. wc3->pts = 0;
  87. wc3->video_stream_index = wc3->audio_stream_index = 0;
  88. av_init_packet(&wc3->vpkt);
  89. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  90. /* skip the first 3 32-bit numbers */
  91. avio_skip(pb, 12);
  92. /* traverse through the chunks and load the header information before
  93. * the first BRCH tag */
  94. fourcc_tag = avio_rl32(pb);
  95. size = (avio_rb32(pb) + 1) & (~1);
  96. do {
  97. switch (fourcc_tag) {
  98. case SOND_TAG:
  99. case INDX_TAG:
  100. /* SOND unknown, INDX unnecessary; ignore both */
  101. avio_skip(pb, size);
  102. break;
  103. case PC__TAG:
  104. /* number of palettes, unneeded */
  105. avio_skip(pb, 12);
  106. break;
  107. case BNAM_TAG:
  108. /* load up the name */
  109. buffer = av_malloc(size+1);
  110. if (!buffer)
  111. return AVERROR(ENOMEM);
  112. if ((ret = avio_read(pb, buffer, size)) != size)
  113. return AVERROR(EIO);
  114. buffer[size] = 0;
  115. av_dict_set(&s->metadata, "title", buffer,
  116. AV_DICT_DONT_STRDUP_VAL);
  117. break;
  118. case SIZE_TAG:
  119. /* video resolution override */
  120. wc3->width = avio_rl32(pb);
  121. wc3->height = avio_rl32(pb);
  122. break;
  123. case PALT_TAG:
  124. /* one of several palettes */
  125. avio_seek(pb, -8, SEEK_CUR);
  126. av_append_packet(pb, &wc3->vpkt, 8 + PALETTE_SIZE);
  127. break;
  128. default:
  129. av_log(s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  130. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  131. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  132. return AVERROR_INVALIDDATA;
  133. }
  134. fourcc_tag = avio_rl32(pb);
  135. /* chunk sizes are 16-bit aligned */
  136. size = (avio_rb32(pb) + 1) & (~1);
  137. if (url_feof(pb))
  138. return AVERROR(EIO);
  139. } while (fourcc_tag != BRCH_TAG);
  140. /* initialize the decoder streams */
  141. st = avformat_new_stream(s, NULL);
  142. if (!st)
  143. return AVERROR(ENOMEM);
  144. avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  145. wc3->video_stream_index = st->index;
  146. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  147. st->codec->codec_id = AV_CODEC_ID_XAN_WC3;
  148. st->codec->codec_tag = 0; /* no fourcc */
  149. st->codec->width = wc3->width;
  150. st->codec->height = wc3->height;
  151. st = avformat_new_stream(s, NULL);
  152. if (!st)
  153. return AVERROR(ENOMEM);
  154. avpriv_set_pts_info(st, 33, 1, WC3_FRAME_FPS);
  155. wc3->audio_stream_index = st->index;
  156. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  157. st->codec->codec_id = AV_CODEC_ID_PCM_S16LE;
  158. st->codec->codec_tag = 1;
  159. st->codec->channels = WC3_AUDIO_CHANNELS;
  160. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  161. st->codec->bits_per_coded_sample = WC3_AUDIO_BITS;
  162. st->codec->sample_rate = WC3_SAMPLE_RATE;
  163. st->codec->bit_rate = st->codec->channels * st->codec->sample_rate *
  164. st->codec->bits_per_coded_sample;
  165. st->codec->block_align = WC3_AUDIO_BITS * WC3_AUDIO_CHANNELS;
  166. return 0;
  167. }
  168. static int wc3_read_packet(AVFormatContext *s,
  169. AVPacket *pkt)
  170. {
  171. Wc3DemuxContext *wc3 = s->priv_data;
  172. AVIOContext *pb = s->pb;
  173. unsigned int fourcc_tag;
  174. unsigned int size;
  175. int packet_read = 0;
  176. int ret = 0;
  177. unsigned char text[1024];
  178. while (!packet_read) {
  179. fourcc_tag = avio_rl32(pb);
  180. /* chunk sizes are 16-bit aligned */
  181. size = (avio_rb32(pb) + 1) & (~1);
  182. if (url_feof(pb))
  183. return AVERROR(EIO);
  184. switch (fourcc_tag) {
  185. case BRCH_TAG:
  186. /* no-op */
  187. break;
  188. case SHOT_TAG:
  189. /* load up new palette */
  190. avio_seek(pb, -8, SEEK_CUR);
  191. av_append_packet(pb, &wc3->vpkt, 8 + 4);
  192. break;
  193. case VGA__TAG:
  194. /* send out video chunk */
  195. avio_seek(pb, -8, SEEK_CUR);
  196. ret= av_append_packet(pb, &wc3->vpkt, 8 + size);
  197. // ignore error if we have some data
  198. if (wc3->vpkt.size > 0)
  199. ret = 0;
  200. *pkt = wc3->vpkt;
  201. wc3->vpkt.data = NULL; wc3->vpkt.size = 0;
  202. pkt->stream_index = wc3->video_stream_index;
  203. pkt->pts = wc3->pts;
  204. packet_read = 1;
  205. break;
  206. case TEXT_TAG:
  207. /* subtitle chunk */
  208. #if 0
  209. avio_skip(pb, size);
  210. #else
  211. if ((unsigned)size > sizeof(text) || (ret = avio_read(pb, text, size)) != size)
  212. ret = AVERROR(EIO);
  213. else {
  214. int i = 0;
  215. av_log (s, AV_LOG_DEBUG, "Subtitle time!\n");
  216. if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
  217. return AVERROR_INVALIDDATA;
  218. av_log (s, AV_LOG_DEBUG, " inglish: %s\n", &text[i + 1]);
  219. i += text[i] + 1;
  220. if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
  221. return AVERROR_INVALIDDATA;
  222. av_log (s, AV_LOG_DEBUG, " doytsch: %s\n", &text[i + 1]);
  223. i += text[i] + 1;
  224. if (i >= size || av_strnlen(&text[i + 1], size - i - 1) >= size - i - 1)
  225. return AVERROR_INVALIDDATA;
  226. av_log (s, AV_LOG_DEBUG, " fronsay: %s\n", &text[i + 1]);
  227. }
  228. #endif
  229. break;
  230. case AUDI_TAG:
  231. /* send out audio chunk */
  232. ret= av_get_packet(pb, pkt, size);
  233. pkt->stream_index = wc3->audio_stream_index;
  234. pkt->pts = wc3->pts;
  235. /* time to advance pts */
  236. wc3->pts++;
  237. packet_read = 1;
  238. break;
  239. default:
  240. av_log (s, AV_LOG_ERROR, " unrecognized WC3 chunk: %c%c%c%c (0x%02X%02X%02X%02X)\n",
  241. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24),
  242. (uint8_t)fourcc_tag, (uint8_t)(fourcc_tag >> 8), (uint8_t)(fourcc_tag >> 16), (uint8_t)(fourcc_tag >> 24));
  243. ret = AVERROR_INVALIDDATA;
  244. packet_read = 1;
  245. break;
  246. }
  247. }
  248. return ret;
  249. }
  250. static int wc3_read_close(AVFormatContext *s)
  251. {
  252. Wc3DemuxContext *wc3 = s->priv_data;
  253. if (wc3->vpkt.size > 0)
  254. av_free_packet(&wc3->vpkt);
  255. return 0;
  256. }
  257. AVInputFormat ff_wc3_demuxer = {
  258. .name = "wc3movie",
  259. .long_name = NULL_IF_CONFIG_SMALL("Wing Commander III movie"),
  260. .priv_data_size = sizeof(Wc3DemuxContext),
  261. .read_probe = wc3_probe,
  262. .read_header = wc3_read_header,
  263. .read_packet = wc3_read_packet,
  264. .read_close = wc3_read_close,
  265. };