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.

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