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.

203 lines
5.9KB

  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/intreadwrite.h"
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "sunrast.h"
  25. typedef struct SUNRASTContext {
  26. AVFrame picture;
  27. } SUNRASTContext;
  28. static av_cold int sunrast_init(AVCodecContext *avctx) {
  29. SUNRASTContext *s = avctx->priv_data;
  30. avcodec_get_frame_defaults(&s->picture);
  31. avctx->coded_frame = &s->picture;
  32. return 0;
  33. }
  34. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  35. int *data_size, AVPacket *avpkt) {
  36. const uint8_t *buf = avpkt->data;
  37. const uint8_t *buf_end = avpkt->data + avpkt->size;
  38. SUNRASTContext * const s = avctx->priv_data;
  39. AVFrame *picture = data;
  40. AVFrame * const p = &s->picture;
  41. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  42. uint8_t *ptr;
  43. const uint8_t *bufstart = buf;
  44. int ret;
  45. if (avpkt->size < 32)
  46. return AVERROR_INVALIDDATA;
  47. if (AV_RB32(buf) != RAS_MAGIC) {
  48. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  49. return AVERROR_INVALIDDATA;
  50. }
  51. w = AV_RB32(buf + 4);
  52. h = AV_RB32(buf + 8);
  53. depth = AV_RB32(buf + 12);
  54. type = AV_RB32(buf + 20);
  55. maptype = AV_RB32(buf + 24);
  56. maplength = AV_RB32(buf + 28);
  57. buf += 32;
  58. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
  59. av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
  60. return AVERROR_PATCHWELCOME;
  61. }
  62. if (type > RT_FORMAT_IFF) {
  63. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. if (av_image_check_size(w, h, 0, avctx)) {
  67. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. if (maptype & ~1) {
  71. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  72. return AVERROR_INVALIDDATA;
  73. }
  74. switch (depth) {
  75. case 1:
  76. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  77. break;
  78. case 8:
  79. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  80. break;
  81. case 24:
  82. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  83. break;
  84. default:
  85. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  86. return AVERROR_INVALIDDATA;
  87. }
  88. if (p->data[0])
  89. avctx->release_buffer(avctx, p);
  90. if (w != avctx->width || h != avctx->height)
  91. avcodec_set_dimensions(avctx, w, h);
  92. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  93. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  94. return ret;
  95. }
  96. p->pict_type = AV_PICTURE_TYPE_I;
  97. if (buf_end - buf < maplength)
  98. return AVERROR_INVALIDDATA;
  99. if (depth != 8 && maplength) {
  100. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  101. } else if (maplength) {
  102. unsigned int len = maplength / 3;
  103. if (maplength % 3 || maplength > 768) {
  104. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  105. return AVERROR_INVALIDDATA;
  106. }
  107. ptr = p->data[1];
  108. for (x = 0; x < len; x++, ptr += 4)
  109. *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
  110. }
  111. buf += maplength;
  112. ptr = p->data[0];
  113. stride = p->linesize[0];
  114. /* scanlines are aligned on 16 bit boundaries */
  115. len = (depth * w + 7) >> 3;
  116. alen = len + (len & 1);
  117. if (type == RT_BYTE_ENCODED) {
  118. int value, run;
  119. uint8_t *end = ptr + h * stride;
  120. x = 0;
  121. while (ptr != end && buf < buf_end) {
  122. run = 1;
  123. if (buf_end - buf < 1)
  124. return AVERROR_INVALIDDATA;
  125. if ((value = *buf++) == 0x80) {
  126. run = *buf++ + 1;
  127. if (run != 1)
  128. value = *buf++;
  129. }
  130. while (run--) {
  131. if (x < len)
  132. ptr[x] = value;
  133. if (++x >= alen) {
  134. x = 0;
  135. ptr += stride;
  136. if (ptr == end)
  137. break;
  138. }
  139. }
  140. }
  141. } else {
  142. for (y = 0; y < h; y++) {
  143. if (buf_end - buf < len)
  144. break;
  145. memcpy(ptr, buf, len);
  146. ptr += stride;
  147. buf += alen;
  148. }
  149. }
  150. *picture = s->picture;
  151. *data_size = sizeof(AVFrame);
  152. return buf - bufstart;
  153. }
  154. static av_cold int sunrast_end(AVCodecContext *avctx) {
  155. SUNRASTContext *s = avctx->priv_data;
  156. if(s->picture.data[0])
  157. avctx->release_buffer(avctx, &s->picture);
  158. return 0;
  159. }
  160. AVCodec ff_sunrast_decoder = {
  161. .name = "sunrast",
  162. .type = AVMEDIA_TYPE_VIDEO,
  163. .id = CODEC_ID_SUNRAST,
  164. .priv_data_size = sizeof(SUNRASTContext),
  165. .init = sunrast_init,
  166. .close = sunrast_end,
  167. .decode = sunrast_decode_frame,
  168. .capabilities = CODEC_CAP_DR1,
  169. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  170. };