libavcodec/eacmv.c
Go to the documentation of this file.
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_end - buf >= s->avctx->width; 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_end - buf > i; 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             if (s->last_frame.data[0])
00110                 cmv_motcomp(s->frame.data[0], s->frame.linesize[0],
00111                             s->last_frame.data[0], s->last_frame.linesize[0],
00112                             x*4, y*4, xoffset, yoffset, s->avctx->width, s->avctx->height);
00113         }
00114         i++;
00115     }
00116 }
00117 
00118 static void cmv_process_header(CmvContext *s, const uint8_t *buf, const uint8_t *buf_end)
00119 {
00120     int pal_start, pal_count, i;
00121 
00122     if(buf_end - buf < 16) {
00123         av_log(s->avctx, AV_LOG_WARNING, "truncated header\n");
00124         return;
00125     }
00126 
00127     s->width  = AV_RL16(&buf[4]);
00128     s->height = AV_RL16(&buf[6]);
00129     if (s->avctx->width!=s->width || s->avctx->height!=s->height)
00130         avcodec_set_dimensions(s->avctx, s->width, s->height);
00131 
00132     s->avctx->time_base.num = 1;
00133     s->avctx->time_base.den = AV_RL16(&buf[10]);
00134 
00135     pal_start = AV_RL16(&buf[12]);
00136     pal_count = AV_RL16(&buf[14]);
00137 
00138     buf += 16;
00139     for (i=pal_start; i<pal_start+pal_count && i<AVPALETTE_COUNT && buf_end - buf >= 3; i++) {
00140         s->palette[i] = AV_RB24(buf);
00141         buf += 3;
00142     }
00143 }
00144 
00145 #define EA_PREAMBLE_SIZE 8
00146 #define MVIh_TAG MKTAG('M', 'V', 'I', 'h')
00147 
00148 static int cmv_decode_frame(AVCodecContext *avctx,
00149                             void *data, int *data_size,
00150                             AVPacket *avpkt)
00151 {
00152     const uint8_t *buf = avpkt->data;
00153     int buf_size = avpkt->size;
00154     CmvContext *s = avctx->priv_data;
00155     const uint8_t *buf_end = buf + buf_size;
00156 
00157     if (buf_end - buf < EA_PREAMBLE_SIZE)
00158         return AVERROR_INVALIDDATA;
00159 
00160     if (AV_RL32(buf)==MVIh_TAG||AV_RB32(buf)==MVIh_TAG) {
00161         cmv_process_header(s, buf+EA_PREAMBLE_SIZE, buf_end);
00162         return buf_size;
00163     }
00164 
00165     if (av_image_check_size(s->width, s->height, 0, s->avctx))
00166         return -1;
00167 
00168     /* shuffle */
00169     if (s->last2_frame.data[0])
00170         avctx->release_buffer(avctx, &s->last2_frame);
00171     FFSWAP(AVFrame, s->last_frame, s->last2_frame);
00172     FFSWAP(AVFrame, s->frame, s->last_frame);
00173 
00174     s->frame.reference = 1;
00175     s->frame.buffer_hints = FF_BUFFER_HINTS_VALID;
00176     if (avctx->get_buffer(avctx, &s->frame)<0) {
00177         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00178         return -1;
00179     }
00180 
00181     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00182 
00183     buf += EA_PREAMBLE_SIZE;
00184     if ((buf[0]&1)) {  // subtype
00185         cmv_decode_inter(s, buf+2, buf_end);
00186         s->frame.key_frame = 0;
00187         s->frame.pict_type = AV_PICTURE_TYPE_P;
00188     }else{
00189         s->frame.key_frame = 1;
00190         s->frame.pict_type = AV_PICTURE_TYPE_I;
00191         cmv_decode_intra(s, buf+2, buf_end);
00192     }
00193 
00194     *data_size = sizeof(AVFrame);
00195     *(AVFrame*)data = s->frame;
00196 
00197     return buf_size;
00198 }
00199 
00200 static av_cold int cmv_decode_end(AVCodecContext *avctx){
00201     CmvContext *s = avctx->priv_data;
00202     if (s->frame.data[0])
00203         s->avctx->release_buffer(avctx, &s->frame);
00204     if (s->last_frame.data[0])
00205         s->avctx->release_buffer(avctx, &s->last_frame);
00206     if (s->last2_frame.data[0])
00207         s->avctx->release_buffer(avctx, &s->last2_frame);
00208 
00209     return 0;
00210 }
00211 
00212 AVCodec ff_eacmv_decoder = {
00213     .name           = "eacmv",
00214     .type           = AVMEDIA_TYPE_VIDEO,
00215     .id             = CODEC_ID_CMV,
00216     .priv_data_size = sizeof(CmvContext),
00217     .init           = cmv_decode_init,
00218     .close          = cmv_decode_end,
00219     .decode         = cmv_decode_frame,
00220     .capabilities   = CODEC_CAP_DR1,
00221     .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts CMV video"),
00222 };