libavcodec/targaenc.c
2c0c00cb
 /*
  * Targa (.tga) image encoder
  * Copyright (c) 2007 Bobby Bingham
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * FFmpeg is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
6a5d31ac
 
 #include "libavutil/intreadwrite.h"
94bed8e5
 #include "libavutil/pixdesc.h"
2c0c00cb
 #include "avcodec.h"
1ca286e1
 #include "rle.h"
b5becbab
 #include "targa.h"
92964be8
 
e2d09b65
 typedef struct TargaContext {
     AVFrame picture;
 } TargaContext;
 
92964be8
 /**
  * RLE compress the image, with maximum size of out_size
  * @param outbuf Output buffer
  * @param out_size Maximum output size
  * @param pic Image to compress
  * @param bpp Bytes per pixel
  * @param w Image width
  * @param h Image height
  * @return Size of output in bytes, or -1 if larger than out_size
  */
 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
                             int bpp, int w, int h)
 {
1ca286e1
     int y,ret;
     uint8_t *out;
92964be8
 
     out = outbuf;
 
     for(y = 0; y < h; y ++) {
b25514a4
         ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
1ca286e1
         if(ret == -1){
             return -1;
92964be8
         }
1ca286e1
         out+= ret;
         out_size -= ret;
92964be8
     }
 
     return out - outbuf;
 }
 
e8952fa5
 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
 {
     int i, n = bpp * w;
     uint8_t *out = outbuf;
     uint8_t *ptr = pic->data[0];
 
     for(i=0; i < h; i++) {
         memcpy(out, ptr, n);
         out += n;
         ptr += pic->linesize[0];
     }
 
     return out - outbuf;
 }
 
2c0c00cb
 static int targa_encode_frame(AVCodecContext *avctx,
                               unsigned char *outbuf,
                               int buf_size, void *data){
     AVFrame *p = data;
e23dd95d
     int bpp, picsize, datasize = -1;
e8952fa5
     uint8_t *out;
2c0c00cb
 
     if(avctx->width > 0xffff || avctx->height > 0xffff) {
         av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
e259eadc
         return AVERROR(EINVAL);
2c0c00cb
     }
92964be8
     picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
     if(buf_size < picsize + 45) {
2c0c00cb
         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
e259eadc
         return AVERROR(EINVAL);
2c0c00cb
     }
 
ce5e49b0
     p->pict_type= AV_PICTURE_TYPE_I;
2c0c00cb
     p->key_frame= 1;
 
     /* zero out the header and only set applicable fields */
d010a2d5
     memset(outbuf, 0, 12);
2c0c00cb
     AV_WL16(outbuf+12, avctx->width);
     AV_WL16(outbuf+14, avctx->height);
aec96695
     /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
     outbuf[17] = 0x20 | (avctx->pix_fmt == PIX_FMT_BGRA ? 8 : 0);
2c0c00cb
 
     switch(avctx->pix_fmt) {
     case PIX_FMT_GRAY8:
bbf874f1
         outbuf[2] = TGA_BW;      /* uncompressed grayscale image */
2c0c00cb
         outbuf[16] = 8;          /* bpp */
         break;
da5bcafe
     case PIX_FMT_RGB555LE:
bbf874f1
         outbuf[2] = TGA_RGB;     /* uncompresses true-color image */
552cf712
         outbuf[16] = 16;         /* bpp */
         break;
2c0c00cb
     case PIX_FMT_BGR24:
bbf874f1
         outbuf[2] = TGA_RGB;     /* uncompressed true-color image */
2c0c00cb
         outbuf[16] = 24;         /* bpp */
         break;
aec96695
     case PIX_FMT_BGRA:
         outbuf[2] = TGA_RGB;     /* uncompressed true-color image */
         outbuf[16] = 32;         /* bpp */
         break;
2c0c00cb
     default:
7c559bc7
         av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
94bed8e5
                av_get_pix_fmt_name(avctx->pix_fmt));
e259eadc
         return AVERROR(EINVAL);
2c0c00cb
     }
e8952fa5
     bpp = outbuf[16] >> 3;
2c0c00cb
 
     out = outbuf + 18;  /* skip past the header we just output */
 
92964be8
     /* try RLE compression */
e23dd95d
     if (avctx->coder_type != FF_CODER_TYPE_RAW)
05ec0c00
         datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
92964be8
 
     /* if that worked well, mark the picture as RLE compressed */
     if(datasize >= 0)
         outbuf[2] |= 8;
 
     /* if RLE didn't make it smaller, go back to no compression */
     else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
 
     out += datasize;
2c0c00cb
 
     /* The standard recommends including this section, even if we don't use
      * any of the features it affords. TODO: take advantage of the pixel
      * aspect ratio and encoder ID fields available? */
     memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
 
     return out + 26 - outbuf;
 }
 
98a6fff9
 static av_cold int targa_encode_init(AVCodecContext *avctx)
2c0c00cb
 {
e2d09b65
     TargaContext *s = avctx->priv_data;
 
     avcodec_get_frame_defaults(&s->picture);
     s->picture.key_frame= 1;
     avctx->coded_frame= &s->picture;
 
2c0c00cb
     return 0;
 }
 
e7e2df27
 AVCodec ff_targa_encoder = {
2c0c00cb
     .name = "targa",
72415b2a
     .type = AVMEDIA_TYPE_VIDEO,
2c0c00cb
     .id = CODEC_ID_TARGA,
e2d09b65
     .priv_data_size = sizeof(TargaContext),
2c0c00cb
     .init = targa_encode_init,
     .encode = targa_encode_frame,
aec96695
     .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
fe4bf374
     .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
2c0c00cb
 };