00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/common.h"
00028 #include "libavutil/intreadwrite.h"
00029 #include "avcodec.h"
00030
00031 typedef struct QdrawContext{
00032 AVCodecContext *avctx;
00033 AVFrame pic;
00034 } QdrawContext;
00035
00036 static int decode_frame(AVCodecContext *avctx,
00037 void *data, int *data_size,
00038 AVPacket *avpkt)
00039 {
00040 const uint8_t *buf = avpkt->data;
00041 const uint8_t *buf_end = avpkt->data + avpkt->size;
00042 int buf_size = avpkt->size;
00043 QdrawContext * const a = avctx->priv_data;
00044 AVFrame * const p = &a->pic;
00045 uint8_t* outdata;
00046 int colors;
00047 int i;
00048 uint32_t *pal;
00049 int r, g, b;
00050
00051 if(p->data[0])
00052 avctx->release_buffer(avctx, p);
00053
00054 p->reference= 0;
00055 if(avctx->get_buffer(avctx, p) < 0){
00056 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00057 return -1;
00058 }
00059 p->pict_type= AV_PICTURE_TYPE_I;
00060 p->key_frame= 1;
00061
00062 outdata = a->pic.data[0];
00063
00064 if (buf_end - buf < 0x68 + 4)
00065 return AVERROR_INVALIDDATA;
00066 buf += 0x68;
00067 colors = AV_RB32(buf);
00068 buf += 4;
00069
00070 if(colors < 0 || colors > 256) {
00071 av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
00072 return -1;
00073 }
00074 if (buf_end - buf < (colors + 1) * 8)
00075 return AVERROR_INVALIDDATA;
00076
00077 pal = (uint32_t*)p->data[1];
00078 for (i = 0; i <= colors; i++) {
00079 unsigned int idx;
00080 idx = AV_RB16(buf);
00081 buf += 2;
00082
00083 if (idx > 255) {
00084 av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
00085 buf += 6;
00086 continue;
00087 }
00088 r = *buf++;
00089 buf++;
00090 g = *buf++;
00091 buf++;
00092 b = *buf++;
00093 buf++;
00094 pal[idx] = 0xFF << 24 | r << 16 | g << 8 | b;
00095 }
00096 p->palette_has_changed = 1;
00097
00098 if (buf_end - buf < 18)
00099 return AVERROR_INVALIDDATA;
00100 buf += 18;
00101 for (i = 0; i < avctx->height; i++) {
00102 int size, left, code, pix;
00103 const uint8_t *next;
00104 uint8_t *out;
00105 int tsize = 0;
00106
00107
00108 out = outdata;
00109 size = AV_RB16(buf);
00110 buf += 2;
00111 if (buf_end - buf < size)
00112 return AVERROR_INVALIDDATA;
00113
00114 left = size;
00115 next = buf + size;
00116 while (left > 0) {
00117 code = *buf++;
00118 if (code & 0x80 ) {
00119 pix = *buf++;
00120 if ((out + (257 - code)) > (outdata + a->pic.linesize[0]))
00121 break;
00122 memset(out, pix, 257 - code);
00123 out += 257 - code;
00124 tsize += 257 - code;
00125 left -= 2;
00126 } else {
00127 if ((out + code) > (outdata + a->pic.linesize[0]))
00128 break;
00129 if (buf_end - buf < code + 1)
00130 return AVERROR_INVALIDDATA;
00131 memcpy(out, buf, code + 1);
00132 out += code + 1;
00133 buf += code + 1;
00134 left -= 2 + code;
00135 tsize += code + 1;
00136 }
00137 }
00138 buf = next;
00139 outdata += a->pic.linesize[0];
00140 }
00141
00142 *data_size = sizeof(AVFrame);
00143 *(AVFrame*)data = a->pic;
00144
00145 return buf_size;
00146 }
00147
00148 static av_cold int decode_init(AVCodecContext *avctx){
00149 QdrawContext * const a = avctx->priv_data;
00150
00151 avcodec_get_frame_defaults(&a->pic);
00152 avctx->pix_fmt= PIX_FMT_PAL8;
00153
00154 return 0;
00155 }
00156
00157 static av_cold int decode_end(AVCodecContext *avctx){
00158 QdrawContext * const a = avctx->priv_data;
00159 AVFrame *pic = &a->pic;
00160
00161 if (pic->data[0])
00162 avctx->release_buffer(avctx, pic);
00163
00164 return 0;
00165 }
00166
00167 AVCodec ff_qdraw_decoder = {
00168 .name = "qdraw",
00169 .type = AVMEDIA_TYPE_VIDEO,
00170 .id = AV_CODEC_ID_QDRAW,
00171 .priv_data_size = sizeof(QdrawContext),
00172 .init = decode_init,
00173 .close = decode_end,
00174 .decode = decode_frame,
00175 .capabilities = CODEC_CAP_DR1,
00176 .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
00177 };