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.

251 lines
7.1KB

  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 "libavcore/imgutils.h"
  23. #include "avcodec.h"
  24. #include "targa.h"
  25. typedef struct TargaContext {
  26. AVFrame picture;
  27. int width, height;
  28. int bpp;
  29. int color_type;
  30. int compression_type;
  31. } TargaContext;
  32. static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
  33. {
  34. int i, x, y;
  35. int depth = (bpp + 1) >> 3;
  36. int type, count;
  37. int diff;
  38. diff = stride - w * depth;
  39. x = y = 0;
  40. while(y < h){
  41. type = *src++;
  42. count = (type & 0x7F) + 1;
  43. type &= 0x80;
  44. if((x + count > w) && (x + count + 1 > (h - y) * w)){
  45. av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
  46. return;
  47. }
  48. for(i = 0; i < count; i++){
  49. switch(depth){
  50. case 1:
  51. *dst = *src;
  52. break;
  53. case 2:
  54. *((uint16_t*)dst) = AV_RL16(src);
  55. break;
  56. case 3:
  57. dst[0] = src[0];
  58. dst[1] = src[1];
  59. dst[2] = src[2];
  60. break;
  61. case 4:
  62. *((uint32_t*)dst) = AV_RL32(src);
  63. break;
  64. }
  65. dst += depth;
  66. if(!type)
  67. src += depth;
  68. x++;
  69. if(x == w){
  70. x = 0;
  71. y++;
  72. dst += diff;
  73. }
  74. }
  75. if(type)
  76. src += depth;
  77. }
  78. }
  79. static int decode_frame(AVCodecContext *avctx,
  80. void *data, int *data_size,
  81. AVPacket *avpkt)
  82. {
  83. const uint8_t *buf = avpkt->data;
  84. int buf_size = avpkt->size;
  85. TargaContext * const s = avctx->priv_data;
  86. AVFrame *picture = data;
  87. AVFrame * const p= (AVFrame*)&s->picture;
  88. uint8_t *dst;
  89. int stride;
  90. int idlen, pal, compr, x, y, w, h, bpp, flags;
  91. int first_clr, colors, csize;
  92. /* parse image header */
  93. idlen = *buf++;
  94. pal = *buf++;
  95. compr = *buf++;
  96. first_clr = AV_RL16(buf); buf += 2;
  97. colors = AV_RL16(buf); buf += 2;
  98. csize = *buf++;
  99. x = AV_RL16(buf); buf += 2;
  100. y = AV_RL16(buf); buf += 2;
  101. w = AV_RL16(buf); buf += 2;
  102. h = AV_RL16(buf); buf += 2;
  103. bpp = *buf++;
  104. flags = *buf++;
  105. //skip identifier if any
  106. buf += idlen;
  107. s->bpp = bpp;
  108. s->width = w;
  109. s->height = h;
  110. switch(s->bpp){
  111. case 8:
  112. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  113. break;
  114. case 15:
  115. avctx->pix_fmt = PIX_FMT_RGB555;
  116. break;
  117. case 16:
  118. avctx->pix_fmt = PIX_FMT_RGB555;
  119. break;
  120. case 24:
  121. avctx->pix_fmt = PIX_FMT_BGR24;
  122. break;
  123. case 32:
  124. avctx->pix_fmt = PIX_FMT_RGB32;
  125. break;
  126. default:
  127. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  128. return -1;
  129. }
  130. if(s->picture.data[0])
  131. avctx->release_buffer(avctx, &s->picture);
  132. if(av_image_check_size(w, h, 0, avctx))
  133. return -1;
  134. if(w != avctx->width || h != avctx->height)
  135. avcodec_set_dimensions(avctx, w, h);
  136. if(avctx->get_buffer(avctx, p) < 0){
  137. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  138. return -1;
  139. }
  140. if(flags & 0x20){
  141. dst = p->data[0];
  142. stride = p->linesize[0];
  143. }else{ //image is upside-down
  144. dst = p->data[0] + p->linesize[0] * (h - 1);
  145. stride = -p->linesize[0];
  146. }
  147. if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
  148. memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  149. if(avctx->palctrl->palette_changed){
  150. p->palette_has_changed = 1;
  151. avctx->palctrl->palette_changed = 0;
  152. }
  153. }
  154. if(colors){
  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. if(csize != 24){
  160. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  161. return -1;
  162. }
  163. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  164. buf += colors * ((csize + 1) >> 3);
  165. else{
  166. int r, g, b, t;
  167. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  168. for(t = 0; t < colors; t++){
  169. r = *buf++;
  170. g = *buf++;
  171. b = *buf++;
  172. *pal++ = (b << 16) | (g << 8) | r;
  173. }
  174. p->palette_has_changed = 1;
  175. }
  176. }
  177. if((compr & (~TGA_RLE)) == TGA_NODATA)
  178. memset(p->data[0], 0, p->linesize[0] * s->height);
  179. else{
  180. if(compr & TGA_RLE)
  181. targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
  182. else{
  183. for(y = 0; y < s->height; y++){
  184. #if HAVE_BIGENDIAN
  185. if((s->bpp + 1) >> 3 == 2){
  186. uint16_t *dst16 = (uint16_t*)dst;
  187. for(x = 0; x < s->width; x++)
  188. dst16[x] = AV_RL16(buf + x * 2);
  189. }else if((s->bpp + 1) >> 3 == 4){
  190. uint32_t *dst32 = (uint32_t*)dst;
  191. for(x = 0; x < s->width; x++)
  192. dst32[x] = AV_RL32(buf + x * 4);
  193. }else
  194. #endif
  195. memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
  196. dst += stride;
  197. buf += s->width * ((s->bpp + 1) >> 3);
  198. }
  199. }
  200. }
  201. *picture= *(AVFrame*)&s->picture;
  202. *data_size = sizeof(AVPicture);
  203. return buf_size;
  204. }
  205. static av_cold int targa_init(AVCodecContext *avctx){
  206. TargaContext *s = avctx->priv_data;
  207. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  208. avctx->coded_frame= (AVFrame*)&s->picture;
  209. return 0;
  210. }
  211. static av_cold int targa_end(AVCodecContext *avctx){
  212. TargaContext *s = avctx->priv_data;
  213. if(s->picture.data[0])
  214. avctx->release_buffer(avctx, &s->picture);
  215. return 0;
  216. }
  217. AVCodec targa_decoder = {
  218. "targa",
  219. AVMEDIA_TYPE_VIDEO,
  220. CODEC_ID_TARGA,
  221. sizeof(TargaContext),
  222. targa_init,
  223. NULL,
  224. targa_end,
  225. decode_frame,
  226. CODEC_CAP_DR1,
  227. NULL,
  228. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  229. };