Libav
qdrw.c
Go to the documentation of this file.
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 
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "internal.h"
34 
36  PACKBITSRECT = 0x0098,
40 
41  EOP = 0x00FF,
42 };
43 
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] = (0xFFU << 24) | (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  /* PICT images start with a 512 bytes empty header */
129  if (bytestream2_peek_be32(&gbc) == 0)
130  bytestream2_skip(&gbc, 512);
131 
132  /* smallest PICT header */
133  if (bytestream2_get_bytes_left(&gbc) < 40) {
134  av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
136  return AVERROR_INVALIDDATA;
137  }
138 
139  bytestream2_skip(&gbc, 6);
140  h = bytestream2_get_be16(&gbc);
141  w = bytestream2_get_be16(&gbc);
142 
143  ret = ff_set_dimensions(avctx, w, h);
144  if (ret < 0)
145  return ret;
146 
147  /* version 1 is identified by 0x1101
148  * it uses byte-aligned opcodes rather than word-aligned */
149  if (bytestream2_get_be32(&gbc) != 0x001102FF) {
150  avpriv_request_sample(avctx, "QuickDraw version 1");
151  return AVERROR_PATCHWELCOME;
152  }
153 
154  bytestream2_skip(&gbc, 26);
155 
156  while (bytestream2_get_bytes_left(&gbc) >= 4) {
157  int bppcnt, bpp;
158  int rowbytes, pack_type;
159  int opcode = bytestream2_get_be16(&gbc);
160 
161  switch(opcode) {
162  case PACKBITSRECT:
163  case PACKBITSRGN:
164  av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
165 
166  bytestream2_skip(&gbc, 30);
167  bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
168  bpp = bytestream2_get_be16(&gbc); /* cmpSize */
169 
170  av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
171  if (bppcnt == 1 && bpp == 8) {
172  avctx->pix_fmt = AV_PIX_FMT_PAL8;
173  } else {
174  av_log(avctx, AV_LOG_ERROR,
175  "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
176  bppcnt, bpp);
177  return AVERROR_INVALIDDATA;
178  }
179 
180  /* jump to palette */
181  bytestream2_skip(&gbc, 18);
182  colors = bytestream2_get_be16(&gbc);
183 
184  if (colors < 0 || colors > 256) {
185  av_log(avctx, AV_LOG_ERROR,
186  "Error color count - %i(0x%X)\n", colors, colors);
187  return AVERROR_INVALIDDATA;
188  }
189  if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
190  av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
192  return AVERROR_INVALIDDATA;
193  }
194  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
195  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
196  return ret;
197  }
198 
199  parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
200  p->palette_has_changed = 1;
201 
202  /* jump to image data */
203  bytestream2_skip(&gbc, 18);
204 
205  if (opcode == PACKBITSRGN) {
206  bytestream2_skip(&gbc, 2 + 8); /* size + rect */
207  avpriv_report_missing_feature(avctx, "Packbit mask region");
208  }
209 
210  ret = decode_rle(avctx, p, &gbc, bppcnt);
211  if (ret < 0)
212  return ret;
213  *got_frame = 1;
214  break;
215  case DIRECTBITSRECT:
216  case DIRECTBITSRGN:
217  av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
218 
219  bytestream2_skip(&gbc, 4);
220  rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
221  if (rowbytes <= 250) {
222  avpriv_report_missing_feature(avctx, "Short rowbytes");
223  return AVERROR_PATCHWELCOME;
224  }
225 
226  bytestream2_skip(&gbc, 10);
227  pack_type = bytestream2_get_be16(&gbc);
228 
229  bytestream2_skip(&gbc, 16);
230  bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
231  bpp = bytestream2_get_be16(&gbc); /* cmpSize */
232 
233  av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
234  if (bppcnt == 3 && bpp == 8) {
235  avctx->pix_fmt = AV_PIX_FMT_RGB24;
236  } else if (bppcnt == 4 && bpp == 8) {
237  avctx->pix_fmt = AV_PIX_FMT_ARGB;
238  } else {
239  av_log(avctx, AV_LOG_ERROR,
240  "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
241  bppcnt, bpp);
242  return AVERROR_INVALIDDATA;
243  }
244 
245  /* set packing when default is selected */
246  if (pack_type == 0)
247  pack_type = bppcnt;
248 
249  if (pack_type != 3 && pack_type != 4) {
250  avpriv_request_sample(avctx, "Pack type %d", pack_type);
251  return AVERROR_PATCHWELCOME;
252  }
253  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
254  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
255  return ret;
256  }
257 
258  /* jump to data */
259  bytestream2_skip(&gbc, 30);
260 
261  if (opcode == DIRECTBITSRGN) {
262  bytestream2_skip(&gbc, 2 + 8); /* size + rect */
263  avpriv_report_missing_feature(avctx, "DirectBit mask region");
264  }
265 
266  ret = decode_rle(avctx, p, &gbc, bppcnt);
267  if (ret < 0)
268  return ret;
269  *got_frame = 1;
270  break;
271  default:
272  av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
273  break;
274  }
275  /* exit the loop when a known pixel block has been found */
276  if (*got_frame) {
277  int eop, trail;
278 
279  /* re-align to a word */
281 
282  eop = bytestream2_get_be16(&gbc);
283  trail = bytestream2_get_bytes_left(&gbc);
284  if (eop != EOP)
285  av_log(avctx, AV_LOG_WARNING,
286  "Missing end of picture opcode (found 0x%04X)\n", eop);
287  if (trail)
288  av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
289  break;
290  }
291  }
292 
293  if (*got_frame) {
295  p->key_frame = 1;
296 
297  return avpkt->size;
298  } else {
299  av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
300 
301  return AVERROR_INVALIDDATA;
302  }
303 }
304 
306  .name = "qdraw",
307  .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
308  .type = AVMEDIA_TYPE_VIDEO,
309  .id = AV_CODEC_ID_QDRAW,
310  .decode = decode_frame,
311  .capabilities = AV_CODEC_CAP_DR1,
312 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
int size
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
Definition: vf_drawbox.c:37
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:61
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:134
int size
Definition: avcodec.h:1347
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:132
AVCodec.
Definition: avcodec.h:3120
Definition: qdrw.c:41
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc, uint32_t *pal, int colors)
Definition: qdrw.c:44
uint8_t
8 bits with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:70
AVCodec ff_qdraw_decoder
Definition: qdrw.c:305
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
#define b
Definition: input.c:52
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
#define r
Definition: input.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:161
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
g
Definition: yuv2rgb.c:546
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:151
static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc, int step)
Definition: qdrw.c:69
const char * name
Name of the codec implementation.
Definition: avcodec.h:3127
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qdrw.c:117
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:89
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:201
int width
picture width / height.
Definition: avcodec.h:1580
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
main external API structure.
Definition: avcodec.h:1409
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:589
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:273
static int step
Definition: avplay.c:247
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
QuickdrawOpcodes
Definition: qdrw.c:35
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
common internal api header.
common internal and external API header
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:196
FILE * out
Definition: movenc.c:54
This structure stores compressed data.
Definition: avcodec.h:1323
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:838