targaenc.c 4.8 KB

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