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.

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