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.

230 lines
6.9KB

  1. /*
  2. * SGI image encoder
  3. * Todd Kirby <doubleshot@pacbell.net>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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,
  32. "Unsupported resolution %dx%d.\n", avctx->width, avctx->height);
  33. return AVERROR_INVALIDDATA;
  34. }
  35. avctx->coded_frame = av_frame_alloc();
  36. if (!avctx->coded_frame)
  37. return AVERROR(ENOMEM);
  38. return 0;
  39. }
  40. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  41. const AVFrame *frame, int *got_packet)
  42. {
  43. const AVFrame * const p = frame;
  44. uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
  45. int x, y, z, length, tablesize, ret;
  46. unsigned int width, height, depth, dimension;
  47. unsigned int bytes_per_channel, pixmax, put_be;
  48. unsigned char *end_buf;
  49. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  50. avctx->coded_frame->key_frame = 1;
  51. width = avctx->width;
  52. height = avctx->height;
  53. bytes_per_channel = 1;
  54. pixmax = 0xFF;
  55. put_be = HAVE_BIGENDIAN;
  56. switch (avctx->pix_fmt) {
  57. case AV_PIX_FMT_GRAY8:
  58. dimension = SGI_SINGLE_CHAN;
  59. depth = SGI_GRAYSCALE;
  60. break;
  61. case AV_PIX_FMT_RGB24:
  62. dimension = SGI_MULTI_CHAN;
  63. depth = SGI_RGB;
  64. break;
  65. case AV_PIX_FMT_RGBA:
  66. dimension = SGI_MULTI_CHAN;
  67. depth = SGI_RGBA;
  68. break;
  69. case AV_PIX_FMT_GRAY16LE:
  70. put_be = !HAVE_BIGENDIAN;
  71. case AV_PIX_FMT_GRAY16BE:
  72. avctx->coder_type = FF_CODER_TYPE_RAW;
  73. bytes_per_channel = 2;
  74. pixmax = 0xFFFF;
  75. dimension = SGI_SINGLE_CHAN;
  76. depth = SGI_GRAYSCALE;
  77. break;
  78. case AV_PIX_FMT_RGB48LE:
  79. put_be = !HAVE_BIGENDIAN;
  80. case AV_PIX_FMT_RGB48BE:
  81. avctx->coder_type = FF_CODER_TYPE_RAW;
  82. bytes_per_channel = 2;
  83. pixmax = 0xFFFF;
  84. dimension = SGI_MULTI_CHAN;
  85. depth = SGI_RGB;
  86. break;
  87. case AV_PIX_FMT_RGBA64LE:
  88. put_be = !HAVE_BIGENDIAN;
  89. case AV_PIX_FMT_RGBA64BE:
  90. avctx->coder_type = FF_CODER_TYPE_RAW;
  91. bytes_per_channel = 2;
  92. pixmax = 0xFFFF;
  93. dimension = SGI_MULTI_CHAN;
  94. depth = SGI_RGBA;
  95. break;
  96. default:
  97. return AVERROR_INVALIDDATA;
  98. }
  99. tablesize = depth * height * 4;
  100. length = SGI_HEADER_SIZE;
  101. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  102. length += depth * height * width;
  103. else // assume ff_rl_encode() produces at most 2x size of input
  104. length += tablesize * 2 + depth * height * (2 * width + 1);
  105. if ((ret = ff_alloc_packet(pkt, bytes_per_channel * length)) < 0) {
  106. av_log(avctx, AV_LOG_ERROR, "Error getting output packet of size %d.\n", length);
  107. return ret;
  108. }
  109. buf = pkt->data;
  110. end_buf = pkt->data + pkt->size;
  111. /* Encode header. */
  112. bytestream_put_be16(&buf, SGI_MAGIC);
  113. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  114. bytestream_put_byte(&buf, bytes_per_channel);
  115. bytestream_put_be16(&buf, dimension);
  116. bytestream_put_be16(&buf, width);
  117. bytestream_put_be16(&buf, height);
  118. bytestream_put_be16(&buf, depth);
  119. bytestream_put_be32(&buf, 0L); /* pixmin */
  120. bytestream_put_be32(&buf, pixmax);
  121. bytestream_put_be32(&buf, 0L); /* dummy */
  122. /* name */
  123. memset(buf, 0, SGI_HEADER_SIZE);
  124. buf += 80;
  125. /* colormap */
  126. bytestream_put_be32(&buf, 0L);
  127. /* The rest of the 512 byte header is unused. */
  128. buf += 404;
  129. offsettab = buf;
  130. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  131. /* Skip RLE offset table. */
  132. buf += tablesize;
  133. lengthtab = buf;
  134. /* Skip RLE length table. */
  135. buf += tablesize;
  136. /* Make an intermediate consecutive buffer. */
  137. if (!(encode_buf = av_malloc(width)))
  138. return -1;
  139. for (z = 0; z < depth; z++) {
  140. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  141. for (y = 0; y < height; y++) {
  142. bytestream_put_be32(&offsettab, buf - pkt->data);
  143. for (x = 0; x < width; x++)
  144. encode_buf[x] = in_buf[depth * x];
  145. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  146. av_free(encode_buf);
  147. return -1;
  148. }
  149. buf += length;
  150. bytestream_put_byte(&buf, 0);
  151. bytestream_put_be32(&lengthtab, length + 1);
  152. in_buf -= p->linesize[0];
  153. }
  154. }
  155. av_free(encode_buf);
  156. } else {
  157. for (z = 0; z < depth; z++) {
  158. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  159. for (y = 0; y < height; y++) {
  160. for (x = 0; x < width * depth; x += depth)
  161. if (bytes_per_channel == 1)
  162. bytestream_put_byte(&buf, in_buf[x]);
  163. else
  164. if (put_be)
  165. bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
  166. else
  167. bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
  168. in_buf -= p->linesize[0];
  169. }
  170. }
  171. }
  172. /* total length */
  173. pkt->size = buf - pkt->data;
  174. pkt->flags |= AV_PKT_FLAG_KEY;
  175. *got_packet = 1;
  176. return 0;
  177. }
  178. static av_cold int encode_close(AVCodecContext *avctx)
  179. {
  180. av_frame_free(&avctx->coded_frame);
  181. return 0;
  182. }
  183. AVCodec ff_sgi_encoder = {
  184. .name = "sgi",
  185. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .id = AV_CODEC_ID_SGI,
  188. .init = encode_init,
  189. .encode2 = encode_frame,
  190. .close = encode_close,
  191. .pix_fmts = (const enum AVPixelFormat[]) {
  192. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
  193. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
  194. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
  195. AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
  196. AV_PIX_FMT_NONE
  197. },
  198. };