00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037
00038 #include "avcodec.h"
00039 #include "dsputil.h"
00040 #include "msrledec.h"
00041
00042 typedef struct MsrleContext {
00043 AVCodecContext *avctx;
00044 AVFrame frame;
00045
00046 const unsigned char *buf;
00047 int size;
00048
00049 } MsrleContext;
00050
00051 static av_cold int msrle_decode_init(AVCodecContext *avctx)
00052 {
00053 MsrleContext *s = avctx->priv_data;
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 return 0;
00077 }
00078
00079 static int msrle_decode_frame(AVCodecContext *avctx,
00080 void *data, int *data_size,
00081 AVPacket *avpkt)
00082 {
00083 const uint8_t *buf = avpkt->data;
00084 int buf_size = avpkt->size;
00085 MsrleContext *s = avctx->priv_data;
00086 int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8;
00087
00088 s->buf = buf;
00089 s->size = buf_size;
00090
00091 s->frame.reference = 1;
00092 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE;
00093 if (avctx->reget_buffer(avctx, &s->frame)) {
00094 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00095 return -1;
00096 }
00097
00098 if (s->avctx->palctrl) {
00099
00100 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE);
00101 if (s->avctx->palctrl->palette_changed) {
00102 s->frame.palette_has_changed = 1;
00103 s->avctx->palctrl->palette_changed = 0;
00104 }
00105 }
00106
00107
00108 if (avctx->height * istride == avpkt->size) {
00109 int linesize = avctx->width * avctx->bits_per_coded_sample / 8;
00110 uint8_t *ptr = s->frame.data[0];
00111 uint8_t *buf = avpkt->data + (avctx->height-1)*istride;
00112 int i, j;
00113
00114 for (i = 0; i < avctx->height; i++) {
00115 if (avctx->bits_per_coded_sample == 4) {
00116 for (j = 0; j < avctx->width - 1; j += 2) {
00117 ptr[j+0] = buf[j>>1] >> 4;
00118 ptr[j+1] = buf[j>>1] & 0xF;
00119 }
00120 if (avctx->width & 1)
00121 ptr[j+0] = buf[j>>1] >> 4;
00122 } else {
00123 memcpy(ptr, buf, linesize);
00124 }
00125 buf -= istride;
00126 ptr += s->frame.linesize[0];
00127 }
00128 } else {
00129 ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size);
00130 }
00131
00132 *data_size = sizeof(AVFrame);
00133 *(AVFrame*)data = s->frame;
00134
00135
00136 return buf_size;
00137 }
00138
00139 static av_cold int msrle_decode_end(AVCodecContext *avctx)
00140 {
00141 MsrleContext *s = avctx->priv_data;
00142
00143
00144 if (s->frame.data[0])
00145 avctx->release_buffer(avctx, &s->frame);
00146
00147 return 0;
00148 }
00149
00150 AVCodec ff_msrle_decoder = {
00151 "msrle",
00152 AVMEDIA_TYPE_VIDEO,
00153 CODEC_ID_MSRLE,
00154 sizeof(MsrleContext),
00155 msrle_decode_init,
00156 NULL,
00157 msrle_decode_end,
00158 msrle_decode_frame,
00159 CODEC_CAP_DR1,
00160 .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"),
00161 };