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.

353 lines
11KB

  1. /*
  2. * Microsoft Video-1 Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file msvideo1.c
  22. * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
  23. * For more information about the MS Video-1 format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * This decoder outputs either PAL8 or RGB555 data, depending on the
  27. * whether a RGB palette was passed through palctrl;
  28. * if it's present, then the data is PAL8; RGB555 otherwise.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "common.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #define PALETTE_COUNT 256
  38. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  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 = (Msvideo1Context *)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. avctx->has_b_frames = 0;
  66. dsputil_init(&s->dsp, avctx);
  67. s->frame.data[0] = NULL;
  68. return 0;
  69. }
  70. static void msvideo1_decode_8bit(Msvideo1Context *s)
  71. {
  72. int block_ptr, pixel_ptr;
  73. int total_blocks;
  74. int pixel_x, pixel_y; /* pixel width and height iterators */
  75. int block_x, block_y; /* block width and height iterators */
  76. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  77. int block_inc;
  78. int row_dec;
  79. /* decoding parameters */
  80. int stream_ptr;
  81. unsigned char byte_a, byte_b;
  82. unsigned short flags;
  83. int skip_blocks;
  84. unsigned char colors[8];
  85. unsigned char *pixels = s->frame.data[0];
  86. int stride = s->frame.linesize[0];
  87. stream_ptr = 0;
  88. skip_blocks = 0;
  89. blocks_wide = s->avctx->width / 4;
  90. blocks_high = s->avctx->height / 4;
  91. total_blocks = blocks_wide * blocks_high;
  92. block_inc = 4;
  93. row_dec = stride + 4;
  94. for (block_y = blocks_high; block_y > 0; block_y--) {
  95. block_ptr = ((block_y * 4) - 1) * stride;
  96. for (block_x = blocks_wide; block_x > 0; block_x--) {
  97. /* check if this block should be skipped */
  98. if (skip_blocks) {
  99. block_ptr += block_inc;
  100. skip_blocks--;
  101. total_blocks--;
  102. continue;
  103. }
  104. pixel_ptr = block_ptr;
  105. /* get the next two bytes in the encoded data stream */
  106. CHECK_STREAM_PTR(2);
  107. byte_a = s->buf[stream_ptr++];
  108. byte_b = s->buf[stream_ptr++];
  109. /* check if the decode is finished */
  110. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  111. return;
  112. else if ((byte_b & 0xFC) == 0x84) {
  113. /* skip code, but don't count the current block */
  114. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  115. } else if (byte_b < 0x80) {
  116. /* 2-color encoding */
  117. flags = (byte_b << 8) | byte_a;
  118. CHECK_STREAM_PTR(2);
  119. colors[0] = s->buf[stream_ptr++];
  120. colors[1] = s->buf[stream_ptr++];
  121. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  122. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  123. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  124. pixel_ptr -= row_dec;
  125. }
  126. } else if (byte_b >= 0x90) {
  127. /* 8-color encoding */
  128. flags = (byte_b << 8) | byte_a;
  129. CHECK_STREAM_PTR(8);
  130. memcpy(colors, &s->buf[stream_ptr], 8);
  131. stream_ptr += 8;
  132. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  133. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  134. pixels[pixel_ptr++] =
  135. colors[((pixel_y & 0x2) << 1) +
  136. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  137. pixel_ptr -= row_dec;
  138. }
  139. } else {
  140. /* 1-color encoding */
  141. colors[0] = byte_a;
  142. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  143. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  144. pixels[pixel_ptr++] = colors[0];
  145. pixel_ptr -= row_dec;
  146. }
  147. }
  148. block_ptr += block_inc;
  149. total_blocks--;
  150. }
  151. }
  152. /* make the palette available on the way out */
  153. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  154. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  155. if (s->avctx->palctrl->palette_changed) {
  156. s->frame.palette_has_changed = 1;
  157. s->avctx->palctrl->palette_changed = 0;
  158. }
  159. }
  160. }
  161. static void msvideo1_decode_16bit(Msvideo1Context *s)
  162. {
  163. int block_ptr, pixel_ptr;
  164. int total_blocks;
  165. int pixel_x, pixel_y; /* pixel width and height iterators */
  166. int block_x, block_y; /* block width and height iterators */
  167. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  168. int block_inc;
  169. int row_dec;
  170. /* decoding parameters */
  171. int stream_ptr;
  172. unsigned char byte_a, byte_b;
  173. unsigned short flags;
  174. int skip_blocks;
  175. unsigned short colors[8];
  176. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  177. int stride = s->frame.linesize[0] / 2;
  178. stream_ptr = 0;
  179. skip_blocks = 0;
  180. blocks_wide = s->avctx->width / 4;
  181. blocks_high = s->avctx->height / 4;
  182. total_blocks = blocks_wide * blocks_high;
  183. block_inc = 4;
  184. row_dec = stride + 4;
  185. for (block_y = blocks_high; block_y > 0; block_y--) {
  186. block_ptr = ((block_y * 4) - 1) * stride;
  187. for (block_x = blocks_wide; block_x > 0; block_x--) {
  188. /* check if this block should be skipped */
  189. if (skip_blocks) {
  190. block_ptr += block_inc;
  191. skip_blocks--;
  192. total_blocks--;
  193. continue;
  194. }
  195. pixel_ptr = block_ptr;
  196. /* get the next two bytes in the encoded data stream */
  197. CHECK_STREAM_PTR(2);
  198. byte_a = s->buf[stream_ptr++];
  199. byte_b = s->buf[stream_ptr++];
  200. /* check if the decode is finished */
  201. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  202. return;
  203. } else if ((byte_b & 0xFC) == 0x84) {
  204. /* skip code, but don't count the current block */
  205. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  206. } else if (byte_b < 0x80) {
  207. /* 2- or 8-color encoding modes */
  208. flags = (byte_b << 8) | byte_a;
  209. CHECK_STREAM_PTR(4);
  210. colors[0] = LE_16(&s->buf[stream_ptr]);
  211. stream_ptr += 2;
  212. colors[1] = LE_16(&s->buf[stream_ptr]);
  213. stream_ptr += 2;
  214. if (colors[0] & 0x8000) {
  215. /* 8-color encoding */
  216. CHECK_STREAM_PTR(12);
  217. colors[2] = LE_16(&s->buf[stream_ptr]);
  218. stream_ptr += 2;
  219. colors[3] = LE_16(&s->buf[stream_ptr]);
  220. stream_ptr += 2;
  221. colors[4] = LE_16(&s->buf[stream_ptr]);
  222. stream_ptr += 2;
  223. colors[5] = LE_16(&s->buf[stream_ptr]);
  224. stream_ptr += 2;
  225. colors[6] = LE_16(&s->buf[stream_ptr]);
  226. stream_ptr += 2;
  227. colors[7] = LE_16(&s->buf[stream_ptr]);
  228. stream_ptr += 2;
  229. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  230. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  231. pixels[pixel_ptr++] =
  232. colors[((pixel_y & 0x2) << 1) +
  233. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  234. pixel_ptr -= row_dec;
  235. }
  236. } else {
  237. /* 2-color encoding */
  238. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  239. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  240. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  241. pixel_ptr -= row_dec;
  242. }
  243. }
  244. } else {
  245. /* otherwise, it's a 1-color block */
  246. colors[0] = (byte_b << 8) | byte_a;
  247. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  248. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  249. pixels[pixel_ptr++] = colors[0];
  250. pixel_ptr -= row_dec;
  251. }
  252. }
  253. block_ptr += block_inc;
  254. total_blocks--;
  255. }
  256. }
  257. }
  258. static int msvideo1_decode_frame(AVCodecContext *avctx,
  259. void *data, int *data_size,
  260. uint8_t *buf, int buf_size)
  261. {
  262. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  263. /* no supplementary picture */
  264. if (buf_size == 0)
  265. return 0;
  266. s->buf = buf;
  267. s->size = buf_size;
  268. s->frame.reference = 1;
  269. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  270. if (avctx->reget_buffer(avctx, &s->frame)) {
  271. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  272. return -1;
  273. }
  274. if (s->mode_8bit)
  275. msvideo1_decode_8bit(s);
  276. else
  277. msvideo1_decode_16bit(s);
  278. *data_size = sizeof(AVFrame);
  279. *(AVFrame*)data = s->frame;
  280. /* report that the buffer was completely consumed */
  281. return buf_size;
  282. }
  283. static int msvideo1_decode_end(AVCodecContext *avctx)
  284. {
  285. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  286. if (s->frame.data[0])
  287. avctx->release_buffer(avctx, &s->frame);
  288. return 0;
  289. }
  290. AVCodec msvideo1_decoder = {
  291. "msvideo1",
  292. CODEC_TYPE_VIDEO,
  293. CODEC_ID_MSVIDEO1,
  294. sizeof(Msvideo1Context),
  295. msvideo1_decode_init,
  296. NULL,
  297. msvideo1_decode_end,
  298. msvideo1_decode_frame,
  299. CODEC_CAP_DR1,
  300. };