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

  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. bytestream2_put = bytestream2_put_byte;
  43. for (x = 0; x < w; x += count) {
  44. /* see if we can encode the next set of pixels with RLE */
  45. count = ff_rle_count_pixels(src, w - x, bpp, 1);
  46. if (count > 1) {
  47. if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
  48. return AVERROR_INVALIDDATA;
  49. val = *src;
  50. bytestream2_put(pbc, count);
  51. bytestream2_put(pbc, val);
  52. } else {
  53. int i;
  54. /* fall back on uncompressed */
  55. count = ff_rle_count_pixels(src, w - x, bpp, 0);
  56. if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
  57. return AVERROR_INVALIDDATA;
  58. bytestream2_put(pbc, count + 0x80);
  59. for (i = 0; i < count; i++) {
  60. val = src[i];
  61. bytestream2_put(pbc, val);
  62. }
  63. }
  64. src += count * bpp;
  65. }
  66. return bytestream2_tell_p(pbc) - start;
  67. }
  68. static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  69. const AVFrame *frame, int *got_packet)
  70. {
  71. const AVFrame * const p = frame;
  72. PutByteContext pbc;
  73. uint8_t *in_buf, *encode_buf;
  74. int x, y, z, length, tablesize, ret;
  75. unsigned int width, height, depth, dimension;
  76. unsigned int bytes_per_channel, pixmax, put_be;
  77. #if FF_API_CODED_FRAME
  78. FF_DISABLE_DEPRECATION_WARNINGS
  79. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  80. avctx->coded_frame->key_frame = 1;
  81. FF_ENABLE_DEPRECATION_WARNINGS
  82. #endif
  83. width = avctx->width;
  84. height = avctx->height;
  85. bytes_per_channel = 1;
  86. pixmax = 0xFF;
  87. put_be = HAVE_BIGENDIAN;
  88. switch (avctx->pix_fmt) {
  89. case AV_PIX_FMT_GRAY8:
  90. dimension = SGI_SINGLE_CHAN;
  91. depth = SGI_GRAYSCALE;
  92. break;
  93. case AV_PIX_FMT_RGB24:
  94. dimension = SGI_MULTI_CHAN;
  95. depth = SGI_RGB;
  96. break;
  97. case AV_PIX_FMT_RGBA:
  98. dimension = SGI_MULTI_CHAN;
  99. depth = SGI_RGBA;
  100. break;
  101. case AV_PIX_FMT_GRAY16LE:
  102. put_be = !HAVE_BIGENDIAN;
  103. case AV_PIX_FMT_GRAY16BE:
  104. avctx->coder_type = FF_CODER_TYPE_RAW;
  105. bytes_per_channel = 2;
  106. pixmax = 0xFFFF;
  107. dimension = SGI_SINGLE_CHAN;
  108. depth = SGI_GRAYSCALE;
  109. break;
  110. case AV_PIX_FMT_RGB48LE:
  111. put_be = !HAVE_BIGENDIAN;
  112. case AV_PIX_FMT_RGB48BE:
  113. avctx->coder_type = FF_CODER_TYPE_RAW;
  114. bytes_per_channel = 2;
  115. pixmax = 0xFFFF;
  116. dimension = SGI_MULTI_CHAN;
  117. depth = SGI_RGB;
  118. break;
  119. case AV_PIX_FMT_RGBA64LE:
  120. put_be = !HAVE_BIGENDIAN;
  121. case AV_PIX_FMT_RGBA64BE:
  122. avctx->coder_type = FF_CODER_TYPE_RAW;
  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)))
  169. return -1;
  170. for (z = 0; z < depth; z++) {
  171. in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
  172. for (y = 0; y < height; y++) {
  173. bytestream2_put_be32(&taboff_pcb, bytestream2_tell_p(&pbc));
  174. for (x = 0; x < width; x++)
  175. encode_buf[x] = in_buf[depth * x];
  176. length = sgi_rle_encode(&pbc, encode_buf, width, 1);
  177. if (length < 1) {
  178. av_free(encode_buf);
  179. return -1;
  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. };