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.

212 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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. buf += 32;
  62. if (type < RT_OLD || type > RT_FORMAT_IFF) {
  63. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  64. return -1;
  65. }
  66. if (av_image_check_size(w, h, 0, avctx)) {
  67. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  68. return -1;
  69. }
  70. if (maptype & ~1) {
  71. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  72. return -1;
  73. }
  74. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
  75. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  76. return -1;
  77. }
  78. switch (depth) {
  79. case 1:
  80. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  81. break;
  82. case 8:
  83. avctx->pix_fmt = PIX_FMT_PAL8;
  84. break;
  85. case 24:
  86. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  87. break;
  88. default:
  89. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  90. return -1;
  91. }
  92. if (p->data[0])
  93. avctx->release_buffer(avctx, p);
  94. if (w != avctx->width || h != avctx->height)
  95. avcodec_set_dimensions(avctx, w, h);
  96. if (avctx->get_buffer(avctx, p) < 0) {
  97. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  98. return -1;
  99. }
  100. p->pict_type = AV_PICTURE_TYPE_I;
  101. if (buf_end - buf < maplength)
  102. return AVERROR_INVALIDDATA;
  103. if (depth != 8 && maplength) {
  104. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  105. } else if (depth == 8) {
  106. unsigned int len = maplength / 3;
  107. if (!maplength) {
  108. av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
  109. return -1;
  110. }
  111. if (maplength % 3 || maplength > 768) {
  112. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  113. return -1;
  114. }
  115. ptr = p->data[1];
  116. for (x=0; x<len; x++, ptr+=4)
  117. *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  118. }
  119. buf += maplength;
  120. ptr = p->data[0];
  121. stride = p->linesize[0];
  122. /* scanlines are aligned on 16 bit boundaries */
  123. len = (depth * w + 7) >> 3;
  124. alen = len + (len&1);
  125. if (type == RT_BYTE_ENCODED) {
  126. int value, run;
  127. uint8_t *end = ptr + h*stride;
  128. x = 0;
  129. while (ptr != end && buf < buf_end) {
  130. run = 1;
  131. if (buf_end - buf < 1)
  132. return AVERROR_INVALIDDATA;
  133. if ((value = *buf++) == 0x80) {
  134. run = *buf++ + 1;
  135. if (run != 1)
  136. value = *buf++;
  137. }
  138. while (run--) {
  139. if (x < len)
  140. ptr[x] = value;
  141. if (++x >= alen) {
  142. x = 0;
  143. ptr += stride;
  144. if (ptr == end)
  145. break;
  146. }
  147. }
  148. }
  149. } else {
  150. for (y=0; y<h; y++) {
  151. if (buf_end - buf < len)
  152. break;
  153. memcpy(ptr, buf, len);
  154. ptr += stride;
  155. buf += alen;
  156. }
  157. }
  158. *picture = s->picture;
  159. *data_size = sizeof(AVFrame);
  160. return buf - bufstart;
  161. }
  162. static av_cold int sunrast_end(AVCodecContext *avctx) {
  163. SUNRASTContext *s = avctx->priv_data;
  164. if(s->picture.data[0])
  165. avctx->release_buffer(avctx, &s->picture);
  166. return 0;
  167. }
  168. AVCodec ff_sunrast_decoder = {
  169. .name = "sunrast",
  170. .type = AVMEDIA_TYPE_VIDEO,
  171. .id = CODEC_ID_SUNRAST,
  172. .priv_data_size = sizeof(SUNRASTContext),
  173. .init = sunrast_init,
  174. .close = sunrast_end,
  175. .decode = sunrast_decode_frame,
  176. .capabilities = CODEC_CAP_DR1,
  177. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  178. };