mmvideo.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 libavcodec/mmvideo.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 "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. 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_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
  62. {
  63. int i;
  64. buf += 4;
  65. for (i=0; i<128 && buf+2<buf_end; i++) {
  66. s->palette[i] = AV_RB24(buf);
  67. s->palette[i+128] = s->palette[i]<<2;
  68. buf += 3;
  69. }
  70. }
  71. static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  72. {
  73. int i, x, y;
  74. i=0; x=0; y=0;
  75. while(i<buf_size) {
  76. int run_length, color;
  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 && y + half_vert < s->avctx->height)
  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 += half_vert ? 2 : 1;
  97. }
  98. }
  99. }
  100. static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
  101. {
  102. const int data_ptr = 2 + AV_RL16(&buf[0]);
  103. int d, r, y;
  104. d = data_ptr; r = 2; y = 0;
  105. while(r < data_ptr) {
  106. int i, j;
  107. int length = buf[r] & 0x7f;
  108. int x = buf[r+1] + ((buf[r] & 0x80) << 1);
  109. r += 2;
  110. if (length==0) {
  111. y += x;
  112. continue;
  113. }
  114. for(i=0; i<length; i++) {
  115. for(j=0; j<8; j++) {
  116. int replace = (buf[r+i] >> (7-j)) & 1;
  117. if (replace) {
  118. int color = buf[d];
  119. s->frame.data[0][y*s->frame.linesize[0] + x] = color;
  120. if (half_horiz)
  121. s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
  122. if (half_vert) {
  123. s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
  124. if (half_horiz)
  125. s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
  126. }
  127. d++;
  128. }
  129. x += half_horiz ? 2 : 1;
  130. }
  131. }
  132. r += length;
  133. y += half_vert ? 2 : 1;
  134. }
  135. }
  136. static int mm_decode_frame(AVCodecContext *avctx,
  137. void *data, int *data_size,
  138. const uint8_t *buf, int buf_size)
  139. {
  140. MmContext *s = avctx->priv_data;
  141. const uint8_t *buf_end = buf+buf_size;
  142. int type;
  143. type = AV_RL16(&buf[0]);
  144. buf += MM_PREAMBLE_SIZE;
  145. buf_size -= MM_PREAMBLE_SIZE;
  146. switch(type) {
  147. case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
  148. case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
  149. case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
  150. case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
  151. case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
  152. case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
  153. case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
  154. default :
  155. return -1;
  156. }
  157. memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
  158. *data_size = sizeof(AVFrame);
  159. *(AVFrame*)data = s->frame;
  160. return buf_size;
  161. }
  162. static av_cold int mm_decode_end(AVCodecContext *avctx)
  163. {
  164. MmContext *s = avctx->priv_data;
  165. if(s->frame.data[0])
  166. avctx->release_buffer(avctx, &s->frame);
  167. return 0;
  168. }
  169. AVCodec mmvideo_decoder = {
  170. "mmvideo",
  171. CODEC_TYPE_VIDEO,
  172. CODEC_ID_MMVIDEO,
  173. sizeof(MmContext),
  174. mm_decode_init,
  175. NULL,
  176. mm_decode_end,
  177. mm_decode_frame,
  178. CODEC_CAP_DR1,
  179. .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
  180. };