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.

271 lines
7.9KB

  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 "sgi.h"
  25. typedef struct SgiState {
  26. AVFrame picture;
  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 *data_size,
  139. AVPacket *avpkt)
  140. {
  141. SgiState *s = avctx->priv_data;
  142. AVFrame *picture = data;
  143. AVFrame *p = &s->picture;
  144. unsigned int dimension, rle;
  145. int ret = 0;
  146. uint8_t *out_buf, *out_end;
  147. bytestream2_init(&s->g, avpkt->data, avpkt->size);
  148. if (bytestream2_get_bytes_left(&s->g) < SGI_HEADER_SIZE) {
  149. av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
  150. return AVERROR_INVALIDDATA;
  151. }
  152. /* Test for SGI magic. */
  153. if (bytestream2_get_be16(&s->g) != SGI_MAGIC) {
  154. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  155. return AVERROR_INVALIDDATA;
  156. }
  157. rle = bytestream2_get_byte(&s->g);
  158. s->bytes_per_channel = bytestream2_get_byte(&s->g);
  159. dimension = bytestream2_get_be16(&s->g);
  160. s->width = bytestream2_get_be16(&s->g);
  161. s->height = bytestream2_get_be16(&s->g);
  162. s->depth = bytestream2_get_be16(&s->g);
  163. if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
  164. av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
  165. return -1;
  166. }
  167. /* Check for supported image dimensions. */
  168. if (dimension != 2 && dimension != 3) {
  169. av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
  170. return -1;
  171. }
  172. if (s->depth == SGI_GRAYSCALE) {
  173. avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
  174. } else if (s->depth == SGI_RGB) {
  175. avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
  176. } else if (s->depth == SGI_RGBA && s->bytes_per_channel == 1) {
  177. avctx->pix_fmt = PIX_FMT_RGBA;
  178. } else {
  179. av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
  180. return -1;
  181. }
  182. if (av_image_check_size(s->width, s->height, 0, avctx))
  183. return -1;
  184. avcodec_set_dimensions(avctx, s->width, s->height);
  185. if (p->data[0])
  186. avctx->release_buffer(avctx, p);
  187. p->reference = 0;
  188. if (avctx->get_buffer(avctx, p) < 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. *picture = s->picture;
  206. *data_size = sizeof(AVPicture);
  207. return avpkt->size;
  208. } else {
  209. return ret;
  210. }
  211. }
  212. static av_cold int sgi_init(AVCodecContext *avctx){
  213. SgiState *s = avctx->priv_data;
  214. avcodec_get_frame_defaults(&s->picture);
  215. avctx->coded_frame = &s->picture;
  216. return 0;
  217. }
  218. static av_cold int sgi_end(AVCodecContext *avctx)
  219. {
  220. SgiState * const s = avctx->priv_data;
  221. if (s->picture.data[0])
  222. avctx->release_buffer(avctx, &s->picture);
  223. return 0;
  224. }
  225. AVCodec ff_sgi_decoder = {
  226. .name = "sgi",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .id = CODEC_ID_SGI,
  229. .priv_data_size = sizeof(SgiState),
  230. .init = sgi_init,
  231. .close = sgi_end,
  232. .decode = decode_frame,
  233. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  234. };