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.

239 lines
7.5KB

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