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.

171 lines
5.2KB

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