mm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/intreadwrite.h"
  33. #include "avformat.h"
  34. #include "internal.h"
  35. #define MM_PREAMBLE_SIZE 6
  36. #define MM_TYPE_HEADER 0x0
  37. #define MM_TYPE_INTER 0x5
  38. #define MM_TYPE_INTRA 0x8
  39. #define MM_TYPE_INTRA_HH 0xc
  40. #define MM_TYPE_INTER_HH 0xd
  41. #define MM_TYPE_INTRA_HHV 0xe
  42. #define MM_TYPE_INTER_HHV 0xf
  43. #define MM_TYPE_AUDIO 0x15
  44. #define MM_TYPE_PALETTE 0x31
  45. #define MM_HEADER_LEN_V 0x16 /* video only */
  46. #define MM_HEADER_LEN_AV 0x18 /* video + audio */
  47. #define MM_PALETTE_COUNT 128
  48. #define MM_PALETTE_SIZE (MM_PALETTE_COUNT*3)
  49. typedef struct {
  50. unsigned int audio_pts, video_pts;
  51. } MmDemuxContext;
  52. static int probe(AVProbeData *p)
  53. {
  54. int len, type, fps, w, h;
  55. if (p->buf_size < MM_HEADER_LEN_AV + MM_PREAMBLE_SIZE)
  56. return 0;
  57. /* the first chunk is always the header */
  58. if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
  59. return 0;
  60. len = AV_RL32(&p->buf[2]);
  61. if (len != MM_HEADER_LEN_V && len != MM_HEADER_LEN_AV)
  62. return 0;
  63. fps = AV_RL16(&p->buf[8]);
  64. w = AV_RL16(&p->buf[12]);
  65. h = AV_RL16(&p->buf[14]);
  66. if (!fps || fps > 60 || !w || w > 2048 || !h || h > 2048)
  67. return 0;
  68. type = AV_RL16(&p->buf[len]);
  69. if (!type || type > 0x31)
  70. return 0;
  71. /* only return half certainty since this check is a bit sketchy */
  72. return AVPROBE_SCORE_MAX / 2;
  73. }
  74. static int read_header(AVFormatContext *s,
  75. AVFormatParameters *ap)
  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 = 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 = CODEC_ID_PCM_U8;
  111. st->codec->channels = 1;
  112. st->codec->sample_rate = 8000;
  113. avpriv_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
  114. }
  115. mm->audio_pts = 0;
  116. mm->video_pts = 0;
  117. return 0;
  118. }
  119. static int read_packet(AVFormatContext *s,
  120. AVPacket *pkt)
  121. {
  122. MmDemuxContext *mm = s->priv_data;
  123. AVIOContext *pb = s->pb;
  124. unsigned char preamble[MM_PREAMBLE_SIZE];
  125. unsigned int type, length;
  126. while(1) {
  127. if (avio_read(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
  128. return AVERROR(EIO);
  129. }
  130. type = AV_RL16(&preamble[0]);
  131. length = AV_RL16(&preamble[2]);
  132. switch(type) {
  133. case MM_TYPE_PALETTE :
  134. case MM_TYPE_INTER :
  135. case MM_TYPE_INTRA :
  136. case MM_TYPE_INTRA_HH :
  137. case MM_TYPE_INTER_HH :
  138. case MM_TYPE_INTRA_HHV :
  139. case MM_TYPE_INTER_HHV :
  140. /* output preamble + data */
  141. if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
  142. return AVERROR(ENOMEM);
  143. memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
  144. if (avio_read(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
  145. return AVERROR(EIO);
  146. pkt->size = length + MM_PREAMBLE_SIZE;
  147. pkt->stream_index = 0;
  148. pkt->pts = mm->video_pts;
  149. if (type!=MM_TYPE_PALETTE)
  150. mm->video_pts++;
  151. return 0;
  152. case MM_TYPE_AUDIO :
  153. if (av_get_packet(s->pb, pkt, length)<0)
  154. return AVERROR(ENOMEM);
  155. pkt->stream_index = 1;
  156. pkt->pts = mm->audio_pts++;
  157. return 0;
  158. default :
  159. av_log(s, AV_LOG_INFO, "unknown chunk type 0x%x\n", type);
  160. avio_skip(pb, length);
  161. }
  162. }
  163. }
  164. AVInputFormat ff_mm_demuxer = {
  165. .name = "mm",
  166. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM format"),
  167. .priv_data_size = sizeof(MmDemuxContext),
  168. .read_probe = probe,
  169. .read_header = read_header,
  170. .read_packet = read_packet,
  171. };