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.

359 lines
12KB

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