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

  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 "avcodec.h"
  22. #include "bytestream.h"
  23. #include "sgi.h"
  24. typedef struct SgiState {
  25. AVFrame picture;
  26. unsigned int width;
  27. unsigned int height;
  28. unsigned int depth;
  29. unsigned int bytes_per_channel;
  30. int linesize;
  31. } SgiState;
  32. /**
  33. * Expand an RLE row into a channel.
  34. * @param in_buf input buffer
  35. * @param in_end end of input buffer
  36. * @param out_buf Points to one line after the output buffer.
  37. * @param out_end end of line in output buffer
  38. * @param pixelstride pixel stride of input buffer
  39. * @return size of output in bytes, -1 if buffer overflows
  40. */
  41. static int expand_rle_row(const uint8_t *in_buf, const uint8_t* in_end,
  42. unsigned char *out_buf, uint8_t* out_end, int pixelstride)
  43. {
  44. unsigned char pixel, count;
  45. unsigned char *orig = out_buf;
  46. while (1) {
  47. if(in_buf + 1 > in_end) return -1;
  48. pixel = bytestream_get_byte(&in_buf);
  49. if (!(count = (pixel & 0x7f))) {
  50. return (out_buf - orig) / pixelstride;
  51. }
  52. /* Check for buffer overflow. */
  53. if(out_buf + pixelstride * count >= out_end) return -1;
  54. if (pixel & 0x80) {
  55. while (count--) {
  56. *out_buf = bytestream_get_byte(&in_buf);
  57. out_buf += pixelstride;
  58. }
  59. } else {
  60. pixel = bytestream_get_byte(&in_buf);
  61. while (count--) {
  62. *out_buf = pixel;
  63. out_buf += pixelstride;
  64. }
  65. }
  66. }
  67. }
  68. /**
  69. * Read a run length encoded SGI image.
  70. * @param out_buf output buffer
  71. * @param in_buf input buffer
  72. * @param in_end end of input buffer
  73. * @param s the current image state
  74. * @return 0 if no error, else return error number.
  75. */
  76. static int read_rle_sgi(unsigned char* out_buf, const uint8_t *in_buf,
  77. const uint8_t *in_end, SgiState* s)
  78. {
  79. uint8_t *dest_row;
  80. unsigned int len = s->height * s->depth * 4;
  81. const uint8_t *start_table = in_buf;
  82. unsigned int y, z;
  83. unsigned int start_offset;
  84. /* size of RLE offset and length tables */
  85. if(len * 2 > in_end - in_buf) {
  86. return AVERROR_INVALIDDATA;
  87. }
  88. in_buf -= SGI_HEADER_SIZE;
  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 = bytestream_get_be32(&start_table);
  94. if(start_offset > in_end - in_buf) {
  95. return AVERROR_INVALIDDATA;
  96. }
  97. if (expand_rle_row(in_buf + start_offset, in_end, dest_row + z,
  98. dest_row + FFABS(s->linesize), s->depth) != s->width)
  99. return AVERROR_INVALIDDATA;
  100. }
  101. }
  102. return 0;
  103. }
  104. /**
  105. * Read an uncompressed SGI image.
  106. * @param out_buf output buffer
  107. * @param out_end end ofoutput buffer
  108. * @param in_buf input buffer
  109. * @param in_end end of input buffer
  110. * @param s the current image state
  111. * @return 0 if read success, otherwise return -1.
  112. */
  113. static int read_uncompressed_sgi(unsigned char* out_buf, uint8_t* out_end,
  114. const uint8_t *in_buf, const uint8_t *in_end, SgiState* s)
  115. {
  116. int x, y, z;
  117. const uint8_t *ptr;
  118. unsigned int offset = s->height * s->width * s->bytes_per_channel;
  119. /* Test buffer size. */
  120. if (offset * s->depth > in_end - in_buf) {
  121. return -1;
  122. }
  123. for (y = s->height - 1; y >= 0; y--) {
  124. out_end = out_buf + (y * s->linesize);
  125. for (x = s->width; x > 0; x--) {
  126. ptr = in_buf += s->bytes_per_channel;
  127. for(z = 0; z < s->depth; z ++) {
  128. memcpy(out_end, ptr, s->bytes_per_channel);
  129. out_end += s->bytes_per_channel;
  130. ptr += offset;
  131. }
  132. }
  133. }
  134. return 0;
  135. }
  136. static int decode_frame(AVCodecContext *avctx,
  137. void *data, int *data_size,
  138. AVPacket *avpkt)
  139. {
  140. const uint8_t *in_buf = avpkt->data;
  141. int buf_size = avpkt->size;
  142. SgiState *s = avctx->priv_data;
  143. AVFrame *picture = data;
  144. AVFrame *p = &s->picture;
  145. const uint8_t *in_end = in_buf + buf_size;
  146. unsigned int dimension, rle;
  147. int ret = 0;
  148. uint8_t *out_buf, *out_end;
  149. if (buf_size < SGI_HEADER_SIZE){
  150. av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", buf_size);
  151. return -1;
  152. }
  153. /* Test for SGI magic. */
  154. if (bytestream_get_be16(&in_buf) != SGI_MAGIC) {
  155. av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
  156. return -1;
  157. }
  158. rle = bytestream_get_byte(&in_buf);
  159. s->bytes_per_channel = bytestream_get_byte(&in_buf);
  160. dimension = bytestream_get_be16(&in_buf);
  161. s->width = bytestream_get_be16(&in_buf);
  162. s->height = bytestream_get_be16(&in_buf);
  163. s->depth = bytestream_get_be16(&in_buf);
  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 && s->bytes_per_channel == 1) {
  178. avctx->pix_fmt = PIX_FMT_RGBA;
  179. } else {
  180. av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
  181. return -1;
  182. }
  183. if (avcodec_check_dimensions(avctx, s->width, s->height))
  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 = FF_I_TYPE;
  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. in_buf += SGI_HEADER_SIZE - 12;
  200. if (rle) {
  201. ret = read_rle_sgi(out_end, in_buf, in_end, s);
  202. } else {
  203. ret = read_uncompressed_sgi(out_buf, out_end, in_buf, in_end, s);
  204. }
  205. if (ret == 0) {
  206. *picture = s->picture;
  207. *data_size = sizeof(AVPicture);
  208. return buf_size;
  209. } else {
  210. return -1;
  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 sgi_decoder = {
  227. "sgi",
  228. AVMEDIA_TYPE_VIDEO,
  229. CODEC_ID_SGI,
  230. sizeof(SgiState),
  231. sgi_init,
  232. NULL,
  233. sgi_end,
  234. decode_frame,
  235. .long_name = NULL_IF_CONFIG_SMALL("SGI image"),
  236. };