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