sunrast.c 5.3 KB

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