libavcodec/msrle.c
2fdf638b
 /*
5ea20630
  * Microsoft RLE video decoder
2fdf638b
  * Copyright (C) 2003 the ffmpeg project
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
2fdf638b
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
2fdf638b
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
2fdf638b
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2fdf638b
  */
 
 /**
ba87f080
  * @file
5ea20630
  * MS RLE video decoder by Mike Melanson (melanson@pcisys.net)
2fdf638b
  * For more information about the MS RLE format, visit:
  *   http://www.pcisys.net/~melanson/codecs/
  *
  * The MS RLE decoder outputs PAL8 colorspace data.
  */
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include "avcodec.h"
 #include "dsputil.h"
44aa9771
 #include "msrledec.h"
2fdf638b
 
 typedef struct MsrleContext {
     AVCodecContext *avctx;
     AVFrame frame;
 
992f71e9
     GetByteContext gb;
7993df65
     const unsigned char *buf;
2fdf638b
     int size;
 
2d8591c2
     uint32_t pal[256];
2fdf638b
 } MsrleContext;
 
98a6fff9
 static av_cold int msrle_decode_init(AVCodecContext *avctx)
2fdf638b
 {
e4141433
     MsrleContext *s = avctx->priv_data;
2fdf638b
 
     s->avctx = avctx;
 
2ed16f30
     switch (avctx->bits_per_coded_sample) {
0b8002fd
     case 1:
         avctx->pix_fmt = PIX_FMT_MONOWHITE;
         break;
2ed16f30
     case 4:
     case 8:
         avctx->pix_fmt = PIX_FMT_PAL8;
         break;
     case 24:
         avctx->pix_fmt = PIX_FMT_BGR24;
         break;
     default:
         av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n");
         return -1;
     }
 
01042d41
     avcodec_get_frame_defaults(&s->frame);
e1c2a5a0
     s->frame.data[0] = NULL;
2fdf638b
 
     return 0;
 }
 
 static int msrle_decode_frame(AVCodecContext *avctx,
                               void *data, int *data_size,
7a00bbad
                               AVPacket *avpkt)
2fdf638b
 {
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
e4141433
     MsrleContext *s = avctx->priv_data;
2ed16f30
     int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8;
2fdf638b
 
     s->buf = buf;
     s->size = buf_size;
 
371e1654
     s->frame.reference = 3;
e1c2a5a0
     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
     if (avctx->reget_buffer(avctx, &s->frame)) {
         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
2fdf638b
         return -1;
     }
 
445ee35e
     if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
2d8591c2
         const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
 
         if (pal) {
30853173
             s->frame.palette_has_changed = 1;
2d8591c2
             memcpy(s->pal, pal, AVPALETTE_SIZE);
30853173
         }
2d8591c2
 
         /* make the palette available */
         memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
a7e56229
     }
2fdf638b
 
2ed16f30
     /* FIXME how to correctly detect RLE ??? */
     if (avctx->height * istride == avpkt->size) { /* assume uncompressed */
4fe796b3
         int linesize = (avctx->width * avctx->bits_per_coded_sample + 7) / 8;
2ed16f30
         uint8_t *ptr = s->frame.data[0];
         uint8_t *buf = avpkt->data + (avctx->height-1)*istride;
         int i, j;
 
         for (i = 0; i < avctx->height; i++) {
             if (avctx->bits_per_coded_sample == 4) {
                 for (j = 0; j < avctx->width - 1; j += 2) {
                     ptr[j+0] = buf[j>>1] >> 4;
                     ptr[j+1] = buf[j>>1] & 0xF;
                 }
                 if (avctx->width & 1)
                     ptr[j+0] = buf[j>>1] >> 4;
             } else {
                 memcpy(ptr, buf, linesize);
             }
             buf -= istride;
             ptr += s->frame.linesize[0];
         }
     } else {
992f71e9
         bytestream2_init(&s->gb, buf, buf_size);
         ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, &s->gb);
2ed16f30
     }
44aa9771
 
2fdf638b
     *data_size = sizeof(AVFrame);
     *(AVFrame*)data = s->frame;
 
     /* report that the buffer was completely consumed */
     return buf_size;
 }
 
98a6fff9
 static av_cold int msrle_decode_end(AVCodecContext *avctx)
2fdf638b
 {
e4141433
     MsrleContext *s = avctx->priv_data;
2fdf638b
 
     /* release the last frame */
e1c2a5a0
     if (s->frame.data[0])
         avctx->release_buffer(avctx, &s->frame);
2fdf638b
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_msrle_decoder = {
ec6402b7
     .name           = "msrle",
     .type           = AVMEDIA_TYPE_VIDEO,
     .id             = CODEC_ID_MSRLE,
     .priv_data_size = sizeof(MsrleContext),
     .init           = msrle_decode_init,
     .close          = msrle_decode_end,
     .decode         = msrle_decode_frame,
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("Microsoft RLE"),
2fdf638b
 };