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.

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