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.

207 lines
5.6KB

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