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
7.9KB

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