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.

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