00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 s->frame.reference = 1;
00062 if (avctx->get_buffer(avctx, &s->frame)) {
00063 av_log(s->avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00064 return -1;
00065 }
00066
00067 return 0;
00068 }
00069
00070 static void mm_decode_pal(MmContext *s, const uint8_t *buf, const uint8_t *buf_end)
00071 {
00072 int i;
00073 buf += 4;
00074 for (i=0; i<128 && buf+2<buf_end; i++) {
00075 s->palette[i] = AV_RB24(buf);
00076 s->palette[i+128] = s->palette[i]<<2;
00077 buf += 3;
00078 }
00079 }
00080
00085 static void mm_decode_intra(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
00086 {
00087 int i, x, y;
00088 i=0; x=0; y=0;
00089
00090 while(i<buf_size) {
00091 int run_length, color;
00092
00093 if (y >= s->avctx->height)
00094 return;
00095
00096 if (buf[i] & 0x80) {
00097 run_length = 1;
00098 color = buf[i];
00099 i++;
00100 }else{
00101 run_length = (buf[i] & 0x7f) + 2;
00102 color = buf[i+1];
00103 i+=2;
00104 }
00105
00106 if (half_horiz)
00107 run_length *=2;
00108
00109 if (color) {
00110 memset(s->frame.data[0] + y*s->frame.linesize[0] + x, color, run_length);
00111 if (half_vert)
00112 memset(s->frame.data[0] + (y+1)*s->frame.linesize[0] + x, color, run_length);
00113 }
00114 x+= run_length;
00115
00116 if (x >= s->avctx->width) {
00117 x=0;
00118 y += 1 + half_vert;
00119 }
00120 }
00121 }
00122
00123
00124
00125
00126
00127 static void mm_decode_inter(MmContext * s, int half_horiz, int half_vert, const uint8_t *buf, int buf_size)
00128 {
00129 const int data_ptr = 2 + AV_RL16(&buf[0]);
00130 int d, r, y;
00131 d = data_ptr; r = 2; y = 0;
00132
00133 while(r < data_ptr) {
00134 int i, j;
00135 int length = buf[r] & 0x7f;
00136 int x = buf[r+1] + ((buf[r] & 0x80) << 1);
00137 r += 2;
00138
00139 if (length==0) {
00140 y += x;
00141 continue;
00142 }
00143
00144 if (y + half_vert >= s->avctx->height)
00145 return;
00146
00147 for(i=0; i<length; i++) {
00148 for(j=0; j<8; j++) {
00149 int replace = (buf[r+i] >> (7-j)) & 1;
00150 if (replace) {
00151 int color = buf[d];
00152 s->frame.data[0][y*s->frame.linesize[0] + x] = color;
00153 if (half_horiz)
00154 s->frame.data[0][y*s->frame.linesize[0] + x + 1] = color;
00155 if (half_vert) {
00156 s->frame.data[0][(y+1)*s->frame.linesize[0] + x] = color;
00157 if (half_horiz)
00158 s->frame.data[0][(y+1)*s->frame.linesize[0] + x + 1] = color;
00159 }
00160 d++;
00161 }
00162 x += 1 + half_horiz;
00163 }
00164 }
00165
00166 r += length;
00167 y += 1 + half_vert;
00168 }
00169 }
00170
00171 static int mm_decode_frame(AVCodecContext *avctx,
00172 void *data, int *data_size,
00173 AVPacket *avpkt)
00174 {
00175 const uint8_t *buf = avpkt->data;
00176 int buf_size = avpkt->size;
00177 MmContext *s = avctx->priv_data;
00178 const uint8_t *buf_end = buf+buf_size;
00179 int type;
00180
00181 type = AV_RL16(&buf[0]);
00182 buf += MM_PREAMBLE_SIZE;
00183 buf_size -= MM_PREAMBLE_SIZE;
00184
00185 switch(type) {
00186 case MM_TYPE_PALETTE : mm_decode_pal(s, buf, buf_end); return buf_size;
00187 case MM_TYPE_INTRA : mm_decode_intra(s, 0, 0, buf, buf_size); break;
00188 case MM_TYPE_INTRA_HH : mm_decode_intra(s, 1, 0, buf, buf_size); break;
00189 case MM_TYPE_INTRA_HHV : mm_decode_intra(s, 1, 1, buf, buf_size); break;
00190 case MM_TYPE_INTER : mm_decode_inter(s, 0, 0, buf, buf_size); break;
00191 case MM_TYPE_INTER_HH : mm_decode_inter(s, 1, 0, buf, buf_size); break;
00192 case MM_TYPE_INTER_HHV : mm_decode_inter(s, 1, 1, buf, buf_size); break;
00193 default :
00194 return -1;
00195 }
00196
00197 memcpy(s->frame.data[1], s->palette, AVPALETTE_SIZE);
00198
00199 *data_size = sizeof(AVFrame);
00200 *(AVFrame*)data = s->frame;
00201
00202 return buf_size;
00203 }
00204
00205 static av_cold int mm_decode_end(AVCodecContext *avctx)
00206 {
00207 MmContext *s = avctx->priv_data;
00208
00209 if(s->frame.data[0])
00210 avctx->release_buffer(avctx, &s->frame);
00211
00212 return 0;
00213 }
00214
00215 AVCodec ff_mmvideo_decoder = {
00216 "mmvideo",
00217 AVMEDIA_TYPE_VIDEO,
00218 CODEC_ID_MMVIDEO,
00219 sizeof(MmContext),
00220 mm_decode_init,
00221 NULL,
00222 mm_decode_end,
00223 mm_decode_frame,
00224 CODEC_CAP_DR1,
00225 .long_name = NULL_IF_CONFIG_SMALL("American Laser Games MM Video"),
00226 };