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.

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