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.

272 lines
8.3KB

  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. #define RAS_MAGIC 0x59a66a95
  25. /* The Old and Standard format types indicate that the image data is
  26. * uncompressed. There is no difference between the two formats. */
  27. #define RT_OLD 0
  28. #define RT_STANDARD 1
  29. /* The Byte-Encoded format type indicates that the image data is compressed
  30. * using a run-length encoding scheme. */
  31. #define RT_BYTE_ENCODED 2
  32. /* The RGB format type indicates that the image is uncompressed with reverse
  33. * component order from Old and Standard (RGB vs BGR). */
  34. #define RT_FORMAT_RGB 3
  35. /* The TIFF and IFF format types indicate that the raster file was originally
  36. * converted from either of these file formats. We do not have any samples or
  37. * documentation of the format details. */
  38. #define RT_FORMAT_TIFF 4
  39. #define RT_FORMAT_IFF 5
  40. /* The Experimental format type is implementation-specific and is generally an
  41. * indication that the image file does not conform to the Sun Raster file
  42. * format specification. */
  43. #define RT_EXPERIMENTAL 0xffff
  44. typedef struct SUNRASTContext {
  45. AVFrame picture;
  46. } SUNRASTContext;
  47. static av_cold int sunrast_init(AVCodecContext *avctx) {
  48. SUNRASTContext *s = avctx->priv_data;
  49. avcodec_get_frame_defaults(&s->picture);
  50. avctx->coded_frame = &s->picture;
  51. return 0;
  52. }
  53. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  54. int *data_size, AVPacket *avpkt) {
  55. const uint8_t *buf = avpkt->data;
  56. const uint8_t *buf_end = avpkt->data + avpkt->size;
  57. SUNRASTContext * const s = avctx->priv_data;
  58. AVFrame *picture = data;
  59. AVFrame * const p = &s->picture;
  60. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  61. uint8_t *ptr, *ptr2 = NULL;
  62. const uint8_t *bufstart = buf;
  63. if (avpkt->size < 32)
  64. return AVERROR_INVALIDDATA;
  65. if (AV_RB32(buf) != RAS_MAGIC) {
  66. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  67. return -1;
  68. }
  69. w = AV_RB32(buf + 4);
  70. h = AV_RB32(buf + 8);
  71. depth = AV_RB32(buf + 12);
  72. type = AV_RB32(buf + 20);
  73. maptype = AV_RB32(buf + 24);
  74. maplength = AV_RB32(buf + 28);
  75. buf += 32;
  76. if (type == RT_EXPERIMENTAL) {
  77. av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
  78. return AVERROR_PATCHWELCOME;
  79. }
  80. if (type > RT_FORMAT_IFF) {
  81. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  82. return -1;
  83. }
  84. if (av_image_check_size(w, h, 0, avctx)) {
  85. av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
  86. return -1;
  87. }
  88. if (maptype & ~1) {
  89. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  90. return -1;
  91. }
  92. if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
  93. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  94. return -1;
  95. }
  96. switch (depth) {
  97. case 1:
  98. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_MONOWHITE;
  99. break;
  100. case 4:
  101. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_NONE;
  102. break;
  103. case 8:
  104. avctx->pix_fmt = maplength ? PIX_FMT_PAL8 : PIX_FMT_GRAY8;
  105. break;
  106. case 24:
  107. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
  108. break;
  109. case 32:
  110. avctx->pix_fmt = (type == RT_FORMAT_RGB) ? PIX_FMT_RGB0 : PIX_FMT_BGR0;
  111. break;
  112. default:
  113. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  114. return -1;
  115. }
  116. if (p->data[0])
  117. avctx->release_buffer(avctx, p);
  118. if (w != avctx->width || h != avctx->height)
  119. avcodec_set_dimensions(avctx, w, h);
  120. if (avctx->get_buffer(avctx, p) < 0) {
  121. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  122. return -1;
  123. }
  124. p->pict_type = AV_PICTURE_TYPE_I;
  125. if (buf_end - buf < maplength)
  126. return AVERROR_INVALIDDATA;
  127. if (depth > 8 && maplength) {
  128. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  129. } else if (maplength) {
  130. unsigned int len = maplength / 3;
  131. if (!maplength) {
  132. av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
  133. return -1;
  134. }
  135. if (maplength % 3 || maplength > 768) {
  136. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  137. return -1;
  138. }
  139. ptr = p->data[1];
  140. for (x = 0; x < len; x++, ptr += 4)
  141. *(uint32_t *)ptr = (0xFF<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  142. }
  143. buf += maplength;
  144. if (maplength && depth < 8) {
  145. ptr = ptr2 = av_malloc((w + 15) * h);
  146. if (!ptr)
  147. return AVERROR(ENOMEM);
  148. stride = (w + 15 >> 3) * depth;
  149. } else {
  150. ptr = p->data[0];
  151. stride = p->linesize[0];
  152. }
  153. /* scanlines are aligned on 16 bit boundaries */
  154. len = (depth * w + 7) >> 3;
  155. alen = len + (len & 1);
  156. if (type == RT_BYTE_ENCODED) {
  157. int value, run;
  158. uint8_t *end = ptr + h * stride;
  159. x = 0;
  160. while (ptr != end && buf < buf_end) {
  161. run = 1;
  162. if (buf_end - buf < 1)
  163. return AVERROR_INVALIDDATA;
  164. if ((value = *buf++) == 0x80) {
  165. run = *buf++ + 1;
  166. if (run != 1)
  167. value = *buf++;
  168. }
  169. while (run--) {
  170. if (x < len)
  171. ptr[x] = value;
  172. if (++x >= alen) {
  173. x = 0;
  174. ptr += stride;
  175. if (ptr == end)
  176. break;
  177. }
  178. }
  179. }
  180. } else {
  181. for (y = 0; y < h; y++) {
  182. if (buf_end - buf < len)
  183. break;
  184. memcpy(ptr, buf, len);
  185. ptr += stride;
  186. buf += alen;
  187. }
  188. }
  189. if (avctx->pix_fmt == PIX_FMT_PAL8 && depth < 8) {
  190. uint8_t *ptr_free = ptr2;
  191. ptr = p->data[0];
  192. for (y=0; y<h; y++) {
  193. for (x = 0; x < (w + 7 >> 3) * depth; x++) {
  194. if (depth == 1) {
  195. ptr[8*x] = ptr2[x] >> 7;
  196. ptr[8*x+1] = ptr2[x] >> 6 & 1;
  197. ptr[8*x+2] = ptr2[x] >> 5 & 1;
  198. ptr[8*x+3] = ptr2[x] >> 4 & 1;
  199. ptr[8*x+4] = ptr2[x] >> 3 & 1;
  200. ptr[8*x+5] = ptr2[x] >> 2 & 1;
  201. ptr[8*x+6] = ptr2[x] >> 1 & 1;
  202. ptr[8*x+7] = ptr2[x] & 1;
  203. } else {
  204. ptr[2*x] = ptr2[x] >> 4;
  205. ptr[2*x+1] = ptr2[x] & 0xF;
  206. }
  207. }
  208. ptr += p->linesize[0];
  209. ptr2 += (w + 15 >> 3) * depth;
  210. }
  211. av_freep(&ptr_free);
  212. }
  213. *picture = s->picture;
  214. *data_size = sizeof(AVFrame);
  215. return buf - bufstart;
  216. }
  217. static av_cold int sunrast_end(AVCodecContext *avctx) {
  218. SUNRASTContext *s = avctx->priv_data;
  219. if(s->picture.data[0])
  220. avctx->release_buffer(avctx, &s->picture);
  221. return 0;
  222. }
  223. AVCodec ff_sunrast_decoder = {
  224. .name = "sunrast",
  225. .type = AVMEDIA_TYPE_VIDEO,
  226. .id = CODEC_ID_SUNRAST,
  227. .priv_data_size = sizeof(SUNRASTContext),
  228. .init = sunrast_init,
  229. .close = sunrast_end,
  230. .decode = sunrast_decode_frame,
  231. .capabilities = CODEC_CAP_DR1,
  232. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  233. };