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.

270 lines
8.3KB

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