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.

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