msvideo1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 libavcodec/msvideo1.c
  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. * This decoder outputs either PAL8 or RGB555 data, depending on the
  28. * whether a RGB palette was passed through palctrl;
  29. * if it's present, then the data is PAL8; RGB555 otherwise.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include "libavutil/intreadwrite.h"
  36. #include "avcodec.h"
  37. #define PALETTE_COUNT 256
  38. #define CHECK_STREAM_PTR(n) \
  39. if ((stream_ptr + n) > s->size ) { \
  40. av_log(s->avctx, AV_LOG_ERROR, " MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
  41. stream_ptr + n, s->size); \
  42. return; \
  43. }
  44. typedef struct Msvideo1Context {
  45. AVCodecContext *avctx;
  46. AVFrame frame;
  47. const unsigned char *buf;
  48. int size;
  49. int mode_8bit; /* if it's not 8-bit, it's 16-bit */
  50. } Msvideo1Context;
  51. static av_cold int msvideo1_decode_init(AVCodecContext *avctx)
  52. {
  53. Msvideo1Context *s = avctx->priv_data;
  54. s->avctx = avctx;
  55. /* figure out the colorspace based on the presence of a palette */
  56. if (s->avctx->palctrl) {
  57. s->mode_8bit = 1;
  58. avctx->pix_fmt = PIX_FMT_PAL8;
  59. } else {
  60. s->mode_8bit = 0;
  61. avctx->pix_fmt = PIX_FMT_RGB555;
  62. }
  63. s->frame.data[0] = NULL;
  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 == PIX_FMT_PAL8) {
  150. memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
  151. if (s->avctx->palctrl->palette_changed) {
  152. s->frame.palette_has_changed = 1;
  153. s->avctx->palctrl->palette_changed = 0;
  154. }
  155. }
  156. }
  157. static void msvideo1_decode_16bit(Msvideo1Context *s)
  158. {
  159. int block_ptr, pixel_ptr;
  160. int total_blocks;
  161. int pixel_x, pixel_y; /* pixel width and height iterators */
  162. int block_x, block_y; /* block width and height iterators */
  163. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  164. int block_inc;
  165. int row_dec;
  166. /* decoding parameters */
  167. int stream_ptr;
  168. unsigned char byte_a, byte_b;
  169. unsigned short flags;
  170. int skip_blocks;
  171. unsigned short colors[8];
  172. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  173. int stride = s->frame.linesize[0] / 2;
  174. stream_ptr = 0;
  175. skip_blocks = 0;
  176. blocks_wide = s->avctx->width / 4;
  177. blocks_high = s->avctx->height / 4;
  178. total_blocks = blocks_wide * blocks_high;
  179. block_inc = 4;
  180. row_dec = stride + 4;
  181. for (block_y = blocks_high; block_y > 0; block_y--) {
  182. block_ptr = ((block_y * 4) - 1) * stride;
  183. for (block_x = blocks_wide; block_x > 0; block_x--) {
  184. /* check if this block should be skipped */
  185. if (skip_blocks) {
  186. block_ptr += block_inc;
  187. skip_blocks--;
  188. total_blocks--;
  189. continue;
  190. }
  191. pixel_ptr = block_ptr;
  192. /* get the next two bytes in the encoded data stream */
  193. CHECK_STREAM_PTR(2);
  194. byte_a = s->buf[stream_ptr++];
  195. byte_b = s->buf[stream_ptr++];
  196. /* check if the decode is finished */
  197. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  198. return;
  199. } else if ((byte_b & 0xFC) == 0x84) {
  200. /* skip code, but don't count the current block */
  201. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  202. } else if (byte_b < 0x80) {
  203. /* 2- or 8-color encoding modes */
  204. flags = (byte_b << 8) | byte_a;
  205. CHECK_STREAM_PTR(4);
  206. colors[0] = AV_RL16(&s->buf[stream_ptr]);
  207. stream_ptr += 2;
  208. colors[1] = AV_RL16(&s->buf[stream_ptr]);
  209. stream_ptr += 2;
  210. if (colors[0] & 0x8000) {
  211. /* 8-color encoding */
  212. CHECK_STREAM_PTR(12);
  213. colors[2] = AV_RL16(&s->buf[stream_ptr]);
  214. stream_ptr += 2;
  215. colors[3] = AV_RL16(&s->buf[stream_ptr]);
  216. stream_ptr += 2;
  217. colors[4] = AV_RL16(&s->buf[stream_ptr]);
  218. stream_ptr += 2;
  219. colors[5] = AV_RL16(&s->buf[stream_ptr]);
  220. stream_ptr += 2;
  221. colors[6] = AV_RL16(&s->buf[stream_ptr]);
  222. stream_ptr += 2;
  223. colors[7] = AV_RL16(&s->buf[stream_ptr]);
  224. stream_ptr += 2;
  225. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  226. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  227. pixels[pixel_ptr++] =
  228. colors[((pixel_y & 0x2) << 1) +
  229. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  230. pixel_ptr -= row_dec;
  231. }
  232. } else {
  233. /* 2-color encoding */
  234. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  235. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  236. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  237. pixel_ptr -= row_dec;
  238. }
  239. }
  240. } else {
  241. /* otherwise, it's a 1-color block */
  242. colors[0] = (byte_b << 8) | byte_a;
  243. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  244. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  245. pixels[pixel_ptr++] = colors[0];
  246. pixel_ptr -= row_dec;
  247. }
  248. }
  249. block_ptr += block_inc;
  250. total_blocks--;
  251. }
  252. }
  253. }
  254. static int msvideo1_decode_frame(AVCodecContext *avctx,
  255. void *data, int *data_size,
  256. const uint8_t *buf, int buf_size)
  257. {
  258. Msvideo1Context *s = avctx->priv_data;
  259. s->buf = buf;
  260. s->size = buf_size;
  261. s->frame.reference = 1;
  262. s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
  263. if (avctx->reget_buffer(avctx, &s->frame)) {
  264. av_log(s->avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  265. return -1;
  266. }
  267. if (s->mode_8bit)
  268. msvideo1_decode_8bit(s);
  269. else
  270. msvideo1_decode_16bit(s);
  271. *data_size = sizeof(AVFrame);
  272. *(AVFrame*)data = s->frame;
  273. /* report that the buffer was completely consumed */
  274. return buf_size;
  275. }
  276. static av_cold int msvideo1_decode_end(AVCodecContext *avctx)
  277. {
  278. Msvideo1Context *s = avctx->priv_data;
  279. if (s->frame.data[0])
  280. avctx->release_buffer(avctx, &s->frame);
  281. return 0;
  282. }
  283. AVCodec msvideo1_decoder = {
  284. "msvideo1",
  285. CODEC_TYPE_VIDEO,
  286. CODEC_ID_MSVIDEO1,
  287. sizeof(Msvideo1Context),
  288. msvideo1_decode_init,
  289. NULL,
  290. msvideo1_decode_end,
  291. msvideo1_decode_frame,
  292. CODEC_CAP_DR1,
  293. .long_name= NULL_IF_CONFIG_SMALL("Microsoft Video 1"),
  294. };