msvideo1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Microsoft Video-1 Decoder
  3. * Copyright (C) 2003 the ffmpeg project
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file msvideo1.c
  22. * Microsoft Video-1 Decoder by Mike Melanson (melanson@pcisys.net)
  23. * For more information about the MS Video-1 format, visit:
  24. * http://www.pcisys.net/~melanson/codecs/
  25. *
  26. * This decoder outputs either PAL8 or RGB555 data, depending on the
  27. * whether a RGB palette was passed through via extradata; if the extradata
  28. * is present, then the data is PAL8; RGB555 otherwise.
  29. */
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include "common.h"
  35. #include "avcodec.h"
  36. #include "dsputil.h"
  37. #define PALETTE_COUNT 256
  38. #define LE_16(x) ((((uint8_t*)(x))[1] << 8) | ((uint8_t*)(x))[0])
  39. #define CHECK_STREAM_PTR(n) \
  40. if ((stream_ptr + n) > s->size ) { \
  41. printf (" MS Video-1 warning: stream_ptr out of bounds (%d >= %d)\n", \
  42. stream_ptr + n, s->size); \
  43. return; \
  44. }
  45. #define COPY_PREV_BLOCK() \
  46. pixel_ptr = block_ptr; \
  47. for (pixel_y = 0; pixel_y < 4; pixel_y++) { \
  48. for (pixel_x = 0; pixel_x < 4; pixel_x++, pixel_ptr++) \
  49. pixels[pixel_ptr] = prev_pixels[pixel_ptr]; \
  50. pixel_ptr -= row_dec; \
  51. }
  52. typedef struct Msvideo1Context {
  53. AVCodecContext *avctx;
  54. DSPContext dsp;
  55. AVFrame frame;
  56. AVFrame prev_frame;
  57. unsigned char *buf;
  58. int size;
  59. int mode_8bit; /* if it's not 8-bit, it's 16-bit */
  60. unsigned char palette[PALETTE_COUNT * 4];
  61. } Msvideo1Context;
  62. static int msvideo1_decode_init(AVCodecContext *avctx)
  63. {
  64. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  65. int i;
  66. unsigned char r, g, b;
  67. unsigned char *raw_palette;
  68. unsigned int *palette32;
  69. s->avctx = avctx;
  70. /* figure out the colorspace based on the presence of a palette in
  71. * extradata */
  72. if (s->avctx->extradata_size) {
  73. s->mode_8bit = 1;
  74. /* load up the palette */
  75. palette32 = (unsigned int *)s->palette;
  76. raw_palette = (unsigned char *)s->avctx->extradata;
  77. for (i = 0; i < s->avctx->extradata_size / 4; i++) {
  78. b = *raw_palette++;
  79. g = *raw_palette++;
  80. r = *raw_palette++;
  81. raw_palette++;
  82. palette32[i] = (r << 16) | (g << 8) | (b);
  83. }
  84. avctx->pix_fmt = PIX_FMT_PAL8;
  85. } else {
  86. s->mode_8bit = 0;
  87. avctx->pix_fmt = PIX_FMT_RGB555;
  88. }
  89. avctx->has_b_frames = 0;
  90. dsputil_init(&s->dsp, avctx);
  91. s->frame.data[0] = s->prev_frame.data[0] = NULL;
  92. return 0;
  93. }
  94. static void msvideo1_decode_8bit(Msvideo1Context *s)
  95. {
  96. int block_ptr, pixel_ptr;
  97. int total_blocks;
  98. int pixel_x, pixel_y; /* pixel width and height iterators */
  99. int block_x, block_y; /* block width and height iterators */
  100. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  101. int block_inc;
  102. int row_dec;
  103. /* decoding parameters */
  104. int stream_ptr;
  105. unsigned char byte_a, byte_b;
  106. unsigned short flags;
  107. int skip_blocks;
  108. unsigned char colors[8];
  109. unsigned char *pixels = s->frame.data[0];
  110. unsigned char *prev_pixels = s->prev_frame.data[0];
  111. int stride = s->frame.linesize[0];
  112. stream_ptr = 0;
  113. skip_blocks = 0;
  114. blocks_wide = s->avctx->width / 4;
  115. blocks_high = s->avctx->height / 4;
  116. total_blocks = blocks_wide * blocks_high;
  117. block_inc = 4;
  118. row_dec = stride + 4;
  119. for (block_y = blocks_high; block_y > 0; block_y--) {
  120. block_ptr = ((block_y * 4) - 1) * stride;
  121. for (block_x = blocks_wide; block_x > 0; block_x--) {
  122. /* check if this block should be skipped */
  123. if (skip_blocks) {
  124. COPY_PREV_BLOCK();
  125. block_ptr += block_inc;
  126. skip_blocks--;
  127. total_blocks--;
  128. continue;
  129. }
  130. pixel_ptr = block_ptr;
  131. /* get the next two bytes in the encoded data stream */
  132. CHECK_STREAM_PTR(2);
  133. byte_a = s->buf[stream_ptr++];
  134. byte_b = s->buf[stream_ptr++];
  135. /* check if the decode is finished */
  136. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0))
  137. return;
  138. else if ((byte_b & 0xFC) == 0x84) {
  139. /* skip code, but don't count the current block */
  140. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  141. COPY_PREV_BLOCK();
  142. } else if (byte_b < 0x80) {
  143. /* 2-color encoding */
  144. flags = (byte_b << 8) | byte_a;
  145. CHECK_STREAM_PTR(2);
  146. colors[0] = s->buf[stream_ptr++];
  147. colors[1] = s->buf[stream_ptr++];
  148. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  149. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  150. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  151. pixel_ptr -= row_dec;
  152. }
  153. } else if (byte_b >= 0x90) {
  154. /* 8-color encoding */
  155. flags = (byte_b << 8) | byte_a;
  156. CHECK_STREAM_PTR(8);
  157. memcpy(colors, &s->buf[stream_ptr], 8);
  158. stream_ptr += 8;
  159. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  160. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  161. pixels[pixel_ptr++] =
  162. colors[((pixel_y & 0x2) << 1) +
  163. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  164. pixel_ptr -= row_dec;
  165. }
  166. } else {
  167. /* 1-color encoding */
  168. colors[0] = byte_a;
  169. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  170. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  171. pixels[pixel_ptr++] = colors[0];
  172. pixel_ptr -= row_dec;
  173. }
  174. }
  175. block_ptr += block_inc;
  176. total_blocks--;
  177. }
  178. }
  179. /* make the palette available on the way out */
  180. if (s->avctx->pix_fmt == PIX_FMT_PAL8)
  181. memcpy(s->frame.data[1], s->palette, PALETTE_COUNT * 4);
  182. }
  183. static void msvideo1_decode_16bit(Msvideo1Context *s)
  184. {
  185. int block_ptr, pixel_ptr;
  186. int total_blocks;
  187. int pixel_x, pixel_y; /* pixel width and height iterators */
  188. int block_x, block_y; /* block width and height iterators */
  189. int blocks_wide, blocks_high; /* width and height in 4x4 blocks */
  190. int block_inc;
  191. int row_dec;
  192. /* decoding parameters */
  193. int stream_ptr;
  194. unsigned char byte_a, byte_b;
  195. unsigned short flags;
  196. int skip_blocks;
  197. unsigned short colors[8];
  198. unsigned short *pixels = (unsigned short *)s->frame.data[0];
  199. unsigned short *prev_pixels = (unsigned short *)s->prev_frame.data[0];
  200. int stride = s->frame.linesize[0] / 2;
  201. stream_ptr = 0;
  202. skip_blocks = 0;
  203. blocks_wide = s->avctx->width / 4;
  204. blocks_high = s->avctx->height / 4;
  205. total_blocks = blocks_wide * blocks_high;
  206. block_inc = 4;
  207. row_dec = stride + 4;
  208. for (block_y = blocks_high; block_y > 0; block_y--) {
  209. block_ptr = ((block_y * 4) - 1) * stride;
  210. for (block_x = blocks_wide; block_x > 0; block_x--) {
  211. /* check if this block should be skipped */
  212. if (skip_blocks) {
  213. COPY_PREV_BLOCK();
  214. block_ptr += block_inc;
  215. skip_blocks--;
  216. total_blocks--;
  217. continue;
  218. }
  219. pixel_ptr = block_ptr;
  220. /* get the next two bytes in the encoded data stream */
  221. CHECK_STREAM_PTR(2);
  222. byte_a = s->buf[stream_ptr++];
  223. byte_b = s->buf[stream_ptr++];
  224. /* check if the decode is finished */
  225. if ((byte_a == 0) && (byte_b == 0) && (total_blocks == 0)) {
  226. return;
  227. } else if ((byte_b & 0xFC) == 0x84) {
  228. /* skip code, but don't count the current block */
  229. skip_blocks = ((byte_b - 0x84) << 8) + byte_a - 1;
  230. COPY_PREV_BLOCK();
  231. } else if (byte_b < 0x80) {
  232. /* 2- or 8-color encoding modes */
  233. flags = (byte_b << 8) | byte_a;
  234. CHECK_STREAM_PTR(4);
  235. colors[0] = LE_16(&s->buf[stream_ptr]);
  236. stream_ptr += 2;
  237. colors[1] = LE_16(&s->buf[stream_ptr]);
  238. stream_ptr += 2;
  239. if (colors[0] & 0x8000) {
  240. /* 8-color encoding */
  241. CHECK_STREAM_PTR(12);
  242. colors[2] = LE_16(&s->buf[stream_ptr]);
  243. stream_ptr += 2;
  244. colors[3] = LE_16(&s->buf[stream_ptr]);
  245. stream_ptr += 2;
  246. colors[4] = LE_16(&s->buf[stream_ptr]);
  247. stream_ptr += 2;
  248. colors[5] = LE_16(&s->buf[stream_ptr]);
  249. stream_ptr += 2;
  250. colors[6] = LE_16(&s->buf[stream_ptr]);
  251. stream_ptr += 2;
  252. colors[7] = LE_16(&s->buf[stream_ptr]);
  253. stream_ptr += 2;
  254. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  255. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  256. pixels[pixel_ptr++] =
  257. colors[((pixel_y & 0x2) << 1) +
  258. (pixel_x & 0x2) + ((flags & 0x1) ^ 1)];
  259. pixel_ptr -= row_dec;
  260. }
  261. } else {
  262. /* 2-color encoding */
  263. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  264. for (pixel_x = 0; pixel_x < 4; pixel_x++, flags >>= 1)
  265. pixels[pixel_ptr++] = colors[(flags & 0x1) ^ 1];
  266. pixel_ptr -= row_dec;
  267. }
  268. }
  269. } else {
  270. /* otherwise, it's a 1-color block */
  271. colors[0] = (byte_b << 8) | byte_a;
  272. for (pixel_y = 0; pixel_y < 4; pixel_y++) {
  273. for (pixel_x = 0; pixel_x < 4; pixel_x++)
  274. pixels[pixel_ptr++] = colors[0];
  275. pixel_ptr -= row_dec;
  276. }
  277. }
  278. block_ptr += block_inc;
  279. total_blocks--;
  280. }
  281. }
  282. }
  283. static int msvideo1_decode_frame(AVCodecContext *avctx,
  284. void *data, int *data_size,
  285. uint8_t *buf, int buf_size)
  286. {
  287. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  288. s->buf = buf;
  289. s->size = buf_size;
  290. if (avctx->get_buffer(avctx, &s->frame)) {
  291. printf (" MS Video-1 Video: get_buffer() failed\n");
  292. return -1;
  293. }
  294. if (s->mode_8bit)
  295. msvideo1_decode_8bit(s);
  296. else
  297. msvideo1_decode_16bit(s);
  298. if (s->prev_frame.data[0])
  299. avctx->release_buffer(avctx, &s->prev_frame);
  300. /* shuffle frames */
  301. s->prev_frame = s->frame;
  302. *data_size = sizeof(AVFrame);
  303. *(AVFrame*)data = s->frame;
  304. /* report that the buffer was completely consumed */
  305. return buf_size;
  306. }
  307. static int msvideo1_decode_end(AVCodecContext *avctx)
  308. {
  309. Msvideo1Context *s = (Msvideo1Context *)avctx->priv_data;
  310. if (s->prev_frame.data[0])
  311. avctx->release_buffer(avctx, &s->prev_frame);
  312. return 0;
  313. }
  314. AVCodec msvideo1_decoder = {
  315. "msvideo1",
  316. CODEC_TYPE_VIDEO,
  317. CODEC_ID_MSVIDEO1,
  318. sizeof(Msvideo1Context),
  319. msvideo1_decode_init,
  320. NULL,
  321. msvideo1_decode_end,
  322. msvideo1_decode_frame,
  323. CODEC_CAP_DR1,
  324. };