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.

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