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.

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