]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
g726dec: set channel layout at initialization instead of validating it
[ffmpeg] / libavcodec / qdrw.c
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
22 /**
23  * @file
24  * Apple QuickDraw codec.
25  */
26
27 #include "libavutil/common.h"
28 #include "libavutil/intreadwrite.h"
29 #include "avcodec.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                         AVPacket *avpkt)
39 {
40     const uint8_t *buf = avpkt->data;
41     const uint8_t *buf_end = avpkt->data + avpkt->size;
42     int buf_size = avpkt->size;
43     QdrawContext * const a = avctx->priv_data;
44     AVFrame * const p = &a->pic;
45     uint8_t* outdata;
46     int colors;
47     int i;
48     uint32_t *pal;
49     int r, g, b;
50
51     if(p->data[0])
52         avctx->release_buffer(avctx, p);
53
54     p->reference= 0;
55     if(avctx->get_buffer(avctx, p) < 0){
56         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
57         return -1;
58     }
59     p->pict_type= AV_PICTURE_TYPE_I;
60     p->key_frame= 1;
61
62     outdata = a->pic.data[0];
63
64     if (buf_end - buf < 0x68 + 4)
65         return AVERROR_INVALIDDATA;
66     buf += 0x68; /* jump to palette */
67     colors = AV_RB32(buf);
68     buf += 4;
69
70     if(colors < 0 || colors > 256) {
71         av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
72         return -1;
73     }
74     if (buf_end - buf < (colors + 1) * 8)
75         return AVERROR_INVALIDDATA;
76
77     pal = (uint32_t*)p->data[1];
78     for (i = 0; i <= colors; i++) {
79         unsigned int idx;
80         idx = AV_RB16(buf); /* color index */
81         buf += 2;
82
83         if (idx > 255) {
84             av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
85             buf += 6;
86             continue;
87         }
88         r = *buf++;
89         buf++;
90         g = *buf++;
91         buf++;
92         b = *buf++;
93         buf++;
94         pal[idx] = (r << 16) | (g << 8) | b;
95     }
96     p->palette_has_changed = 1;
97
98     if (buf_end - buf < 18)
99         return AVERROR_INVALIDDATA;
100     buf += 18; /* skip unneeded data */
101     for (i = 0; i < avctx->height; i++) {
102         int size, left, code, pix;
103         const uint8_t *next;
104         uint8_t *out;
105         int tsize = 0;
106
107         /* decode line */
108         out = outdata;
109         size = AV_RB16(buf); /* size of packed line */
110         buf += 2;
111         if (buf_end - buf < size)
112             return AVERROR_INVALIDDATA;
113
114         left = size;
115         next = buf + size;
116         while (left > 0) {
117             code = *buf++;
118             if (code & 0x80 ) { /* run */
119                 pix = *buf++;
120                 if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
121                     break;
122                 memset(out, pix, 257 - code);
123                 out += 257 - code;
124                 tsize += 257 - code;
125                 left -= 2;
126             } else { /* copy */
127                 if ((out + code) > (outdata +  a->pic.linesize[0]))
128                     break;
129                 if (buf_end - buf < code + 1)
130                     return AVERROR_INVALIDDATA;
131                 memcpy(out, buf, code + 1);
132                 out += code + 1;
133                 buf += code + 1;
134                 left -= 2 + code;
135                 tsize += code + 1;
136             }
137         }
138         buf = next;
139         outdata += a->pic.linesize[0];
140     }
141
142     *data_size = sizeof(AVFrame);
143     *(AVFrame*)data = a->pic;
144
145     return buf_size;
146 }
147
148 static av_cold int decode_init(AVCodecContext *avctx){
149 //    QdrawContext * const a = avctx->priv_data;
150
151     avctx->pix_fmt= AV_PIX_FMT_PAL8;
152
153     return 0;
154 }
155
156 static av_cold int decode_end(AVCodecContext *avctx){
157     QdrawContext * const a = avctx->priv_data;
158     AVFrame *pic = &a->pic;
159
160     if (pic->data[0])
161         avctx->release_buffer(avctx, pic);
162
163     return 0;
164 }
165
166 AVCodec ff_qdraw_decoder = {
167     .name           = "qdraw",
168     .type           = AVMEDIA_TYPE_VIDEO,
169     .id             = AV_CODEC_ID_QDRAW,
170     .priv_data_size = sizeof(QdrawContext),
171     .init           = decode_init,
172     .close          = decode_end,
173     .decode         = decode_frame,
174     .capabilities   = CODEC_CAP_DR1,
175     .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
176 };