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.

200 lines
6.1KB

  1. /*
  2. * Targa (.tga) image encoder
  3. * Copyright (c) 2007 Bobby Bingham
  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. /**
  24. * Count up to 127 consecutive pixels which are either all the same or
  25. * all differ from the previous and next pixels.
  26. * @param start Pointer to the first pixel
  27. * @param len Maximum number of pixels
  28. * @param bpp Bytes per pixel
  29. * @param same 1 if searching for identical pixel values. 0 for differing
  30. * @return Number of matching consecutive pixels found
  31. */
  32. static int count_pixels(uint8_t *start, int len, int bpp, int same)
  33. {
  34. uint8_t *pos;
  35. int count = 1;
  36. for(pos = start + bpp; count < FFMIN(128, len); pos += bpp, count ++) {
  37. if(same != !memcmp(pos-bpp, pos, bpp)) {
  38. if(!same) {
  39. /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single
  40. * raw block of pixels. for larger bpp, RLE is as good or better */
  41. if(bpp == 1 && count + 1 < FFMIN(128, len) && *pos != *(pos+1))
  42. continue;
  43. /* if RLE can encode the next block better than as a raw block,
  44. * back up and leave _all_ the identical pixels for RLE */
  45. count --;
  46. }
  47. break;
  48. }
  49. }
  50. return count;
  51. }
  52. /**
  53. * RLE compress the image, with maximum size of out_size
  54. * @param outbuf Output buffer
  55. * @param out_size Maximum output size
  56. * @param pic Image to compress
  57. * @param bpp Bytes per pixel
  58. * @param w Image width
  59. * @param h Image height
  60. * @return Size of output in bytes, or -1 if larger than out_size
  61. */
  62. static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
  63. int bpp, int w, int h)
  64. {
  65. int count, x, y;
  66. uint8_t *ptr, *line, *out;
  67. out = outbuf;
  68. line = pic->data[0];
  69. for(y = 0; y < h; y ++) {
  70. ptr = line;
  71. for(x = 0; x < w; x += count) {
  72. /* see if we can encode the next set of pixels with RLE */
  73. if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
  74. if(out + bpp + 1 > outbuf + out_size) return -1;
  75. *out++ = 0x80 | (count - 1);
  76. memcpy(out, ptr, bpp);
  77. out += bpp;
  78. } else {
  79. /* fall back on uncompressed */
  80. count = count_pixels(ptr, w-x, bpp, 0);
  81. *out++ = count - 1;
  82. if(out + bpp*count > outbuf + out_size) return -1;
  83. memcpy(out, ptr, bpp * count);
  84. out += bpp * count;
  85. }
  86. ptr += count * bpp;
  87. }
  88. line += pic->linesize[0];
  89. }
  90. return out - outbuf;
  91. }
  92. static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
  93. {
  94. int i, n = bpp * w;
  95. uint8_t *out = outbuf;
  96. uint8_t *ptr = pic->data[0];
  97. for(i=0; i < h; i++) {
  98. memcpy(out, ptr, n);
  99. out += n;
  100. ptr += pic->linesize[0];
  101. }
  102. return out - outbuf;
  103. }
  104. static int targa_encode_frame(AVCodecContext *avctx,
  105. unsigned char *outbuf,
  106. int buf_size, void *data){
  107. AVFrame *p = data;
  108. int bpp, picsize, datasize;
  109. uint8_t *out;
  110. if(avctx->width > 0xffff || avctx->height > 0xffff) {
  111. av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
  112. return -1;
  113. }
  114. picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  115. if(buf_size < picsize + 45) {
  116. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  117. return -1;
  118. }
  119. p->pict_type= FF_I_TYPE;
  120. p->key_frame= 1;
  121. /* zero out the header and only set applicable fields */
  122. memset(outbuf, 0, 11);
  123. AV_WL16(outbuf+12, avctx->width);
  124. AV_WL16(outbuf+14, avctx->height);
  125. outbuf[17] = 0x20; /* origin is top-left. no alpha */
  126. /* TODO: support alpha channel */
  127. switch(avctx->pix_fmt) {
  128. case PIX_FMT_GRAY8:
  129. outbuf[2] = 3; /* uncompressed grayscale image */
  130. outbuf[16] = 8; /* bpp */
  131. break;
  132. case PIX_FMT_RGB555:
  133. outbuf[2] = 2; /* uncompresses true-color image */
  134. outbuf[16] = 16; /* bpp */
  135. break;
  136. case PIX_FMT_BGR24:
  137. outbuf[2] = 2; /* uncompressed true-color image */
  138. outbuf[16] = 24; /* bpp */
  139. break;
  140. default:
  141. return -1;
  142. }
  143. bpp = outbuf[16] >> 3;
  144. out = outbuf + 18; /* skip past the header we just output */
  145. /* try RLE compression */
  146. datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
  147. /* if that worked well, mark the picture as RLE compressed */
  148. if(datasize >= 0)
  149. outbuf[2] |= 8;
  150. /* if RLE didn't make it smaller, go back to no compression */
  151. else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
  152. out += datasize;
  153. /* The standard recommends including this section, even if we don't use
  154. * any of the features it affords. TODO: take advantage of the pixel
  155. * aspect ratio and encoder ID fields available? */
  156. memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
  157. return out + 26 - outbuf;
  158. }
  159. static int targa_encode_init(AVCodecContext *avctx)
  160. {
  161. return 0;
  162. }
  163. AVCodec targa_encoder = {
  164. .name = "targa",
  165. .type = CODEC_TYPE_VIDEO,
  166. .id = CODEC_ID_TARGA,
  167. .priv_data_size = 0,
  168. .init = targa_encode_init,
  169. .encode = targa_encode_frame,
  170. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1},
  171. };