List QuickTime container as MOV/QuickTime.
[FFMpeg-mirror/ffmpeg-vdpau.git] / libavcodec / targa.c
blobf9bc343997bd3c6868becf8a303dac00f47cd6cc
1 /*
2 * Targa (.tga) image decoder
3 * Copyright (c) 2006 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
21 #include "avcodec.h"
23 enum TargaCompr{
24 TGA_NODATA = 0, // no image data
25 TGA_PAL = 1, // palettized
26 TGA_RGB = 2, // true-color
27 TGA_BW = 3, // black & white or grayscale
28 TGA_RLE = 8, // flag pointing that data is RLE-coded
31 typedef struct TargaContext {
32 AVFrame picture;
34 int width, height;
35 int bpp;
36 int color_type;
37 int compression_type;
38 } TargaContext;
40 static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
42 int i, x, y;
43 int depth = (bpp + 1) >> 3;
44 int type, count;
45 int diff;
47 diff = stride - w * depth;
48 x = y = 0;
49 while(y < h){
50 type = *src++;
51 count = (type & 0x7F) + 1;
52 type &= 0x80;
53 if((x + count > w) && (x + count + 1 > (h - y) * w)){
54 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
55 return;
57 for(i = 0; i < count; i++){
58 switch(depth){
59 case 1:
60 *dst = *src;
61 break;
62 case 2:
63 *((uint16_t*)dst) = AV_RL16(src);
64 break;
65 case 3:
66 dst[0] = src[0];
67 dst[1] = src[1];
68 dst[2] = src[2];
69 break;
70 case 4:
71 *((uint32_t*)dst) = AV_RL32(src);
72 break;
74 dst += depth;
75 if(!type)
76 src += depth;
78 x++;
79 if(x == w){
80 x = 0;
81 y++;
82 dst += diff;
85 if(type)
86 src += depth;
90 static int decode_frame(AVCodecContext *avctx,
91 void *data, int *data_size,
92 const uint8_t *buf, int buf_size)
94 TargaContext * const s = avctx->priv_data;
95 AVFrame *picture = data;
96 AVFrame * const p= (AVFrame*)&s->picture;
97 uint8_t *dst;
98 int stride;
99 int idlen, pal, compr, x, y, w, h, bpp, flags;
100 int first_clr, colors, csize;
102 /* parse image header */
103 idlen = *buf++;
104 pal = *buf++;
105 compr = *buf++;
106 first_clr = AV_RL16(buf); buf += 2;
107 colors = AV_RL16(buf); buf += 2;
108 csize = *buf++;
109 x = AV_RL16(buf); buf += 2;
110 y = AV_RL16(buf); buf += 2;
111 w = AV_RL16(buf); buf += 2;
112 h = AV_RL16(buf); buf += 2;
113 bpp = *buf++;
114 flags = *buf++;
115 //skip identifier if any
116 buf += idlen;
117 s->bpp = bpp;
118 s->width = w;
119 s->height = h;
120 switch(s->bpp){
121 case 8:
122 avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
123 break;
124 case 15:
125 avctx->pix_fmt = PIX_FMT_RGB555;
126 break;
127 case 16:
128 avctx->pix_fmt = PIX_FMT_RGB555;
129 break;
130 case 24:
131 avctx->pix_fmt = PIX_FMT_BGR24;
132 break;
133 case 32:
134 avctx->pix_fmt = PIX_FMT_RGB32;
135 break;
136 default:
137 av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
138 return -1;
141 if(s->picture.data[0])
142 avctx->release_buffer(avctx, &s->picture);
144 if(avcodec_check_dimensions(avctx, w, h))
145 return -1;
146 if(w != avctx->width || h != avctx->height)
147 avcodec_set_dimensions(avctx, w, h);
148 if(avctx->get_buffer(avctx, p) < 0){
149 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
150 return -1;
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];
160 if(avctx->pix_fmt == PIX_FMT_PAL8 && avctx->palctrl){
161 memcpy(p->data[1], avctx->palctrl->palette, AVPALETTE_SIZE);
162 if(avctx->palctrl->palette_changed){
163 p->palette_has_changed = 1;
164 avctx->palctrl->palette_changed = 0;
167 if(colors){
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;
172 if(csize != 24){
173 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
174 return -1;
176 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
177 buf += colors * ((csize + 1) >> 3);
178 else{
179 int r, g, b, t;
180 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
181 for(t = 0; t < colors; t++){
182 r = *buf++;
183 g = *buf++;
184 b = *buf++;
185 *pal++ = (b << 16) | (g << 8) | r;
187 p->palette_has_changed = 1;
188 avctx->palctrl->palette_changed = 0;
191 if((compr & (~TGA_RLE)) == TGA_NODATA)
192 memset(p->data[0], 0, p->linesize[0] * s->height);
193 else{
194 if(compr & TGA_RLE)
195 targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
196 else{
197 for(y = 0; y < s->height; y++){
198 #ifdef WORDS_BIGENDIAN
199 if((s->bpp + 1) >> 3 == 2){
200 uint16_t *dst16 = (uint16_t*)dst;
201 for(x = 0; x < s->width; x++)
202 dst16[x] = AV_RL16(buf + x * 2);
203 }else if((s->bpp + 1) >> 3 == 4){
204 uint32_t *dst32 = (uint32_t*)dst;
205 for(x = 0; x < s->width; x++)
206 dst32[x] = AV_RL32(buf + x * 4);
207 }else
208 #endif
209 memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
211 dst += stride;
212 buf += s->width * ((s->bpp + 1) >> 3);
217 *picture= *(AVFrame*)&s->picture;
218 *data_size = sizeof(AVPicture);
220 return buf_size;
223 static av_cold int targa_init(AVCodecContext *avctx){
224 TargaContext *s = avctx->priv_data;
226 avcodec_get_frame_defaults((AVFrame*)&s->picture);
227 avctx->coded_frame= (AVFrame*)&s->picture;
228 s->picture.data[0] = NULL;
230 return 0;
233 static av_cold int targa_end(AVCodecContext *avctx){
234 TargaContext *s = avctx->priv_data;
236 if(s->picture.data[0])
237 avctx->release_buffer(avctx, &s->picture);
239 return 0;
242 AVCodec targa_decoder = {
243 "targa",
244 CODEC_TYPE_VIDEO,
245 CODEC_ID_TARGA,
246 sizeof(TargaContext),
247 targa_init,
248 NULL,
249 targa_end,
250 decode_frame,
252 NULL,
253 .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),