Merge branch 'master' of http://repo.or.cz/r/FFMpeg-mirror
[FFMpeg-mirror/DVCPRO-HD.git] / libavcodec / qdrw.c
blobe0e4142b81fd5ef36cd27ef97bae1f6834423bd5
1 /*
2 * QuickDraw (qdrw) codec
3 * Copyright (c) 2004 Konstantin Shishkov
5 * This file is part of FFmpeg.
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.
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.
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
22 /**
23 * @file qdrw.c
24 * Apple QuickDraw codec.
27 #include "avcodec.h"
29 typedef struct QdrawContext{
30 AVCodecContext *avctx;
31 AVFrame pic;
32 } QdrawContext;
34 static int decode_frame(AVCodecContext *avctx,
35 void *data, int *data_size,
36 const uint8_t *buf, int buf_size)
38 QdrawContext * const a = avctx->priv_data;
39 AVFrame * const p= (AVFrame*)&a->pic;
40 uint8_t* outdata;
41 int colors;
42 int i;
43 uint32_t *pal;
44 int r, g, b;
46 if(p->data[0])
47 avctx->release_buffer(avctx, p);
49 p->reference= 0;
50 if(avctx->get_buffer(avctx, p) < 0){
51 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
52 return -1;
54 p->pict_type= FF_I_TYPE;
55 p->key_frame= 1;
57 outdata = a->pic.data[0];
59 buf += 0x68; /* jump to palette */
60 colors = AV_RB32(buf);
61 buf += 4;
63 if(colors < 0 || colors > 256) {
64 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
65 return -1;
68 pal = (uint32_t*)p->data[1];
69 for (i = 0; i <= colors; i++) {
70 unsigned int idx;
71 idx = AV_RB16(buf); /* color index */
72 buf += 2;
74 if (idx > 255) {
75 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
76 buf += 6;
77 continue;
79 r = *buf++;
80 buf++;
81 g = *buf++;
82 buf++;
83 b = *buf++;
84 buf++;
85 pal[idx] = (r << 16) | (g << 8) | b;
87 p->palette_has_changed = 1;
89 buf += 18; /* skip unneeded data */
90 for (i = 0; i < avctx->height; i++) {
91 int size, left, code, pix;
92 const uint8_t *next;
93 uint8_t *out;
94 int tsize = 0;
96 /* decode line */
97 out = outdata;
98 size = AV_RB16(buf); /* size of packed line */
99 buf += 2;
100 left = size;
101 next = buf + size;
102 while (left > 0) {
103 code = *buf++;
104 if (code & 0x80 ) { /* run */
105 pix = *buf++;
106 if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
107 break;
108 memset(out, pix, 257 - code);
109 out += 257 - code;
110 tsize += 257 - code;
111 left -= 2;
112 } else { /* copy */
113 if ((out + code) > (outdata + a->pic.linesize[0]))
114 break;
115 memcpy(out, buf, code + 1);
116 out += code + 1;
117 buf += code + 1;
118 left -= 2 + code;
119 tsize += code + 1;
122 buf = next;
123 outdata += a->pic.linesize[0];
126 *data_size = sizeof(AVFrame);
127 *(AVFrame*)data = a->pic;
129 return buf_size;
132 static av_cold int decode_init(AVCodecContext *avctx){
133 // QdrawContext * const a = avctx->priv_data;
135 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
136 return 1;
139 avctx->pix_fmt= PIX_FMT_PAL8;
141 return 0;
144 AVCodec qdraw_decoder = {
145 "qdraw",
146 CODEC_TYPE_VIDEO,
147 CODEC_ID_QDRAW,
148 sizeof(QdrawContext),
149 decode_init,
150 NULL,
151 NULL,
152 decode_frame,
153 CODEC_CAP_DR1,
154 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),