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.

221 lines
6.8KB

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