FFmpeg
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 (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  if (type == RT_FORMAT_TIFF || type == RT_FORMAT_IFF) {
74  av_log(avctx, AV_LOG_ERROR, "unsupported (compression) type\n");
75  return AVERROR_PATCHWELCOME;
76  }
77 
78  switch (depth) {
79  case 1:
80  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_MONOWHITE;
81  break;
82  case 4:
83  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_NONE;
84  break;
85  case 8:
86  avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
87  break;
88  case 24:
90  break;
91  case 32:
93  break;
94  default:
95  av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
96  return AVERROR_INVALIDDATA;
97  }
98 
99  ret = ff_set_dimensions(avctx, w, h);
100  if (ret < 0)
101  return ret;
102 
103  /* scanlines are aligned on 16 bit boundaries */
104  len = (depth * w + 7) >> 3;
105  alen = len + (len & 1);
106 
107  if (buf_end - buf < maplength + (len * h) * 3 / 256)
108  return AVERROR_INVALIDDATA;
109 
110  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
111  return ret;
112 
114 
115  if (depth > 8 && maplength) {
116  av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
117 
118  } else if (maplength) {
119  unsigned int len = maplength / 3;
120 
121  if (maplength % 3 || maplength > 768) {
122  av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
123  return AVERROR_INVALIDDATA;
124  }
125 
126  ptr = p->data[1];
127  for (x = 0; x < len; x++, ptr += 4)
128  *(uint32_t *)ptr = (0xFFU<<24) + (buf[x]<<16) + (buf[len+x]<<8) + buf[len+len+x];
129  }
130 
131  buf += maplength;
132 
133  if (maplength && depth < 8) {
134  ptr = ptr2 = av_malloc_array((w + 15), h);
135  if (!ptr)
136  return AVERROR(ENOMEM);
137  stride = (w + 15 >> 3) * depth;
138  } else {
139  ptr = p->data[0];
140  stride = p->linesize[0];
141  }
142 
143  if (type == RT_BYTE_ENCODED) {
144  int value, run;
145  uint8_t *end = ptr + h * stride;
146 
147  x = 0;
148  while (ptr != end && buf < buf_end) {
149  run = 1;
150  if (buf_end - buf < 1)
151  return AVERROR_INVALIDDATA;
152 
153  if ((value = *buf++) == RLE_TRIGGER) {
154  run = *buf++ + 1;
155  if (run != 1)
156  value = *buf++;
157  }
158  while (run--) {
159  if (x < len)
160  ptr[x] = value;
161  if (++x >= alen) {
162  x = 0;
163  ptr += stride;
164  if (ptr == end)
165  break;
166  }
167  }
168  }
169  } else {
170  for (y = 0; y < h; y++) {
171  if (buf_end - buf < alen)
172  break;
173  memcpy(ptr, buf, len);
174  ptr += stride;
175  buf += alen;
176  }
177  }
178  if (avctx->pix_fmt == AV_PIX_FMT_PAL8 && depth < 8) {
179  uint8_t *ptr_free = ptr2;
180  ptr = p->data[0];
181  for (y=0; y<h; y++) {
182  for (x = 0; x < (w + 7 >> 3) * depth; x++) {
183  if (depth == 1) {
184  ptr[8*x] = ptr2[x] >> 7;
185  ptr[8*x+1] = ptr2[x] >> 6 & 1;
186  ptr[8*x+2] = ptr2[x] >> 5 & 1;
187  ptr[8*x+3] = ptr2[x] >> 4 & 1;
188  ptr[8*x+4] = ptr2[x] >> 3 & 1;
189  ptr[8*x+5] = ptr2[x] >> 2 & 1;
190  ptr[8*x+6] = ptr2[x] >> 1 & 1;
191  ptr[8*x+7] = ptr2[x] & 1;
192  } else {
193  ptr[2*x] = ptr2[x] >> 4;
194  ptr[2*x+1] = ptr2[x] & 0xF;
195  }
196  }
197  ptr += p->linesize[0];
198  ptr2 += (w + 15 >> 3) * depth;
199  }
200  av_freep(&ptr_free);
201  }
202 
203  *got_frame = 1;
204 
205  return buf - bufstart;
206 }
207 
209  .name = "sunrast",
210  .long_name = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
211  .type = AVMEDIA_TYPE_VIDEO,
212  .id = AV_CODEC_ID_SUNRAST,
213  .decode = sunrast_decode_frame,
214  .capabilities = AV_CODEC_CAP_DR1,
215 };
AVCodec
AVCodec.
Definition: codec.h:202
ff_sunrast_decoder
const AVCodec ff_sunrast_decoder
Definition: sunrast.c:208
stride
int stride
Definition: mace.c:144
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
w
uint8_t w
Definition: llviddspenc.c:38
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
data
const char data[16]
Definition: mxf.c:143
AV_PIX_FMT_MONOWHITE
@ AV_PIX_FMT_MONOWHITE
Y , 1bpp, 0 is white, 1 is black, in each byte pixels are ordered from the msb to the lsb.
Definition: pixfmt.h:75
AV_PIX_FMT_BGR24
@ AV_PIX_FMT_BGR24
packed RGB 8:8:8, 24bpp, BGRBGR...
Definition: pixfmt.h:69
RMT_RAW
#define RMT_RAW
the data layout of this map type is unknown
Definition: sunrast.h:29
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
U
#define U(x)
Definition: vp56_arith.h:37
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
RT_FORMAT_RGB
#define RT_FORMAT_RGB
Definition: sunrast.h:43
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
intreadwrite.h
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
run
uint8_t run
Definition: svq3.c:203
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
RT_EXPERIMENTAL
#define RT_EXPERIMENTAL
Definition: sunrast.h:54
sunrast.h
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:414
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1652
AV_PIX_FMT_RGB24
@ AV_PIX_FMT_RGB24
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:68
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
RLE_TRIGGER
#define RLE_TRIGGER
Definition: sunrast.h:39
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
common.h
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
avcodec.h
AV_PIX_FMT_PAL8
@ AV_PIX_FMT_PAL8
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:77
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_0BGR
@ AV_PIX_FMT_0BGR
packed BGR 8:8:8, 32bpp, XBGRXBGR... X=unused/undefined
Definition: pixfmt.h:229
AVCodecContext
main external API structure.
Definition: avcodec.h:383
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
sunrast_decode_frame
static int sunrast_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: sunrast.c:29
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_set_dimensions
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:86
RT_BYTE_ENCODED
#define RT_BYTE_ENCODED
Definition: sunrast.h:38
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
AVPacket
This structure stores compressed data.
Definition: packet.h:350
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:362
AV_PIX_FMT_0RGB
@ AV_PIX_FMT_0RGB
packed RGB 8:8:8, 32bpp, XRGBXRGB... X=unused/undefined
Definition: pixfmt.h:227
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AV_CODEC_ID_SUNRAST
@ AV_CODEC_ID_SUNRAST
Definition: codec_id.h:160
h
h
Definition: vp9dsp_template.c:2038
RT_FORMAT_TIFF
#define RT_FORMAT_TIFF
Definition: sunrast.h:48
RAS_MAGIC
#define RAS_MAGIC
Definition: sunrast.h:25
RT_FORMAT_IFF
#define RT_FORMAT_IFF
Definition: sunrast.h:49