libavcodec/msrle.c
Go to the documentation of this file.
00001 /*
00002  * Microsoft RLE video decoder
00003  * Copyright (C) 2003 the ffmpeg project
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 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 
00035 #include "avcodec.h"
00036 #include "dsputil.h"
00037 #include "msrledec.h"
00038 
00039 typedef struct MsrleContext {
00040     AVCodecContext *avctx;
00041     AVFrame frame;
00042 
00043     const unsigned char *buf;
00044     int size;
00045 
00046     uint32_t pal[256];
00047 } MsrleContext;
00048 
00049 static av_cold int msrle_decode_init(AVCodecContext *avctx)
00050 {
00051     MsrleContext *s = avctx->priv_data;
00052 
00053     s->avctx = avctx;
00054 
00055     switch (avctx->bits_per_coded_sample) {
00056     case 1:
00057         avctx->pix_fmt = PIX_FMT_MONOWHITE;
00058         break;
00059     case 4:
00060     case 8:
00061         avctx->pix_fmt = PIX_FMT_PAL8;
00062         break;
00063     case 24:
00064         avctx->pix_fmt = PIX_FMT_BGR24;
00065         break;
00066     default:
00067         av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n");
00068         return -1;
00069     }
00070 
00071     avcodec_get_frame_defaults(&s->frame);
00072     s->frame.data[0] = NULL;
00073 
00074     return 0;
00075 }
00076 
00077 static int msrle_decode_frame(AVCodecContext *avctx,
00078                               void *data, int *data_size,
00079                               AVPacket *avpkt)
00080 {
00081     const uint8_t *buf = avpkt->data;
00082     int buf_size = avpkt->size;
00083     MsrleContext *s = avctx->priv_data;
00084     int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8;
00085 
00086     s->buf = buf;
00087     s->size = buf_size;
00088 
00089     s->frame.reference = 3;
00090     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00091     if (avctx->reget_buffer(avctx, &s->frame)) {
00092         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00093         return -1;
00094     }
00095 
00096     if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
00097         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00098 
00099         if (pal) {
00100             s->frame.palette_has_changed = 1;
00101             memcpy(s->pal, pal, AVPALETTE_SIZE);
00102         }
00103 
00104         /* make the palette available */
00105         memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00106     }
00107 
00108     /* FIXME how to correctly detect RLE ??? */
00109     if (avctx->height * istride == avpkt->size) { /* assume uncompressed */
00110         int linesize = (avctx->width * avctx->bits_per_coded_sample + 7) / 8;
00111         uint8_t *ptr = s->frame.data[0];
00112         uint8_t *buf = avpkt->data + (avctx->height-1)*istride;
00113         int i, j;
00114 
00115         for (i = 0; i < avctx->height; i++) {
00116             if (avctx->bits_per_coded_sample == 4) {
00117                 for (j = 0; j < avctx->width - 1; j += 2) {
00118                     ptr[j+0] = buf[j>>1] >> 4;
00119                     ptr[j+1] = buf[j>>1] & 0xF;
00120                 }
00121                 if (avctx->width & 1)
00122                     ptr[j+0] = buf[j>>1] >> 4;
00123             } else {
00124                 memcpy(ptr, buf, linesize);
00125             }
00126             buf -= istride;
00127             ptr += s->frame.linesize[0];
00128         }
00129     } else {
00130         ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size);
00131     }
00132 
00133     *data_size = sizeof(AVFrame);
00134     *(AVFrame*)data = s->frame;
00135 
00136     /* report that the buffer was completely consumed */
00137     return buf_size;
00138 }
00139 
00140 static av_cold int msrle_decode_end(AVCodecContext *avctx)
00141 {
00142     MsrleContext *s = avctx->priv_data;
00143 
00144     /* release the last frame */
00145     if (s->frame.data[0])
00146         avctx->release_buffer(avctx, &s->frame);
00147 
00148     return 0;
00149 }
00150 
00151 AVCodec ff_msrle_decoder = {
00152     .name           = "msrle",
00153     .type           = AVMEDIA_TYPE_VIDEO,
00154     .id             = CODEC_ID_MSRLE,
00155     .priv_data_size = sizeof(MsrleContext),
00156     .init           = msrle_decode_init,
00157     .close          = msrle_decode_end,
00158     .decode         = msrle_decode_frame,
00159     .capabilities   = CODEC_CAP_DR1,
00160     .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"),
00161 };