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.

253 lines
8.8KB

  1. /*
  2. * XWD image format
  3. *
  4. * Copyright (c) 2012 Paul B Mahol
  5. *
  6. * This file is part of Libav.
  7. *
  8. * Libav is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * Libav is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with Libav; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <inttypes.h>
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "xwd.h"
  28. static int xwd_decode_frame(AVCodecContext *avctx, void *data,
  29. int *got_frame, AVPacket *avpkt)
  30. {
  31. AVFrame *p = data;
  32. const uint8_t *buf = avpkt->data;
  33. int i, ret, buf_size = avpkt->size;
  34. uint32_t version, header_size, vclass, ncolors;
  35. uint32_t xoffset, be, bpp, lsize, rsize;
  36. uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
  37. uint32_t rgb[3];
  38. uint8_t *ptr;
  39. GetByteContext gb;
  40. if (buf_size < XWD_HEADER_SIZE)
  41. return AVERROR_INVALIDDATA;
  42. bytestream2_init(&gb, buf, buf_size);
  43. header_size = bytestream2_get_be32u(&gb);
  44. version = bytestream2_get_be32u(&gb);
  45. if (version != XWD_VERSION) {
  46. av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
  47. return AVERROR_INVALIDDATA;
  48. }
  49. if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
  50. av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
  51. return AVERROR_INVALIDDATA;
  52. }
  53. pixformat = bytestream2_get_be32u(&gb);
  54. pixdepth = bytestream2_get_be32u(&gb);
  55. avctx->width = bytestream2_get_be32u(&gb);
  56. avctx->height = bytestream2_get_be32u(&gb);
  57. xoffset = bytestream2_get_be32u(&gb);
  58. be = bytestream2_get_be32u(&gb);
  59. bunit = bytestream2_get_be32u(&gb);
  60. bitorder = bytestream2_get_be32u(&gb);
  61. bpad = bytestream2_get_be32u(&gb);
  62. bpp = bytestream2_get_be32u(&gb);
  63. lsize = bytestream2_get_be32u(&gb);
  64. vclass = bytestream2_get_be32u(&gb);
  65. rgb[0] = bytestream2_get_be32u(&gb);
  66. rgb[1] = bytestream2_get_be32u(&gb);
  67. rgb[2] = bytestream2_get_be32u(&gb);
  68. bytestream2_skipu(&gb, 8);
  69. ncolors = bytestream2_get_be32u(&gb);
  70. bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
  71. av_log(avctx, AV_LOG_DEBUG,
  72. "pixformat %"PRIu32", pixdepth %"PRIu32", bunit %"PRIu32", bitorder %"PRIu32", bpad %"PRIu32"\n",
  73. pixformat, pixdepth, bunit, bitorder, bpad);
  74. av_log(avctx, AV_LOG_DEBUG,
  75. "vclass %"PRIu32", ncolors %"PRIu32", bpp %"PRIu32", be %"PRIu32", lsize %"PRIu32", xoffset %"PRIu32"\n",
  76. vclass, ncolors, bpp, be, lsize, xoffset);
  77. av_log(avctx, AV_LOG_DEBUG,
  78. "red %0"PRIx32", green %0"PRIx32", blue %0"PRIx32"\n",
  79. rgb[0], rgb[1], rgb[2]);
  80. if (pixformat > XWD_Z_PIXMAP) {
  81. av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (pixdepth == 0 || pixdepth > 32) {
  85. av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. if (xoffset) {
  89. avpriv_request_sample(avctx, "xoffset %"PRIu32"", xoffset);
  90. return AVERROR_PATCHWELCOME;
  91. }
  92. if (be > 1) {
  93. av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
  94. return AVERROR_INVALIDDATA;
  95. }
  96. if (bitorder > 1) {
  97. av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
  98. return AVERROR_INVALIDDATA;
  99. }
  100. if (bunit != 8 && bunit != 16 && bunit != 32) {
  101. av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
  102. return AVERROR_INVALIDDATA;
  103. }
  104. if (bpad != 8 && bpad != 16 && bpad != 32) {
  105. av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
  106. return AVERROR_INVALIDDATA;
  107. }
  108. if (bpp == 0 || bpp > 32) {
  109. av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
  110. return AVERROR_INVALIDDATA;
  111. }
  112. if (ncolors > 256) {
  113. av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
  114. return AVERROR_INVALIDDATA;
  115. }
  116. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
  117. return ret;
  118. rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
  119. if (lsize < rsize) {
  120. av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
  121. return AVERROR_INVALIDDATA;
  122. }
  123. if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + (uint64_t)avctx->height * lsize) {
  124. av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
  125. return AVERROR_INVALIDDATA;
  126. }
  127. if (pixformat != XWD_Z_PIXMAP) {
  128. av_log(avctx, AV_LOG_ERROR, "pixmap format %"PRIu32" unsupported\n", pixformat);
  129. return AVERROR_PATCHWELCOME;
  130. }
  131. avctx->pix_fmt = AV_PIX_FMT_NONE;
  132. switch (vclass) {
  133. case XWD_STATIC_GRAY:
  134. case XWD_GRAY_SCALE:
  135. if (bpp != 1)
  136. return AVERROR_INVALIDDATA;
  137. if (pixdepth == 1)
  138. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  139. break;
  140. case XWD_STATIC_COLOR:
  141. case XWD_PSEUDO_COLOR:
  142. if (bpp == 8)
  143. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  144. break;
  145. case XWD_TRUE_COLOR:
  146. case XWD_DIRECT_COLOR:
  147. if (bpp != 16 && bpp != 24 && bpp != 32)
  148. return AVERROR_INVALIDDATA;
  149. if (bpp == 16 && pixdepth == 15) {
  150. if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
  151. avctx->pix_fmt = be ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
  152. else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
  153. avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_PIX_FMT_BGR555LE;
  154. } else if (bpp == 16 && pixdepth == 16) {
  155. if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
  156. avctx->pix_fmt = be ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
  157. else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
  158. avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
  159. } else if (bpp == 24) {
  160. if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
  161. avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  162. else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
  163. avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
  164. } else if (bpp == 32) {
  165. if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
  166. avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
  167. else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
  168. avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
  169. }
  170. bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
  171. break;
  172. default:
  173. av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
  174. return AVERROR_INVALIDDATA;
  175. }
  176. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  177. avpriv_request_sample(avctx,
  178. "Unknown file: bpp %"PRIu32", pixdepth %"PRIu32", vclass %"PRIu32"",
  179. bpp, pixdepth, vclass);
  180. return AVERROR_PATCHWELCOME;
  181. }
  182. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  183. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  184. return ret;
  185. }
  186. p->key_frame = 1;
  187. p->pict_type = AV_PICTURE_TYPE_I;
  188. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  189. uint32_t *dst = (uint32_t *)p->data[1];
  190. uint8_t red, green, blue;
  191. for (i = 0; i < ncolors; i++) {
  192. bytestream2_skipu(&gb, 4); // skip colormap entry number
  193. red = bytestream2_get_byteu(&gb);
  194. bytestream2_skipu(&gb, 1);
  195. green = bytestream2_get_byteu(&gb);
  196. bytestream2_skipu(&gb, 1);
  197. blue = bytestream2_get_byteu(&gb);
  198. bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
  199. dst[i] = red << 16 | green << 8 | blue;
  200. }
  201. }
  202. ptr = p->data[0];
  203. for (i = 0; i < avctx->height; i++) {
  204. bytestream2_get_bufferu(&gb, ptr, rsize);
  205. bytestream2_skipu(&gb, lsize - rsize);
  206. ptr += p->linesize[0];
  207. }
  208. *got_frame = 1;
  209. return buf_size;
  210. }
  211. AVCodec ff_xwd_decoder = {
  212. .name = "xwd",
  213. .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .id = AV_CODEC_ID_XWD,
  216. .decode = xwd_decode_frame,
  217. .capabilities = AV_CODEC_CAP_DR1,
  218. };