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.

239 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 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. #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. s->frame.reference = 1;
  55. return 0;
  56. }
  57. static int mm_decode_pal(MmContext *s)
  58. {
  59. int i;
  60. bytestream2_skip(&s->gb, 4);
  61. for (i = 0; i < 128; i++) {
  62. s->palette[i] = bytestream2_get_be24(&s->gb);
  63. s->palette[i+128] = s->palette[i]<<2;
  64. }
  65. return 0;
  66. }
  67. /**
  68. * @param half_horiz Half horizontal resolution (0 or 1)
  69. * @param half_vert Half vertical resolution (0 or 1)
  70. */
  71. static int mm_decode_intra(MmContext * s, int half_horiz, int half_vert)
  72. {
  73. int x = 0, y = 0;
  74. while (bytestream2_get_bytes_left(&s->gb) > 0) {
  75. int run_length, color;
  76. if (y >= s->avctx->height)
  77. return 0;
  78. color = bytestream2_get_byte(&s->gb);
  79. if (color & 0x80) {
  80. run_length = 1;
  81. }else{
  82. run_length = (color & 0x7f) + 2;
  83. color = bytestream2_get_byte(&s->gb);
  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 += 1 + half_vert;
  96. }
  97. }
  98. return 0;
  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 int mm_decode_inter(MmContext * s, int half_horiz, int half_vert)
  105. {
  106. int data_off = bytestream2_get_le16(&s->gb), y;
  107. GetByteContext data_ptr;
  108. if (bytestream2_get_bytes_left(&s->gb) < data_off)
  109. return AVERROR_INVALIDDATA;
  110. bytestream2_init(&data_ptr, s->gb.buffer + data_off, bytestream2_get_bytes_left(&s->gb) - data_off);
  111. while (s->gb.buffer < data_ptr.buffer_start) {
  112. int i, j;
  113. int length = bytestream2_get_byte(&s->gb);
  114. int x = bytestream2_get_byte(&s->gb) + ((length & 0x80) << 1);
  115. length &= 0x7F;
  116. if (length==0) {
  117. y += x;
  118. continue;
  119. }
  120. if (y + half_vert >= s->avctx->height)
  121. return 0;
  122. for(i=0; i<length; i++) {
  123. int replace_array = bytestream2_get_byte(&s->gb);
  124. for(j=0; j<8; j++) {
  125. int replace = (replace_array >> (7-j)) & 1;
  126. if (replace) {
  127. int color = bytestream2_get_byte(&data_ptr);
  128. s->frame.data[0][y*s->frame.linesize[0] + x] = color;
  129. if (half_horiz)
  130. s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
  131. if (half_vert) {
  132. s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
  133. if (half_horiz)
  134. s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
  135. }
  136. }
  137. x += 1 + half_horiz;
  138. }
  139. }
  140. y += 1 + half_vert;
  141. }
  142. return 0;
  143. }
  144. static int mm_decode_frame(AVCodecContext *avctx,
  145. void *data, int *data_size,
  146. AVPacket *avpkt)
  147. {
  148. const uint8_t *buf = avpkt->data;
  149. int buf_size = avpkt->size;
  150. MmContext *s = avctx->priv_data;
  151. int type, res;
  152. if (buf_size < MM_PREAMBLE_SIZE)
  153. return AVERROR_INVALIDDATA;
  154. type = AV_RL16(&buf[0]);
  155. buf += MM_PREAMBLE_SIZE;
  156. buf_size -= MM_PREAMBLE_SIZE;
  157. bytestream2_init(&s->gb, buf, buf_size);
  158. if (avctx->reget_buffer(avctx, &s->frame) < 0) {
  159. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  160. return -1;
  161. }
  162. switch(type) {
  163. case MM_TYPE_PALETTE : res = mm_decode_pal(s); return buf_size;
  164. case MM_TYPE_INTRA : res = mm_decode_intra(s, 0, 0); break;
  165. case MM_TYPE_INTRA_HH : res = mm_decode_intra(s, 1, 0); break;
  166. case MM_TYPE_INTRA_HHV : res = mm_decode_intra(s, 1, 1); break;
  167. case MM_TYPE_INTER : res = mm_decode_inter(s, 0, 0); break;
  168. case MM_TYPE_INTER_HH : res = mm_decode_inter(s, 1, 0); break;
  169. case MM_TYPE_INTER_HHV : res = mm_decode_inter(s, 1, 1); break;
  170. default:
  171. res = AVERROR_INVALIDDATA;
  172. break;
  173. }
  174. if (res < 0)
  175. return res;
  176. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  177. *data_size = sizeof(AVFrame);
  178. *(AVFrame*)data = s->frame;
  179. return buf_size;
  180. }
  181. static av_cold int mm_decode_end(AVCodecContext *avctx)
  182. {
  183. MmContext *s = avctx->priv_data;
  184. if(s->frame.data[0])
  185. avctx->release_buffer(avctx, &s->frame);
  186. return 0;
  187. }
  188. AVCodec ff_mmvideo_decoder = {
  189. .name = "mmvideo",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .id = CODEC_ID_MMVIDEO,
  192. .priv_data_size = sizeof(MmContext),
  193. .init = mm_decode_init,
  194. .close = mm_decode_end,
  195. .decode = mm_decode_frame,
  196. .capabilities = CODEC_CAP_DR1,
  197. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
  198. };