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.

254 lines
7.2KB

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