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.

286 lines
8.3KB

  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. } SgiState;
  34. /**
  35. * Expand an RLE row into a channel.
  36. * @param in_buf input buffer
  37. * @param in_end end of input buffer
  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(const uint8_t *in_buf, const uint8_t* in_end,
  44. unsigned char *out_buf, uint8_t* out_end, int pixelstride)
  45. {
  46. unsigned char pixel, count;
  47. unsigned char *orig = out_buf;
  48. while (1) {
  49. if(in_buf + 1 > in_end) return -1;
  50. pixel = bytestream_get_byte(&in_buf);
  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 = bytestream_get_byte(&in_buf);
  59. out_buf += pixelstride;
  60. }
  61. } else {
  62. pixel = bytestream_get_byte(&in_buf);
  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 in_buf input buffer
  74. * @param in_end end of input 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(unsigned char* out_buf, const uint8_t *in_buf,
  79. const uint8_t *in_end, SgiState* s)
  80. {
  81. uint8_t *dest_row;
  82. unsigned int len = s->height * s->depth * 4;
  83. const uint8_t *start_table = in_buf;
  84. unsigned int y, z;
  85. unsigned int start_offset;
  86. /* size of RLE offset and length tables */
  87. if(len * 2 > in_end - in_buf) {
  88. return AVERROR_INVALIDDATA;
  89. }
  90. in_buf -= SGI_HEADER_SIZE;
  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 = bytestream_get_be32(&start_table);
  96. if(start_offset > in_end - in_buf) {
  97. return AVERROR_INVALIDDATA;
  98. }
  99. if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z,
  100. dest_row + FFABS(s->linesize), s->depth) != s->width)
  101. return AVERROR_INVALIDDATA;
  102. }
  103. }
  104. return 0;
  105. }
  106. static av_always_inline void copy_loop(uint8_t *out_buf, const uint8_t *in_buf,
  107. unsigned offset, unsigned bytes_per_channel,
  108. SgiState *s)
  109. {
  110. int x, y, z;
  111. for (y = s->height - 1; y >= 0; y--) {
  112. uint8_t *line = out_buf + (y * s->linesize);
  113. for (x = s->width; x > 0; x--) {
  114. const uint8_t *ptr = in_buf;
  115. in_buf += bytes_per_channel;
  116. for(z = 0; z < s->depth; z ++) {
  117. memcpy(line, ptr, bytes_per_channel);
  118. line += bytes_per_channel;
  119. ptr += offset;
  120. }
  121. }
  122. }
  123. }
  124. /**
  125. * Read an uncompressed SGI image.
  126. * @param out_buf output buffer
  127. * @param out_end end ofoutput buffer
  128. * @param in_buf input buffer
  129. * @param in_end end of input buffer
  130. * @param s the current image state
  131. * @return 0 if read success, otherwise return -1.
  132. */
  133. static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
  134. const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)
  135. {
  136. unsigned int offset = s->height * s->width * s->bytes_per_channel;
  137. /* Test buffer size. */
  138. if (offset * s->depth > in_end - in_buf) {
  139. return -1;
  140. }
  141. if (s->bytes_per_channel == 2) {
  142. copy_loop(out_buf, in_buf, offset, 2, s);
  143. } else {
  144. av_assert1(s->bytes_per_channel == 1);
  145. copy_loop(out_buf, in_buf, offset, 1, s);
  146. }
  147. return 0;
  148. }
  149. static int decode_frame(AVCodecContext *avctx,
  150. void *data, int *data_size,
  151. AVPacket *avpkt)
  152. {
  153. const uint8_t *in_buf = avpkt->data;
  154. int buf_size = avpkt->size;
  155. SgiState *s = avctx->priv_data;
  156. AVFrame *picture = data;
  157. AVFrame *p = &s->picture;
  158. const uint8_t *in_end = in_buf + buf_size;
  159. unsigned int dimension, rle;
  160. int ret = 0;
  161. uint8_t *out_buf, *out_end;
  162. if (buf_size < SGI_HEADER_SIZE){
  163. av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size);
  164. return -1;
  165. }
  166. /* Test for SGI magic. */
  167. if (bytestream_get_be16(&in_buf) != SGI_MAGIC) {
  168. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  169. return -1;
  170. }
  171. rle = bytestream_get_byte(&in_buf);
  172. s->bytes_per_channel = bytestream_get_byte(&in_buf);
  173. dimension = bytestream_get_be16(&in_buf);
  174. s->width = bytestream_get_be16(&in_buf);
  175. s->height = bytestream_get_be16(&in_buf);
  176. s->depth = bytestream_get_be16(&in_buf);
  177. if (s->bytes_per_channel != 1 && (s->bytes_per_channel != 2 || rle)) {
  178. av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
  179. return -1;
  180. }
  181. /* Check for supported image dimensions. */
  182. if (dimension != 2 && dimension != 3) {
  183. av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
  184. return -1;
  185. }
  186. if (s->depth == SGI_GRAYSCALE) {
  187. avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_GRAY16BE : PIX_FMT_GRAY8;
  188. } else if (s->depth == SGI_RGB) {
  189. avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGB48BE : PIX_FMT_RGB24;
  190. } else if (s->depth == SGI_RGBA) {
  191. avctx->pix_fmt = s->bytes_per_channel == 2 ? PIX_FMT_RGBA64BE : PIX_FMT_RGBA;
  192. } else {
  193. av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
  194. return -1;
  195. }
  196. if (av_image_check_size(s->width, s->height, 0, avctx))
  197. return -1;
  198. avcodec_set_dimensions(avctx, s->width, s->height);
  199. if (p->data[0])
  200. avctx->release_buffer(avctx, p);
  201. p->reference = 0;
  202. if (avctx->get_buffer(avctx, p) < 0) {
  203. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed.\n");
  204. return -1;
  205. }
  206. p->pict_type = AV_PICTURE_TYPE_I;
  207. p->key_frame = 1;
  208. out_buf = p->data[0];
  209. out_end = out_buf + p->linesize[0] * s->height;
  210. s->linesize = p->linesize[0];
  211. /* Skip header. */
  212. in_buf += SGI_HEADER_SIZE - 12;
  213. if (rle) {
  214. ret = read_rle_sgi(out_end, in_buf, in_end, s);
  215. } else {
  216. ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s);
  217. }
  218. if (ret == 0) {
  219. *picture = s->picture;
  220. *data_size = sizeof(AVPicture);
  221. return buf_size;
  222. } else {
  223. return -1;
  224. }
  225. }
  226. static av_cold int sgi_init(AVCodecContext *avctx){
  227. SgiState *s = avctx->priv_data;
  228. avcodec_get_frame_defaults(&s->picture);
  229. avctx->coded_frame = &s->picture;
  230. return 0;
  231. }
  232. static av_cold int sgi_end(AVCodecContext *avctx)
  233. {
  234. SgiState * const s = avctx->priv_data;
  235. if (s->picture.data[0])
  236. avctx->release_buffer(avctx, &s->picture);
  237. return 0;
  238. }
  239. AVCodec ff_sgi_decoder = {
  240. .name = "sgi",
  241. .type = AVMEDIA_TYPE_VIDEO,
  242. .id = CODEC_ID_SGI,
  243. .priv_data_size = sizeof(SgiState),
  244. .init = sgi_init,
  245. .close = sgi_end,
  246. .decode = decode_frame,
  247. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  248. };