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.

240 lines
8.7KB

  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/intreadwrite.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "avcodec.h"
  25. #include "bytestream.h"
  26. #include "internal.h"
  27. #include "xwd.h"
  28. #define WINDOW_NAME "lavcxwdenc"
  29. #define WINDOW_NAME_SIZE 11
  30. static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  31. const AVFrame *pict, int *got_packet)
  32. {
  33. enum AVPixelFormat pix_fmt = avctx->pix_fmt;
  34. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  35. uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
  36. uint32_t rgb[3] = { 0 }, bitorder = 0;
  37. uint32_t header_size;
  38. int i, out_size, ret;
  39. uint8_t *ptr, *buf;
  40. AVFrame * const p = (AVFrame *)pict;
  41. pixdepth = av_get_bits_per_pixel(desc);
  42. if (desc->flags & AV_PIX_FMT_FLAG_BE)
  43. be = 1;
  44. switch (pix_fmt) {
  45. case AV_PIX_FMT_ARGB:
  46. case AV_PIX_FMT_BGRA:
  47. case AV_PIX_FMT_RGBA:
  48. case AV_PIX_FMT_ABGR:
  49. if (pix_fmt == AV_PIX_FMT_ARGB ||
  50. pix_fmt == AV_PIX_FMT_ABGR)
  51. be = 1;
  52. if (pix_fmt == AV_PIX_FMT_ABGR ||
  53. pix_fmt == AV_PIX_FMT_RGBA) {
  54. rgb[0] = 0xFF;
  55. rgb[1] = 0xFF00;
  56. rgb[2] = 0xFF0000;
  57. } else {
  58. rgb[0] = 0xFF0000;
  59. rgb[1] = 0xFF00;
  60. rgb[2] = 0xFF;
  61. }
  62. bpp = 32;
  63. pixdepth = 24;
  64. vclass = XWD_TRUE_COLOR;
  65. bpad = 32;
  66. break;
  67. case AV_PIX_FMT_BGR24:
  68. case AV_PIX_FMT_RGB24:
  69. if (pix_fmt == AV_PIX_FMT_RGB24)
  70. be = 1;
  71. bpp = 24;
  72. vclass = XWD_TRUE_COLOR;
  73. bpad = 32;
  74. rgb[0] = 0xFF0000;
  75. rgb[1] = 0xFF00;
  76. rgb[2] = 0xFF;
  77. break;
  78. case AV_PIX_FMT_RGB565LE:
  79. case AV_PIX_FMT_RGB565BE:
  80. case AV_PIX_FMT_BGR565LE:
  81. case AV_PIX_FMT_BGR565BE:
  82. if (pix_fmt == AV_PIX_FMT_BGR565LE ||
  83. pix_fmt == AV_PIX_FMT_BGR565BE) {
  84. rgb[0] = 0x1F;
  85. rgb[1] = 0x7E0;
  86. rgb[2] = 0xF800;
  87. } else {
  88. rgb[0] = 0xF800;
  89. rgb[1] = 0x7E0;
  90. rgb[2] = 0x1F;
  91. }
  92. bpp = 16;
  93. vclass = XWD_TRUE_COLOR;
  94. bpad = 16;
  95. break;
  96. case AV_PIX_FMT_RGB555LE:
  97. case AV_PIX_FMT_RGB555BE:
  98. case AV_PIX_FMT_BGR555LE:
  99. case AV_PIX_FMT_BGR555BE:
  100. if (pix_fmt == AV_PIX_FMT_BGR555LE ||
  101. pix_fmt == AV_PIX_FMT_BGR555BE) {
  102. rgb[0] = 0x1F;
  103. rgb[1] = 0x3E0;
  104. rgb[2] = 0x7C00;
  105. } else {
  106. rgb[0] = 0x7C00;
  107. rgb[1] = 0x3E0;
  108. rgb[2] = 0x1F;
  109. }
  110. bpp = 16;
  111. vclass = XWD_TRUE_COLOR;
  112. bpad = 16;
  113. break;
  114. case AV_PIX_FMT_RGB8:
  115. case AV_PIX_FMT_BGR8:
  116. case AV_PIX_FMT_RGB4_BYTE:
  117. case AV_PIX_FMT_BGR4_BYTE:
  118. case AV_PIX_FMT_PAL8:
  119. bpp = 8;
  120. vclass = XWD_PSEUDO_COLOR;
  121. bpad = 8;
  122. ncolors = 256;
  123. break;
  124. case AV_PIX_FMT_GRAY8:
  125. bpp = 8;
  126. bpad = 8;
  127. vclass = XWD_STATIC_GRAY;
  128. break;
  129. case AV_PIX_FMT_MONOWHITE:
  130. be = 1;
  131. bitorder = 1;
  132. bpp = 1;
  133. bpad = 8;
  134. vclass = XWD_STATIC_GRAY;
  135. break;
  136. default:
  137. av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
  138. return AVERROR(EINVAL);
  139. }
  140. lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
  141. header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
  142. out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
  143. if ((ret = ff_alloc_packet2(avctx, pkt, out_size, 0)) < 0)
  144. return ret;
  145. buf = pkt->data;
  146. p->key_frame = 1;
  147. p->pict_type = AV_PICTURE_TYPE_I;
  148. bytestream_put_be32(&buf, header_size);
  149. bytestream_put_be32(&buf, XWD_VERSION); // file version
  150. bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
  151. bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
  152. bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
  153. bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
  154. bytestream_put_be32(&buf, 0); // bitmap x offset
  155. bytestream_put_be32(&buf, be); // byte order
  156. bytestream_put_be32(&buf, 32); // bitmap unit
  157. bytestream_put_be32(&buf, bitorder); // bit-order of image data
  158. bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
  159. bytestream_put_be32(&buf, bpp); // bits per pixel
  160. bytestream_put_be32(&buf, lsize); // bytes per scan-line
  161. bytestream_put_be32(&buf, vclass); // visual class
  162. bytestream_put_be32(&buf, rgb[0]); // red mask
  163. bytestream_put_be32(&buf, rgb[1]); // green mask
  164. bytestream_put_be32(&buf, rgb[2]); // blue mask
  165. bytestream_put_be32(&buf, 8); // size of each bitmask in bits
  166. bytestream_put_be32(&buf, ncolors); // number of colors
  167. bytestream_put_be32(&buf, ncolors); // number of entries in color map
  168. bytestream_put_be32(&buf, avctx->width); // window width
  169. bytestream_put_be32(&buf, avctx->height); // window height
  170. bytestream_put_be32(&buf, 0); // window upper left X coordinate
  171. bytestream_put_be32(&buf, 0); // window upper left Y coordinate
  172. bytestream_put_be32(&buf, 0); // window border width
  173. bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
  174. for (i = 0; i < ncolors; i++) {
  175. uint32_t val;
  176. uint8_t red, green, blue;
  177. val = AV_RN32A(p->data[1] + i * 4);
  178. red = (val >> 16) & 0xFF;
  179. green = (val >> 8) & 0xFF;
  180. blue = val & 0xFF;
  181. bytestream_put_be32(&buf, i); // colormap entry number
  182. bytestream_put_be16(&buf, red << 8);
  183. bytestream_put_be16(&buf, green << 8);
  184. bytestream_put_be16(&buf, blue << 8);
  185. bytestream_put_byte(&buf, 0x7); // bitmask flag
  186. bytestream_put_byte(&buf, 0); // padding
  187. }
  188. ptr = p->data[0];
  189. for (i = 0; i < avctx->height; i++) {
  190. bytestream_put_buffer(&buf, ptr, lsize);
  191. ptr += p->linesize[0];
  192. }
  193. pkt->flags |= AV_PKT_FLAG_KEY;
  194. *got_packet = 1;
  195. return 0;
  196. }
  197. AVCodec ff_xwd_encoder = {
  198. .name = "xwd",
  199. .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
  200. .type = AVMEDIA_TYPE_VIDEO,
  201. .id = AV_CODEC_ID_XWD,
  202. .encode2 = xwd_encode_frame,
  203. .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
  204. AV_PIX_FMT_RGBA,
  205. AV_PIX_FMT_ARGB,
  206. AV_PIX_FMT_ABGR,
  207. AV_PIX_FMT_RGB24,
  208. AV_PIX_FMT_BGR24,
  209. AV_PIX_FMT_RGB565BE,
  210. AV_PIX_FMT_RGB565LE,
  211. AV_PIX_FMT_BGR565BE,
  212. AV_PIX_FMT_BGR565LE,
  213. AV_PIX_FMT_RGB555BE,
  214. AV_PIX_FMT_RGB555LE,
  215. AV_PIX_FMT_BGR555BE,
  216. AV_PIX_FMT_BGR555LE,
  217. AV_PIX_FMT_RGB8,
  218. AV_PIX_FMT_BGR8,
  219. AV_PIX_FMT_RGB4_BYTE,
  220. AV_PIX_FMT_BGR4_BYTE,
  221. AV_PIX_FMT_PAL8,
  222. AV_PIX_FMT_GRAY8,
  223. AV_PIX_FMT_MONOWHITE,
  224. AV_PIX_FMT_NONE },
  225. };