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.

247 lines
8.6KB

  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 "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "internal.h"
  26. #include "xwd.h"
  27. static int xwd_decode_frame(AVCodecContext *avctx, void *data,
  28. int *got_frame, AVPacket *avpkt)
  29. {
  30. AVFrame *p = data;
  31. const uint8_t *buf = avpkt->data;
  32. int i, ret, buf_size = avpkt->size;
  33. uint32_t version, header_size, vclass, ncolors;
  34. uint32_t xoffset, be, bpp, lsize, rsize;
  35. uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
  36. uint32_t rgb[3];
  37. uint8_t *ptr;
  38. GetByteContext gb;
  39. if (buf_size < XWD_HEADER_SIZE)
  40. return AVERROR_INVALIDDATA;
  41. bytestream2_init(&gb, buf, buf_size);
  42. header_size = bytestream2_get_be32u(&gb);
  43. version = bytestream2_get_be32u(&gb);
  44. if (version != XWD_VERSION) {
  45. av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
  46. return AVERROR_INVALIDDATA;
  47. }
  48. if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
  49. av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
  50. return AVERROR_INVALIDDATA;
  51. }
  52. pixformat = bytestream2_get_be32u(&gb);
  53. pixdepth = bytestream2_get_be32u(&gb);
  54. avctx->width = bytestream2_get_be32u(&gb);
  55. avctx->height = bytestream2_get_be32u(&gb);
  56. xoffset = bytestream2_get_be32u(&gb);
  57. be = bytestream2_get_be32u(&gb);
  58. bunit = bytestream2_get_be32u(&gb);
  59. bitorder = bytestream2_get_be32u(&gb);
  60. bpad = bytestream2_get_be32u(&gb);
  61. bpp = bytestream2_get_be32u(&gb);
  62. lsize = bytestream2_get_be32u(&gb);
  63. vclass = bytestream2_get_be32u(&gb);
  64. rgb[0] = bytestream2_get_be32u(&gb);
  65. rgb[1] = bytestream2_get_be32u(&gb);
  66. rgb[2] = bytestream2_get_be32u(&gb);
  67. bytestream2_skipu(&gb, 8);
  68. ncolors = bytestream2_get_be32u(&gb);
  69. bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
  70. av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
  71. pixformat, pixdepth, bunit, bitorder, bpad);
  72. av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
  73. vclass, ncolors, bpp, be, lsize, xoffset);
  74. av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
  75. if (pixformat > XWD_Z_PIXMAP) {
  76. av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
  77. return AVERROR_INVALIDDATA;
  78. }
  79. if (pixdepth == 0 || pixdepth > 32) {
  80. av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
  81. return AVERROR_INVALIDDATA;
  82. }
  83. if (xoffset) {
  84. avpriv_request_sample(avctx, "xoffset %d", xoffset);
  85. return AVERROR_PATCHWELCOME;
  86. }
  87. if (be > 1) {
  88. av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
  89. return AVERROR_INVALIDDATA;
  90. }
  91. if (bitorder > 1) {
  92. av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
  93. return AVERROR_INVALIDDATA;
  94. }
  95. if (bunit != 8 && bunit != 16 && bunit != 32) {
  96. av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
  97. return AVERROR_INVALIDDATA;
  98. }
  99. if (bpad != 8 && bpad != 16 && bpad != 32) {
  100. av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
  101. return AVERROR_INVALIDDATA;
  102. }
  103. if (bpp == 0 || bpp > 32) {
  104. av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
  105. return AVERROR_INVALIDDATA;
  106. }
  107. if (ncolors > 256) {
  108. av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
  109. return AVERROR_INVALIDDATA;
  110. }
  111. if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
  112. return ret;
  113. rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
  114. if (lsize < rsize) {
  115. av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
  116. return AVERROR_INVALIDDATA;
  117. }
  118. if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
  119. av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
  120. return AVERROR_INVALIDDATA;
  121. }
  122. if (pixformat != XWD_Z_PIXMAP) {
  123. av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
  124. return AVERROR_PATCHWELCOME;
  125. }
  126. avctx->pix_fmt = AV_PIX_FMT_NONE;
  127. switch (vclass) {
  128. case XWD_STATIC_GRAY:
  129. case XWD_GRAY_SCALE:
  130. if (bpp != 1)
  131. return AVERROR_INVALIDDATA;
  132. if (pixdepth == 1)
  133. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  134. break;
  135. case XWD_STATIC_COLOR:
  136. case XWD_PSEUDO_COLOR:
  137. if (bpp == 8)
  138. avctx->pix_fmt = AV_PIX_FMT_PAL8;
  139. break;
  140. case XWD_TRUE_COLOR:
  141. case XWD_DIRECT_COLOR:
  142. if (bpp != 16 && bpp != 24 && bpp != 32)
  143. return AVERROR_INVALIDDATA;
  144. if (bpp == 16 && pixdepth == 15) {
  145. if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
  146. avctx->pix_fmt = be ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
  147. else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
  148. avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_PIX_FMT_BGR555LE;
  149. } else if (bpp == 16 && pixdepth == 16) {
  150. if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
  151. avctx->pix_fmt = be ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
  152. else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
  153. avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
  154. } else if (bpp == 24) {
  155. if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
  156. avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  157. else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
  158. avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
  159. } else if (bpp == 32) {
  160. if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
  161. avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
  162. else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
  163. avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
  164. }
  165. bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
  166. break;
  167. default:
  168. av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
  169. return AVERROR_INVALIDDATA;
  170. }
  171. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  172. avpriv_request_sample(avctx,
  173. "Unknown file: bpp %d, pixdepth %d, vclass %d",
  174. bpp, pixdepth, vclass);
  175. return AVERROR_PATCHWELCOME;
  176. }
  177. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  178. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  179. return ret;
  180. }
  181. p->key_frame = 1;
  182. p->pict_type = AV_PICTURE_TYPE_I;
  183. if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
  184. uint32_t *dst = (uint32_t *)p->data[1];
  185. uint8_t red, green, blue;
  186. for (i = 0; i < ncolors; i++) {
  187. bytestream2_skipu(&gb, 4); // skip colormap entry number
  188. red = bytestream2_get_byteu(&gb);
  189. bytestream2_skipu(&gb, 1);
  190. green = bytestream2_get_byteu(&gb);
  191. bytestream2_skipu(&gb, 1);
  192. blue = bytestream2_get_byteu(&gb);
  193. bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
  194. dst[i] = red << 16 | green << 8 | blue;
  195. }
  196. }
  197. ptr = p->data[0];
  198. for (i = 0; i < avctx->height; i++) {
  199. bytestream2_get_bufferu(&gb, ptr, rsize);
  200. bytestream2_skipu(&gb, lsize - rsize);
  201. ptr += p->linesize[0];
  202. }
  203. *got_frame = 1;
  204. return buf_size;
  205. }
  206. AVCodec ff_xwd_decoder = {
  207. .name = "xwd",
  208. .type = AVMEDIA_TYPE_VIDEO,
  209. .id = AV_CODEC_ID_XWD,
  210. .decode = xwd_decode_frame,
  211. .capabilities = CODEC_CAP_DR1,
  212. .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
  213. };