]> git.sesse.net Git - ffmpeg/blob - libavcodec/targaenc.c
mpeg12dec: avoid signed overflow in bitrate calculation
[ffmpeg] / libavcodec / targaenc.c
1 /*
2  * Targa (.tga) image encoder
3  * Copyright (c) 2007 Bobby Bingham
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <string.h>
23
24 #include "libavutil/imgutils.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/pixdesc.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 #include "rle.h"
32 #include "targa.h"
33
34 typedef struct TargaContext {
35     AVClass *class;
36
37     int rle;
38 } TargaContext;
39
40 /**
41  * RLE compress the image, with maximum size of out_size
42  * @param outbuf Output buffer
43  * @param out_size Maximum output size
44  * @param pic Image to compress
45  * @param bpp Bytes per pixel
46  * @param w Image width
47  * @param h Image height
48  * @return Size of output in bytes, or -1 if larger than out_size
49  */
50 static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
51                             int bpp, int w, int h)
52 {
53     int y,ret;
54     uint8_t *out;
55
56     out = outbuf;
57
58     for(y = 0; y < h; y ++) {
59         ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
60         if(ret == -1){
61             return -1;
62         }
63         out+= ret;
64         out_size -= ret;
65     }
66
67     return out - outbuf;
68 }
69
70 static int targa_encode_normal(uint8_t *outbuf, const AVFrame *pic, int bpp, int w, int h)
71 {
72     int i, n = bpp * w;
73     uint8_t *out = outbuf;
74     uint8_t *ptr = pic->data[0];
75
76     for(i=0; i < h; i++) {
77         memcpy(out, ptr, n);
78         out += n;
79         ptr += pic->linesize[0];
80     }
81
82     return out - outbuf;
83 }
84
85 static int targa_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
86                               const AVFrame *p, int *got_packet)
87 {
88     TargaContext *s = avctx->priv_data;
89     int bpp, picsize, datasize = -1, ret;
90     uint8_t *out;
91
92     picsize = av_image_get_buffer_size(avctx->pix_fmt,
93                                        avctx->width, avctx->height, 1);
94     if ((ret = ff_alloc_packet(pkt, picsize + 45)) < 0) {
95         av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
96         return ret;
97     }
98
99     /* zero out the header and only set applicable fields */
100     memset(pkt->data, 0, 12);
101     AV_WL16(pkt->data+12, avctx->width);
102     AV_WL16(pkt->data+14, avctx->height);
103     /* image descriptor byte: origin is always top-left, bits 0-3 specify alpha */
104     pkt->data[17] = 0x20 | (avctx->pix_fmt == AV_PIX_FMT_BGRA ? 8 : 0);
105
106     switch(avctx->pix_fmt) {
107     case AV_PIX_FMT_GRAY8:
108         pkt->data[2]  = TGA_BW;     /* uncompressed grayscale image */
109         pkt->data[16] = 8;          /* bpp */
110         break;
111     case AV_PIX_FMT_RGB555LE:
112         pkt->data[2]  = TGA_RGB;    /* uncompresses true-color image */
113         pkt->data[16] = 16;         /* bpp */
114         break;
115     case AV_PIX_FMT_BGR24:
116         pkt->data[2]  = TGA_RGB;    /* uncompressed true-color image */
117         pkt->data[16] = 24;         /* bpp */
118         break;
119     case AV_PIX_FMT_BGRA:
120         pkt->data[2]  = TGA_RGB;    /* uncompressed true-color image */
121         pkt->data[16] = 32;         /* bpp */
122         break;
123     default:
124         av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
125                av_get_pix_fmt_name(avctx->pix_fmt));
126         return AVERROR(EINVAL);
127     }
128     bpp = pkt->data[16] >> 3;
129
130     out = pkt->data + 18;  /* skip past the header we just output */
131
132 #if FF_API_CODER_TYPE
133 FF_DISABLE_DEPRECATION_WARNINGS
134     if (avctx->coder_type == FF_CODER_TYPE_RAW)
135         s->rle = 0;
136 FF_ENABLE_DEPRECATION_WARNINGS
137 #endif
138
139     /* try RLE compression */
140     if (s->rle)
141         datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
142
143     /* if that worked well, mark the picture as RLE compressed */
144     if(datasize >= 0)
145         pkt->data[2] |= 8;
146
147     /* if RLE didn't make it smaller, go back to no compression */
148     else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
149
150     out += datasize;
151
152     /* The standard recommends including this section, even if we don't use
153      * any of the features it affords. TODO: take advantage of the pixel
154      * aspect ratio and encoder ID fields available? */
155     memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
156
157     pkt->size   = out + 26 - pkt->data;
158     pkt->flags |= AV_PKT_FLAG_KEY;
159     *got_packet = 1;
160
161     return 0;
162 }
163
164 static av_cold int targa_encode_init(AVCodecContext *avctx)
165 {
166     if (avctx->width > 0xffff || avctx->height > 0xffff) {
167         av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
168         return AVERROR(EINVAL);
169     }
170
171 #if FF_API_CODED_FRAME
172 FF_DISABLE_DEPRECATION_WARNINGS
173     avctx->coded_frame->key_frame = 1;
174     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
175 FF_ENABLE_DEPRECATION_WARNINGS
176 #endif
177
178     return 0;
179 }
180
181 #define OFFSET(x) offsetof(TargaContext, x)
182 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
183 static const AVOption options[] = {
184     { "rle", "Use run-length compression", OFFSET(rle), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
185
186     { NULL },
187 };
188
189 static const AVClass targa_class = {
190     .class_name = "targa",
191     .item_name  = av_default_item_name,
192     .option     = options,
193     .version    = LIBAVUTIL_VERSION_INT,
194 };
195
196 AVCodec ff_targa_encoder = {
197     .name           = "targa",
198     .long_name      = NULL_IF_CONFIG_SMALL("Truevision Targa image"),
199     .type           = AVMEDIA_TYPE_VIDEO,
200     .id             = AV_CODEC_ID_TARGA,
201     .priv_data_size = sizeof(TargaContext),
202     .priv_class     = &targa_class,
203     .init           = targa_encode_init,
204     .encode2        = targa_encode_frame,
205     .pix_fmts       = (const enum AVPixelFormat[]){
206         AV_PIX_FMT_BGR24, AV_PIX_FMT_BGRA, AV_PIX_FMT_RGB555LE, AV_PIX_FMT_GRAY8,
207         AV_PIX_FMT_NONE
208     },
209 };