Statistics
| Branch: | Revision:

ffmpeg / libavcodec / targaenc.c @ e259eadc

History | View | Annotate | Download (5.01 KB)

1 2c0c00cb Bobby Bingham
/*
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 6a5d31ac Diego Biurrun
22
#include "libavutil/intreadwrite.h"
23 2c0c00cb Bobby Bingham
#include "avcodec.h"
24 1ca286e1 Michael Niedermayer
#include "rle.h"
25 92964be8 Bobby Bingham
26 e2d09b65 Michael Niedermayer
typedef struct TargaContext {
27
    AVFrame picture;
28
} TargaContext;
29
30 92964be8 Bobby Bingham
/**
31
 * RLE compress the image, with maximum size of out_size
32
 * @param outbuf Output buffer
33
 * @param out_size Maximum output size
34
 * @param pic Image to compress
35
 * @param bpp Bytes per pixel
36
 * @param w Image width
37
 * @param h Image height
38
 * @return Size of output in bytes, or -1 if larger than out_size
39
 */
40
static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic,
41
                            int bpp, int w, int h)
42
{
43 1ca286e1 Michael Niedermayer
    int y,ret;
44
    uint8_t *out;
45 92964be8 Bobby Bingham
46
    out = outbuf;
47
48
    for(y = 0; y < h; y ++) {
49 b25514a4 Xiaohui Sun
        ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0);
50 1ca286e1 Michael Niedermayer
        if(ret == -1){
51
            return -1;
52 92964be8 Bobby Bingham
        }
53 1ca286e1 Michael Niedermayer
        out+= ret;
54
        out_size -= ret;
55 92964be8 Bobby Bingham
    }
56
57
    return out - outbuf;
58
}
59
60 e8952fa5 Bobby Bingham
static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h)
61
{
62
    int i, n = bpp * w;
63
    uint8_t *out = outbuf;
64
    uint8_t *ptr = pic->data[0];
65
66
    for(i=0; i < h; i++) {
67
        memcpy(out, ptr, n);
68
        out += n;
69
        ptr += pic->linesize[0];
70
    }
71
72
    return out - outbuf;
73
}
74
75 2c0c00cb Bobby Bingham
static int targa_encode_frame(AVCodecContext *avctx,
76
                              unsigned char *outbuf,
77
                              int buf_size, void *data){
78
    AVFrame *p = data;
79 e23dd95d Anne-Laure de Smit
    int bpp, picsize, datasize = -1;
80 e8952fa5 Bobby Bingham
    uint8_t *out;
81 2c0c00cb Bobby Bingham
82
    if(avctx->width > 0xffff || avctx->height > 0xffff) {
83
        av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
84 e259eadc Stefano Sabatini
        return AVERROR(EINVAL);
85 2c0c00cb Bobby Bingham
    }
86 92964be8 Bobby Bingham
    picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height);
87
    if(buf_size < picsize + 45) {
88 2c0c00cb Bobby Bingham
        av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
89 e259eadc Stefano Sabatini
        return AVERROR(EINVAL);
90 2c0c00cb Bobby Bingham
    }
91
92
    p->pict_type= FF_I_TYPE;
93
    p->key_frame= 1;
94
95
    /* zero out the header and only set applicable fields */
96 d010a2d5 Ramiro Polla
    memset(outbuf, 0, 12);
97 2c0c00cb Bobby Bingham
    AV_WL16(outbuf+12, avctx->width);
98
    AV_WL16(outbuf+14, avctx->height);
99
    outbuf[17] = 0x20;           /* origin is top-left. no alpha */
100
101 92964be8 Bobby Bingham
    /* TODO: support alpha channel */
102 2c0c00cb Bobby Bingham
    switch(avctx->pix_fmt) {
103
    case PIX_FMT_GRAY8:
104
        outbuf[2] = 3;           /* uncompressed grayscale image */
105
        outbuf[16] = 8;          /* bpp */
106
        break;
107 da5bcafe Alexis Ballier
    case PIX_FMT_RGB555LE:
108 552cf712 Bobby Bingham
        outbuf[2] = 2;           /* uncompresses true-color image */
109
        outbuf[16] = 16;         /* bpp */
110
        break;
111 2c0c00cb Bobby Bingham
    case PIX_FMT_BGR24:
112
        outbuf[2] = 2;           /* uncompressed true-color image */
113
        outbuf[16] = 24;         /* bpp */
114
        break;
115
    default:
116 7c559bc7 Stefano Sabatini
        av_log(avctx, AV_LOG_ERROR, "Pixel format '%s' not supported.\n",
117
               avcodec_get_pix_fmt_name(avctx->pix_fmt));
118 e259eadc Stefano Sabatini
        return AVERROR(EINVAL);
119 2c0c00cb Bobby Bingham
    }
120 e8952fa5 Bobby Bingham
    bpp = outbuf[16] >> 3;
121 2c0c00cb Bobby Bingham
122
    out = outbuf + 18;  /* skip past the header we just output */
123
124 92964be8 Bobby Bingham
    /* try RLE compression */
125 e23dd95d Anne-Laure de Smit
    if (avctx->coder_type != FF_CODER_TYPE_RAW)
126 05ec0c00 Benoit Fouet
        datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
127 92964be8 Bobby Bingham
128
    /* if that worked well, mark the picture as RLE compressed */
129
    if(datasize >= 0)
130
        outbuf[2] |= 8;
131
132
    /* if RLE didn't make it smaller, go back to no compression */
133
    else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height);
134
135
    out += datasize;
136 2c0c00cb Bobby Bingham
137
    /* The standard recommends including this section, even if we don't use
138
     * any of the features it affords. TODO: take advantage of the pixel
139
     * aspect ratio and encoder ID fields available? */
140
    memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
141
142
    return out + 26 - outbuf;
143
}
144
145 98a6fff9 Zuxy Meng
static av_cold int targa_encode_init(AVCodecContext *avctx)
146 2c0c00cb Bobby Bingham
{
147 e2d09b65 Michael Niedermayer
    TargaContext *s = avctx->priv_data;
148
149
    avcodec_get_frame_defaults(&s->picture);
150
    s->picture.key_frame= 1;
151
    avctx->coded_frame= &s->picture;
152
153 2c0c00cb Bobby Bingham
    return 0;
154
}
155
156
AVCodec targa_encoder = {
157
    .name = "targa",
158 72415b2a Stefano Sabatini
    .type = AVMEDIA_TYPE_VIDEO,
159 2c0c00cb Bobby Bingham
    .id = CODEC_ID_TARGA,
160 e2d09b65 Michael Niedermayer
    .priv_data_size = sizeof(TargaContext),
161 2c0c00cb Bobby Bingham
    .init = targa_encode_init,
162
    .encode = targa_encode_frame,
163 da5bcafe Alexis Ballier
    .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
164 fe4bf374 Stefano Sabatini
    .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
165 2c0c00cb Bobby Bingham
};