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.

304 lines
9.7KB

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