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 typedef struct QdrawContext{
35 } QdrawContext;
36 
37 static int decode_frame(AVCodecContext *avctx,
38  void *data, int *got_frame,
39  AVPacket *avpkt)
40 {
41  const uint8_t *buf = avpkt->data;
42  const uint8_t *buf_end = avpkt->data + avpkt->size;
43  int buf_size = avpkt->size;
44  QdrawContext * const a = avctx->priv_data;
45  AVFrame * const p = &a->pic;
46  uint8_t* outdata;
47  int colors;
48  int i;
49  uint32_t *pal;
50  int r, g, b;
51 
52  if(p->data[0])
53  avctx->release_buffer(avctx, p);
54 
55  p->reference= 0;
56  if(ff_get_buffer(avctx, p) < 0){
57  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
58  return -1;
59  }
61  p->key_frame= 1;
62 
63  outdata = a->pic.data[0];
64 
65  if (buf_end - buf < 0x68 + 4)
66  return AVERROR_INVALIDDATA;
67  buf += 0x68; /* jump to palette */
68  colors = AV_RB32(buf);
69  buf += 4;
70 
71  if(colors < 0 || colors > 256) {
72  av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
73  return -1;
74  }
75  if (buf_end - buf < (colors + 1) * 8)
76  return AVERROR_INVALIDDATA;
77 
78  pal = (uint32_t*)p->data[1];
79  for (i = 0; i <= colors; i++) {
80  unsigned int idx;
81  idx = AV_RB16(buf); /* color index */
82  buf += 2;
83 
84  if (idx > 255) {
85  av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
86  buf += 6;
87  continue;
88  }
89  r = *buf++;
90  buf++;
91  g = *buf++;
92  buf++;
93  b = *buf++;
94  buf++;
95  pal[idx] = (r << 16) | (g << 8) | b;
96  }
97  p->palette_has_changed = 1;
98 
99  if (buf_end - buf < 18)
100  return AVERROR_INVALIDDATA;
101  buf += 18; /* skip unneeded data */
102  for (i = 0; i < avctx->height; i++) {
103  int size, left, code, pix;
104  const uint8_t *next;
105  uint8_t *out;
106  int tsize = 0;
107 
108  /* decode line */
109  out = outdata;
110  size = AV_RB16(buf); /* size of packed line */
111  buf += 2;
112  if (buf_end - buf < size)
113  return AVERROR_INVALIDDATA;
114 
115  left = size;
116  next = buf + size;
117  while (left > 0) {
118  code = *buf++;
119  if (code & 0x80 ) { /* run */
120  pix = *buf++;
121  if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
122  break;
123  memset(out, pix, 257 - code);
124  out += 257 - code;
125  tsize += 257 - code;
126  left -= 2;
127  } else { /* copy */
128  if ((out + code) > (outdata + a->pic.linesize[0]))
129  break;
130  if (buf_end - buf < code + 1)
131  return AVERROR_INVALIDDATA;
132  memcpy(out, buf, code + 1);
133  out += code + 1;
134  buf += code + 1;
135  left -= 2 + code;
136  tsize += code + 1;
137  }
138  }
139  buf = next;
140  outdata += a->pic.linesize[0];
141  }
142 
143  *got_frame = 1;
144  *(AVFrame*)data = a->pic;
145 
146  return buf_size;
147 }
148 
149 static av_cold int decode_init(AVCodecContext *avctx){
150 // QdrawContext * const a = avctx->priv_data;
151 
152  avctx->pix_fmt= AV_PIX_FMT_PAL8;
153 
154  return 0;
155 }
156 
157 static av_cold int decode_end(AVCodecContext *avctx){
158  QdrawContext * const a = avctx->priv_data;
159  AVFrame *pic = &a->pic;
160 
161  if (pic->data[0])
162  avctx->release_buffer(avctx, pic);
163 
164  return 0;
165 }
166 
168  .name = "qdraw",
169  .type = AVMEDIA_TYPE_VIDEO,
170  .id = AV_CODEC_ID_QDRAW,
171  .priv_data_size = sizeof(QdrawContext),
172  .init = decode_init,
173  .close = decode_end,
174  .decode = decode_frame,
175  .capabilities = CODEC_CAP_DR1,
176  .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
177 };
int size
static av_cold int decode_init(AVCodecContext *avctx)
Definition: qdrw.c:149
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2259
int size
Definition: avcodec.h:916
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
AVCodec.
Definition: avcodec.h:2960
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:228
struct QdrawContext QdrawContext
uint8_t
8 bit with PIX_FMT_RGB32 palette
Definition: pixfmt.h:76
AVCodec ff_qdraw_decoder
Definition: qdrw.c:167
#define AV_RB32
Definition: intreadwrite.h:130
#define b
Definition: input.c:52
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
AVCodecContext * avctx
Definition: qdrw.c:33
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
#define r
Definition: input.c:51
int reference
is this picture used as reference The values for this are the same as the MpegEncContext.picture_structure variable, that is 1->top field, 2->bottom field, 3->frame/both fields.
Definition: avcodec.h:1132
#define AV_RB16
Definition: intreadwrite.h:53
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
g
Definition: yuv2rgb.c:540
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: qdrw.c:37
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Definition: avcodec.h:1065
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame)
Get a buffer for a frame.
Definition: utils.c:464
external API header
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
int palette_has_changed
Tell user application that palette has changed from previous frame.
Definition: avcodec.h:1246
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
AVFrame pic
Definition: qdrw.c:34
common internal api header.
common internal and external API header
void * priv_data
Definition: avcodec.h:1382
int key_frame
1 -> keyframe, 0-> not
Definition: avcodec.h:1058
static av_cold int decode_end(AVCodecContext *avctx)
Definition: qdrw.c:157
This structure stores compressed data.
Definition: avcodec.h:898
for(j=16;j >0;--j)