annotate sgienc.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
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
1 /*
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
2 * SGI image encoder
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
3 * Todd Kirby <doubleshot@pacbell.net>
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
4 *
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
5 * This file is part of FFmpeg.
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
6 *
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
11 *
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
15 * Lesser General Public License for more details.
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
16 *
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
20 */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
21
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
22 #include "avcodec.h"
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
23 #include "bytestream.h"
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
24 #include "sgi.h"
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
25 #include "rle.h"
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
26
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
27 #define SGI_SINGLE_CHAN 2
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
28 #define SGI_MULTI_CHAN 3
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
29
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
30 typedef struct SgiContext {
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
31 AVFrame picture;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
32 } SgiContext;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
33
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
34 static av_cold int encode_init(AVCodecContext *avctx)
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
35 {
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
36 SgiContext *s = avctx->priv_data;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
37
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
38 avcodec_get_frame_defaults(&s->picture);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
39 avctx->coded_frame = &s->picture;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
40
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
41 return 0;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
42 }
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
43
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
44 static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
45 int buf_size, void *data)
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
46 {
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
47 SgiContext *s = avctx->priv_data;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
48 AVFrame * const p = &s->picture;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
49 uint8_t *offsettab, *lengthtab, *in_buf, *encode_buf;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
50 int x, y, z, length, tablesize;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
51 unsigned int width, height, depth, dimension;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
52 unsigned char *orig_buf = buf, *end_buf = buf + buf_size;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
53
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
54 *p = *(AVFrame*)data;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
55 p->pict_type = FF_I_TYPE;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
56 p->key_frame = 1;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
57
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
58 width = avctx->width;
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
59 height = avctx->height;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
60
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
61 switch (avctx->pix_fmt) {
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
62 case PIX_FMT_GRAY8:
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
63 dimension = SGI_SINGLE_CHAN;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
64 depth = SGI_GRAYSCALE;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
65 break;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
66 case PIX_FMT_RGB24:
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
67 dimension = SGI_MULTI_CHAN;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
68 depth = SGI_RGB;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
69 break;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
70 case PIX_FMT_RGBA:
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
71 dimension = SGI_MULTI_CHAN;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
72 depth = SGI_RGBA;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
73 break;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
74 default:
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
75 return AVERROR_INVALIDDATA;
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
76 }
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
77
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
78 tablesize = depth * height * 4;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
79 length = tablesize * 2 + SGI_HEADER_SIZE;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
80
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
81 if (buf_size < length) {
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
82 av_log(avctx, AV_LOG_ERROR, "buf_size too small(need %d, got %d)\n", length, buf_size);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
83 return -1;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
84 }
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
85
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
86 /* Encode header. */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
87 bytestream_put_be16(&buf, SGI_MAGIC);
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
88 bytestream_put_byte(&buf, avctx->coder_type != FF_CODER_TYPE_RAW); /* RLE 1 - VERBATIM 0*/
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
89 bytestream_put_byte(&buf, 1); /* bytes_per_channel */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
90 bytestream_put_be16(&buf, dimension);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
91 bytestream_put_be16(&buf, width);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
92 bytestream_put_be16(&buf, height);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
93 bytestream_put_be16(&buf, depth);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
94
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
95 /* The rest are constant in this implementation. */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
96 bytestream_put_be32(&buf, 0L); /* pixmin */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
97 bytestream_put_be32(&buf, 255L); /* pixmax */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
98 bytestream_put_be32(&buf, 0L); /* dummy */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
99
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
100 /* name */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
101 memset(buf, 0, SGI_HEADER_SIZE);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
102 buf += 80;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
103
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
104 /* colormap */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
105 bytestream_put_be32(&buf, 0L);
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
106
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
107 /* The rest of the 512 byte header is unused. */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
108 buf += 404;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
109 offsettab = buf;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
110
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
111 if (avctx->coder_type != FF_CODER_TYPE_RAW) {
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
112 /* Skip RLE offset table. */
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
113 buf += tablesize;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
114 lengthtab = buf;
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
115
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
116 /* Skip RLE length table. */
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
117 buf += tablesize;
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
118
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
119 /* Make an intermediate consecutive buffer. */
10339
a352df49a10f Use "!exp" instead of "exp == NULL" in if condition.
diego
parents: 10338
diff changeset
120 if (!(encode_buf = av_malloc(width)))
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
121 return -1;
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
122
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
123 for (z = 0; z < depth; z++) {
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
124 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
125
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
126 for (y = 0; y < height; y++) {
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
127 bytestream_put_be32(&offsettab, buf - orig_buf);
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
128
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
129 for (x = 0; x < width; x++)
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
130 encode_buf[x] = in_buf[depth * x];
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
131
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
132 if ((length = ff_rle_encode(buf, end_buf - buf - 1, encode_buf, 1, width, 0, 0, 0x80, 0)) < 1) {
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
133 av_free(encode_buf);
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
134 return -1;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
135 }
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
136
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
137 buf += length;
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
138 bytestream_put_byte(&buf, 0);
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
139 bytestream_put_be32(&lengthtab, length + 1);
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
140 in_buf -= p->linesize[0];
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
141 }
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
142 }
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
143
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
144 av_free(encode_buf);
10340
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
145 } else {
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
146 for (z = 0; z < depth; z++) {
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
147 in_buf = p->data[0] + p->linesize[0] * (height - 1) + z;
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
148
10340
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
149 for (y = 0; y < height; y++) {
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
150 for (x = 0; x < width * depth; x += depth)
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
151 bytestream_put_byte(&buf, in_buf[x]);
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
152
10340
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
153 in_buf -= p->linesize[0];
10338
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
154 }
118f18919f4b cosmetics: K&R coding style, prettyprinting
diego
parents: 10330
diff changeset
155 }
10340
5f263438d11b Fix wrongly indented block.
diego
parents: 10339
diff changeset
156 }
10330
ac0b95400577 Add support for SGI images without RLE compression.
diego
parents: 10146
diff changeset
157
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
158 /* total length */
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
159 return buf - orig_buf;
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
160 }
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
161
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
162 AVCodec sgi_encoder = {
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
163 "sgi",
11560
8a4984c5cacc Define AVMediaType enum, and use it instead of enum CodecType, which
stefano
parents: 10340
diff changeset
164 AVMEDIA_TYPE_VIDEO,
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
165 CODEC_ID_SGI,
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
166 sizeof(SgiContext),
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
167 encode_init,
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
168 encode_frame,
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
169 NULL,
10146
38cfe222e1a4 Mark all pix_fmts and supported_framerates compound literals as const.
reimar
parents: 9624
diff changeset
170 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_GRAY8, PIX_FMT_NONE},
7040
e943e1409077 Make AVCodec long_names definition conditional depending on CONFIG_SMALL.
stefano
parents: 6788
diff changeset
171 .long_name= NULL_IF_CONFIG_SMALL("SGI image"),
4790
2b825cb391f2 SGI image decoder ported to the new image API.
diego
parents:
diff changeset
172 };