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.

252 lines
7.6KB

  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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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, y;
  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. ptr = pixels;
  77. #define GET_VALUE y >= avctx->height ? 0 : x >= len ? ptr[len-1] : ptr[x]
  78. x = 0, y = 0;
  79. value2 = GET_VALUE;
  80. while (y < avctx->height) {
  81. run = 1;
  82. value = value2;
  83. x++;
  84. if (x >= alen) {
  85. x = 0;
  86. ptr += linesize, y++;
  87. }
  88. value2 = GET_VALUE;
  89. while (value2 == value && run < 256 && y < avctx->height) {
  90. x++;
  91. run++;
  92. if (x >= alen) {
  93. x = 0;
  94. ptr += linesize, y++;
  95. }
  96. value2 = GET_VALUE;
  97. }
  98. if (run > 2 || value == RLE_TRIGGER) {
  99. bytestream2_put_byteu(&s->p, RLE_TRIGGER);
  100. bytestream2_put_byteu(&s->p, run - 1);
  101. if (run > 1)
  102. bytestream2_put_byteu(&s->p, value);
  103. } else if (run == 1) {
  104. bytestream2_put_byteu(&s->p, value);
  105. } else
  106. bytestream2_put_be16u(&s->p, (value << 8) | value);
  107. }
  108. // update data length for header
  109. s->length = bytestream2_tell_p(&s->p) - 32 - s->maplength;
  110. } else {
  111. for (y = 0; y < avctx->height; y++) {
  112. bytestream2_put_buffer(&s->p, ptr, len);
  113. if (len < alen)
  114. bytestream2_put_byteu(&s->p, 0);
  115. ptr += linesize;
  116. }
  117. }
  118. }
  119. static av_cold int sunrast_encode_init(AVCodecContext *avctx)
  120. {
  121. SUNRASTContext *s = avctx->priv_data;
  122. #if FF_API_CODER_TYPE
  123. FF_DISABLE_DEPRECATION_WARNINGS
  124. switch (avctx->coder_type) {
  125. case FF_CODER_TYPE_RLE:
  126. s->type = RT_BYTE_ENCODED;
  127. break;
  128. case FF_CODER_TYPE_RAW:
  129. s->type = RT_STANDARD;
  130. break;
  131. default:
  132. av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
  133. return AVERROR(EINVAL);
  134. }
  135. FF_ENABLE_DEPRECATION_WARNINGS
  136. if (s->type != RT_BYTE_ENCODED && s->type != RT_STANDARD)
  137. #endif
  138. // adjust boolean option to RT equivalent
  139. s->type++;
  140. s->maptype = RMT_NONE;
  141. s->maplength = 0;
  142. switch (avctx->pix_fmt) {
  143. case AV_PIX_FMT_MONOWHITE:
  144. s->depth = 1;
  145. break;
  146. case AV_PIX_FMT_PAL8 :
  147. s->maptype = RMT_EQUAL_RGB;
  148. s->maplength = 3 * 256;
  149. /* fall-through */
  150. case AV_PIX_FMT_GRAY8:
  151. s->depth = 8;
  152. break;
  153. case AV_PIX_FMT_BGR24:
  154. s->depth = 24;
  155. break;
  156. default:
  157. return AVERROR_BUG;
  158. }
  159. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  160. s->size = 32 + s->maplength + s->length * s->type;
  161. return 0;
  162. }
  163. static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  164. const AVFrame *frame, int *got_packet_ptr)
  165. {
  166. SUNRASTContext *s = avctx->priv_data;
  167. int ret;
  168. if ((ret = ff_alloc_packet2(avctx, avpkt, s->size, 0)) < 0)
  169. return ret;
  170. bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
  171. sunrast_image_write_header(avctx);
  172. sunrast_image_write_image(avctx, frame->data[0],
  173. (const uint32_t *)frame->data[1],
  174. frame->linesize[0]);
  175. // update data length in header after RLE
  176. if (s->type == RT_BYTE_ENCODED)
  177. AV_WB32(&avpkt->data[16], s->length);
  178. *got_packet_ptr = 1;
  179. avpkt->flags |= AV_PKT_FLAG_KEY;
  180. avpkt->size = bytestream2_tell_p(&s->p);
  181. return 0;
  182. }
  183. #define OFFSET(x) offsetof(SUNRASTContext, x)
  184. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  185. static const AVOption options[] = {
  186. { "rle", "Use run-length compression", OFFSET(type), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  187. { NULL },
  188. };
  189. static const AVClass sunrast_class = {
  190. .class_name = "sunrast",
  191. .item_name = av_default_item_name,
  192. .option = options,
  193. .version = LIBAVUTIL_VERSION_INT,
  194. };
  195. #if FF_API_CODER_TYPE
  196. static const AVCodecDefault sunrast_defaults[] = {
  197. { "coder", "rle" },
  198. { NULL },
  199. };
  200. #endif
  201. AVCodec ff_sunrast_encoder = {
  202. .name = "sunrast",
  203. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  204. .type = AVMEDIA_TYPE_VIDEO,
  205. .id = AV_CODEC_ID_SUNRAST,
  206. .priv_data_size = sizeof(SUNRASTContext),
  207. .init = sunrast_encode_init,
  208. .encode2 = sunrast_encode_frame,
  209. #if FF_API_CODER_TYPE
  210. .defaults = sunrast_defaults,
  211. #endif
  212. .priv_class = &sunrast_class,
  213. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
  214. AV_PIX_FMT_PAL8,
  215. AV_PIX_FMT_GRAY8,
  216. AV_PIX_FMT_MONOWHITE,
  217. AV_PIX_FMT_NONE },
  218. };