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.

173 lines
4.9KB

  1. /*
  2. * SGI image encoder
  3. * Todd Kirby <doubleshot@pacbell.net>
  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 "sgi.h"
  24. #include "rle.h"
  25. #define SGI_SINGLE_CHAN 2
  26. #define SGI_MULTI_CHAN 3
  27. typedef struct SgiContext {
  28. AVFrame picture;
  29. } SgiContext;
  30. static av_cold int encode_init(AVCodecContext *avctx)
  31. {
  32. SgiContext *s = avctx->priv_data;
  33. avcodec_get_frame_defaults(&s->picture);
  34. avctx->coded_frame = &s->picture;
  35. return 0;
  36. }
  37. static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
  38. int buf_size, void *data)
  39. {
  40. SgiContext *s = avctx->priv_data;
  41. AVFrame * const p = &s->picture;
  42. uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf;
  43. int x, y, z, length, tablesize;
  44. unsigned int width, height, depth, dimension;
  45. unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
  46. *p = *(AVFrame*)data;
  47. p->pict_type = FF_I_TYPE;
  48. p->key_frame = 1;
  49. width = avctx->width;
  50. height = avctx->height;
  51. switch (avctx->pix_fmt) {
  52. case PIX_FMT_GRAY8:
  53. dimension = SGI_SINGLE_CHAN;
  54. depth = SGI_GRAYSCALE;
  55. break;
  56. case PIX_FMT_RGB24:
  57. dimension = SGI_MULTI_CHAN;
  58. depth = SGI_RGB;
  59. break;
  60. case PIX_FMT_RGBA:
  61. dimension = SGI_MULTI_CHAN;
  62. depth = SGI_RGBA;
  63. break;
  64. default:
  65. return AVERROR_INVALIDDATA;
  66. }
  67. tablesize = depth * height * 4;
  68. length = tablesize * 2 + SGI_HEADER_SIZE;
  69. if (buf_size < length) {
  70. av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
  71. return -1;
  72. }
  73. /* Encode header. */
  74. bytestream_put_be16(&buf, SGI_MAGIC);
  75. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  76. bytestream_put_byte(&buf, 1); /* bytes_per_channel */
  77. bytestream_put_be16(&buf, dimension);
  78. bytestream_put_be16(&buf, width);
  79. bytestream_put_be16(&buf, height);
  80. bytestream_put_be16(&buf, depth);
  81. /* The rest are constant in this implementation. */
  82. bytestream_put_be32(&buf, 0L); /* pixmin */
  83. bytestream_put_be32(&buf, 255L); /* pixmax */
  84. bytestream_put_be32(&buf, 0L); /* dummy */
  85. /* name */
  86. memset(buf, 0, SGI_HEADER_SIZE);
  87. buf += 80;
  88. /* colormap */
  89. bytestream_put_be32(&buf, 0L);
  90. /* The rest of the 512 byte header is unused. */
  91. buf += 404;
  92. offsettab = buf;
  93. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  94. /* Skip RLE offset table. */
  95. buf += tablesize;
  96. lengthtab = buf;
  97. /* Skip RLE length table. */
  98. buf += tablesize;
  99. /* Make an intermediate consecutive buffer. */
  100. if (!(encode_buf = av_malloc(width)))
  101. return -1;
  102. for (z = 0; z < depth; z++) {
  103. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  104. for (y = 0; y < height; y++) {
  105. bytestream_put_be32(&offsettab, buf - orig_buf);
  106. for (x = 0; x < width; x++)
  107. encode_buf[x] = in_buf[depth * x];
  108. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  109. av_free(encode_buf);
  110. return -1;
  111. }
  112. buf += length;
  113. bytestream_put_byte(&buf, 0);
  114. bytestream_put_be32(&lengthtab, length + 1);
  115. in_buf -= p->linesize[0];
  116. }
  117. }
  118. av_free(encode_buf);
  119. } else {
  120. for (z = 0; z < depth; z++) {
  121. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  122. for (y = 0; y < height; y++) {
  123. for (x = 0; x < width * depth; x += depth)
  124. bytestream_put_byte(&buf, in_buf[x]);
  125. in_buf -= p->linesize[0];
  126. }
  127. }
  128. }
  129. /* total length */
  130. return buf - orig_buf;
  131. }
  132. AVCodec sgi_encoder = {
  133. "sgi",
  134. CODEC_TYPE_VIDEO,
  135. CODEC_ID_SGI,
  136. sizeof(SgiContext),
  137. encode_init,
  138. encode_frame,
  139. NULL,
  140. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE},
  141. .long_name= NULL_IF_CONFIG_SMALL("SGI image"),
  142. };