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.

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