libavcodec/qdrw.c
Go to the documentation of this file.
00001 /*
00002  * QuickDraw (qdrw) codec
00003  * Copyright (c) 2004 Konstantin Shishkov
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00027 #include "libavutil/intreadwrite.h"
00028 #include "avcodec.h"
00029 
00030 typedef struct QdrawContext{
00031     AVCodecContext *avctx;
00032     AVFrame pic;
00033 } QdrawContext;
00034 
00035 static int decode_frame(AVCodecContext *avctx,
00036                         void *data, int *data_size,
00037                         AVPacket *avpkt)
00038 {
00039     const uint8_t *buf = avpkt->data;
00040     const uint8_t *buf_end = avpkt->data + avpkt->size;
00041     int buf_size = avpkt->size;
00042     QdrawContext * const a = avctx->priv_data;
00043     AVFrame * const p= (AVFrame*)&a->pic;
00044     uint8_t* outdata;
00045     int colors;
00046     int i;
00047     uint32_t *pal;
00048     int r, g, b;
00049 
00050     if(p->data[0])
00051         avctx->release_buffer(avctx, p);
00052 
00053     p->reference= 0;
00054     if(avctx->get_buffer(avctx, p) < 0){
00055         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00056         return -1;
00057     }
00058     p->pict_type= AV_PICTURE_TYPE_I;
00059     p->key_frame= 1;
00060 
00061     outdata = a->pic.data[0];
00062 
00063     if (buf_end - buf < 0x68 + 4)
00064         return AVERROR_INVALIDDATA;
00065     buf += 0x68; /* jump to palette */
00066     colors = AV_RB32(buf);
00067     buf += 4;
00068 
00069     if(colors < 0 || colors > 256) {
00070         av_log(avctx, AV_LOG_ERROR, "Error color count - %i(0x%X)\n", colors, colors);
00071         return -1;
00072     }
00073     if (buf_end - buf < (colors + 1) * 8)
00074         return AVERROR_INVALIDDATA;
00075 
00076     pal = (uint32_t*)p->data[1];
00077     for (i = 0; i <= colors; i++) {
00078         unsigned int idx;
00079         idx = AV_RB16(buf); /* color index */
00080         buf += 2;
00081 
00082         if (idx > 255) {
00083             av_log(avctx, AV_LOG_ERROR, "Palette index out of range: %u\n", idx);
00084             buf += 6;
00085             continue;
00086         }
00087         r = *buf++;
00088         buf++;
00089         g = *buf++;
00090         buf++;
00091         b = *buf++;
00092         buf++;
00093         pal[idx] = 0xFF << 24 | r << 16 | g << 8 | b;
00094     }
00095     p->palette_has_changed = 1;
00096 
00097     if (buf_end - buf < 18)
00098         return AVERROR_INVALIDDATA;
00099     buf += 18; /* skip unneeded data */
00100     for (i = 0; i < avctx->height; i++) {
00101         int size, left, code, pix;
00102         const uint8_t *next;
00103         uint8_t *out;
00104         int tsize = 0;
00105 
00106         /* decode line */
00107         out = outdata;
00108         size = AV_RB16(buf); /* size of packed line */
00109         buf += 2;
00110         if (buf_end - buf < size)
00111             return AVERROR_INVALIDDATA;
00112 
00113         left = size;
00114         next = buf + size;
00115         while (left > 0) {
00116             code = *buf++;
00117             if (code & 0x80 ) { /* run */
00118                 pix = *buf++;
00119                 if ((out + (257 - code)) > (outdata +  a->pic.linesize[0]))
00120                     break;
00121                 memset(out, pix, 257 - code);
00122                 out += 257 - code;
00123                 tsize += 257 - code;
00124                 left -= 2;
00125             } else { /* copy */
00126                 if ((out + code) > (outdata +  a->pic.linesize[0]))
00127                     break;
00128                 if (buf_end - buf < code + 1)
00129                     return AVERROR_INVALIDDATA;
00130                 memcpy(out, buf, code + 1);
00131                 out += code + 1;
00132                 buf += code + 1;
00133                 left -= 2 + code;
00134                 tsize += code + 1;
00135             }
00136         }
00137         buf = next;
00138         outdata += a->pic.linesize[0];
00139     }
00140 
00141     *data_size = sizeof(AVFrame);
00142     *(AVFrame*)data = a->pic;
00143 
00144     return buf_size;
00145 }
00146 
00147 static av_cold int decode_init(AVCodecContext *avctx){
00148     QdrawContext * const a = avctx->priv_data;
00149 
00150     avcodec_get_frame_defaults(&a->pic);
00151     avctx->pix_fmt= PIX_FMT_PAL8;
00152 
00153     return 0;
00154 }
00155 
00156 static av_cold int decode_end(AVCodecContext *avctx){
00157     QdrawContext * const a = avctx->priv_data;
00158     AVFrame *pic = &a->pic;
00159 
00160     if (pic->data[0])
00161         avctx->release_buffer(avctx, pic);
00162 
00163     return 0;
00164 }
00165 
00166 AVCodec ff_qdraw_decoder = {
00167     .name           = "qdraw",
00168     .type           = AVMEDIA_TYPE_VIDEO,
00169     .id             = CODEC_ID_QDRAW,
00170     .priv_data_size = sizeof(QdrawContext),
00171     .init           = decode_init,
00172     .close          = decode_end,
00173     .decode         = decode_frame,
00174     .capabilities   = CODEC_CAP_DR1,
00175     .long_name = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
00176 };