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.

186 lines
5.4KB

  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 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. unsigned int audio_pts, video_pts;
  49. } MmDemuxContext;
  50. static int mm_probe(AVProbeData *p)
  51. {
  52. /* the first chunk is always the header */
  53. if (AV_RL16(&p->buf[0]) != MM_TYPE_HEADER)
  54. return 0;
  55. if (AV_RL32(&p->buf[2]) != MM_HEADER_LEN_V && AV_RL32(&p->buf[2]) != MM_HEADER_LEN_AV)
  56. return 0;
  57. /* only return half certainty since this check is a bit sketchy */
  58. return AVPROBE_SCORE_MAX / 2;
  59. }
  60. static int mm_read_header(AVFormatContext *s,
  61. AVFormatParameters *ap)
  62. {
  63. MmDemuxContext *mm = s->priv_data;
  64. ByteIOContext *pb = s->pb;
  65. AVStream *st;
  66. unsigned int type, length;
  67. unsigned int frame_rate, width, height;
  68. type = get_le16(pb);
  69. length = get_le32(pb);
  70. if (type != MM_TYPE_HEADER)
  71. return AVERROR_INVALIDDATA;
  72. /* read header */
  73. get_le16(pb); /* total number of chunks */
  74. frame_rate = get_le16(pb);
  75. get_le16(pb); /* ibm-pc video bios mode */
  76. width = get_le16(pb);
  77. height = get_le16(pb);
  78. url_fseek(pb, length - 10, SEEK_CUR); /* unknown data */
  79. /* video stream */
  80. st = av_new_stream(s, 0);
  81. if (!st)
  82. return AVERROR(ENOMEM);
  83. st->codec->codec_type = CODEC_TYPE_VIDEO;
  84. st->codec->codec_id = CODEC_ID_MMVIDEO;
  85. st->codec->codec_tag = 0; /* no fourcc */
  86. st->codec->width = width;
  87. st->codec->height = height;
  88. av_set_pts_info(st, 64, 1, frame_rate);
  89. /* audio stream */
  90. if (length == MM_HEADER_LEN_AV) {
  91. st = av_new_stream(s, 0);
  92. if (!st)
  93. return AVERROR(ENOMEM);
  94. st->codec->codec_type = CODEC_TYPE_AUDIO;
  95. st->codec->codec_tag = 0; /* no fourcc */
  96. st->codec->codec_id = CODEC_ID_PCM_U8;
  97. st->codec->channels = 1;
  98. st->codec->sample_rate = 8000;
  99. av_set_pts_info(st, 64, 1, 8000); /* 8000 hz */
  100. }
  101. mm->audio_pts = 0;
  102. mm->video_pts = 0;
  103. return 0;
  104. }
  105. static int mm_read_packet(AVFormatContext *s,
  106. AVPacket *pkt)
  107. {
  108. MmDemuxContext *mm = s->priv_data;
  109. ByteIOContext *pb = s->pb;
  110. unsigned char preamble[MM_PREAMBLE_SIZE];
  111. unsigned int type, length;
  112. while(1) {
  113. if (get_buffer(pb, preamble, MM_PREAMBLE_SIZE) != MM_PREAMBLE_SIZE) {
  114. return AVERROR(EIO);
  115. }
  116. type = AV_RL16(&preamble[0]);
  117. length = AV_RL16(&preamble[2]);
  118. switch(type) {
  119. case MM_TYPE_PALETTE :
  120. case MM_TYPE_INTER :
  121. case MM_TYPE_INTRA :
  122. case MM_TYPE_INTRA_HH :
  123. case MM_TYPE_INTER_HH :
  124. case MM_TYPE_INTRA_HHV :
  125. case MM_TYPE_INTER_HHV :
  126. /* output preamble + data */
  127. if (av_new_packet(pkt, length + MM_PREAMBLE_SIZE))
  128. return AVERROR(ENOMEM);
  129. memcpy(pkt->data, preamble, MM_PREAMBLE_SIZE);
  130. if (get_buffer(pb, pkt->data + MM_PREAMBLE_SIZE, length) != length)
  131. return AVERROR(EIO);
  132. pkt->size = length + MM_PREAMBLE_SIZE;
  133. pkt->stream_index = 0;
  134. pkt->pts = mm->video_pts;
  135. if (type!=MM_TYPE_PALETTE)
  136. mm->video_pts++;
  137. return 0;
  138. case MM_TYPE_AUDIO :
  139. if (av_get_packet(s->pb, pkt, length)<0)
  140. return AVERROR(ENOMEM);
  141. pkt->size = length;
  142. pkt->stream_index = 1;
  143. pkt->pts = mm->audio_pts++;
  144. return 0;
  145. default :
  146. av_log(NULL, AV_LOG_INFO, "mm: unknown chunk type 0x%x\n", type);
  147. url_fseek(pb, length, SEEK_CUR);
  148. }
  149. }
  150. return 0;
  151. }
  152. AVInputFormat mm_demuxer = {
  153. "mm",
  154. NULL_IF_CONFIG_SMALL("American Laser Games MM format"),
  155. sizeof(MmDemuxContext),
  156. mm_probe,
  157. mm_read_header,
  158. mm_read_packet,
  159. };