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.

198 lines
5.8KB

  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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * American Laser Games MM Format Demuxer
  24. * by Peter Ross (pross@xvid.org)
  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 "libavutil/channel_layout.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "avformat.h"
  35. #include "internal.h"
  36. #define MM_PREAMBLE_SIZE 6
  37. #define MM_TYPE_HEADER 0x0
  38. #define MM_TYPE_INTER 0x5
  39. #define MM_TYPE_INTRA 0x8
  40. #define MM_TYPE_INTRA_HH 0xc
  41. #define MM_TYPE_INTER_HH 0xd
  42. #define MM_TYPE_INTRA_HHV 0xe
  43. #define MM_TYPE_INTER_HHV 0xf
  44. #define MM_TYPE_AUDIO 0x15
  45. #define MM_TYPE_PALETTE 0x31
  46. #define MM_HEADER_LEN_V 0x16 /* video only */
  47. #define MM_HEADER_LEN_AV 0x18 /* video + audio */
  48. #define MM_PALETTE_COUNT 128
  49. #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
  50. typedef struct {
  51. unsigned int audio_pts, video_pts;
  52. } MmDemuxContext;
  53. static int probe(AVProbeData *p)
  54. {
  55. int len, type, fps, w, h;
  56. if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
  57. return 0;
  58. /* the first chunk is always the header */
  59. if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
  60. return 0;
  61. len = AV_RL32(&p->buf[2]);
  62. if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV)
  63. return 0;
  64. fps = AV_RL16(&p->buf[8]);
  65. w = AV_RL16(&p->buf[12]);
  66. h = AV_RL16(&p->buf[14]);
  67. if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
  68. return 0;
  69. type = AV_RL16(&p->buf[len]);
  70. if (!type || type > 0x31)
  71. return 0;
  72. /* only return half certainty since this check is a bit sketchy */
  73. return AVPROBE_SCORE_EXTENSION;
  74. }
  75. static int read_header(AVFormatContext *s)
  76. {
  77. MmDemuxContext *mm = s->priv_data;
  78. AVIOContext *pb = s->pb;
  79. AVStream *st;
  80. unsigned int type, length;
  81. unsigned int frame_rate, width, height;
  82. type = avio_rl16(pb);
  83. length = avio_rl32(pb);
  84. if (type != MM_TYPE_HEADER)
  85. return AVERROR_INVALIDDATA;
  86. /* read header */
  87. avio_rl16(pb); /* total number of chunks */
  88. frame_rate = avio_rl16(pb);
  89. avio_rl16(pb); /* ibm-pc video bios mode */
  90. width = avio_rl16(pb);
  91. height = avio_rl16(pb);
  92. avio_skip(pb, length - 10); /* unknown data */
  93. /* video stream */
  94. st = avformat_new_stream(s, NULL);
  95. if (!st)
  96. return AVERROR(ENOMEM);
  97. st->codec->codec_type = AVMEDIA_TYPE_VIDEO;
  98. st->codec->codec_id = AV_CODEC_ID_MMVIDEO;
  99. st->codec->codec_tag = 0; /* no fourcc */
  100. st->codec->width = width;
  101. st->codec->height = height;
  102. avpriv_set_pts_info(st, 64, 1, frame_rate);
  103. /* audio stream */
  104. if (length == MM_HEADER_LEN_AV) {
  105. st = avformat_new_stream(s, NULL);
  106. if (!st)
  107. return AVERROR(ENOMEM);
  108. st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
  109. st->codec->codec_tag = 0; /* no fourcc */
  110. st->codec->codec_id = AV_CODEC_ID_PCM_U8;
  111. st->codec->channels = 1;
  112. st->codec->channel_layout = AV_CH_LAYOUT_MONO;
  113. st->codec->sample_rate = 8000;
  114. avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
  115. }
  116. mm->audio_pts = 0;
  117. mm->video_pts = 0;
  118. return 0;
  119. }
  120. static int read_packet(AVFormatContext *s,
  121. AVPacket *pkt)
  122. {
  123. MmDemuxContext *mm = s->priv_data;
  124. AVIOContext *pb = s->pb;
  125. unsigned char preamble[MM_PREAMBLE_SIZE];
  126. unsigned int type, length;
  127. while(1) {
  128. if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
  129. return AVERROR(EIO);
  130. }
  131. type = AV_RL16(&preamble[0]);
  132. length = AV_RL16(&preamble[2]);
  133. switch(type) {
  134. case MM_TYPE_PALETTE :
  135. case MM_TYPE_INTER :
  136. case MM_TYPE_INTRA :
  137. case MM_TYPE_INTRA_HH :
  138. case MM_TYPE_INTER_HH :
  139. case MM_TYPE_INTRA_HHV :
  140. case MM_TYPE_INTER_HHV :
  141. /* output preamble + data */
  142. if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
  143. return AVERROR(ENOMEM);
  144. memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
  145. if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
  146. return AVERROR(EIO);
  147. pkt->size = length + MM_PREAMBLE_SIZE;
  148. pkt->stream_index = 0;
  149. pkt->pts = mm->video_pts;
  150. if (type!=MM_TYPE_PALETTE)
  151. mm->video_pts++;
  152. return 0;
  153. case MM_TYPE_AUDIO :
  154. if (av_get_packet(s->pb, pkt, length)<0)
  155. return AVERROR(ENOMEM);
  156. pkt->stream_index = 1;
  157. pkt->pts = mm->audio_pts++;
  158. return 0;
  159. default :
  160. av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
  161. avio_skip(pb, length);
  162. }
  163. }
  164. }
  165. AVInputFormat ff_mm_demuxer = {
  166. .name = "mm",
  167. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM"),
  168. .priv_data_size = sizeof(MmDemuxContext),
  169. .read_probe = probe,
  170. .read_header = read_header,
  171. .read_packet = read_packet,
  172. };