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.

286 lines
8.5KB

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