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.

225 lines
7.0KB

  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. PutByteContext pbc;
  43. uint8_t *in_buf, *encode_buf;
  44. int x, y, z, length, tablesize, ret;
  45. unsigned int width, height, depth, dimension;
  46. unsigned int bytes_per_channel, pixmax, put_be;
  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. bytestream2_init_writer(&pbc, pkt->data, pkt->size);
  110. /* Encode header. */
  111. bytestream2_put_be16(&pbc, SGI_MAGIC);
  112. bytestream2_put_byte(&pbc, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0 */
  113. bytestream2_put_byte(&pbc, bytes_per_channel);
  114. bytestream2_put_be16(&pbc, dimension);
  115. bytestream2_put_be16(&pbc, width);
  116. bytestream2_put_be16(&pbc, height);
  117. bytestream2_put_be16(&pbc, depth);
  118. bytestream2_put_be32(&pbc, 0L); /* pixmin */
  119. bytestream2_put_be32(&pbc, pixmax);
  120. bytestream2_put_be32(&pbc, 0L); /* dummy */
  121. /* name */
  122. bytestream2_skip_p(&pbc, 80);
  123. /* colormap */
  124. bytestream2_put_be32(&pbc, 0L);
  125. /* The rest of the 512 byte header is unused. */
  126. bytestream2_skip_p(&pbc, 404);
  127. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  128. PutByteContext taboff_pcb, tablen_pcb;
  129. /* Skip RLE offset table. */
  130. bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
  131. bytestream2_skip_p(&pbc, tablesize);
  132. /* Skip RLE length table. */
  133. bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
  134. bytestream2_skip_p(&pbc, tablesize);
  135. /* Make an intermediate consecutive buffer. */
  136. if (!(encode_buf = av_malloc(width)))
  137. return AVERROR(ENOMEM);
  138. for (z = 0; z < depth; z++) {
  139. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  140. for (y = 0; y < height; y++) {
  141. bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
  142. for (x = 0; x < width; x++)
  143. encode_buf[x] = in_buf[depth * x];
  144. length = ff_rle_encode(pbc.buffer,
  145. bytestream2_get_bytes_left_p(&pbc),
  146. encode_buf, 1, width, 0, 0, 0x80, 0);
  147. if (length < 1) {
  148. av_free(encode_buf);
  149. return -1;
  150. }
  151. bytestream2_skip_p(&pbc, length);
  152. bytestream2_put_be32(&tablen_pcb, length);
  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. bytestream2_put_byte(&pbc, in_buf[x]);
  164. else
  165. if (put_be)
  166. bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
  167. else
  168. bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
  169. in_buf -= p->linesize[0];
  170. }
  171. }
  172. }
  173. /* total length */
  174. pkt->size = bytestream2_tell_p(&pbc);
  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. };