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" |
0c58c165 |
#include "libavutil/imgutils.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; |
21eafa18 |
int i; |
2fdf638b |
s->avctx = avctx;
|
2ed16f30 |
switch (avctx->bits_per_coded_sample) { |
0b8002fd |
case 1: |
ac627b3d |
avctx->pix_fmt = AV_PIX_FMT_MONOWHITE; |
0b8002fd |
break; |
2ed16f30 |
case 4:
case 8: |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_PAL8; |
2ed16f30 |
break;
case 24: |
716d413c |
avctx->pix_fmt = AV_PIX_FMT_BGR24; |
2ed16f30 |
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 |
|
c98d3056 |
if (avctx->extradata_size >= 4)
for (i = 0; i < FFMIN(avctx->extradata_size, AVPALETTE_SIZE)/4; i++) |
2fed05f5 |
s->pal[i] = 0xFFU<<24 | AV_RL32(avctx->extradata+4*i); |
21eafa18 |
|
2fdf638b |
return 0;
}
static int msrle_decode_frame(AVCodecContext *avctx, |
df9b9567 |
void *data, int *got_frame, |
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 |
} |
58825a18 |
/* 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 */ |
0c58c165 |
int linesize = av_image_get_linesize(avctx->pix_fmt, avctx->width, 0); |
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 |
|
df9b9567 |
*got_frame = 1; |
2fdf638b |
*(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, |
36ef5369 |
.id = AV_CODEC_ID_MSRLE, |
ec6402b7 |
.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 |
}; |