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.

228 lines
6.1KB

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