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.

300 lines
9.4KB

  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. AV_WN16A(dst, AV_RN16A(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. AV_WN32A(dst, AV_RN32A(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. case 16:
  137. avctx->pix_fmt = PIX_FMT_RGB555LE;
  138. break;
  139. case 24:
  140. avctx->pix_fmt = PIX_FMT_BGR24;
  141. break;
  142. case 32:
  143. avctx->pix_fmt = PIX_FMT_BGRA;
  144. break;
  145. default:
  146. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  147. return -1;
  148. }
  149. if(s->picture.data[0])
  150. avctx->release_buffer(avctx, &s->picture);
  151. if(av_image_check_size(w, h, 0, avctx))
  152. return -1;
  153. if(w != avctx->width || h != avctx->height)
  154. avcodec_set_dimensions(avctx, w, h);
  155. if(avctx->get_buffer(avctx, p) < 0){
  156. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  157. return -1;
  158. }
  159. if(flags & 0x20){
  160. dst = p->data[0];
  161. stride = p->linesize[0];
  162. }else{ //image is upside-down
  163. dst = p->data[0] + p->linesize[0] * (h - 1);
  164. stride = -p->linesize[0];
  165. }
  166. if(colors){
  167. int pal_size, pal_sample_size;
  168. if((colors + first_clr) > 256){
  169. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  170. return -1;
  171. }
  172. switch (csize) {
  173. case 24: pal_sample_size = 3; break;
  174. case 16:
  175. case 15: pal_sample_size = 2; break;
  176. default:
  177. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  178. return -1;
  179. }
  180. pal_size = colors * pal_sample_size;
  181. CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
  182. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  183. buf += pal_size;
  184. else{
  185. int t;
  186. uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
  187. switch (pal_sample_size) {
  188. case 3:
  189. /* RGB24 */
  190. for (t = 0; t < colors; t++)
  191. *pal++ = (0xffU<<24) | bytestream_get_le24(&buf);
  192. break;
  193. case 2:
  194. /* RGB555 */
  195. for (t = 0; t < colors; t++) {
  196. uint32_t v = bytestream_get_le16(&buf);
  197. v = ((v & 0x7C00) << 9) |
  198. ((v & 0x03E0) << 6) |
  199. ((v & 0x001F) << 3);
  200. /* left bit replication */
  201. v |= (v & 0xE0E0E0U) >> 5;
  202. *pal++ = (0xffU<<24) | v;
  203. }
  204. break;
  205. }
  206. p->palette_has_changed = 1;
  207. }
  208. }
  209. if((compr & (~TGA_RLE)) == TGA_NODATA)
  210. memset(p->data[0], 0, p->linesize[0] * s->height);
  211. else{
  212. if(compr & TGA_RLE){
  213. int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
  214. if (res < 0)
  215. return -1;
  216. buf += res;
  217. }else{
  218. size_t img_size = s->width * ((s->bpp + 1) >> 3);
  219. CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
  220. for(y = 0; y < s->height; y++){
  221. memcpy(dst, buf, img_size);
  222. dst += stride;
  223. buf += img_size;
  224. }
  225. }
  226. }
  227. if(flags & 0x10){ // right-to-left, needs horizontal flip
  228. int x;
  229. for(y = 0; y < s->height; y++){
  230. void *line = &p->data[0][y * p->linesize[0]];
  231. for(x = 0; x < s->width >> 1; x++){
  232. switch(s->bpp){
  233. case 32:
  234. FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]);
  235. break;
  236. case 24:
  237. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]);
  238. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]);
  239. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]);
  240. break;
  241. case 16:
  242. FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]);
  243. break;
  244. case 8:
  245. FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]);
  246. }
  247. }
  248. }
  249. }
  250. *picture= *(AVFrame*)&s->picture;
  251. *data_size = sizeof(AVPicture);
  252. return avpkt->size;
  253. }
  254. static av_cold int targa_init(AVCodecContext *avctx){
  255. TargaContext *s = avctx->priv_data;
  256. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  257. avctx->coded_frame= (AVFrame*)&s->picture;
  258. return 0;
  259. }
  260. static av_cold int targa_end(AVCodecContext *avctx){
  261. TargaContext *s = avctx->priv_data;
  262. if(s->picture.data[0])
  263. avctx->release_buffer(avctx, &s->picture);
  264. return 0;
  265. }
  266. AVCodec ff_targa_decoder = {
  267. .name = "targa",
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .id = CODEC_ID_TARGA,
  270. .priv_data_size = sizeof(TargaContext),
  271. .init = targa_init,
  272. .close = targa_end,
  273. .decode = decode_frame,
  274. .capabilities = CODEC_CAP_DR1,
  275. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  276. };