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.

233 lines
7.3KB

  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. #if FF_API_CODED_FRAME
  137. FF_DISABLE_DEPRECATION_WARNINGS
  138. avctx->coded_frame->key_frame = 1;
  139. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  140. FF_ENABLE_DEPRECATION_WARNINGS
  141. #endif
  142. s->maptype = RMT_NONE;
  143. s->maplength = 0;
  144. switch (avctx->pix_fmt) {
  145. case AV_PIX_FMT_MONOWHITE:
  146. s->depth = 1;
  147. break;
  148. case AV_PIX_FMT_PAL8 :
  149. s->maptype = RMT_EQUAL_RGB;
  150. s->maplength = 3 * 256;
  151. /* fall-through */
  152. case AV_PIX_FMT_GRAY8:
  153. s->depth = 8;
  154. break;
  155. case AV_PIX_FMT_BGR24:
  156. s->depth = 24;
  157. break;
  158. default:
  159. return AVERROR_BUG;
  160. }
  161. s->length = avctx->height * (FFALIGN(avctx->width * s->depth, 16) >> 3);
  162. s->size = 32 + s->maplength +
  163. s->length * (s->type == RT_BYTE_ENCODED ? 2 : 1);
  164. return 0;
  165. }
  166. static int sunrast_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
  167. const AVFrame *frame, int *got_packet_ptr)
  168. {
  169. SUNRASTContext *s = avctx->priv_data;
  170. int ret;
  171. if ((ret = ff_alloc_packet(avpkt, s->size)) < 0)
  172. return ret;
  173. bytestream2_init_writer(&s->p, avpkt->data, avpkt->size);
  174. sunrast_image_write_header(avctx);
  175. sunrast_image_write_image(avctx, frame->data[0],
  176. (const uint32_t *)frame->data[1],
  177. frame->linesize[0]);
  178. // update data length in header after RLE
  179. if (s->type == RT_BYTE_ENCODED)
  180. AV_WB32(&avpkt->data[16], s->length);
  181. *got_packet_ptr = 1;
  182. avpkt->flags |= AV_PKT_FLAG_KEY;
  183. avpkt->size = bytestream2_tell_p(&s->p);
  184. return 0;
  185. }
  186. static const AVCodecDefault sunrast_defaults[] = {
  187. { "coder", "rle" },
  188. { NULL },
  189. };
  190. AVCodec ff_sunrast_encoder = {
  191. .name = "sunrast",
  192. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  193. .type = AVMEDIA_TYPE_VIDEO,
  194. .id = AV_CODEC_ID_SUNRAST,
  195. .priv_data_size = sizeof(SUNRASTContext),
  196. .init = sunrast_encode_init,
  197. .encode2 = sunrast_encode_frame,
  198. .defaults = sunrast_defaults,
  199. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_BGR24,
  200. AV_PIX_FMT_PAL8,
  201. AV_PIX_FMT_GRAY8,
  202. AV_PIX_FMT_MONOWHITE,
  203. AV_PIX_FMT_NONE },
  204. };