]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
Add useful Altivec types, taken from with permission from x264 authors
[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     uint8_t palette[256*3];
35 } QdrawContext;
36
37 static int decode_frame(AVCodecContext *avctx,
38                         void *data, int *data_size,
39                         uint8_t *buf, int buf_size)
40 {
41     QdrawContext * const a = avctx->priv_data;
42     AVFrame * const p= (AVFrame*)&a->pic;
43     uint8_t* outdata;
44     int colors;
45     int i;
46
47     if(p->data[0])
48         avctx->release_buffer(avctx, p);
49
50     p->reference= 0;
51     if(avctx->get_buffer(avctx, p) < 0){
52         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
53         return -1;
54     }
55     p->pict_type= I_TYPE;
56     p->key_frame= 1;
57
58     outdata = a->pic.data[0];
59
60     buf += 0x68; /* jump to palette */
61     colors = BE_32(buf);
62     buf += 4;
63
64     if(colors < 0 || colors > 256) {
65         av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
66         return -1;
67     }
68
69     for (i = 0; i <= colors; i++) {
70         unsigned int idx;
71         idx = BE_16(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         a->palette[idx * 3 + 0] = *buf++;
80         buf++;
81         a->palette[idx * 3 + 1] = *buf++;
82         buf++;
83         a->palette[idx * 3 + 2] = *buf++;
84         buf++;
85     }
86
87     buf += 18; /* skip unneeded data */
88     for (i = 0; i < avctx->height; i++) {
89         int size, left, code, pix;
90         uint8_t *next;
91         uint8_t *out;
92         int tsize = 0;
93
94         /* decode line */
95         out = outdata;
96         size = BE_16(buf); /* size of packed line */
97         buf += 2;
98         left = size;
99         next = buf + size;
100         while (left > 0) {
101             code = *buf++;
102             if (code & 0x80 ) { /* run */
103                 int i;
104                 pix = *buf++;
105                 if ((out + (257 - code) * 3) > (outdata +  a->pic.linesize[0]))
106                     break;
107                 for (i = 0; i < 257 - code; i++) {
108                     *out++ = a->palette[pix * 3 + 0];
109                     *out++ = a->palette[pix * 3 + 1];
110                     *out++ = a->palette[pix * 3 + 2];
111                 }
112                 tsize += 257 - code;
113                 left -= 2;
114             } else { /* copy */
115                 int i, pix;
116                 if ((out + code * 3) > (outdata +  a->pic.linesize[0]))
117                     break;
118                 for (i = 0; i <= code; i++) {
119                     pix = *buf++;
120                     *out++ = a->palette[pix * 3 + 0];
121                     *out++ = a->palette[pix * 3 + 1];
122                     *out++ = a->palette[pix * 3 + 2];
123                 }
124                 left -= 2 + code;
125                 tsize += code + 1;
126             }
127         }
128         buf = next;
129         outdata += a->pic.linesize[0];
130     }
131
132     *data_size = sizeof(AVFrame);
133     *(AVFrame*)data = a->pic;
134
135     return buf_size;
136 }
137
138 static int decode_init(AVCodecContext *avctx){
139 //    QdrawContext * const a = avctx->priv_data;
140
141     if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
142         return 1;
143     }
144
145     avctx->pix_fmt= PIX_FMT_RGB24;
146
147     return 0;
148 }
149
150 AVCodec qdraw_decoder = {
151     "qdraw",
152     CODEC_TYPE_VIDEO,
153     CODEC_ID_QDRAW,
154     sizeof(QdrawContext),
155     decode_init,
156     NULL,
157     NULL,
158     decode_frame,
159     CODEC_CAP_DR1,
160 };