annotate ffmpeg/libavcodec/targaenc.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * Targa (.tga) image encoder
yading@10 3 * Copyright (c) 2007 Bobby Bingham
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21
yading@10 22 #include <string.h>
yading@10 23
yading@10 24 #include "libavutil/internal.h"
yading@10 25 #include "libavutil/intreadwrite.h"
yading@10 26 #include "libavutil/pixdesc.h"
yading@10 27 #include "avcodec.h"
yading@10 28 #include "internal.h"
yading@10 29 #include "rle.h"
yading@10 30 #include "targa.h"
yading@10 31
yading@10 32 typedef struct TargaContext {
yading@10 33 AVFrame picture;
yading@10 34 } TargaContext;
yading@10 35
yading@10 36 /**
yading@10 37 * RLE compress the image, with maximum size of out_size
yading@10 38 * @param outbuf Output buffer
yading@10 39 * @param out_size Maximum output size
yading@10 40 * @param pic Image to compress
yading@10 41 * @param bpp Bytes per pixel
yading@10 42 * @param w Image width
yading@10 43 * @param h Image height
yading@10 44 * @return Size of output in bytes, or -1 if larger than out_size
yading@10 45 */
yading@10 46 static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
yading@10 47 int bpp, int w, int h)
yading@10 48 {
yading@10 49 int y,ret;
yading@10 50 uint8_t *out;
yading@10 51
yading@10 52 out = outbuf;
yading@10 53
yading@10 54 for(y = 0; y < h; y ++) {
yading@10 55 ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
yading@10 56 if(ret == -1){
yading@10 57 return -1;
yading@10 58 }
yading@10 59 out+= ret;
yading@10 60 out_size -= ret;
yading@10 61 }
yading@10 62
yading@10 63 return out - outbuf;
yading@10 64 }
yading@10 65
yading@10 66 static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
yading@10 67 {
yading@10 68 int i, n = bpp * w;
yading@10 69 uint8_t *out = outbuf;
yading@10 70 uint8_t *ptr = pic->data[0];
yading@10 71
yading@10 72 for(i=0; i < h; i++) {
yading@10 73 memcpy(out, ptr, n);
yading@10 74 out += n;
yading@10 75 ptr += pic->linesize[0];
yading@10 76 }
yading@10 77
yading@10 78 return out - outbuf;
yading@10 79 }
yading@10 80
yading@10 81 static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
yading@10 82 const AVFrame *p, int *got_packet)
yading@10 83 {
yading@10 84 int bpp, picsize, datasize = -1, ret, i;
yading@10 85 uint8_t *out;
yading@10 86
yading@10 87 if(avctx->width > 0xffff || avctx->height > 0xffff) {
yading@10 88 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
yading@10 89 return AVERROR(EINVAL);
yading@10 90 }
yading@10 91 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
yading@10 92 if ((ret = ff_alloc_packet2(avctx, pkt, picsize + 45)) < 0)
yading@10 93 return ret;
yading@10 94
yading@10 95 /* zero out the header and only set applicable fields */
yading@10 96 memset(pkt->data, 0, 12);
yading@10 97 AV_WL16(pkt->data+12, avctx->width);
yading@10 98 AV_WL16(pkt->data+14, avctx->height);
yading@10 99 /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
yading@10 100 pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0);
yading@10 101
yading@10 102 out = pkt->data + 18; /* skip past the header we write */
yading@10 103
yading@10 104 avctx->bits_per_coded_sample = av_get_bits_per_pixel(av_pix_fmt_desc_get(avctx->pix_fmt));
yading@10 105 switch(avctx->pix_fmt) {
yading@10 106 case AV_PIX_FMT_PAL8: {
yading@10 107 int pal_bpp = 24; /* Only write 32bit palette if there is transparency information */
yading@10 108 for (i = 0; i < 256; i++)
yading@10 109 if (AV_RN32(p->data[1] + 4 * i) >> 24 != 0xFF) {
yading@10 110 pal_bpp = 32;
yading@10 111 break;
yading@10 112 }
yading@10 113 pkt->data[1] = 1; /* palette present */
yading@10 114 pkt->data[2] = TGA_PAL; /* uncompressed palettised image */
yading@10 115 pkt->data[6] = 1; /* palette contains 256 entries */
yading@10 116 pkt->data[7] = pal_bpp; /* palette contains pal_bpp bit entries */
yading@10 117 pkt->data[16] = 8; /* bpp */
yading@10 118 for (i = 0; i < 256; i++)
yading@10 119 if (pal_bpp == 32) {
yading@10 120 AV_WL32(pkt->data + 18 + 4 * i, *(uint32_t *)(p->data[1] + i * 4));
yading@10 121 } else {
yading@10 122 AV_WL24(pkt->data + 18 + 3 * i, *(uint32_t *)(p->data[1] + i * 4));
yading@10 123 }
yading@10 124 out += 32 * pal_bpp; /* skip past the palette we just output */
yading@10 125 break;
yading@10 126 }
yading@10 127 case AV_PIX_FMT_GRAY8:
yading@10 128 pkt->data[2] = TGA_BW; /* uncompressed grayscale image */
yading@10 129 avctx->bits_per_coded_sample = 0x28;
yading@10 130 pkt->data[16] = 8; /* bpp */
yading@10 131 break;
yading@10 132 case AV_PIX_FMT_RGB555LE:
yading@10 133 pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
yading@10 134 avctx->bits_per_coded_sample =
yading@10 135 pkt->data[16] = 16; /* bpp */
yading@10 136 break;
yading@10 137 case AV_PIX_FMT_BGR24:
yading@10 138 pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
yading@10 139 pkt->data[16] = 24; /* bpp */
yading@10 140 break;
yading@10 141 case AV_PIX_FMT_BGRA:
yading@10 142 pkt->data[2] = TGA_RGB; /* uncompressed true-color image */
yading@10 143 pkt->data[16] = 32; /* bpp */
yading@10 144 break;
yading@10 145 default:
yading@10 146 av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
yading@10 147 av_get_pix_fmt_name(avctx->pix_fmt));
yading@10 148 return AVERROR(EINVAL);
yading@10 149 }
yading@10 150 bpp = pkt->data[16] >> 3;
yading@10 151
yading@10 152 /* try RLE compression */
yading@10 153 if (avctx->coder_type != FF_CODER_TYPE_RAW)
yading@10 154 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
yading@10 155
yading@10 156 /* if that worked well, mark the picture as RLE compressed */
yading@10 157 if(datasize >= 0)
yading@10 158 pkt->data[2] |= TGA_RLE;
yading@10 159
yading@10 160 /* if RLE didn't make it smaller, go back to no compression */
yading@10 161 else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
yading@10 162
yading@10 163 out += datasize;
yading@10 164
yading@10 165 /* The standard recommends including this section, even if we don't use
yading@10 166 * any of the features it affords. TODO: take advantage of the pixel
yading@10 167 * aspect ratio and encoder ID fields available? */
yading@10 168 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
yading@10 169
yading@10 170 pkt->size = out + 26 - pkt->data;
yading@10 171 pkt->flags |= AV_PKT_FLAG_KEY;
yading@10 172 *got_packet = 1;
yading@10 173
yading@10 174 return 0;
yading@10 175 }
yading@10 176
yading@10 177 static av_cold int targa_encode_init(AVCodecContext *avctx)
yading@10 178 {
yading@10 179 TargaContext *s = avctx->priv_data;
yading@10 180
yading@10 181 avcodec_get_frame_defaults(&s->picture);
yading@10 182 s->picture.key_frame= 1;
yading@10 183 s->picture.pict_type = AV_PICTURE_TYPE_I;
yading@10 184 avctx->coded_frame= &s->picture;
yading@10 185
yading@10 186 return 0;
yading@10 187 }
yading@10 188
yading@10 189 AVCodec ff_targa_encoder = {
yading@10 190 .name = "targa",
yading@10 191 .type = AVMEDIA_TYPE_VIDEO,
yading@10 192 .id = AV_CODEC_ID_TARGA,
yading@10 193 .priv_data_size = sizeof(TargaContext),
yading@10 194 .init = targa_encode_init,
yading@10 195 .encode2 = targa_encode_frame,
yading@10 196 .pix_fmts = (const enum AVPixelFormat[]){
yading@10 197 AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_GRAY8, AV_PIX_FMT_PAL8,
yading@10 198 AV_PIX_FMT_NONE
yading@10 199 },
yading@10 200 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
yading@10 201 };