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.

227 lines
7.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 "avcodec.h"
  22. #include "bytestream.h"
  23. #include "internal.h"
  24. #include "sunrast.h"
  25. typedef struct SUNRASTContext {
  26. AVFrame picture;
  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 *end = pixels + avctx->height * linesize;
  76. ptr = pixels;
  77. #define GET_VALUE ptr >= end ? 0 : x >= len ? ptr[len-1] : ptr[x]
  78. x = 0;
  79. value2 = GET_VALUE;
  80. while (ptr < end) {
  81. run = 1;
  82. value = value2;
  83. x++;
  84. if (x >= alen) {
  85. x = 0;
  86. ptr += linesize;
  87. }
  88. value2 = GET_VALUE;
  89. while (value2 == value && run < 256 && ptr < end) {
  90. x++;
  91. run++;
  92. if (x >= alen) {
  93. x = 0;
  94. ptr += linesize;
  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. int y;
  112. for (y = 0; y < avctx->height; y++) {
  113. bytestream2_put_buffer(&s->p, ptr, len);
  114. if (len < alen)
  115. bytestream2_put_byteu(&s->p, 0);
  116. ptr += linesize;
  117. }
  118. }
  119. }
  120. static av_cold int sunrast_encode_init(AVCodecContext *avctx)
  121. {
  122. SUNRASTContext *s = avctx->priv_data;
  123. switch (avctx->coder_type) {
  124. case FF_CODER_TYPE_RLE:
  125. s->type = RT_BYTE_ENCODED;
  126. break;
  127. case FF_CODER_TYPE_RAW:
  128. s->type = RT_STANDARD;
  129. break;
  130. default:
  131. av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
  132. return AVERROR(EINVAL);
  133. }
  134. avctx->coded_frame = &s->picture;
  135. avctx->coded_frame->key_frame = 1;
  136. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  137. s->maptype = RMT_NONE;
  138. s->maplength = 0;
  139. switch (avctx->pix_fmt) {
  140. case PIX_FMT_MONOWHITE:
  141. s->depth = 1;
  142. break;
  143. case PIX_FMT_PAL8 :
  144. s->maptype = RMT_EQUAL_RGB;
  145. s->maplength = 3 * 256;
  146. case PIX_FMT_GRAY8:
  147. s->depth = 8;
  148. break;
  149. case PIX_FMT_BGR24:
  150. s->depth = 24;
  151. break;
  152. default:
  153. return AVERROR_BUG;
  154. }
  155. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  156. s->size = 32 + s->maplength +
  157. s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
  158. return 0;
  159. }
  160. static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  161. const AVFrame *frame, int *got_packet_ptr)
  162. {
  163. SUNRASTContext *s = avctx->priv_data;
  164. int ret;
  165. if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
  166. return ret;
  167. bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
  168. sunrast_image_write_header(avctx);
  169. sunrast_image_write_image(avctx, frame->data[0],
  170. (const uint32_t *)frame->data[1],
  171. frame->linesize[0]);
  172. // update data length in header after RLE
  173. if (s->type == RT_BYTE_ENCODED)
  174. AV_WB32(&avpkt->data[16], s->length);
  175. *got_packet_ptr = 1;
  176. avpkt->flags |= AV_PKT_FLAG_KEY;
  177. avpkt->size = bytestream2_tell_p(&s->p);
  178. return 0;
  179. }
  180. static const AVCodecDefault sunrast_defaults[] = {
  181. { "coder", "rle" },
  182. { NULL },
  183. };
  184. AVCodec ff_sunrast_encoder = {
  185. .name = "sunrast",
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .id = CODEC_ID_SUNRAST,
  188. .priv_data_size = sizeof(SUNRASTContext),
  189. .init = sunrast_encode_init,
  190. .encode2 = sunrast_encode_frame,
  191. .defaults = sunrast_defaults,
  192. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_BGR24,
  193. PIX_FMT_PAL8,
  194. PIX_FMT_GRAY8,
  195. PIX_FMT_MONOWHITE,
  196. PIX_FMT_NONE },
  197. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  198. };