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.

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