00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 GetByteContext gb;
00044 const unsigned char *buf;
00045 int size;
00046
00047 uint32_t pal[256];
00048 } MsrleContext;
00049
00050 static av_cold int msrle_decode_init(AVCodecContext *avctx)
00051 {
00052 MsrleContext *s = avctx->priv_data;
00053 int i;
00054
00055 s->avctx = avctx;
00056
00057 switch (avctx->bits_per_coded_sample) {
00058 case 1:
00059 avctx->pix_fmt = PIX_FMT_MONOWHITE;
00060 break;
00061 case 4:
00062 case 8:
00063 avctx->pix_fmt = PIX_FMT_PAL8;
00064 break;
00065 case 24:
00066 avctx->pix_fmt = PIX_FMT_BGR24;
00067 break;
00068 default:
00069 av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n");
00070 return -1;
00071 }
00072
00073 avcodec_get_frame_defaults(&s->frame);
00074 s->frame.data[0] = NULL;
00075
00076 if (avctx->extradata_size >= AVPALETTE_SIZE)
00077 for (i = 0; i < AVPALETTE_SIZE/4; i++)
00078 s->pal[i] = 0xFF<<24 | AV_RL32(avctx->extradata+4*i);
00079
00080 return 0;
00081 }
00082
00083 static int msrle_decode_frame(AVCodecContext *avctx,
00084 void *data, int *data_size,
00085 AVPacket *avpkt)
00086 {
00087 const uint8_t *buf = avpkt->data;
00088 int buf_size = avpkt->size;
00089 MsrleContext *s = avctx->priv_data;
00090 int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8;
00091
00092 s->buf = buf;
00093 s->size = buf_size;
00094
00095 s->frame.reference = 3;
00096 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00097 if (avctx->reget_buffer(avctx, &s->frame)) {
00098 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00099 return -1;
00100 }
00101
00102 if (avctx->bits_per_coded_sample > 1 && avctx->bits_per_coded_sample <= 8) {
00103 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL);
00104
00105 if (pal) {
00106 s->frame.palette_has_changed = 1;
00107 memcpy(s->pal, pal, AVPALETTE_SIZE);
00108 }
00109
00110 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE);
00111 }
00112
00113
00114 if (avctx->height * istride == avpkt->size) {
00115 int linesize = (avctx->width * avctx->bits_per_coded_sample + 7) / 8;
00116 uint8_t *ptr = s->frame.data[0];
00117 uint8_t *buf = avpkt->data + (avctx->height-1)*istride;
00118 int i, j;
00119
00120 for (i = 0; i < avctx->height; i++) {
00121 if (avctx->bits_per_coded_sample == 4) {
00122 for (j = 0; j < avctx->width - 1; j += 2) {
00123 ptr[j+0] = buf[j>>1] >> 4;
00124 ptr[j+1] = buf[j>>1] & 0xF;
00125 }
00126 if (avctx->width & 1)
00127 ptr[j+0] = buf[j>>1] >> 4;
00128 } else {
00129 memcpy(ptr, buf, linesize);
00130 }
00131 buf -= istride;
00132 ptr += s->frame.linesize[0];
00133 }
00134 } else {
00135 bytestream2_init(&s->gb, buf, buf_size);
00136 ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, &s->gb);
00137 }
00138
00139 *data_size = sizeof(AVFrame);
00140 *(AVFrame*)data = s->frame;
00141
00142
00143 return buf_size;
00144 }
00145
00146 static av_cold int msrle_decode_end(AVCodecContext *avctx)
00147 {
00148 MsrleContext *s = avctx->priv_data;
00149
00150
00151 if (s->frame.data[0])
00152 avctx->release_buffer(avctx, &s->frame);
00153
00154 return 0;
00155 }
00156
00157 AVCodec ff_msrle_decoder = {
00158 .name = "msrle",
00159 .type = AVMEDIA_TYPE_VIDEO,
00160 .id = AV_CODEC_ID_MSRLE,
00161 .priv_data_size = sizeof(MsrleContext),
00162 .init = msrle_decode_init,
00163 .close = msrle_decode_end,
00164 .decode = msrle_decode_frame,
00165 .capabilities = CODEC_CAP_DR1,
00166 .long_name = NULL_IF_CONFIG_SMALL("Microsoft RLE"),
00167 };