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.

224 lines
6.8KB

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