Imported Upstream version 6.1
[platform/upstream/ffmpeg.git] / libavcodec / sgidec.c
1 /*
2  * SGI image decoder
3  * Todd Kirby <doubleshot@pacbell.net>
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 "avcodec.h"
23 #include "bytestream.h"
24 #include "codec_internal.h"
25 #include "decode.h"
26 #include "sgi.h"
27
28 /**
29  * Expand an RLE row into a channel.
30  * @param logctx a logcontext
31  * @param out_buf Points to one line after the output buffer.
32  * @param g   GetByteContext used to read input from
33  * @param width length of out_buf in nb of elements
34  * @return nb of elements written, else return error code.
35  */
36 static int expand_rle_row8(void *logctx, uint8_t *out_buf,
37                            GetByteContext *g, unsigned width)
38 {
39     unsigned char pixel, count;
40     unsigned char *orig = out_buf;
41     uint8_t *out_end = out_buf + width;
42
43     while (out_buf < out_end) {
44         if (bytestream2_get_bytes_left(g) < 1)
45             return AVERROR_INVALIDDATA;
46         pixel = bytestream2_get_byteu(g);
47         if (!(count = (pixel & 0x7f))) {
48             break;
49         }
50
51         /* Check for buffer overflow. */
52         if (out_end - out_buf < count) {
53             av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
54             return AVERROR_INVALIDDATA;
55         }
56
57         if (pixel & 0x80) {
58             while (count--)
59                 *out_buf++ = bytestream2_get_byte(g);
60         } else {
61             pixel = bytestream2_get_byte(g);
62
63             while (count--)
64                 *out_buf++ = pixel;
65         }
66     }
67     return out_buf - orig;
68 }
69
70 static int expand_rle_row16(void *logctx, uint16_t *out_buf,
71                             GetByteContext *g, unsigned width)
72 {
73     unsigned short pixel;
74     unsigned char count;
75     unsigned short *orig = out_buf;
76     uint16_t *out_end = out_buf + width;
77
78     while (out_buf < out_end) {
79         if (bytestream2_get_bytes_left(g) < 2)
80             return AVERROR_INVALIDDATA;
81         pixel = bytestream2_get_be16u(g);
82         if (!(count = (pixel & 0x7f)))
83             break;
84
85         /* Check for buffer overflow. */
86         if (out_end - out_buf < count) {
87             av_log(logctx, AV_LOG_ERROR, "Invalid pixel count.\n");
88             return AVERROR_INVALIDDATA;
89         }
90
91         if (pixel & 0x80) {
92             while (count--) {
93                 pixel = bytestream2_get_ne16(g);
94                 AV_WN16A(out_buf, pixel);
95                 out_buf++;
96             }
97         } else {
98             pixel = bytestream2_get_ne16(g);
99
100             while (count--) {
101                 AV_WN16A(out_buf, pixel);
102                 out_buf++;
103             }
104         }
105     }
106     return out_buf - orig;
107 }
108
109
110 /**
111  * Read a run length encoded SGI image.
112  * @param out_buf output buffer
113  * @param s the current image state
114  * @return 0 if no error, else return error code.
115  */
116 static int read_rle_sgi(void *logctx, uint8_t *out[4], ptrdiff_t stride[4],
117                         GetByteContext *g, unsigned width, int height,
118                         unsigned nb_components, unsigned bytes_per_channel)
119 {
120     unsigned int len = height * nb_components * 4;
121     GetByteContext g_table = *g;
122     unsigned int start_offset;
123     int ret;
124
125     /* size of  RLE offset and length tables */
126     if (len * 2 > bytestream2_get_bytes_left(g)) {
127         return AVERROR_INVALIDDATA;
128     }
129
130     for (unsigned z = 0; z < nb_components; z++) {
131         uint8_t *dest_row = out[z] + (height - 1) * stride[z];
132         while (1) {
133             start_offset = bytestream2_get_be32(&g_table);
134             bytestream2_seek(g, start_offset, SEEK_SET);
135             if (bytes_per_channel == 1)
136                 ret = expand_rle_row8(logctx, dest_row, g, width);
137             else
138                 ret = expand_rle_row16(logctx, (uint16_t *)dest_row, g, width);
139             if (ret != width)
140                 return AVERROR_INVALIDDATA;
141             if (dest_row == out[z])
142                 break;
143             dest_row -= stride[z];
144         }
145     }
146     return 0;
147 }
148
149 /**
150  * Read an uncompressed SGI image.
151  * @param out_buf output buffer
152  * @param s the current image state
153  * @return 0 if read success, else return error code.
154  */
155 static int read_uncompressed_sgi(uint8_t *const out[4], const ptrdiff_t stride[4],
156                                  GetByteContext *g, unsigned width, int height,
157                                  unsigned nb_components, unsigned bytes_per_channel)
158 {
159     unsigned rowsize = width * bytes_per_channel;
160
161     /* Test buffer size. */
162     if (rowsize * (int64_t)height * nb_components > bytestream2_get_bytes_left(g))
163         return AVERROR_INVALIDDATA;
164
165     for (unsigned z = 0; z < nb_components; z++) {
166         uint8_t *cur_row = out[z] + (height - 1) * stride[z];
167         while (1) {
168             bytestream2_get_bufferu(g, cur_row, rowsize);
169             if (cur_row == out[z])
170                 break;
171             cur_row -= stride[z];
172         }
173     }
174     return 0;
175 }
176
177 static int decode_frame(AVCodecContext *avctx, AVFrame *p,
178                         int *got_frame, AVPacket *avpkt)
179 {
180     GetByteContext g;
181     unsigned int bytes_per_channel, nb_components, dimension, rle, width;
182     uint8_t *out[4];
183     ptrdiff_t linesize[4];
184     int height;
185     int ret = 0;
186
187     bytestream2_init(&g, avpkt->data, avpkt->size);
188     if (bytestream2_get_bytes_left(&g) < SGI_HEADER_SIZE) {
189         av_log(avctx, AV_LOG_ERROR, "buf_size too small (%d)\n", avpkt->size);
190         return AVERROR_INVALIDDATA;
191     }
192
193     /* Test for SGI magic. */
194     if (bytestream2_get_be16u(&g) != SGI_MAGIC) {
195         av_log(avctx, AV_LOG_ERROR, "bad magic number\n");
196         return AVERROR_INVALIDDATA;
197     }
198
199     rle                  = bytestream2_get_byteu(&g);
200     bytes_per_channel    = bytestream2_get_byteu(&g);
201     dimension            = bytestream2_get_be16u(&g);
202     width                = bytestream2_get_be16u(&g);
203     height               = bytestream2_get_be16u(&g);
204     nb_components        = bytestream2_get_be16u(&g);
205
206     if (bytes_per_channel != 1 && bytes_per_channel != 2) {
207         av_log(avctx, AV_LOG_ERROR, "wrong channel number\n");
208         return AVERROR_INVALIDDATA;
209     }
210
211     /* Check for supported image dimensions. */
212     if (dimension != 2 && dimension != 3) {
213         av_log(avctx, AV_LOG_ERROR, "wrong dimension number\n");
214         return AVERROR_INVALIDDATA;
215     }
216
217     if (nb_components == SGI_GRAYSCALE) {
218         avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GRAY16BE : AV_PIX_FMT_GRAY8;
219     } else if (nb_components == SGI_RGB) {
220         avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRP16BE : AV_PIX_FMT_GBRP;
221     } else if (nb_components == SGI_RGBA) {
222         avctx->pix_fmt = bytes_per_channel == 2 ? AV_PIX_FMT_GBRAP16BE : AV_PIX_FMT_GBRAP;
223     } else {
224         av_log(avctx, AV_LOG_ERROR, "wrong picture format\n");
225         return AVERROR_INVALIDDATA;
226     }
227
228     ret = ff_set_dimensions(avctx, width, height);
229     if (ret < 0)
230         return ret;
231
232     if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
233         return ret;
234
235     switch (nb_components) {
236 #define MAP(in_idx, out_idx) \
237     out[(in_idx)]      = p->data[(out_idx)]; \
238     linesize[(in_idx)] = p->linesize[(out_idx)]
239     case SGI_GRAYSCALE:
240         MAP(0, 0);
241         break;
242     case SGI_RGBA:
243         MAP(3, 3);
244         /* fallthrough */
245     case SGI_RGB:
246         MAP(0, 2);
247         MAP(1, 0);
248         MAP(2, 1);
249         break;
250     }
251     p->pict_type = AV_PICTURE_TYPE_I;
252     p->flags |= AV_FRAME_FLAG_KEY;
253
254     /* Skip header. */
255     bytestream2_seek(&g, SGI_HEADER_SIZE, SEEK_SET);
256     if (rle) {
257         ret = read_rle_sgi(avctx, out, linesize, &g,
258                            width, height, nb_components, bytes_per_channel);
259     } else {
260         ret = read_uncompressed_sgi(out, linesize, &g,
261                                     width, height, nb_components, bytes_per_channel);
262     }
263     if (ret)
264         return ret;
265
266     *got_frame = 1;
267     return avpkt->size;
268 }
269
270 const FFCodec ff_sgi_decoder = {
271     .p.name         = "sgi",
272     CODEC_LONG_NAME("SGI image"),
273     .p.type         = AVMEDIA_TYPE_VIDEO,
274     .p.id           = AV_CODEC_ID_SGI,
275     FF_CODEC_DECODE_CB(decode_frame),
276     .p.capabilities = AV_CODEC_CAP_DR1,
277 };