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.

263 lines
8.1KB

  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. AVClass *class;
  28. PutByteContext p;
  29. int depth; ///< depth of pixel
  30. int length; ///< length (bytes) of image
  31. int type; ///< type of file
  32. int maptype; ///< type of colormap
  33. int maplength; ///< length (bytes) of colormap
  34. int size;
  35. } SUNRASTContext;
  36. static void sunrast_image_write_header(AVCodecContext *avctx)
  37. {
  38. SUNRASTContext *s = avctx->priv_data;
  39. bytestream2_put_be32u(&s->p, RAS_MAGIC);
  40. bytestream2_put_be32u(&s->p, avctx->width);
  41. bytestream2_put_be32u(&s->p, avctx->height);
  42. bytestream2_put_be32u(&s->p, s->depth);
  43. bytestream2_put_be32u(&s->p, s->length);
  44. bytestream2_put_be32u(&s->p, s->type);
  45. bytestream2_put_be32u(&s->p, s->maptype);
  46. bytestream2_put_be32u(&s->p, s->maplength);
  47. }
  48. static void sunrast_image_write_image(AVCodecContext *avctx,
  49. const uint8_t *pixels,
  50. const uint32_t *palette_data,
  51. int linesize)
  52. {
  53. SUNRASTContext *s = avctx->priv_data;
  54. const uint8_t *ptr;
  55. int len, alen, x;
  56. if (s->maplength) { // palettized
  57. PutByteContext pb_r, pb_g;
  58. int len = s->maplength / 3;
  59. pb_r = s->p;
  60. bytestream2_skip_p(&s->p, len);
  61. pb_g = s->p;
  62. bytestream2_skip_p(&s->p, len);
  63. for (x = 0; x < len; x++) {
  64. uint32_t pixel = palette_data[x];
  65. bytestream2_put_byteu(&pb_r, (pixel >> 16) & 0xFF);
  66. bytestream2_put_byteu(&pb_g, (pixel >> 8) & 0xFF);
  67. bytestream2_put_byteu(&s->p, pixel & 0xFF);
  68. }
  69. }
  70. len = (s->depth * avctx->width + 7) >> 3;
  71. alen = len + (len & 1);
  72. ptr = pixels;
  73. if (s->type == RT_BYTE_ENCODED) {
  74. uint8_t value, value2;
  75. int run;
  76. const uint8_t *start = linesize < 0 ? pixels + (avctx->height - 1) * linesize
  77. : pixels;
  78. const uint8_t *end = linesize < 0 ? pixels - linesize
  79. : pixels + avctx->height * linesize;
  80. ptr = pixels;
  81. #define GET_VALUE ptr >= end || ptr < start ? 0 : x >= len ? ptr[len-1] : ptr[x]
  82. x = 0;
  83. value2 = GET_VALUE;
  84. while (ptr < end && ptr >= start) {
  85. run = 1;
  86. value = value2;
  87. x++;
  88. if (x >= alen) {
  89. x = 0;
  90. ptr += linesize;
  91. }
  92. value2 = GET_VALUE;
  93. while (value2 == value && run < 256 && ptr < end && ptr >= start) {
  94. x++;
  95. run++;
  96. if (x >= alen) {
  97. x = 0;
  98. ptr += linesize;
  99. }
  100. value2 = GET_VALUE;
  101. }
  102. if (run > 2 || value == RLE_TRIGGER) {
  103. bytestream2_put_byteu(&s->p, RLE_TRIGGER);
  104. bytestream2_put_byteu(&s->p, run - 1);
  105. if (run > 1)
  106. bytestream2_put_byteu(&s->p, value);
  107. } else if (run == 1) {
  108. bytestream2_put_byteu(&s->p, value);
  109. } else
  110. bytestream2_put_be16u(&s->p, (value << 8) | value);
  111. }
  112. // update data length for header
  113. s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
  114. } else {
  115. int y;
  116. for (y = 0; y < avctx->height; y++) {
  117. bytestream2_put_buffer(&s->p, ptr, len);
  118. if (len < alen)
  119. bytestream2_put_byteu(&s->p, 0);
  120. ptr += linesize;
  121. }
  122. }
  123. }
  124. static av_cold int sunrast_encode_init(AVCodecContext *avctx)
  125. {
  126. SUNRASTContext *s = avctx->priv_data;
  127. #if FF_API_CODER_TYPE
  128. FF_DISABLE_DEPRECATION_WARNINGS
  129. switch (avctx->coder_type) {
  130. case FF_CODER_TYPE_RLE:
  131. s->type = RT_BYTE_ENCODED;
  132. break;
  133. case FF_CODER_TYPE_RAW:
  134. s->type = RT_STANDARD;
  135. break;
  136. default:
  137. av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
  138. return AVERROR(EINVAL);
  139. }
  140. FF_ENABLE_DEPRECATION_WARNINGS
  141. if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
  142. #endif
  143. // adjust boolean option to RT equivalent
  144. s->type++;
  145. #if FF_API_CODED_FRAME
  146. FF_DISABLE_DEPRECATION_WARNINGS
  147. avctx->coded_frame->key_frame = 1;
  148. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  149. FF_ENABLE_DEPRECATION_WARNINGS
  150. #endif
  151. s->maptype = RMT_NONE;
  152. s->maplength = 0;
  153. switch (avctx->pix_fmt) {
  154. case AV_PIX_FMT_MONOWHITE:
  155. s->depth = 1;
  156. break;
  157. case AV_PIX_FMT_PAL8 :
  158. s->maptype = RMT_EQUAL_RGB;
  159. s->maplength = 3 * 256;
  160. /* fall-through */
  161. case AV_PIX_FMT_GRAY8:
  162. s->depth = 8;
  163. break;
  164. case AV_PIX_FMT_BGR24:
  165. s->depth = 24;
  166. break;
  167. default:
  168. return AVERROR_BUG;
  169. }
  170. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  171. s->size = 32 + s->maplength + s->length * s->type;
  172. return 0;
  173. }
  174. static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  175. const AVFrame *frame, int *got_packet_ptr)
  176. {
  177. SUNRASTContext *s = avctx->priv_data;
  178. int ret;
  179. if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
  180. return ret;
  181. bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
  182. sunrast_image_write_header(avctx);
  183. sunrast_image_write_image(avctx, frame->data[0],
  184. (const uint32_t *)frame->data[1],
  185. frame->linesize[0]);
  186. // update data length in header after RLE
  187. if (s->type == RT_BYTE_ENCODED)
  188. AV_WB32(&avpkt->data[16], s->length);
  189. *got_packet_ptr = 1;
  190. avpkt->flags |= AV_PKT_FLAG_KEY;
  191. avpkt->size = bytestream2_tell_p(&s->p);
  192. return 0;
  193. }
  194. #define OFFSET(x) offsetof(SUNRASTContext, x)
  195. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  196. static const AVOption options[] = {
  197. { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  198. { NULL },
  199. };
  200. static const AVClass sunrast_class = {
  201. .class_name = "sunrast",
  202. .item_name = av_default_item_name,
  203. .option = options,
  204. .version = LIBAVUTIL_VERSION_INT,
  205. };
  206. #if FF_API_CODER_TYPE
  207. static const AVCodecDefault sunrast_defaults[] = {
  208. { "coder", "rle" },
  209. { NULL },
  210. };
  211. #endif
  212. AVCodec ff_sunrast_encoder = {
  213. .name = "sunrast",
  214. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .id = AV_CODEC_ID_SUNRAST,
  217. .priv_data_size = sizeof(SUNRASTContext),
  218. .priv_class = &sunrast_class,
  219. .init = sunrast_encode_init,
  220. .encode2 = sunrast_encode_frame,
  221. #if FF_API_CODER_TYPE
  222. .defaults = sunrast_defaults,
  223. #endif
  224. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
  225. AV_PIX_FMT_PAL8,
  226. AV_PIX_FMT_GRAY8,
  227. AV_PIX_FMT_MONOWHITE,
  228. AV_PIX_FMT_NONE },
  229. };