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.

349 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/internal.h"
  32. #include "libavutil/intreadwrite.h"
  33. #include "avcodec.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 = PIX_FMT_PAL8;
  57. } else {
  58. s->mode_8bit = 0;
  59. avctx->pix_fmt = PIX_FMT_RGB555;
  60. }
  61. avcodec_get_frame_defaults(&s->frame);
  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->pal, AVPALETTE_SIZE);
  150. }
  151. static void msvideo1_decode_16bit(Msvideo1Context *s)
  152. {
  153. int block_ptr, pixel_ptr;
  154. int total_blocks;
  155. int pixel_x, pixel_y; /* pixel width and height iterators */
  156. int block_x, block_y; /* block width and height iterators */
  157. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  158. int block_inc;
  159. int row_dec;
  160. /* decoding parameters */
  161. int stream_ptr;
  162. unsigned char byte_a, byte_b;
  163. unsigned short flags;
  164. int skip_blocks;
  165. unsigned short colors[8];
  166. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  167. int stride = s->frame.linesize[0] / 2;
  168. stream_ptr = 0;
  169. skip_blocks = 0;
  170. blocks_wide = s->avctx->width / 4;
  171. blocks_high = s->avctx->height / 4;
  172. total_blocks = blocks_wide * blocks_high;
  173. block_inc = 4;
  174. row_dec = stride + 4;
  175. for (block_y = blocks_high; block_y > 0; block_y--) {
  176. block_ptr = ((block_y * 4) - 1) * stride;
  177. for (block_x = blocks_wide; block_x > 0; block_x--) {
  178. /* check if this block should be skipped */
  179. if (skip_blocks) {
  180. block_ptr += block_inc;
  181. skip_blocks--;
  182. total_blocks--;
  183. continue;
  184. }
  185. pixel_ptr = block_ptr;
  186. /* get the next two bytes in the encoded data stream */
  187. CHECK_STREAM_PTR(2);
  188. byte_a = s->buf[stream_ptr++];
  189. byte_b = s->buf[stream_ptr++];
  190. /* check if the decode is finished */
  191. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  192. return;
  193. } else if ((byte_b & 0xFC) == 0x84) {
  194. /* skip code, but don't count the current block */
  195. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  196. } else if (byte_b < 0x80) {
  197. /* 2- or 8-color encoding modes */
  198. flags = (byte_b << 8) | byte_a;
  199. CHECK_STREAM_PTR(4);
  200. colors[0] = AV_RL16(&s->buf[stream_ptr]);
  201. stream_ptr += 2;
  202. colors[1] = AV_RL16(&s->buf[stream_ptr]);
  203. stream_ptr += 2;
  204. if (colors[0] & 0x8000) {
  205. /* 8-color encoding */
  206. CHECK_STREAM_PTR(12);
  207. colors[2] = AV_RL16(&s->buf[stream_ptr]);
  208. stream_ptr += 2;
  209. colors[3] = AV_RL16(&s->buf[stream_ptr]);
  210. stream_ptr += 2;
  211. colors[4] = AV_RL16(&s->buf[stream_ptr]);
  212. stream_ptr += 2;
  213. colors[5] = AV_RL16(&s->buf[stream_ptr]);
  214. stream_ptr += 2;
  215. colors[6] = AV_RL16(&s->buf[stream_ptr]);
  216. stream_ptr += 2;
  217. colors[7] = AV_RL16(&s->buf[stream_ptr]);
  218. stream_ptr += 2;
  219. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  220. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  221. pixels[pixel_ptr++] =
  222. colors[((pixel_y & 0x2) << 1) +
  223. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  224. pixel_ptr -= row_dec;
  225. }
  226. } else {
  227. /* 2-color encoding */
  228. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  229. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  230. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  231. pixel_ptr -= row_dec;
  232. }
  233. }
  234. } else {
  235. /* otherwise, it's a 1-color block */
  236. colors[0] = (byte_b << 8) | byte_a;
  237. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  238. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  239. pixels[pixel_ptr++] = colors[0];
  240. pixel_ptr -= row_dec;
  241. }
  242. }
  243. block_ptr += block_inc;
  244. total_blocks--;
  245. }
  246. }
  247. }
  248. static int msvideo1_decode_frame(AVCodecContext *avctx,
  249. void *data, int *data_size,
  250. AVPacket *avpkt)
  251. {
  252. const uint8_t *buf = avpkt->data;
  253. int buf_size = avpkt->size;
  254. Msvideo1Context *s = avctx->priv_data;
  255. s->buf = buf;
  256. s->size = buf_size;
  257. s->frame.reference = 3;
  258. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  259. if (avctx->reget_buffer(avctx, &s->frame)) {
  260. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  261. return -1;
  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. *data_size = sizeof(AVFrame);
  275. *(AVFrame*)data = s->frame;
  276. /* report that the buffer was completely consumed */
  277. return buf_size;
  278. }
  279. static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
  280. {
  281. Msvideo1Context *s = avctx->priv_data;
  282. if (s->frame.data[0])
  283. avctx->release_buffer(avctx, &s->frame);
  284. return 0;
  285. }
  286. AVCodec ff_msvideo1_decoder = {
  287. .name = "msvideo1",
  288. .type = AVMEDIA_TYPE_VIDEO,
  289. .id = AV_CODEC_ID_MSVIDEO1,
  290. .priv_data_size = sizeof(Msvideo1Context),
  291. .init = msvideo1_decode_init,
  292. .close = msvideo1_decode_end,
  293. .decode = msvideo1_decode_frame,
  294. .capabilities = CODEC_CAP_DR1,
  295. .long_name = NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
  296. };