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.

248 lines
7.5KB

  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 "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "sunrast.h"
  25. typedef struct SUNRASTContext {
  26. AVFrame picture;
  27. } SUNRASTContext;
  28. static av_cold int sunrast_init(AVCodecContext *avctx) {
  29. SUNRASTContext *s = avctx->priv_data;
  30. avcodec_get_frame_defaults(&s->picture);
  31. avctx->coded_frame = &s->picture;
  32. return 0;
  33. }
  34. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  35. int *data_size, AVPacket *avpkt) {
  36. const uint8_t *buf = avpkt->data;
  37. const uint8_t *buf_end = avpkt->data + avpkt->size;
  38. SUNRASTContext * const s = avctx->priv_data;
  39. AVFrame *picture = data;
  40. AVFrame * const p = &s->picture;
  41. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  42. uint8_t *ptr, *ptr2 = NULL;
  43. const uint8_t *bufstart = buf;
  44. int ret;
  45. if (avpkt->size < 32)
  46. return AVERROR_INVALIDDATA;
  47. if (AV_RB32(buf) != RAS_MAGIC) {
  48. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  49. return AVERROR_INVALIDDATA;
  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. buf += 32;
  58. if (type == RT_EXPERIMENTAL) {
  59. av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
  60. return AVERROR_PATCHWELCOME;
  61. }
  62. if (type > RT_FORMAT_IFF) {
  63. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  64. return AVERROR_INVALIDDATA;
  65. }
  66. if (av_image_check_size(w, h, 0, avctx)) {
  67. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  68. return AVERROR_INVALIDDATA;
  69. }
  70. if (maptype == RMT_RAW) {
  71. av_log_ask_for_sample(avctx, "unsupported colormap type\n");
  72. return AVERROR_PATCHWELCOME;
  73. }
  74. if (maptype > RMT_RAW) {
  75. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  76. return AVERROR_INVALIDDATA;
  77. }
  78. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
  79. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  80. return -1;
  81. }
  82. switch (depth) {
  83. case 1:
  84. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
  85. break;
  86. case 4:
  87. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
  88. break;
  89. case 8:
  90. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  91. break;
  92. case 24:
  93. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  94. break;
  95. case 32:
  96. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_0RGB : PIX_FMT_0BGR;
  97. break;
  98. default:
  99. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  100. return AVERROR_INVALIDDATA;
  101. }
  102. if (p->data[0])
  103. avctx->release_buffer(avctx, p);
  104. if (w != avctx->width || h != avctx->height)
  105. avcodec_set_dimensions(avctx, w, h);
  106. if ((ret = avctx->get_buffer(avctx, p)) < 0) {
  107. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  108. return ret;
  109. }
  110. p->pict_type = AV_PICTURE_TYPE_I;
  111. if (buf_end - buf < maplength)
  112. return AVERROR_INVALIDDATA;
  113. if (depth > 8 && maplength) {
  114. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  115. } else if (maplength) {
  116. unsigned int len = maplength / 3;
  117. if (maplength % 3 || maplength > 768) {
  118. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  119. return AVERROR_INVALIDDATA;
  120. }
  121. ptr = p->data[1];
  122. for (x = 0; x < len; x++, ptr += 4)
  123. *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  124. }
  125. buf += maplength;
  126. if (maplength && depth < 8) {
  127. ptr = ptr2 = av_malloc((w + 15) * h);
  128. if (!ptr)
  129. return AVERROR(ENOMEM);
  130. stride = (w + 15 >> 3) * depth;
  131. } else {
  132. ptr = p->data[0];
  133. stride = p->linesize[0];
  134. }
  135. /* scanlines are aligned on 16 bit boundaries */
  136. len = (depth * w + 7) >> 3;
  137. alen = len + (len & 1);
  138. if (type == RT_BYTE_ENCODED) {
  139. int value, run;
  140. uint8_t *end = ptr + h * stride;
  141. x = 0;
  142. while (ptr != end && buf < buf_end) {
  143. run = 1;
  144. if (buf_end - buf < 1)
  145. return AVERROR_INVALIDDATA;
  146. if ((value = *buf++) == RLE_TRIGGER) {
  147. run = *buf++ + 1;
  148. if (run != 1)
  149. value = *buf++;
  150. }
  151. while (run--) {
  152. if (x < len)
  153. ptr[x] = value;
  154. if (++x >= alen) {
  155. x = 0;
  156. ptr += stride;
  157. if (ptr == end)
  158. break;
  159. }
  160. }
  161. }
  162. } else {
  163. for (y = 0; y < h; y++) {
  164. if (buf_end - buf < len)
  165. break;
  166. memcpy(ptr, buf, len);
  167. ptr += stride;
  168. buf += alen;
  169. }
  170. }
  171. if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
  172. uint8_t *ptr_free = ptr2;
  173. ptr = p->data[0];
  174. for (y=0; y<h; y++) {
  175. for (x = 0; x < (w + 7 >> 3) * depth; x++) {
  176. if (depth == 1) {
  177. ptr[8*x] = ptr2[x] >> 7;
  178. ptr[8*x+1] = ptr2[x] >> 6 & 1;
  179. ptr[8*x+2] = ptr2[x] >> 5 & 1;
  180. ptr[8*x+3] = ptr2[x] >> 4 & 1;
  181. ptr[8*x+4] = ptr2[x] >> 3 & 1;
  182. ptr[8*x+5] = ptr2[x] >> 2 & 1;
  183. ptr[8*x+6] = ptr2[x] >> 1 & 1;
  184. ptr[8*x+7] = ptr2[x] & 1;
  185. } else {
  186. ptr[2*x] = ptr2[x] >> 4;
  187. ptr[2*x+1] = ptr2[x] & 0xF;
  188. }
  189. }
  190. ptr += p->linesize[0];
  191. ptr2 += (w + 15 >> 3) * depth;
  192. }
  193. av_freep(&ptr_free);
  194. }
  195. *picture = s->picture;
  196. *data_size = sizeof(AVFrame);
  197. return buf - bufstart;
  198. }
  199. static av_cold int sunrast_end(AVCodecContext *avctx) {
  200. SUNRASTContext *s = avctx->priv_data;
  201. if(s->picture.data[0])
  202. avctx->release_buffer(avctx, &s->picture);
  203. return 0;
  204. }
  205. AVCodec ff_sunrast_decoder = {
  206. .name = "sunrast",
  207. .type = AVMEDIA_TYPE_VIDEO,
  208. .id = CODEC_ID_SUNRAST,
  209. .priv_data_size = sizeof(SUNRASTContext),
  210. .init = sunrast_init,
  211. .close = sunrast_end,
  212. .decode = sunrast_decode_frame,
  213. .capabilities = CODEC_CAP_DR1,
  214. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  215. };