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.

229 lines
6.8KB

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