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.

218 lines
6.8KB

  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 (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. avpriv_request_sample(avctx, "Unknown colormap type");
  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. return ret;
  98. p->pict_type = AV_PICTURE_TYPE_I;
  99. if (buf_end - buf < maplength)
  100. return AVERROR_INVALIDDATA;
  101. if (depth > 8 && maplength) {
  102. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  103. } else if (maplength) {
  104. unsigned int len = maplength / 3;
  105. if (maplength % 3 || maplength > 768) {
  106. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  107. return AVERROR_INVALIDDATA;
  108. }
  109. ptr = p->data[1];
  110. for (x = 0; x < len; x++, ptr += 4)
  111. *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  112. }
  113. buf += maplength;
  114. if (maplength && depth < 8) {
  115. ptr = ptr2 = av_malloc((w + 15) * h);
  116. if (!ptr)
  117. return AVERROR(ENOMEM);
  118. stride = (w + 15 >> 3) * depth;
  119. } else {
  120. ptr = p->data[0];
  121. stride = p->linesize[0];
  122. }
  123. /* scanlines are aligned on 16 bit boundaries */
  124. len = (depth * w + 7) >> 3;
  125. alen = len + (len & 1);
  126. if (type == RT_BYTE_ENCODED) {
  127. int value, run;
  128. uint8_t *end = ptr + h * stride;
  129. x = 0;
  130. while (ptr != end && buf < buf_end) {
  131. run = 1;
  132. if (buf_end - buf < 1)
  133. return AVERROR_INVALIDDATA;
  134. if ((value = *buf++) == RLE_TRIGGER) {
  135. run = *buf++ + 1;
  136. if (run != 1)
  137. value = *buf++;
  138. }
  139. while (run--) {
  140. if (x < len)
  141. ptr[x] = value;
  142. if (++x >= alen) {
  143. x = 0;
  144. ptr += stride;
  145. if (ptr == end)
  146. break;
  147. }
  148. }
  149. }
  150. } else {
  151. for (y = 0; y < h; y++) {
  152. if (buf_end - buf < len)
  153. break;
  154. memcpy(ptr, buf, len);
  155. ptr += stride;
  156. buf += alen;
  157. }
  158. }
  159. if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
  160. uint8_t *ptr_free = ptr2;
  161. ptr = p->data[0];
  162. for (y=0; y<h; y++) {
  163. for (x = 0; x < (w + 7 >> 3) * depth; x++) {
  164. if (depth == 1) {
  165. ptr[8*x] = ptr2[x] >> 7;
  166. ptr[8*x+1] = ptr2[x] >> 6 & 1;
  167. ptr[8*x+2] = ptr2[x] >> 5 & 1;
  168. ptr[8*x+3] = ptr2[x] >> 4 & 1;
  169. ptr[8*x+4] = ptr2[x] >> 3 & 1;
  170. ptr[8*x+5] = ptr2[x] >> 2 & 1;
  171. ptr[8*x+6] = ptr2[x] >> 1 & 1;
  172. ptr[8*x+7] = ptr2[x] & 1;
  173. } else {
  174. ptr[2*x] = ptr2[x] >> 4;
  175. ptr[2*x+1] = ptr2[x] & 0xF;
  176. }
  177. }
  178. ptr += p->linesize[0];
  179. ptr2 += (w + 15 >> 3) * depth;
  180. }
  181. av_freep(&ptr_free);
  182. }
  183. *got_frame = 1;
  184. return buf - bufstart;
  185. }
  186. AVCodec ff_sunrast_decoder = {
  187. .name = "sunrast",
  188. .type = AVMEDIA_TYPE_VIDEO,
  189. .id = AV_CODEC_ID_SUNRAST,
  190. .decode = sunrast_decode_frame,
  191. .capabilities = CODEC_CAP_DR1,
  192. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  193. };