2 * Targa (.tga) image encoder
3 * Copyright (c) 2007 Bobby Bingham
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
26 typedef struct TargaContext
{
31 * RLE compress the image, with maximum size of out_size
32 * @param outbuf Output buffer
33 * @param out_size Maximum output size
34 * @param pic Image to compress
35 * @param bpp Bytes per pixel
36 * @param w Image width
37 * @param h Image height
38 * @return Size of output in bytes, or -1 if larger than out_size
40 static int targa_encode_rle(uint8_t *outbuf
, int out_size
, AVFrame
*pic
,
41 int bpp
, int w
, int h
)
48 for(y
= 0; y
< h
; y
++) {
49 ret
= ff_rle_encode(out
, out_size
, pic
->data
[0] + pic
->linesize
[0] * y
, bpp
, w
, 0x7f, 0, -1, 0);
60 static int targa_encode_normal(uint8_t *outbuf
, AVFrame
*pic
, int bpp
, int w
, int h
)
63 uint8_t *out
= outbuf
;
64 uint8_t *ptr
= pic
->data
[0];
66 for(i
=0; i
< h
; i
++) {
69 ptr
+= pic
->linesize
[0];
75 static int targa_encode_frame(AVCodecContext
*avctx
,
76 unsigned char *outbuf
,
77 int buf_size
, void *data
){
79 int bpp
, picsize
, datasize
;
82 if(avctx
->width
> 0xffff || avctx
->height
> 0xffff) {
83 av_log(avctx
, AV_LOG_ERROR
, "image dimensions too large\n");
86 picsize
= avpicture_get_size(avctx
->pix_fmt
, avctx
->width
, avctx
->height
);
87 if(buf_size
< picsize
+ 45) {
88 av_log(avctx
, AV_LOG_ERROR
, "encoded frame too large\n");
92 p
->pict_type
= FF_I_TYPE
;
95 /* zero out the header and only set applicable fields */
96 memset(outbuf
, 0, 12);
97 AV_WL16(outbuf
+12, avctx
->width
);
98 AV_WL16(outbuf
+14, avctx
->height
);
99 outbuf
[17] = 0x20; /* origin is top-left. no alpha */
101 /* TODO: support alpha channel */
102 switch(avctx
->pix_fmt
) {
104 outbuf
[2] = 3; /* uncompressed grayscale image */
105 outbuf
[16] = 8; /* bpp */
108 outbuf
[2] = 2; /* uncompresses true-color image */
109 outbuf
[16] = 16; /* bpp */
112 outbuf
[2] = 2; /* uncompressed true-color image */
113 outbuf
[16] = 24; /* bpp */
118 bpp
= outbuf
[16] >> 3;
120 out
= outbuf
+ 18; /* skip past the header we just output */
122 /* try RLE compression */
123 datasize
= targa_encode_rle(out
, picsize
, p
, bpp
, avctx
->width
, avctx
->height
);
125 /* if that worked well, mark the picture as RLE compressed */
129 /* if RLE didn't make it smaller, go back to no compression */
130 else datasize
= targa_encode_normal(out
, p
, bpp
, avctx
->width
, avctx
->height
);
134 /* The standard recommends including this section, even if we don't use
135 * any of the features it affords. TODO: take advantage of the pixel
136 * aspect ratio and encoder ID fields available? */
137 memcpy(out
, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
139 return out
+ 26 - outbuf
;
142 static av_cold
int targa_encode_init(AVCodecContext
*avctx
)
144 TargaContext
*s
= avctx
->priv_data
;
146 avcodec_get_frame_defaults(&s
->picture
);
147 s
->picture
.key_frame
= 1;
148 avctx
->coded_frame
= &s
->picture
;
153 AVCodec targa_encoder
= {
155 .type
= CODEC_TYPE_VIDEO
,
156 .id
= CODEC_ID_TARGA
,
157 .priv_data_size
= sizeof(TargaContext
),
158 .init
= targa_encode_init
,
159 .encode
= targa_encode_frame
,
160 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_BGR24
, PIX_FMT_RGB555
, PIX_FMT_GRAY8
, PIX_FMT_NONE
},
161 .long_name
= NULL_IF_CONFIG_SMALL("Truevision Targa image"),