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.

223 lines
6.8KB

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