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.

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