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.

258 lines
9.0KB

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