2 * IFF PBM/ILBM bitmap decoder
3 * Copyright (c) 2010 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
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.
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.
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
23 * @file libavcodec/iff.c
24 * IFF PBM/ILBM bitmap decoder
27 #include "bytestream.h"
31 * Convert CMAP buffer (stored in extradata) to lavc palette format
33 int ff_cmap_read_palette(AVCodecContext
*avctx
, uint32_t *pal
)
37 if (avctx
->bits_per_coded_sample
> 8) {
38 av_log(avctx
, AV_LOG_ERROR
, "bit_per_coded_sample > 8 not supported\n");
39 return AVERROR_INVALIDDATA
;
42 count
= 1 << avctx
->bits_per_coded_sample
;
43 if (avctx
->extradata_size
< count
* 3) {
44 av_log(avctx
, AV_LOG_ERROR
, "palette data underflow\n");
45 return AVERROR_INVALIDDATA
;
47 for (i
=0; i
< count
; i
++) {
48 pal
[i
] = AV_RB24( avctx
->extradata
+ i
*3 );
53 static av_cold
int decode_init(AVCodecContext
*avctx
)
55 AVFrame
*frame
= avctx
->priv_data
;
57 avctx
->pix_fmt
= PIX_FMT_PAL8
;
60 if (avctx
->get_buffer(avctx
, frame
) < 0) {
61 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
62 return AVERROR_UNKNOWN
;
64 return ff_cmap_read_palette(avctx
, (uint32_t*)frame
->data
[1]);
70 static void imemcpy(uint8_t *dst
, const uint8_t const *buf
, int x
, int bps
, int plane
, int length
)
73 for(i
= 0; i
< length
; i
++) {
75 for (b
= 0; b
< bps
; b
++) {
77 dst
[ (x
+i
)*bps
+ 7 - b
] |= 1<<plane
;
85 static void imemset(uint8_t *dst
, int value
, int x
, int bps
, int plane
, int length
)
88 for(i
= 0; i
< length
; i
++) {
89 for (b
= 0; b
< bps
; b
++) {
91 dst
[ (x
+i
)*bps
+ 7 - b
] |= 1<<plane
;
96 static int decode_frame_ilbm(AVCodecContext
*avctx
,
97 void *data
, int *data_size
,
100 AVFrame
*frame
= avctx
->priv_data
;
101 const uint8_t *buf
= avpkt
->data
;
102 int buf_size
= avpkt
->size
;
103 int planewidth
= avctx
->width
/ avctx
->bits_per_coded_sample
;
106 if (avctx
->reget_buffer(avctx
, frame
) < 0){
107 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
111 if (buf_size
< avctx
->width
* avctx
->height
) {
112 av_log(avctx
, AV_LOG_ERROR
, "buffer underflow\n");
116 for(y
= 0; y
< avctx
->height
; y
++ ) {
117 uint8_t *row
= &frame
->data
[0][ y
*frame
->linesize
[0] ];
118 memset(row
, 0, avctx
->width
);
119 for (plane
= 0; plane
< avctx
->bits_per_coded_sample
; plane
++) {
120 imemcpy(row
, buf
, 0, avctx
->bits_per_coded_sample
, plane
, planewidth
);
125 *data_size
= sizeof(AVFrame
);
126 *(AVFrame
*)data
= *frame
;
130 static int decode_frame_byterun1(AVCodecContext
*avctx
,
131 void *data
, int *data_size
,
134 AVFrame
*frame
= avctx
->priv_data
;
135 const uint8_t *buf
= avpkt
->data
;
136 int buf_size
= avpkt
->size
;
137 const uint8_t *buf_end
= buf
+buf_size
;
138 int planewidth
= avctx
->width
/ avctx
->bits_per_coded_sample
;
141 if (avctx
->reget_buffer(avctx
, frame
) < 0){
142 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
146 for(y
= 0; y
< avctx
->height
; y
++ ) {
147 uint8_t *row
= &frame
->data
[0][ y
*frame
->linesize
[0] ];
148 if (avctx
->codec_tag
== MKTAG('I','L','B','M')) { //interleaved
149 memset(row
, 0, avctx
->width
);
150 for (plane
= 0; plane
< avctx
->bits_per_coded_sample
; plane
++) {
151 for(x
= 0; x
< planewidth
&& buf
< buf_end
; ) {
156 imemcpy(row
, buf
, x
, avctx
->bits_per_coded_sample
, plane
, FFMIN3(length
, buf_end
- buf
, planewidth
- x
));
158 } else if (value
> -128) {
160 imemset(row
, *buf
++, x
, avctx
->bits_per_coded_sample
, plane
, FFMIN(length
, planewidth
- x
));
168 for(x
= 0; x
< avctx
->width
&& buf
< buf_end
; ) {
173 memcpy(row
+ x
, buf
, FFMIN3(length
, buf_end
- buf
, avctx
->width
- x
));
175 } else if (value
> -128) {
177 memset(row
+ x
, *buf
++, FFMIN(length
, avctx
->width
- x
));
186 *data_size
= sizeof(AVFrame
);
187 *(AVFrame
*)data
= *frame
;
191 static av_cold
int decode_end(AVCodecContext
*avctx
)
193 AVFrame
*frame
= avctx
->priv_data
;
195 avctx
->release_buffer(avctx
, frame
);
199 AVCodec iff_ilbm_decoder
= {
209 .long_name
= NULL_IF_CONFIG_SMALL("IFF ILBM"),
212 AVCodec iff_byterun1_decoder
= {
215 CODEC_ID_IFF_BYTERUN1
,
220 decode_frame_byterun1
,
222 .long_name
= NULL_IF_CONFIG_SMALL("IFF ByteRun1"),