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.

245 lines
6.8KB

  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. */
  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, 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) = LE_16(src);
  60. break;
  61. case 3:
  62. dst[0] = src[0];
  63. dst[1] = src[1];
  64. dst[2] = src[2];
  65. break;
  66. }
  67. dst += depth;
  68. if(!type)
  69. src += depth;
  70. x++;
  71. if(x == w){
  72. x = 0;
  73. y++;
  74. dst += diff;
  75. }
  76. }
  77. if(type)
  78. src += depth;
  79. }
  80. }
  81. static int decode_frame(AVCodecContext *avctx,
  82. void *data, int *data_size,
  83. uint8_t *buf, int buf_size)
  84. {
  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 = LE_16(buf); buf += 2;
  97. colors = LE_16(buf); buf += 2;
  98. csize = *buf++;
  99. x = LE_16(buf); buf += 2;
  100. y = LE_16(buf); buf += 2;
  101. w = LE_16(buf); buf += 2;
  102. h = LE_16(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. default:
  124. av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", avctx->bits_per_sample);
  125. return -1;
  126. }
  127. if(s->picture.data[0])
  128. avctx->release_buffer(avctx, &s->picture);
  129. if(avcodec_check_dimensions(avctx, w, h))
  130. return -1;
  131. if(w != avctx->width || h != avctx->height)
  132. avcodec_set_dimensions(avctx, w, h);
  133. if(avctx->get_buffer(avctx, p) < 0){
  134. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  135. return -1;
  136. }
  137. if(flags & 0x20){
  138. dst = p->data[0];
  139. stride = p->linesize[0];
  140. }else{ //image is upside-down
  141. dst = p->data[0] + p->linesize[0] * (h - 1);
  142. stride = -p->linesize[0];
  143. }
  144. if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
  145. memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
  146. if(avctx->palctrl->palette_changed){
  147. p->palette_has_changed = 1;
  148. avctx->palctrl->palette_changed = 0;
  149. }
  150. }
  151. if(colors){
  152. if((colors + first_clr) > 256){
  153. av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
  154. return -1;
  155. }
  156. if(csize != 24){
  157. av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
  158. return -1;
  159. }
  160. if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
  161. buf += colors * ((csize + 1) >> 3);
  162. else{
  163. int r, g, b, t;
  164. int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
  165. for(t = 0; t < colors; t++){
  166. r = *buf++;
  167. g = *buf++;
  168. b = *buf++;
  169. *pal++ = (b << 16) | (g << 8) | r;
  170. }
  171. p->palette_has_changed = 1;
  172. avctx->palctrl->palette_changed = 0;
  173. }
  174. }
  175. if((compr & (~TGA_RLE)) == TGA_NODATA)
  176. memset(p->data[0], 0, p->linesize[0] * s->height);
  177. else{
  178. if(compr & TGA_RLE)
  179. targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
  180. else{
  181. for(y = 0; y < s->height; y++){
  182. #ifdef WORDS_BIGENDIAN
  183. if((s->bpp + 1) >> 3 == 2){
  184. uint16_t *dst16 = (uint16_t*)dst;
  185. for(x = 0; x < s->width; x++)
  186. dst16[x] = LE_16(buf + x * 2);
  187. }else
  188. #endif
  189. memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
  190. dst += stride;
  191. buf += s->width * ((s->bpp + 1) >> 3);
  192. }
  193. }
  194. }
  195. *picture= *(AVFrame*)&s->picture;
  196. *data_size = sizeof(AVPicture);
  197. return buf_size;
  198. }
  199. static int targa_init(AVCodecContext *avctx){
  200. TargaContext *s = avctx->priv_data;
  201. avcodec_get_frame_defaults((AVFrame*)&s->picture);
  202. avctx->coded_frame= (AVFrame*)&s->picture;
  203. s->picture.data[0] = NULL;
  204. return 0;
  205. }
  206. static int targa_end(AVCodecContext *avctx){
  207. TargaContext *s = avctx->priv_data;
  208. if(s->picture.data[0])
  209. avctx->release_buffer(avctx, &s->picture);
  210. return 0;
  211. }
  212. AVCodec targa_decoder = {
  213. "targa",
  214. CODEC_TYPE_VIDEO,
  215. CODEC_ID_TARGA,
  216. sizeof(TargaContext),
  217. targa_init,
  218. NULL,
  219. targa_end,
  220. decode_frame,
  221. 0,
  222. NULL
  223. };