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.

213 lines
6.4KB

  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, bytes_per_channel, pixmax, put_be;
  45. unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
  46. *p = *(AVFrame*)data;
  47. p->pict_type = AV_PICTURE_TYPE_I;
  48. p->key_frame = 1;
  49. width = avctx->width;
  50. height = avctx->height;
  51. bytes_per_channel = 1;
  52. pixmax = 0xFF;
  53. put_be = HAVE_BIGENDIAN;
  54. switch (avctx->pix_fmt) {
  55. case PIX_FMT_GRAY8:
  56. dimension = SGI_SINGLE_CHAN;
  57. depth = SGI_GRAYSCALE;
  58. break;
  59. case PIX_FMT_RGB24:
  60. dimension = SGI_MULTI_CHAN;
  61. depth = SGI_RGB;
  62. break;
  63. case PIX_FMT_RGBA:
  64. dimension = SGI_MULTI_CHAN;
  65. depth = SGI_RGBA;
  66. break;
  67. case PIX_FMT_GRAY16LE:
  68. put_be = !HAVE_BIGENDIAN;
  69. case PIX_FMT_GRAY16BE:
  70. avctx->coder_type = FF_CODER_TYPE_RAW;
  71. bytes_per_channel = 2;
  72. pixmax = 0xFFFF;
  73. dimension = SGI_SINGLE_CHAN;
  74. depth = SGI_GRAYSCALE;
  75. break;
  76. case PIX_FMT_RGB48LE:
  77. put_be = !HAVE_BIGENDIAN;
  78. case PIX_FMT_RGB48BE:
  79. avctx->coder_type = FF_CODER_TYPE_RAW;
  80. bytes_per_channel = 2;
  81. pixmax = 0xFFFF;
  82. dimension = SGI_MULTI_CHAN;
  83. depth = SGI_RGB;
  84. break;
  85. case PIX_FMT_RGBA64LE:
  86. put_be = !HAVE_BIGENDIAN;
  87. case PIX_FMT_RGBA64BE:
  88. avctx->coder_type = FF_CODER_TYPE_RAW;
  89. bytes_per_channel = 2;
  90. pixmax = 0xFFFF;
  91. dimension = SGI_MULTI_CHAN;
  92. depth = SGI_RGBA;
  93. break;
  94. default:
  95. return AVERROR_INVALIDDATA;
  96. }
  97. tablesize = depth * height * 4;
  98. length = tablesize * 2 + SGI_HEADER_SIZE;
  99. if (buf_size < length) {
  100. av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
  101. return -1;
  102. }
  103. /* Encode header. */
  104. bytestream_put_be16(&buf, SGI_MAGIC);
  105. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  106. bytestream_put_byte(&buf, bytes_per_channel);
  107. bytestream_put_be16(&buf, dimension);
  108. bytestream_put_be16(&buf, width);
  109. bytestream_put_be16(&buf, height);
  110. bytestream_put_be16(&buf, depth);
  111. bytestream_put_be32(&buf, 0L); /* pixmin */
  112. bytestream_put_be32(&buf, pixmax);
  113. bytestream_put_be32(&buf, 0L); /* dummy */
  114. /* name */
  115. memset(buf, 0, SGI_HEADER_SIZE);
  116. buf += 80;
  117. /* colormap */
  118. bytestream_put_be32(&buf, 0L);
  119. /* The rest of the 512 byte header is unused. */
  120. buf += 404;
  121. offsettab = buf;
  122. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  123. /* Skip RLE offset table. */
  124. buf += tablesize;
  125. lengthtab = buf;
  126. /* Skip RLE length table. */
  127. buf += tablesize;
  128. /* Make an intermediate consecutive buffer. */
  129. if (!(encode_buf = av_malloc(width)))
  130. return -1;
  131. for (z = 0; z < depth; z++) {
  132. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  133. for (y = 0; y < height; y++) {
  134. bytestream_put_be32(&offsettab, buf - orig_buf);
  135. for (x = 0; x < width; x++)
  136. encode_buf[x] = in_buf[depth * x];
  137. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  138. av_free(encode_buf);
  139. return -1;
  140. }
  141. buf += length;
  142. bytestream_put_byte(&buf, 0);
  143. bytestream_put_be32(&lengthtab, length + 1);
  144. in_buf -= p->linesize[0];
  145. }
  146. }
  147. av_free(encode_buf);
  148. } else {
  149. for (z = 0; z < depth; z++) {
  150. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  151. for (y = 0; y < height; y++) {
  152. for (x = 0; x < width * depth; x += depth)
  153. if (bytes_per_channel == 1) {
  154. bytestream_put_byte(&buf, in_buf[x]);
  155. } else {
  156. if (put_be) {
  157. bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
  158. } else {
  159. bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
  160. }
  161. }
  162. in_buf -= p->linesize[0];
  163. }
  164. }
  165. }
  166. /* total length */
  167. return buf - orig_buf;
  168. }
  169. AVCodec ff_sgi_encoder = {
  170. .name = "sgi",
  171. .type = AVMEDIA_TYPE_VIDEO,
  172. .id = CODEC_ID_SGI,
  173. .priv_data_size = sizeof(SgiContext),
  174. .init = encode_init,
  175. .encode = encode_frame,
  176. .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA,
  177. PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
  178. PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
  179. PIX_FMT_GRAY16LE, PIX_FMT_GRAY16BE,
  180. PIX_FMT_GRAY8, PIX_FMT_NONE},
  181. .long_name= NULL_IF_CONFIG_SMALL("SGI image"),
  182. };