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 {
32 
35 } TargaContext;
36 
38  uint8_t *dst, int w, int h, int stride, int bpp)
39 {
40  int x, y;
41  int depth = (bpp + 1) >> 3;
42  int type, count;
43  int diff;
44 
45  diff = stride - w * depth;
46  x = y = 0;
47  while (y < h) {
48  if (bytestream2_get_bytes_left(&s->gb) <= 0) {
49  av_log(avctx, AV_LOG_ERROR,
50  "Ran ouf of data before end-of-image\n");
51  return AVERROR_INVALIDDATA;
52  }
53  type = bytestream2_get_byteu(&s->gb);
54  count = (type & 0x7F) + 1;
55  type &= 0x80;
56  if (x + count > w && x + count + 1 > (h - y) * w) {
57  av_log(avctx, AV_LOG_ERROR,
58  "Packet went out of bounds: position (%i,%i) size %i\n",
59  x, y, count);
60  return AVERROR_INVALIDDATA;
61  }
62  if (!type) {
63  do {
64  int n = FFMIN(count, w - x);
65  bytestream2_get_buffer(&s->gb, dst, n * depth);
66  count -= n;
67  dst += n * depth;
68  x += n;
69  if (x == w) {
70  x = 0;
71  y++;
72  dst += diff;
73  }
74  } while (count > 0);
75  } else {
76  uint8_t tmp[4];
77  bytestream2_get_buffer(&s->gb, tmp, depth);
78  do {
79  int n = FFMIN(count, w - x);
80  count -= n;
81  x += n;
82  do {
83  memcpy(dst, tmp, depth);
84  dst += depth;
85  } while (--n);
86  if (x == w) {
87  x = 0;
88  y++;
89  dst += diff;
90  }
91  } while (count > 0);
92  }
93  }
94  return 0;
95 }
96 
97 static int decode_frame(AVCodecContext *avctx,
98  void *data, int *got_frame,
99  AVPacket *avpkt)
100 {
101  TargaContext * const s = avctx->priv_data;
102  AVFrame *picture = data;
103  AVFrame * const p = &s->picture;
104  uint8_t *dst;
105  int stride;
106  int idlen, compr, y, w, h, bpp, flags;
107  int first_clr, colors, csize;
108 
109  bytestream2_init(&s->gb, avpkt->data, avpkt->size);
110 
111  /* parse image header */
112  idlen = bytestream2_get_byte(&s->gb);
113  bytestream2_skip(&s->gb, 1); /* pal */
114  compr = bytestream2_get_byte(&s->gb);
115  first_clr = bytestream2_get_le16(&s->gb);
116  colors = bytestream2_get_le16(&s->gb);
117  csize = bytestream2_get_byte(&s->gb);
118  bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
119  w = bytestream2_get_le16(&s->gb);
120  h = bytestream2_get_le16(&s->gb);
121  bpp = bytestream2_get_byte(&s->gb);
122  flags = bytestream2_get_byte(&s->gb);
123  // skip identifier if any
124  bytestream2_skip(&s->gb, idlen);
125 
126  switch(bpp){
127  case 8:
128  avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
129  break;
130  case 15:
131  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
132  break;
133  case 16:
134  avctx->pix_fmt = AV_PIX_FMT_RGB555LE;
135  break;
136  case 24:
137  avctx->pix_fmt = AV_PIX_FMT_BGR24;
138  break;
139  case 32:
140  avctx->pix_fmt = AV_PIX_FMT_BGRA;
141  break;
142  default:
143  av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
144  return -1;
145  }
146 
147  if(s->picture.data[0])
148  avctx->release_buffer(avctx, &s->picture);
149 
150  if(av_image_check_size(w, h, 0, avctx))
151  return -1;
152  if(w != avctx->width || h != avctx->height)
153  avcodec_set_dimensions(avctx, w, h);
154  if(ff_get_buffer(avctx, p) < 0){
155  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
156  return -1;
157  }
158  if(flags & 0x20){
159  dst = p->data[0];
160  stride = p->linesize[0];
161  }else{ //image is upside-down
162  dst = p->data[0] + p->linesize[0] * (h - 1);
163  stride = -p->linesize[0];
164  }
165 
166  if(colors){
167  int pal_size, pal_sample_size;
168  if((colors + first_clr) > 256){
169  av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
170  return -1;
171  }
172  switch (csize) {
173  case 24: pal_sample_size = 3; break;
174  case 16:
175  case 15: pal_sample_size = 2; break;
176  default:
177  av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
178  return -1;
179  }
180  pal_size = colors * pal_sample_size;
181  if(avctx->pix_fmt != AV_PIX_FMT_PAL8)//should not occur but skip palette anyway
182  bytestream2_skip(&s->gb, pal_size);
183  else{
184  int t;
185  uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
186 
187  if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
188  av_log(avctx, AV_LOG_ERROR,
189  "Not enough data to read palette\n");
190  return AVERROR_INVALIDDATA;
191  }
192  switch (pal_sample_size) {
193  case 3:
194  /* RGB24 */
195  for (t = 0; t < colors; t++)
196  *pal++ = bytestream2_get_le24u(&s->gb);
197  break;
198  case 2:
199  /* RGB555 */
200  for (t = 0; t < colors; t++) {
201  uint32_t v = bytestream2_get_le16u(&s->gb);
202  v = ((v & 0x7C00) << 9) |
203  ((v & 0x03E0) << 6) |
204  ((v & 0x001F) << 3);
205  /* left bit replication */
206  v |= (v & 0xE0E0E0U) >> 5;
207  *pal++ = v;
208  }
209  break;
210  }
211  p->palette_has_changed = 1;
212  }
213  }
214  if ((compr & (~TGA_RLE)) == TGA_NODATA) {
215  memset(p->data[0], 0, p->linesize[0] * h);
216  } else {
217  if(compr & TGA_RLE){
218  int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp);
219  if (res < 0)
220  return res;
221  } else {
222  size_t img_size = w * ((bpp + 1) >> 3);
223  if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
224  av_log(avctx, AV_LOG_ERROR,
225  "Not enough data available for image\n");
226  return AVERROR_INVALIDDATA;
227  }
228  for (y = 0; y < h; y++) {
229  bytestream2_get_bufferu(&s->gb, dst, img_size);
230  dst += stride;
231  }
232  }
233  }
234 
235  *picture = s->picture;
236  *got_frame = 1;
237 
238  return avpkt->size;
239 }
240 
241 static av_cold int targa_init(AVCodecContext *avctx){
242  TargaContext *s = avctx->priv_data;
243 
245  avctx->coded_frame = &s->picture;
246 
247  return 0;
248 }
249 
250 static av_cold int targa_end(AVCodecContext *avctx){
251  TargaContext *s = avctx->priv_data;
252 
253  if(s->picture.data[0])
254  avctx->release_buffer(avctx, &s->picture);
255 
256  return 0;
257 }
258 
260  .name = "targa",
261  .type = AVMEDIA_TYPE_VIDEO,
262  .id = AV_CODEC_ID_TARGA,
263  .priv_data_size = sizeof(TargaContext),
264  .init = targa_init,
265  .close = targa_end,
266  .decode = decode_frame,
267  .capabilities = CODEC_CAP_DR1,
268  .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
269 };
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
Definition: targa.h:37
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
misc image utilities
Definition: targa.h:38
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
packed RGB 5:5:5, 16bpp, (msb)1A 5R 5G 5B(lsb), little-endian, most significant bit to 0 ...
Definition: pixfmt.h:114
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:149
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:130
AVFrame picture
Definition: targa.c:30
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2960
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:228
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:37
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int flags
Definition: log.c:42
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: targa.c:97
static float t
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
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:88
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition: pixfmt.h:96
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
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
GetByteContext gb
Definition: targa.c:31
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:220
static AVFrame * picture
int width
picture width / height.
Definition: avcodec.h:1508
int color_type
Definition: targa.c:33
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
static av_cold int targa_init(AVCodecContext *avctx)
Definition: targa.c:241
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
int compression_type
Definition: targa.c:34
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
AVCodec ff_targa_decoder
Definition: targa.c:259
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:602
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
Definition: vf_drawbox.c:36
struct TargaContext TargaContext
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
static av_cold int targa_end(AVCodecContext *avctx)
Definition: targa.c:250
void * priv_data
Definition: avcodec.h:1382
This structure stores compressed data.
Definition: avcodec.h:898