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.

314 lines
9.9KB

  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. int pal_size, pal_sample_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. switch (csize) {
  175. case 24: pal_sample_size = 3; break;
  176. case 16:
  177. case 15: pal_sample_size = 2; break;
  178. default:
  179. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  180. return -1;
  181. }
  182. pal_size = colors * pal_sample_size;
  183. CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
  184. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  185. buf += pal_size;
  186. else{
  187. int t;
  188. uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
  189. switch (pal_sample_size) {
  190. case 3:
  191. /* RGB24 */
  192. for (t = 0; t < colors; t++)
  193. *pal++ = (0xffU<<24) | bytestream_get_le24(&buf);
  194. break;
  195. case 2:
  196. /* RGB555 */
  197. for (t = 0; t < colors; t++) {
  198. uint32_t v = bytestream_get_le16(&buf);
  199. v = ((v & 0x7C00) << 9) |
  200. ((v & 0x03E0) << 6) |
  201. ((v & 0x001F) << 3);
  202. /* left bit replication */
  203. v |= (v & 0xE0E0E0U) >> 5;
  204. *pal++ = (0xffU<<24) | v;
  205. }
  206. break;
  207. }
  208. p->palette_has_changed = 1;
  209. }
  210. }
  211. if((compr & (~TGA_RLE)) == TGA_NODATA)
  212. memset(p->data[0], 0, p->linesize[0] * s->height);
  213. else{
  214. if(compr & TGA_RLE){
  215. int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
  216. if (res < 0)
  217. return -1;
  218. buf += res;
  219. }else{
  220. size_t img_size = s->width * ((s->bpp + 1) >> 3);
  221. CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
  222. for(y = 0; y < s->height; y++){
  223. #if HAVE_BIGENDIAN
  224. int x;
  225. if((s->bpp + 1) >> 3 == 2){
  226. uint16_t *dst16 = (uint16_t*)dst;
  227. for(x = 0; x < s->width; x++)
  228. dst16[x] = AV_RL16(buf + x * 2);
  229. }else if((s->bpp + 1) >> 3 == 4){
  230. uint32_t *dst32 = (uint32_t*)dst;
  231. for(x = 0; x < s->width; x++)
  232. dst32[x] = AV_RL32(buf + x * 4);
  233. }else
  234. #endif
  235. memcpy(dst, buf, img_size);
  236. dst += stride;
  237. buf += img_size;
  238. }
  239. }
  240. }
  241. if(flags & 0x10){ // right-to-left, needs horizontal flip
  242. int x;
  243. for(y = 0; y < s->height; y++){
  244. void *line = &p->data[0][y * p->linesize[0]];
  245. for(x = 0; x < s->width >> 1; x++){
  246. switch(s->bpp){
  247. case 32:
  248. FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]);
  249. break;
  250. case 24:
  251. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]);
  252. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]);
  253. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]);
  254. break;
  255. case 16:
  256. FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]);
  257. break;
  258. case 8:
  259. FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]);
  260. }
  261. }
  262. }
  263. }
  264. *picture= *(AVFrame*)&s->picture;
  265. *data_size = sizeof(AVPicture);
  266. return avpkt->size;
  267. }
  268. static av_cold int targa_init(AVCodecContext *avctx){
  269. TargaContext *s = avctx->priv_data;
  270. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  271. avctx->coded_frame= (AVFrame*)&s->picture;
  272. return 0;
  273. }
  274. static av_cold int targa_end(AVCodecContext *avctx){
  275. TargaContext *s = avctx->priv_data;
  276. if(s->picture.data[0])
  277. avctx->release_buffer(avctx, &s->picture);
  278. return 0;
  279. }
  280. AVCodec ff_targa_decoder = {
  281. .name = "targa",
  282. .type = AVMEDIA_TYPE_VIDEO,
  283. .id = CODEC_ID_TARGA,
  284. .priv_data_size = sizeof(TargaContext),
  285. .init = targa_init,
  286. .close = targa_end,
  287. .decode = decode_frame,
  288. .capabilities = CODEC_CAP_DR1,
  289. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  290. };