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.

273 lines
8.0KB

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