Mercurial > libavcodec.hg
annotate targaenc.c @ 5140:9d4ca30481e0 libavcodec
Simplify init preprocessor statements.
patch by Albert Lee, trisk+xine acm.jhu edu
author | diego |
---|---|
date | Thu, 14 Jun 2007 08:44:41 +0000 |
parents | 7cd3ffe4897f |
children | 2b72f9bc4f06 |
rev | line source |
---|---|
4673 | 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 */ | |
22 #include "avcodec.h" | |
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
23 #include "rle.h" |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
24 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
25 /** |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
26 * RLE compress the image, with maximum size of out_size |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
27 * @param outbuf Output buffer |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
28 * @param out_size Maximum output size |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
29 * @param pic Image to compress |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
30 * @param bpp Bytes per pixel |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
31 * @param w Image width |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
32 * @param h Image height |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
33 * @return Size of output in bytes, or -1 if larger than out_size |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
34 */ |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
35 static int targa_encode_rle(uint8_t *outbuf, int out_size, AVFrame *pic, |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
36 int bpp, int w, int h) |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
37 { |
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
38 int y,ret; |
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
39 uint8_t *out; |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
40 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
41 out = outbuf; |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
42 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
43 for(y = 0; y < h; y ++) { |
4771
7cd3ffe4897f
Changed the rle encoder a little and made it more universal.
michael
parents:
4767
diff
changeset
|
44 ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * y, bpp, w, 0x7f, 0, -1, 0); |
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
45 if(ret == -1){ |
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
46 return -1; |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
47 } |
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
48 out+= ret; |
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
49 out_size -= ret; |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
50 } |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
51 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
52 return out - outbuf; |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
53 } |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
54 |
4677
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
55 static int targa_encode_normal(uint8_t *outbuf, AVFrame *pic, int bpp, int w, int h) |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
56 { |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
57 int i, n = bpp * w; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
58 uint8_t *out = outbuf; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
59 uint8_t *ptr = pic->data[0]; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
60 |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
61 for(i=0; i < h; i++) { |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
62 memcpy(out, ptr, n); |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
63 out += n; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
64 ptr += pic->linesize[0]; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
65 } |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
66 |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
67 return out - outbuf; |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
68 } |
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
69 |
4673 | 70 static int targa_encode_frame(AVCodecContext *avctx, |
71 unsigned char *outbuf, | |
72 int buf_size, void *data){ | |
73 AVFrame *p = data; | |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
74 int bpp, picsize, datasize; |
4677
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
75 uint8_t *out; |
4673 | 76 |
77 if(avctx->width > 0xffff || avctx->height > 0xffff) { | |
78 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n"); | |
79 return -1; | |
80 } | |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
81 picsize = avpicture_get_size(avctx->pix_fmt, avctx->width, avctx->height); |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
82 if(buf_size < picsize + 45) { |
4673 | 83 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n"); |
84 return -1; | |
85 } | |
86 | |
87 p->pict_type= FF_I_TYPE; | |
88 p->key_frame= 1; | |
89 | |
90 /* zero out the header and only set applicable fields */ | |
91 memset(outbuf, 0, 11); | |
92 AV_WL16(outbuf+12, avctx->width); | |
93 AV_WL16(outbuf+14, avctx->height); | |
94 outbuf[17] = 0x20; /* origin is top-left. no alpha */ | |
95 | |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
96 /* TODO: support alpha channel */ |
4673 | 97 switch(avctx->pix_fmt) { |
98 case PIX_FMT_GRAY8: | |
99 outbuf[2] = 3; /* uncompressed grayscale image */ | |
100 outbuf[16] = 8; /* bpp */ | |
101 break; | |
4676
046456cf7d31
Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4673
diff
changeset
|
102 case PIX_FMT_RGB555: |
046456cf7d31
Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4673
diff
changeset
|
103 outbuf[2] = 2; /* uncompresses true-color image */ |
046456cf7d31
Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4673
diff
changeset
|
104 outbuf[16] = 16; /* bpp */ |
046456cf7d31
Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4673
diff
changeset
|
105 break; |
4673 | 106 case PIX_FMT_BGR24: |
107 outbuf[2] = 2; /* uncompressed true-color image */ | |
108 outbuf[16] = 24; /* bpp */ | |
109 break; | |
110 default: | |
111 return -1; | |
112 } | |
4677
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
113 bpp = outbuf[16] >> 3; |
4673 | 114 |
115 out = outbuf + 18; /* skip past the header we just output */ | |
116 | |
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
117 /* try RLE compression */ |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
118 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height); |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
119 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
120 /* if that worked well, mark the picture as RLE compressed */ |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
121 if(datasize >= 0) |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
122 outbuf[2] |= 8; |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
123 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
124 /* if RLE didn't make it smaller, go back to no compression */ |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
125 else datasize = targa_encode_normal(out, p, bpp, avctx->width, avctx->height); |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
126 |
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
127 out += datasize; |
4673 | 128 |
129 /* The standard recommends including this section, even if we don't use | |
130 * any of the features it affords. TODO: take advantage of the pixel | |
131 * aspect ratio and encoder ID fields available? */ | |
132 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26); | |
133 | |
134 return out + 26 - outbuf; | |
135 } | |
136 | |
137 static int targa_encode_init(AVCodecContext *avctx) | |
138 { | |
139 return 0; | |
140 } | |
141 | |
142 AVCodec targa_encoder = { | |
143 .name = "targa", | |
144 .type = CODEC_TYPE_VIDEO, | |
145 .id = CODEC_ID_TARGA, | |
146 .priv_data_size = 0, | |
147 .init = targa_encode_init, | |
148 .encode = targa_encode_frame, | |
4676
046456cf7d31
Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4673
diff
changeset
|
149 .pix_fmts= (enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555, PIX_FMT_GRAY8, -1}, |
4673 | 150 }; |