FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 FFmpeg.
7  *
8  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 
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  if (pos >= offset)
99  return AVERROR_INVALIDDATA;
100  }
101  left -= 2;
102  } else { /* copy */
103  for (j = 0; j < code + 1; j++) {
104  out[pos] = bytestream2_get_byte(gbc);
105  pos += step;
106  if (pos >= offset) {
107  pos -= offset;
108  pos++;
109  }
110  if (pos >= offset)
111  return AVERROR_INVALIDDATA;
112  }
113  left -= 2 + code;
114  }
115  }
116  outdata += p->linesize[0];
117  }
118  return 0;
119 }
120 
121 static int check_header(const char *buf, int buf_size)
122 {
123  unsigned w, h, v0, v1;
124 
125  if (buf_size < 40)
126  return 0;
127 
128  w = AV_RB16(buf+6);
129  h = AV_RB16(buf+8);
130  v0 = AV_RB16(buf+10);
131  v1 = AV_RB16(buf+12);
132 
133  if (!w || !h)
134  return 0;
135 
136  if (v0 == 0x1101)
137  return 1;
138  if (v0 == 0x0011 && v1 == 0x02FF)
139  return 2;
140  return 0;
141 }
142 
143 
144 static int decode_frame(AVCodecContext *avctx,
145  void *data, int *got_frame,
146  AVPacket *avpkt)
147 {
148  AVFrame * const p = data;
149  GetByteContext gbc;
150  int colors;
151  int w, h, ret;
152  int ver;
153 
154  bytestream2_init(&gbc, avpkt->data, avpkt->size);
155  if ( bytestream2_get_bytes_left(&gbc) >= 552
156  && check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
157  )
158  bytestream2_skip(&gbc, 512);
159 
161 
162  /* smallest PICT header */
163  if (bytestream2_get_bytes_left(&gbc) < 40) {
164  av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
166  return AVERROR_INVALIDDATA;
167  }
168 
169  bytestream2_skip(&gbc, 6);
170  h = bytestream2_get_be16(&gbc);
171  w = bytestream2_get_be16(&gbc);
172 
173  ret = ff_set_dimensions(avctx, w, h);
174  if (ret < 0)
175  return ret;
176 
177  /* version 1 is identified by 0x1101
178  * it uses byte-aligned opcodes rather than word-aligned */
179  if (ver == 1) {
180  avpriv_request_sample(avctx, "QuickDraw version 1");
181  return AVERROR_PATCHWELCOME;
182  } else if (ver != 2) {
183  avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
184  return AVERROR_PATCHWELCOME;
185  }
186 
187  bytestream2_skip(&gbc, 4+26);
188 
189  while (bytestream2_get_bytes_left(&gbc) >= 4) {
190  int bppcnt, bpp;
191  int rowbytes, pack_type;
192  int opcode = bytestream2_get_be16(&gbc);
193 
194  switch(opcode) {
195  case PACKBITSRECT:
196  case PACKBITSRGN:
197  av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
198 
199  bytestream2_skip(&gbc, 30);
200  bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
201  bpp = bytestream2_get_be16(&gbc); /* cmpSize */
202 
203  av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
204  if (bppcnt == 1 && bpp == 8) {
205  avctx->pix_fmt = AV_PIX_FMT_PAL8;
206  } else {
207  av_log(avctx, AV_LOG_ERROR,
208  "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
209  bppcnt, bpp);
210  return AVERROR_INVALIDDATA;
211  }
212 
213  /* jump to palette */
214  bytestream2_skip(&gbc, 18);
215  colors = bytestream2_get_be16(&gbc);
216 
217  if (colors < 0 || colors > 256) {
218  av_log(avctx, AV_LOG_ERROR,
219  "Error color count - %i(0x%X)\n", colors, colors);
220  return AVERROR_INVALIDDATA;
221  }
222  if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
223  av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
225  return AVERROR_INVALIDDATA;
226  }
227  if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
228  return ret;
229 
230  parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
231  p->palette_has_changed = 1;
232 
233  /* jump to image data */
234  bytestream2_skip(&gbc, 18);
235 
236  if (opcode == PACKBITSRGN) {
237  bytestream2_skip(&gbc, 2 + 8); /* size + rect */
238  avpriv_report_missing_feature(avctx, "Packbit mask region");
239  }
240 
241  ret = decode_rle(avctx, p, &gbc, bppcnt);
242  if (ret < 0)
243  return ret;
244  *got_frame = 1;
245  break;
246  case DIRECTBITSRECT:
247  case DIRECTBITSRGN:
248  av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
249 
250  bytestream2_skip(&gbc, 4);
251  rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
252  if (rowbytes <= 250) {
253  avpriv_report_missing_feature(avctx, "Short rowbytes");
254  return AVERROR_PATCHWELCOME;
255  }
256 
257  bytestream2_skip(&gbc, 10);
258  pack_type = bytestream2_get_be16(&gbc);
259 
260  bytestream2_skip(&gbc, 16);
261  bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
262  bpp = bytestream2_get_be16(&gbc); /* cmpSize */
263 
264  av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
265  if (bppcnt == 3 && bpp == 8) {
266  avctx->pix_fmt = AV_PIX_FMT_RGB24;
267  } else if (bppcnt == 4 && bpp == 8) {
268  avctx->pix_fmt = AV_PIX_FMT_ARGB;
269  } else {
270  av_log(avctx, AV_LOG_ERROR,
271  "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
272  bppcnt, bpp);
273  return AVERROR_INVALIDDATA;
274  }
275 
276  /* set packing when default is selected */
277  if (pack_type == 0)
278  pack_type = bppcnt;
279 
280  if (pack_type != 3 && pack_type != 4) {
281  avpriv_request_sample(avctx, "Pack type %d", pack_type);
282  return AVERROR_PATCHWELCOME;
283  }
284  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
285  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
286  return ret;
287  }
288 
289  /* jump to data */
290  bytestream2_skip(&gbc, 30);
291 
292  if (opcode == DIRECTBITSRGN) {
293  bytestream2_skip(&gbc, 2 + 8); /* size + rect */
294  avpriv_report_missing_feature(avctx, "DirectBit mask region");
295  }
296 
297  ret = decode_rle(avctx, p, &gbc, bppcnt);
298  if (ret < 0)
299  return ret;
300  *got_frame = 1;
301  break;
302  default:
303  av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
304  break;
305  }
306  /* exit the loop when a known pixel block has been found */
307  if (*got_frame) {
308  int eop, trail;
309 
310  /* re-align to a word */
312 
313  eop = bytestream2_get_be16(&gbc);
314  trail = bytestream2_get_bytes_left(&gbc);
315  if (eop != EOP)
316  av_log(avctx, AV_LOG_WARNING,
317  "Missing end of picture opcode (found 0x%04X)\n", eop);
318  if (trail)
319  av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
320  break;
321  }
322  }
323 
324  if (*got_frame) {
326  p->key_frame = 1;
327 
328  return avpkt->size;
329  } else {
330  av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
331 
332  return AVERROR_INVALIDDATA;
333  }
334 }
335 
337  .name = "qdraw",
338  .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
339  .type = AVMEDIA_TYPE_VIDEO,
340  .id = AV_CODEC_ID_QDRAW,
341  .decode = decode_frame,
342  .capabilities = AV_CODEC_CAP_DR1,
343 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:65
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:216
const char * g
Definition: vf_curves.c:108
int size
Definition: avcodec.h:1424
const char * b
Definition: vf_curves.c:109
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1722
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:133
GLfloat v0
Definition: opengl_enc.c:107
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_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
AVCodec.
Definition: avcodec.h:3472
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 bit with AV_PIX_FMT_RGB32 palette
Definition: pixfmt.h:74
AVCodec ff_qdraw_decoder
Definition: qdrw.c:336
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:202
uint8_t * data
Definition: avcodec.h:1423
const uint8_t * buffer
Definition: bytestream.h:34
ptrdiff_t size
Definition: opengl_enc.c:101
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:164
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
const char * r
Definition: vf_curves.c:107
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static av_always_inline unsigned int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:154
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:3479
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qdrw.c:144
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
Libavcodec external API header.
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
Definition: pixfmt.h:94
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:242
int width
picture width / height.
Definition: avcodec.h:1681
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
main external API structure.
Definition: avcodec.h:1502
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:1040
void * buf
Definition: avisynth_c.h:553
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: frame.h:377
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:182
common internal api header.
common internal and external API header
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:237
static int check_header(const char *buf, int buf_size)
Definition: qdrw.c:121
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
This structure stores compressed data.
Definition: avcodec.h:1400
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:857