]> git.sesse.net Git - ffmpeg/blob - libavcodec/sunrast.c
Add av_log_{ask_for_sample|missing_feature} replacements to libavutil
[ffmpeg] / libavcodec / sunrast.c
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         av_log_ask_for_sample(avctx, "unsupported (compression) type\n");
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         av_log_ask_for_sample(avctx, "unsupported colormap type\n");
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
78     switch (depth) {
79         case 1:
80             avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
81             break;
82         case 8:
83             avctx->pix_fmt = maplength ? AV_PIX_FMT_PAL8 : AV_PIX_FMT_GRAY8;
84             break;
85         case 24:
86             avctx->pix_fmt = (type == RT_FORMAT_RGB) ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
87             break;
88         default:
89             av_log(avctx, AV_LOG_ERROR, "invalid depth\n");
90             return AVERROR_INVALIDDATA;
91     }
92
93     if (w != avctx->width || h != avctx->height)
94         avcodec_set_dimensions(avctx, w, h);
95     if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
96         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
97         return ret;
98     }
99
100     p->pict_type = AV_PICTURE_TYPE_I;
101
102     if (buf_end - buf < maplength)
103         return AVERROR_INVALIDDATA;
104
105     if (depth != 8 && maplength) {
106         av_log(avctx, AV_LOG_WARNING, "useless colormap found or file is corrupted, trying to recover\n");
107
108     } else if (maplength) {
109         unsigned int len = maplength / 3;
110
111         if (maplength % 3 || maplength > 768) {
112             av_log(avctx, AV_LOG_WARNING, "invalid colormap length\n");
113             return AVERROR_INVALIDDATA;
114         }
115
116         ptr = p->data[1];
117         for (x = 0; x < len; x++, ptr += 4)
118             *(uint32_t *)ptr = (buf[x] << 16) + (buf[len + x] << 8) + buf[len + len + x];
119     }
120
121     buf += maplength;
122
123     ptr    = p->data[0];
124     stride = p->linesize[0];
125
126     /* scanlines are aligned on 16 bit boundaries */
127     len  = (depth * w + 7) >> 3;
128     alen = len + (len & 1);
129
130     if (type == RT_BYTE_ENCODED) {
131         int value, run;
132         uint8_t *end = ptr + h * stride;
133
134         x = 0;
135         while (ptr != end && buf < buf_end) {
136             run = 1;
137             if (buf_end - buf < 1)
138                 return AVERROR_INVALIDDATA;
139
140             if ((value = *buf++) == RLE_TRIGGER) {
141                 run = *buf++ + 1;
142                 if (run != 1)
143                     value = *buf++;
144             }
145             while (run--) {
146                 if (x < len)
147                     ptr[x] = value;
148                 if (++x >= alen) {
149                     x = 0;
150                     ptr += stride;
151                     if (ptr == end)
152                         break;
153                 }
154             }
155         }
156     } else {
157         for (y = 0; y < h; y++) {
158             if (buf_end - buf < len)
159                 break;
160             memcpy(ptr, buf, len);
161             ptr += stride;
162             buf += alen;
163         }
164     }
165
166     *got_frame = 1;
167
168     return buf - bufstart;
169 }
170
171 AVCodec ff_sunrast_decoder = {
172     .name           = "sunrast",
173     .type           = AVMEDIA_TYPE_VIDEO,
174     .id             = AV_CODEC_ID_SUNRAST,
175     .decode         = sunrast_decode_frame,
176     .capabilities   = CODEC_CAP_DR1,
177     .long_name      = NULL_IF_CONFIG_SMALL("Sun Rasterfile image"),
178 };