r955: Fix the Diffkey icon.
[cinelerra_cv.git] / quicktime / ffmpeg / libavcodec / qdrw.c
bloba12d4506792c2ef7b0f66c65c35b4ec92a84f383
1 /*
2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /**
22 * @file qdrw.c
23 * Apple QuickDraw codec.
26 #include "avcodec.h"
27 #include "mpegvideo.h"
29 typedef struct QdrawContext{
30 AVCodecContext *avctx;
31 AVFrame pic;
32 uint8_t palette[256*3];
33 } QdrawContext;
35 static int decode_frame(AVCodecContext *avctx,
36 void *data, int *data_size,
37 uint8_t *buf, int buf_size)
39 QdrawContext * const a = avctx->priv_data;
40 AVFrame * const p= (AVFrame*)&a->pic;
41 uint8_t* outdata;
42 int colors;
43 int i;
45 if(p->data[0])
46 avctx->release_buffer(avctx, p);
48 p->reference= 0;
49 if(avctx->get_buffer(avctx, p) < 0){
50 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
51 return -1;
53 p->pict_type= I_TYPE;
54 p->key_frame= 1;
56 outdata = a->pic.data[0];
58 buf += 0x68; /* jump to palette */
59 colors = BE_32(buf);
60 buf += 4;
62 if(colors < 0 || colors > 256) {
63 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
64 return -1;
67 for (i = 0; i <= colors; i++) {
68 unsigned int idx;
69 idx = BE_16(buf); /* color index */
70 buf += 2;
72 if (idx > 255) {
73 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
74 buf += 6;
75 continue;
77 a->palette[idx * 3 + 0] = *buf++;
78 buf++;
79 a->palette[idx * 3 + 1] = *buf++;
80 buf++;
81 a->palette[idx * 3 + 2] = *buf++;
82 buf++;
85 buf += 18; /* skip unneeded data */
86 for (i = 0; i < avctx->height; i++) {
87 int size, left, code, pix;
88 uint8_t *next;
89 uint8_t *out;
90 int tsize = 0;
92 /* decode line */
93 out = outdata;
94 size = BE_16(buf); /* size of packed line */
95 buf += 2;
96 left = size;
97 next = buf + size;
98 while (left > 0) {
99 code = *buf++;
100 if (code & 0x80 ) { /* run */
101 int i;
102 pix = *buf++;
103 if ((out + (257 - code) * 3) > (outdata + a->pic.linesize[0]))
104 break;
105 for (i = 0; i < 257 - code; i++) {
106 *out++ = a->palette[pix * 3 + 0];
107 *out++ = a->palette[pix * 3 + 1];
108 *out++ = a->palette[pix * 3 + 2];
110 tsize += 257 - code;
111 left -= 2;
112 } else { /* copy */
113 int i, pix;
114 if ((out + code * 3) > (outdata + a->pic.linesize[0]))
115 break;
116 for (i = 0; i <= code; i++) {
117 pix = *buf++;
118 *out++ = a->palette[pix * 3 + 0];
119 *out++ = a->palette[pix * 3 + 1];
120 *out++ = a->palette[pix * 3 + 2];
122 left -= 2 + code;
123 tsize += code + 1;
126 buf = next;
127 outdata += a->pic.linesize[0];
130 *data_size = sizeof(AVFrame);
131 *(AVFrame*)data = a->pic;
133 return buf_size;
136 static int decode_init(AVCodecContext *avctx){
137 // QdrawContext * const a = avctx->priv_data;
139 if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
140 return 1;
143 avctx->pix_fmt= PIX_FMT_RGB24;
145 return 0;
148 AVCodec qdraw_decoder = {
149 "qdraw",
150 CODEC_TYPE_VIDEO,
151 CODEC_ID_QDRAW,
152 sizeof(QdrawContext),
153 decode_init,
154 NULL,
155 NULL,
156 decode_frame,
157 CODEC_CAP_DR1,