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.

260 lines
8.0KB

  1. /*
  2. * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image encoder
  3. * Copyright (c) 2012 Aneesh Dogra (lionaneesh) <lionaneesh@gmail.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/opt.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #include "sunrast.h"
  26. typedef struct SUNRASTContext {
  27. PutByteContext p;
  28. int depth; ///< depth of pixel
  29. int length; ///< length (bytes) of image
  30. int type; ///< type of file
  31. int maptype; ///< type of colormap
  32. int maplength; ///< length (bytes) of colormap
  33. int size;
  34. } SUNRASTContext;
  35. static void sunrast_image_write_header(AVCodecContext *avctx)
  36. {
  37. SUNRASTContext *s = avctx->priv_data;
  38. bytestream2_put_be32u(&s->p, RAS_MAGIC);
  39. bytestream2_put_be32u(&s->p, avctx->width);
  40. bytestream2_put_be32u(&s->p, avctx->height);
  41. bytestream2_put_be32u(&s->p, s->depth);
  42. bytestream2_put_be32u(&s->p, s->length);
  43. bytestream2_put_be32u(&s->p, s->type);
  44. bytestream2_put_be32u(&s->p, s->maptype);
  45. bytestream2_put_be32u(&s->p, s->maplength);
  46. }
  47. static void sunrast_image_write_image(AVCodecContext *avctx,
  48. const uint8_t *pixels,
  49. const uint32_t *palette_data,
  50. int linesize)
  51. {
  52. SUNRASTContext *s = avctx->priv_data;
  53. const uint8_t *ptr;
  54. int len, alen, x;
  55. if (s->maplength) { // palettized
  56. PutByteContext pb_r, pb_g;
  57. int len = s->maplength / 3;
  58. pb_r = s->p;
  59. bytestream2_skip_p(&s->p, len);
  60. pb_g = s->p;
  61. bytestream2_skip_p(&s->p, len);
  62. for (x = 0; x < len; x++) {
  63. uint32_t pixel = palette_data[x];
  64. bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
  65. bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
  66. bytestream2_put_byteu(&s->p, pixel & 0xFF);
  67. }
  68. }
  69. len = (s->depth * avctx->width + 7) >> 3;
  70. alen = len + (len & 1);
  71. ptr = pixels;
  72. if (s->type == RT_BYTE_ENCODED) {
  73. uint8_t value, value2;
  74. int run;
  75. const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
  76. : pixels;
  77. const uint8_t *end = linesize < 0 ? pixels - linesize
  78. : pixels + avctx->height * linesize;
  79. ptr = pixels;
  80. #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
  81. x = 0;
  82. value2 = GET_VALUE;
  83. while (ptr < end && ptr >= start) {
  84. run = 1;
  85. value = value2;
  86. x++;
  87. if (x >= alen) {
  88. x = 0;
  89. ptr += linesize;
  90. }
  91. value2 = GET_VALUE;
  92. while (value2 == value && run < 256 && ptr < end && ptr >= start) {
  93. x++;
  94. run++;
  95. if (x >= alen) {
  96. x = 0;
  97. ptr += linesize;
  98. }
  99. value2 = GET_VALUE;
  100. }
  101. if (run > 2 || value == RLE_TRIGGER) {
  102. bytestream2_put_byteu(&s->p, RLE_TRIGGER);
  103. bytestream2_put_byteu(&s->p, run - 1);
  104. if (run > 1)
  105. bytestream2_put_byteu(&s->p, value);
  106. } else if (run == 1) {
  107. bytestream2_put_byteu(&s->p, value);
  108. } else
  109. bytestream2_put_be16u(&s->p, (value << 8) | value);
  110. }
  111. // update data length for header
  112. s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
  113. } else {
  114. int y;
  115. for (y = 0; y < avctx->height; y++) {
  116. bytestream2_put_buffer(&s->p, ptr, len);
  117. if (len < alen)
  118. bytestream2_put_byteu(&s->p, 0);
  119. ptr += linesize;
  120. }
  121. }
  122. }
  123. static av_cold int sunrast_encode_init(AVCodecContext *avctx)
  124. {
  125. SUNRASTContext *s = avctx->priv_data;
  126. #if FF_API_CODER_TYPE
  127. FF_DISABLE_DEPRECATION_WARNINGS
  128. switch (avctx->coder_type) {
  129. case FF_CODER_TYPE_RLE:
  130. s->type = RT_BYTE_ENCODED;
  131. break;
  132. case FF_CODER_TYPE_RAW:
  133. s->type = RT_STANDARD;
  134. break;
  135. default:
  136. av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
  137. return AVERROR(EINVAL);
  138. }
  139. FF_ENABLE_DEPRECATION_WARNINGS
  140. if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
  141. #endif
  142. // adjust boolean option to RT equivalent
  143. s->type++;
  144. #if FF_API_CODED_FRAME
  145. FF_DISABLE_DEPRECATION_WARNINGS
  146. avctx->coded_frame->key_frame = 1;
  147. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  148. FF_ENABLE_DEPRECATION_WARNINGS
  149. #endif
  150. s->maptype = RMT_NONE;
  151. s->maplength = 0;
  152. switch (avctx->pix_fmt) {
  153. case AV_PIX_FMT_MONOWHITE:
  154. s->depth = 1;
  155. break;
  156. case AV_PIX_FMT_PAL8 :
  157. s->maptype = RMT_EQUAL_RGB;
  158. s->maplength = 3 * 256;
  159. /* fall-through */
  160. case AV_PIX_FMT_GRAY8:
  161. s->depth = 8;
  162. break;
  163. case AV_PIX_FMT_BGR24:
  164. s->depth = 24;
  165. break;
  166. default:
  167. return AVERROR_BUG;
  168. }
  169. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  170. s->size = 32 + s->maplength + s->length * s->type;
  171. return 0;
  172. }
  173. static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  174. const AVFrame *frame, int *got_packet_ptr)
  175. {
  176. SUNRASTContext *s = avctx->priv_data;
  177. int ret;
  178. if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
  179. return ret;
  180. bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
  181. sunrast_image_write_header(avctx);
  182. sunrast_image_write_image(avctx, frame->data[0],
  183. (const uint32_t *)frame->data[1],
  184. frame->linesize[0]);
  185. // update data length in header after RLE
  186. if (s->type == RT_BYTE_ENCODED)
  187. AV_WB32(&avpkt->data[16], s->length);
  188. *got_packet_ptr = 1;
  189. avpkt->flags |= AV_PKT_FLAG_KEY;
  190. avpkt->size = bytestream2_tell_p(&s->p);
  191. return 0;
  192. }
  193. #define OFFSET(x) offsetof(SUNRASTContext, x)
  194. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  195. static const AVOption options[] = {
  196. { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  197. { NULL },
  198. };
  199. static const AVClass utvideo_class = {
  200. .class_name = "sunrast",
  201. .item_name = av_default_item_name,
  202. .option = options,
  203. .version = LIBAVUTIL_VERSION_INT,
  204. };
  205. #if FF_API_CODER_TYPE
  206. static const AVCodecDefault sunrast_defaults[] = {
  207. { "coder", "rle" },
  208. { NULL },
  209. };
  210. #endif
  211. AVCodec ff_sunrast_encoder = {
  212. .name = "sunrast",
  213. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  214. .type = AVMEDIA_TYPE_VIDEO,
  215. .id = AV_CODEC_ID_SUNRAST,
  216. .priv_data_size = sizeof(SUNRASTContext),
  217. .init = sunrast_encode_init,
  218. .encode2 = sunrast_encode_frame,
  219. #if FF_API_CODER_TYPE
  220. .defaults = sunrast_defaults,
  221. #endif
  222. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
  223. AV_PIX_FMT_PAL8,
  224. AV_PIX_FMT_GRAY8,
  225. AV_PIX_FMT_MONOWHITE,
  226. AV_PIX_FMT_NONE },
  227. };