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.

230 lines
7.2KB

  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 *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. switch (avctx->coder_type) {
  127. case FF_CODER_TYPE_RLE:
  128. s->type = RT_BYTE_ENCODED;
  129. break;
  130. case FF_CODER_TYPE_RAW:
  131. s->type = RT_STANDARD;
  132. break;
  133. default:
  134. av_log(avctx, AV_LOG_ERROR, "invalid coder_type\n");
  135. return AVERROR(EINVAL);
  136. }
  137. avctx->coded_frame = &s->picture;
  138. avctx->coded_frame->key_frame = 1;
  139. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  140. s->maptype = RMT_NONE;
  141. s->maplength = 0;
  142. switch (avctx->pix_fmt) {
  143. case PIX_FMT_MONOWHITE:
  144. s->depth = 1;
  145. break;
  146. case PIX_FMT_PAL8 :
  147. s->maptype = RMT_EQUAL_RGB;
  148. s->maplength = 3 * 256;
  149. case PIX_FMT_GRAY8:
  150. s->depth = 8;
  151. break;
  152. case PIX_FMT_BGR24:
  153. s->depth = 24;
  154. break;
  155. default:
  156. return AVERROR_BUG;
  157. }
  158. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  159. s->size = 32 + s->maplength +
  160. s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
  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_packet(avpkt, s->size)) < 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. static const AVCodecDefault sunrast_defaults[] = {
  184. { "coder", "rle" },
  185. { NULL },
  186. };
  187. AVCodec ff_sunrast_encoder = {
  188. .name = "sunrast",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .id = CODEC_ID_SUNRAST,
  191. .priv_data_size = sizeof(SUNRASTContext),
  192. .init = sunrast_encode_init,
  193. .encode2 = sunrast_encode_frame,
  194. .defaults = sunrast_defaults,
  195. .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_BGR24,
  196. PIX_FMT_PAL8,
  197. PIX_FMT_GRAY8,
  198. PIX_FMT_MONOWHITE,
  199. PIX_FMT_NONE },
  200. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  201. };