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.

308 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 "internal.h"
  26. #include "targa.h"
  27. typedef struct TargaContext {
  28. GetByteContext gb;
  29. } TargaContext;
  30. static uint8_t *advance_line(uint8_t *start, uint8_t *line,
  31. int stride, int *y, int h, int interleave)
  32. {
  33. *y += interleave;
  34. if (*y < h) {
  35. return line + interleave * stride;
  36. } else {
  37. *y = (*y + 1) & (interleave - 1);
  38. if (*y && *y < h) {
  39. return start + *y * stride;
  40. } else {
  41. return NULL;
  42. }
  43. }
  44. }
  45. static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
  46. uint8_t *start, int w, int h, int stride,
  47. int bpp, int interleave)
  48. {
  49. int x, y;
  50. int depth = (bpp + 1) >> 3;
  51. int type, count;
  52. uint8_t *line = start;
  53. uint8_t *dst = line;
  54. x = y = count = 0;
  55. while (dst) {
  56. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  57. av_log(avctx, AV_LOG_ERROR,
  58. "Ran ouf of data before end-of-image\n");
  59. return AVERROR_INVALIDDATA;
  60. }
  61. type = bytestream2_get_byteu(&s->gb);
  62. count = (type & 0x7F) + 1;
  63. type &= 0x80;
  64. if (!type) {
  65. do {
  66. int n = FFMIN(count, w - x);
  67. bytestream2_get_buffer(&s->gb, dst, n * depth);
  68. count -= n;
  69. dst += n * depth;
  70. x += n;
  71. if (x == w) {
  72. x = 0;
  73. dst = line = advance_line(start, line, stride, &y, h, interleave);
  74. }
  75. } while (dst && count > 0);
  76. } else {
  77. uint8_t tmp[4];
  78. bytestream2_get_buffer(&s->gb, tmp, depth);
  79. do {
  80. int n = FFMIN(count, w - x);
  81. count -= n;
  82. x += n;
  83. do {
  84. memcpy(dst, tmp, depth);
  85. dst += depth;
  86. } while (--n);
  87. if (x == w) {
  88. x = 0;
  89. dst = line = advance_line(start, line, stride, &y, h, interleave);
  90. }
  91. } while (dst && count > 0);
  92. }
  93. }
  94. if (count) {
  95. av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
  96. return AVERROR_INVALIDDATA;
  97. }
  98. return 0;
  99. }
  100. static int decode_frame(AVCodecContext *avctx,
  101. void *data, int *got_frame,
  102. AVPacket *avpkt)
  103. {
  104. TargaContext * const s = avctx->priv_data;
  105. AVFrame * const p = data;
  106. uint8_t *dst;
  107. int stride;
  108. int idlen, pal, compr, y, w, h, bpp, flags, ret;
  109. int first_clr, colors, csize;
  110. int interleave;
  111. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  112. /* parse image header */
  113. idlen = bytestream2_get_byte(&s->gb);
  114. pal = bytestream2_get_byte(&s->gb);
  115. compr = bytestream2_get_byte(&s->gb);
  116. first_clr = bytestream2_get_le16(&s->gb);
  117. colors = bytestream2_get_le16(&s->gb);
  118. csize = bytestream2_get_byte(&s->gb);
  119. bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
  120. w = bytestream2_get_le16(&s->gb);
  121. h = bytestream2_get_le16(&s->gb);
  122. bpp = bytestream2_get_byte(&s->gb);
  123. if (bytestream2_get_bytes_left(&s->gb) <= idlen) {
  124. av_log(avctx, AV_LOG_ERROR,
  125. "Not enough data to read header\n");
  126. return AVERROR_INVALIDDATA;
  127. }
  128. flags = bytestream2_get_byte(&s->gb);
  129. if (!pal && (first_clr || colors || csize)) {
  130. av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
  131. // specification says we should ignore those value in this case
  132. first_clr = colors = csize = 0;
  133. }
  134. // skip identifier if any
  135. bytestream2_skip(&s->gb, idlen);
  136. switch (bpp) {
  137. case 8:
  138. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
  139. break;
  140. case 15:
  141. case 16:
  142. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  143. break;
  144. case 24:
  145. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  146. break;
  147. case 32:
  148. avctx->pix_fmt = AV_PIX_FMT_BGRA;
  149. break;
  150. default:
  151. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
  152. return AVERROR_INVALIDDATA;
  153. }
  154. if (colors && (colors + first_clr) > 256) {
  155. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  156. return AVERROR_INVALIDDATA;
  157. }
  158. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  159. return ret;
  160. if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
  161. return ret;
  162. p->pict_type = AV_PICTURE_TYPE_I;
  163. if (flags & TGA_TOPTOBOTTOM) {
  164. dst = p->data[0];
  165. stride = p->linesize[0];
  166. } else { //image is upside-down
  167. dst = p->data[0] + p->linesize[0] * (h - 1);
  168. stride = -p->linesize[0];
  169. }
  170. interleave = flags & TGA_INTERLEAVE2 ? 2 :
  171. flags & TGA_INTERLEAVE4 ? 4 : 1;
  172. if (colors) {
  173. int pal_size, pal_sample_size;
  174. switch (csize) {
  175. case 32: pal_sample_size = 4; break;
  176. case 24: pal_sample_size = 3; break;
  177. case 16:
  178. case 15: pal_sample_size = 2; break;
  179. default:
  180. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  181. return AVERROR_INVALIDDATA;
  182. }
  183. pal_size = colors * pal_sample_size;
  184. if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
  185. bytestream2_skip(&s->gb, pal_size);
  186. else {
  187. int t;
  188. uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
  189. if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
  190. av_log(avctx, AV_LOG_ERROR,
  191. "Not enough data to read palette\n");
  192. return AVERROR_INVALIDDATA;
  193. }
  194. switch (pal_sample_size) {
  195. case 4:
  196. for (t = 0; t < colors; t++)
  197. *pal++ = bytestream2_get_le32u(&s->gb);
  198. break;
  199. case 3:
  200. /* RGB24 */
  201. for (t = 0; t < colors; t++)
  202. *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
  203. break;
  204. case 2:
  205. /* RGB555 */
  206. for (t = 0; t < colors; t++) {
  207. uint32_t v = bytestream2_get_le16u(&s->gb);
  208. v = ((v & 0x7C00) << 9) |
  209. ((v & 0x03E0) << 6) |
  210. ((v & 0x001F) << 3);
  211. /* left bit replication */
  212. v |= (v & 0xE0E0E0U) >> 5;
  213. *pal++ = (0xffU<<24) | v;
  214. }
  215. break;
  216. }
  217. p->palette_has_changed = 1;
  218. }
  219. }
  220. if ((compr & (~TGA_RLE)) == TGA_NODATA) {
  221. memset(p->data[0], 0, p->linesize[0] * h);
  222. } else {
  223. if (compr & TGA_RLE) {
  224. int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
  225. if (res < 0)
  226. return res;
  227. } else {
  228. size_t img_size = w * ((bpp + 1) >> 3);
  229. uint8_t *line;
  230. if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
  231. av_log(avctx, AV_LOG_ERROR,
  232. "Not enough data available for image\n");
  233. return AVERROR_INVALIDDATA;
  234. }
  235. line = dst;
  236. y = 0;
  237. do {
  238. bytestream2_get_buffer(&s->gb, line, img_size);
  239. line = advance_line(dst, line, stride, &y, h, interleave);
  240. } while (line);
  241. }
  242. }
  243. if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
  244. int x;
  245. for (y = 0; y < h; y++) {
  246. void *line = &p->data[0][y * p->linesize[0]];
  247. for (x = 0; x < w >> 1; x++) {
  248. switch (bpp) {
  249. case 32:
  250. FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
  251. break;
  252. case 24:
  253. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
  254. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
  255. FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
  256. break;
  257. case 16:
  258. FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
  259. break;
  260. case 8:
  261. FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
  262. }
  263. }
  264. }
  265. }
  266. *got_frame = 1;
  267. return avpkt->size;
  268. }
  269. AVCodec ff_targa_decoder = {
  270. .name = "targa",
  271. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  272. .type = AVMEDIA_TYPE_VIDEO,
  273. .id = AV_CODEC_ID_TARGA,
  274. .priv_data_size = sizeof(TargaContext),
  275. .decode = decode_frame,
  276. .capabilities = AV_CODEC_CAP_DR1,
  277. };