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.

207 lines
6.1KB

  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 == RMT_RAW) {
  71. av_log_ask_for_sample(avctx, "unsupported colormap type\n");
  72. return AVERROR_PATCHWELCOME;
  73. }
  74. if (maptype > RMT_RAW) {
  75. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. switch (depth) {
  79. case 1:
  80. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  81. break;
  82. case 8:
  83. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  84. break;
  85. case 24:
  86. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  87. break;
  88. default:
  89. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  90. return AVERROR_INVALIDDATA;
  91. }
  92. if (p->data[0])
  93. avctx->release_buffer(avctx, p);
  94. if (w != avctx->width || h != avctx->height)
  95. avcodec_set_dimensions(avctx, w, h);
  96. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  97. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  98. return ret;
  99. }
  100. p->pict_type = AV_PICTURE_TYPE_I;
  101. if (buf_end - buf < maplength)
  102. return AVERROR_INVALIDDATA;
  103. if (depth != 8 && maplength) {
  104. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  105. } else if (maplength) {
  106. unsigned int len = maplength / 3;
  107. if (maplength % 3 || maplength > 768) {
  108. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  109. return AVERROR_INVALIDDATA;
  110. }
  111. ptr = p->data[1];
  112. for (x = 0; x < len; x++, ptr += 4)
  113. *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
  114. }
  115. buf += maplength;
  116. ptr = p->data[0];
  117. stride = p->linesize[0];
  118. /* scanlines are aligned on 16 bit boundaries */
  119. len = (depth * w + 7) >> 3;
  120. alen = len + (len & 1);
  121. if (type == RT_BYTE_ENCODED) {
  122. int value, run;
  123. uint8_t *end = ptr + h * stride;
  124. x = 0;
  125. while (ptr != end && buf < buf_end) {
  126. run = 1;
  127. if (buf_end - buf < 1)
  128. return AVERROR_INVALIDDATA;
  129. if ((value = *buf++) == RLE_TRIGGER) {
  130. run = *buf++ + 1;
  131. if (run != 1)
  132. value = *buf++;
  133. }
  134. while (run--) {
  135. if (x < len)
  136. ptr[x] = value;
  137. if (++x >= alen) {
  138. x = 0;
  139. ptr += stride;
  140. if (ptr == end)
  141. break;
  142. }
  143. }
  144. }
  145. } else {
  146. for (y = 0; y < h; y++) {
  147. if (buf_end - buf < len)
  148. break;
  149. memcpy(ptr, buf, len);
  150. ptr += stride;
  151. buf += alen;
  152. }
  153. }
  154. *picture = s->picture;
  155. *data_size = sizeof(AVFrame);
  156. return buf - bufstart;
  157. }
  158. static av_cold int sunrast_end(AVCodecContext *avctx) {
  159. SUNRASTContext *s = avctx->priv_data;
  160. if(s->picture.data[0])
  161. avctx->release_buffer(avctx, &s->picture);
  162. return 0;
  163. }
  164. AVCodec ff_sunrast_decoder = {
  165. .name = "sunrast",
  166. .type = AVMEDIA_TYPE_VIDEO,
  167. .id = CODEC_ID_SUNRAST,
  168. .priv_data_size = sizeof(SUNRASTContext),
  169. .init = sunrast_init,
  170. .close = sunrast_end,
  171. .decode = sunrast_decode_frame,
  172. .capabilities = CODEC_CAP_DR1,
  173. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  174. };