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.

273 lines
9.2KB

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