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.

274 lines
9.3KB

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