sgienc.c 4.4 KB

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