FFmpeg
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 "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "targa.h"
27 
28 typedef struct TargaContext {
30 } TargaContext;
31 
32 static uint8_t *advance_line(uint8_t *start, uint8_t *line,
33  int stride, int *y, int h, int interleave)
34 {
35  *y += interleave;
36 
37  if (*y < h) {
38  return line + interleave * stride;
39  } else {
40  *y = (*y + 1) & (interleave - 1);
41  if (*y && *y < h) {
42  return start + *y * stride;
43  } else {
44  return NULL;
45  }
46  }
47 }
48 
50  uint8_t *start, int w, int h, int stride,
51  int bpp, int interleave)
52 {
53  int x, y;
54  int depth = (bpp + 1) >> 3;
55  int type, count;
56  uint8_t *line = start;
57  uint8_t *dst = line;
58 
59  x = y = count = 0;
60  while (dst) {
61  if (bytestream2_get_bytes_left(&s->gb) <= 0) {
62  av_log(avctx, AV_LOG_ERROR,
63  "Ran ouf of data before end-of-image\n");
64  return AVERROR_INVALIDDATA;
65  }
66  type = bytestream2_get_byteu(&s->gb);
67  count = (type & 0x7F) + 1;
68  type &= 0x80;
69  if (!type) {
70  do {
71  int n = FFMIN(count, w - x);
72  bytestream2_get_buffer(&s->gb, dst, n * depth);
73  count -= n;
74  dst += n * depth;
75  x += n;
76  if (x == w) {
77  x = 0;
78  dst = line = advance_line(start, line, stride, &y, h, interleave);
79  }
80  } while (dst && count > 0);
81  } else {
82  uint8_t tmp[4];
83  bytestream2_get_buffer(&s->gb, tmp, depth);
84  do {
85  int n = FFMIN(count, w - x);
86  count -= n;
87  x += n;
88  do {
89  memcpy(dst, tmp, depth);
90  dst += depth;
91  } while (--n);
92  if (x == w) {
93  x = 0;
94  dst = line = advance_line(start, line, stride, &y, h, interleave);
95  }
96  } while (dst && count > 0);
97  }
98  }
99 
100  if (count) {
101  av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
102  return AVERROR_INVALIDDATA;
103  }
104 
105  return 0;
106 }
107 
108 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
109  int *got_frame, AVPacket *avpkt)
110 {
111  TargaContext * const s = avctx->priv_data;
112  uint8_t *dst;
113  int stride;
114  int idlen, pal, compr, y, w, h, bpp, flags, ret;
115  int first_clr, colors, csize;
116  int interleave;
117  size_t img_size;
118 
119  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
120 
121  /* parse image header */
122  idlen = bytestream2_get_byte(&s->gb);
123  pal = bytestream2_get_byte(&s->gb);
124  compr = bytestream2_get_byte(&s->gb);
125  first_clr = bytestream2_get_le16(&s->gb);
126  colors = bytestream2_get_le16(&s->gb);
127  csize = bytestream2_get_byte(&s->gb);
128  bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
129  w = bytestream2_get_le16(&s->gb);
130  h = bytestream2_get_le16(&s->gb);
131  bpp = bytestream2_get_byte(&s->gb);
132 
133  flags = bytestream2_get_byte(&s->gb);
134 
135  if (!pal && (first_clr || colors || csize)) {
136  av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
137  // specification says we should ignore those value in this case
138  first_clr = colors = csize = 0;
139  }
140 
141  if (bytestream2_get_bytes_left(&s->gb) < idlen + 2*colors) {
142  av_log(avctx, AV_LOG_ERROR,
143  "Not enough data to read header\n");
144  return AVERROR_INVALIDDATA;
145  }
146 
147  // skip identifier if any
148  bytestream2_skip(&s->gb, idlen);
149 
150  switch (bpp) {
151  case 8:
152  avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
153  break;
154  case 15:
155  case 16:
156  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
157  break;
158  case 24:
159  avctx->pix_fmt = AV_PIX_FMT_BGR24;
160  break;
161  case 32:
162  avctx->pix_fmt = AV_PIX_FMT_BGRA;
163  break;
164  default:
165  av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
166  return AVERROR_INVALIDDATA;
167  }
168 
169  if (colors && (colors + first_clr) > 256) {
170  av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
171  return AVERROR_INVALIDDATA;
172  }
173 
174  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
175  return ret;
176 
177  if ((compr & (~TGA_RLE)) == TGA_NODATA) {
178  return avpkt->size;
179  }
180 
181  if (!(compr & TGA_RLE)) {
182  img_size = w * ((bpp + 1) >> 3);
183  if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
184  av_log(avctx, AV_LOG_ERROR,
185  "Not enough data available for image\n");
186  return AVERROR_INVALIDDATA;
187  }
188  }
189 
190  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
191  return ret;
193 
194  if (flags & TGA_TOPTOBOTTOM) {
195  dst = p->data[0];
196  stride = p->linesize[0];
197  } else { //image is upside-down
198  dst = p->data[0] + p->linesize[0] * (h - 1);
199  stride = -p->linesize[0];
200  }
201 
203  flags & TGA_INTERLEAVE4 ? 4 : 1;
204 
205  if (colors) {
206  int pal_size, pal_sample_size;
207 
208  switch (csize) {
209  case 32: pal_sample_size = 4; break;
210  case 24: pal_sample_size = 3; break;
211  case 16:
212  case 15: pal_sample_size = 2; break;
213  default:
214  av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
215  return AVERROR_INVALIDDATA;
216  }
217  pal_size = colors * pal_sample_size;
218  if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
219  bytestream2_skip(&s->gb, pal_size);
220  else {
221  int t;
222  uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
223 
224  if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
225  av_log(avctx, AV_LOG_ERROR,
226  "Not enough data to read palette\n");
227  return AVERROR_INVALIDDATA;
228  }
229  switch (pal_sample_size) {
230  case 4:
231  for (t = 0; t < colors; t++)
232  *pal++ = bytestream2_get_le32u(&s->gb);
233  break;
234  case 3:
235  /* RGB24 */
236  for (t = 0; t < colors; t++)
237  *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
238  break;
239  case 2:
240  /* RGB555 */
241  for (t = 0; t < colors; t++) {
242  uint32_t v = bytestream2_get_le16u(&s->gb);
243  v = ((v & 0x7C00) << 9) |
244  ((v & 0x03E0) << 6) |
245  ((v & 0x001F) << 3);
246  /* left bit replication */
247  v |= (v & 0xE0E0E0U) >> 5;
248  *pal++ = (0xffU<<24) | v;
249  }
250  break;
251  }
252 #if FF_API_PALETTE_HAS_CHANGED
254  p->palette_has_changed = 1;
256 #endif
257  }
258  }
259 
260  if (compr & TGA_RLE) {
261  int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
262  if (res < 0)
263  return res;
264  } else {
265  uint8_t *line;
266  if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
267  av_log(avctx, AV_LOG_ERROR,
268  "Not enough data available for image\n");
269  return AVERROR_INVALIDDATA;
270  }
271 
272  line = dst;
273  y = 0;
274  do {
275  bytestream2_get_buffer(&s->gb, line, img_size);
277  } while (line);
278  }
279 
280  if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
281  for (int y = 0; y < h; y++) {
282  void *line = &p->data[0][y * p->linesize[0]];
283  for (int x = 0; x < w >> 1; x++) {
284  switch (bpp) {
285  case 32:
286  FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
287  break;
288  case 24:
289  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
290  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
291  FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
292  break;
293  case 16:
294  FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
295  break;
296  case 8:
297  FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
298  }
299  }
300  }
301  }
302 
303 
304  *got_frame = 1;
305 
306  return avpkt->size;
307 }
308 
310  .p.name = "targa",
311  CODEC_LONG_NAME("Truevision Targa image"),
312  .p.type = AVMEDIA_TYPE_VIDEO,
313  .p.id = AV_CODEC_ID_TARGA,
314  .p.capabilities = AV_CODEC_CAP_DR1,
315  .priv_data_size = sizeof(TargaContext),
317 };
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVFrame::palette_has_changed
attribute_deprecated int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:596
TGA_INTERLEAVE2
@ TGA_INTERLEAVE2
Definition: targa.h:44
GetByteContext
Definition: bytestream.h:33
TGA_RIGHTTOLEFT
@ TGA_RIGHTTOLEFT
Definition: targa.h:42
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
w
uint8_t w
Definition: llviddspenc.c:38
AVPacket::data
uint8_t * data
Definition: packet.h:539
FFCodec
Definition: codec_internal.h:127
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:76
AV_PIX_FMT_BGRA
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:102
targa.h
ff_set_dimensions
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:94
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:424
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:311
TGA_RLE
@ TGA_RLE
Definition: targa.h:38
s
#define s(width, name)
Definition: cbs_vp9.c:198
TGA_BW
@ TGA_BW
Definition: targa.h:37
TargaContext::gb
GetByteContext gb
Definition: targa.c:29
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:296
NULL
#define NULL
Definition: coverity.c:32
TargaContext
Definition: targa.c:28
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
bytestream2_get_buffer
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:267
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
interleave
static void interleave(uint8_t *dst, uint8_t *src, int w, int h, int dst_linesize, int src_linesize, enum FilterMode mode, int swap)
Definition: vf_il.c:113
decode_frame
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition: targa.c:108
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:505
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1697
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:540
codec_internal.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
line
Definition: graph2dot.c:48
AV_PIX_FMT_RGB555LE
@ AV_PIX_FMT_RGB555LE
packed RGB 5:5:5, 16bpp, (msb)1X 5R 5G 5B(lsb), little-endian, X=unused/undefined
Definition: pixfmt.h:115
TGA_INTERLEAVE4
@ TGA_INTERLEAVE4
Definition: targa.h:45
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:671
avcodec.h
stride
#define stride
Definition: h264pred_template.c:537
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:84
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:451
ff_targa_decoder
const FFCodec ff_targa_decoder
Definition: targa.c:309
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVPacket
This structure stores compressed data.
Definition: packet.h:516
targa_decode_rle
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:49
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:448
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
TGA_TOPTOBOTTOM
@ TGA_TOPTOBOTTOM
Definition: targa.h:43
h
h
Definition: vp9dsp_template.c:2070
AV_CODEC_ID_TARGA
@ AV_CODEC_ID_TARGA
Definition: codec_id.h:145
advance_line
static uint8_t * advance_line(uint8_t *start, uint8_t *line, int stride, int *y, int h, int interleave)
Definition: targa.c:32
TGA_NODATA
@ TGA_NODATA
Definition: targa.h:34
line
The official guide to swscale for confused that consecutive non overlapping rectangles of slice_bottom special converter These generally are unscaled converters of common like for each output line the vertical scaler pulls lines from a ring buffer When the ring buffer does not contain the wanted line
Definition: swscale.txt:40