targa.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. const uint8_t *buf, int buf_size)
  87. {
  88. TargaContext * const s = avctx->priv_data;
  89. AVFrame *picture = data;
  90. AVFrame * const p= (AVFrame*)&s->picture;
  91. uint8_t *dst;
  92. int stride;
  93. int idlen, pal, compr, x, y, w, h, bpp, flags;
  94. int first_clr, colors, csize;
  95. /* parse image header */
  96. idlen = *buf++;
  97. pal = *buf++;
  98. compr = *buf++;
  99. first_clr = AV_RL16(buf); buf += 2;
  100. colors = AV_RL16(buf); buf += 2;
  101. csize = *buf++;
  102. x = AV_RL16(buf); buf += 2;
  103. y = AV_RL16(buf); buf += 2;
  104. w = AV_RL16(buf); buf += 2;
  105. h = AV_RL16(buf); buf += 2;
  106. bpp = *buf++;
  107. flags = *buf++;
  108. //skip identifier if any
  109. buf += idlen;
  110. s->bpp = bpp;
  111. s->width = w;
  112. s->height = h;
  113. switch(s->bpp){
  114. case 8:
  115. avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
  116. break;
  117. case 15:
  118. avctx->pix_fmt = PIX_FMT_RGB555;
  119. break;
  120. case 16:
  121. avctx->pix_fmt = PIX_FMT_RGB555;
  122. break;
  123. case 24:
  124. avctx->pix_fmt = PIX_FMT_BGR24;
  125. break;
  126. case 32:
  127. avctx->pix_fmt = PIX_FMT_RGB32;
  128. break;
  129. default:
  130. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
  131. return -1;
  132. }
  133. if(s->picture.data[0])
  134. avctx->release_buffer(avctx, &s->picture);
  135. if(avcodec_check_dimensions(avctx, w, h))
  136. return -1;
  137. if(w != avctx->width || h != avctx->height)
  138. avcodec_set_dimensions(avctx, w, h);
  139. if(avctx->get_buffer(avctx, p) < 0){
  140. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  141. return -1;
  142. }
  143. if(flags & 0x20){
  144. dst = p->data[0];
  145. stride = p->linesize[0];
  146. }else{ //image is upside-down
  147. dst = p->data[0] + p->linesize[0] * (h - 1);
  148. stride = -p->linesize[0];
  149. }
  150. if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
  151. memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  152. if(avctx->palctrl->palette_changed){
  153. p->palette_has_changed = 1;
  154. avctx->palctrl->palette_changed = 0;
  155. }
  156. }
  157. if(colors){
  158. if((colors + first_clr) > 256){
  159. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  160. return -1;
  161. }
  162. if(csize != 24){
  163. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  164. return -1;
  165. }
  166. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  167. buf += colors * ((csize + 1) >> 3);
  168. else{
  169. int r, g, b, t;
  170. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  171. for(t = 0; t < colors; t++){
  172. r = *buf++;
  173. g = *buf++;
  174. b = *buf++;
  175. *pal++ = (b << 16) | (g << 8) | r;
  176. }
  177. p->palette_has_changed = 1;
  178. avctx->palctrl->palette_changed = 0;
  179. }
  180. }
  181. if((compr & (~TGA_RLE)) == TGA_NODATA)
  182. memset(p->data[0], 0, p->linesize[0] * s->height);
  183. else{
  184. if(compr & TGA_RLE)
  185. targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
  186. else{
  187. for(y = 0; y < s->height; y++){
  188. #ifdef WORDS_BIGENDIAN
  189. if((s->bpp + 1) >> 3 == 2){
  190. uint16_t *dst16 = (uint16_t*)dst;
  191. for(x = 0; x < s->width; x++)
  192. dst16[x] = AV_RL16(buf + x * 2);
  193. }else if((s->bpp + 1) >> 3 == 4){
  194. uint32_t *dst32 = (uint32_t*)dst;
  195. for(x = 0; x < s->width; x++)
  196. dst32[x] = AV_RL32(buf + x * 4);
  197. }else
  198. #endif
  199. memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
  200. dst += stride;
  201. buf += s->width * ((s->bpp + 1) >> 3);
  202. }
  203. }
  204. }
  205. *picture= *(AVFrame*)&s->picture;
  206. *data_size = sizeof(AVPicture);
  207. return buf_size;
  208. }
  209. static av_cold int targa_init(AVCodecContext *avctx){
  210. TargaContext *s = avctx->priv_data;
  211. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  212. avctx->coded_frame= (AVFrame*)&s->picture;
  213. s->picture.data[0] = NULL;
  214. return 0;
  215. }
  216. static av_cold int targa_end(AVCodecContext *avctx){
  217. TargaContext *s = avctx->priv_data;
  218. if(s->picture.data[0])
  219. avctx->release_buffer(avctx, &s->picture);
  220. return 0;
  221. }
  222. AVCodec targa_decoder = {
  223. "targa",
  224. CODEC_TYPE_VIDEO,
  225. CODEC_ID_TARGA,
  226. sizeof(TargaContext),
  227. targa_init,
  228. NULL,
  229. targa_end,
  230. decode_frame,
  231. 0,
  232. NULL,
  233. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  234. };