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 |
|
1d9c2dc8 |
#include <string.h>
#include "libavutil/internal.h" |
6a5d31ac |
#include "libavutil/intreadwrite.h" |
94bed8e5 |
#include "libavutil/pixdesc.h" |
2c0c00cb |
#include "avcodec.h" |
61eaf45c |
#include "internal.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
*/ |
61eaf45c |
static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic, |
92964be8 |
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;
}
|
61eaf45c |
static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h) |
e8952fa5 |
{
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;
}
|
61eaf45c |
static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
const AVFrame *p, int *got_packet)
{ |
541bfba8 |
int bpp, picsize, datasize = -1, ret, i; |
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); |
ae2c33b0 |
if ((ret = ff_alloc_packet2(avctx, pkt, picsize + 45)) < 0) |
61eaf45c |
return ret; |
2c0c00cb |
/* zero out the header and only set applicable fields */ |
61eaf45c |
memset(pkt->data, 0, 12);
AV_WL16(pkt->data+12, avctx->width);
AV_WL16(pkt->data+14, avctx->height); |
aec96695 |
/* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */ |
716d413c |
pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0); |
2c0c00cb |
|
541bfba8 |
out = pkt->data + 18; /* skip past the header we write */
|
a51540d8 |
avctx->bits_per_coded_sample = av_get_bits_per_pixel(av_pix_fmt_desc_get(avctx->pix_fmt)); |
2c0c00cb |
switch(avctx->pix_fmt) { |
61a9f099 |
case AV_PIX_FMT_PAL8: {
int pal_bpp = 24; /* Only write 32bit palette if there is transparency information */
for (i = 0; i < 256; i++)
if (AV_RN32(p->data[1] + 4 * i) >> 24 != 0xFF) {
pal_bpp = 32;
break;
} |
541bfba8 |
pkt->data[1] = 1; /* palette present */
pkt->data[2] = TGA_PAL; /* uncompressed palettised image */
pkt->data[6] = 1; /* palette contains 256 entries */ |
61a9f099 |
pkt->data[7] = pal_bpp; /* palette contains pal_bpp bit entries */ |
541bfba8 |
pkt->data[16] = 8; /* bpp */
for (i = 0; i < 256; i++) |
61a9f099 |
if (pal_bpp == 32) {
AV_WL32(pkt->data + 18 + 4 * i, *(uint32_t *)(p->data[1] + i * 4));
} else { |
541bfba8 |
AV_WL24(pkt->data + 18 + 3 * i, *(uint32_t *)(p->data[1] + i * 4)); |
61a9f099 |
}
out += 32 * pal_bpp; /* skip past the palette we just output */ |
541bfba8 |
break; |
61a9f099 |
} |
716d413c |
case AV_PIX_FMT_GRAY8: |
61eaf45c |
pkt->data[2] = TGA_BW; /* uncompressed grayscale image */ |
ab75ad01 |
avctx->bits_per_coded_sample = 0x28; |
61eaf45c |
pkt->data[16] = 8; /* bpp */ |
2c0c00cb |
break; |
716d413c |
case AV_PIX_FMT_RGB555LE: |
8e590625 |
pkt->data[2] = TGA_RGB; /* uncompressed true-color image */ |
ab75ad01 |
avctx->bits_per_coded_sample = |
61eaf45c |
pkt->data[16] = 16; /* bpp */ |
552cf712 |
break; |
716d413c |
case AV_PIX_FMT_BGR24: |
61eaf45c |
pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
pkt->data[16] = 24; /* bpp */ |
2c0c00cb |
break; |
716d413c |
case AV_PIX_FMT_BGRA: |
61eaf45c |
pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
pkt->data[16] = 32; /* bpp */ |
aec96695 |
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 |
} |
61eaf45c |
bpp = pkt->data[16] >> 3; |
2c0c00cb |
|
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) |
4aade44c |
pkt->data[2] |= TGA_RLE; |
92964be8 |
/* 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);
|
61eaf45c |
pkt->size = out + 26 - pkt->data;
pkt->flags |= AV_PKT_FLAG_KEY;
*got_packet = 1;
return 0; |
2c0c00cb |
}
|
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; |
1e742ea3 |
s->picture.pict_type = AV_PICTURE_TYPE_I; |
e2d09b65 |
avctx->coded_frame= &s->picture;
|
2c0c00cb |
return 0;
}
|
e7e2df27 |
AVCodec ff_targa_encoder = { |
00c3b67b |
.name = "targa",
.type = AVMEDIA_TYPE_VIDEO, |
36ef5369 |
.id = AV_CODEC_ID_TARGA, |
e2d09b65 |
.priv_data_size = sizeof(TargaContext), |
00c3b67b |
.init = targa_encode_init,
.encode2 = targa_encode_frame, |
716d413c |
.pix_fmts = (const enum AVPixelFormat[]){ |
ac627b3d |
AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8, |
716d413c |
AV_PIX_FMT_NONE |
00c3b67b |
}, |
fe4bf374 |
.long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"), |
2c0c00cb |
}; |