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.

213 lines
5.8KB

  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 mm.c
  23. * American Laser Games MM Video Decoder
  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 "avcodec.h"
  33. #define MM_PREAMBLE_SIZE 6
  34. #define MM_TYPE_INTER 0x5
  35. #define MM_TYPE_INTRA 0x8
  36. #define MM_TYPE_INTRA_HH 0xc
  37. #define MM_TYPE_INTER_HH 0xd
  38. #define MM_TYPE_INTRA_HHV 0xe
  39. #define MM_TYPE_INTER_HHV 0xf
  40. #define MM_TYPE_PALETTE 0x31
  41. typedef struct MmContext {
  42. AVCodecContext *avctx;
  43. AVFrame frame;
  44. int palette[AVPALETTE_COUNT];
  45. } MmContext;
  46. static av_cold int mm_decode_init(AVCodecContext *avctx)
  47. {
  48. MmContext *s = avctx->priv_data;
  49. s->avctx = avctx;
  50. avctx->pix_fmt = PIX_FMT_PAL8;
  51. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  52. return -1;
  53. s->frame.reference = 1;
  54. if (avctx->get_buffer(avctx, &s->frame)) {
  55. av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n");
  56. return -1;
  57. }
  58. return 0;
  59. }
  60. static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
  61. {
  62. int i;
  63. buf += 4;
  64. for (i=0; i<128 && buf+2<buf_end; i++) {
  65. s->palette[i] = AV_RB24(buf);
  66. s->palette[i+128] = s->palette[i]<<2;
  67. buf += 3;
  68. }
  69. }
  70. static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  71. {
  72. int i, x, y;
  73. i=0; x=0; y=0;
  74. while(i<buf_size) {
  75. int run_length, color;
  76. if (buf[i] & 0x80) {
  77. run_length = 1;
  78. color = buf[i];
  79. i++;
  80. }else{
  81. run_length = (buf[i] & 0x7f) + 2;
  82. color = buf[i+1];
  83. i+=2;
  84. }
  85. if (half_horiz)
  86. run_length *=2;
  87. if (color) {
  88. memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
  89. if (half_vert)
  90. memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
  91. }
  92. x+= run_length;
  93. if (x >= s->avctx->width) {
  94. x=0;
  95. y += half_vert ? 2 : 1;
  96. }
  97. }
  98. }
  99. static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  100. {
  101. const int data_ptr = 2 + AV_RL16(&buf[0]);
  102. int d, r, y;
  103. d = data_ptr; r = 2; y = 0;
  104. while(r < data_ptr) {
  105. int i, j;
  106. int length = buf[r] & 0x7f;
  107. int x = buf[r+1] + ((buf[r] & 0x80) << 1);
  108. r += 2;
  109. if (length==0) {
  110. y += x;
  111. continue;
  112. }
  113. for(i=0; i<length; i++) {
  114. for(j=0; j<8; j++) {
  115. int replace = (buf[r+i] >> (7-j)) & 1;
  116. if (replace) {
  117. int color = buf[d];
  118. s->frame.data[0][y*s->frame.linesize[0] + x] = color;
  119. if (half_horiz)
  120. s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
  121. if (half_vert) {
  122. s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
  123. if (half_horiz)
  124. s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
  125. }
  126. d++;
  127. }
  128. x += half_horiz ? 2 : 1;
  129. }
  130. }
  131. r += length;
  132. y += half_vert ? 2 : 1;
  133. }
  134. }
  135. static int mm_decode_frame(AVCodecContext *avctx,
  136. void *data, int *data_size,
  137. const uint8_t *buf, int buf_size)
  138. {
  139. MmContext *s = avctx->priv_data;
  140. const uint8_t *buf_end = buf+buf_size;
  141. int type;
  142. type = AV_RL16(&buf[0]);
  143. buf += MM_PREAMBLE_SIZE;
  144. buf_size -= MM_PREAMBLE_SIZE;
  145. switch(type) {
  146. case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
  147. case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
  148. case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
  149. case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
  150. case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
  151. case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
  152. case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
  153. default :
  154. return -1;
  155. }
  156. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  157. *data_size = sizeof(AVFrame);
  158. *(AVFrame*)data = s->frame;
  159. return buf_size;
  160. }
  161. static av_cold int mm_decode_end(AVCodecContext *avctx)
  162. {
  163. MmContext *s = avctx->priv_data;
  164. if(s->frame.data[0])
  165. avctx->release_buffer(avctx, &s->frame);
  166. return 0;
  167. }
  168. AVCodec mmvideo_decoder = {
  169. "mmvideo",
  170. CODEC_TYPE_VIDEO,
  171. CODEC_ID_MMVIDEO,
  172. sizeof(MmContext),
  173. mm_decode_init,
  174. NULL,
  175. mm_decode_end,
  176. mm_decode_frame,
  177. CODEC_CAP_DR1,
  178. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
  179. };