00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avcodec.h"
00024 #include "rle.h"
00025 #include "targa.h"
00026
00027 typedef struct TargaContext {
00028 AVFrame picture;
00029 } TargaContext;
00030
00041 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
00042 int bpp, int w, int h)
00043 {
00044 int y,ret;
00045 uint8_t *out;
00046
00047 out = outbuf;
00048
00049 for(y = 0; y < h; y ++) {
00050 ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
00051 if(ret == -1){
00052 return -1;
00053 }
00054 out+= ret;
00055 out_size -= ret;
00056 }
00057
00058 return out - outbuf;
00059 }
00060
00061 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
00062 {
00063 int i, n = bpp * w;
00064 uint8_t *out = outbuf;
00065 uint8_t *ptr = pic->data[0];
00066
00067 for(i=0; i < h; i++) {
00068 memcpy(out, ptr, n);
00069 out += n;
00070 ptr += pic->linesize[0];
00071 }
00072
00073 return out - outbuf;
00074 }
00075
00076 static int targa_encode_frame(AVCodecContext *avctx,
00077 unsigned char *outbuf,
00078 int buf_size, void *data){
00079 AVFrame *p = data;
00080 int bpp, picsize, datasize = -1;
00081 uint8_t *out;
00082
00083 if(avctx->width > 0xffff || avctx->height > 0xffff) {
00084 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
00085 return AVERROR(EINVAL);
00086 }
00087 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
00088 if(buf_size < picsize + 45) {
00089 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
00090 return AVERROR(EINVAL);
00091 }
00092
00093 p->pict_type= FF_I_TYPE;
00094 p->key_frame= 1;
00095
00096
00097 memset(outbuf, 0, 12);
00098 AV_WL16(outbuf+12, avctx->width);
00099 AV_WL16(outbuf+14, avctx->height);
00100
00101 outbuf[17] = 0x20 | (avctx->pix_fmt == PIX_FMT_BGRA ? 8 : 0);
00102
00103 switch(avctx->pix_fmt) {
00104 case PIX_FMT_GRAY8:
00105 outbuf[2] = TGA_BW;
00106 outbuf[16] = 8;
00107 break;
00108 case PIX_FMT_RGB555LE:
00109 outbuf[2] = TGA_RGB;
00110 outbuf[16] = 16;
00111 break;
00112 case PIX_FMT_BGR24:
00113 outbuf[2] = TGA_RGB;
00114 outbuf[16] = 24;
00115 break;
00116 case PIX_FMT_BGRA:
00117 outbuf[2] = TGA_RGB;
00118 outbuf[16] = 32;
00119 break;
00120 default:
00121 av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
00122 avcodec_get_pix_fmt_name(avctx->pix_fmt));
00123 return AVERROR(EINVAL);
00124 }
00125 bpp = outbuf[16] >> 3;
00126
00127 out = outbuf + 18;
00128
00129
00130 if (avctx->coder_type != FF_CODER_TYPE_RAW)
00131 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
00132
00133
00134 if(datasize >= 0)
00135 outbuf[2] |= 8;
00136
00137
00138 else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
00139
00140 out += datasize;
00141
00142
00143
00144
00145 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
00146
00147 return out + 26 - outbuf;
00148 }
00149
00150 static av_cold int targa_encode_init(AVCodecContext *avctx)
00151 {
00152 TargaContext *s = avctx->priv_data;
00153
00154 avcodec_get_frame_defaults(&s->picture);
00155 s->picture.key_frame= 1;
00156 avctx->coded_frame= &s->picture;
00157
00158 return 0;
00159 }
00160
00161 AVCodec ff_targa_encoder = {
00162 .name = "targa",
00163 .type = AVMEDIA_TYPE_VIDEO,
00164 .id = CODEC_ID_TARGA,
00165 .priv_data_size = sizeof(TargaContext),
00166 .init = targa_encode_init,
00167 .encode = targa_encode_frame,
00168 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
00169 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
00170 };