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.

211 lines
6.3KB

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