FFmpeg  2.1.1
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 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, *ptr2 = NULL;
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_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 (av_image_check_size(w, h, 0, avctx)) {
65  av_log(avctx, AV_LOG_ERROR, "invalid image size\n");
66  return AVERROR_INVALIDDATA;
67  }
68  if (maptype == RMT_RAW) {
69  avpriv_request_sample(avctx, "Unknown colormap type");
70  return AVERROR_PATCHWELCOME;
71  }
72  if (maptype > RMT_RAW) {
73  av_log(avctx, AV_LOG_ERROR, "invalid colormap type\n");
74  return AVERROR_INVALIDDATA;
75  }
76 
77  if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
78  av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
79  return -1;
80  }
81 
82  switch (depth) {
83  case 1:
84  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
85  break;
86  case 4:
87  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
88  break;
89  case 8:
90  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
91  break;
92  case 24:
94  break;
95  case 32:
97  break;
98  default:
99  av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
100  return AVERROR_INVALIDDATA;
101  }
102 
103  if (w != avctx->width || h != avctx->height)
104  avcodec_set_dimensions(avctx, w, h);
105  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
106  return ret;
107 
109 
110  if (buf_end - buf < maplength)
111  return AVERROR_INVALIDDATA;
112 
113  if (depth > 8 && maplength) {
114  av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
115 
116  } else if (maplength) {
117  unsigned int len = maplength / 3;
118 
119  if (maplength % 3 || maplength > 768) {
120  av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
121  return AVERROR_INVALIDDATA;
122  }
123 
124  ptr = p->data[1];
125  for (x = 0; x < len; x++, ptr += 4)
126  *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
127  }
128 
129  buf += maplength;
130 
131  if (maplength && depth < 8) {
132  ptr = ptr2 = av_malloc((w + 15) * h);
133  if (!ptr)
134  return AVERROR(ENOMEM);
135  stride = (w + 15 >> 3) * depth;
136  } else {
137  ptr = p->data[0];
138  stride = p->linesize[0];
139  }
140 
141  /* scanlines are aligned on 16 bit boundaries */
142  len = (depth * w + 7) >> 3;
143  alen = len + (len & 1);
144 
145  if (type == RT_BYTE_ENCODED) {
146  int value, run;
147  uint8_t *end = ptr + h * stride;
148 
149  x = 0;
150  while (ptr != end && buf < buf_end) {
151  run = 1;
152  if (buf_end - buf < 1)
153  return AVERROR_INVALIDDATA;
154 
155  if ((value = *buf++) == RLE_TRIGGER) {
156  run = *buf++ + 1;
157  if (run != 1)
158  value = *buf++;
159  }
160  while (run--) {
161  if (x < len)
162  ptr[x] = value;
163  if (++x >= alen) {
164  x = 0;
165  ptr += stride;
166  if (ptr == end)
167  break;
168  }
169  }
170  }
171  } else {
172  for (y = 0; y < h; y++) {
173  if (buf_end - buf < len)
174  break;
175  memcpy(ptr, buf, len);
176  ptr += stride;
177  buf += alen;
178  }
179  }
180  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
181  uint8_t *ptr_free = ptr2;
182  ptr = p->data[0];
183  for (y=0; y<h; y++) {
184  for (x = 0; x < (w + 7 >> 3) * depth; x++) {
185  if (depth == 1) {
186  ptr[8*x] = ptr2[x] >> 7;
187  ptr[8*x+1] = ptr2[x] >> 6 & 1;
188  ptr[8*x+2] = ptr2[x] >> 5 & 1;
189  ptr[8*x+3] = ptr2[x] >> 4 & 1;
190  ptr[8*x+4] = ptr2[x] >> 3 & 1;
191  ptr[8*x+5] = ptr2[x] >> 2 & 1;
192  ptr[8*x+6] = ptr2[x] >> 1 & 1;
193  ptr[8*x+7] = ptr2[x] & 1;
194  } else {
195  ptr[2*x] = ptr2[x] >> 4;
196  ptr[2*x+1] = ptr2[x] & 0xF;
197  }
198  }
199  ptr += p->linesize[0];
200  ptr2 += (w + 15 >> 3) * depth;
201  }
202  av_freep(&ptr_free);
203  }
204 
205  *got_frame = 1;
206 
207  return buf - bufstart;
208 }
209 
211  .name = "sunrast",
212  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
213  .type = AVMEDIA_TYPE_VIDEO,
214  .id = AV_CODEC_ID_SUNRAST,
215  .decode = sunrast_decode_frame,
216  .capabilities = CODEC_CAP_DR1,
217 };
#define AVERROR_PATCHWELCOME
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
#define RMT_RAW
the data layout of this map type is unknown
Definition: sunrast.h:29
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: avcodec.h:4536
#define RT_FORMAT_IFF
Definition: sunrast.h:49
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
Definition: utils.c:233
int size
Definition: avcodec.h:1064
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
uint8_t run
Definition: svq3.c:145
Pixel format.
Definition: avcodec.h:4533
int stride
Definition: mace.c:144
AVCodec.
Definition: avcodec.h:2922
Y , 8bpp.
Definition: avcodec.h:4542
packed BGR 8:8:8, 32bpp, 0BGR0BGR...
Definition: avcodec.h:4691
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: avcodec.h:4537
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
#define RT_FORMAT_RGB
Definition: sunrast.h:43
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:67
const char * name
Name of the codec implementation.
Definition: avcodec.h:2929
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:742
const char data[16]
Definition: mxf.c:68
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
Libavcodec external API header.
int depth
Definition: v4l.c:61
#define RLE_TRIGGER
Definition: sunrast.h:39
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:231
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:167
float y
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define RT_EXPERIMENTAL
Definition: sunrast.h:54
AVCodec ff_sunrast_decoder
Definition: sunrast.c:210
main external API structure.
Definition: avcodec.h:1146
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
void * buf
Definition: avisynth_c.h:594
static int sunrast_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sunrast.c:29
double value
Definition: eval.c:83
uint8_t * data
Definition: avcodec.h:1063
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
#define type
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb...
Definition: avcodec.h:4543
common internal api header.
#define AV_RB32(x)
Definition: intreadwrite.h:258
#define AVERROR_INVALIDDATA
int len
#define RAS_MAGIC
Definition: sunrast.h:25
#define AVERROR(e)
8 bit with PIX_FMT_RGB32 palette
Definition: avcodec.h:4545
packed RGB 8:8:8, 32bpp, 0RGB0RGB...
Definition: avcodec.h:4689
This structure stores compressed data.
Definition: avcodec.h:1040
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
#define RT_FORMAT_TIFF
Definition: sunrast.h:48