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.

205 lines
5.6KB

  1. /*
  2. * American Laser Games MM Video Decoder
  3. * Copyright (c) 2006 Peter Ross
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file mm.c
  21. * American Laser Games MM Video Decoder
  22. * by Peter Ross (suxen_drol at hotmail dot com)
  23. *
  24. * The MM format was used by IBM-PC ports of ALG's "arcade shooter" games,
  25. * including Mad Dog McCree and Crime Patrol.
  26. *
  27. * Technical details here:
  28. * http://wiki.multimedia.cx/index.php?title=American_Laser_Games_MM
  29. */
  30. #include "avcodec.h"
  31. #define MM_PREAMBLE_SIZE 6
  32. #define MM_TYPE_INTER 0x5
  33. #define MM_TYPE_INTRA 0x8
  34. #define MM_TYPE_INTRA_HH 0xc
  35. #define MM_TYPE_INTER_HH 0xd
  36. #define MM_TYPE_INTRA_HHV 0xe
  37. #define MM_TYPE_INTER_HHV 0xf
  38. typedef struct MmContext {
  39. AVCodecContext *avctx;
  40. AVFrame frame;
  41. } MmContext;
  42. static int mm_decode_init(AVCodecContext *avctx)
  43. {
  44. MmContext *s = avctx->priv_data;
  45. s->avctx = avctx;
  46. if (s->avctx->palctrl == NULL) {
  47. av_log(avctx, AV_LOG_ERROR, "mmvideo: palette expected.\n");
  48. return -1;
  49. }
  50. avctx->pix_fmt = PIX_FMT_PAL8;
  51. avctx->has_b_frames = 0;
  52. if (avcodec_check_dimensions(avctx, avctx->width, avctx->height))
  53. return -1;
  54. s->frame.reference = 1;
  55. if (avctx->get_buffer(avctx, &s->frame)) {
  56. av_log(s->avctx, AV_LOG_ERROR, "mmvideo: get_buffer() failed\n");
  57. return -1;
  58. }
  59. return 0;
  60. }
  61. static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  62. {
  63. int i, x, y;
  64. i=0; x=0; y=0;
  65. while(i<buf_size) {
  66. int run_length, color;
  67. if (buf[i] & 0x80) {
  68. run_length = 1;
  69. color = buf[i];
  70. i++;
  71. }else{
  72. run_length = (buf[i] & 0x7f) + 2;
  73. color = buf[i+1];
  74. i+=2;
  75. }
  76. if (half_horiz)
  77. run_length *=2;
  78. if (color) {
  79. memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
  80. if (half_vert)
  81. memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
  82. }
  83. x+= run_length;
  84. if (x >= s->avctx->width) {
  85. x=0;
  86. y += half_vert ? 2 : 1;
  87. }
  88. }
  89. }
  90. static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  91. {
  92. const int data_ptr = 2 + LE_16(&buf[0]);
  93. int d, r, y;
  94. d = data_ptr; r = 2; y = 0;
  95. while(r < data_ptr) {
  96. int i, j;
  97. int length = buf[r] & 0x7f;
  98. int x = buf[r+1] + ((buf[r] & 0x80) << 1);
  99. r += 2;
  100. if (length==0) {
  101. y += x;
  102. continue;
  103. }
  104. for(i=0; i<length; i++) {
  105. for(j=0; j<8; j++) {
  106. int replace = (buf[r+i] >> (7-j)) & 1;
  107. if (replace) {
  108. int color = buf[d];
  109. s->frame.data[0][y*s->frame.linesize[0] + x] = color;
  110. if (half_horiz)
  111. s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
  112. if (half_vert) {
  113. s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
  114. if (half_horiz)
  115. s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
  116. }
  117. d++;
  118. }
  119. x += half_horiz ? 2 : 1;
  120. }
  121. }
  122. r += length;
  123. y += half_vert ? 2 : 1;
  124. }
  125. }
  126. static int mm_decode_frame(AVCodecContext *avctx,
  127. void *data, int *data_size,
  128. uint8_t *buf, int buf_size)
  129. {
  130. MmContext *s = avctx->priv_data;
  131. AVPaletteControl *palette_control = avctx->palctrl;
  132. int type;
  133. if (palette_control->palette_changed) {
  134. memcpy(s->frame.data[1], palette_control->palette, AVPALETTE_SIZE);
  135. palette_control->palette_changed = 0;
  136. }
  137. type = LE_16(&buf[0]);
  138. buf += MM_PREAMBLE_SIZE;
  139. buf_size -= MM_PREAMBLE_SIZE;
  140. switch(type) {
  141. case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
  142. case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
  143. case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
  144. case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
  145. case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
  146. case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
  147. default :
  148. return -1;
  149. }
  150. *data_size = sizeof(AVFrame);
  151. *(AVFrame*)data = s->frame;
  152. return buf_size;
  153. }
  154. static int mm_decode_end(AVCodecContext *avctx)
  155. {
  156. MmContext *s = avctx->priv_data;
  157. if(s->frame.data[0])
  158. avctx->release_buffer(avctx, &s->frame);
  159. return 0;
  160. }
  161. AVCodec mmvideo_decoder = {
  162. "mmvideo",
  163. CODEC_TYPE_VIDEO,
  164. CODEC_ID_MMVIDEO,
  165. sizeof(MmContext),
  166. mm_decode_init,
  167. NULL,
  168. mm_decode_end,
  169. mm_decode_frame,
  170. CODEC_CAP_DR1,
  171. };