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.

225 lines
6.9KB

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