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.

267 lines
7.7KB

  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, 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. buf++; /* pal */
  110. compr = *buf++;
  111. first_clr = bytestream_get_le16(&buf);
  112. colors = bytestream_get_le16(&buf);
  113. csize = *buf++;
  114. buf += 2; /* x */
  115. y = bytestream_get_le16(&buf);
  116. w = bytestream_get_le16(&buf);
  117. h = bytestream_get_le16(&buf);
  118. bpp = *buf++;
  119. flags = *buf++;
  120. //skip identifier if any
  121. CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
  122. buf += idlen;
  123. s->bpp = bpp;
  124. s->width = w;
  125. s->height = h;
  126. switch(s->bpp){
  127. case 8:
  128. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  129. break;
  130. case 15:
  131. avctx->pix_fmt = PIX_FMT_RGB555;
  132. break;
  133. case 16:
  134. avctx->pix_fmt = PIX_FMT_RGB555;
  135. break;
  136. case 24:
  137. avctx->pix_fmt = PIX_FMT_BGR24;
  138. break;
  139. case 32:
  140. avctx->pix_fmt = PIX_FMT_RGB32;
  141. break;
  142. default:
  143. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  144. return -1;
  145. }
  146. if(s->picture.data[0])
  147. avctx->release_buffer(avctx, &s->picture);
  148. if(av_image_check_size(w, h, 0, avctx))
  149. return -1;
  150. if(w != avctx->width || h != avctx->height)
  151. avcodec_set_dimensions(avctx, w, h);
  152. if(avctx->get_buffer(avctx, p) < 0){
  153. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  154. return -1;
  155. }
  156. if(flags & 0x20){
  157. dst = p->data[0];
  158. stride = p->linesize[0];
  159. }else{ //image is upside-down
  160. dst = p->data[0] + p->linesize[0] * (h - 1);
  161. stride = -p->linesize[0];
  162. }
  163. if(colors){
  164. size_t pal_size;
  165. if((colors + first_clr) > 256){
  166. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  167. return -1;
  168. }
  169. if(csize != 24){
  170. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  171. return -1;
  172. }
  173. pal_size = colors * ((csize + 1) >> 3);
  174. CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
  175. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  176. buf += pal_size;
  177. else{
  178. int t;
  179. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  180. for(t = 0; t < colors; t++){
  181. *pal++ = (0xff<<24) | bytestream_get_le24(&buf);
  182. }
  183. p->palette_has_changed = 1;
  184. }
  185. }
  186. if((compr & (~TGA_RLE)) == TGA_NODATA)
  187. memset(p->data[0], 0, p->linesize[0] * s->height);
  188. else{
  189. if(compr & TGA_RLE){
  190. int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
  191. if (res < 0)
  192. return -1;
  193. buf += res;
  194. }else{
  195. size_t img_size = s->width * ((s->bpp + 1) >> 3);
  196. CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
  197. for(y = 0; y < s->height; y++){
  198. #if HAVE_BIGENDIAN
  199. int x;
  200. if((s->bpp + 1) >> 3 == 2){
  201. uint16_t *dst16 = (uint16_t*)dst;
  202. for(x = 0; x < s->width; x++)
  203. dst16[x] = AV_RL16(buf + x * 2);
  204. }else if((s->bpp + 1) >> 3 == 4){
  205. uint32_t *dst32 = (uint32_t*)dst;
  206. for(x = 0; x < s->width; x++)
  207. dst32[x] = AV_RL32(buf + x * 4);
  208. }else
  209. #endif
  210. memcpy(dst, buf, img_size);
  211. dst += stride;
  212. buf += img_size;
  213. }
  214. }
  215. }
  216. *picture= *(AVFrame*)&s->picture;
  217. *data_size = sizeof(AVPicture);
  218. return avpkt->size;
  219. }
  220. static av_cold int targa_init(AVCodecContext *avctx){
  221. TargaContext *s = avctx->priv_data;
  222. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  223. avctx->coded_frame= (AVFrame*)&s->picture;
  224. return 0;
  225. }
  226. static av_cold int targa_end(AVCodecContext *avctx){
  227. TargaContext *s = avctx->priv_data;
  228. if(s->picture.data[0])
  229. avctx->release_buffer(avctx, &s->picture);
  230. return 0;
  231. }
  232. AVCodec ff_targa_decoder = {
  233. "targa",
  234. AVMEDIA_TYPE_VIDEO,
  235. CODEC_ID_TARGA,
  236. sizeof(TargaContext),
  237. targa_init,
  238. NULL,
  239. targa_end,
  240. decode_frame,
  241. CODEC_CAP_DR1,
  242. NULL,
  243. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  244. };