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.

216 lines
6.6KB

  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/common.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/imgutils.h"
  24. #include "avcodec.h"
  25. #include "internal.h"
  26. #include "sunrast.h"
  27. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  28. int *got_frame, AVPacket *avpkt)
  29. {
  30. const uint8_t *buf = avpkt->data;
  31. const uint8_t *buf_end = avpkt->data + avpkt->size;
  32. AVFrame * const p = data;
  33. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  34. uint8_t *ptr, *ptr2 = NULL;
  35. const uint8_t *bufstart = buf;
  36. int ret;
  37. if (avpkt->size < 32)
  38. return AVERROR_INVALIDDATA;
  39. if (AV_RB32(buf) != RAS_MAGIC) {
  40. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  41. return AVERROR_INVALIDDATA;
  42. }
  43. w = AV_RB32(buf + 4);
  44. h = AV_RB32(buf + 8);
  45. depth = AV_RB32(buf + 12);
  46. type = AV_RB32(buf + 20);
  47. maptype = AV_RB32(buf + 24);
  48. maplength = AV_RB32(buf + 28);
  49. buf += 32;
  50. if (type == RT_EXPERIMENTAL) {
  51. avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
  52. return AVERROR_PATCHWELCOME;
  53. }
  54. if (type > RT_FORMAT_IFF) {
  55. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  56. return AVERROR_INVALIDDATA;
  57. }
  58. if (maptype == RMT_RAW) {
  59. avpriv_request_sample(avctx, "Unknown colormap type");
  60. return AVERROR_PATCHWELCOME;
  61. }
  62. if (maptype > RMT_RAW) {
  63. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
  67. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  68. return -1;
  69. }
  70. switch (depth) {
  71. case 1:
  72. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
  73. break;
  74. case 4:
  75. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
  76. break;
  77. case 8:
  78. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  79. break;
  80. case 24:
  81. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  82. break;
  83. case 32:
  84. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR;
  85. break;
  86. default:
  87. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  88. return AVERROR_INVALIDDATA;
  89. }
  90. ret = ff_set_dimensions(avctx, w, h);
  91. if (ret < 0)
  92. return ret;
  93. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  94. return ret;
  95. p->pict_type = AV_PICTURE_TYPE_I;
  96. if (buf_end - buf < maplength)
  97. return AVERROR_INVALIDDATA;
  98. if (depth > 8 && maplength) {
  99. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  100. } else if (maplength) {
  101. unsigned int len = maplength / 3;
  102. if (maplength % 3 || maplength > 768) {
  103. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  104. return AVERROR_INVALIDDATA;
  105. }
  106. ptr = p->data[1];
  107. for (x = 0; x < len; x++, ptr += 4)
  108. *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  109. }
  110. buf += maplength;
  111. if (maplength && depth < 8) {
  112. ptr = ptr2 = av_malloc_array((w + 15), h);
  113. if (!ptr)
  114. return AVERROR(ENOMEM);
  115. stride = (w + 15 >> 3) * depth;
  116. } else {
  117. ptr = p->data[0];
  118. stride = p->linesize[0];
  119. }
  120. /* scanlines are aligned on 16 bit boundaries */
  121. len = (depth * w + 7) >> 3;
  122. alen = len + (len & 1);
  123. if (type == RT_BYTE_ENCODED) {
  124. int value, run;
  125. uint8_t *end = ptr + h * stride;
  126. x = 0;
  127. while (ptr != end && buf < buf_end) {
  128. run = 1;
  129. if (buf_end - buf < 1)
  130. return AVERROR_INVALIDDATA;
  131. if ((value = *buf++) == RLE_TRIGGER) {
  132. run = *buf++ + 1;
  133. if (run != 1)
  134. value = *buf++;
  135. }
  136. while (run--) {
  137. if (x < len)
  138. ptr[x] = value;
  139. if (++x >= alen) {
  140. x = 0;
  141. ptr += stride;
  142. if (ptr == end)
  143. break;
  144. }
  145. }
  146. }
  147. } else {
  148. for (y = 0; y < h; y++) {
  149. if (buf_end - buf < alen)
  150. break;
  151. memcpy(ptr, buf, len);
  152. ptr += stride;
  153. buf += alen;
  154. }
  155. }
  156. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
  157. uint8_t *ptr_free = ptr2;
  158. ptr = p->data[0];
  159. for (y=0; y<h; y++) {
  160. for (x = 0; x < (w + 7 >> 3) * depth; x++) {
  161. if (depth == 1) {
  162. ptr[8*x] = ptr2[x] >> 7;
  163. ptr[8*x+1] = ptr2[x] >> 6 & 1;
  164. ptr[8*x+2] = ptr2[x] >> 5 & 1;
  165. ptr[8*x+3] = ptr2[x] >> 4 & 1;
  166. ptr[8*x+4] = ptr2[x] >> 3 & 1;
  167. ptr[8*x+5] = ptr2[x] >> 2 & 1;
  168. ptr[8*x+6] = ptr2[x] >> 1 & 1;
  169. ptr[8*x+7] = ptr2[x] & 1;
  170. } else {
  171. ptr[2*x] = ptr2[x] >> 4;
  172. ptr[2*x+1] = ptr2[x] & 0xF;
  173. }
  174. }
  175. ptr += p->linesize[0];
  176. ptr2 += (w + 15 >> 3) * depth;
  177. }
  178. av_freep(&ptr_free);
  179. }
  180. *got_frame = 1;
  181. return buf - bufstart;
  182. }
  183. AVCodec ff_sunrast_decoder = {
  184. .name = "sunrast",
  185. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  186. .type = AVMEDIA_TYPE_VIDEO,
  187. .id = AV_CODEC_ID_SUNRAST,
  188. .decode = sunrast_decode_frame,
  189. .capabilities = AV_CODEC_CAP_DR1,
  190. };