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.

213 lines
6.0KB

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