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