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.

269 lines
8.2KB

  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 "targa.h"
  26. typedef struct TargaContext {
  27. AVFrame picture;
  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 *data_size,
  92. AVPacket *avpkt)
  93. {
  94. TargaContext * const s = avctx->priv_data;
  95. AVFrame *picture = data;
  96. AVFrame * const p = &s->picture;
  97. uint8_t *dst;
  98. int stride;
  99. int idlen, compr, y, w, h, bpp, flags;
  100. int first_clr, colors, csize;
  101. bytestream2_init(&s->gb, avpkt->data, avpkt->size);
  102. /* parse image header */
  103. idlen = bytestream2_get_byte(&s->gb);
  104. bytestream2_skip(&s->gb, 1); /* pal */
  105. compr = bytestream2_get_byte(&s->gb);
  106. first_clr = bytestream2_get_le16(&s->gb);
  107. colors = bytestream2_get_le16(&s->gb);
  108. csize = bytestream2_get_byte(&s->gb);
  109. bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
  110. w = bytestream2_get_le16(&s->gb);
  111. h = bytestream2_get_le16(&s->gb);
  112. bpp = bytestream2_get_byte(&s->gb);
  113. flags = bytestream2_get_byte(&s->gb);
  114. // skip identifier if any
  115. bytestream2_skip(&s->gb, idlen);
  116. switch(bpp){
  117. case 8:
  118. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  119. break;
  120. case 15:
  121. avctx->pix_fmt = PIX_FMT_RGB555LE;
  122. break;
  123. case 16:
  124. avctx->pix_fmt = PIX_FMT_RGB555LE;
  125. break;
  126. case 24:
  127. avctx->pix_fmt = PIX_FMT_BGR24;
  128. break;
  129. case 32:
  130. avctx->pix_fmt = PIX_FMT_BGRA;
  131. break;
  132. default:
  133. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
  134. return -1;
  135. }
  136. if(s->picture.data[0])
  137. avctx->release_buffer(avctx, &s->picture);
  138. if(av_image_check_size(w, h, 0, avctx))
  139. return -1;
  140. if(w != avctx->width || h != avctx->height)
  141. avcodec_set_dimensions(avctx, w, h);
  142. if(avctx->get_buffer(avctx, p) < 0){
  143. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  144. return -1;
  145. }
  146. if(flags & 0x20){
  147. dst = p->data[0];
  148. stride = p->linesize[0];
  149. }else{ //image is upside-down
  150. dst = p->data[0] + p->linesize[0] * (h - 1);
  151. stride = -p->linesize[0];
  152. }
  153. if(colors){
  154. int pal_size, pal_sample_size;
  155. if((colors + first_clr) > 256){
  156. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  157. return -1;
  158. }
  159. switch (csize) {
  160. case 24: pal_sample_size = 3; break;
  161. case 16:
  162. case 15: pal_sample_size = 2; break;
  163. default:
  164. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  165. return -1;
  166. }
  167. pal_size = colors * pal_sample_size;
  168. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  169. bytestream2_skip(&s->gb, pal_size);
  170. else{
  171. int t;
  172. uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
  173. if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
  174. av_log(avctx, AV_LOG_ERROR,
  175. "Not enough data to read palette\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. switch (pal_sample_size) {
  179. case 3:
  180. /* RGB24 */
  181. for (t = 0; t < colors; t++)
  182. *pal++ = bytestream2_get_le24u(&s->gb);
  183. break;
  184. case 2:
  185. /* RGB555 */
  186. for (t = 0; t < colors; t++) {
  187. uint32_t v = bytestream2_get_le16u(&s->gb);
  188. v = ((v & 0x7C00) << 9) |
  189. ((v & 0x03E0) << 6) |
  190. ((v & 0x001F) << 3);
  191. /* left bit replication */
  192. v |= (v & 0xE0E0E0U) >> 5;
  193. *pal++ = v;
  194. }
  195. break;
  196. }
  197. p->palette_has_changed = 1;
  198. }
  199. }
  200. if ((compr & (~TGA_RLE)) == TGA_NODATA) {
  201. memset(p->data[0], 0, p->linesize[0] * h);
  202. } else {
  203. if(compr & TGA_RLE){
  204. int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
  205. if (res < 0)
  206. return res;
  207. } else {
  208. size_t img_size = w * ((bpp + 1) >> 3);
  209. if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
  210. av_log(avctx, AV_LOG_ERROR,
  211. "Not enough data available for image\n");
  212. return AVERROR_INVALIDDATA;
  213. }
  214. for (y = 0; y < h; y++) {
  215. bytestream2_get_bufferu(&s->gb, dst, img_size);
  216. dst += stride;
  217. }
  218. }
  219. }
  220. *picture = s->picture;
  221. *data_size = sizeof(AVPicture);
  222. return avpkt->size;
  223. }
  224. static av_cold int targa_init(AVCodecContext *avctx){
  225. TargaContext *s = avctx->priv_data;
  226. avcodec_get_frame_defaults(&s->picture);
  227. avctx->coded_frame = &s->picture;
  228. return 0;
  229. }
  230. static av_cold int targa_end(AVCodecContext *avctx){
  231. TargaContext *s = avctx->priv_data;
  232. if(s->picture.data[0])
  233. avctx->release_buffer(avctx, &s->picture);
  234. return 0;
  235. }
  236. AVCodec ff_targa_decoder = {
  237. .name = "targa",
  238. .type = AVMEDIA_TYPE_VIDEO,
  239. .id = CODEC_ID_TARGA,
  240. .priv_data_size = sizeof(TargaContext),
  241. .init = targa_init,
  242. .close = targa_end,
  243. .decode = decode_frame,
  244. .capabilities = CODEC_CAP_DR1,
  245. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  246. };