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.

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