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.

270 lines
8.2KB

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