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