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.

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