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.

243 lines
7.7KB

  1. /*
  2. * Targa (.tga) image decoder
  3. * Copyright (c) 2006 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. int color_type;
  30. int compression_type;
  31. } TargaContext;
  32. static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s,
  33. uint8_t *dst, int w, int h, int stride, int bpp)
  34. {
  35. int x, y;
  36. int depth = (bpp + 1) >> 3;
  37. int type, count;
  38. int diff;
  39. diff = stride - w * depth;
  40. x = y = 0;
  41. while (y < h) {
  42. if (bytestream2_get_bytes_left(&s->gb) <= 0) {
  43. av_log(avctx, AV_LOG_ERROR,
  44. "Ran ouf of data before end-of-image\n");
  45. return AVERROR_INVALIDDATA;
  46. }
  47. type = bytestream2_get_byteu(&s->gb);
  48. count = (type & 0x7F) + 1;
  49. type &= 0x80;
  50. if (x + count > w && x + count + 1 > (h - y) * w) {
  51. av_log(avctx, AV_LOG_ERROR,
  52. "Packet went out of bounds: position (%i,%i) size %i\n",
  53. x, y, count);
  54. return AVERROR_INVALIDDATA;
  55. }
  56. if (!type) {
  57. do {
  58. int n = FFMIN(count, w - x);
  59. bytestream2_get_buffer(&s->gb, dst, n * depth);
  60. count -= n;
  61. dst += n * depth;
  62. x += n;
  63. if (x == w) {
  64. x = 0;
  65. y++;
  66. dst += diff;
  67. }
  68. } while (count > 0);
  69. } else {
  70. uint8_t tmp[4];
  71. bytestream2_get_buffer(&s->gb, tmp, depth);
  72. do {
  73. int n = FFMIN(count, w - x);
  74. count -= n;
  75. x += n;
  76. do {
  77. memcpy(dst, tmp, depth);
  78. dst += depth;
  79. } while (--n);
  80. if (x == w) {
  81. x = 0;
  82. y++;
  83. dst += diff;
  84. }
  85. } while (count > 0);
  86. }
  87. }
  88. return 0;
  89. }
  90. static int decode_frame(AVCodecContext *avctx,
  91. void *data, int *got_frame,
  92. AVPacket *avpkt)
  93. {
  94. TargaContext * const s = avctx->priv_data;
  95. AVFrame * const p = data;
  96. uint8_t *dst;
  97. int stride;
  98. int idlen, compr, y, w, h, bpp, flags, ret;
  99. int first_clr, colors, csize;
  100. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  101. /* parse image header */
  102. idlen = bytestream2_get_byte(&s->gb);
  103. bytestream2_skip(&s->gb, 1); /* pal */
  104. compr = bytestream2_get_byte(&s->gb);
  105. first_clr = bytestream2_get_le16(&s->gb);
  106. colors = bytestream2_get_le16(&s->gb);
  107. csize = bytestream2_get_byte(&s->gb);
  108. bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
  109. w = bytestream2_get_le16(&s->gb);
  110. h = bytestream2_get_le16(&s->gb);
  111. bpp = bytestream2_get_byte(&s->gb);
  112. flags = bytestream2_get_byte(&s->gb);
  113. // skip identifier if any
  114. bytestream2_skip(&s->gb, idlen);
  115. switch(bpp){
  116. case 8:
  117. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
  118. break;
  119. case 15:
  120. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  121. break;
  122. case 16:
  123. avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
  124. break;
  125. case 24:
  126. avctx->pix_fmt = AV_PIX_FMT_BGR24;
  127. break;
  128. case 32:
  129. avctx->pix_fmt = AV_PIX_FMT_BGRA;
  130. break;
  131. default:
  132. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
  133. return AVERROR_INVALIDDATA;
  134. }
  135. if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
  136. return ret;
  137. if ((ret = ff_get_buffer(avctx, p, 0)) < 0){
  138. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  139. return ret;
  140. }
  141. if(flags & 0x20){
  142. dst = p->data[0];
  143. stride = p->linesize[0];
  144. }else{ //image is upside-down
  145. dst = p->data[0] + p->linesize[0] * (h - 1);
  146. stride = -p->linesize[0];
  147. }
  148. if(colors){
  149. int pal_size, pal_sample_size;
  150. if((colors + first_clr) > 256){
  151. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  152. return AVERROR_INVALIDDATA;
  153. }
  154. switch (csize) {
  155. case 24: pal_sample_size = 3; break;
  156. case 16:
  157. case 15: pal_sample_size = 2; break;
  158. default:
  159. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  160. return AVERROR_INVALIDDATA;
  161. }
  162. pal_size = colors * pal_sample_size;
  163. if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
  164. bytestream2_skip(&s->gb, pal_size);
  165. else{
  166. int t;
  167. uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
  168. if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
  169. av_log(avctx, AV_LOG_ERROR,
  170. "Not enough data to read palette\n");
  171. return AVERROR_INVALIDDATA;
  172. }
  173. switch (pal_sample_size) {
  174. case 3:
  175. /* RGB24 */
  176. for (t = 0; t < colors; t++)
  177. *pal++ = bytestream2_get_le24u(&s->gb);
  178. break;
  179. case 2:
  180. /* RGB555 */
  181. for (t = 0; t < colors; t++) {
  182. uint32_t v = bytestream2_get_le16u(&s->gb);
  183. v = ((v & 0x7C00) << 9) |
  184. ((v & 0x03E0) << 6) |
  185. ((v & 0x001F) << 3);
  186. /* left bit replication */
  187. v |= (v & 0xE0E0E0U) >> 5;
  188. *pal++ = v;
  189. }
  190. break;
  191. }
  192. p->palette_has_changed = 1;
  193. }
  194. }
  195. if ((compr & (~TGA_RLE)) == TGA_NODATA) {
  196. memset(p->data[0], 0, p->linesize[0] * h);
  197. } else {
  198. if(compr & TGA_RLE){
  199. int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
  200. if (res < 0)
  201. return res;
  202. } else {
  203. size_t img_size = w * ((bpp + 1) >> 3);
  204. if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
  205. av_log(avctx, AV_LOG_ERROR,
  206. "Not enough data available for image\n");
  207. return AVERROR_INVALIDDATA;
  208. }
  209. for (y = 0; y < h; y++) {
  210. bytestream2_get_bufferu(&s->gb, dst, img_size);
  211. dst += stride;
  212. }
  213. }
  214. }
  215. *got_frame = 1;
  216. return avpkt->size;
  217. }
  218. AVCodec ff_targa_decoder = {
  219. .name = "targa",
  220. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  221. .type = AVMEDIA_TYPE_VIDEO,
  222. .id = AV_CODEC_ID_TARGA,
  223. .priv_data_size = sizeof(TargaContext),
  224. .decode = decode_frame,
  225. .capabilities = AV_CODEC_CAP_DR1,
  226. };