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.

220 lines
6.9KB

  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. av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
  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 (av_image_check_size(w, h, 0, avctx)) {
  59. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  60. return AVERROR_INVALIDDATA;
  61. }
  62. if (maptype == RMT_RAW) {
  63. av_log_ask_for_sample(avctx, "unsupported colormap type\n");
  64. return AVERROR_PATCHWELCOME;
  65. }
  66. if (maptype > RMT_RAW) {
  67. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
  71. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  72. return -1;
  73. }
  74. switch (depth) {
  75. case 1:
  76. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
  77. break;
  78. case 4:
  79. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
  80. break;
  81. case 8:
  82. avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
  83. break;
  84. case 24:
  85. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
  86. break;
  87. case 32:
  88. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR;
  89. break;
  90. default:
  91. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  92. return AVERROR_INVALIDDATA;
  93. }
  94. if (w != avctx->width || h != avctx->height)
  95. avcodec_set_dimensions(avctx, w, h);
  96. if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
  97. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  98. return ret;
  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 (maplength) {
  106. unsigned int len = maplength / 3;
  107. if (maplength % 3 || maplength > 768) {
  108. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  109. return AVERROR_INVALIDDATA;
  110. }
  111. ptr = p->data[1];
  112. for (x = 0; x < len; x++, ptr += 4)
  113. *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  114. }
  115. buf += maplength;
  116. if (maplength && depth < 8) {
  117. ptr = ptr2 = av_malloc((w + 15) * h);
  118. if (!ptr)
  119. return AVERROR(ENOMEM);
  120. stride = (w + 15 >> 3) * depth;
  121. } else {
  122. ptr = p->data[0];
  123. stride = p->linesize[0];
  124. }
  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++) == RLE_TRIGGER) {
  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. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
  162. uint8_t *ptr_free = ptr2;
  163. ptr = p->data[0];
  164. for (y=0; y<h; y++) {
  165. for (x = 0; x < (w + 7 >> 3) * depth; x++) {
  166. if (depth == 1) {
  167. ptr[8*x] = ptr2[x] >> 7;
  168. ptr[8*x+1] = ptr2[x] >> 6 & 1;
  169. ptr[8*x+2] = ptr2[x] >> 5 & 1;
  170. ptr[8*x+3] = ptr2[x] >> 4 & 1;
  171. ptr[8*x+4] = ptr2[x] >> 3 & 1;
  172. ptr[8*x+5] = ptr2[x] >> 2 & 1;
  173. ptr[8*x+6] = ptr2[x] >> 1 & 1;
  174. ptr[8*x+7] = ptr2[x] & 1;
  175. } else {
  176. ptr[2*x] = ptr2[x] >> 4;
  177. ptr[2*x+1] = ptr2[x] & 0xF;
  178. }
  179. }
  180. ptr += p->linesize[0];
  181. ptr2 += (w + 15 >> 3) * depth;
  182. }
  183. av_freep(&ptr_free);
  184. }
  185. *got_frame = 1;
  186. return buf - bufstart;
  187. }
  188. AVCodec ff_sunrast_decoder = {
  189. .name = "sunrast",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .id = AV_CODEC_ID_SUNRAST,
  192. .decode = sunrast_decode_frame,
  193. .capabilities = CODEC_CAP_DR1,
  194. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  195. };