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.

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