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 "avcodec.h"
  23. enum TargaCompr{
  24. TGA_NODATA = 0, // no image data
  25. TGA_PAL = 1, // palettized
  26. TGA_RGB = 2, // true-color
  27. TGA_BW = 3, // black & white or grayscale
  28. TGA_RLE = 8, // flag pointing that data is RLE-coded
  29. };
  30. typedef struct TargaContext {
  31. AVFrame picture;
  32. int width, height;
  33. int bpp;
  34. int color_type;
  35. int compression_type;
  36. } TargaContext;
  37. static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
  38. {
  39. int i, x, y;
  40. int depth = (bpp + 1) >> 3;
  41. int type, count;
  42. int diff;
  43. diff = stride - w * depth;
  44. x = y = 0;
  45. while(y < h){
  46. type = *src++;
  47. count = (type & 0x7F) + 1;
  48. type &= 0x80;
  49. if((x + count > w) && (x + count + 1 > (h - y) * w)){
  50. av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
  51. return;
  52. }
  53. for(i = 0; i < count; i++){
  54. switch(depth){
  55. case 1:
  56. *dst = *src;
  57. break;
  58. case 2:
  59. *((uint16_t*)dst) = AV_RL16(src);
  60. break;
  61. case 3:
  62. dst[0] = src[0];
  63. dst[1] = src[1];
  64. dst[2] = src[2];
  65. break;
  66. case 4:
  67. *((uint32_t*)dst) = AV_RL32(src);
  68. break;
  69. }
  70. dst += depth;
  71. if(!type)
  72. src += depth;
  73. x++;
  74. if(x == w){
  75. x = 0;
  76. y++;
  77. dst += diff;
  78. }
  79. }
  80. if(type)
  81. src += depth;
  82. }
  83. }
  84. static int decode_frame(AVCodecContext *avctx,
  85. void *data, int *data_size,
  86. AVPacket *avpkt)
  87. {
  88. const uint8_t *buf = avpkt->data;
  89. int buf_size = avpkt->size;
  90. TargaContext * const s = avctx->priv_data;
  91. AVFrame *picture = data;
  92. AVFrame * const p= (AVFrame*)&s->picture;
  93. uint8_t *dst;
  94. int stride;
  95. int idlen, pal, compr, x, y, w, h, bpp, flags;
  96. int first_clr, colors, csize;
  97. /* parse image header */
  98. idlen = *buf++;
  99. pal = *buf++;
  100. compr = *buf++;
  101. first_clr = AV_RL16(buf); buf += 2;
  102. colors = AV_RL16(buf); buf += 2;
  103. csize = *buf++;
  104. x = AV_RL16(buf); buf += 2;
  105. y = AV_RL16(buf); buf += 2;
  106. w = AV_RL16(buf); buf += 2;
  107. h = AV_RL16(buf); buf += 2;
  108. bpp = *buf++;
  109. flags = *buf++;
  110. //skip identifier if any
  111. buf += idlen;
  112. s->bpp = bpp;
  113. s->width = w;
  114. s->height = h;
  115. switch(s->bpp){
  116. case 8:
  117. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  118. break;
  119. case 15:
  120. avctx->pix_fmt = PIX_FMT_RGB555;
  121. break;
  122. case 16:
  123. avctx->pix_fmt = PIX_FMT_RGB555;
  124. break;
  125. case 24:
  126. avctx->pix_fmt = PIX_FMT_BGR24;
  127. break;
  128. case 32:
  129. avctx->pix_fmt = PIX_FMT_RGB32;
  130. break;
  131. default:
  132. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  133. return -1;
  134. }
  135. if(s->picture.data[0])
  136. avctx->release_buffer(avctx, &s->picture);
  137. if(avcodec_check_dimensions(avctx, w, h))
  138. return -1;
  139. if(w != avctx->width || h != avctx->height)
  140. avcodec_set_dimensions(avctx, w, h);
  141. if(avctx->get_buffer(avctx, p) < 0){
  142. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  143. return -1;
  144. }
  145. if(flags & 0x20){
  146. dst = p->data[0];
  147. stride = p->linesize[0];
  148. }else{ //image is upside-down
  149. dst = p->data[0] + p->linesize[0] * (h - 1);
  150. stride = -p->linesize[0];
  151. }
  152. if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
  153. memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  154. if(avctx->palctrl->palette_changed){
  155. p->palette_has_changed = 1;
  156. avctx->palctrl->palette_changed = 0;
  157. }
  158. }
  159. if(colors){
  160. if((colors + first_clr) > 256){
  161. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  162. return -1;
  163. }
  164. if(csize != 24){
  165. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  166. return -1;
  167. }
  168. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  169. buf += colors * ((csize + 1) >> 3);
  170. else{
  171. int r, g, b, t;
  172. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  173. for(t = 0; t < colors; t++){
  174. r = *buf++;
  175. g = *buf++;
  176. b = *buf++;
  177. *pal++ = (b << 16) | (g << 8) | r;
  178. }
  179. p->palette_has_changed = 1;
  180. avctx->palctrl->palette_changed = 0;
  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. #ifdef WORDS_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. CODEC_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. };