Libav
qdrw.c
Go to the documentation of this file.
1 /*
2  * QuickDraw (qdrw) codec
3  * Copyright (c) 2004 Konstantin Shishkov
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 
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avcodec.h"
30 #include "internal.h"
31 
32 static int decode_frame(AVCodecContext *avctx,
33  void *data, int *got_frame,
34  AVPacket *avpkt)
35 {
36  const uint8_t *buf = avpkt->data;
37  const uint8_t *buf_end = avpkt->data + avpkt->size;
38  int buf_size = avpkt->size;
39  AVFrame * const p = data;
40  uint8_t* outdata;
41  int colors;
42  int i, ret;
43  uint32_t *pal;
44  int r, g, b;
45 
46  if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
47  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
48  return ret;
49  }
51  p->key_frame = 1;
52 
53  outdata = p->data[0];
54 
55  if (buf_end - buf < 0x68 + 4)
56  return AVERROR_INVALIDDATA;
57  buf += 0x68; /* jump to palette */
58  colors = AV_RB32(buf);
59  buf += 4;
60 
61  if (colors < 0 || colors > 256) {
62  av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
63  return AVERROR_INVALIDDATA;
64  }
65  if (buf_end - buf < (colors + 1) * 8)
66  return AVERROR_INVALIDDATA;
67 
68  pal = (uint32_t*)p->data[1];
69  for (i = 0; i <= colors; i++) {
70  unsigned int idx;
71  idx = AV_RB16(buf); /* color index */
72  buf += 2;
73 
74  if (idx > 255) {
75  av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
76  buf += 6;
77  continue;
78  }
79  r = *buf++;
80  buf++;
81  g = *buf++;
82  buf++;
83  b = *buf++;
84  buf++;
85  pal[idx] = (r << 16) | (g << 8) | b;
86  }
87  p->palette_has_changed = 1;
88 
89  if (buf_end - buf < 18)
90  return AVERROR_INVALIDDATA;
91  buf += 18; /* skip unneeded data */
92  for (i = 0; i < avctx->height; i++) {
93  int size, left, code, pix;
94  const uint8_t *next;
95  uint8_t *out;
96  int tsize = 0;
97 
98  /* decode line */
99  out = outdata;
100  size = AV_RB16(buf); /* size of packed line */
101  buf += 2;
102  if (buf_end - buf < size)
103  return AVERROR_INVALIDDATA;
104 
105  left = size;
106  next = buf + size;
107  while (left > 0) {
108  code = *buf++;
109  if (code & 0x80 ) { /* run */
110  pix = *buf++;
111  if ((out + (257 - code)) > (outdata + p->linesize[0]))
112  break;
113  memset(out, pix, 257 - code);
114  out += 257 - code;
115  tsize += 257 - code;
116  left -= 2;
117  } else { /* copy */
118  if ((out + code) > (outdata + p->linesize[0]))
119  break;
120  if (buf_end - buf < code + 1)
121  return AVERROR_INVALIDDATA;
122  memcpy(out, buf, code + 1);
123  out += code + 1;
124  buf += code + 1;
125  left -= 2 + code;
126  tsize += code + 1;
127  }
128  }
129  buf = next;
130  outdata += p->linesize[0];
131  }
132 
133  *got_frame = 1;
134 
135  return buf_size;
136 }
137 
139 {
140  avctx->pix_fmt= AV_PIX_FMT_PAL8;
141 
142  return 0;
143 }
144 
146  .name = "qdraw",
147  .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
148  .type = AVMEDIA_TYPE_VIDEO,
149  .id = AV_CODEC_ID_QDRAW,
150  .init = decode_init,
151  .decode = decode_frame,
152  .capabilities = CODEC_CAP_DR1,
153 };