Libav 0.7.1
|
00001 /* 00002 * Electronic Arts CMV Video Decoder 00003 * Copyright (c) 2007-2008 Peter Ross 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 St, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00031 #include "libavutil/intreadwrite.h" 00032 #include "libavutil/imgutils.h" 00033 #include "avcodec.h" 00034 00035 typedef struct CmvContext { 00036 AVCodecContext *avctx; 00037 AVFrame frame; 00038 AVFrame last_frame; 00039 AVFrame last2_frame; 00040 int width, height; 00041 unsigned int palette[AVPALETTE_COUNT]; 00042 } CmvContext; 00043 00044 static av_cold int cmv_decode_init(AVCodecContext *avctx){ 00045 CmvContext *s = avctx->priv_data; 00046 s->avctx = avctx; 00047 avctx->pix_fmt = PIX_FMT_PAL8; 00048 return 0; 00049 } 00050 00051 static void cmv_decode_intra(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){ 00052 unsigned char *dst = s->frame.data[0]; 00053 int i; 00054 00055 for (i=0; i < s->avctx->height && buf+s->avctx->width<=buf_end; i++) { 00056 memcpy(dst, buf, s->avctx->width); 00057 dst += s->frame.linesize[0]; 00058 buf += s->avctx->width; 00059 } 00060 } 00061 00062 static void cmv_motcomp(unsigned char *dst, int dst_stride, 00063 const unsigned char *src, int src_stride, 00064 int x, int y, 00065 int xoffset, int yoffset, 00066 int width, int height){ 00067 int i,j; 00068 00069 for(j=y;j<y+4;j++) 00070 for(i=x;i<x+4;i++) 00071 { 00072 if (i+xoffset>=0 && i+xoffset<width && 00073 j+yoffset>=0 && j+yoffset<height) { 00074 dst[j*dst_stride + i] = src[(j+yoffset)*src_stride + i+xoffset]; 00075 }else{ 00076 dst[j*dst_stride + i] = 0; 00077 } 00078 } 00079 } 00080 00081 static void cmv_decode_inter(CmvContext * s, const uint8_t *buf, const uint8_t *buf_end){ 00082 const uint8_t *raw = buf + (s->avctx->width*s->avctx->height/16); 00083 int x,y,i; 00084 00085 i = 0; 00086 for(y=0; y<s->avctx->height/4; y++) 00087 for(x=0; x<s->avctx->width/4 && buf+i<buf_end; x++) { 00088 if (buf[i]==0xFF) { 00089 unsigned char *dst = s->frame.data[0] + (y*4)*s->frame.linesize[0] + x*4; 00090 if (raw+16<buf_end && *raw==0xFF) { /* intra */ 00091 raw++; 00092 memcpy(dst, raw, 4); 00093 memcpy(dst+s->frame.linesize[0], raw+4, 4); 00094 memcpy(dst+2*s->frame.linesize[0], raw+8, 4); 00095 memcpy(dst+3*s->frame.linesize[0], raw+12, 4); 00096 raw+=16; 00097 }else if(raw<buf_end) { /* inter using second-last frame as reference */ 00098 int xoffset = (*raw & 0xF) - 7; 00099 int yoffset = ((*raw >> 4)) - 7; 00100 if (s->last2_frame.data[0]) 00101 cmv_motcomp(s->frame.data[0], s->frame.linesize[0], 00102 s->last2_frame.data[0], s->last2_frame.linesize[0], 00103 x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height); 00104 raw++; 00105 } 00106 }else{ /* inter using last frame as reference */ 00107 int xoffset = (buf[i] & 0xF) - 7; 00108 int yoffset = ((buf[i] >> 4)) - 7; 00109 cmv_motcomp(s->frame.data[0], s->frame.linesize[0], 00110 s->last_frame.data[0], s->last_frame.linesize[0], 00111 x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height); 00112 } 00113 i++; 00114 } 00115 } 00116 00117 static void cmv_process_header(CmvContext *s, const uint8_t *buf, const uint8_t *buf_end) 00118 { 00119 int pal_start, pal_count, i; 00120 00121 if(buf+16>=buf_end) { 00122 av_log(s->avctx, AV_LOG_WARNING, "truncated header\n"); 00123 return; 00124 } 00125 00126 s->width = AV_RL16(&buf[4]); 00127 s->height = AV_RL16(&buf[6]); 00128 if (s->avctx->width!=s->width || s->avctx->height!=s->height) 00129 avcodec_set_dimensions(s->avctx, s->width, s->height); 00130 00131 s->avctx->time_base.num = 1; 00132 s->avctx->time_base.den = AV_RL16(&buf[10]); 00133 00134 pal_start = AV_RL16(&buf[12]); 00135 pal_count = AV_RL16(&buf[14]); 00136 00137 buf += 16; 00138 for (i=pal_start; i<pal_start+pal_count && i<AVPALETTE_COUNT && buf+2<buf_end; i++) { 00139 s->palette[i] = AV_RB24(buf); 00140 buf += 3; 00141 } 00142 } 00143 00144 #define EA_PREAMBLE_SIZE 8 00145 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h') 00146 00147 static int cmv_decode_frame(AVCodecContext *avctx, 00148 void *data, int *data_size, 00149 AVPacket *avpkt) 00150 { 00151 const uint8_t *buf = avpkt->data; 00152 int buf_size = avpkt->size; 00153 CmvContext *s = avctx->priv_data; 00154 const uint8_t *buf_end = buf + buf_size; 00155 00156 if (AV_RL32(buf)==MVIh_TAG||AV_RB32(buf)==MVIh_TAG) { 00157 cmv_process_header(s, buf+EA_PREAMBLE_SIZE, buf_end); 00158 return buf_size; 00159 } 00160 00161 if (av_image_check_size(s->width, s->height, 0, s->avctx)) 00162 return -1; 00163 00164 /* shuffle */ 00165 if (s->last2_frame.data[0]) 00166 avctx->release_buffer(avctx, &s->last2_frame); 00167 FFSWAP(AVFrame, s->last_frame, s->last2_frame); 00168 FFSWAP(AVFrame, s->frame, s->last_frame); 00169 00170 s->frame.reference = 1; 00171 s->frame.buffer_hints = FF_BUFFER_HINTS_VALID; 00172 if (avctx->get_buffer(avctx, &s->frame)<0) { 00173 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 00174 return -1; 00175 } 00176 00177 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE); 00178 00179 buf += EA_PREAMBLE_SIZE; 00180 if ((buf[0]&1)) { // subtype 00181 cmv_decode_inter(s, buf+2, buf_end); 00182 s->frame.key_frame = 0; 00183 s->frame.pict_type = AV_PICTURE_TYPE_P; 00184 }else{ 00185 s->frame.key_frame = 1; 00186 s->frame.pict_type = AV_PICTURE_TYPE_I; 00187 cmv_decode_intra(s, buf+2, buf_end); 00188 } 00189 00190 *data_size = sizeof(AVFrame); 00191 *(AVFrame*)data = s->frame; 00192 00193 return buf_size; 00194 } 00195 00196 static av_cold int cmv_decode_end(AVCodecContext *avctx){ 00197 CmvContext *s = avctx->priv_data; 00198 if (s->frame.data[0]) 00199 s->avctx->release_buffer(avctx, &s->frame); 00200 if (s->last_frame.data[0]) 00201 s->avctx->release_buffer(avctx, &s->last_frame); 00202 if (s->last2_frame.data[0]) 00203 s->avctx->release_buffer(avctx, &s->last2_frame); 00204 00205 return 0; 00206 } 00207 00208 AVCodec ff_eacmv_decoder = { 00209 "eacmv", 00210 AVMEDIA_TYPE_VIDEO, 00211 CODEC_ID_CMV, 00212 sizeof(CmvContext), 00213 cmv_decode_init, 00214 NULL, 00215 cmv_decode_end, 00216 cmv_decode_frame, 00217 CODEC_CAP_DR1, 00218 .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts CMV video"), 00219 };