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