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.

258 lines
7.3KB

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