• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

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

Generated on Fri Sep 16 2011 17:17:36 for FFmpeg by  doxygen 1.7.1