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.

262 lines
8.2KB

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