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.

184 lines
5.4KB

  1. /*
  2. * SGI image encoder
  3. * Todd Kirby <doubleshot@pacbell.net>
  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 "sgi.h"
  25. #include "rle.h"
  26. #define SGI_SINGLE_CHAN 2
  27. #define SGI_MULTI_CHAN 3
  28. static av_cold int encode_init(AVCodecContext *avctx)
  29. {
  30. avctx->coded_frame = av_frame_alloc();
  31. if (!avctx->coded_frame)
  32. return AVERROR(ENOMEM);
  33. return 0;
  34. }
  35. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  36. const AVFrame *frame, int *got_packet)
  37. {
  38. const AVFrame * const p = frame;
  39. uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
  40. int x, y, z, length, tablesize, ret;
  41. unsigned int width, height, depth, dimension;
  42. unsigned char *end_buf;
  43. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  44. avctx->coded_frame->key_frame = 1;
  45. width = avctx->width;
  46. height = avctx->height;
  47. switch (avctx->pix_fmt) {
  48. case AV_PIX_FMT_GRAY8:
  49. dimension = SGI_SINGLE_CHAN;
  50. depth = SGI_GRAYSCALE;
  51. break;
  52. case AV_PIX_FMT_RGB24:
  53. dimension = SGI_MULTI_CHAN;
  54. depth = SGI_RGB;
  55. break;
  56. case AV_PIX_FMT_RGBA:
  57. dimension = SGI_MULTI_CHAN;
  58. depth = SGI_RGBA;
  59. break;
  60. default:
  61. return AVERROR_INVALIDDATA;
  62. }
  63. tablesize = depth * height * 4;
  64. length = SGI_HEADER_SIZE;
  65. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  66. length += depth * height * width;
  67. else // assume ff_rl_encode() produces at most 2x size of input
  68. length += tablesize * 2 + depth * height * (2 * width + 1);
  69. if ((ret = ff_alloc_packet(pkt, length)) < 0) {
  70. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
  71. return ret;
  72. }
  73. buf = pkt->data;
  74. end_buf = pkt->data + pkt->size;
  75. /* Encode header. */
  76. bytestream_put_be16(&buf, SGI_MAGIC);
  77. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  78. bytestream_put_byte(&buf, 1); /* bytes_per_channel */
  79. bytestream_put_be16(&buf, dimension);
  80. bytestream_put_be16(&buf, width);
  81. bytestream_put_be16(&buf, height);
  82. bytestream_put_be16(&buf, depth);
  83. /* The rest are constant in this implementation. */
  84. bytestream_put_be32(&buf, 0L); /* pixmin */
  85. bytestream_put_be32(&buf, 255L); /* pixmax */
  86. bytestream_put_be32(&buf, 0L); /* dummy */
  87. /* name */
  88. memset(buf, 0, SGI_HEADER_SIZE);
  89. buf += 80;
  90. /* colormap */
  91. bytestream_put_be32(&buf, 0L);
  92. /* The rest of the 512 byte header is unused. */
  93. buf += 404;
  94. offsettab = buf;
  95. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  96. /* Skip RLE offset table. */
  97. buf += tablesize;
  98. lengthtab = buf;
  99. /* Skip RLE length table. */
  100. buf += tablesize;
  101. /* Make an intermediate consecutive buffer. */
  102. if (!(encode_buf = av_malloc(width)))
  103. return -1;
  104. for (z = 0; z < depth; z++) {
  105. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  106. for (y = 0; y < height; y++) {
  107. bytestream_put_be32(&offsettab, buf - pkt->data);
  108. for (x = 0; x < width; x++)
  109. encode_buf[x] = in_buf[depth * x];
  110. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  111. av_free(encode_buf);
  112. return -1;
  113. }
  114. buf += length;
  115. bytestream_put_byte(&buf, 0);
  116. bytestream_put_be32(&lengthtab, length + 1);
  117. in_buf -= p->linesize[0];
  118. }
  119. }
  120. av_free(encode_buf);
  121. } else {
  122. for (z = 0; z < depth; z++) {
  123. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  124. for (y = 0; y < height; y++) {
  125. for (x = 0; x < width * depth; x += depth)
  126. bytestream_put_byte(&buf, in_buf[x]);
  127. in_buf -= p->linesize[0];
  128. }
  129. }
  130. }
  131. /* total length */
  132. pkt->size = buf - pkt->data;
  133. pkt->flags |= AV_PKT_FLAG_KEY;
  134. *got_packet = 1;
  135. return 0;
  136. }
  137. static av_cold int encode_close(AVCodecContext *avctx)
  138. {
  139. av_frame_free(&avctx->coded_frame);
  140. return 0;
  141. }
  142. AVCodec ff_sgi_encoder = {
  143. .name = "sgi",
  144. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  145. .type = AVMEDIA_TYPE_VIDEO,
  146. .id = AV_CODEC_ID_SGI,
  147. .init = encode_init,
  148. .encode2 = encode_frame,
  149. .close = encode_close,
  150. .pix_fmts = (const enum AVPixelFormat[]){
  151. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  152. },
  153. };