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.

199 lines
5.3KB

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