FFmpeg  4.4.9
targa.c
Go to the documentation of this file.
1 /*
2  * Targa (.tga) image decoder
3  * Copyright (c) 2006 Konstantin Shishkov
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 
22 #include "libavutil/intreadwrite.h"
23 #include "libavutil/imgutils.h"
24 #include "avcodec.h"
25 #include "bytestream.h"
26 #include "internal.h"
27 #include "targa.h"
28 
29 typedef struct TargaContext {
31 } TargaContext;
32 
34  int stride, int *y, int h, int interleave)
35 {
36  *y += interleave;
37 
38  if (*y < h) {
39  return line + interleave * stride;
40  } else {
41  *y = (*y + 1) & (interleave - 1);
42  if (*y && *y < h) {
43  return start + *y * stride;
44  } else {
45  return NULL;
46  }
47  }
48 }
49 
51  uint8_t *start, int w, int h, int stride,
52  int bpp, int interleave)
53 {
54  int x, y;
55  int depth = (bpp + 1) >> 3;
56  int type, count;
57  uint8_t *line = start;
58  uint8_t *dst = line;
59 
60  x = y = count = 0;
61  while (dst) {
62  if (bytestream2_get_bytes_left(&s->gb) <= 0) {
63  av_log(avctx, AV_LOG_ERROR,
64  "Ran ouf of data before end-of-image\n");
65  return AVERROR_INVALIDDATA;
66  }
67  type = bytestream2_get_byteu(&s->gb);
68  count = (type & 0x7F) + 1;
69  type &= 0x80;
70  if (!type) {
71  do {
72  int n = FFMIN(count, w - x);
73  if (bytestream2_get_bytes_left(&s->gb) < n * depth) {
74  av_log(avctx, AV_LOG_ERROR,
75  "Not enough data for raw RLE packet\n");
76  return AVERROR_INVALIDDATA;
77  }
78  bytestream2_get_bufferu(&s->gb, dst, n * depth);
79  count -= n;
80  dst += n * depth;
81  x += n;
82  if (x == w) {
83  x = 0;
84  dst = line = advance_line(start, line, stride, &y, h, interleave);
85  }
86  } while (dst && count > 0);
87  } else {
88  uint8_t tmp[4];
89  if (bytestream2_get_buffer(&s->gb, tmp, depth) != depth) {
90  av_log(avctx, AV_LOG_ERROR,
91  "Not enough data for RLE pixel\n");
92  return AVERROR_INVALIDDATA;
93  }
94  do {
95  int n = FFMIN(count, w - x);
96  count -= n;
97  x += n;
98  do {
99  memcpy(dst, tmp, depth);
100  dst += depth;
101  } while (--n);
102  if (x == w) {
103  x = 0;
104  dst = line = advance_line(start, line, stride, &y, h, interleave);
105  }
106  } while (dst && count > 0);
107  }
108  }
109 
110  if (count) {
111  av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
112  return AVERROR_INVALIDDATA;
113  }
114 
115  return 0;
116 }
117 
118 static int decode_frame(AVCodecContext *avctx,
119  void *data, int *got_frame,
120  AVPacket *avpkt)
121 {
122  TargaContext * const s = avctx->priv_data;
123  AVFrame * const p = data;
124  uint8_t *dst;
125  int stride;
126  int idlen, pal, compr, y, w, h, bpp, flags, ret;
127  int first_clr, colors, csize;
128  int interleave;
129 
130  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
131 
132  /* parse image header */
133  idlen = bytestream2_get_byte(&s->gb);
134  pal = bytestream2_get_byte(&s->gb);
135  compr = bytestream2_get_byte(&s->gb);
136  first_clr = bytestream2_get_le16(&s->gb);
137  colors = bytestream2_get_le16(&s->gb);
138  csize = bytestream2_get_byte(&s->gb);
139  bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
140  w = bytestream2_get_le16(&s->gb);
141  h = bytestream2_get_le16(&s->gb);
142  bpp = bytestream2_get_byte(&s->gb);
143 
144  flags = bytestream2_get_byte(&s->gb);
145 
146  if (!pal && (first_clr || colors || csize)) {
147  av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
148  // specification says we should ignore those value in this case
149  first_clr = colors = csize = 0;
150  }
151 
152  if (bytestream2_get_bytes_left(&s->gb) < idlen + 2*colors) {
153  av_log(avctx, AV_LOG_ERROR,
154  "Not enough data to read header\n");
155  return AVERROR_INVALIDDATA;
156  }
157 
158  // skip identifier if any
159  bytestream2_skip(&s->gb, idlen);
160 
161  switch (bpp) {
162  case 8:
163  avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
164  break;
165  case 15:
166  case 16:
167  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
168  break;
169  case 24:
170  avctx->pix_fmt = AV_PIX_FMT_BGR24;
171  break;
172  case 32:
173  avctx->pix_fmt = AV_PIX_FMT_BGRA;
174  break;
175  default:
176  av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
177  return AVERROR_INVALIDDATA;
178  }
179 
180  if (colors && (colors + first_clr) > 256) {
181  av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
182  return AVERROR_INVALIDDATA;
183  }
184 
185  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
186  return ret;
187 
188  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
189  return ret;
191 
192  if (flags & TGA_TOPTOBOTTOM) {
193  dst = p->data[0];
194  stride = p->linesize[0];
195  } else { //image is upside-down
196  dst = p->data[0] + p->linesize[0] * (h - 1);
197  stride = -p->linesize[0];
198  }
199 
201  flags & TGA_INTERLEAVE4 ? 4 : 1;
202 
203  if (colors) {
204  int pal_size, pal_sample_size;
205 
206  switch (csize) {
207  case 32: pal_sample_size = 4; break;
208  case 24: pal_sample_size = 3; break;
209  case 16:
210  case 15: pal_sample_size = 2; break;
211  default:
212  av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
213  return AVERROR_INVALIDDATA;
214  }
215  pal_size = colors * pal_sample_size;
216  if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
217  bytestream2_skip(&s->gb, pal_size);
218  else {
219  int t;
220  uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
221 
222  if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
223  av_log(avctx, AV_LOG_ERROR,
224  "Not enough data to read palette\n");
225  return AVERROR_INVALIDDATA;
226  }
227  switch (pal_sample_size) {
228  case 4:
229  for (t = 0; t < colors; t++)
230  *pal++ = bytestream2_get_le32u(&s->gb);
231  break;
232  case 3:
233  /* RGB24 */
234  for (t = 0; t < colors; t++)
235  *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
236  break;
237  case 2:
238  /* RGB555 */
239  for (t = 0; t < colors; t++) {
240  uint32_t v = bytestream2_get_le16u(&s->gb);
241  v = ((v & 0x7C00) << 9) |
242  ((v & 0x03E0) << 6) |
243  ((v & 0x001F) << 3);
244  /* left bit replication */
245  v |= (v & 0xE0E0E0U) >> 5;
246  *pal++ = (0xffU<<24) | v;
247  }
248  break;
249  }
250  p->palette_has_changed = 1;
251  }
252  }
253 
254  if ((compr & (~TGA_RLE)) == TGA_NODATA) {
255  memset(p->data[0], 0, p->linesize[0] * h);
256  } else {
257  if (compr & TGA_RLE) {
258  int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
259  if (res < 0)
260  return res;
261  } else {
262  size_t img_size = w * ((bpp + 1) >> 3);
263  uint8_t *line;
264  if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
265  av_log(avctx, AV_LOG_ERROR,
266  "Not enough data available for image\n");
267  return AVERROR_INVALIDDATA;
268  }
269 
270  line = dst;
271  y = 0;
272  do {
273  bytestream2_get_buffer(&s->gb, line, img_size);
274  line = advance_line(dst, line, stride, &y, h, interleave);
275  } while (line);
276  }
277 
278  if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
279  int x;
280  for (y = 0; y < h; y++) {
281  void *line = &p->data[0][y * p->linesize[0]];
282  for (x = 0; x < w >> 1; x++) {
283  switch (bpp) {
284  case 32:
285  FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
286  break;
287  case 24:
288  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
289  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
290  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
291  break;
292  case 16:
293  FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
294  break;
295  case 8:
296  FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
297  }
298  }
299  }
300  }
301  }
302 
303 
304  *got_frame = 1;
305 
306  return avpkt->size;
307 }
308 
310  .name = "targa",
311  .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
312  .type = AVMEDIA_TYPE_VIDEO,
313  .id = AV_CODEC_ID_TARGA,
314  .priv_data_size = sizeof(TargaContext),
315  .decode = decode_frame,
316  .capabilities = AV_CODEC_CAP_DR1,
317 };
uint8_t
Libavcodec external API header.
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:277
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define s(width, name)
Definition: cbs_vp9.c:257
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1911
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static av_always_inline void RENAME() interleave(TYPE *dst, TYPE *src0, TYPE *src1, int w2, int add, int shift)
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
@ AV_CODEC_ID_TARGA
Definition: codec_id.h:142
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
cl_device_type type
misc image utilities
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:84
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
uint8_t w
Definition: llviddspenc.c:39
int stride
Definition: mace.c:144
const char data[16]
Definition: mxf.c:142
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:95
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:108
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:475
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:401
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
uint8_t * data
Definition: packet.h:369
GetByteContext gb
Definition: targa.c:30
Definition: graph2dot.c:48
#define av_log(a,...)
static uint8_t * advance_line(uint8_t *start, uint8_t *line, int stride, int *y, int h, int interleave)
Definition: targa.c:33
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: targa.c:118
static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *start, int w, int h, int stride, int bpp, int interleave)
Definition: targa.c:50
AVCodec ff_targa_decoder
Definition: targa.c:309
targa file common definitions
@ TGA_BW
Definition: targa.h:37
@ TGA_NODATA
Definition: targa.h:34
@ TGA_RLE
Definition: targa.h:38
@ TGA_INTERLEAVE4
Definition: targa.h:45
@ TGA_RIGHTTOLEFT
Definition: targa.h:42
@ TGA_TOPTOBOTTOM
Definition: targa.h:43
@ TGA_INTERLEAVE2
Definition: targa.h:44
static uint8_t tmp[11]
Definition: aes_ctr.c:27