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.

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