annotate targaenc.c @ 12530:63edd10ad4bc libavcodec tip

Try to fix crashes introduced by r25218 r25218 made assumptions about the existence of past reference frames that weren't necessarily true.
author darkshikari
date Tue, 28 Sep 2010 09:06:22 +0000
parents 8a4984c5cacc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
1 /*
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
2 * Targa (.tga) image encoder
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
3 * Copyright (c) 2007 Bobby Bingham
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
4 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
5 * This file is part of FFmpeg.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
6 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
11 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
15 * Lesser General Public License for more details.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
16 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
20 */
8573
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7040
diff changeset
21
2acf0ae7b041 Fix build: Add intreadwrite.h and bswap.h #includes where necessary.
diego
parents: 7040
diff changeset
22 #include "libavutil/intreadwrite.h"
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
23 #include "avcodec.h"
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
24 #include "rle.h"
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
25
6919
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
26 typedef struct TargaContext {
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
27 AVFrame picture;
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
28 } TargaContext;
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
29
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
30 /**
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
31 * 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
32 * @param outbuf Output buffer
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
33 * @param out_size Maximum output size
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
34 * @param pic Image to compress
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
35 * @param bpp Bytes per pixel
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
36 * @param w Image width
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
37 * @param h Image height
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
38 * @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
39 */
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
40 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
41 int bpp, int w, int h)
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
42 {
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
43 int y,ret;
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
44 uint8_t *out;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
45
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
46 out = outbuf;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
47
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
48 for(y = 0; y < h; y ++) {
4771
7cd3ffe4897f Changed the rle encoder a little and made it more universal.
michael
parents: 4767
diff changeset
49 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
50 if(ret == -1){
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
51 return -1;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
52 }
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
53 out+= ret;
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
54 out_size -= ret;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
55 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
56
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
57 return out - outbuf;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
58 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
59
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
60 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
61 {
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
62 int i, n = bpp * w;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
63 uint8_t *out = outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
64 uint8_t *ptr = pic->data[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 for(i=0; i < h; i++) {
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
67 memcpy(out, ptr, n);
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
68 out += n;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
69 ptr += pic->linesize[0];
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
70 }
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
71
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
72 return out - outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
73 }
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
74
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
75 static int targa_encode_frame(AVCodecContext *avctx,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
76 unsigned char *outbuf,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
77 int buf_size, void *data){
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
78 AVFrame *p = data;
10341
e9435ad2d61e Add support for TARGA images without RLE compression.
benoit
parents: 10146
diff changeset
79 int bpp, picsize, datasize = -1;
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
80 uint8_t *out;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
81
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
82 if(avctx->width > 0xffff || avctx->height > 0xffff) {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
83 av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
84 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
85 }
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
86 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
87 if(buf_size < picsize + 45) {
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
88 av_log(avctx, AV_LOG_ERROR, "encoded frame too large\n");
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
89 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
90 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
91
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
92 p->pict_type= FF_I_TYPE;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
93 p->key_frame= 1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
94
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
95 /* zero out the header and only set applicable fields */
6748
7fea187c80b0 Prevent targaenc.c from outputting trash byte.
ramiro
parents: 6722
diff changeset
96 memset(outbuf, 0, 12);
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
97 AV_WL16(outbuf+12, avctx->width);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
98 AV_WL16(outbuf+14, avctx->height);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
99 outbuf[17] = 0x20; /* origin is top-left. no alpha */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
100
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
101 /* TODO: support alpha channel */
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
102 switch(avctx->pix_fmt) {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
103 case PIX_FMT_GRAY8:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
104 outbuf[2] = 3; /* uncompressed grayscale image */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
105 outbuf[16] = 8; /* bpp */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
106 break;
11404
a744eb5328b2 targeenc: fix rgb555 encoding on big endian systems.
benoit
parents: 10342
diff changeset
107 case PIX_FMT_RGB555LE:
4676
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
108 outbuf[2] = 2; /* uncompresses true-color image */
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
109 outbuf[16] = 16; /* bpp */
046456cf7d31 Add 15 bit support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4673
diff changeset
110 break;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
111 case PIX_FMT_BGR24:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
112 outbuf[2] = 2; /* uncompressed true-color image */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
113 outbuf[16] = 24; /* bpp */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
114 break;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
115 default:
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
116 return -1;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
117 }
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
118 bpp = outbuf[16] >> 3;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
119
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
120 out = outbuf + 18; /* skip past the header we just output */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
121
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
122 /* try RLE compression */
10341
e9435ad2d61e Add support for TARGA images without RLE compression.
benoit
parents: 10146
diff changeset
123 if (avctx->coder_type != FF_CODER_TYPE_RAW)
10342
fc22144efcaf Fix indentation after last commit.
benoit
parents: 10341
diff changeset
124 datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, avctx->height);
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
125
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
126 /* 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
127 if(datasize >= 0)
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
128 outbuf[2] |= 8;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
129
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
130 /* 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
131 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
132
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
133 out += datasize;
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
134
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
135 /* The standard recommends including this section, even if we don't use
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
136 * any of the features it affords. TODO: take advantage of the pixel
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
137 * aspect ratio and encoder ID fields available? */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
138 memcpy(out, "\0\0\0\0\0\0\0\0TRUEVISION-XFILE.", 26);
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
139
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
140 return out + 26 - outbuf;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
141 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
142
6517
48759bfbd073 Apply 'cold' attribute to init/uninit functions in libavcodec
zuxy
parents: 5215
diff changeset
143 static av_cold int targa_encode_init(AVCodecContext *avctx)
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
144 {
6919
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
145 TargaContext *s = avctx->priv_data;
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
146
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
147 avcodec_get_frame_defaults(&s->picture);
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
148 s->picture.key_frame= 1;
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
149 avctx->coded_frame= &s->picture;
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
150
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
151 return 0;
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
152 }
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
153
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
154 AVCodec targa_encoder = {
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
155 .name = "targa",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 11404
diff changeset
156 .type = AVMEDIA_TYPE_VIDEO,
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
157 .id = CODEC_ID_TARGA,
6919
7cf90c252373 set coded_frame
michael
parents: 6788
diff changeset
158 .priv_data_size = sizeof(TargaContext),
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
159 .init = targa_encode_init,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
160 .encode = targa_encode_frame,
11404
a744eb5328b2 targeenc: fix rgb555 encoding on big endian systems.
benoit
parents: 10342
diff changeset
161 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_BGR24, PIX_FMT_RGB555LE, PIX_FMT_GRAY8, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6919
diff changeset
162 .long_name= NULL_IF_CONFIG_SMALL("Truevision Targa image"),
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
163 };