FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sunrast.c
Go to the documentation of this file.
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/common.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/imgutils.h"
25 #include "avcodec.h"
26 #include "internal.h"
27 #include "sunrast.h"
28 
29 typedef struct SUNRASTContext {
32 
33 static av_cold int sunrast_init(AVCodecContext *avctx) {
34  SUNRASTContext *s = avctx->priv_data;
35 
37  avctx->coded_frame = &s->picture;
38 
39  return 0;
40 }
41 
42 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
43  int *got_frame, AVPacket *avpkt)
44 {
45  const uint8_t *buf = avpkt->data;
46  const uint8_t *buf_end = avpkt->data + avpkt->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, *ptr2 = NULL;
52  const uint8_t *bufstart = buf;
53  int ret;
54 
55  if (avpkt->size < 32)
56  return AVERROR_INVALIDDATA;
57 
58  if (AV_RB32(buf) != RAS_MAGIC) {
59  av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
60  return AVERROR_INVALIDDATA;
61  }
62 
63  w = AV_RB32(buf + 4);
64  h = AV_RB32(buf + 8);
65  depth = AV_RB32(buf + 12);
66  type = AV_RB32(buf + 20);
67  maptype = AV_RB32(buf + 24);
68  maplength = AV_RB32(buf + 28);
69  buf += 32;
70 
71  if (type == RT_EXPERIMENTAL) {
72  av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
73  return AVERROR_PATCHWELCOME;
74  }
75  if (type > RT_FORMAT_IFF) {
76  av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
77  return AVERROR_INVALIDDATA;
78  }
79  if (av_image_check_size(w, h, 0, avctx)) {
80  av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
81  return AVERROR_INVALIDDATA;
82  }
83  if (maptype == RMT_RAW) {
84  av_log_ask_for_sample(avctx, "unsupported colormap type\n");
85  return AVERROR_PATCHWELCOME;
86  }
87  if (maptype > RMT_RAW) {
88  av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
89  return AVERROR_INVALIDDATA;
90  }
91 
92  if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
93  av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
94  return -1;
95  }
96 
97  switch (depth) {
98  case 1:
99  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
100  break;
101  case 4:
102  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
103  break;
104  case 8:
105  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
106  break;
107  case 24:
109  break;
110  case 32:
111  avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_0RGB : AV_PIX_FMT_0BGR;
112  break;
113  default:
114  av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
115  return AVERROR_INVALIDDATA;
116  }
117 
118  if (p->data[0])
119  avctx->release_buffer(avctx, p);
120 
121  if (w != avctx->width || h != avctx->height)
122  avcodec_set_dimensions(avctx, w, h);
123  if ((ret = ff_get_buffer(avctx, p)) < 0) {
124  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
125  return ret;
126  }
127 
129 
130  if (buf_end - buf < maplength)
131  return AVERROR_INVALIDDATA;
132 
133  if (depth > 8 && maplength) {
134  av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
135 
136  } else if (maplength) {
137  unsigned int len = maplength / 3;
138 
139  if (maplength % 3 || maplength > 768) {
140  av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
141  return AVERROR_INVALIDDATA;
142  }
143 
144  ptr = p->data[1];
145  for (x = 0; x < len; x++, ptr += 4)
146  *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
147  }
148 
149  buf += maplength;
150 
151  if (maplength && depth < 8) {
152  ptr = ptr2 = av_malloc((w + 15) * h);
153  if (!ptr)
154  return AVERROR(ENOMEM);
155  stride = (w + 15 >> 3) * depth;
156  } else {
157  ptr = p->data[0];
158  stride = p->linesize[0];
159  }
160 
161  /* scanlines are aligned on 16 bit boundaries */
162  len = (depth * w + 7) >> 3;
163  alen = len + (len & 1);
164 
165  if (type == RT_BYTE_ENCODED) {
166  int value, run;
167  uint8_t *end = ptr + h * stride;
168 
169  x = 0;
170  while (ptr != end && buf < buf_end) {
171  run = 1;
172  if (buf_end - buf < 1)
173  return AVERROR_INVALIDDATA;
174 
175  if ((value = *buf++) == RLE_TRIGGER) {
176  run = *buf++ + 1;
177  if (run != 1)
178  value = *buf++;
179  }
180  while (run--) {
181  if (x < len)
182  ptr[x] = value;
183  if (++x >= alen) {
184  x = 0;
185  ptr += stride;
186  if (ptr == end)
187  break;
188  }
189  }
190  }
191  } else {
192  for (y = 0; y < h; y++) {
193  if (buf_end - buf < len)
194  break;
195  memcpy(ptr, buf, len);
196  ptr += stride;
197  buf += alen;
198  }
199  }
200  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
201  uint8_t *ptr_free = ptr2;
202  ptr = p->data[0];
203  for (y=0; y<h; y++) {
204  for (x = 0; x < (w + 7 >> 3) * depth; x++) {
205  if (depth == 1) {
206  ptr[8*x] = ptr2[x] >> 7;
207  ptr[8*x+1] = ptr2[x] >> 6 & 1;
208  ptr[8*x+2] = ptr2[x] >> 5 & 1;
209  ptr[8*x+3] = ptr2[x] >> 4 & 1;
210  ptr[8*x+4] = ptr2[x] >> 3 & 1;
211  ptr[8*x+5] = ptr2[x] >> 2 & 1;
212  ptr[8*x+6] = ptr2[x] >> 1 & 1;
213  ptr[8*x+7] = ptr2[x] & 1;
214  } else {
215  ptr[2*x] = ptr2[x] >> 4;
216  ptr[2*x+1] = ptr2[x] & 0xF;
217  }
218  }
219  ptr += p->linesize[0];
220  ptr2 += (w + 15 >> 3) * depth;
221  }
222  av_freep(&ptr_free);
223  }
224 
225  *picture = s->picture;
226  *got_frame = 1;
227 
228  return buf - bufstart;
229 }
230 
231 static av_cold int sunrast_end(AVCodecContext *avctx) {
232  SUNRASTContext *s = avctx->priv_data;
233 
234  if(s->picture.data[0])
235  avctx->release_buffer(avctx, &s->picture);
236 
237  return 0;
238 }
239 
241  .name = "sunrast",
242  .type = AVMEDIA_TYPE_VIDEO,
243  .id = AV_CODEC_ID_SUNRAST,
244  .priv_data_size = sizeof(SUNRASTContext),
245  .init = sunrast_init,
246  .close = sunrast_end,
248  .capabilities = CODEC_CAP_DR1,
249  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
250 };