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