FFmpeg
Loading...
Searching...
No Matches
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
31
32static 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");
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 if (bytestream2_get_bytes_left(&s->gb) < n * depth) {
73 av_log(avctx, AV_LOG_ERROR,
74 "Not enough data for raw RLE packet\n");
76 }
77 bytestream2_get_bufferu(&s->gb, dst, n * depth);
78 count -= n;
79 dst += n * depth;
80 x += n;
81 if (x == w) {
82 x = 0;
83 dst = line = advance_line(start, line, stride, &y, h, interleave);
84 }
85 } while (dst && count > 0);
86 } else {
87 uint8_t tmp[4];
88 if (bytestream2_get_buffer(&s->gb, tmp, depth) != depth) {
89 av_log(avctx, AV_LOG_ERROR,
90 "Not enough data for RLE pixel\n");
92 }
93 do {
94 int n = FFMIN(count, w - x);
95 count -= n;
96 x += n;
97 do {
98 memcpy(dst, tmp, depth);
99 dst += depth;
100 } while (--n);
101 if (x == w) {
102 x = 0;
103 dst = line = advance_line(start, line, stride, &y, h, interleave);
104 }
105 } while (dst && count > 0);
106 }
107 }
108
109 if (count) {
110 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds\n");
111 return AVERROR_INVALIDDATA;
112 }
113
114 return 0;
115}
116
117static int decode_frame(AVCodecContext *avctx, AVFrame *p,
118 int *got_frame, AVPacket *avpkt)
119{
120 TargaContext * const s = avctx->priv_data;
121 uint8_t *dst;
122 int stride;
123 int idlen, pal, compr, y, w, h, bpp, flags, ret;
124 int first_clr, colors, csize;
125 int interleave;
126 size_t img_size;
127
128 bytestream2_init(&s->gb, avpkt->data, avpkt->size);
129
130 /* parse image header */
131 idlen = bytestream2_get_byte(&s->gb);
132 pal = bytestream2_get_byte(&s->gb);
133 compr = bytestream2_get_byte(&s->gb);
134 first_clr = bytestream2_get_le16(&s->gb);
135 colors = bytestream2_get_le16(&s->gb);
136 csize = bytestream2_get_byte(&s->gb);
137 bytestream2_skip(&s->gb, 4); /* 2: x, 2: y */
138 w = bytestream2_get_le16(&s->gb);
139 h = bytestream2_get_le16(&s->gb);
140 bpp = bytestream2_get_byte(&s->gb);
141
142 flags = bytestream2_get_byte(&s->gb);
143
144 if (!pal && (first_clr || colors || csize)) {
145 av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
146 // specification says we should ignore those value in this case
147 first_clr = colors = csize = 0;
148 }
149
150 if (bytestream2_get_bytes_left(&s->gb) < idlen + 2*colors) {
151 av_log(avctx, AV_LOG_ERROR,
152 "Not enough data to read header\n");
153 return AVERROR_INVALIDDATA;
154 }
155
156 // skip identifier if any
157 bytestream2_skip(&s->gb, idlen);
158
159 switch (bpp) {
160 case 8:
161 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_PAL8;
162 break;
163 case 15:
164 case 16:
166 break;
167 case 24:
168 avctx->pix_fmt = AV_PIX_FMT_BGR24;
169 break;
170 case 32:
171 avctx->pix_fmt = AV_PIX_FMT_BGRA;
172 break;
173 default:
174 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", bpp);
175 return AVERROR_INVALIDDATA;
176 }
177
178 if (colors && (colors + first_clr) > 256) {
179 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
180 return AVERROR_INVALIDDATA;
181 }
182
183 if ((ret = ff_set_dimensions(avctx, w, h)) < 0)
184 return ret;
185
186 if ((compr & (~TGA_RLE)) == TGA_NODATA) {
187 return avpkt->size;
188 }
189
190 if (!(compr & TGA_RLE)) {
191 img_size = w * ((bpp + 1) >> 3);
192 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
193 av_log(avctx, AV_LOG_ERROR,
194 "Not enough data available for image\n");
195 return AVERROR_INVALIDDATA;
196 }
197 }
198
199 if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
200 return ret;
201 p->pict_type = AV_PICTURE_TYPE_I;
202
203 if (flags & TGA_TOPTOBOTTOM) {
204 dst = p->data[0];
205 stride = p->linesize[0];
206 } else { //image is upside-down
207 dst = p->data[0] + p->linesize[0] * (h - 1);
208 stride = -p->linesize[0];
209 }
210
212 flags & TGA_INTERLEAVE4 ? 4 : 1;
213
214 if (colors) {
215 int pal_size, pal_sample_size;
216
217 switch (csize) {
218 case 32: pal_sample_size = 4; break;
219 case 24: pal_sample_size = 3; break;
220 case 16:
221 case 15: pal_sample_size = 2; break;
222 default:
223 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
224 return AVERROR_INVALIDDATA;
225 }
226 pal_size = colors * pal_sample_size;
227 if (avctx->pix_fmt != AV_PIX_FMT_PAL8) //should not occur but skip palette anyway
228 bytestream2_skip(&s->gb, pal_size);
229 else {
230 int t;
231 uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
232
233 if (bytestream2_get_bytes_left(&s->gb) < pal_size) {
234 av_log(avctx, AV_LOG_ERROR,
235 "Not enough data to read palette\n");
236 return AVERROR_INVALIDDATA;
237 }
238 switch (pal_sample_size) {
239 case 4:
240 for (t = 0; t < colors; t++)
241 *pal++ = bytestream2_get_le32u(&s->gb);
242 break;
243 case 3:
244 /* RGB24 */
245 for (t = 0; t < colors; t++)
246 *pal++ = (0xffU<<24) | bytestream2_get_le24u(&s->gb);
247 break;
248 case 2:
249 /* RGB555 */
250 for (t = 0; t < colors; t++) {
251 uint32_t v = bytestream2_get_le16u(&s->gb);
252 v = ((v & 0x7C00) << 9) |
253 ((v & 0x03E0) << 6) |
254 ((v & 0x001F) << 3);
255 /* left bit replication */
256 v |= (v & 0xE0E0E0U) >> 5;
257 *pal++ = (0xffU<<24) | v;
258 }
259 break;
260 }
261 }
262 }
263
264 if (compr & TGA_RLE) {
265 int res = targa_decode_rle(avctx, s, dst, w, h, stride, bpp, interleave);
266 if (res < 0)
267 return res;
268 } else {
269 uint8_t *line;
270 if (bytestream2_get_bytes_left(&s->gb) < img_size * h) {
271 av_log(avctx, AV_LOG_ERROR,
272 "Not enough data available for image\n");
273 return AVERROR_INVALIDDATA;
274 }
275
276 line = dst;
277 y = 0;
278 do {
279 bytestream2_get_buffer(&s->gb, line, img_size);
281 } while (line);
282 }
283
284 if (flags & TGA_RIGHTTOLEFT) { // right-to-left, needs horizontal flip
285 for (int y = 0; y < h; y++) {
286 void *line = &p->data[0][y * p->linesize[0]];
287 for (int x = 0; x < w >> 1; x++) {
288 switch (bpp) {
289 case 32:
290 FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[w - x - 1]);
291 break;
292 case 24:
293 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x ], ((uint8_t *)line)[3 * w - 3 * x - 3]);
294 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * w - 3 * x - 2]);
295 FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * w - 3 * x - 1]);
296 break;
297 case 16:
298 FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[w - x - 1]);
299 break;
300 case 8:
301 FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[w - x - 1]);
302 }
303 }
304 }
305 }
306
307
308 *got_frame = 1;
309
310 return avpkt->size;
311}
312
314 .p.name = "targa",
315 CODEC_LONG_NAME("Truevision Targa image"),
316 .p.type = AVMEDIA_TYPE_VIDEO,
317 .p.id = AV_CODEC_ID_TARGA,
318 .p.capabilities = AV_CODEC_CAP_DR1,
319 .priv_data_size = sizeof(TargaContext),
321};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
const FFCodec ff_targa_decoder
Definition targa.c:313
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(const 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_h264.c:74
#define s(width, name)
Definition cbs_vp9.c:198
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#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:1781
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Definition utils.c:91
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_TARGA
Definition codec_id.h:143
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
uint32_t type
Definition jpegmpfenc.c:80
static int decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_frame, AVPacket *avpkt)
Definition 4xm.c:837
uint8_t w
Definition llvidencdsp.c:39
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
@ AV_PIX_FMT_BGRA
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
Definition pixfmt.h:102
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ 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
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition pixfmt.h:84
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition pixfmt.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
GetByteContext gb
Definition targa.c:29
#define stride
#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:32
static int decode_frame(AVCodecContext *avctx, AVFrame *p, int *got_frame, AVPacket *avpkt)
Definition targa.c:117
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
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[40]
Definition aes_ctr.c:52
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