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