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.

270 lines
8.1KB

  1. /*
  2. * Targa (.tga) image decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  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/intreadwrite.h"
  22. #include "libavutil/imgutils.h"
  23. #include "avcodec.h"
  24. #include "bytestream.h"
  25. #include "targa.h"
  26. typedef struct TargaContext {
  27. AVFrame picture;
  28. int width, height;
  29. int bpp;
  30. int color_type;
  31. int compression_type;
  32. } TargaContext;
  33. #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
  34. if(needed > buf_end - buf){ \
  35. av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
  36. return -1; \
  37. } \
  38. static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
  39. {
  40. int i, x, y;
  41. int depth = (bpp + 1) >> 3;
  42. int type, count;
  43. int diff;
  44. const uint8_t *src_end = src + src_size;
  45. diff = stride - w * depth;
  46. x = y = 0;
  47. while(y < h){
  48. CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
  49. type = *src++;
  50. count = (type & 0x7F) + 1;
  51. type &= 0x80;
  52. if((x + count > w) && (x + count + 1 > (h - y) * w)){
  53. av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
  54. return -1;
  55. }
  56. if(type){
  57. CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
  58. }else{
  59. CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
  60. }
  61. for(i = 0; i < count; i++){
  62. switch(depth){
  63. case 1:
  64. *dst = *src;
  65. break;
  66. case 2:
  67. *((uint16_t*)dst) = AV_RL16(src);
  68. break;
  69. case 3:
  70. dst[0] = src[0];
  71. dst[1] = src[1];
  72. dst[2] = src[2];
  73. break;
  74. case 4:
  75. *((uint32_t*)dst) = AV_RL32(src);
  76. break;
  77. }
  78. dst += depth;
  79. if(!type)
  80. src += depth;
  81. x++;
  82. if(x == w){
  83. x = 0;
  84. y++;
  85. dst += diff;
  86. }
  87. }
  88. if(type)
  89. src += depth;
  90. }
  91. return src_size;
  92. }
  93. static int decode_frame(AVCodecContext *avctx,
  94. void *data, int *data_size,
  95. AVPacket *avpkt)
  96. {
  97. const uint8_t *buf = avpkt->data;
  98. const uint8_t *buf_end = avpkt->data + avpkt->size;
  99. TargaContext * const s = avctx->priv_data;
  100. AVFrame *picture = data;
  101. AVFrame * const p= (AVFrame*)&s->picture;
  102. uint8_t *dst;
  103. int stride;
  104. int idlen, pal, compr, y, w, h, bpp, flags;
  105. int first_clr, colors, csize;
  106. /* parse image header */
  107. CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
  108. idlen = *buf++;
  109. pal = *buf++;
  110. compr = *buf++;
  111. first_clr = bytestream_get_le16(&buf);
  112. colors = bytestream_get_le16(&buf);
  113. csize = *buf++;
  114. if (!pal && (first_clr || colors || csize)) {
  115. av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
  116. // specification says we should ignore those value in this case
  117. first_clr = colors = csize = 0;
  118. }
  119. buf += 2; /* x */
  120. y = bytestream_get_le16(&buf);
  121. w = bytestream_get_le16(&buf);
  122. h = bytestream_get_le16(&buf);
  123. bpp = *buf++;
  124. flags = *buf++;
  125. //skip identifier if any
  126. CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
  127. buf += idlen;
  128. s->bpp = bpp;
  129. s->width = w;
  130. s->height = h;
  131. switch(s->bpp){
  132. case 8:
  133. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  134. break;
  135. case 15:
  136. avctx->pix_fmt = PIX_FMT_RGB555;
  137. break;
  138. case 16:
  139. avctx->pix_fmt = PIX_FMT_RGB555;
  140. break;
  141. case 24:
  142. avctx->pix_fmt = PIX_FMT_BGR24;
  143. break;
  144. case 32:
  145. avctx->pix_fmt = PIX_FMT_RGB32;
  146. break;
  147. default:
  148. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  149. return -1;
  150. }
  151. if(s->picture.data[0])
  152. avctx->release_buffer(avctx, &s->picture);
  153. if(av_image_check_size(w, h, 0, avctx))
  154. return -1;
  155. if(w != avctx->width || h != avctx->height)
  156. avcodec_set_dimensions(avctx, w, h);
  157. if(avctx->get_buffer(avctx, p) < 0){
  158. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  159. return -1;
  160. }
  161. if(flags & 0x20){
  162. dst = p->data[0];
  163. stride = p->linesize[0];
  164. }else{ //image is upside-down
  165. dst = p->data[0] + p->linesize[0] * (h - 1);
  166. stride = -p->linesize[0];
  167. }
  168. if(colors){
  169. size_t pal_size;
  170. if((colors + first_clr) > 256){
  171. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  172. return -1;
  173. }
  174. if(csize != 24){
  175. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  176. return -1;
  177. }
  178. pal_size = colors * ((csize + 1) >> 3);
  179. CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
  180. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  181. buf += pal_size;
  182. else{
  183. int t;
  184. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  185. for(t = 0; t < colors; t++){
  186. *pal++ = (0xff<<24) | bytestream_get_le24(&buf);
  187. }
  188. p->palette_has_changed = 1;
  189. }
  190. }
  191. if((compr & (~TGA_RLE)) == TGA_NODATA)
  192. memset(p->data[0], 0, p->linesize[0] * s->height);
  193. else{
  194. if(compr & TGA_RLE){
  195. int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
  196. if (res < 0)
  197. return -1;
  198. buf += res;
  199. }else{
  200. size_t img_size = s->width * ((s->bpp + 1) >> 3);
  201. CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
  202. for(y = 0; y < s->height; y++){
  203. #if HAVE_BIGENDIAN
  204. int x;
  205. if((s->bpp + 1) >> 3 == 2){
  206. uint16_t *dst16 = (uint16_t*)dst;
  207. for(x = 0; x < s->width; x++)
  208. dst16[x] = AV_RL16(buf + x * 2);
  209. }else if((s->bpp + 1) >> 3 == 4){
  210. uint32_t *dst32 = (uint32_t*)dst;
  211. for(x = 0; x < s->width; x++)
  212. dst32[x] = AV_RL32(buf + x * 4);
  213. }else
  214. #endif
  215. memcpy(dst, buf, img_size);
  216. dst += stride;
  217. buf += img_size;
  218. }
  219. }
  220. }
  221. *picture= *(AVFrame*)&s->picture;
  222. *data_size = sizeof(AVPicture);
  223. return avpkt->size;
  224. }
  225. static av_cold int targa_init(AVCodecContext *avctx){
  226. TargaContext *s = avctx->priv_data;
  227. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  228. avctx->coded_frame= (AVFrame*)&s->picture;
  229. return 0;
  230. }
  231. static av_cold int targa_end(AVCodecContext *avctx){
  232. TargaContext *s = avctx->priv_data;
  233. if(s->picture.data[0])
  234. avctx->release_buffer(avctx, &s->picture);
  235. return 0;
  236. }
  237. AVCodec ff_targa_decoder = {
  238. .name = "targa",
  239. .type = AVMEDIA_TYPE_VIDEO,
  240. .id = CODEC_ID_TARGA,
  241. .priv_data_size = sizeof(TargaContext),
  242. .init = targa_init,
  243. .close = targa_end,
  244. .decode = decode_frame,
  245. .capabilities = CODEC_CAP_DR1,
  246. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  247. };