ffmpeg / libavcodec / iff.c @ 005caa34
History | View | Annotate | Download (7.62 KB)
1 |
/*
|
---|---|
2 |
* IFF PBM/ILBM bitmap decoder
|
3 |
* Copyright (c) 2010 Peter Ross <pross@xvid.org>
|
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 |
/**
|
23 |
* @file libavcodec/iff.c
|
24 |
* IFF PBM/ILBM bitmap decoder
|
25 |
*/
|
26 |
|
27 |
#include "bytestream.h" |
28 |
#include "avcodec.h" |
29 |
#include "get_bits.h" |
30 |
|
31 |
typedef struct { |
32 |
AVFrame frame; |
33 |
int planesize;
|
34 |
uint8_t * planebuf; |
35 |
} IffContext; |
36 |
|
37 |
/**
|
38 |
* Convert CMAP buffer (stored in extradata) to lavc palette format
|
39 |
*/
|
40 |
int ff_cmap_read_palette(AVCodecContext *avctx, uint32_t *pal)
|
41 |
{ |
42 |
int count, i;
|
43 |
|
44 |
if (avctx->bits_per_coded_sample > 8) { |
45 |
av_log(avctx, AV_LOG_ERROR, "bit_per_coded_sample > 8 not supported\n");
|
46 |
return AVERROR_INVALIDDATA;
|
47 |
} |
48 |
|
49 |
count = 1 << avctx->bits_per_coded_sample;
|
50 |
if (avctx->extradata_size < count * 3) { |
51 |
av_log(avctx, AV_LOG_ERROR, "palette data underflow\n");
|
52 |
return AVERROR_INVALIDDATA;
|
53 |
} |
54 |
for (i=0; i < count; i++) { |
55 |
pal[i] = 0xFF000000 | AV_RB24( avctx->extradata + i*3 ); |
56 |
} |
57 |
return 0; |
58 |
} |
59 |
|
60 |
static av_cold int decode_init(AVCodecContext *avctx) |
61 |
{ |
62 |
IffContext *s = avctx->priv_data; |
63 |
|
64 |
if (avctx->bits_per_coded_sample <= 8) { |
65 |
avctx->pix_fmt = PIX_FMT_PAL8; |
66 |
} else if (avctx->bits_per_coded_sample <= 32) { |
67 |
avctx->pix_fmt = PIX_FMT_BGR32; |
68 |
} else {
|
69 |
return AVERROR_INVALIDDATA;
|
70 |
} |
71 |
|
72 |
s->planesize = avctx->width / 8;
|
73 |
s->planebuf = av_malloc(s->planesize + FF_INPUT_BUFFER_PADDING_SIZE); |
74 |
if (!s->planebuf)
|
75 |
return AVERROR(ENOMEM);
|
76 |
|
77 |
s->frame.reference = 1;
|
78 |
if (avctx->get_buffer(avctx, &s->frame) < 0) { |
79 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
80 |
return AVERROR_UNKNOWN;
|
81 |
} |
82 |
|
83 |
return avctx->bits_per_coded_sample <= 8 ? |
84 |
ff_cmap_read_palette(avctx, (uint32_t*)s->frame.data[1]) : 0; |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Decode interleaved plane buffer
|
89 |
* @param dst Destination buffer
|
90 |
* @param buf Source buffer
|
91 |
* @param buf_size
|
92 |
* @param bps bits_per_coded_sample
|
93 |
* @param plane plane number to decode as
|
94 |
*/
|
95 |
#define DECLARE_DECODEPLANE(suffix, type) \
|
96 |
static void decodeplane##suffix(void *dst, const uint8_t const *buf, int buf_size, int bps, int plane) \ |
97 |
{ \ |
98 |
GetBitContext gb; \ |
99 |
int i, b; \
|
100 |
init_get_bits(&gb, buf, buf_size * 8); \
|
101 |
for(i = 0; i < (buf_size * 8 + bps - 1) / bps; i++) { \ |
102 |
for (b = 0; b < bps; b++) { \ |
103 |
((type *)dst)[ i*bps + b ] |= get_bits1(&gb) << plane; \ |
104 |
} \ |
105 |
} \ |
106 |
} |
107 |
DECLARE_DECODEPLANE(8, uint8_t)
|
108 |
DECLARE_DECODEPLANE(32, uint32_t)
|
109 |
|
110 |
static int decode_frame_ilbm(AVCodecContext *avctx, |
111 |
void *data, int *data_size, |
112 |
AVPacket *avpkt) |
113 |
{ |
114 |
IffContext *s = avctx->priv_data; |
115 |
const uint8_t *buf = avpkt->data;
|
116 |
int buf_size = avpkt->size;
|
117 |
int y, plane;
|
118 |
|
119 |
if (avctx->reget_buffer(avctx, &s->frame) < 0){ |
120 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
121 |
return -1; |
122 |
} |
123 |
|
124 |
if (buf_size < avctx->width * avctx->height) {
|
125 |
av_log(avctx, AV_LOG_ERROR, "buffer underflow\n");
|
126 |
return -1; |
127 |
} |
128 |
|
129 |
for(y = 0; y < avctx->height; y++ ) { |
130 |
uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ]; |
131 |
memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4)); |
132 |
for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) { |
133 |
if (avctx->pix_fmt == PIX_FMT_PAL8) {
|
134 |
decodeplane8(row, buf, s->planesize, avctx->bits_per_coded_sample, plane); |
135 |
} else { // PIX_FMT_BGR32 |
136 |
decodeplane32(row, buf, s->planesize, avctx->bits_per_coded_sample, plane); |
137 |
} |
138 |
buf += s->planesize; |
139 |
} |
140 |
} |
141 |
|
142 |
*data_size = sizeof(AVFrame);
|
143 |
*(AVFrame*)data = s->frame; |
144 |
return buf_size;
|
145 |
} |
146 |
|
147 |
static int decode_frame_byterun1(AVCodecContext *avctx, |
148 |
void *data, int *data_size, |
149 |
AVPacket *avpkt) |
150 |
{ |
151 |
IffContext *s = avctx->priv_data; |
152 |
const uint8_t *buf = avpkt->data;
|
153 |
int buf_size = avpkt->size;
|
154 |
const uint8_t *buf_end = buf+buf_size;
|
155 |
int y, plane, x;
|
156 |
|
157 |
if (avctx->reget_buffer(avctx, &s->frame) < 0){ |
158 |
av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
|
159 |
return -1; |
160 |
} |
161 |
|
162 |
for(y = 0; y < avctx->height ; y++ ) { |
163 |
uint8_t *row = &s->frame.data[0][ y*s->frame.linesize[0] ]; |
164 |
if (avctx->codec_tag == MKTAG('I','L','B','M')) { //interleaved |
165 |
memset(row, 0, avctx->pix_fmt == PIX_FMT_PAL8 ? avctx->width : (avctx->width * 4)); |
166 |
for (plane = 0; plane < avctx->bits_per_coded_sample; plane++) { |
167 |
for(x = 0; x < s->planesize && buf < buf_end; ) { |
168 |
int8_t value = *buf++; |
169 |
int length;
|
170 |
if (value >= 0) { |
171 |
length = value + 1;
|
172 |
memcpy(s->planebuf + x, buf, FFMIN3(length, s->planesize - x, buf_end - buf)); |
173 |
buf += length; |
174 |
} else if (value > -128) { |
175 |
length = -value + 1;
|
176 |
memset(s->planebuf + x, *buf++, FFMIN(length, s->planesize - x)); |
177 |
} else { //noop |
178 |
continue;
|
179 |
} |
180 |
x += length; |
181 |
} |
182 |
if (avctx->pix_fmt == PIX_FMT_PAL8) {
|
183 |
decodeplane8(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane); |
184 |
} else { //PIX_FMT_BGR32 |
185 |
decodeplane32(row, s->planebuf, s->planesize, avctx->bits_per_coded_sample, plane); |
186 |
} |
187 |
} |
188 |
} else {
|
189 |
for(x = 0; x < avctx->width && buf < buf_end; ) { |
190 |
int8_t value = *buf++; |
191 |
int length;
|
192 |
if (value >= 0) { |
193 |
length = value + 1;
|
194 |
memcpy(row + x, buf, FFMIN3(length, buf_end - buf, avctx->width - x)); |
195 |
buf += length; |
196 |
} else if (value > -128) { |
197 |
length = -value + 1;
|
198 |
memset(row + x, *buf++, FFMIN(length, avctx->width - x)); |
199 |
} else { //noop |
200 |
continue;
|
201 |
} |
202 |
x += length; |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
*data_size = sizeof(AVFrame);
|
208 |
*(AVFrame*)data = s->frame; |
209 |
return buf_size;
|
210 |
} |
211 |
|
212 |
static av_cold int decode_end(AVCodecContext *avctx) |
213 |
{ |
214 |
IffContext *s = avctx->priv_data; |
215 |
if (s->frame.data[0]) |
216 |
avctx->release_buffer(avctx, &s->frame); |
217 |
av_freep(&s->planebuf); |
218 |
return 0; |
219 |
} |
220 |
|
221 |
AVCodec iff_ilbm_decoder = { |
222 |
"iff_ilbm",
|
223 |
CODEC_TYPE_VIDEO, |
224 |
CODEC_ID_IFF_ILBM, |
225 |
sizeof(IffContext),
|
226 |
decode_init, |
227 |
NULL,
|
228 |
decode_end, |
229 |
decode_frame_ilbm, |
230 |
CODEC_CAP_DR1, |
231 |
.long_name = NULL_IF_CONFIG_SMALL("IFF ILBM"),
|
232 |
}; |
233 |
|
234 |
AVCodec iff_byterun1_decoder = { |
235 |
"iff_byterun1",
|
236 |
CODEC_TYPE_VIDEO, |
237 |
CODEC_ID_IFF_BYTERUN1, |
238 |
sizeof(IffContext),
|
239 |
decode_init, |
240 |
NULL,
|
241 |
decode_end, |
242 |
decode_frame_byterun1, |
243 |
CODEC_CAP_DR1, |
244 |
.long_name = NULL_IF_CONFIG_SMALL("IFF ByteRun1"),
|
245 |
}; |