]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
This codec is PAL8 so make it output PAL8 too
[ffmpeg] / libavcodec / qdrw.c
1 /*
2  * QuickDraw (qdrw) codec
3  * Copyright (c) 2004 Konstantin Shishkov
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  */
22
23 /**
24  * @file qdrw.c
25  * Apple QuickDraw codec.
26  */
27
28 #include "avcodec.h"
29 #include "mpegvideo.h"
30
31 typedef struct QdrawContext{
32     AVCodecContext *avctx;
33     AVFrame pic;
34 } QdrawContext;
35
36 static int decode_frame(AVCodecContext *avctx,
37                         void *data, int *data_size,
38                         uint8_t *buf, int buf_size)
39 {
40     QdrawContext * const a = avctx->priv_data;
41     AVFrame * const p= (AVFrame*)&a->pic;
42     uint8_t* outdata;
43     int colors;
44     int i;
45     uint32_t *pal;
46     int r, g, b;
47
48     if(p->data[0])
49         avctx->release_buffer(avctx, p);
50
51     p->reference= 0;
52     if(avctx->get_buffer(avctx, p) < 0){
53         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
54         return -1;
55     }
56     p->pict_type= I_TYPE;
57     p->key_frame= 1;
58
59     outdata = a->pic.data[0];
60
61     buf += 0x68; /* jump to palette */
62     colors = AV_RB32(buf);
63     buf += 4;
64
65     if(colors < 0 || colors > 256) {
66         av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
67         return -1;
68     }
69
70     pal = (uint32_t*)p->data[1];
71     for (i = 0; i <= colors; i++) {
72         unsigned int idx;
73         idx = AV_RB16(buf); /* color index */
74         buf += 2;
75
76         if (idx > 255) {
77             av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
78             buf += 6;
79             continue;
80         }
81         r = *buf++;
82         buf++;
83         g = *buf++;
84         buf++;
85         b = *buf++;
86         buf++;
87         pal[idx] = (r << 16) | (g << 8) | b;
88     }
89     p->palette_has_changed = 1;
90
91     buf += 18; /* skip unneeded data */
92     for (i = 0; i < avctx->height; i++) {
93         int size, left, code, pix;
94         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         left = size;
103         next = buf + size;
104         while (left > 0) {
105             code = *buf++;
106             if (code & 0x80 ) { /* run */
107                 pix = *buf++;
108                 if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
109                     break;
110                 memset(out, pix, 257 - code);
111                 out += 257 - code;
112                 tsize += 257 - code;
113                 left -= 2;
114             } else { /* copy */
115                 if ((out + code) > (outdata +  a->pic.linesize[0]))
116                     break;
117                 memcpy(out, buf, code + 1);
118                 out += code + 1;
119                 buf += code + 1;
120                 left -= 2 + code;
121                 tsize += code + 1;
122             }
123         }
124         buf = next;
125         outdata += a->pic.linesize[0];
126     }
127
128     *data_size = sizeof(AVFrame);
129     *(AVFrame*)data = a->pic;
130
131     return buf_size;
132 }
133
134 static int decode_init(AVCodecContext *avctx){
135 //    QdrawContext * const a = avctx->priv_data;
136
137     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
138         return 1;
139     }
140
141     avctx->pix_fmt= PIX_FMT_PAL8;
142
143     return 0;
144 }
145
146 AVCodec qdraw_decoder = {
147     "qdraw",
148     CODEC_TYPE_VIDEO,
149     CODEC_ID_QDRAW,
150     sizeof(QdrawContext),
151     decode_init,
152     NULL,
153     NULL,
154     decode_frame,
155     CODEC_CAP_DR1,
156 };