]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
add assert to clarify that we know what we are doing
[ffmpeg] / libavcodec / qdrw.c
1 /*
2  * QuickDraw (qdrw) codec
3  * Copyright (c) 2004 Konstantin Shishkov
4  *
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.
9  *
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.
14  *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  */
20
21 /**
22  * @file qdrw.c
23  * Apple QuickDraw codec.
24  */
25
26 #include "avcodec.h"
27 #include "mpegvideo.h"
28
29 typedef struct QdrawContext{
30     AVCodecContext *avctx;
31     AVFrame pic;
32     uint8_t palette[256*3];
33 } QdrawContext;
34
35 static int decode_frame(AVCodecContext *avctx,
36                         void *data, int *data_size,
37                         uint8_t *buf, int buf_size)
38 {
39     QdrawContext * const a = avctx->priv_data;
40     AVFrame * const p= (AVFrame*)&a->pic;
41     uint8_t* outdata;
42     int colors;
43     int i;
44
45     if(p->data[0])
46         avctx->release_buffer(avctx, p);
47
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;
52     }
53     p->pict_type= I_TYPE;
54     p->key_frame= 1;
55
56     outdata = a->pic.data[0];
57
58     buf += 0x68; /* jump to palette */
59     colors = BE_32(buf);
60     buf += 4;
61
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;
65     }
66
67     for (i = 0; i <= colors; i++) {
68         unsigned int idx;
69         idx = BE_16(buf); /* color index */
70         buf += 2;
71
72         if (idx > 255) {
73             av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
74             buf += 6;
75             continue;
76         }
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++;
83     }
84
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;
91
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];
109                 }
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];
121                 }
122                 left -= 2 + code;
123                 tsize += code + 1;
124             }
125         }
126         buf = next;
127         outdata += a->pic.linesize[0];
128     }
129
130     *data_size = sizeof(AVFrame);
131     *(AVFrame*)data = a->pic;
132
133     return buf_size;
134 }
135
136 static int decode_init(AVCodecContext *avctx){
137 //    QdrawContext * const a = avctx->priv_data;
138
139     if (avcodec_check_dimensions(avctx, avctx->height, avctx->width) < 0) {
140         return 1;
141     }
142
143     avctx->pix_fmt= PIX_FMT_RGB24;
144
145     return 0;
146 }
147
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,
158 };