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. 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. av_log(avctx, AV_LOG_ERROR, "SGI does not support resolutions above 65535x65535\n");
  34. return AVERROR_INVALIDDATA;
  35. }
  36. return 0;
  37. }
  38. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  39. const AVFrame *frame, int *got_packet)
  40. {
  41. const AVFrame * const p = frame;
  42. uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf, *buf;
  43. int x, y, z, length, tablesize, ret;
  44. unsigned int width, height, depth, dimension;
  45. unsigned int bytes_per_channel, pixmax, put_be;
  46. unsigned char *end_buf;
  47. #if FF_API_CODED_FRAME
  48. FF_DISABLE_DEPRECATION_WARNINGS
  49. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  50. avctx->coded_frame->key_frame = 1;
  51. FF_ENABLE_DEPRECATION_WARNINGS
  52. #endif
  53. width = avctx->width;
  54. height = avctx->height;
  55. bytes_per_channel = 1;
  56. pixmax = 0xFF;
  57. put_be = HAVE_BIGENDIAN;
  58. switch (avctx->pix_fmt) {
  59. case AV_PIX_FMT_GRAY8:
  60. dimension = SGI_SINGLE_CHAN;
  61. depth = SGI_GRAYSCALE;
  62. break;
  63. case AV_PIX_FMT_RGB24:
  64. dimension = SGI_MULTI_CHAN;
  65. depth = SGI_RGB;
  66. break;
  67. case AV_PIX_FMT_RGBA:
  68. dimension = SGI_MULTI_CHAN;
  69. depth = SGI_RGBA;
  70. break;
  71. case AV_PIX_FMT_GRAY16LE:
  72. put_be = !HAVE_BIGENDIAN;
  73. case AV_PIX_FMT_GRAY16BE:
  74. avctx->coder_type = FF_CODER_TYPE_RAW;
  75. bytes_per_channel = 2;
  76. pixmax = 0xFFFF;
  77. dimension = SGI_SINGLE_CHAN;
  78. depth = SGI_GRAYSCALE;
  79. break;
  80. case AV_PIX_FMT_RGB48LE:
  81. put_be = !HAVE_BIGENDIAN;
  82. case AV_PIX_FMT_RGB48BE:
  83. avctx->coder_type = FF_CODER_TYPE_RAW;
  84. bytes_per_channel = 2;
  85. pixmax = 0xFFFF;
  86. dimension = SGI_MULTI_CHAN;
  87. depth = SGI_RGB;
  88. break;
  89. case AV_PIX_FMT_RGBA64LE:
  90. put_be = !HAVE_BIGENDIAN;
  91. case AV_PIX_FMT_RGBA64BE:
  92. avctx->coder_type = FF_CODER_TYPE_RAW;
  93. bytes_per_channel = 2;
  94. pixmax = 0xFFFF;
  95. dimension = SGI_MULTI_CHAN;
  96. depth = SGI_RGBA;
  97. break;
  98. default:
  99. return AVERROR_INVALIDDATA;
  100. }
  101. tablesize = depth * height * 4;
  102. length = SGI_HEADER_SIZE;
  103. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  104. length += depth * height * width;
  105. else // assume ff_rl_encode() produces at most 2x size of input
  106. length += tablesize * 2 + depth * height * (2 * width + 1);
  107. if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
  108. return ret;
  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 AVERROR(ENOMEM);
  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. }
  169. }
  170. in_buf -= p->linesize[0];
  171. }
  172. }
  173. }
  174. /* total length */
  175. pkt->size = buf - pkt->data;
  176. pkt->flags |= AV_PKT_FLAG_KEY;
  177. *got_packet = 1;
  178. return 0;
  179. }
  180. AVCodec ff_sgi_encoder = {
  181. .name = "sgi",
  182. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  183. .type = AVMEDIA_TYPE_VIDEO,
  184. .id = AV_CODEC_ID_SGI,
  185. .init = encode_init,
  186. .encode2 = encode_frame,
  187. .pix_fmts = (const enum AVPixelFormat[]) {
  188. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
  189. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
  190. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
  191. AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
  192. AV_PIX_FMT_NONE
  193. },
  194. };