sgidec.c 7.4 KB

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