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.

213 lines
6.3KB

  1. /*
  2. * American Laser Games MM Format Demuxer
  3. * Copyright (c) 2006 Peter Ross
  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 St, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file mm.c
  23. * American Laser Games MM Format Demuxer
  24. * by Peter Ross (suxen_drol at hotmail dot com)
  25. *
  26. * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
  27. * including Mad Dog McCree and Crime Patrol.
  28. *
  29. * Technical details here:
  30. * http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
  31. */
  32. #include "avformat.h"
  33. #define MM_PREAMBLE_SIZE 6
  34. #define MM_TYPE_HEADER 0x0
  35. #define MM_TYPE_INTER 0x5
  36. #define MM_TYPE_INTRA 0x8
  37. #define MM_TYPE_INTRA_HH 0xc
  38. #define MM_TYPE_INTER_HH 0xd
  39. #define MM_TYPE_INTRA_HHV 0xe
  40. #define MM_TYPE_INTER_HHV 0xf
  41. #define MM_TYPE_AUDIO 0x15
  42. #define MM_TYPE_PALETTE 0x31
  43. #define MM_HEADER_LEN_V 0x16 /* video only */
  44. #define MM_HEADER_LEN_AV 0x18 /* video + audio */
  45. #define MM_PALETTE_COUNT 128
  46. #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
  47. typedef struct {
  48. AVPaletteControl palette_control;
  49. unsigned int audio_pts, video_pts;
  50. } MmDemuxContext;
  51. static int mm_probe(AVProbeData *p)
  52. {
  53. /* the first chunk is always the header */
  54. if (p->buf_size < MM_PREAMBLE_SIZE)
  55. return 0;
  56. if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
  57. return 0;
  58. if (AV_RL32(&p->buf[2]) != MM_HEADER_LEN_V && AV_RL32(&p->buf[2]) != MM_HEADER_LEN_AV)
  59. return 0;
  60. /* only return half certainty since this check is a bit sketchy */
  61. return AVPROBE_SCORE_MAX / 2;
  62. }
  63. static int mm_read_header(AVFormatContext *s,
  64. AVFormatParameters *ap)
  65. {
  66. MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
  67. ByteIOContext *pb = &s->pb;
  68. AVStream *st;
  69. unsigned int type, length;
  70. unsigned int frame_rate, width, height;
  71. type = get_le16(pb);
  72. length = get_le32(pb);
  73. if (type != MM_TYPE_HEADER)
  74. return AVERROR_INVALIDDATA;
  75. /* read header */
  76. get_le16(pb); /* total number of chunks */
  77. frame_rate = get_le16(pb);
  78. get_le16(pb); /* ibm-pc video bios mode */
  79. width = get_le16(pb);
  80. height = get_le16(pb);
  81. url_fseek(pb, length - 10, SEEK_CUR); /* unknown data */
  82. /* video stream */
  83. st = av_new_stream(s, 0);
  84. if (!st)
  85. return AVERROR_NOMEM;
  86. st->codec->codec_type = CODEC_TYPE_VIDEO;
  87. st->codec->codec_id = CODEC_ID_MMVIDEO;
  88. st->codec->codec_tag = 0; /* no fourcc */
  89. st->codec->width = width;
  90. st->codec->height = height;
  91. st->codec->palctrl = &mm->palette_control;
  92. av_set_pts_info(st, 64, 1, frame_rate);
  93. /* audio stream */
  94. if (length == MM_HEADER_LEN_AV) {
  95. st = av_new_stream(s, 0);
  96. if (!st)
  97. return AVERROR_NOMEM;
  98. st->codec->codec_type = CODEC_TYPE_AUDIO;
  99. st->codec->codec_tag = 0; /* no fourcc */
  100. st->codec->codec_id = CODEC_ID_PCM_U8;
  101. st->codec->channels = 1;
  102. st->codec->sample_rate = 8000;
  103. av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
  104. }
  105. mm->palette_control.palette_changed = 0;
  106. mm->audio_pts = 0;
  107. mm->video_pts = 0;
  108. return 0;
  109. }
  110. static int mm_read_packet(AVFormatContext *s,
  111. AVPacket *pkt)
  112. {
  113. MmDemuxContext *mm = (MmDemuxContext *)s->priv_data;
  114. ByteIOContext *pb = &s->pb;
  115. unsigned char preamble[MM_PREAMBLE_SIZE];
  116. unsigned char pal[MM_PALETTE_SIZE];
  117. unsigned int type, length;
  118. int i;
  119. while(1) {
  120. if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
  121. return AVERROR_IO;
  122. }
  123. type = AV_RL16(&preamble[0]);
  124. length = AV_RL16(&preamble[2]);
  125. switch(type) {
  126. case MM_TYPE_PALETTE :
  127. url_fseek(pb, 4, SEEK_CUR); /* unknown data */
  128. if (get_buffer(pb, pal, MM_PALETTE_SIZE) != MM_PALETTE_SIZE)
  129. return AVERROR_IO;
  130. url_fseek(pb, length - (4 + MM_PALETTE_SIZE), SEEK_CUR);
  131. for (i=0; i<MM_PALETTE_COUNT; i++) {
  132. int r = pal[i*3 + 0];
  133. int g = pal[i*3 + 1];
  134. int b = pal[i*3 + 2];
  135. mm->palette_control.palette[i] = (r << 16) | (g << 8) | (b);
  136. /* repeat palette, where each components is multiplied by four */
  137. mm->palette_control.palette[i+128] = (r << 18) | (g << 10) | (b<<2);
  138. }
  139. mm->palette_control.palette_changed = 1;
  140. break;
  141. case MM_TYPE_INTER :
  142. case MM_TYPE_INTRA :
  143. case MM_TYPE_INTRA_HH :
  144. case MM_TYPE_INTER_HH :
  145. case MM_TYPE_INTRA_HHV :
  146. case MM_TYPE_INTER_HHV :
  147. /* output preamble + data */
  148. if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
  149. return AVERROR_NOMEM;
  150. memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
  151. if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
  152. return AVERROR_IO;
  153. pkt->size = length + MM_PREAMBLE_SIZE;
  154. pkt->stream_index = 0;
  155. pkt->pts = mm->video_pts++;
  156. return 0;
  157. case MM_TYPE_AUDIO :
  158. if (av_get_packet(&s->pb, pkt, length)<0)
  159. return AVERROR_NOMEM;
  160. pkt->size = length;
  161. pkt->stream_index = 1;
  162. pkt->pts = mm->audio_pts++;
  163. return 0;
  164. default :
  165. av_log(NULL, AV_LOG_INFO, "mm: unknown chunk type 0x%x\n", type);
  166. url_fseek(pb, length, SEEK_CUR);
  167. }
  168. }
  169. return 0;
  170. }
  171. static int mm_read_close(AVFormatContext *s)
  172. {
  173. return 0;
  174. }
  175. AVInputFormat mm_demuxer = {
  176. "mm",
  177. "American Laser Games MM format",
  178. sizeof(MmDemuxContext),
  179. mm_probe,
  180. mm_read_header,
  181. mm_read_packet,
  182. mm_read_close,
  183. };