annotate sunrast.c @ 6137:5ef885b008c0 libavcodec

Clear synth_buf on flush(). Part of a patch by Brian Brice bbrice gmail com.
author michael
date Fri, 11 Jan 2008 18:20:42 +0000
parents e458d5374a03
children 5b6f78cd3d50
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6084
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
1 /*
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
3 * Copyright (c) 2007, 2008 Ivo van Poorten
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
4 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
5 * This file is part of FFmpeg.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
6 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
11 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
15 * Lesser General Public License for more details.
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
16 *
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
20 */
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
21
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
22 #include "avcodec.h"
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
23
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
24 #define RT_OLD 0
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
25 #define RT_STANDARD 1
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
26 #define RT_BYTE_ENCODED 2
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
27 #define RT_FORMAT_RGB 3
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
28 #define RT_FORMAT_TIFF 4
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
29 #define RT_FORMAT_IFF 5
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
30
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
31 typedef struct SUNRASTContext {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
32 AVFrame picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
33 } SUNRASTContext;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
34
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
35 static int sunrast_init(AVCodecContext *avctx) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
36 SUNRASTContext *s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
37
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
38 avcodec_get_frame_defaults(&s->picture);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
39 avctx->coded_frame= &s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
40
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
41 return 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
42 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
43
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
44 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
45 int *data_size, uint8_t *buf, int buf_size) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
46 SUNRASTContext * const s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
47 AVFrame *picture = data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
48 AVFrame * const p = &s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
49 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
50 uint8_t *ptr, *bufstart = buf;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
51
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
52 if (AV_RB32(buf) != 0x59a66a95) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
53 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
54 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
55 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
56
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
57 w = AV_RB32(buf+4);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
58 h = AV_RB32(buf+8);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
59 depth = AV_RB32(buf+12);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
60 type = AV_RB32(buf+20);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
61 maptype = AV_RB32(buf+24);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
62 maplength = AV_RB32(buf+28);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
63
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
64 if (type > RT_BYTE_ENCODED && type <= RT_FORMAT_IFF) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
65 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
66 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
67 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
68 if (type > RT_FORMAT_IFF) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
69 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
70 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
71 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
72 if (maptype & ~1) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
73 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
74 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
75 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
76
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
77 buf += 32;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
78
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
79 switch (depth) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
80 case 1:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
81 avctx->pix_fmt = PIX_FMT_MONOWHITE;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
82 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
83 case 8:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
84 avctx->pix_fmt = PIX_FMT_PAL8;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
85 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
86 case 24:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
87 avctx->pix_fmt = PIX_FMT_BGR24;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
88 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
89 default:
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
90 av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
91 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
92 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
93
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
94 if (p->data[0])
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
95 avctx->release_buffer(avctx, p);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
96
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
97 if (avcodec_check_dimensions(avctx, w, h))
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
98 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
99 if (w != avctx->width || h != avctx->height)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
100 avcodec_set_dimensions(avctx, w, h);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
101 if (avctx->get_buffer(avctx, p) < 0) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
102 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
103 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
104 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
105
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
106 p->pict_type = FF_I_TYPE;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
107
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
108 if (depth != 8 && maplength) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
109 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
110
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
111 } else if (depth == 8) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
112 unsigned int len = maplength / 3;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
113
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
114 if (!maplength) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
115 av_log(avctx, AV_LOG_ERROR, "colormap expected\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
116 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
117 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
118 if (maplength % 3 || maplength > 768) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
119 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
120 return -1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
121 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
122
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
123 ptr = p->data[1];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
124 for (x=0; x<len; x++, ptr+=4)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
125 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
126 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
127
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
128 buf += maplength;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
129
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
130 ptr = p->data[0];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
131 stride = p->linesize[0];
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
132
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
133 /* scanlines are aligned on 16 bit boundaries */
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
134 len = (depth * w + 7) >> 3;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
135 alen = len + (len&1);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
136
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
137 if (type == RT_BYTE_ENCODED) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
138 int value, run;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
139 uint8_t *end = ptr + h*stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
140
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
141 x = 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
142 while (ptr != end) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
143 run = 1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
144 if ((value = *buf++) == 0x80) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
145 run = *buf++ + 1;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
146 if (run != 1)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
147 value = *buf++;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
148 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
149 while (run--) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
150 if (x < len)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
151 ptr[x] = value;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
152 if (++x >= alen) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
153 x = 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
154 ptr += stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
155 if (ptr == end)
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
156 break;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
157 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
158 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
159 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
160 } else {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
161 for (y=0; y<h; y++) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
162 memcpy(ptr, buf, len);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
163 ptr += stride;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
164 buf += alen;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
165 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
166 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
167
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
168 *picture = s->picture;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
169 *data_size = sizeof(AVFrame);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
170
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
171 return buf - bufstart;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
172 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
173
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
174 static int sunrast_end(AVCodecContext *avctx) {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
175 SUNRASTContext *s = avctx->priv_data;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
176
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
177 if(s->picture.data[0])
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
178 avctx->release_buffer(avctx, &s->picture);
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
179
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
180 return 0;
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
181 }
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
182
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
183 AVCodec sunrast_decoder = {
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
184 "sunrast",
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
185 CODEC_TYPE_VIDEO,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
186 CODEC_ID_SUNRAST,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
187 sizeof(SUNRASTContext),
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
188 sunrast_init,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
189 NULL,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
190 sunrast_end,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
191 sunrast_decode_frame,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
192 0,
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
193 NULL
d33520d1ca92 Sun Rasterfile decoder
ivo
parents:
diff changeset
194 };