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.

200 lines
5.4KB

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