Libav 0.7.1
|
00001 /* 00002 * Micrsoft RLE Video Decoder 00003 * Copyright (C) 2003 the ffmpeg project 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav 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 * Libav 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 Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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 const unsigned char *buf; 00044 int size; 00045 00046 uint32_t pal[256]; 00047 } MsrleContext; 00048 00049 static av_cold int msrle_decode_init(AVCodecContext *avctx) 00050 { 00051 MsrleContext *s = avctx->priv_data; 00052 00053 s->avctx = avctx; 00054 00055 switch (avctx->bits_per_coded_sample) { 00056 case 4: 00057 case 8: 00058 avctx->pix_fmt = PIX_FMT_PAL8; 00059 break; 00060 case 24: 00061 avctx->pix_fmt = PIX_FMT_BGR24; 00062 break; 00063 default: 00064 av_log(avctx, AV_LOG_ERROR, "unsupported bits per sample\n"); 00065 return -1; 00066 } 00067 00068 s->frame.data[0] = NULL; 00069 00070 return 0; 00071 } 00072 00073 static int msrle_decode_frame(AVCodecContext *avctx, 00074 void *data, int *data_size, 00075 AVPacket *avpkt) 00076 { 00077 const uint8_t *buf = avpkt->data; 00078 int buf_size = avpkt->size; 00079 MsrleContext *s = avctx->priv_data; 00080 int istride = FFALIGN(avctx->width*avctx->bits_per_coded_sample, 32) / 8; 00081 00082 s->buf = buf; 00083 s->size = buf_size; 00084 00085 s->frame.reference = 1; 00086 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE | FF_BUFFER_HINTS_REUSABLE; 00087 if (avctx->reget_buffer(avctx, &s->frame)) { 00088 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n"); 00089 return -1; 00090 } 00091 00092 if (avctx->bits_per_coded_sample <= 8) { 00093 const uint8_t *pal = av_packet_get_side_data(avpkt, AV_PKT_DATA_PALETTE, NULL); 00094 00095 if (pal) { 00096 s->frame.palette_has_changed = 1; 00097 memcpy(s->pal, pal, AVPALETTE_SIZE); 00098 } 00099 00100 /* make the palette available */ 00101 memcpy(s->frame.data[1], s->pal, AVPALETTE_SIZE); 00102 } 00103 00104 /* FIXME how to correctly detect RLE ??? */ 00105 if (avctx->height * istride == avpkt->size) { /* assume uncompressed */ 00106 int linesize = avctx->width * avctx->bits_per_coded_sample / 8; 00107 uint8_t *ptr = s->frame.data[0]; 00108 uint8_t *buf = avpkt->data + (avctx->height-1)*istride; 00109 int i, j; 00110 00111 for (i = 0; i < avctx->height; i++) { 00112 if (avctx->bits_per_coded_sample == 4) { 00113 for (j = 0; j < avctx->width - 1; j += 2) { 00114 ptr[j+0] = buf[j>>1] >> 4; 00115 ptr[j+1] = buf[j>>1] & 0xF; 00116 } 00117 if (avctx->width & 1) 00118 ptr[j+0] = buf[j>>1] >> 4; 00119 } else { 00120 memcpy(ptr, buf, linesize); 00121 } 00122 buf -= istride; 00123 ptr += s->frame.linesize[0]; 00124 } 00125 } else { 00126 ff_msrle_decode(avctx, (AVPicture*)&s->frame, avctx->bits_per_coded_sample, buf, buf_size); 00127 } 00128 00129 *data_size = sizeof(AVFrame); 00130 *(AVFrame*)data = s->frame; 00131 00132 /* report that the buffer was completely consumed */ 00133 return buf_size; 00134 } 00135 00136 static av_cold int msrle_decode_end(AVCodecContext *avctx) 00137 { 00138 MsrleContext *s = avctx->priv_data; 00139 00140 /* release the last frame */ 00141 if (s->frame.data[0]) 00142 avctx->release_buffer(avctx, &s->frame); 00143 00144 return 0; 00145 } 00146 00147 AVCodec ff_msrle_decoder = { 00148 "msrle", 00149 AVMEDIA_TYPE_VIDEO, 00150 CODEC_ID_MSRLE, 00151 sizeof(MsrleContext), 00152 msrle_decode_init, 00153 NULL, 00154 msrle_decode_end, 00155 msrle_decode_frame, 00156 CODEC_CAP_DR1, 00157 .long_name= NULL_IF_CONFIG_SMALL("Microsoft RLE"), 00158 };