Libav
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 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 "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 
34 } TargaContext;
35 
37  uint8_t *dst, int w, int h, int stride, int bpp)
38 {
39  int x, y;
40  int depth = (bpp + 1) >> 3;
41  int type, count;
42  int diff;
43 
44  diff = stride - w * depth;
45  x = y = 0;
46  while (y < h) {
47  if (bytestream2_get_bytes_left(&s->gb) <= 0) {
48  av_log(avctx, AV_LOG_ERROR,
49  "Ran ouf of data before end-of-image\n");
50  return AVERROR_INVALIDDATA;
51  }
52  type = bytestream2_get_byteu(&s->gb);
53  count = (type & 0x7F) + 1;
54  type &= 0x80;
55  if (x + count > w && x + count + 1 > (h - y) * w) {
56  av_log(avctx, AV_LOG_ERROR,
57  "Packet went out of bounds: position (%i,%i) size %i\n",
58  x, y, count);
59  return AVERROR_INVALIDDATA;
60  }
61  if (!type) {
62  do {
63  int n = FFMIN(count, w - x);
64  bytestream2_get_buffer(&s->gb, dst, n * depth);
65  count -= n;
66  dst += n * depth;
67  x += n;
68  if (x == w) {
69  x = 0;
70  y++;
71  dst += diff;
72  }
73  } while (count > 0);
74  } else {
75  uint8_t tmp[4];
76  bytestream2_get_buffer(&s->gb, tmp, depth);
77  do {
78  int n = FFMIN(count, w - x);
79  count -= n;
80  x += n;
81  do {
82  memcpy(dst, tmp, depth);
83  dst += depth;
84  } while (--n);
85  if (x == w) {
86  x = 0;
87  y++;
88  dst += diff;
89  }
90  } while (count > 0);
91  }
92  }
93  return 0;
94 }
95 
96 static int decode_frame(AVCodecContext *avctx,
97  void *data, int *got_frame,
98  AVPacket *avpkt)
99 {
100  TargaContext * const s = avctx->priv_data;
101  AVFrame * const p = data;
102  uint8_t *dst;
103  int stride;
104  int idlen, compr, y, w, h, bpp, flags, ret;
105  int first_clr, colors, csize;
106 
107  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
108 
109  /* parse image header */
110  idlen = bytestream2_get_byte(&s->gb);
111  bytestream2_skip(&s->gb, 1); /* pal */
112  compr = bytestream2_get_byte(&s->gb);
113  first_clr = bytestream2_get_le16(&s->gb);
114  colors = bytestream2_get_le16(&s->gb);
115  csize = bytestream2_get_byte(&s->gb);
116  bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
117  w = bytestream2_get_le16(&s->gb);
118  h = bytestream2_get_le16(&s->gb);
119  bpp = bytestream2_get_byte(&s->gb);
120  flags = bytestream2_get_byte(&s->gb);
121  // skip identifier if any
122  bytestream2_skip(&s->gb, idlen);
123 
124  switch(bpp){
125  case 8:
126  avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
127  break;
128  case 15:
129  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
130  break;
131  case 16:
132  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
133  break;
134  case 24:
135  avctx->pix_fmt = AV_PIX_FMT_BGR24;
136  break;
137  case 32:
138  avctx->pix_fmt = AV_PIX_FMT_BGRA;
139  break;
140  default:
141  av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
142  return AVERROR_INVALIDDATA;
143  }
144 
145  if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
146  return ret;
147 
148  if ((ret = ff_get_buffer(avctx, p, 0)) < 0){
149  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
150  return ret;
151  }
152  if(flags & 0x20){
153  dst = p->data[0];
154  stride = p->linesize[0];
155  }else{ //image is upside-down
156  dst = p->data[0] + p->linesize[0] * (h - 1);
157  stride = -p->linesize[0];
158  }
159 
160  if(colors){
161  int pal_size, pal_sample_size;
162  if((colors + first_clr) > 256){
163  av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
164  return AVERROR_INVALIDDATA;
165  }
166  switch (csize) {
167  case 24: pal_sample_size = 3; break;
168  case 16:
169  case 15: pal_sample_size = 2; break;
170  default:
171  av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
172  return AVERROR_INVALIDDATA;
173  }
174  pal_size = colors * pal_sample_size;
175  if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
176  bytestream2_skip(&s->gb, pal_size);
177  else{
178  int t;
179  uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
180 
181  if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
182  av_log(avctx, AV_LOG_ERROR,
183  "Not enough data to read palette\n");
184  return AVERROR_INVALIDDATA;
185  }
186  switch (pal_sample_size) {
187  case 3:
188  /* RGB24 */
189  for (t = 0; t < colors; t++)
190  *pal++ = bytestream2_get_le24u(&s->gb);
191  break;
192  case 2:
193  /* RGB555 */
194  for (t = 0; t < colors; t++) {
195  uint32_t v = bytestream2_get_le16u(&s->gb);
196  v = ((v & 0x7C00) << 9) |
197  ((v & 0x03E0) << 6) |
198  ((v & 0x001F) << 3);
199  /* left bit replication */
200  v |= (v & 0xE0E0E0U) >> 5;
201  *pal++ = v;
202  }
203  break;
204  }
205  p->palette_has_changed = 1;
206  }
207  }
208  if ((compr & (~TGA_RLE)) == TGA_NODATA) {
209  memset(p->data[0], 0, p->linesize[0] * h);
210  } else {
211  if(compr & TGA_RLE){
212  int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
213  if (res < 0)
214  return res;
215  } else {
216  size_t img_size = w * ((bpp + 1) >> 3);
217  if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
218  av_log(avctx, AV_LOG_ERROR,
219  "Not enough data available for image\n");
220  return AVERROR_INVALIDDATA;
221  }
222  for (y = 0; y < h; y++) {
223  bytestream2_get_bufferu(&s->gb, dst, img_size);
224  dst += stride;
225  }
226  }
227  }
228 
229  *got_frame = 1;
230 
231  return avpkt->size;
232 }
233 
235  .name = "targa",
236  .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
237  .type = AVMEDIA_TYPE_VIDEO,
238  .id = AV_CODEC_ID_TARGA,
239  .priv_data_size = sizeof(TargaContext),
240  .decode = decode_frame,
241  .capabilities = CODEC_CAP_DR1,
242 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
Definition: targa.h:37
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:98
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:133
Definition: targa.h:38
int size
Definition: avcodec.h:974
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2796
static av_always_inline unsigned int bytestream2_get_bufferu(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:268
targa file common definitions
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
uint8_t
static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, uint8_t *dst, int w, int h, int stride, int bpp)
Definition: targa.c:36
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
static int flags
Definition: log.c:44
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: targa.c:96
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:159
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static av_always_inline unsigned int bytestream2_get_buffer(GetByteContext *g, uint8_t *dst, unsigned int size)
Definition: bytestream.h:258
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:149
Libavcodec external API header.
GetByteContext gb
Definition: targa.c:30
int color_type
Definition: targa.c:32
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:118
int compression_type
Definition: targa.c:33
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
AVCodec ff_targa_decoder
Definition: targa.c:234
main external API structure.
Definition: avcodec.h:1050
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
uint8_t * data
Definition: avcodec.h:973
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:330
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
void * priv_data
Definition: avcodec.h:1092
common internal api header.
Definition: vf_drawbox.c:37
Y , 8bpp.
Definition: pixfmt.h:73
This structure stores compressed data.
Definition: avcodec.h:950
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
#define FFMIN(a, b)
Definition: common.h:57