libavcodec/8bps.c
Go to the documentation of this file.
00001 /*
00002  * Quicktime Planar RGB (8BPS) Video Decoder
00003  * Copyright (C) 2003 Roberto Togni
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 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 
00037 #include "libavutil/intreadwrite.h"
00038 #include "avcodec.h"
00039 
00040 
00041 static const enum PixelFormat pixfmt_rgb24[] = {PIX_FMT_BGR24, PIX_FMT_RGB32, PIX_FMT_NONE};
00042 
00043 /*
00044  * Decoder context
00045  */
00046 typedef struct EightBpsContext {
00047 
00048         AVCodecContext *avctx;
00049         AVFrame pic;
00050 
00051         unsigned char planes;
00052         unsigned char planemap[4];
00053 
00054         uint32_t pal[256];
00055 } EightBpsContext;
00056 
00057 
00058 /*
00059  *
00060  * Decode a frame
00061  *
00062  */
00063 static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
00064 {
00065         const uint8_t *buf = avpkt->data;
00066         int buf_size = avpkt->size;
00067         EightBpsContext * const c = avctx->priv_data;
00068         const unsigned char *encoded = buf;
00069         unsigned char *pixptr, *pixptr_end;
00070         unsigned int height = avctx->height; // Real image height
00071         unsigned int dlen, p, row;
00072         const unsigned char *lp, *dp, *ep;
00073         unsigned char count;
00074         unsigned int px_inc;
00075         unsigned int planes = c->planes;
00076         unsigned char *planemap = c->planemap;
00077 
00078         if(c->pic.data[0])
00079                 avctx->release_buffer(avctx, &c->pic);
00080 
00081         c->pic.reference = 0;
00082         c->pic.buffer_hints = FF_BUFFER_HINTS_VALID;
00083         if(avctx->get_buffer(avctx, &c->pic) < 0){
00084                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00085                 return -1;
00086         }
00087 
00088         ep = encoded + buf_size;
00089 
00090         /* Set data pointer after line lengths */
00091         dp = encoded + planes * (height << 1);
00092 
00093         /* Ignore alpha plane, don't know what to do with it */
00094         if (planes == 4)
00095                 planes--;
00096 
00097         px_inc = planes + (avctx->pix_fmt == PIX_FMT_RGB32);
00098 
00099         for (p = 0; p < planes; p++) {
00100                 /* Lines length pointer for this plane */
00101                 lp = encoded + p * (height << 1);
00102 
00103                 /* Decode a plane */
00104                 for(row = 0; row < height; row++) {
00105                         pixptr = c->pic.data[0] + row * c->pic.linesize[0] + planemap[p];
00106                         pixptr_end = pixptr + c->pic.linesize[0];
00107                         if (ep - lp < row * 2 + 2)
00108                             return AVERROR_INVALIDDATA;
00109                         dlen = av_be2ne16(*(const unsigned short *)(lp+row*2));
00110                         /* Decode a row of this plane */
00111                         while(dlen > 0) {
00112                                 if(ep - dp <= 1) return -1;
00113                                 if ((count = *dp++) <= 127) {
00114                                         count++;
00115                                         dlen -= count + 1;
00116                                         if (pixptr + count * px_inc > pixptr_end)
00117                                             break;
00118                                         if(ep - dp < count) return -1;
00119                                         while(count--) {
00120                                                 *pixptr = *dp++;
00121                                                 pixptr += px_inc;
00122                                         }
00123                                 } else {
00124                                         count = 257 - count;
00125                                         if (pixptr + count * px_inc > pixptr_end)
00126                                             break;
00127                                         while(count--) {
00128                                                 *pixptr = *dp;
00129                                                 pixptr += px_inc;
00130                                         }
00131                                         dp++;
00132                                         dlen -= 2;
00133                                 }
00134                         }
00135                 }
00136         }
00137 
00138         if (avctx->bits_per_coded_sample <= 8) {
00139                 const uint8_t *pal = av_packet_get_side_data(avpkt,
00140                                                              AV_PKT_DATA_PALETTE,
00141                                                              NULL);
00142                 if (pal) {
00143                         c->pic.palette_has_changed = 1;
00144                         memcpy(c->pal, pal, AVPALETTE_SIZE);
00145                 }
00146 
00147                 memcpy (c->pic.data[1], c->pal, AVPALETTE_SIZE);
00148         }
00149 
00150         *data_size = sizeof(AVFrame);
00151         *(AVFrame*)data = c->pic;
00152 
00153         /* always report that the buffer was completely consumed */
00154         return buf_size;
00155 }
00156 
00157 
00158 /*
00159  *
00160  * Init 8BPS decoder
00161  *
00162  */
00163 static av_cold int decode_init(AVCodecContext *avctx)
00164 {
00165         EightBpsContext * const c = avctx->priv_data;
00166 
00167         c->avctx = avctx;
00168 
00169         c->pic.data[0] = NULL;
00170 
00171         switch (avctx->bits_per_coded_sample) {
00172                 case 8:
00173                         avctx->pix_fmt = PIX_FMT_PAL8;
00174                         c->planes = 1;
00175                         c->planemap[0] = 0; // 1st plane is palette indexes
00176                         break;
00177                 case 24:
00178                         avctx->pix_fmt = avctx->get_format(avctx, pixfmt_rgb24);
00179                         c->planes = 3;
00180                         c->planemap[0] = 2; // 1st plane is red
00181                         c->planemap[1] = 1; // 2nd plane is green
00182                         c->planemap[2] = 0; // 3rd plane is blue
00183                         break;
00184                 case 32:
00185                         avctx->pix_fmt = PIX_FMT_RGB32;
00186                         c->planes = 4;
00187 #if HAVE_BIGENDIAN
00188                         c->planemap[0] = 1; // 1st plane is red
00189                         c->planemap[1] = 2; // 2nd plane is green
00190                         c->planemap[2] = 3; // 3rd plane is blue
00191                         c->planemap[3] = 0; // 4th plane is alpha???
00192 #else
00193                         c->planemap[0] = 2; // 1st plane is red
00194                         c->planemap[1] = 1; // 2nd plane is green
00195                         c->planemap[2] = 0; // 3rd plane is blue
00196                         c->planemap[3] = 3; // 4th plane is alpha???
00197 #endif
00198                         break;
00199                 default:
00200                         av_log(avctx, AV_LOG_ERROR, "Error: Unsupported color depth: %u.\n", avctx->bits_per_coded_sample);
00201                         return -1;
00202         }
00203 
00204   return 0;
00205 }
00206 
00207 
00208 
00209 
00210 /*
00211  *
00212  * Uninit 8BPS decoder
00213  *
00214  */
00215 static av_cold int decode_end(AVCodecContext *avctx)
00216 {
00217         EightBpsContext * const c = avctx->priv_data;
00218 
00219         if (c->pic.data[0])
00220                 avctx->release_buffer(avctx, &c->pic);
00221 
00222         return 0;
00223 }
00224 
00225 
00226 
00227 AVCodec ff_eightbps_decoder = {
00228     .name           = "8bps",
00229     .type           = AVMEDIA_TYPE_VIDEO,
00230     .id             = CODEC_ID_8BPS,
00231     .priv_data_size = sizeof(EightBpsContext),
00232     .init           = decode_init,
00233     .close          = decode_end,
00234     .decode         = decode_frame,
00235     .capabilities   = CODEC_CAP_DR1,
00236     .long_name      = NULL_IF_CONFIG_SMALL("QuickTime 8BPS video"),
00237 };