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