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.

297 lines
8.9KB

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