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.

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