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.

245 lines
7.4KB

  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. unsigned int width;
  28. unsigned int height;
  29. unsigned int depth;
  30. unsigned int bytes_per_channel;
  31. int linesize;
  32. GetByteContext g;
  33. } SgiState;
  34. /**
  35. * Expand an RLE row into a channel.
  36. * @param s the current image state
  37. * @param out_buf Points to one line after the output buffer.
  38. * @param out_end end of line in output buffer
  39. * @param pixelstride pixel stride of input buffer
  40. * @return size of output in bytes, -1 if buffer overflows
  41. */
  42. static int expand_rle_row(SgiState *s, uint8_t *out_buf,
  43. uint8_t *out_end, int pixelstride)
  44. {
  45. unsigned char pixel, count;
  46. unsigned char *orig = out_buf;
  47. while (1) {
  48. if (bytestream2_get_bytes_left(&s->g) < 1)
  49. return AVERROR_INVALIDDATA;
  50. pixel = bytestream2_get_byteu(&s->g);
  51. if (!(count = (pixel & 0x7f))) {
  52. return (out_buf - orig) / pixelstride;
  53. }
  54. /* Check for buffer overflow. */
  55. if(out_buf + pixelstride * count >= out_end) return -1;
  56. if (pixel & 0x80) {
  57. while (count--) {
  58. *out_buf = bytestream2_get_byte(&s->g);
  59. out_buf += pixelstride;
  60. }
  61. } else {
  62. pixel = bytestream2_get_byte(&s->g);
  63. while (count--) {
  64. *out_buf = pixel;
  65. out_buf += pixelstride;
  66. }
  67. }
  68. }
  69. }
  70. /**
  71. * Read a run length encoded SGI image.
  72. * @param out_buf output buffer
  73. * @param s the current image state
  74. * @return 0 if no error, else return error number.
  75. */
  76. static int read_rle_sgi(uint8_t *out_buf, SgiState *s)
  77. {
  78. uint8_t *dest_row;
  79. unsigned int len = s->height * s->depth * 4;
  80. GetByteContext g_table = s->g;
  81. unsigned int y, z;
  82. unsigned int start_offset;
  83. /* size of RLE offset and length tables */
  84. if (len * 2 > bytestream2_get_bytes_left(&s->g)) {
  85. return AVERROR_INVALIDDATA;
  86. }
  87. for (z = 0; z < s->depth; z++) {
  88. dest_row = out_buf;
  89. for (y = 0; y < s->height; y++) {
  90. dest_row -= s->linesize;
  91. start_offset = bytestream2_get_be32(&g_table);
  92. bytestream2_seek(&s->g, start_offset, SEEK_SET);
  93. if (expand_rle_row(s, dest_row + z, dest_row + FFABS(s->linesize),
  94. s->depth) != s->width) {
  95. return AVERROR_INVALIDDATA;
  96. }
  97. }
  98. }
  99. return 0;
  100. }
  101. /**
  102. * Read an uncompressed SGI image.
  103. * @param out_buf output buffer
  104. * @param out_end end ofoutput buffer
  105. * @param s the current image state
  106. * @return 0 if read success, otherwise return -1.
  107. */
  108. static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
  109. SgiState *s)
  110. {
  111. int x, y, z;
  112. unsigned int offset = s->height * s->width * s->bytes_per_channel;
  113. GetByteContext gp[4];
  114. /* Test buffer size. */
  115. if (offset * s->depth > bytestream2_get_bytes_left(&s->g))
  116. return AVERROR_INVALIDDATA;
  117. /* Create a reader for each plane */
  118. for (z = 0; z < s->depth; z++) {
  119. gp[z] = s->g;
  120. bytestream2_skip(&gp[z], z * offset);
  121. }
  122. for (y = s->height - 1; y >= 0; y--) {
  123. out_end = out_buf + (y * s->linesize);
  124. if (s->bytes_per_channel == 1) {
  125. for (x = s->width; x > 0; x--)
  126. for (z = 0; z < s->depth; z++)
  127. *out_end++ = bytestream2_get_byteu(&gp[z]);
  128. } else {
  129. uint16_t *out16 = (uint16_t *)out_end;
  130. for (x = s->width; x > 0; x--)
  131. for (z = 0; z < s->depth; z++)
  132. *out16++ = bytestream2_get_ne16u(&gp[z]);
  133. }
  134. }
  135. return 0;
  136. }
  137. static int decode_frame(AVCodecContext *avctx,
  138. void *data, int *got_frame,
  139. AVPacket *avpkt)
  140. {
  141. SgiState *s = avctx->priv_data;
  142. AVFrame *p = data;
  143. unsigned int dimension, rle;
  144. int ret = 0;
  145. uint8_t *out_buf, *out_end;
  146. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  147. if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
  148. av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
  149. return AVERROR_INVALIDDATA;
  150. }
  151. /* Test for SGI magic. */
  152. if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
  153. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  154. return AVERROR_INVALIDDATA;
  155. }
  156. rle = bytestream2_get_byte(&s->g);
  157. s->bytes_per_channel = bytestream2_get_byte(&s->g);
  158. dimension = bytestream2_get_be16(&s->g);
  159. s->width = bytestream2_get_be16(&s->g);
  160. s->height = bytestream2_get_be16(&s->g);
  161. s->depth = bytestream2_get_be16(&s->g);
  162. if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
  163. av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
  164. return -1;
  165. }
  166. /* Check for supported image dimensions. */
  167. if (dimension != 2 && dimension != 3) {
  168. av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
  169. return -1;
  170. }
  171. if (s->depth == SGI_GRAYSCALE) {
  172. avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
  173. } else if (s->depth == SGI_RGB) {
  174. avctx->pix_fmt = s->bytes_per_channel == 2 ? AV_PIX_FMT_RGB48BE : AV_PIX_FMT_RGB24;
  175. } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
  176. avctx->pix_fmt = AV_PIX_FMT_RGBA;
  177. } else {
  178. av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
  179. return -1;
  180. }
  181. if (av_image_check_size(s->width, s->height, 0, avctx))
  182. return -1;
  183. avcodec_set_dimensions(avctx, s->width, s->height);
  184. if (ff_get_buffer(avctx, p, 0) < 0) {
  185. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
  186. return -1;
  187. }
  188. p->pict_type = AV_PICTURE_TYPE_I;
  189. p->key_frame = 1;
  190. out_buf = p->data[0];
  191. out_end = out_buf + p->linesize[0] * s->height;
  192. s->linesize = p->linesize[0];
  193. /* Skip header. */
  194. bytestream2_seek(&s->g, SGI_HEADER_SIZE, SEEK_SET);
  195. if (rle) {
  196. ret = read_rle_sgi(out_end, s);
  197. } else {
  198. ret = read_uncompressed_sgi(out_buf, out_end, s);
  199. }
  200. if (ret == 0) {
  201. *got_frame = 1;
  202. return avpkt->size;
  203. } else {
  204. return ret;
  205. }
  206. }
  207. AVCodec ff_sgi_decoder = {
  208. .name = "sgi",
  209. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  210. .type = AVMEDIA_TYPE_VIDEO,
  211. .id = AV_CODEC_ID_SGI,
  212. .priv_data_size = sizeof(SgiState),
  213. .decode = decode_frame,
  214. .capabilities = CODEC_CAP_DR1,
  215. };