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.

348 lines
11KB

  1. /*
  2. * Microsoft Video-1 Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  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. /**
  23. * @file msvideo1.c
  24. * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
  25. * For more information about the MS Video-1 format, visit:
  26. * http://www.pcisys.net/~melanson/codecs/
  27. *
  28. * This decoder outputs either PAL8 or RGB555 data, depending on the
  29. * whether a RGB palette was passed through palctrl;
  30. * if it's present, then the data is PAL8; RGB555 otherwise.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <unistd.h>
  36. #include "avcodec.h"
  37. #include "dsputil.h"
  38. #define PALETTE_COUNT 256
  39. #define CHECK_STREAM_PTR(n) \
  40. if ((stream_ptr + n) > s->size ) { \
  41. av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
  42. stream_ptr + n, s->size); \
  43. return; \
  44. }
  45. typedef struct Msvideo1Context {
  46. AVCodecContext *avctx;
  47. DSPContext dsp;
  48. AVFrame frame;
  49. unsigned char *buf;
  50. int size;
  51. int mode_8bit; /* if it's not 8-bit, it's 16-bit */
  52. } Msvideo1Context;
  53. static int msvideo1_decode_init(AVCodecContext *avctx)
  54. {
  55. Msvideo1Context *s = avctx->priv_data;
  56. s->avctx = avctx;
  57. /* figure out the colorspace based on the presence of a palette */
  58. if (s->avctx->palctrl) {
  59. s->mode_8bit = 1;
  60. avctx->pix_fmt = PIX_FMT_PAL8;
  61. } else {
  62. s->mode_8bit = 0;
  63. avctx->pix_fmt = PIX_FMT_RGB555;
  64. }
  65. dsputil_init(&s->dsp, avctx);
  66. s->frame.data[0] = NULL;
  67. return 0;
  68. }
  69. static void msvideo1_decode_8bit(Msvideo1Context *s)
  70. {
  71. int block_ptr, pixel_ptr;
  72. int total_blocks;
  73. int pixel_x, pixel_y; /* pixel width and height iterators */
  74. int block_x, block_y; /* block width and height iterators */
  75. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  76. int block_inc;
  77. int row_dec;
  78. /* decoding parameters */
  79. int stream_ptr;
  80. unsigned char byte_a, byte_b;
  81. unsigned short flags;
  82. int skip_blocks;
  83. unsigned char colors[8];
  84. unsigned char *pixels = s->frame.data[0];
  85. int stride = s->frame.linesize[0];
  86. stream_ptr = 0;
  87. skip_blocks = 0;
  88. blocks_wide = s->avctx->width / 4;
  89. blocks_high = s->avctx->height / 4;
  90. total_blocks = blocks_wide * blocks_high;
  91. block_inc = 4;
  92. row_dec = stride + 4;
  93. for (block_y = blocks_high; block_y > 0; block_y--) {
  94. block_ptr = ((block_y * 4) - 1) * stride;
  95. for (block_x = blocks_wide; block_x > 0; block_x--) {
  96. /* check if this block should be skipped */
  97. if (skip_blocks) {
  98. block_ptr += block_inc;
  99. skip_blocks--;
  100. total_blocks--;
  101. continue;
  102. }
  103. pixel_ptr = block_ptr;
  104. /* get the next two bytes in the encoded data stream */
  105. CHECK_STREAM_PTR(2);
  106. byte_a = s->buf[stream_ptr++];
  107. byte_b = s->buf[stream_ptr++];
  108. /* check if the decode is finished */
  109. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  110. return;
  111. else if ((byte_b & 0xFC) == 0x84) {
  112. /* skip code, but don't count the current block */
  113. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  114. } else if (byte_b < 0x80) {
  115. /* 2-color encoding */
  116. flags = (byte_b << 8) | byte_a;
  117. CHECK_STREAM_PTR(2);
  118. colors[0] = s->buf[stream_ptr++];
  119. colors[1] = s->buf[stream_ptr++];
  120. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  121. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  122. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  123. pixel_ptr -= row_dec;
  124. }
  125. } else if (byte_b >= 0x90) {
  126. /* 8-color encoding */
  127. flags = (byte_b << 8) | byte_a;
  128. CHECK_STREAM_PTR(8);
  129. memcpy(colors, &s->buf[stream_ptr], 8);
  130. stream_ptr += 8;
  131. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  132. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  133. pixels[pixel_ptr++] =
  134. colors[((pixel_y & 0x2) << 1) +
  135. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  136. pixel_ptr -= row_dec;
  137. }
  138. } else {
  139. /* 1-color encoding */
  140. colors[0] = byte_a;
  141. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  142. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  143. pixels[pixel_ptr++] = colors[0];
  144. pixel_ptr -= row_dec;
  145. }
  146. }
  147. block_ptr += block_inc;
  148. total_blocks--;
  149. }
  150. }
  151. /* make the palette available on the way out */
  152. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  153. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  154. if (s->avctx->palctrl->palette_changed) {
  155. s->frame.palette_has_changed = 1;
  156. s->avctx->palctrl->palette_changed = 0;
  157. }
  158. }
  159. }
  160. static void msvideo1_decode_16bit(Msvideo1Context *s)
  161. {
  162. int block_ptr, pixel_ptr;
  163. int total_blocks;
  164. int pixel_x, pixel_y; /* pixel width and height iterators */
  165. int block_x, block_y; /* block width and height iterators */
  166. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  167. int block_inc;
  168. int row_dec;
  169. /* decoding parameters */
  170. int stream_ptr;
  171. unsigned char byte_a, byte_b;
  172. unsigned short flags;
  173. int skip_blocks;
  174. unsigned short colors[8];
  175. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  176. int stride = s->frame.linesize[0] / 2;
  177. stream_ptr = 0;
  178. skip_blocks = 0;
  179. blocks_wide = s->avctx->width / 4;
  180. blocks_high = s->avctx->height / 4;
  181. total_blocks = blocks_wide * blocks_high;
  182. block_inc = 4;
  183. row_dec = stride + 4;
  184. for (block_y = blocks_high; block_y > 0; block_y--) {
  185. block_ptr = ((block_y * 4) - 1) * stride;
  186. for (block_x = blocks_wide; block_x > 0; block_x--) {
  187. /* check if this block should be skipped */
  188. if (skip_blocks) {
  189. block_ptr += block_inc;
  190. skip_blocks--;
  191. total_blocks--;
  192. continue;
  193. }
  194. pixel_ptr = block_ptr;
  195. /* get the next two bytes in the encoded data stream */
  196. CHECK_STREAM_PTR(2);
  197. byte_a = s->buf[stream_ptr++];
  198. byte_b = s->buf[stream_ptr++];
  199. /* check if the decode is finished */
  200. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  201. return;
  202. } else if ((byte_b & 0xFC) == 0x84) {
  203. /* skip code, but don't count the current block */
  204. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  205. } else if (byte_b < 0x80) {
  206. /* 2- or 8-color encoding modes */
  207. flags = (byte_b << 8) | byte_a;
  208. CHECK_STREAM_PTR(4);
  209. colors[0] = AV_RL16(&s->buf[stream_ptr]);
  210. stream_ptr += 2;
  211. colors[1] = AV_RL16(&s->buf[stream_ptr]);
  212. stream_ptr += 2;
  213. if (colors[0] & 0x8000) {
  214. /* 8-color encoding */
  215. CHECK_STREAM_PTR(12);
  216. colors[2] = AV_RL16(&s->buf[stream_ptr]);
  217. stream_ptr += 2;
  218. colors[3] = AV_RL16(&s->buf[stream_ptr]);
  219. stream_ptr += 2;
  220. colors[4] = AV_RL16(&s->buf[stream_ptr]);
  221. stream_ptr += 2;
  222. colors[5] = AV_RL16(&s->buf[stream_ptr]);
  223. stream_ptr += 2;
  224. colors[6] = AV_RL16(&s->buf[stream_ptr]);
  225. stream_ptr += 2;
  226. colors[7] = AV_RL16(&s->buf[stream_ptr]);
  227. stream_ptr += 2;
  228. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  229. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  230. pixels[pixel_ptr++] =
  231. colors[((pixel_y & 0x2) << 1) +
  232. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  233. pixel_ptr -= row_dec;
  234. }
  235. } else {
  236. /* 2-color encoding */
  237. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  238. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  239. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  240. pixel_ptr -= row_dec;
  241. }
  242. }
  243. } else {
  244. /* otherwise, it's a 1-color block */
  245. colors[0] = (byte_b << 8) | byte_a;
  246. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  247. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  248. pixels[pixel_ptr++] = colors[0];
  249. pixel_ptr -= row_dec;
  250. }
  251. }
  252. block_ptr += block_inc;
  253. total_blocks--;
  254. }
  255. }
  256. }
  257. static int msvideo1_decode_frame(AVCodecContext *avctx,
  258. void *data, int *data_size,
  259. uint8_t *buf, int buf_size)
  260. {
  261. Msvideo1Context *s = avctx->priv_data;
  262. s->buf = buf;
  263. s->size = buf_size;
  264. s->frame.reference = 1;
  265. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  266. if (avctx->reget_buffer(avctx, &s->frame)) {
  267. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  268. return -1;
  269. }
  270. if (s->mode_8bit)
  271. msvideo1_decode_8bit(s);
  272. else
  273. msvideo1_decode_16bit(s);
  274. *data_size = sizeof(AVFrame);
  275. *(AVFrame*)data = s->frame;
  276. /* report that the buffer was completely consumed */
  277. return buf_size;
  278. }
  279. static int msvideo1_decode_end(AVCodecContext *avctx)
  280. {
  281. Msvideo1Context *s = avctx->priv_data;
  282. if (s->frame.data[0])
  283. avctx->release_buffer(avctx, &s->frame);
  284. return 0;
  285. }
  286. AVCodec msvideo1_decoder = {
  287. "msvideo1",
  288. CODEC_TYPE_VIDEO,
  289. CODEC_ID_MSVIDEO1,
  290. sizeof(Msvideo1Context),
  291. msvideo1_decode_init,
  292. NULL,
  293. msvideo1_decode_end,
  294. msvideo1_decode_frame,
  295. CODEC_CAP_DR1,
  296. };