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.

261 lines
8.2KB

  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 sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
  39. int w, int bpp)
  40. {
  41. int val, count, x, start = bytestream2_tell_p(pbc);
  42. void (*bytestream2_put)(PutByteContext *, unsigned int);
  43. if (bpp == 1)
  44. bytestream2_put = bytestream2_put_byte;
  45. else
  46. bytestream2_put = bytestream2_put_be16;
  47. for (x = 0; x < w; x += count) {
  48. /* see if we can encode the next set of pixels with RLE */
  49. count = ff_rle_count_pixels(src, w - x, bpp, 1);
  50. if (count > 1) {
  51. if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
  52. return AVERROR_INVALIDDATA;
  53. val = bpp == 1 ? *src : AV_RB16(src);
  54. bytestream2_put(pbc, count);
  55. bytestream2_put(pbc, val);
  56. } else {
  57. int i;
  58. /* fall back on uncompressed */
  59. count = ff_rle_count_pixels(src, w - x, bpp, 0);
  60. if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
  61. return AVERROR_INVALIDDATA;
  62. bytestream2_put(pbc, count + 0x80);
  63. for (i = 0; i < count; i++) {
  64. val = bpp == 1 ? src[i] : AV_RB16(src + i * bpp);
  65. bytestream2_put(pbc, val);
  66. }
  67. }
  68. src += count * bpp;
  69. }
  70. return bytestream2_tell_p(pbc) - start;
  71. }
  72. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  73. const AVFrame *frame, int *got_packet)
  74. {
  75. const AVFrame * const p = frame;
  76. PutByteContext pbc;
  77. uint8_t *in_buf, *encode_buf;
  78. int x, y, z, length, tablesize, ret;
  79. unsigned int width, height, depth, dimension;
  80. unsigned int bytes_per_channel, pixmax, put_be;
  81. #if FF_API_CODED_FRAME
  82. FF_DISABLE_DEPRECATION_WARNINGS
  83. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  84. avctx->coded_frame->key_frame = 1;
  85. FF_ENABLE_DEPRECATION_WARNINGS
  86. #endif
  87. width = avctx->width;
  88. height = avctx->height;
  89. bytes_per_channel = 1;
  90. pixmax = 0xFF;
  91. put_be = HAVE_BIGENDIAN;
  92. switch (avctx->pix_fmt) {
  93. case AV_PIX_FMT_GRAY8:
  94. dimension = SGI_SINGLE_CHAN;
  95. depth = SGI_GRAYSCALE;
  96. break;
  97. case AV_PIX_FMT_RGB24:
  98. dimension = SGI_MULTI_CHAN;
  99. depth = SGI_RGB;
  100. break;
  101. case AV_PIX_FMT_RGBA:
  102. dimension = SGI_MULTI_CHAN;
  103. depth = SGI_RGBA;
  104. break;
  105. case AV_PIX_FMT_GRAY16LE:
  106. put_be = !HAVE_BIGENDIAN;
  107. case AV_PIX_FMT_GRAY16BE:
  108. bytes_per_channel = 2;
  109. pixmax = 0xFFFF;
  110. dimension = SGI_SINGLE_CHAN;
  111. depth = SGI_GRAYSCALE;
  112. break;
  113. case AV_PIX_FMT_RGB48LE:
  114. put_be = !HAVE_BIGENDIAN;
  115. case AV_PIX_FMT_RGB48BE:
  116. bytes_per_channel = 2;
  117. pixmax = 0xFFFF;
  118. dimension = SGI_MULTI_CHAN;
  119. depth = SGI_RGB;
  120. break;
  121. case AV_PIX_FMT_RGBA64LE:
  122. put_be = !HAVE_BIGENDIAN;
  123. case AV_PIX_FMT_RGBA64BE:
  124. bytes_per_channel = 2;
  125. pixmax = 0xFFFF;
  126. dimension = SGI_MULTI_CHAN;
  127. depth = SGI_RGBA;
  128. break;
  129. default:
  130. return AVERROR_INVALIDDATA;
  131. }
  132. tablesize = depth * height * 4;
  133. length = SGI_HEADER_SIZE;
  134. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  135. length += depth * height * width;
  136. else // assume sgi_rle_encode() produces at most 2x size of input
  137. length += tablesize * 2 + depth * height * (2 * width + 1);
  138. if ((ret = ff_alloc_packet2(avctx, pkt, bytes_per_channel * length, 0)) < 0)
  139. return ret;
  140. bytestream2_init_writer(&pbc, pkt->data, pkt->size);
  141. /* Encode header. */
  142. bytestream2_put_be16(&pbc, SGI_MAGIC);
  143. bytestream2_put_byte(&pbc, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0 */
  144. bytestream2_put_byte(&pbc, bytes_per_channel);
  145. bytestream2_put_be16(&pbc, dimension);
  146. bytestream2_put_be16(&pbc, width);
  147. bytestream2_put_be16(&pbc, height);
  148. bytestream2_put_be16(&pbc, depth);
  149. bytestream2_put_be32(&pbc, 0L); /* pixmin */
  150. bytestream2_put_be32(&pbc, pixmax);
  151. bytestream2_put_be32(&pbc, 0L); /* dummy */
  152. /* name */
  153. bytestream2_skip_p(&pbc, 80);
  154. /* colormap */
  155. bytestream2_put_be32(&pbc, 0L);
  156. /* The rest of the 512 byte header is unused. */
  157. bytestream2_skip_p(&pbc, 404);
  158. if (avctx->coder_type != FF_CODER_TYPE_RAW) {
  159. PutByteContext taboff_pcb, tablen_pcb;
  160. /* Skip RLE offset table. */
  161. bytestream2_init_writer(&taboff_pcb, pbc.buffer, tablesize);
  162. bytestream2_skip_p(&pbc, tablesize);
  163. /* Skip RLE length table. */
  164. bytestream2_init_writer(&tablen_pcb, pbc.buffer, tablesize);
  165. bytestream2_skip_p(&pbc, tablesize);
  166. /* Make an intermediate consecutive buffer. */
  167. if (!(encode_buf = av_malloc(width * bytes_per_channel)))
  168. return AVERROR(ENOMEM);
  169. for (z = 0; z < depth; z++) {
  170. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  171. for (y = 0; y < height; y++) {
  172. bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
  173. for (x = 0; x < width * bytes_per_channel; x += bytes_per_channel)
  174. encode_buf[x] = in_buf[depth * x];
  175. length = sgi_rle_encode(&pbc, encode_buf, width,
  176. bytes_per_channel);
  177. if (length < 1) {
  178. av_free(encode_buf);
  179. return AVERROR_INVALIDDATA;
  180. }
  181. bytestream2_put_be32(&tablen_pcb, length);
  182. in_buf -= p->linesize[0];
  183. }
  184. }
  185. av_free(encode_buf);
  186. } else {
  187. for (z = 0; z < depth; z++) {
  188. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z * bytes_per_channel;
  189. for (y = 0; y < height; y++) {
  190. for (x = 0; x < width * depth; x += depth)
  191. if (bytes_per_channel == 1)
  192. bytestream2_put_byte(&pbc, in_buf[x]);
  193. else
  194. if (put_be)
  195. bytestream2_put_be16(&pbc, ((uint16_t *)in_buf)[x]);
  196. else
  197. bytestream2_put_le16(&pbc, ((uint16_t *)in_buf)[x]);
  198. in_buf -= p->linesize[0];
  199. }
  200. }
  201. }
  202. /* total length */
  203. pkt->size = bytestream2_tell_p(&pbc);
  204. pkt->flags |= AV_PKT_FLAG_KEY;
  205. *got_packet = 1;
  206. return 0;
  207. }
  208. AVCodec ff_sgi_encoder = {
  209. .name = "sgi",
  210. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  211. .type = AVMEDIA_TYPE_VIDEO,
  212. .id = AV_CODEC_ID_SGI,
  213. .init = encode_init,
  214. .encode2 = encode_frame,
  215. .pix_fmts = (const enum AVPixelFormat[]) {
  216. AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA,
  217. AV_PIX_FMT_RGB48LE, AV_PIX_FMT_RGB48BE,
  218. AV_PIX_FMT_RGBA64LE, AV_PIX_FMT_RGBA64BE,
  219. AV_PIX_FMT_GRAY16LE, AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY8,
  220. AV_PIX_FMT_NONE
  221. },
  222. };