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.

215 lines
6.1KB

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