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

libavcodec/mmvideo.c

Go to the documentation of this file.
00001 /*
00002  * American Laser Games MM Video Decoder
00003  * Copyright (c) 2006,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 Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00034 #include "libavutil/intreadwrite.h"
00035 #include "avcodec.h"
00036 
00037 #define MM_PREAMBLE_SIZE    6
00038 
00039 #define MM_TYPE_INTER       0x5
00040 #define MM_TYPE_INTRA       0x8
00041 #define MM_TYPE_INTRA_HH    0xc
00042 #define MM_TYPE_INTER_HH    0xd
00043 #define MM_TYPE_INTRA_HHV   0xe
00044 #define MM_TYPE_INTER_HHV   0xf
00045 #define MM_TYPE_PALETTE     0x31
00046 
00047 typedef struct MmContext {
00048     AVCodecContext *avctx;
00049     AVFrame frame;
00050     int palette[AVPALETTE_COUNT];
00051 } MmContext;
00052 
00053 static av_cold int mm_decode_init(AVCodecContext *avctx)
00054 {
00055     MmContext *s = avctx->priv_data;
00056 
00057     s->avctx = avctx;
00058 
00059     avctx->pix_fmt = PIX_FMT_PAL8;
00060 
00061     avcodec_get_frame_defaults(&s->frame);
00062     s->frame.reference = 1;
00063 
00064     return 0;
00065 }
00066 
00067 static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
00068 {
00069     int i;
00070     buf += 4;
00071     for (i=0; i<128 && buf+2<buf_end; i++) {
00072         s->palette[i] = AV_RB24(buf);
00073         s->palette[i+128] = s->palette[i]<<2;
00074         buf += 3;
00075     }
00076 }
00077 
00082 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
00083 {
00084     int i, x, y;
00085     i=0; x=0; y=0;
00086 
00087     while(i<buf_size) {
00088         int run_length, color;
00089 
00090         if (y >= s->avctx->height)
00091             return;
00092 
00093         if (buf[i] & 0x80) {
00094             run_length = 1;
00095             color = buf[i];
00096             i++;
00097         }else{
00098             run_length = (buf[i] & 0x7f) + 2;
00099             color = buf[i+1];
00100             i+=2;
00101         }
00102 
00103         if (half_horiz)
00104             run_length *=2;
00105 
00106         if (color) {
00107             memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
00108             if (half_vert)
00109                 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
00110         }
00111         x+= run_length;
00112 
00113         if (x >= s->avctx->width) {
00114             x=0;
00115             y += 1 + half_vert;
00116         }
00117     }
00118 }
00119 
00120 /*
00121  * @param half_horiz Half horizontal resolution (0 or 1)
00122  * @param half_vert Half vertical resolution (0 or 1)
00123  */
00124 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
00125 {
00126     const int data_ptr = 2 + AV_RL16(&buf[0]);
00127     int d, r, y;
00128     d = data_ptr; r = 2; y = 0;
00129 
00130     while(r < data_ptr) {
00131         int i, j;
00132         int length = buf[r] & 0x7f;
00133         int x = buf[r+1] + ((buf[r] & 0x80) << 1);
00134         r += 2;
00135 
00136         if (length==0) {
00137             y += x;
00138             continue;
00139         }
00140 
00141         if (y + half_vert >= s->avctx->height)
00142             return;
00143 
00144         for(i=0; i<length; i++) {
00145             for(j=0; j<8; j++) {
00146                 int replace = (buf[r+i] >> (7-j)) & 1;
00147                 if (replace) {
00148                     int color = buf[d];
00149                     s->frame.data[0][y*s->frame.linesize[0] + x] = color;
00150                     if (half_horiz)
00151                         s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
00152                     if (half_vert) {
00153                         s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
00154                         if (half_horiz)
00155                             s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
00156                     }
00157                     d++;
00158                 }
00159                 x += 1 + half_horiz;
00160             }
00161         }
00162 
00163         r += length;
00164         y += 1 + half_vert;
00165     }
00166 }
00167 
00168 static int mm_decode_frame(AVCodecContext *avctx,
00169                             void *data, int *data_size,
00170                             AVPacket *avpkt)
00171 {
00172     const uint8_t *buf = avpkt->data;
00173     int buf_size = avpkt->size;
00174     MmContext *s = avctx->priv_data;
00175     const uint8_t *buf_end = buf+buf_size;
00176     int type;
00177 
00178     type = AV_RL16(&buf[0]);
00179     buf += MM_PREAMBLE_SIZE;
00180     buf_size -= MM_PREAMBLE_SIZE;
00181 
00182     if (avctx->reget_buffer(avctx, &s->frame) < 0) {
00183         av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00184         return -1;
00185     }
00186 
00187     switch(type) {
00188     case MM_TYPE_PALETTE   : mm_decode_pal(s, buf, buf_end); return buf_size;
00189     case MM_TYPE_INTRA     : mm_decode_intra(s, 0, 0, buf, buf_size); break;
00190     case MM_TYPE_INTRA_HH  : mm_decode_intra(s, 1, 0, buf, buf_size); break;
00191     case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
00192     case MM_TYPE_INTER     : mm_decode_inter(s, 0, 0, buf, buf_size); break;
00193     case MM_TYPE_INTER_HH  : mm_decode_inter(s, 1, 0, buf, buf_size); break;
00194     case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
00195     default :
00196         return -1;
00197     }
00198 
00199     memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00200 
00201     *data_size = sizeof(AVFrame);
00202     *(AVFrame*)data = s->frame;
00203 
00204     return buf_size;
00205 }
00206 
00207 static av_cold int mm_decode_end(AVCodecContext *avctx)
00208 {
00209     MmContext *s = avctx->priv_data;
00210 
00211     if(s->frame.data[0])
00212         avctx->release_buffer(avctx, &s->frame);
00213 
00214     return 0;
00215 }
00216 
00217 AVCodec ff_mmvideo_decoder = {
00218     "mmvideo",
00219     AVMEDIA_TYPE_VIDEO,
00220     CODEC_ID_MMVIDEO,
00221     sizeof(MmContext),
00222     mm_decode_init,
00223     NULL,
00224     mm_decode_end,
00225     mm_decode_frame,
00226     CODEC_CAP_DR1,
00227     .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
00228 };

Generated on Wed Apr 11 2012 07:31:34 for FFmpeg by  doxygen 1.7.1