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.

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