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.

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