Libav
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 Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 static int sunrast_decode_frame(AVCodecContext *avctx, void *data,
30  int *got_frame, AVPacket *avpkt)
31 {
32  const uint8_t *buf = avpkt->data;
33  const uint8_t *buf_end = avpkt->data + avpkt->size;
34  AVFrame * const p = data;
35  unsigned int w, h, depth, type, maptype, maplength, stride, x, y, len, alen;
36  uint8_t *ptr;
37  const uint8_t *bufstart = buf;
38  int ret;
39 
40  if (avpkt->size < 32)
41  return AVERROR_INVALIDDATA;
42 
43  if (AV_RB32(buf) != RAS_MAGIC) {
44  av_log(avctx, AV_LOG_ERROR, "this is not sunras encoded data\n");
45  return AVERROR_INVALIDDATA;
46  }
47 
48  w = AV_RB32(buf + 4);
49  h = AV_RB32(buf + 8);
50  depth = AV_RB32(buf + 12);
51  type = AV_RB32(buf + 20);
52  maptype = AV_RB32(buf + 24);
53  maplength = AV_RB32(buf + 28);
54  buf += 32;
55 
56  if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF || type == RT_EXPERIMENTAL) {
57  avpriv_request_sample(avctx, "TIFF/IFF/EXPERIMENTAL (compression) type");
58  return AVERROR_PATCHWELCOME;
59  }
60  if (type > RT_FORMAT_IFF) {
61  av_log(avctx, AV_LOG_ERROR, "invalid (compression) type\n");
62  return AVERROR_INVALIDDATA;
63  }
64  if (maptype == RMT_RAW) {
65  avpriv_request_sample(avctx, "Unknown colormap type");
66  return AVERROR_PATCHWELCOME;
67  }
68  if (maptype > RMT_RAW) {
69  av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
70  return AVERROR_INVALIDDATA;
71  }
72 
73 
74  switch (depth) {
75  case 1:
77  break;
78  case 8:
79  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
80  break;
81  case 24:
83  break;
84  default:
85  av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
86  return AVERROR_INVALIDDATA;
87  }
88 
89  ret = ff_set_dimensions(avctx, w, h);
90  if (ret < 0)
91  return ret;
92 
93  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
94  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
95  return ret;
96  }
97 
99 
100  if (buf_end - buf < maplength)
101  return AVERROR_INVALIDDATA;
102 
103  if (depth != 8 && maplength) {
104  av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
105 
106  } else if (maplength) {
107  unsigned int len = maplength / 3;
108 
109  if (maplength % 3 || maplength > 768) {
110  av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
111  return AVERROR_INVALIDDATA;
112  }
113 
114  ptr = p->data[1];
115  for (x = 0; x < len; x++, ptr += 4)
116  *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
117  }
118 
119  buf += maplength;
120 
121  ptr = p->data[0];
122  stride = p->linesize[0];
123 
124  /* scanlines are aligned on 16 bit boundaries */
125  len = (depth * w + 7) >> 3;
126  alen = len + (len & 1);
127 
128  if (type == RT_BYTE_ENCODED) {
129  int value, run;
130  uint8_t *end = ptr + h * stride;
131 
132  x = 0;
133  while (ptr != end && buf < buf_end) {
134  run = 1;
135  if (buf_end - buf < 1)
136  return AVERROR_INVALIDDATA;
137 
138  if ((value = *buf++) == RLE_TRIGGER) {
139  run = *buf++ + 1;
140  if (run != 1)
141  value = *buf++;
142  }
143  while (run--) {
144  if (x < len)
145  ptr[x] = value;
146  if (++x >= alen) {
147  x = 0;
148  ptr += stride;
149  if (ptr == end)
150  break;
151  }
152  }
153  }
154  } else {
155  for (y = 0; y < h; y++) {
156  if (buf_end - buf < len)
157  break;
158  memcpy(ptr, buf, len);
159  ptr += stride;
160  buf += alen;
161  }
162  }
163 
164  *got_frame = 1;
165 
166  return buf - bufstart;
167 }
168 
170  .name = "sunrast",
171  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
172  .type = AVMEDIA_TYPE_VIDEO,
173  .id = AV_CODEC_ID_SUNRAST,
174  .decode = sunrast_decode_frame,
175  .capabilities = CODEC_CAP_DR1,
176 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
#define RMT_RAW
the data layout of this map type is unknown
Definition: sunrast.h:29
#define RT_FORMAT_IFF
Definition: sunrast.h:49
int size
Definition: avcodec.h:974
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
uint8_t run
Definition: svq3.c:146
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2796
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
#define RT_FORMAT_RGB
Definition: sunrast.h:43
#define AV_RB32
Definition: intreadwrite.h:130
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:973
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2803
#define RLE_TRIGGER
Definition: sunrast.h:39
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:196
#define RT_EXPERIMENTAL
Definition: sunrast.h:54
AVCodec ff_sunrast_decoder
Definition: sunrast.c:169
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:68
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
main external API structure.
Definition: avcodec.h:1050
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:612
static int sunrast_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sunrast.c:29
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
common internal and external API header
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: pixfmt.h:74
int len
#define RAS_MAGIC
Definition: sunrast.h:25
This structure stores compressed data.
Definition: avcodec.h:950
#define RT_FORMAT_TIFF
Definition: sunrast.h:48