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.

227 lines
6.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 "internal.h"
  24. #include "sgi.h"
  25. #include "rle.h"
  26. #define SGI_SINGLE_CHAN 2
  27. #define SGI_MULTI_CHAN 3
  28. static av_cold int encode_init(AVCodecContext *avctx)
  29. {
  30. if (avctx->width > 65535 || avctx->height > 65535) {
  31. av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
  32. return -1;
  33. }
  34. avctx->coded_frame = av_frame_alloc();
  35. if (!avctx->coded_frame)
  36. return AVERROR(ENOMEM);
  37. return 0;
  38. }
  39. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  40. const AVFrame *frame, int *got_packet)
  41. {
  42. const AVFrame * const p = frame;
  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. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  48. avctx->coded_frame->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 AV_PIX_FMT_GRAY8:
  56. dimension = SGI_SINGLE_CHAN;
  57. depth = SGI_GRAYSCALE;
  58. break;
  59. case AV_PIX_FMT_RGB24:
  60. dimension = SGI_MULTI_CHAN;
  61. depth = SGI_RGB;
  62. break;
  63. case AV_PIX_FMT_RGBA:
  64. dimension = SGI_MULTI_CHAN;
  65. depth = SGI_RGBA;
  66. break;
  67. case AV_PIX_FMT_GRAY16LE:
  68. put_be = !HAVE_BIGENDIAN;
  69. case AV_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 AV_PIX_FMT_RGB48LE:
  77. put_be = !HAVE_BIGENDIAN;
  78. case AV_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 AV_PIX_FMT_RGBA64LE:
  86. put_be = !HAVE_BIGENDIAN;
  87. case AV_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 = SGI_HEADER_SIZE;
  99. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  100. length += depth * height * width;
  101. else // assume ff_rl_encode() produces at most 2x size of input
  102. length += tablesize * 2 + depth * height * (2 * width + 1);
  103. if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
  104. return ret;
  105. buf = pkt->data;
  106. end_buf = pkt->data + pkt->size;
  107. /* Encode header. */
  108. bytestream_put_be16(&buf, SGI_MAGIC);
  109. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  110. bytestream_put_byte(&buf, bytes_per_channel);
  111. bytestream_put_be16(&buf, dimension);
  112. bytestream_put_be16(&buf, width);
  113. bytestream_put_be16(&buf, height);
  114. bytestream_put_be16(&buf, depth);
  115. bytestream_put_be32(&buf, 0L); /* pixmin */
  116. bytestream_put_be32(&buf, pixmax);
  117. bytestream_put_be32(&buf, 0L); /* dummy */
  118. /* name */
  119. memset(buf, 0, SGI_HEADER_SIZE);
  120. buf += 80;
  121. /* colormap */
  122. bytestream_put_be32(&buf, 0L);
  123. /* The rest of the 512 byte header is unused. */
  124. buf += 404;
  125. offsettab = buf;
  126. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  127. /* Skip RLE offset table. */
  128. buf += tablesize;
  129. lengthtab = buf;
  130. /* Skip RLE length table. */
  131. buf += tablesize;
  132. /* Make an intermediate consecutive buffer. */
  133. if (!(encode_buf = av_malloc(width)))
  134. return -1;
  135. for (z = 0; z < depth; z++) {
  136. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  137. for (y = 0; y < height; y++) {
  138. bytestream_put_be32(&offsettab, buf - pkt->data);
  139. for (x = 0; x < width; x++)
  140. encode_buf[x] = in_buf[depth * x];
  141. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  142. av_free(encode_buf);
  143. return -1;
  144. }
  145. buf += length;
  146. bytestream_put_byte(&buf, 0);
  147. bytestream_put_be32(&lengthtab, length + 1);
  148. in_buf -= p->linesize[0];
  149. }
  150. }
  151. av_free(encode_buf);
  152. } else {
  153. for (z = 0; z < depth; z++) {
  154. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  155. for (y = 0; y < height; y++) {
  156. for (x = 0; x < width * depth; x += depth)
  157. if (bytes_per_channel == 1) {
  158. bytestream_put_byte(&buf, in_buf[x]);
  159. } else {
  160. if (put_be) {
  161. bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
  162. } else {
  163. bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
  164. }
  165. }
  166. in_buf -= p->linesize[0];
  167. }
  168. }
  169. }
  170. /* total length */
  171. pkt->size = buf - pkt->data;
  172. pkt->flags |= AV_PKT_FLAG_KEY;
  173. *got_packet = 1;
  174. return 0;
  175. }
  176. static av_cold int encode_close(AVCodecContext *avctx)
  177. {
  178. av_frame_free(&avctx->coded_frame);
  179. return 0;
  180. }
  181. AVCodec ff_sgi_encoder = {
  182. .name = "sgi",
  183. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  184. .type = AVMEDIA_TYPE_VIDEO,
  185. .id = AV_CODEC_ID_SGI,
  186. .init = encode_init,
  187. .encode2 = encode_frame,
  188. .close = encode_close,
  189. .pix_fmts = (const enum AVPixelFormat[]){
  190. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
  191. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
  192. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
  193. AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE,
  194. AV_PIX_FMT_GRAY8, AV_PIX_FMT_NONE
  195. },
  196. };