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.6KB

  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. avcodec_get_frame_defaults(&s->picture);
  35. avctx->coded_frame = &s->picture;
  36. return 0;
  37. }
  38. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  39. const AVFrame *frame, int *got_packet)
  40. {
  41. SgiContext *s = avctx->priv_data;
  42. AVFrame * const p = &s->picture;
  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. *p = *frame;
  48. p->pict_type = AV_PICTURE_TYPE_I;
  49. p->key_frame = 1;
  50. width = avctx->width;
  51. height = avctx->height;
  52. bytes_per_channel = 1;
  53. pixmax = 0xFF;
  54. put_be = HAVE_BIGENDIAN;
  55. switch (avctx->pix_fmt) {
  56. case PIX_FMT_GRAY8:
  57. dimension = SGI_SINGLE_CHAN;
  58. depth = SGI_GRAYSCALE;
  59. break;
  60. case PIX_FMT_RGB24:
  61. dimension = SGI_MULTI_CHAN;
  62. depth = SGI_RGB;
  63. break;
  64. case PIX_FMT_RGBA:
  65. dimension = SGI_MULTI_CHAN;
  66. depth = SGI_RGBA;
  67. break;
  68. case PIX_FMT_GRAY16LE:
  69. put_be = !HAVE_BIGENDIAN;
  70. case PIX_FMT_GRAY16BE:
  71. avctx->coder_type = FF_CODER_TYPE_RAW;
  72. bytes_per_channel = 2;
  73. pixmax = 0xFFFF;
  74. dimension = SGI_SINGLE_CHAN;
  75. depth = SGI_GRAYSCALE;
  76. break;
  77. case PIX_FMT_RGB48LE:
  78. put_be = !HAVE_BIGENDIAN;
  79. case PIX_FMT_RGB48BE:
  80. avctx->coder_type = FF_CODER_TYPE_RAW;
  81. bytes_per_channel = 2;
  82. pixmax = 0xFFFF;
  83. dimension = SGI_MULTI_CHAN;
  84. depth = SGI_RGB;
  85. break;
  86. case PIX_FMT_RGBA64LE:
  87. put_be = !HAVE_BIGENDIAN;
  88. case PIX_FMT_RGBA64BE:
  89. avctx->coder_type = FF_CODER_TYPE_RAW;
  90. bytes_per_channel = 2;
  91. pixmax = 0xFFFF;
  92. dimension = SGI_MULTI_CHAN;
  93. depth = SGI_RGBA;
  94. break;
  95. default:
  96. return AVERROR_INVALIDDATA;
  97. }
  98. tablesize = depth * height * 4;
  99. length = SGI_HEADER_SIZE;
  100. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  101. length += depth * height * width;
  102. else // assume ff_rl_encode() produces at most 2x size of input
  103. length += tablesize * 2 + depth * height * (2 * width + 1);
  104. if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length)) < 0)
  105. return ret;
  106. buf = pkt->data;
  107. end_buf = pkt->data + pkt->size;
  108. /* Encode header. */
  109. bytestream_put_be16(&buf, SGI_MAGIC);
  110. bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
  111. bytestream_put_byte(&buf, bytes_per_channel);
  112. bytestream_put_be16(&buf, dimension);
  113. bytestream_put_be16(&buf, width);
  114. bytestream_put_be16(&buf, height);
  115. bytestream_put_be16(&buf, depth);
  116. bytestream_put_be32(&buf, 0L); /* pixmin */
  117. bytestream_put_be32(&buf, pixmax);
  118. bytestream_put_be32(&buf, 0L); /* dummy */
  119. /* name */
  120. memset(buf, 0, SGI_HEADER_SIZE);
  121. buf += 80;
  122. /* colormap */
  123. bytestream_put_be32(&buf, 0L);
  124. /* The rest of the 512 byte header is unused. */
  125. buf += 404;
  126. offsettab = buf;
  127. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  128. /* Skip RLE offset table. */
  129. buf += tablesize;
  130. lengthtab = buf;
  131. /* Skip RLE length table. */
  132. buf += tablesize;
  133. /* Make an intermediate consecutive buffer. */
  134. if (!(encode_buf = av_malloc(width)))
  135. return -1;
  136. for (z = 0; z < depth; z++) {
  137. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  138. for (y = 0; y < height; y++) {
  139. bytestream_put_be32(&offsettab, buf - pkt->data);
  140. for (x = 0; x < width; x++)
  141. encode_buf[x] = in_buf[depth * x];
  142. if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
  143. av_free(encode_buf);
  144. return -1;
  145. }
  146. buf += length;
  147. bytestream_put_byte(&buf, 0);
  148. bytestream_put_be32(&lengthtab, length + 1);
  149. in_buf -= p->linesize[0];
  150. }
  151. }
  152. av_free(encode_buf);
  153. } else {
  154. for (z = 0; z < depth; z++) {
  155. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  156. for (y = 0; y < height; y++) {
  157. for (x = 0; x < width * depth; x += depth)
  158. if (bytes_per_channel == 1) {
  159. bytestream_put_byte(&buf, in_buf[x]);
  160. } else {
  161. if (put_be) {
  162. bytestream_put_be16(&buf, ((uint16_t *)in_buf)[x]);
  163. } else {
  164. bytestream_put_le16(&buf, ((uint16_t *)in_buf)[x]);
  165. }
  166. }
  167. in_buf -= p->linesize[0];
  168. }
  169. }
  170. }
  171. /* total length */
  172. pkt->size = buf - pkt->data;
  173. pkt->flags |= AV_PKT_FLAG_KEY;
  174. *got_packet = 1;
  175. return 0;
  176. }
  177. AVCodec ff_sgi_encoder = {
  178. .name = "sgi",
  179. .type = AVMEDIA_TYPE_VIDEO,
  180. .id = CODEC_ID_SGI,
  181. .priv_data_size = sizeof(SgiContext),
  182. .init = encode_init,
  183. .encode2 = encode_frame,
  184. .pix_fmts = (const enum PixelFormat[]){
  185. PIX_FMT_RGB24, PIX_FMT_RGBA,
  186. PIX_FMT_RGB48LE, PIX_FMT_RGB48BE,
  187. PIX_FMT_RGBA64LE, PIX_FMT_RGBA64BE,
  188. PIX_FMT_GRAY16LE, PIX_FMT_GRAY16BE,
  189. PIX_FMT_GRAY8, PIX_FMT_NONE
  190. },
  191. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  192. };