]> git.sesse.net Git - ffmpeg/blob - libavcodec/targaenc.c
Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
[ffmpeg] / libavcodec / targaenc.c
1 /*
2  * Targa (.tga) image encoder
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avcodec.h"
22 #include "rle.h"
23
24 typedef struct TargaContext {
25     AVFrame picture;
26 } TargaContext;
27
28 /**
29  * RLE compress the image, with maximum size of out_size
30  * @param outbuf Output buffer
31  * @param out_size Maximum output size
32  * @param pic Image to compress
33  * @param bpp Bytes per pixel
34  * @param w Image width
35  * @param h Image height
36  * @return Size of output in bytes, or -1 if larger than out_size
37  */
38 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
39                             int bpp, int w, int h)
40 {
41     int y,ret;
42     uint8_t *out;
43
44     out = outbuf;
45
46     for(y = 0; y < h; y ++) {
47         ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
48         if(ret == -1){
49             return -1;
50         }
51         out+= ret;
52         out_size -= ret;
53     }
54
55     return out - outbuf;
56 }
57
58 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
59 {
60     int i, n = bpp * w;
61     uint8_t *out = outbuf;
62     uint8_t *ptr = pic->data[0];
63
64     for(i=0; i < h; i++) {
65         memcpy(out, ptr, n);
66         out += n;
67         ptr += pic->linesize[0];
68     }
69
70     return out - outbuf;
71 }
72
73 static int targa_encode_frame(AVCodecContext *avctx,
74                               unsigned char *outbuf,
75                               int buf_size, void *data){
76     AVFrame *p = data;
77     int bpp, picsize, datasize;
78     uint8_t *out;
79
80     if(avctx->width > 0xffff || avctx->height > 0xffff) {
81         av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
82         return -1;
83     }
84     picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
85     if(buf_size < picsize + 45) {
86         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
87         return -1;
88     }
89
90     p->pict_type= FF_I_TYPE;
91     p->key_frame= 1;
92
93     /* zero out the header and only set applicable fields */
94     memset(outbuf, 0, 12);
95     AV_WL16(outbuf+12, avctx->width);
96     AV_WL16(outbuf+14, avctx->height);
97     outbuf[17] = 0x20;           /* origin is top-left. no alpha */
98
99     /* TODO: support alpha channel */
100     switch(avctx->pix_fmt) {
101     case PIX_FMT_GRAY8:
102         outbuf[2] = 3;           /* uncompressed grayscale image */
103         outbuf[16] = 8;          /* bpp */
104         break;
105     case PIX_FMT_RGB555:
106         outbuf[2] = 2;           /* uncompresses true-color image */
107         outbuf[16] = 16;         /* bpp */
108         break;
109     case PIX_FMT_BGR24:
110         outbuf[2] = 2;           /* uncompressed true-color image */
111         outbuf[16] = 24;         /* bpp */
112         break;
113     default:
114         return -1;
115     }
116     bpp = outbuf[16] >> 3;
117
118     out = outbuf + 18;  /* skip past the header we just output */
119
120     /* try RLE compression */
121     datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
122
123     /* if that worked well, mark the picture as RLE compressed */
124     if(datasize >= 0)
125         outbuf[2] |= 8;
126
127     /* if RLE didn't make it smaller, go back to no compression */
128     else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
129
130     out += datasize;
131
132     /* The standard recommends including this section, even if we don't use
133      * any of the features it affords. TODO: take advantage of the pixel
134      * aspect ratio and encoder ID fields available? */
135     memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
136
137     return out + 26 - outbuf;
138 }
139
140 static av_cold int targa_encode_init(AVCodecContext *avctx)
141 {
142     TargaContext *s = avctx->priv_data;
143
144     avcodec_get_frame_defaults(&s->picture);
145     s->picture.key_frame= 1;
146     avctx->coded_frame= &s->picture;
147
148     return 0;
149 }
150
151 AVCodec targa_encoder = {
152     .name = "targa",
153     .type = CODEC_TYPE_VIDEO,
154     .id = CODEC_ID_TARGA,
155     .priv_data_size = sizeof(TargaContext),
156     .init = targa_encode_init,
157     .encode = targa_encode_frame,
158     .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, PIX_FMT_NONE},
159     .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
160 };