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.

241 lines
6.7KB

  1. /*
  2. * American Laser Games MM Video Decoder
  3. * Copyright (c) 2006,2008 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 Video Decoder
  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 "avcodec.h"
  34. #include "bytestream.h"
  35. #define MM_PREAMBLE_SIZE 6
  36. #define MM_TYPE_INTER 0x5
  37. #define MM_TYPE_INTRA 0x8
  38. #define MM_TYPE_INTRA_HH 0xc
  39. #define MM_TYPE_INTER_HH 0xd
  40. #define MM_TYPE_INTRA_HHV 0xe
  41. #define MM_TYPE_INTER_HHV 0xf
  42. #define MM_TYPE_PALETTE 0x31
  43. typedef struct MmContext {
  44. AVCodecContext *avctx;
  45. AVFrame frame;
  46. int palette[AVPALETTE_COUNT];
  47. GetByteContext gb;
  48. } MmContext;
  49. static av_cold int mm_decode_init(AVCodecContext *avctx)
  50. {
  51. MmContext *s = avctx->priv_data;
  52. s->avctx = avctx;
  53. avctx->pix_fmt = PIX_FMT_PAL8;
  54. avcodec_get_frame_defaults(&s->frame);
  55. s->frame.reference = 3;
  56. return 0;
  57. }
  58. static int mm_decode_pal(MmContext *s)
  59. {
  60. int i;
  61. bytestream2_skip(&s->gb, 4);
  62. for (i = 0; i < 128; i++) {
  63. s->palette[i] = 0xFF << 24 | bytestream2_get_be24(&s->gb);
  64. s->palette[i+128] = s->palette[i]<<2;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * @param half_horiz Half horizontal resolution (0 or 1)
  70. * @param half_vert Half vertical resolution (0 or 1)
  71. */
  72. static int mm_decode_intra(MmContext * s, int half_horiz, int half_vert)
  73. {
  74. int x, y;
  75. x=0; y=0;
  76. while (bytestream2_get_bytes_left(&s->gb) > 0) {
  77. int run_length, color;
  78. if (y >= s->avctx->height)
  79. return 0;
  80. color = bytestream2_get_byte(&s->gb);
  81. if (color & 0x80) {
  82. run_length = 1;
  83. }else{
  84. run_length = (color & 0x7f) + 2;
  85. color = bytestream2_get_byte(&s->gb);
  86. }
  87. if (half_horiz)
  88. run_length *=2;
  89. if (color) {
  90. memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
  91. if (half_vert)
  92. memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
  93. }
  94. x+= run_length;
  95. if (x >= s->avctx->width) {
  96. x=0;
  97. y += 1 + half_vert;
  98. }
  99. }
  100. return 0;
  101. }
  102. /*
  103. * @param half_horiz Half horizontal resolution (0 or 1)
  104. * @param half_vert Half vertical resolution (0 or 1)
  105. */
  106. static int mm_decode_inter(MmContext * s, int half_horiz, int half_vert)
  107. {
  108. int data_off = bytestream2_get_le16(&s->gb), y = 0;
  109. GetByteContext data_ptr;
  110. if (bytestream2_get_bytes_left(&s->gb) < data_off)
  111. return AVERROR_INVALIDDATA;
  112. bytestream2_init(&data_ptr, s->gb.buffer + data_off, bytestream2_get_bytes_left(&s->gb) - data_off);
  113. while (s->gb.buffer < data_ptr.buffer_start) {
  114. int i, j;
  115. int length = bytestream2_get_byte(&s->gb);
  116. int x = bytestream2_get_byte(&s->gb) + ((length & 0x80) << 1);
  117. length &= 0x7F;
  118. if (length==0) {
  119. y += x;
  120. continue;
  121. }
  122. if (y + half_vert >= s->avctx->height)
  123. return 0;
  124. for(i=0; i<length; i++) {
  125. int replace_array = bytestream2_get_byte(&s->gb);
  126. for(j=0; j<8; j++) {
  127. int replace = (replace_array >> (7-j)) & 1;
  128. if (replace) {
  129. int color = bytestream2_get_byte(&data_ptr);
  130. s->frame.data[0][y*s->frame.linesize[0] + x] = color;
  131. if (half_horiz)
  132. s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
  133. if (half_vert) {
  134. s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
  135. if (half_horiz)
  136. s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
  137. }
  138. }
  139. x += 1 + half_horiz;
  140. }
  141. }
  142. y += 1 + half_vert;
  143. }
  144. return 0;
  145. }
  146. static int mm_decode_frame(AVCodecContext *avctx,
  147. void *data, int *data_size,
  148. AVPacket *avpkt)
  149. {
  150. const uint8_t *buf = avpkt->data;
  151. int buf_size = avpkt->size;
  152. MmContext *s = avctx->priv_data;
  153. int type, res;
  154. if (buf_size < MM_PREAMBLE_SIZE)
  155. return AVERROR_INVALIDDATA;
  156. type = AV_RL16(&buf[0]);
  157. buf += MM_PREAMBLE_SIZE;
  158. buf_size -= MM_PREAMBLE_SIZE;
  159. bytestream2_init(&s->gb, buf, buf_size);
  160. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  161. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  162. return -1;
  163. }
  164. switch(type) {
  165. case MM_TYPE_PALETTE : res = mm_decode_pal(s); return buf_size;
  166. case MM_TYPE_INTRA : res = mm_decode_intra(s, 0, 0); break;
  167. case MM_TYPE_INTRA_HH : res = mm_decode_intra(s, 1, 0); break;
  168. case MM_TYPE_INTRA_HHV : res = mm_decode_intra(s, 1, 1); break;
  169. case MM_TYPE_INTER : res = mm_decode_inter(s, 0, 0); break;
  170. case MM_TYPE_INTER_HH : res = mm_decode_inter(s, 1, 0); break;
  171. case MM_TYPE_INTER_HHV : res = mm_decode_inter(s, 1, 1); break;
  172. default:
  173. res = AVERROR_INVALIDDATA;
  174. break;
  175. }
  176. if (res < 0)
  177. return res;
  178. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  179. *data_size = sizeof(AVFrame);
  180. *(AVFrame*)data = s->frame;
  181. return buf_size;
  182. }
  183. static av_cold int mm_decode_end(AVCodecContext *avctx)
  184. {
  185. MmContext *s = avctx->priv_data;
  186. if(s->frame.data[0])
  187. avctx->release_buffer(avctx, &s->frame);
  188. return 0;
  189. }
  190. AVCodec ff_mmvideo_decoder = {
  191. .name = "mmvideo",
  192. .type = AVMEDIA_TYPE_VIDEO,
  193. .id = CODEC_ID_MMVIDEO,
  194. .priv_data_size = sizeof(MmContext),
  195. .init = mm_decode_init,
  196. .close = mm_decode_end,
  197. .decode = mm_decode_frame,
  198. .capabilities = CODEC_CAP_DR1,
  199. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
  200. };