]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
quickdraw: Support direct pixel blocks
[ffmpeg] / libavcodec / qdrw.c
1 /*
2  * QuickDraw (qdrw) codec
3  * Copyright (c) 2004 Konstantin Shishkov
4  * Copyright (c) 2015 Vittorio Giovara
5  *
6  * This file is part of Libav.
7  *
8  * Libav is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * Libav is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * Apple QuickDraw codec.
26  * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
27  */
28
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "internal.h"
34
35 enum QuickdrawOpcodes {
36     PACKBITSRECT = 0x0098,
37     PACKBITSRGN,
38     DIRECTBITSRECT,
39     DIRECTBITSRGN,
40
41     EOP = 0x00FF,
42 };
43
44 static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
45                          uint32_t *pal, int colors)
46 {
47     int i;
48
49     for (i = 0; i <= colors; i++) {
50         uint8_t r, g, b;
51         unsigned int idx = bytestream2_get_be16(gbc); /* color index */
52         if (idx > 255) {
53             av_log(avctx, AV_LOG_WARNING,
54                    "Palette index out of range: %u\n", idx);
55             bytestream2_skip(gbc, 6);
56             continue;
57         }
58         r = bytestream2_get_byte(gbc);
59         bytestream2_skip(gbc, 1);
60         g = bytestream2_get_byte(gbc);
61         bytestream2_skip(gbc, 1);
62         b = bytestream2_get_byte(gbc);
63         bytestream2_skip(gbc, 1);
64         pal[idx] = (r << 16) | (g << 8) | b;
65     }
66     return 0;
67 }
68
69 static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
70                       int step)
71 {
72     int i, j;
73     int offset = avctx->width * step;
74     uint8_t *outdata = p->data[0];
75
76     for (i = 0; i < avctx->height; i++) {
77         int size, left, code, pix;
78         uint8_t *out = outdata;
79         int pos = 0;
80
81         /* size of packed line */
82         size = left = bytestream2_get_be16(gbc);
83         if (bytestream2_get_bytes_left(gbc) < size)
84             return AVERROR_INVALIDDATA;
85
86         /* decode line */
87         while (left > 0) {
88             code = bytestream2_get_byte(gbc);
89             if (code & 0x80 ) { /* run */
90                 pix = bytestream2_get_byte(gbc);
91                 for (j = 0; j < 257 - code; j++) {
92                     out[pos] = pix;
93                     pos += step;
94                     if (pos >= offset) {
95                         pos -= offset;
96                         pos++;
97                     }
98                 }
99                 left  -= 2;
100             } else { /* copy */
101                 for (j = 0; j < code + 1; j++) {
102                     out[pos] = bytestream2_get_byte(gbc);
103                     pos += step;
104                     if (pos >= offset) {
105                         pos -= offset;
106                         pos++;
107                     }
108                 }
109                 left  -= 2 + code;
110             }
111         }
112         outdata += p->linesize[0];
113     }
114     return 0;
115 }
116
117 static int decode_frame(AVCodecContext *avctx,
118                         void *data, int *got_frame,
119                         AVPacket *avpkt)
120 {
121     AVFrame * const p      = data;
122     GetByteContext gbc;
123     int colors;
124     int w, h, ret;
125
126     bytestream2_init(&gbc, avpkt->data, avpkt->size);
127
128     /* smallest PICT header */
129     if (bytestream2_get_bytes_left(&gbc) < 40) {
130         av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
131                bytestream2_get_bytes_left(&gbc));
132         return AVERROR_INVALIDDATA;
133     }
134
135     bytestream2_skip(&gbc, 6);
136     h = bytestream2_get_be16(&gbc);
137     w = bytestream2_get_be16(&gbc);
138
139     ret = ff_set_dimensions(avctx, w, h);
140     if (ret < 0)
141         return ret;
142
143     /* version 1 is identified by 0x1101
144      * it uses byte-aligned opcodes rather than word-aligned */
145     if (bytestream2_get_be32(&gbc) != 0x001102FF) {
146         avpriv_request_sample(avctx, "QuickDraw version 1");
147         return AVERROR_PATCHWELCOME;
148     }
149
150     bytestream2_skip(&gbc, 26);
151
152     while (bytestream2_get_bytes_left(&gbc) >= 4) {
153         int bppcnt, bpp;
154         int rowbytes, pack_type;
155         int opcode = bytestream2_get_be16(&gbc);
156
157         switch(opcode) {
158         case PACKBITSRECT:
159         case PACKBITSRGN:
160             av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
161
162             bytestream2_skip(&gbc, 30);
163             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
164             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
165
166             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
167             if (bppcnt == 1 && bpp == 8) {
168                 avctx->pix_fmt = AV_PIX_FMT_PAL8;
169             } else {
170                 av_log(avctx, AV_LOG_ERROR,
171                        "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
172                        bppcnt, bpp);
173                 return AVERROR_INVALIDDATA;
174             }
175
176             /* jump to palette */
177             bytestream2_skip(&gbc, 18);
178             colors = bytestream2_get_be16(&gbc);
179
180             if (colors < 0 || colors > 256) {
181                 av_log(avctx, AV_LOG_ERROR,
182                        "Error color count - %i(0x%X)\n", colors, colors);
183                 return AVERROR_INVALIDDATA;
184             }
185             if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
186                 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
187                        bytestream2_get_bytes_left(&gbc));
188                 return AVERROR_INVALIDDATA;
189             }
190             if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
191                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
192                 return ret;
193             }
194
195             parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
196             p->palette_has_changed = 1;
197
198             /* jump to image data */
199             bytestream2_skip(&gbc, 18);
200
201             if (opcode == PACKBITSRGN) {
202                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
203                 avpriv_report_missing_feature(avctx, "Packbit mask region");
204             }
205
206             ret = decode_rle(avctx, p, &gbc, bppcnt);
207             if (ret < 0)
208                 return ret;
209             *got_frame = 1;
210             break;
211         case DIRECTBITSRECT:
212         case DIRECTBITSRGN:
213             av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
214
215             bytestream2_skip(&gbc, 4);
216             rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
217             if (rowbytes <= 250) {
218                 avpriv_report_missing_feature(avctx, "Short rowbytes");
219                 return AVERROR_PATCHWELCOME;
220             }
221
222             bytestream2_skip(&gbc, 10);
223             pack_type = bytestream2_get_be16(&gbc);
224
225             bytestream2_skip(&gbc, 16);
226             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
227             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
228
229             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
230             if (bppcnt == 3 && bpp == 8) {
231                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
232             } else if (bppcnt == 4 && bpp == 8) {
233                 avctx->pix_fmt = AV_PIX_FMT_ARGB;
234             } else {
235                 av_log(avctx, AV_LOG_ERROR,
236                        "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
237                        bppcnt, bpp);
238                 return AVERROR_INVALIDDATA;
239             }
240
241             /* set packing when default is selected */
242             if (pack_type == 0)
243                 pack_type = bppcnt;
244
245             if (pack_type != 3 && pack_type != 4) {
246                 avpriv_request_sample(avctx, "Pack type %d", pack_type);
247                 return AVERROR_PATCHWELCOME;
248             }
249             if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
250                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
251                 return ret;
252             }
253
254             /* jump to data */
255             bytestream2_skip(&gbc, 30);
256
257             if (opcode == DIRECTBITSRGN) {
258                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
259                 avpriv_report_missing_feature(avctx, "DirectBit mask region");
260             }
261
262             ret = decode_rle(avctx, p, &gbc, bppcnt);
263             if (ret < 0)
264                 return ret;
265             *got_frame = 1;
266             break;
267         default:
268             av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
269             break;
270         }
271         /* exit the loop when a known pixel block has been found */
272         if (*got_frame) {
273             int eop, trail;
274
275             /* re-align to a word */
276             bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
277
278             eop = bytestream2_get_be16(&gbc);
279             trail = bytestream2_get_bytes_left(&gbc);
280             if (eop != EOP)
281                 av_log(avctx, AV_LOG_WARNING,
282                        "Missing end of picture opcode (found 0x%04X)\n", eop);
283             if (trail)
284                 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
285             break;
286         }
287     }
288
289     if (*got_frame) {
290         p->pict_type = AV_PICTURE_TYPE_I;
291         p->key_frame = 1;
292
293         return avpkt->size;
294     } else {
295         av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
296
297         return AVERROR_INVALIDDATA;
298     }
299 }
300
301 AVCodec ff_qdraw_decoder = {
302     .name           = "qdraw",
303     .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
304     .type           = AVMEDIA_TYPE_VIDEO,
305     .id             = AV_CODEC_ID_QDRAW,
306     .decode         = decode_frame,
307     .capabilities   = CODEC_CAP_DR1,
308 };