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.

231 lines
6.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 RAS_MAGIC 0x59a66a95
  25. /* The Old and Standard format types indicate that the image data is
  26. * uncompressed. There is no difference between the two formats. */
  27. #define RT_OLD 0
  28. #define RT_STANDARD 1
  29. /* The Byte-Encoded format type indicates that the image data is compressed
  30. * using a run-length encoding scheme. */
  31. #define RT_BYTE_ENCODED 2
  32. /* The RGB format type indicates that the image is uncompressed with reverse
  33. * component order from Old and Standard (RGB vs BGR). */
  34. #define RT_FORMAT_RGB 3
  35. /* The TIFF and IFF format types indicate that the raster file was originally
  36. * converted from either of these file formats. We do not have any samples or
  37. * documentation of the format details. */
  38. #define RT_FORMAT_TIFF 4
  39. #define RT_FORMAT_IFF 5
  40. /* The Experimental format type is implementation-specific and is generally an
  41. * indication that the image file does not conform to the Sun Raster file
  42. * format specification. */
  43. #define RT_EXPERIMENTAL 0xffff
  44. typedef struct SUNRASTContext {
  45. AVFrame picture;
  46. } SUNRASTContext;
  47. static av_cold int sunrast_init(AVCodecContext *avctx) {
  48. SUNRASTContext *s = avctx->priv_data;
  49. avcodec_get_frame_defaults(&s->picture);
  50. avctx->coded_frame = &s->picture;
  51. return 0;
  52. }
  53. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  54. int *data_size, AVPacket *avpkt) {
  55. const uint8_t *buf = avpkt->data;
  56. const uint8_t *buf_end = avpkt->data + avpkt->size;
  57. SUNRASTContext * const s = avctx->priv_data;
  58. AVFrame *picture = data;
  59. AVFrame * const p = &s->picture;
  60. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  61. uint8_t *ptr;
  62. const uint8_t *bufstart = buf;
  63. if (avpkt->size < 32)
  64. return AVERROR_INVALIDDATA;
  65. if (AV_RB32(buf) != RAS_MAGIC) {
  66. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  67. return -1;
  68. }
  69. w = AV_RB32(buf + 4);
  70. h = AV_RB32(buf + 8);
  71. depth = AV_RB32(buf + 12);
  72. type = AV_RB32(buf + 20);
  73. maptype = AV_RB32(buf + 24);
  74. maplength = AV_RB32(buf + 28);
  75. buf += 32;
  76. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
  77. av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
  78. return AVERROR_PATCHWELCOME;
  79. }
  80. if (type > RT_FORMAT_IFF) {
  81. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  82. return -1;
  83. }
  84. if (av_image_check_size(w, h, 0, avctx)) {
  85. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  86. return -1;
  87. }
  88. if (maptype & ~1) {
  89. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  90. return -1;
  91. }
  92. switch (depth) {
  93. case 1:
  94. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  95. break;
  96. case 8:
  97. avctx->pix_fmt = PIX_FMT_PAL8;
  98. break;
  99. case 24:
  100. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  101. break;
  102. default:
  103. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  104. return -1;
  105. }
  106. if (p->data[0])
  107. avctx->release_buffer(avctx, p);
  108. if (w != avctx->width || h != avctx->height)
  109. avcodec_set_dimensions(avctx, w, h);
  110. if (avctx->get_buffer(avctx, p) < 0) {
  111. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  112. return -1;
  113. }
  114. p->pict_type = AV_PICTURE_TYPE_I;
  115. if (buf_end - buf < maplength)
  116. return AVERROR_INVALIDDATA;
  117. if (depth != 8 && maplength) {
  118. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  119. } else if (depth == 8) {
  120. unsigned int len = maplength / 3;
  121. if (!maplength) {
  122. av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
  123. return -1;
  124. }
  125. if (maplength % 3 || maplength > 768) {
  126. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  127. return -1;
  128. }
  129. ptr = p->data[1];
  130. for (x = 0; x < len; x++, ptr += 4)
  131. *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
  132. }
  133. buf += maplength;
  134. ptr = p->data[0];
  135. stride = p->linesize[0];
  136. /* scanlines are aligned on 16 bit boundaries */
  137. len = (depth * w + 7) >> 3;
  138. alen = len + (len & 1);
  139. if (type == RT_BYTE_ENCODED) {
  140. int value, run;
  141. uint8_t *end = ptr + h * stride;
  142. x = 0;
  143. while (ptr != end && buf < buf_end) {
  144. run = 1;
  145. if (buf_end - buf < 1)
  146. return AVERROR_INVALIDDATA;
  147. if ((value = *buf++) == 0x80) {
  148. run = *buf++ + 1;
  149. if (run != 1)
  150. value = *buf++;
  151. }
  152. while (run--) {
  153. if (x < len)
  154. ptr[x] = value;
  155. if (++x >= alen) {
  156. x = 0;
  157. ptr += stride;
  158. if (ptr == end)
  159. break;
  160. }
  161. }
  162. }
  163. } else {
  164. for (y = 0; y < h; y++) {
  165. if (buf_end - buf < len)
  166. break;
  167. memcpy(ptr, buf, len);
  168. ptr += stride;
  169. buf += alen;
  170. }
  171. }
  172. *picture = s->picture;
  173. *data_size = sizeof(AVFrame);
  174. return buf - bufstart;
  175. }
  176. static av_cold int sunrast_end(AVCodecContext *avctx) {
  177. SUNRASTContext *s = avctx->priv_data;
  178. if(s->picture.data[0])
  179. avctx->release_buffer(avctx, &s->picture);
  180. return 0;
  181. }
  182. AVCodec ff_sunrast_decoder = {
  183. .name = "sunrast",
  184. .type = AVMEDIA_TYPE_VIDEO,
  185. .id = CODEC_ID_SUNRAST,
  186. .priv_data_size = sizeof(SUNRASTContext),
  187. .init = sunrast_init,
  188. .close = sunrast_end,
  189. .decode = sunrast_decode_frame,
  190. .capabilities = CODEC_CAP_DR1,
  191. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  192. };