targa.c 7.8 KB

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