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.

177 lines
5.2KB

  1. /*
  2. * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
  3. * Copyright (c) 2007, 2008 Ivo van Poorten
  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/common.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. #include "sunrast.h"
  27. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  28. int *got_frame, AVPacket *avpkt)
  29. {
  30. const uint8_t *buf = avpkt->data;
  31. const uint8_t *buf_end = avpkt->data + avpkt->size;
  32. AVFrame * const p = data;
  33. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  34. uint8_t *ptr;
  35. const uint8_t *bufstart = buf;
  36. int ret;
  37. if (avpkt->size < 32)
  38. return AVERROR_INVALIDDATA;
  39. if (AV_RB32(buf) != RAS_MAGIC) {
  40. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  41. return AVERROR_INVALIDDATA;
  42. }
  43. w = AV_RB32(buf + 4);
  44. h = AV_RB32(buf + 8);
  45. depth = AV_RB32(buf + 12);
  46. type = AV_RB32(buf + 20);
  47. maptype = AV_RB32(buf + 24);
  48. maplength = AV_RB32(buf + 28);
  49. buf += 32;
  50. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
  51. avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
  52. return AVERROR_PATCHWELCOME;
  53. }
  54. if (type > RT_FORMAT_IFF) {
  55. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  56. return AVERROR_INVALIDDATA;
  57. }
  58. if (maptype == RMT_RAW) {
  59. avpriv_request_sample(avctx, "Unknown colormap type");
  60. return AVERROR_PATCHWELCOME;
  61. }
  62. if (maptype > RMT_RAW) {
  63. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. switch (depth) {
  67. case 1:
  68. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  69. break;
  70. case 8:
  71. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  72. break;
  73. case 24:
  74. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  75. break;
  76. default:
  77. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  78. return AVERROR_INVALIDDATA;
  79. }
  80. ret = ff_set_dimensions(avctx, w, h);
  81. if (ret < 0)
  82. return ret;
  83. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  84. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  85. return ret;
  86. }
  87. p->pict_type = AV_PICTURE_TYPE_I;
  88. if (buf_end - buf < maplength)
  89. return AVERROR_INVALIDDATA;
  90. if (depth != 8 && maplength) {
  91. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  92. } else if (maplength) {
  93. unsigned int len = maplength / 3;
  94. if (maplength % 3 || maplength > 768) {
  95. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. ptr = p->data[1];
  99. for (x = 0; x < len; x++, ptr += 4)
  100. *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
  101. }
  102. buf += maplength;
  103. ptr = p->data[0];
  104. stride = p->linesize[0];
  105. /* scanlines are aligned on 16 bit boundaries */
  106. len = (depth * w + 7) >> 3;
  107. alen = len + (len & 1);
  108. if (type == RT_BYTE_ENCODED) {
  109. int value, run;
  110. uint8_t *end = ptr + h * stride;
  111. x = 0;
  112. while (ptr != end && buf < buf_end) {
  113. run = 1;
  114. if (buf_end - buf < 1)
  115. return AVERROR_INVALIDDATA;
  116. if ((value = *buf++) == RLE_TRIGGER) {
  117. run = *buf++ + 1;
  118. if (run != 1)
  119. value = *buf++;
  120. }
  121. while (run--) {
  122. if (x < len)
  123. ptr[x] = value;
  124. if (++x >= alen) {
  125. x = 0;
  126. ptr += stride;
  127. if (ptr == end)
  128. break;
  129. }
  130. }
  131. }
  132. } else {
  133. for (y = 0; y < h; y++) {
  134. if (buf_end - buf < len)
  135. break;
  136. memcpy(ptr, buf, len);
  137. ptr += stride;
  138. buf += alen;
  139. }
  140. }
  141. *got_frame = 1;
  142. return buf - bufstart;
  143. }
  144. AVCodec ff_sunrast_decoder = {
  145. .name = "sunrast",
  146. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  147. .type = AVMEDIA_TYPE_VIDEO,
  148. .id = AV_CODEC_ID_SUNRAST,
  149. .decode = sunrast_decode_frame,
  150. .capabilities = AV_CODEC_CAP_DR1,
  151. };