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.

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