Remove even condition on pad and crop.
[ffmpeg-lucabe.git] / libavcodec / qdrw.c
blob0f9609159ccfc64f41dfeefd64861b90431d31e8
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 libavcodec/qdrw.c
24 * Apple QuickDraw codec.
27 #include "libavutil/intreadwrite.h"
28 #include "avcodec.h"
30 typedef struct QdrawContext{
31 AVCodecContext *avctx;
32 AVFrame pic;
33 } QdrawContext;
35 static int decode_frame(AVCodecContext *avctx,
36 void *data, int *data_size,
37 AVPacket *avpkt)
39 const uint8_t *buf = avpkt->data;
40 int buf_size = avpkt->size;
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 uint32_t *pal;
47 int r, g, b;
49 if(p->data[0])
50 avctx->release_buffer(avctx, p);
52 p->reference= 0;
53 if(avctx->get_buffer(avctx, p) < 0){
54 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
55 return -1;
57 p->pict_type= FF_I_TYPE;
58 p->key_frame= 1;
60 outdata = a->pic.data[0];
62 buf += 0x68; /* jump to palette */
63 colors = AV_RB32(buf);
64 buf += 4;
66 if(colors < 0 || colors > 256) {
67 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
68 return -1;
71 pal = (uint32_t*)p->data[1];
72 for (i = 0; i <= colors; i++) {
73 unsigned int idx;
74 idx = AV_RB16(buf); /* color index */
75 buf += 2;
77 if (idx > 255) {
78 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
79 buf += 6;
80 continue;
82 r = *buf++;
83 buf++;
84 g = *buf++;
85 buf++;
86 b = *buf++;
87 buf++;
88 pal[idx] = (r << 16) | (g << 8) | b;
90 p->palette_has_changed = 1;
92 buf += 18; /* skip unneeded data */
93 for (i = 0; i < avctx->height; i++) {
94 int size, left, code, pix;
95 const uint8_t *next;
96 uint8_t *out;
97 int tsize = 0;
99 /* decode line */
100 out = outdata;
101 size = AV_RB16(buf); /* size of packed line */
102 buf += 2;
103 left = size;
104 next = buf + size;
105 while (left > 0) {
106 code = *buf++;
107 if (code & 0x80 ) { /* run */
108 pix = *buf++;
109 if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
110 break;
111 memset(out, pix, 257 - code);
112 out += 257 - code;
113 tsize += 257 - code;
114 left -= 2;
115 } else { /* copy */
116 if ((out + code) > (outdata + a->pic.linesize[0]))
117 break;
118 memcpy(out, buf, code + 1);
119 out += code + 1;
120 buf += code + 1;
121 left -= 2 + code;
122 tsize += code + 1;
125 buf = next;
126 outdata += a->pic.linesize[0];
129 *data_size = sizeof(AVFrame);
130 *(AVFrame*)data = a->pic;
132 return buf_size;
135 static av_cold int decode_init(AVCodecContext *avctx){
136 // QdrawContext * const a = avctx->priv_data;
138 if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0) {
139 return 1;
142 avctx->pix_fmt= PIX_FMT_PAL8;
144 return 0;
147 AVCodec qdraw_decoder = {
148 "qdraw",
149 CODEC_TYPE_VIDEO,
150 CODEC_ID_QDRAW,
151 sizeof(QdrawContext),
152 decode_init,
153 NULL,
154 NULL,
155 decode_frame,
156 CODEC_CAP_DR1,
157 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),