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.

259 lines
7.6KB

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