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.

305 lines
9.0KB

  1. /*
  2. * SGI image decoder
  3. * Todd Kirby <doubleshot@pacbell.net>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/imgutils.h"
  22. #include "avcodec.h"
  23. #include "bytestream.h"
  24. #include "internal.h"
  25. #include "sgi.h"
  26. typedef struct SgiState {
  27. AVCodecContext *avctx;
  28. unsigned int width;
  29. unsigned int height;
  30. unsigned int depth;
  31. unsigned int bytes_per_channel;
  32. int linesize;
  33. GetByteContext g;
  34. } SgiState;
  35. /**
  36. * Expand an RLE row into a channel.
  37. * @param s the current image state
  38. * @param out_buf Points to one line after the output buffer.
  39. * @param len length of out_buf in bytes
  40. * @param pixelstride pixel stride of input buffer
  41. * @return size of output in bytes, -1 if buffer overflows
  42. */
  43. static int expand_rle_row8(SgiState *s, uint8_t *out_buf,
  44. int len, int pixelstride)
  45. {
  46. unsigned char pixel, count;
  47. unsigned char *orig = out_buf;
  48. uint8_t *out_end = out_buf + len;
  49. while (out_buf < out_end) {
  50. if (bytestream2_get_bytes_left(&s->g) < 1)
  51. return AVERROR_INVALIDDATA;
  52. pixel = bytestream2_get_byteu(&s->g);
  53. if (!(count = (pixel & 0x7f))) {
  54. break;
  55. }
  56. /* Check for buffer overflow. */
  57. if (pixelstride * (count - 1) >= len) {
  58. av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
  59. return AVERROR_INVALIDDATA;
  60. }
  61. if (pixel & 0x80) {
  62. while (count--) {
  63. *out_buf = bytestream2_get_byte(&s->g);
  64. out_buf += pixelstride;
  65. }
  66. } else {
  67. pixel = bytestream2_get_byte(&s->g);
  68. while (count--) {
  69. *out_buf = pixel;
  70. out_buf += pixelstride;
  71. }
  72. }
  73. }
  74. return (out_buf - orig) / pixelstride;
  75. }
  76. static int expand_rle_row16(SgiState *s, uint16_t *out_buf,
  77. int len, int pixelstride)
  78. {
  79. unsigned short pixel;
  80. unsigned char count;
  81. unsigned short *orig = out_buf;
  82. uint16_t *out_end = out_buf + len;
  83. while (out_buf < out_end) {
  84. if (bytestream2_get_bytes_left(&s->g) < 2)
  85. return AVERROR_INVALIDDATA;
  86. pixel = bytestream2_get_be16u(&s->g);
  87. if (!(count = (pixel & 0x7f)))
  88. break;
  89. /* Check for buffer overflow. */
  90. if (pixelstride * (count - 1) >= len) {
  91. av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");
  92. return AVERROR_INVALIDDATA;
  93. }
  94. if (pixel & 0x80) {
  95. while (count--) {
  96. pixel = bytestream2_get_ne16(&s->g);
  97. AV_WN16A(out_buf, pixel);
  98. out_buf += pixelstride;
  99. }
  100. } else {
  101. pixel = bytestream2_get_ne16(&s->g);
  102. while (count--) {
  103. AV_WN16A(out_buf, pixel);
  104. out_buf += pixelstride;
  105. }
  106. }
  107. }
  108. return (out_buf - orig) / pixelstride;
  109. }
  110. /**
  111. * Read a run length encoded SGI image.
  112. * @param out_buf output buffer
  113. * @param s the current image state
  114. * @return 0 if no error, else return error number.
  115. */
  116. static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
  117. {
  118. uint8_t *dest_row;
  119. unsigned int len = s->height * s->depth * 4;
  120. GetByteContext g_table = s->g;
  121. unsigned int y, z;
  122. unsigned int start_offset;
  123. int linesize, ret;
  124. /* size of RLE offset and length tables */
  125. if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
  126. return AVERROR_INVALIDDATA;
  127. }
  128. for (z = 0; z < s->depth; z++) {
  129. dest_row = out_buf;
  130. for (y = 0; y < s->height; y++) {
  131. linesize = s->width * s->depth * s->bytes_per_channel;
  132. dest_row -= s->linesize;
  133. start_offset = bytestream2_get_be32(&g_table);
  134. bytestream2_seek(&s->g, start_offset, SEEK_SET);
  135. if (s->bytes_per_channel == 1)
  136. ret = expand_rle_row8(s, dest_row + z, linesize, s->depth);
  137. else
  138. ret = expand_rle_row16(s, (uint16_t *)dest_row + z, linesize, s->depth);
  139. if (ret != s->width)
  140. return AVERROR_INVALIDDATA;
  141. }
  142. }
  143. return 0;
  144. }
  145. /**
  146. * Read an uncompressed SGI image.
  147. * @param out_buf output buffer
  148. * @param s the current image state
  149. * @return 0 if read success, otherwise return -1.
  150. */
  151. static int read_uncompressed_sgi(unsigned char *out_buf, SgiState *s)
  152. {
  153. int x, y, z;
  154. unsigned int offset = s->height * s->width * s->bytes_per_channel;
  155. GetByteContext gp[4];
  156. uint8_t *out_end;
  157. /* Test buffer size. */
  158. if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
  159. return AVERROR_INVALIDDATA;
  160. /* Create a reader for each plane */
  161. for (z = 0; z < s->depth; z++) {
  162. gp[z] = s->g;
  163. bytestream2_skip(&gp[z], z * offset);
  164. }
  165. for (y = s->height - 1; y >= 0; y--) {
  166. out_end = out_buf + (y * s->linesize);
  167. if (s->bytes_per_channel == 1) {
  168. for (x = s->width; x > 0; x--)
  169. for (z = 0; z < s->depth; z++)
  170. *out_end++ = bytestream2_get_byteu(&gp[z]);
  171. } else {
  172. uint16_t *out16 = (uint16_t *)out_end;
  173. for (x = s->width; x > 0; x--)
  174. for (z = 0; z < s->depth; z++)
  175. *out16++ = bytestream2_get_ne16u(&gp[z]);
  176. }
  177. }
  178. return 0;
  179. }
  180. static int decode_frame(AVCodecContext *avctx,
  181. void *data, int *got_frame,
  182. AVPacket *avpkt)
  183. {
  184. SgiState *s = avctx->priv_data;
  185. AVFrame *p = data;
  186. unsigned int dimension, rle;
  187. int ret = 0;
  188. uint8_t *out_buf, *out_end;
  189. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  190. if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
  191. av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
  192. return AVERROR_INVALIDDATA;
  193. }
  194. /* Test for SGI magic. */
  195. if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
  196. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  197. return AVERROR_INVALIDDATA;
  198. }
  199. rle = bytestream2_get_byte(&s->g);
  200. s->bytes_per_channel = bytestream2_get_byte(&s->g);
  201. dimension = bytestream2_get_be16(&s->g);
  202. s->width = bytestream2_get_be16(&s->g);
  203. s->height = bytestream2_get_be16(&s->g);
  204. s->depth = bytestream2_get_be16(&s->g);
  205. if (s->bytes_per_channel != 1 && s->bytes_per_channel != 2) {
  206. av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
  207. return AVERROR(EINVAL);
  208. }
  209. /* Check for supported image dimensions. */
  210. if (dimension != 2 && dimension != 3) {
  211. av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
  212. return AVERROR(EINVAL);
  213. }
  214. if (s->depth == SGI_GRAYSCALE) {
  215. avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
  216. } else if (s->depth == SGI_RGB) {
  217. avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
  218. } else if (s->depth == SGI_RGBA) {
  219. avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGBA64BE : AV_PIX_FMT_RGBA;
  220. } else {
  221. av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
  222. return AVERROR(EINVAL);
  223. }
  224. ret = ff_set_dimensions(avctx, s->width, s->height);
  225. if (ret < 0)
  226. return ret;
  227. ret = ff_get_buffer(avctx, p, 0);
  228. if (ret < 0) {
  229. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
  230. return ret;
  231. }
  232. p->pict_type = AV_PICTURE_TYPE_I;
  233. p->key_frame = 1;
  234. out_buf = p->data[0];
  235. out_end = out_buf + p->linesize[0] * s->height;
  236. s->linesize = p->linesize[0];
  237. /* Skip header. */
  238. bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
  239. if (rle) {
  240. ret = read_rle_sgi(out_end, s);
  241. } else {
  242. ret = read_uncompressed_sgi(out_buf, s);
  243. }
  244. if (ret == 0) {
  245. *got_frame = 1;
  246. return avpkt->size;
  247. } else {
  248. return ret;
  249. }
  250. }
  251. static av_cold int sgi_decode_init(AVCodecContext *avctx)
  252. {
  253. SgiState *s = avctx->priv_data;
  254. s->avctx = avctx;
  255. return 0;
  256. }
  257. AVCodec ff_sgi_decoder = {
  258. .name = "sgi",
  259. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  260. .type = AVMEDIA_TYPE_VIDEO,
  261. .id = AV_CODEC_ID_SGI,
  262. .priv_data_size = sizeof(SgiState),
  263. .decode = decode_frame,
  264. .init = sgi_decode_init,
  265. .capabilities = AV_CODEC_CAP_DR1,
  266. };