Libav
|
00001 /* 00002 * Micrsoft RLE Video Decoder 00003 * Copyright (C) 2003 the ffmpeg project 00004 * 00005 * This file is part of FFmpeg. 00006 * 00007 * FFmpeg is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * FFmpeg is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with FFmpeg; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 4: 00059 case 8: 00060 avctx->pix_fmt = PIX_FMT_PAL8; 00061 break; 00062 case 24: 00063 avctx->pix_fmt = PIX_FMT_BGR24; 00064 break; 00065 default: 00066 av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n"); 00067 return -1; 00068 } 00069 00070 s->frame.data[0] = NULL; 00071 00072 return 0; 00073 } 00074 00075 static int msrle_decode_frame(AVCodecContext *avctx, 00076 void *data, int *data_size, 00077 AVPacket *avpkt) 00078 { 00079 const uint8_t *buf = avpkt->data; 00080 int buf_size = avpkt->size; 00081 MsrleContext *s = avctx->priv_data; 00082 int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8; 00083 00084 s->buf = buf; 00085 s->size = buf_size; 00086 00087 s->frame.reference = 1; 00088 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; 00089 if (avctx->reget_buffer(avctx, &s->frame)) { 00090 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00091 return -1; 00092 } 00093 00094 if (s->avctx->palctrl) { 00095 /* make the palette available */ 00096 memcpy(s->frame.data[1], s->avctx->palctrl->palette, AVPALETTE_SIZE); 00097 if (s->avctx->palctrl->palette_changed) { 00098 s->frame.palette_has_changed = 1; 00099 s->avctx->palctrl->palette_changed = 0; 00100 } 00101 } 00102 00103 /* FIXME how to correctly detect RLE ??? */ 00104 if (avctx->height * istride == avpkt->size) { /* assume uncompressed */ 00105 int linesize = avctx->width * avctx->bits_per_coded_sample / 8; 00106 uint8_t *ptr = s->frame.data[0]; 00107 uint8_t *buf = avpkt->data + (avctx->height-1)*istride; 00108 int i, j; 00109 00110 for (i = 0; i < avctx->height; i++) { 00111 if (avctx->bits_per_coded_sample == 4) { 00112 for (j = 0; j < avctx->width - 1; j += 2) { 00113 ptr[j+0] = buf[j>>1] >> 4; 00114 ptr[j+1] = buf[j>>1] & 0xF; 00115 } 00116 if (avctx->width & 1) 00117 ptr[j+0] = buf[j>>1] >> 4; 00118 } else { 00119 memcpy(ptr, buf, linesize); 00120 } 00121 buf -= istride; 00122 ptr += s->frame.linesize[0]; 00123 } 00124 } else { 00125 ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size); 00126 } 00127 00128 *data_size = sizeof(AVFrame); 00129 *(AVFrame*)data = s->frame; 00130 00131 /* report that the buffer was completely consumed */ 00132 return buf_size; 00133 } 00134 00135 static av_cold int msrle_decode_end(AVCodecContext *avctx) 00136 { 00137 MsrleContext *s = avctx->priv_data; 00138 00139 /* release the last frame */ 00140 if (s->frame.data[0]) 00141 avctx->release_buffer(avctx, &s->frame); 00142 00143 return 0; 00144 } 00145 00146 AVCodec msrle_decoder = { 00147 "msrle", 00148 AVMEDIA_TYPE_VIDEO, 00149 CODEC_ID_MSRLE, 00150 sizeof(MsrleContext), 00151 msrle_decode_init, 00152 NULL, 00153 msrle_decode_end, 00154 msrle_decode_frame, 00155 CODEC_CAP_DR1, 00156 .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"), 00157 };