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.

197 lines
5.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 "avcodec.h"
  22. #define RT_OLD 0
  23. #define RT_STANDARD 1
  24. #define RT_BYTE_ENCODED 2
  25. #define RT_FORMAT_RGB 3
  26. #define RT_FORMAT_TIFF 4
  27. #define RT_FORMAT_IFF 5
  28. typedef struct SUNRASTContext {
  29. AVFrame picture;
  30. } SUNRASTContext;
  31. static av_cold int sunrast_init(AVCodecContext *avctx) {
  32. SUNRASTContext *s = avctx->priv_data;
  33. avcodec_get_frame_defaults(&s->picture);
  34. avctx->coded_frame= &s->picture;
  35. return 0;
  36. }
  37. static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
  38. int *data_size, const uint8_t *buf, int buf_size) {
  39. SUNRASTContext * const s = avctx->priv_data;
  40. AVFrame *picture = data;
  41. AVFrame * const p = &s->picture;
  42. unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
  43. uint8_t *ptr;
  44. const uint8_t *bufstart = buf;
  45. if (AV_RB32(buf) != 0x59a66a95) {
  46. av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
  47. return -1;
  48. }
  49. w = AV_RB32(buf+4);
  50. h = AV_RB32(buf+8);
  51. depth = AV_RB32(buf+12);
  52. type = AV_RB32(buf+20);
  53. maptype = AV_RB32(buf+24);
  54. maplength = AV_RB32(buf+28);
  55. if (type > RT_BYTE_ENCODED && type <= RT_FORMAT_IFF) {
  56. av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
  57. return -1;
  58. }
  59. if (type > RT_FORMAT_IFF) {
  60. av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
  61. return -1;
  62. }
  63. if (maptype & ~1) {
  64. av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
  65. return -1;
  66. }
  67. buf += 32;
  68. switch (depth) {
  69. case 1:
  70. avctx->pix_fmt = PIX_FMT_MONOWHITE;
  71. break;
  72. case 8:
  73. avctx->pix_fmt = PIX_FMT_PAL8;
  74. break;
  75. case 24:
  76. avctx->pix_fmt = PIX_FMT_BGR24;
  77. break;
  78. default:
  79. av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
  80. return -1;
  81. }
  82. if (p->data[0])
  83. avctx->release_buffer(avctx, p);
  84. if (avcodec_check_dimensions(avctx, w, h))
  85. return -1;
  86. if (w != avctx->width || h != avctx->height)
  87. avcodec_set_dimensions(avctx, w, h);
  88. if (avctx->get_buffer(avctx, p) < 0) {
  89. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  90. return -1;
  91. }
  92. p->pict_type = FF_I_TYPE;
  93. if (depth != 8 && maplength) {
  94. av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
  95. } else if (depth == 8) {
  96. unsigned int len = maplength / 3;
  97. if (!maplength) {
  98. av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
  99. return -1;
  100. }
  101. if (maplength % 3 || maplength > 768) {
  102. av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
  103. return -1;
  104. }
  105. ptr = p->data[1];
  106. for (x=0; x<len; x++, ptr+=4)
  107. *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
  108. }
  109. buf += maplength;
  110. ptr = p->data[0];
  111. stride = p->linesize[0];
  112. /* scanlines are aligned on 16 bit boundaries */
  113. len = (depth * w + 7) >> 3;
  114. alen = len + (len&1);
  115. if (type == RT_BYTE_ENCODED) {
  116. int value, run;
  117. uint8_t *end = ptr + h*stride;
  118. x = 0;
  119. while (ptr != end) {
  120. run = 1;
  121. if ((value = *buf++) == 0x80) {
  122. run = *buf++ + 1;
  123. if (run != 1)
  124. value = *buf++;
  125. }
  126. while (run--) {
  127. if (x < len)
  128. ptr[x] = value;
  129. if (++x >= alen) {
  130. x = 0;
  131. ptr += stride;
  132. if (ptr == end)
  133. break;
  134. }
  135. }
  136. }
  137. } else {
  138. for (y=0; y<h; y++) {
  139. memcpy(ptr, buf, len);
  140. ptr += stride;
  141. buf += alen;
  142. }
  143. }
  144. *picture = s->picture;
  145. *data_size = sizeof(AVFrame);
  146. return buf - bufstart;
  147. }
  148. static av_cold int sunrast_end(AVCodecContext *avctx) {
  149. SUNRASTContext *s = avctx->priv_data;
  150. if(s->picture.data[0])
  151. avctx->release_buffer(avctx, &s->picture);
  152. return 0;
  153. }
  154. AVCodec sunrast_decoder = {
  155. "sunrast",
  156. CODEC_TYPE_VIDEO,
  157. CODEC_ID_SUNRAST,
  158. sizeof(SUNRASTContext),
  159. sunrast_init,
  160. NULL,
  161. sunrast_end,
  162. sunrast_decode_frame,
  163. 0,
  164. NULL,
  165. .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
  166. };