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.

179 lines
5.4KB

  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 (av_image_check_size(w, h, 0, avctx)) {
  59. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  60. return AVERROR_INVALIDDATA;
  61. }
  62. if (maptype == RMT_RAW) {
  63. avpriv_request_sample(avctx, "Unknown colormap type");
  64. return AVERROR_PATCHWELCOME;
  65. }
  66. if (maptype > RMT_RAW) {
  67. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. switch (depth) {
  71. case 1:
  72. avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
  73. break;
  74. case 8:
  75. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  76. break;
  77. case 24:
  78. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  79. break;
  80. default:
  81. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  82. return AVERROR_INVALIDDATA;
  83. }
  84. if (w != avctx->width || h != avctx->height)
  85. avcodec_set_dimensions(avctx, w, h);
  86. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  87. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  88. return ret;
  89. }
  90. p->pict_type = AV_PICTURE_TYPE_I;
  91. if (buf_end - buf < maplength)
  92. return AVERROR_INVALIDDATA;
  93. if (depth != 8 && maplength) {
  94. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  95. } else if (maplength) {
  96. unsigned int len = maplength / 3;
  97. if (maplength % 3 || maplength > 768) {
  98. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  99. return AVERROR_INVALIDDATA;
  100. }
  101. ptr = p->data[1];
  102. for (x = 0; x < len; x++, ptr += 4)
  103. *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
  104. }
  105. buf += maplength;
  106. ptr = p->data[0];
  107. stride = p->linesize[0];
  108. /* scanlines are aligned on 16 bit boundaries */
  109. len = (depth * w + 7) >> 3;
  110. alen = len + (len & 1);
  111. if (type == RT_BYTE_ENCODED) {
  112. int value, run;
  113. uint8_t *end = ptr + h * stride;
  114. x = 0;
  115. while (ptr != end && buf < buf_end) {
  116. run = 1;
  117. if (buf_end - buf < 1)
  118. return AVERROR_INVALIDDATA;
  119. if ((value = *buf++) == RLE_TRIGGER) {
  120. run = *buf++ + 1;
  121. if (run != 1)
  122. value = *buf++;
  123. }
  124. while (run--) {
  125. if (x < len)
  126. ptr[x] = value;
  127. if (++x >= alen) {
  128. x = 0;
  129. ptr += stride;
  130. if (ptr == end)
  131. break;
  132. }
  133. }
  134. }
  135. } else {
  136. for (y = 0; y < h; y++) {
  137. if (buf_end - buf < len)
  138. break;
  139. memcpy(ptr, buf, len);
  140. ptr += stride;
  141. buf += alen;
  142. }
  143. }
  144. *got_frame = 1;
  145. return buf - bufstart;
  146. }
  147. AVCodec ff_sunrast_decoder = {
  148. .name = "sunrast",
  149. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  150. .type = AVMEDIA_TYPE_VIDEO,
  151. .id = AV_CODEC_ID_SUNRAST,
  152. .decode = sunrast_decode_frame,
  153. .capabilities = CODEC_CAP_DR1,
  154. };