• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/pictordec.c

Go to the documentation of this file.
00001 /*
00002  * Pictor/PC Paint decoder
00003  * Copyright (c) 2010 Peter Ross <pross@xvid.org>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; 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/imgutils.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "cga_data.h"
00031 
00032 typedef struct PicContext {
00033     AVFrame frame;
00034     int width, height;
00035     int nb_planes;
00036     GetByteContext g;
00037 } PicContext;
00038 
00039 static void picmemset_8bpp(PicContext *s, int value, int run, int *x, int *y)
00040 {
00041     while (run > 0) {
00042         uint8_t *d = s->frame.data[0] + *y * s->frame.linesize[0];
00043         if (*x + run >= s->width) {
00044             int n = s->width - *x;
00045             memset(d + *x, value, n);
00046             run -= n;
00047             *x = 0;
00048             *y -= 1;
00049             if (*y < 0)
00050                 break;
00051         } else {
00052             memset(d + *x, value, run);
00053             *x += run;
00054             break;
00055         }
00056     }
00057 }
00058 
00059 static void picmemset(PicContext *s, int value, int run,
00060                       int *x, int *y, int *plane, int bits_per_plane)
00061 {
00062     uint8_t *d;
00063     int shift = *plane * bits_per_plane;
00064     int mask  = ((1 << bits_per_plane) - 1) << shift;
00065     value   <<= shift;
00066 
00067     while (run > 0) {
00068         int j;
00069         for (j = 8-bits_per_plane; j >= 0; j -= bits_per_plane) {
00070             d = s->frame.data[0] + *y * s->frame.linesize[0];
00071             d[*x] |= (value >> j) & mask;
00072             *x += 1;
00073             if (*x == s->width) {
00074                 *y -= 1;
00075                 *x = 0;
00076                 if (*y < 0) {
00077                    *y = s->height - 1;
00078                    *plane += 1;
00079                    value <<= bits_per_plane;
00080                    mask  <<= bits_per_plane;
00081                    if (*plane >= s->nb_planes)
00082                        break;
00083                 }
00084             }
00085         }
00086         run--;
00087     }
00088 }
00089 
00090 static const uint8_t cga_mode45_index[6][4] = {
00091     [0] = { 0, 3,  5,   7 }, // mode4, palette#1, low intensity
00092     [1] = { 0, 2,  4,   6 }, // mode4, palette#2, low intensity
00093     [2] = { 0, 3,  4,   7 }, // mode5, low intensity
00094     [3] = { 0, 11, 13, 15 }, // mode4, palette#1, high intensity
00095     [4] = { 0, 10, 12, 14 }, // mode4, palette#2, high intensity
00096     [5] = { 0, 11, 12, 15 }, // mode5, high intensity
00097 };
00098 
00099 static int decode_frame(AVCodecContext *avctx,
00100                         void *data, int *data_size,
00101                         AVPacket *avpkt)
00102 {
00103     PicContext *s = avctx->priv_data;
00104     uint32_t *palette;
00105     int bits_per_plane, bpp, etype, esize, npal, pos_after_pal;
00106     int i, x, y, plane, tmp;
00107 
00108     bytestream2_init(&s->g, avpkt->data, avpkt->size);
00109 
00110     if (bytestream2_get_bytes_left(&s->g) < 11)
00111         return AVERROR_INVALIDDATA;
00112 
00113     if (bytestream2_get_le16u(&s->g) != 0x1234)
00114         return AVERROR_INVALIDDATA;
00115 
00116     s->width       = bytestream2_get_le16u(&s->g);
00117     s->height      = bytestream2_get_le16u(&s->g);
00118     bytestream2_skip(&s->g, 4);
00119     tmp            = bytestream2_get_byteu(&s->g);
00120     bits_per_plane = tmp & 0xF;
00121     s->nb_planes   = (tmp >> 4) + 1;
00122     bpp            = bits_per_plane * s->nb_planes;
00123     if (bits_per_plane > 8 || bpp < 1 || bpp > 32) {
00124         av_log_ask_for_sample(s, "unsupported bit depth\n");
00125         return AVERROR_INVALIDDATA;
00126     }
00127 
00128     if (bytestream2_peek_byte(&s->g) == 0xFF) {
00129         bytestream2_skip(&s->g, 2);
00130         etype = bytestream2_get_le16(&s->g);
00131         esize = bytestream2_get_le16(&s->g);
00132         if (bytestream2_get_bytes_left(&s->g) < esize)
00133             return AVERROR_INVALIDDATA;
00134     } else {
00135         etype = -1;
00136         esize = 0;
00137     }
00138 
00139     avctx->pix_fmt = PIX_FMT_PAL8;
00140 
00141     if (s->width != avctx->width && s->height != avctx->height) {
00142         if (av_image_check_size(s->width, s->height, 0, avctx) < 0)
00143             return -1;
00144         avcodec_set_dimensions(avctx, s->width, s->height);
00145         if (s->frame.data[0])
00146             avctx->release_buffer(avctx, &s->frame);
00147     }
00148 
00149     if (avctx->get_buffer(avctx, &s->frame) < 0){
00150         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00151         return -1;
00152     }
00153     memset(s->frame.data[0], 0, s->height * s->frame.linesize[0]);
00154     s->frame.pict_type           = AV_PICTURE_TYPE_I;
00155     s->frame.palette_has_changed = 1;
00156 
00157     pos_after_pal = bytestream2_tell(&s->g) + esize;
00158     palette = (uint32_t*)s->frame.data[1];
00159     if (etype == 1 && esize > 1 && bytestream2_peek_byte(&s->g) < 6) {
00160         int idx = bytestream2_get_byte(&s->g);
00161         npal = 4;
00162         for (i = 0; i < npal; i++)
00163             palette[i] = ff_cga_palette[ cga_mode45_index[idx][i] ];
00164     } else if (etype == 2) {
00165         npal = FFMIN(esize, 16);
00166         for (i = 0; i < npal; i++) {
00167             int pal_idx = bytestream2_get_byte(&s->g);
00168             palette[i]  = ff_cga_palette[FFMIN(pal_idx, 16)];
00169         }
00170     } else if (etype == 3) {
00171         npal = FFMIN(esize, 16);
00172         for (i = 0; i < npal; i++) {
00173             int pal_idx = bytestream2_get_byte(&s->g);
00174             palette[i]  = ff_ega_palette[FFMIN(pal_idx, 63)];
00175         }
00176     } else if (etype == 4 || etype == 5) {
00177         npal = FFMIN(esize / 3, 256);
00178         for (i = 0; i < npal; i++)
00179             palette[i] = bytestream2_get_be24(&s->g) << 2;
00180     } else {
00181         if (bpp == 1) {
00182             npal = 2;
00183             palette[0] = 0x000000;
00184             palette[1] = 0xFFFFFF;
00185         } else if (bpp == 2) {
00186             npal = 4;
00187             for (i = 0; i < npal; i++)
00188                 palette[i] = ff_cga_palette[ cga_mode45_index[0][i] ];
00189         } else {
00190             npal = 16;
00191             memcpy(palette, ff_cga_palette, npal * 4);
00192         }
00193     }
00194     // fill remaining palette entries
00195     memset(palette + npal, 0, AVPALETTE_SIZE - npal * 4);
00196     // skip remaining palette bytes
00197     bytestream2_seek(&s->g, pos_after_pal, SEEK_SET);
00198 
00199     x = 0;
00200     y = s->height - 1;
00201     plane = 0;
00202     if (bytestream2_get_le16(&s->g)) {
00203         while (bytestream2_get_bytes_left(&s->g) >= 6) {
00204             int stop_size, marker, t1, t2;
00205 
00206             t1        = bytestream2_get_bytes_left(&s->g);
00207             t2        = bytestream2_get_le16(&s->g);
00208             stop_size = t1 - FFMIN(t1, t2);
00209             // ignore uncompressed block size
00210             bytestream2_skip(&s->g, 2);
00211             marker    = bytestream2_get_byte(&s->g);
00212 
00213             while (plane < s->nb_planes &&
00214                    bytestream2_get_bytes_left(&s->g) > stop_size) {
00215                 int run = 1;
00216                 int val = bytestream2_get_byte(&s->g);
00217                 if (val == marker) {
00218                     run = bytestream2_get_byte(&s->g);
00219                     if (run == 0)
00220                         run = bytestream2_get_le16(&s->g);
00221                     val = bytestream2_get_byte(&s->g);
00222                 }
00223                 if (!bytestream2_get_bytes_left(&s->g))
00224                     break;
00225 
00226                 if (bits_per_plane == 8) {
00227                     picmemset_8bpp(s, val, run, &x, &y);
00228                     if (y < 0)
00229                         break;
00230                 } else {
00231                     picmemset(s, val, run, &x, &y, &plane, bits_per_plane);
00232                 }
00233             }
00234         }
00235     } else {
00236         av_log_ask_for_sample(s, "uncompressed image\n");
00237         return avpkt->size;
00238     }
00239 
00240     *data_size = sizeof(AVFrame);
00241     *(AVFrame*)data = s->frame;
00242     return avpkt->size;
00243 }
00244 
00245 static av_cold int decode_end(AVCodecContext *avctx)
00246 {
00247     PicContext *s = avctx->priv_data;
00248     if (s->frame.data[0])
00249         avctx->release_buffer(avctx, &s->frame);
00250     return 0;
00251 }
00252 
00253 AVCodec ff_pictor_decoder = {
00254     .name           = "pictor",
00255     .type           = AVMEDIA_TYPE_VIDEO,
00256     .id             = CODEC_ID_PICTOR,
00257     .priv_data_size = sizeof(PicContext),
00258     .close          = decode_end,
00259     .decode         = decode_frame,
00260     .capabilities   = CODEC_CAP_DR1,
00261     .long_name = NULL_IF_CONFIG_SMALL("Pictor/PC Paint"),
00262 };
Generated on Thu Jul 11 2013 15:38:21 for Libav by doxygen 1.7.1