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.

210 lines
6.3KB

  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 <string.h>
  22. #include "libavutil/imgutils.h"
  23. #include "libavutil/internal.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/pixdesc.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "rle.h"
  30. #include "targa.h"
  31. typedef struct TargaContext {
  32. AVClass *class;
  33. int rle;
  34. } TargaContext;
  35. /**
  36. * RLE compress the image, with maximum size of out_size
  37. * @param outbuf Output buffer
  38. * @param out_size Maximum output size
  39. * @param pic Image to compress
  40. * @param bpp Bytes per pixel
  41. * @param w Image width
  42. * @param h Image height
  43. * @return Size of output in bytes, or -1 if larger than out_size
  44. */
  45. static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
  46. int bpp, int w, int h)
  47. {
  48. int y,ret;
  49. uint8_t *out;
  50. out = outbuf;
  51. for(y = 0; y < h; y ++) {
  52. ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
  53. if(ret == -1){
  54. return -1;
  55. }
  56. out+= ret;
  57. out_size -= ret;
  58. }
  59. return out - outbuf;
  60. }
  61. static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
  62. {
  63. int i, n = bpp * w;
  64. uint8_t *out = outbuf;
  65. uint8_t *ptr = pic->data[0];
  66. for(i=0; i < h; i++) {
  67. memcpy(out, ptr, n);
  68. out += n;
  69. ptr += pic->linesize[0];
  70. }
  71. return out - outbuf;
  72. }
  73. static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
  74. const AVFrame *p, int *got_packet)
  75. {
  76. TargaContext *s = avctx->priv_data;
  77. int bpp, picsize, datasize = -1, ret;
  78. uint8_t *out;
  79. picsize = av_image_get_buffer_size(avctx->pix_fmt,
  80. avctx->width, avctx->height, 1);
  81. if ((ret = ff_alloc_packet(pkt, picsize + 45)) < 0) {
  82. av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
  83. return ret;
  84. }
  85. /* zero out the header and only set applicable fields */
  86. memset(pkt->data, 0, 12);
  87. AV_WL16(pkt->data+12, avctx->width);
  88. AV_WL16(pkt->data+14, avctx->height);
  89. /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
  90. pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0);
  91. switch(avctx->pix_fmt) {
  92. case AV_PIX_FMT_GRAY8:
  93. pkt->data[2] = TGA_BW; /* uncompressed grayscale image */
  94. pkt->data[16] = 8; /* bpp */
  95. break;
  96. case AV_PIX_FMT_RGB555LE:
  97. pkt->data[2] = TGA_RGB; /* uncompresses true-color image */
  98. pkt->data[16] = 16; /* bpp */
  99. break;
  100. case AV_PIX_FMT_BGR24:
  101. pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
  102. pkt->data[16] = 24; /* bpp */
  103. break;
  104. case AV_PIX_FMT_BGRA:
  105. pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
  106. pkt->data[16] = 32; /* bpp */
  107. break;
  108. default:
  109. av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
  110. av_get_pix_fmt_name(avctx->pix_fmt));
  111. return AVERROR(EINVAL);
  112. }
  113. bpp = pkt->data[16] >> 3;
  114. out = pkt->data + 18; /* skip past the header we just output */
  115. #if FF_API_CODER_TYPE
  116. FF_DISABLE_DEPRECATION_WARNINGS
  117. if (avctx->coder_type == FF_CODER_TYPE_RAW)
  118. s->rle = 0;
  119. FF_ENABLE_DEPRECATION_WARNINGS
  120. #endif
  121. /* try RLE compression */
  122. if (s->rle)
  123. datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
  124. /* if that worked well, mark the picture as RLE compressed */
  125. if(datasize >= 0)
  126. pkt->data[2] |= 8;
  127. /* if RLE didn't make it smaller, go back to no compression */
  128. else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
  129. out += datasize;
  130. /* The standard recommends including this section, even if we don't use
  131. * any of the features it affords. TODO: take advantage of the pixel
  132. * aspect ratio and encoder ID fields available? */
  133. memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
  134. pkt->size = out + 26 - pkt->data;
  135. pkt->flags |= AV_PKT_FLAG_KEY;
  136. *got_packet = 1;
  137. return 0;
  138. }
  139. static av_cold int targa_encode_init(AVCodecContext *avctx)
  140. {
  141. if (avctx->width > 0xffff || avctx->height > 0xffff) {
  142. av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
  143. return AVERROR(EINVAL);
  144. }
  145. #if FF_API_CODED_FRAME
  146. FF_DISABLE_DEPRECATION_WARNINGS
  147. avctx->coded_frame->key_frame = 1;
  148. avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
  149. FF_ENABLE_DEPRECATION_WARNINGS
  150. #endif
  151. return 0;
  152. }
  153. #define OFFSET(x) offsetof(TargaContext, x)
  154. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  155. static const AVOption options[] = {
  156. { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
  157. { NULL },
  158. };
  159. static const AVClass targa_class = {
  160. .class_name = "targa",
  161. .item_name = av_default_item_name,
  162. .option = options,
  163. .version = LIBAVUTIL_VERSION_INT,
  164. };
  165. AVCodec ff_targa_encoder = {
  166. .name = "targa",
  167. .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
  168. .type = AVMEDIA_TYPE_VIDEO,
  169. .id = AV_CODEC_ID_TARGA,
  170. .priv_data_size = sizeof(TargaContext),
  171. .priv_class = &targa_class,
  172. .init = targa_encode_init,
  173. .encode2 = targa_encode_frame,
  174. .pix_fmts = (const enum AVPixelFormat[]){
  175. AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_GRAY8,
  176. AV_PIX_FMT_NONE
  177. },
  178. };