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.

373 lines
12KB

  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. #define COPY_PREV_BLOCK() \
  46. pixel_ptr = block_ptr; \
  47. for (pixel_y = 0; pixel_y < 4; pixel_y++) { \
  48. for (pixel_x = 0; pixel_x < 4; pixel_x++, pixel_ptr++) \
  49. pixels[pixel_ptr] = prev_pixels[pixel_ptr]; \
  50. pixel_ptr -= row_dec; \
  51. }
  52. typedef struct Msvideo1Context {
  53. AVCodecContext *avctx;
  54. DSPContext dsp;
  55. AVFrame frame;
  56. AVFrame prev_frame;
  57. unsigned char *buf;
  58. int size;
  59. int mode_8bit; /* if it's not 8-bit, it's 16-bit */
  60. } Msvideo1Context;
  61. static int msvideo1_decode_init(AVCodecContext *avctx)
  62. {
  63. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  64. s->avctx = avctx;
  65. /* figure out the colorspace based on the presence of a palette */
  66. if (s->avctx->palctrl) {
  67. s->mode_8bit = 1;
  68. avctx->pix_fmt = PIX_FMT_PAL8;
  69. } else {
  70. s->mode_8bit = 0;
  71. avctx->pix_fmt = PIX_FMT_RGB555;
  72. }
  73. avctx->has_b_frames = 0;
  74. dsputil_init(&s->dsp, avctx);
  75. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  76. return 0;
  77. }
  78. static void msvideo1_decode_8bit(Msvideo1Context *s)
  79. {
  80. int block_ptr, pixel_ptr;
  81. int total_blocks;
  82. int pixel_x, pixel_y; /* pixel width and height iterators */
  83. int block_x, block_y; /* block width and height iterators */
  84. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  85. int block_inc;
  86. int row_dec;
  87. /* decoding parameters */
  88. int stream_ptr;
  89. unsigned char byte_a, byte_b;
  90. unsigned short flags;
  91. int skip_blocks;
  92. unsigned char colors[8];
  93. unsigned char *pixels = s->frame.data[0];
  94. unsigned char *prev_pixels = s->prev_frame.data[0];
  95. int stride = s->frame.linesize[0];
  96. stream_ptr = 0;
  97. skip_blocks = 0;
  98. blocks_wide = s->avctx->width / 4;
  99. blocks_high = s->avctx->height / 4;
  100. total_blocks = blocks_wide * blocks_high;
  101. block_inc = 4;
  102. row_dec = stride + 4;
  103. for (block_y = blocks_high; block_y > 0; block_y--) {
  104. block_ptr = ((block_y * 4) - 1) * stride;
  105. for (block_x = blocks_wide; block_x > 0; block_x--) {
  106. /* check if this block should be skipped */
  107. if (skip_blocks) {
  108. COPY_PREV_BLOCK();
  109. block_ptr += block_inc;
  110. skip_blocks--;
  111. total_blocks--;
  112. continue;
  113. }
  114. pixel_ptr = block_ptr;
  115. /* get the next two bytes in the encoded data stream */
  116. CHECK_STREAM_PTR(2);
  117. byte_a = s->buf[stream_ptr++];
  118. byte_b = s->buf[stream_ptr++];
  119. /* check if the decode is finished */
  120. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  121. return;
  122. else if ((byte_b & 0xFC) == 0x84) {
  123. /* skip code, but don't count the current block */
  124. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  125. COPY_PREV_BLOCK();
  126. } else if (byte_b < 0x80) {
  127. /* 2-color encoding */
  128. flags = (byte_b << 8) | byte_a;
  129. CHECK_STREAM_PTR(2);
  130. colors[0] = s->buf[stream_ptr++];
  131. colors[1] = s->buf[stream_ptr++];
  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++] = colors[(flags & 0x1) ^ 1];
  135. pixel_ptr -= row_dec;
  136. }
  137. } else if (byte_b >= 0x90) {
  138. /* 8-color encoding */
  139. flags = (byte_b << 8) | byte_a;
  140. CHECK_STREAM_PTR(8);
  141. memcpy(colors, &s->buf[stream_ptr], 8);
  142. stream_ptr += 8;
  143. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  144. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  145. pixels[pixel_ptr++] =
  146. colors[((pixel_y & 0x2) << 1) +
  147. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  148. pixel_ptr -= row_dec;
  149. }
  150. } else {
  151. /* 1-color encoding */
  152. colors[0] = byte_a;
  153. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  154. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  155. pixels[pixel_ptr++] = colors[0];
  156. pixel_ptr -= row_dec;
  157. }
  158. }
  159. block_ptr += block_inc;
  160. total_blocks--;
  161. }
  162. }
  163. /* make the palette available on the way out */
  164. if (s->avctx->pix_fmt == PIX_FMT_PAL8) {
  165. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  166. if (s->avctx->palctrl->palette_changed) {
  167. s->frame.palette_has_changed = 1;
  168. s->avctx->palctrl->palette_changed = 0;
  169. }
  170. }
  171. }
  172. static void msvideo1_decode_16bit(Msvideo1Context *s)
  173. {
  174. int block_ptr, pixel_ptr;
  175. int total_blocks;
  176. int pixel_x, pixel_y; /* pixel width and height iterators */
  177. int block_x, block_y; /* block width and height iterators */
  178. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  179. int block_inc;
  180. int row_dec;
  181. /* decoding parameters */
  182. int stream_ptr;
  183. unsigned char byte_a, byte_b;
  184. unsigned short flags;
  185. int skip_blocks;
  186. unsigned short colors[8];
  187. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  188. unsigned short *prev_pixels = (unsigned short *)s->prev_frame.data[0];
  189. int stride = s->frame.linesize[0] / 2;
  190. stream_ptr = 0;
  191. skip_blocks = 0;
  192. blocks_wide = s->avctx->width / 4;
  193. blocks_high = s->avctx->height / 4;
  194. total_blocks = blocks_wide * blocks_high;
  195. block_inc = 4;
  196. row_dec = stride + 4;
  197. for (block_y = blocks_high; block_y > 0; block_y--) {
  198. block_ptr = ((block_y * 4) - 1) * stride;
  199. for (block_x = blocks_wide; block_x > 0; block_x--) {
  200. /* check if this block should be skipped */
  201. if (skip_blocks) {
  202. COPY_PREV_BLOCK();
  203. block_ptr += block_inc;
  204. skip_blocks--;
  205. total_blocks--;
  206. continue;
  207. }
  208. pixel_ptr = block_ptr;
  209. /* get the next two bytes in the encoded data stream */
  210. CHECK_STREAM_PTR(2);
  211. byte_a = s->buf[stream_ptr++];
  212. byte_b = s->buf[stream_ptr++];
  213. /* check if the decode is finished */
  214. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  215. return;
  216. } else if ((byte_b & 0xFC) == 0x84) {
  217. /* skip code, but don't count the current block */
  218. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  219. COPY_PREV_BLOCK();
  220. } else if (byte_b < 0x80) {
  221. /* 2- or 8-color encoding modes */
  222. flags = (byte_b << 8) | byte_a;
  223. CHECK_STREAM_PTR(4);
  224. colors[0] = LE_16(&s->buf[stream_ptr]);
  225. stream_ptr += 2;
  226. colors[1] = LE_16(&s->buf[stream_ptr]);
  227. stream_ptr += 2;
  228. if (colors[0] & 0x8000) {
  229. /* 8-color encoding */
  230. CHECK_STREAM_PTR(12);
  231. colors[2] = LE_16(&s->buf[stream_ptr]);
  232. stream_ptr += 2;
  233. colors[3] = LE_16(&s->buf[stream_ptr]);
  234. stream_ptr += 2;
  235. colors[4] = LE_16(&s->buf[stream_ptr]);
  236. stream_ptr += 2;
  237. colors[5] = LE_16(&s->buf[stream_ptr]);
  238. stream_ptr += 2;
  239. colors[6] = LE_16(&s->buf[stream_ptr]);
  240. stream_ptr += 2;
  241. colors[7] = LE_16(&s->buf[stream_ptr]);
  242. stream_ptr += 2;
  243. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  244. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  245. pixels[pixel_ptr++] =
  246. colors[((pixel_y & 0x2) << 1) +
  247. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  248. pixel_ptr -= row_dec;
  249. }
  250. } else {
  251. /* 2-color encoding */
  252. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  253. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  254. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  255. pixel_ptr -= row_dec;
  256. }
  257. }
  258. } else {
  259. /* otherwise, it's a 1-color block */
  260. colors[0] = (byte_b << 8) | byte_a;
  261. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  262. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  263. pixels[pixel_ptr++] = colors[0];
  264. pixel_ptr -= row_dec;
  265. }
  266. }
  267. block_ptr += block_inc;
  268. total_blocks--;
  269. }
  270. }
  271. }
  272. static int msvideo1_decode_frame(AVCodecContext *avctx,
  273. void *data, int *data_size,
  274. uint8_t *buf, int buf_size)
  275. {
  276. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  277. s->buf = buf;
  278. s->size = buf_size;
  279. if (avctx->get_buffer(avctx, &s->frame)) {
  280. av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 Video: get_buffer() failed\n");
  281. return -1;
  282. }
  283. if (s->prev_frame.data[0] &&(s->frame.linesize[0] != s->prev_frame.linesize[0]))
  284. av_log(avctx, AV_LOG_ERROR, " MS Video-1: Buffer linesize changed: current %u, previous %u.\n"
  285. " Expect wrong image and/or crash!\n",
  286. s->frame.linesize[0], s->prev_frame.linesize[0]);
  287. if (s->mode_8bit)
  288. msvideo1_decode_8bit(s);
  289. else
  290. msvideo1_decode_16bit(s);
  291. if (s->prev_frame.data[0])
  292. avctx->release_buffer(avctx, &s->prev_frame);
  293. /* shuffle frames */
  294. s->prev_frame = s->frame;
  295. *data_size = sizeof(AVFrame);
  296. *(AVFrame*)data = s->frame;
  297. /* report that the buffer was completely consumed */
  298. return buf_size;
  299. }
  300. static int msvideo1_decode_end(AVCodecContext *avctx)
  301. {
  302. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  303. if (s->prev_frame.data[0])
  304. avctx->release_buffer(avctx, &s->prev_frame);
  305. return 0;
  306. }
  307. AVCodec msvideo1_decoder = {
  308. "msvideo1",
  309. CODEC_TYPE_VIDEO,
  310. CODEC_ID_MSVIDEO1,
  311. sizeof(Msvideo1Context),
  312. msvideo1_decode_init,
  313. NULL,
  314. msvideo1_decode_end,
  315. msvideo1_decode_frame,
  316. CODEC_CAP_DR1,
  317. };