1/* 2 * Sun Rasterfile (.sun/.ras/im{1,8,24}/.sunras) image decoder 3 * Copyright (c) 2007, 2008 Ivo van Poorten 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#include "libavutil/intreadwrite.h" 23#include "avcodec.h" 24 25#define RT_OLD 0 26#define RT_STANDARD 1 27#define RT_BYTE_ENCODED 2 28#define RT_FORMAT_RGB 3 29#define RT_FORMAT_TIFF 4 30#define RT_FORMAT_IFF 5 31 32typedef struct SUNRASTContext { 33 AVFrame picture; 34} SUNRASTContext; 35 36static av_cold int sunrast_init(AVCodecContext *avctx) { 37 SUNRASTContext *s = avctx->priv_data; 38 39 avcodec_get_frame_defaults(&s->picture); 40 avctx->coded_frame= &s->picture; 41 42 return 0; 43} 44 45static int sunrast_decode_frame(AVCodecContext *avctx, void *data, 46 int *data_size, const uint8_t *buf, int buf_size) { 47 SUNRASTContext * const s = avctx->priv_data; 48 AVFrame *picture = data; 49 AVFrame * const p = &s->picture; 50 unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen; 51 uint8_t *ptr; 52 const uint8_t *bufstart = buf; 53 54 if (AV_RB32(buf) != 0x59a66a95) { 55 av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n"); 56 return -1; 57 } 58 59 w = AV_RB32(buf+4); 60 h = AV_RB32(buf+8); 61 depth = AV_RB32(buf+12); 62 type = AV_RB32(buf+20); 63 maptype = AV_RB32(buf+24); 64 maplength = AV_RB32(buf+28); 65 66 if (type > RT_BYTE_ENCODED && type <= RT_FORMAT_IFF) { 67 av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n"); 68 return -1; 69 } 70 if (type > RT_FORMAT_IFF) { 71 av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n"); 72 return -1; 73 } 74 if (maptype & ~1) { 75 av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n"); 76 return -1; 77 } 78 79 buf += 32; 80 81 switch (depth) { 82 case 1: 83 avctx->pix_fmt = PIX_FMT_MONOWHITE; 84 break; 85 case 8: 86 avctx->pix_fmt = PIX_FMT_PAL8; 87 break; 88 case 24: 89 avctx->pix_fmt = PIX_FMT_BGR24; 90 break; 91 default: 92 av_log(avctx, AV_LOG_ERROR, "invalid depth\n"); 93 return -1; 94 } 95 96 if (p->data[0]) 97 avctx->release_buffer(avctx, p); 98 99 if (avcodec_check_dimensions(avctx, w, h)) 100 return -1; 101 if (w != avctx->width || h != avctx->height) 102 avcodec_set_dimensions(avctx, w, h); 103 if (avctx->get_buffer(avctx, p) < 0) { 104 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 105 return -1; 106 } 107 108 p->pict_type = FF_I_TYPE; 109 110 if (depth != 8 && maplength) { 111 av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n"); 112 113 } else if (depth == 8) { 114 unsigned int len = maplength / 3; 115 116 if (!maplength) { 117 av_log(avctx, AV_LOG_ERROR, "colormap expected\n"); 118 return -1; 119 } 120 if (maplength % 3 || maplength > 768) { 121 av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n"); 122 return -1; 123 } 124 125 ptr = p->data[1]; 126 for (x=0; x<len; x++, ptr+=4) 127 *(uint32_t *)ptr = (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x]; 128 } 129 130 buf += maplength; 131 132 ptr = p->data[0]; 133 stride = p->linesize[0]; 134 135 /* scanlines are aligned on 16 bit boundaries */ 136 len = (depth * w + 7) >> 3; 137 alen = len + (len&1); 138 139 if (type == RT_BYTE_ENCODED) { 140 int value, run; 141 uint8_t *end = ptr + h*stride; 142 143 x = 0; 144 while (ptr != end) { 145 run = 1; 146 if ((value = *buf++) == 0x80) { 147 run = *buf++ + 1; 148 if (run != 1) 149 value = *buf++; 150 } 151 while (run--) { 152 if (x < len) 153 ptr[x] = value; 154 if (++x >= alen) { 155 x = 0; 156 ptr += stride; 157 if (ptr == end) 158 break; 159 } 160 } 161 } 162 } else { 163 for (y=0; y<h; y++) { 164 memcpy(ptr, buf, len); 165 ptr += stride; 166 buf += alen; 167 } 168 } 169 170 *picture = s->picture; 171 *data_size = sizeof(AVFrame); 172 173 return buf - bufstart; 174} 175 176static av_cold int sunrast_end(AVCodecContext *avctx) { 177 SUNRASTContext *s = avctx->priv_data; 178 179 if(s->picture.data[0]) 180 avctx->release_buffer(avctx, &s->picture); 181 182 return 0; 183} 184 185AVCodec sunrast_decoder = { 186 "sunrast", 187 CODEC_TYPE_VIDEO, 188 CODEC_ID_SUNRAST, 189 sizeof(SUNRASTContext), 190 sunrast_init, 191 NULL, 192 sunrast_end, 193 sunrast_decode_frame, 194 0, 195 NULL, 196 .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"), 197}; 198