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.

338 lines
11KB

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