2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder
3 * Copyright (c) 2007, 2008 Ivo van Poorten
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
22 #include "libavutil/intreadwrite.h"
27 #define RT_BYTE_ENCODED 2
28 #define RT_FORMAT_RGB 3
29 #define RT_FORMAT_TIFF 4
30 #define RT_FORMAT_IFF 5
32 typedef struct SUNRASTContext
{
36 static av_cold
int sunrast_init(AVCodecContext
*avctx
) {
37 SUNRASTContext
*s
= avctx
->priv_data
;
39 avcodec_get_frame_defaults(&s
->picture
);
40 avctx
->coded_frame
= &s
->picture
;
45 static int sunrast_decode_frame(AVCodecContext
*avctx
, void *data
,
46 int *data_size
, AVPacket
*avpkt
) {
47 const uint8_t *buf
= avpkt
->data
;
48 SUNRASTContext
* const s
= avctx
->priv_data
;
49 AVFrame
*picture
= data
;
50 AVFrame
* const p
= &s
->picture
;
51 unsigned int w
, h
, depth
, type
, maptype
, maplength
, stride
, x
, y
, len
, alen
;
53 const uint8_t *bufstart
= buf
;
55 if (AV_RB32(buf
) != 0x59a66a95) {
56 av_log(avctx
, AV_LOG_ERROR
, "this is not sunras encoded data\n");
62 depth
= AV_RB32(buf
+12);
63 type
= AV_RB32(buf
+20);
64 maptype
= AV_RB32(buf
+24);
65 maplength
= AV_RB32(buf
+28);
67 if (type
> RT_BYTE_ENCODED
&& type
<= RT_FORMAT_IFF
) {
68 av_log(avctx
, AV_LOG_ERROR
, "unsupported (compression) type\n");
71 if (type
> RT_FORMAT_IFF
) {
72 av_log(avctx
, AV_LOG_ERROR
, "invalid (compression) type\n");
76 av_log(avctx
, AV_LOG_ERROR
, "invalid colormap type\n");
84 avctx
->pix_fmt
= PIX_FMT_MONOWHITE
;
87 avctx
->pix_fmt
= PIX_FMT_PAL8
;
90 avctx
->pix_fmt
= PIX_FMT_BGR24
;
93 av_log(avctx
, AV_LOG_ERROR
, "invalid depth\n");
98 avctx
->release_buffer(avctx
, p
);
100 if (avcodec_check_dimensions(avctx
, w
, h
))
102 if (w
!= avctx
->width
|| h
!= avctx
->height
)
103 avcodec_set_dimensions(avctx
, w
, h
);
104 if (avctx
->get_buffer(avctx
, p
) < 0) {
105 av_log(avctx
, AV_LOG_ERROR
, "get_buffer() failed\n");
109 p
->pict_type
= FF_I_TYPE
;
111 if (depth
!= 8 && maplength
) {
112 av_log(avctx
, AV_LOG_WARNING
, "useless colormap found or file is corrupted, trying to recover\n");
114 } else if (depth
== 8) {
115 unsigned int len
= maplength
/ 3;
118 av_log(avctx
, AV_LOG_ERROR
, "colormap expected\n");
121 if (maplength
% 3 || maplength
> 768) {
122 av_log(avctx
, AV_LOG_WARNING
, "invalid colormap length\n");
127 for (x
=0; x
<len
; x
++, ptr
+=4)
128 *(uint32_t *)ptr
= (buf
[x
]<<16) + (buf
[len
+x
]<<8) + buf
[len
+len
+x
];
134 stride
= p
->linesize
[0];
136 /* scanlines are aligned on 16 bit boundaries */
137 len
= (depth
* w
+ 7) >> 3;
138 alen
= len
+ (len
&1);
140 if (type
== RT_BYTE_ENCODED
) {
142 uint8_t *end
= ptr
+ h
*stride
;
147 if ((value
= *buf
++) == 0x80) {
164 for (y
=0; y
<h
; y
++) {
165 memcpy(ptr
, buf
, len
);
171 *picture
= s
->picture
;
172 *data_size
= sizeof(AVFrame
);
174 return buf
- bufstart
;
177 static av_cold
int sunrast_end(AVCodecContext
*avctx
) {
178 SUNRASTContext
*s
= avctx
->priv_data
;
180 if(s
->picture
.data
[0])
181 avctx
->release_buffer(avctx
, &s
->picture
);
186 AVCodec sunrast_decoder
= {
190 sizeof(SUNRASTContext
),
194 sunrast_decode_frame
,
197 .long_name
= NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),