Libav
targaenc.c
Go to the documentation of this file.
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 
22 #include <string.h>
23 
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "rle.h"
32 #include "targa.h"
33 
34 typedef struct TargaContext {
35  AVClass *class;
36 
37  int rle;
38 } TargaContext;
39 
50 static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
51  int bpp, int w, int h)
52 {
53  int y,ret;
54  uint8_t *out;
55 
56  out = outbuf;
57 
58  for(y = 0; y < h; y ++) {
59  ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
60  if(ret == -1){
61  return -1;
62  }
63  out+= ret;
64  out_size -= ret;
65  }
66 
67  return out - outbuf;
68 }
69 
70 static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
71 {
72  int i, n = bpp * w;
73  uint8_t *out = outbuf;
74  uint8_t *ptr = pic->data[0];
75 
76  for(i=0; i < h; i++) {
77  memcpy(out, ptr, n);
78  out += n;
79  ptr += pic->linesize[0];
80  }
81 
82  return out - outbuf;
83 }
84 
85 static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
86  const AVFrame *p, int *got_packet)
87 {
88  TargaContext *s = avctx->priv_data;
89  int bpp, picsize, datasize = -1, ret;
90  uint8_t *out;
91 
92  picsize = av_image_get_buffer_size(avctx->pix_fmt,
93  avctx->width, avctx->height, 1);
94  if ((ret = ff_alloc_packet(pkt, picsize + 45)) < 0) {
95  av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
96  return ret;
97  }
98 
99  /* zero out the header and only set applicable fields */
100  memset(pkt->data, 0, 12);
101  AV_WL16(pkt->data+12, avctx->width);
102  AV_WL16(pkt->data+14, avctx->height);
103  /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
104  pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0);
105 
106  switch(avctx->pix_fmt) {
107  case AV_PIX_FMT_GRAY8:
108  pkt->data[2] = TGA_BW; /* uncompressed grayscale image */
109  pkt->data[16] = 8; /* bpp */
110  break;
111  case AV_PIX_FMT_RGB555LE:
112  pkt->data[2] = TGA_RGB; /* uncompresses true-color image */
113  pkt->data[16] = 16; /* bpp */
114  break;
115  case AV_PIX_FMT_BGR24:
116  pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
117  pkt->data[16] = 24; /* bpp */
118  break;
119  case AV_PIX_FMT_BGRA:
120  pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
121  pkt->data[16] = 32; /* bpp */
122  break;
123  default:
124  av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
125  av_get_pix_fmt_name(avctx->pix_fmt));
126  return AVERROR(EINVAL);
127  }
128  bpp = pkt->data[16] >> 3;
129 
130  out = pkt->data + 18; /* skip past the header we just output */
131 
132 #if FF_API_CODER_TYPE
134  if (avctx->coder_type == FF_CODER_TYPE_RAW)
135  s->rle = 0;
137 #endif
138 
139  /* try RLE compression */
140  if (s->rle)
141  datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
142 
143  /* if that worked well, mark the picture as RLE compressed */
144  if(datasize >= 0)
145  pkt->data[2] |= 8;
146 
147  /* if RLE didn't make it smaller, go back to no compression */
148  else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
149 
150  out += datasize;
151 
152  /* The standard recommends including this section, even if we don't use
153  * any of the features it affords. TODO: take advantage of the pixel
154  * aspect ratio and encoder ID fields available? */
155  memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
156 
157  pkt->size = out + 26 - pkt->data;
158  pkt->flags |= AV_PKT_FLAG_KEY;
159  *got_packet = 1;
160 
161  return 0;
162 }
163 
165 {
166  if (avctx->width > 0xffff || avctx->height > 0xffff) {
167  av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
168  return AVERROR(EINVAL);
169  }
170 
171 #if FF_API_CODED_FRAME
173  avctx->coded_frame->key_frame = 1;
176 #endif
177 
178  return 0;
179 }
180 
181 #define OFFSET(x) offsetof(TargaContext, x)
182 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
183 static const AVOption options[] = {
184  { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
185 
186  { NULL },
187 };
188 
189 static const AVClass targa_class = {
190  .class_name = "targa",
191  .item_name = av_default_item_name,
192  .option = options,
193  .version = LIBAVUTIL_VERSION_INT,
194 };
195 
197  .name = "targa",
198  .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
199  .type = AVMEDIA_TYPE_VIDEO,
200  .id = AV_CODEC_ID_TARGA,
201  .priv_data_size = sizeof(TargaContext),
202  .priv_class = &targa_class,
204  .encode2 = targa_encode_frame,
205  .pix_fmts = (const enum AVPixelFormat[]){
208  },
209 };
static const AVOption options[]
Definition: targaenc.c:183
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
Definition: targa.h:37
misc image utilities
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:112
int size
Definition: avcodec.h:1347
#define VE
Definition: targaenc.c:182
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
int out_size
Definition: movenc.c:55
AVCodec.
Definition: avcodec.h:3120
Definition: targa.h:36
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
targa file common definitions
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *p, int *got_packet)
Definition: targaenc.c:85
uint8_t * data
Definition: avcodec.h:1346
#define AV_WL16(p, val)
Definition: intreadwrite.h:231
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
Return the size in bytes of the amount of data required to store an image with the given parameters...
Definition: imgutils.c:323
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
#define OFFSET(x)
Definition: targaenc.c:181
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:92
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
common internal API header
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int width
picture width / height.
Definition: avcodec.h:1580
static const AVClass targa_class
Definition: targaenc.c:189
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1211
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:62
attribute_deprecated int coder_type
Definition: avcodec.h:2440
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
av_default_item_name
Definition: dnxhdenc.c:55
main external API structure.
Definition: avcodec.h:1409
Describe the class of an AVClass context structure.
Definition: log.h:34
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:257
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
AVCodec ff_targa_encoder
Definition: targaenc.c:196
static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic, int bpp, int w, int h)
RLE compress the image, with maximum size of out_size.
Definition: targaenc.c:50
static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
Definition: targaenc.c:70
Y , 8bpp.
Definition: pixfmt.h:67
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
#define FF_CODER_TYPE_RAW
Definition: avcodec.h:2431
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2797
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:582
void * priv_data
Definition: avcodec.h:1451
static av_cold int targa_encode_init(AVCodecContext *avctx)
Definition: targaenc.c:164
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
FILE * out
Definition: movenc.c:54
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp, int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
RLE compress the row, with maximum size of out_size.
Definition: rle.c:52
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:1704
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
This structure stores compressed data.
Definition: avcodec.h:1323