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.

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