libavcodec/targa.c
Go to the documentation of this file.
00001 /*
00002  * Targa (.tga) image decoder
00003  * Copyright (c) 2006 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00022 #include "libavutil/intreadwrite.h"
00023 #include "libavutil/imgutils.h"
00024 #include "avcodec.h"
00025 #include "bytestream.h"
00026 #include "targa.h"
00027 
00028 typedef struct TargaContext {
00029     AVFrame picture;
00030 
00031     int width, height;
00032     int bpp;
00033     int color_type;
00034     int compression_type;
00035 } TargaContext;
00036 
00037 #define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
00038     if(needed > buf_end - buf){ \
00039         av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
00040         return -1; \
00041     } \
00042 
00043 static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
00044 {
00045     int i, x, y;
00046     int depth = (bpp + 1) >> 3;
00047     int type, count;
00048     int diff;
00049     const uint8_t *src_end = src + src_size;
00050 
00051     diff = stride - w * depth;
00052     x = y = 0;
00053     while(y < h){
00054         CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
00055         type = *src++;
00056         count = (type & 0x7F) + 1;
00057         type &= 0x80;
00058         if((x + count > w) && (x + count + 1 > (h - y) * w)){
00059             av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
00060             return -1;
00061         }
00062         if(type){
00063             CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
00064         }else{
00065             CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
00066         }
00067         for(i = 0; i < count; i++){
00068             switch(depth){
00069             case 1:
00070                 *dst = *src;
00071                 break;
00072             case 2:
00073                 AV_WN16A(dst, AV_RN16A(src));
00074                 break;
00075             case 3:
00076                 dst[0] = src[0];
00077                 dst[1] = src[1];
00078                 dst[2] = src[2];
00079                 break;
00080             case 4:
00081                 AV_WN32A(dst, AV_RN32A(src));
00082                 break;
00083             }
00084             dst += depth;
00085             if(!type)
00086                 src += depth;
00087 
00088             x++;
00089             if(x == w){
00090                 x = 0;
00091                 y++;
00092                 dst += diff;
00093             }
00094         }
00095         if(type)
00096             src += depth;
00097     }
00098     return src_size;
00099 }
00100 
00101 static int decode_frame(AVCodecContext *avctx,
00102                         void *data, int *data_size,
00103                         AVPacket *avpkt)
00104 {
00105     const uint8_t *buf = avpkt->data;
00106     const uint8_t *buf_end = avpkt->data + avpkt->size;
00107     TargaContext * const s = avctx->priv_data;
00108     AVFrame *picture = data;
00109     AVFrame * const p= (AVFrame*)&s->picture;
00110     uint8_t *dst;
00111     int stride;
00112     int idlen, pal, compr, y, w, h, bpp, flags;
00113     int first_clr, colors, csize;
00114 
00115     /* parse image header */
00116     CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
00117     idlen = *buf++;
00118     pal = *buf++;
00119     compr = *buf++;
00120     first_clr = bytestream_get_le16(&buf);
00121     colors = bytestream_get_le16(&buf);
00122     csize = *buf++;
00123     if (!pal && (first_clr || colors || csize)) {
00124         av_log(avctx, AV_LOG_WARNING, "File without colormap has colormap information set.\n");
00125         // specification says we should ignore those value in this case
00126         first_clr = colors = csize = 0;
00127     }
00128     buf += 2; /* x */
00129     y = bytestream_get_le16(&buf);
00130     w = bytestream_get_le16(&buf);
00131     h = bytestream_get_le16(&buf);
00132     bpp = *buf++;
00133     flags = *buf++;
00134     //skip identifier if any
00135     CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
00136     buf += idlen;
00137     s->bpp = bpp;
00138     s->width = w;
00139     s->height = h;
00140     switch(s->bpp){
00141     case 8:
00142         avctx->pix_fmt = ((compr & (~TGA_RLE)) == TGA_BW) ? PIX_FMT_GRAY8 : PIX_FMT_PAL8;
00143         break;
00144     case 15:
00145     case 16:
00146         avctx->pix_fmt = PIX_FMT_RGB555LE;
00147         break;
00148     case 24:
00149         avctx->pix_fmt = PIX_FMT_BGR24;
00150         break;
00151     case 32:
00152         avctx->pix_fmt = PIX_FMT_BGRA;
00153         break;
00154     default:
00155         av_log(avctx, AV_LOG_ERROR, "Bit depth %i is not supported\n", s->bpp);
00156         return -1;
00157     }
00158 
00159     if(s->picture.data[0])
00160         avctx->release_buffer(avctx, &s->picture);
00161 
00162     if(av_image_check_size(w, h, 0, avctx))
00163         return -1;
00164     if(w != avctx->width || h != avctx->height)
00165         avcodec_set_dimensions(avctx, w, h);
00166     if(avctx->get_buffer(avctx, p) < 0){
00167         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00168         return -1;
00169     }
00170     if(flags & 0x20){
00171         dst = p->data[0];
00172         stride = p->linesize[0];
00173     }else{ //image is upside-down
00174         dst = p->data[0] + p->linesize[0] * (h - 1);
00175         stride = -p->linesize[0];
00176     }
00177 
00178     if(colors){
00179         int pal_size, pal_sample_size;
00180         if((colors + first_clr) > 256){
00181             av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
00182             return -1;
00183         }
00184         switch (csize) {
00185         case 24: pal_sample_size = 3; break;
00186         case 16:
00187         case 15: pal_sample_size = 2; break;
00188         default:
00189             av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
00190             return -1;
00191         }
00192         pal_size = colors * pal_sample_size;
00193         CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
00194         if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
00195             buf += pal_size;
00196         else{
00197             int t;
00198             uint32_t *pal = ((uint32_t *)p->data[1]) + first_clr;
00199 
00200             switch (pal_sample_size) {
00201             case 3:
00202                 /* RGB24 */
00203                 for (t = 0; t < colors; t++)
00204                     *pal++ = (0xffU<<24) | bytestream_get_le24(&buf);
00205                 break;
00206             case 2:
00207                 /* RGB555 */
00208                 for (t = 0; t < colors; t++) {
00209                     uint32_t v = bytestream_get_le16(&buf);
00210                     v = ((v & 0x7C00) <<  9) |
00211                         ((v & 0x03E0) <<  6) |
00212                         ((v & 0x001F) <<  3);
00213                     /* left bit replication */
00214                     v |= (v & 0xE0E0E0U) >> 5;
00215                     *pal++ = (0xffU<<24) | v;
00216                 }
00217                 break;
00218             }
00219             p->palette_has_changed = 1;
00220         }
00221     }
00222     if((compr & (~TGA_RLE)) == TGA_NODATA)
00223         memset(p->data[0], 0, p->linesize[0] * s->height);
00224     else{
00225         if(compr & TGA_RLE){
00226             int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
00227             if (res < 0)
00228                 return -1;
00229             buf += res;
00230         }else{
00231             size_t img_size = s->width * ((s->bpp + 1) >> 3);
00232             CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
00233             for(y = 0; y < s->height; y++){
00234                     memcpy(dst, buf, img_size);
00235 
00236                 dst += stride;
00237                 buf += img_size;
00238             }
00239         }
00240     }
00241     if(flags & 0x10){ // right-to-left, needs horizontal flip
00242         int x;
00243         for(y = 0; y < s->height; y++){
00244             void *line = &p->data[0][y * p->linesize[0]];
00245             for(x = 0; x < s->width >> 1; x++){
00246                 switch(s->bpp){
00247                 case 32:
00248                     FFSWAP(uint32_t, ((uint32_t *)line)[x], ((uint32_t *)line)[s->width - x - 1]);
00249                     break;
00250                 case 24:
00251                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x    ], ((uint8_t *)line)[3 * s->width - 3 * x - 3]);
00252                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 1], ((uint8_t *)line)[3 * s->width - 3 * x - 2]);
00253                     FFSWAP(uint8_t, ((uint8_t *)line)[3 * x + 2], ((uint8_t *)line)[3 * s->width - 3 * x - 1]);
00254                     break;
00255                 case 16:
00256                     FFSWAP(uint16_t, ((uint16_t *)line)[x], ((uint16_t *)line)[s->width - x - 1]);
00257                     break;
00258                 case 8:
00259                     FFSWAP(uint8_t, ((uint8_t *)line)[x], ((uint8_t *)line)[s->width - x - 1]);
00260                 }
00261             }
00262         }
00263     }
00264 
00265     *picture= *(AVFrame*)&s->picture;
00266     *data_size = sizeof(AVPicture);
00267 
00268     return avpkt->size;
00269 }
00270 
00271 static av_cold int targa_init(AVCodecContext *avctx){
00272     TargaContext *s = avctx->priv_data;
00273 
00274     avcodec_get_frame_defaults((AVFrame*)&s->picture);
00275     avctx->coded_frame= (AVFrame*)&s->picture;
00276 
00277     return 0;
00278 }
00279 
00280 static av_cold int targa_end(AVCodecContext *avctx){
00281     TargaContext *s = avctx->priv_data;
00282 
00283     if(s->picture.data[0])
00284         avctx->release_buffer(avctx, &s->picture);
00285 
00286     return 0;
00287 }
00288 
00289 AVCodec ff_targa_decoder = {
00290     .name           = "targa",
00291     .type           = AVMEDIA_TYPE_VIDEO,
00292     .id             = CODEC_ID_TARGA,
00293     .priv_data_size = sizeof(TargaContext),
00294     .init           = targa_init,
00295     .close          = targa_end,
00296     .decode         = decode_frame,
00297     .capabilities   = CODEC_CAP_DR1,
00298     .long_name = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
00299 };