Libav
|
00001 /* 00002 * Targa (.tga) image encoder 00003 * Copyright (c) 2007 Bobby Bingham 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 "avcodec.h" 00024 #include "rle.h" 00025 00026 typedef struct TargaContext { 00027 AVFrame picture; 00028 } TargaContext; 00029 00040 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic, 00041 int bpp, int w, int h) 00042 { 00043 int y,ret; 00044 uint8_t *out; 00045 00046 out = outbuf; 00047 00048 for(y = 0; y < h; y ++) { 00049 ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0); 00050 if(ret == -1){ 00051 return -1; 00052 } 00053 out+= ret; 00054 out_size -= ret; 00055 } 00056 00057 return out - outbuf; 00058 } 00059 00060 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h) 00061 { 00062 int i, n = bpp * w; 00063 uint8_t *out = outbuf; 00064 uint8_t *ptr = pic->data[0]; 00065 00066 for(i=0; i < h; i++) { 00067 memcpy(out, ptr, n); 00068 out += n; 00069 ptr += pic->linesize[0]; 00070 } 00071 00072 return out - outbuf; 00073 } 00074 00075 static int targa_encode_frame(AVCodecContext *avctx, 00076 unsigned char *outbuf, 00077 int buf_size, void *data){ 00078 AVFrame *p = data; 00079 int bpp, picsize, datasize = -1; 00080 uint8_t *out; 00081 00082 if(avctx->width > 0xffff || avctx->height > 0xffff) { 00083 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n"); 00084 return -1; 00085 } 00086 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); 00087 if(buf_size < picsize + 45) { 00088 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); 00089 return -1; 00090 } 00091 00092 p->pict_type= FF_I_TYPE; 00093 p->key_frame= 1; 00094 00095 /* zero out the header and only set applicable fields */ 00096 memset(outbuf, 0, 12); 00097 AV_WL16(outbuf+12, avctx->width); 00098 AV_WL16(outbuf+14, avctx->height); 00099 outbuf[17] = 0x20; /* origin is top-left. no alpha */ 00100 00101 /* TODO: support alpha channel */ 00102 switch(avctx->pix_fmt) { 00103 case PIX_FMT_GRAY8: 00104 outbuf[2] = 3; /* uncompressed grayscale image */ 00105 outbuf[16] = 8; /* bpp */ 00106 break; 00107 case PIX_FMT_RGB555LE: 00108 outbuf[2] = 2; /* uncompresses true-color image */ 00109 outbuf[16] = 16; /* bpp */ 00110 break; 00111 case PIX_FMT_BGR24: 00112 outbuf[2] = 2; /* uncompressed true-color image */ 00113 outbuf[16] = 24; /* bpp */ 00114 break; 00115 default: 00116 return -1; 00117 } 00118 bpp = outbuf[16] >> 3; 00119 00120 out = outbuf + 18; /* skip past the header we just output */ 00121 00122 /* try RLE compression */ 00123 if (avctx->coder_type != FF_CODER_TYPE_RAW) 00124 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height); 00125 00126 /* if that worked well, mark the picture as RLE compressed */ 00127 if(datasize >= 0) 00128 outbuf[2] |= 8; 00129 00130 /* if RLE didn't make it smaller, go back to no compression */ 00131 else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height); 00132 00133 out += datasize; 00134 00135 /* The standard recommends including this section, even if we don't use 00136 * any of the features it affords. TODO: take advantage of the pixel 00137 * aspect ratio and encoder ID fields available? */ 00138 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26); 00139 00140 return out + 26 - outbuf; 00141 } 00142 00143 static av_cold int targa_encode_init(AVCodecContext *avctx) 00144 { 00145 TargaContext *s = avctx->priv_data; 00146 00147 avcodec_get_frame_defaults(&s->picture); 00148 s->picture.key_frame= 1; 00149 avctx->coded_frame= &s->picture; 00150 00151 return 0; 00152 } 00153 00154 AVCodec targa_encoder = { 00155 .name = "targa", 00156 .type = AVMEDIA_TYPE_VIDEO, 00157 .id = CODEC_ID_TARGA, 00158 .priv_data_size = sizeof(TargaContext), 00159 .init = targa_encode_init, 00160 .encode = targa_encode_frame, 00161 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE}, 00162 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"), 00163 };