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.

150 lines
4.4KB

  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. #include "avcodec.h"
  22. #include "rle.h"
  23. /**
  24. * RLE compress the image, with maximum size of out_size
  25. * @param outbuf Output buffer
  26. * @param out_size Maximum output size
  27. * @param pic Image to compress
  28. * @param bpp Bytes per pixel
  29. * @param w Image width
  30. * @param h Image height
  31. * @return Size of output in bytes, or -1 if larger than out_size
  32. */
  33. static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
  34. int bpp, int w, int h)
  35. {
  36. int y,ret;
  37. uint8_t *out;
  38. out = outbuf;
  39. for(y = 0; y < h; y ++) {
  40. ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
  41. if(ret == -1){
  42. return -1;
  43. }
  44. out+= ret;
  45. out_size -= ret;
  46. }
  47. return out - outbuf;
  48. }
  49. static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
  50. {
  51. int i, n = bpp * w;
  52. uint8_t *out = outbuf;
  53. uint8_t *ptr = pic->data[0];
  54. for(i=0; i < h; i++) {
  55. memcpy(out, ptr, n);
  56. out += n;
  57. ptr += pic->linesize[0];
  58. }
  59. return out - outbuf;
  60. }
  61. static int targa_encode_frame(AVCodecContext *avctx,
  62. unsigned char *outbuf,
  63. int buf_size, void *data){
  64. AVFrame *p = data;
  65. int bpp, picsize, datasize;
  66. uint8_t *out;
  67. if(avctx->width > 0xffff || avctx->height > 0xffff) {
  68. av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
  69. return -1;
  70. }
  71. picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
  72. if(buf_size < picsize + 45) {
  73. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  74. return -1;
  75. }
  76. p->pict_type= FF_I_TYPE;
  77. p->key_frame= 1;
  78. /* zero out the header and only set applicable fields */
  79. memset(outbuf, 0, 11);
  80. AV_WL16(outbuf+12, avctx->width);
  81. AV_WL16(outbuf+14, avctx->height);
  82. outbuf[17] = 0x20; /* origin is top-left. no alpha */
  83. /* TODO: support alpha channel */
  84. switch(avctx->pix_fmt) {
  85. case PIX_FMT_GRAY8:
  86. outbuf[2] = 3; /* uncompressed grayscale image */
  87. outbuf[16] = 8; /* bpp */
  88. break;
  89. case PIX_FMT_RGB555:
  90. outbuf[2] = 2; /* uncompresses true-color image */
  91. outbuf[16] = 16; /* bpp */
  92. break;
  93. case PIX_FMT_BGR24:
  94. outbuf[2] = 2; /* uncompressed true-color image */
  95. outbuf[16] = 24; /* bpp */
  96. break;
  97. default:
  98. return -1;
  99. }
  100. bpp = outbuf[16] >> 3;
  101. out = outbuf + 18; /* skip past the header we just output */
  102. /* try RLE compression */
  103. datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
  104. /* if that worked well, mark the picture as RLE compressed */
  105. if(datasize >= 0)
  106. outbuf[2] |= 8;
  107. /* if RLE didn't make it smaller, go back to no compression */
  108. else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
  109. out += datasize;
  110. /* The standard recommends including this section, even if we don't use
  111. * any of the features it affords. TODO: take advantage of the pixel
  112. * aspect ratio and encoder ID fields available? */
  113. memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
  114. return out + 26 - outbuf;
  115. }
  116. static av_cold int targa_encode_init(AVCodecContext *avctx)
  117. {
  118. return 0;
  119. }
  120. AVCodec targa_encoder = {
  121. .name = "targa",
  122. .type = CODEC_TYPE_VIDEO,
  123. .id = CODEC_ID_TARGA,
  124. .priv_data_size = 0,
  125. .init = targa_encode_init,
  126. .encode = targa_encode_frame,
  127. .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1},
  128. };