Browse code

Electronic Arts Madcow decoder

Originally committed as revision 19178 to svn://svn.ffmpeg.org/ffmpeg/trunk

Peter Ross authored on 2009/06/13 11:19:41
Showing 6 changed files
... ...
@@ -24,6 +24,7 @@ version <next>:
24 24
 - SoX native format muxer and demuxer
25 25
 - AMR-NB decoding/encoding, AMR-WB decoding via OpenCORE libraries
26 26
 - DPX image decoder
27
+- Electronic Arts Madcow decoder
27 28
 
28 29
 
29 30
 
... ...
@@ -356,6 +356,7 @@ following image formats are supported:
356 356
     @tab Codec originally used in Feeble Files game.
357 357
 @item Electronic Arts CMV video  @tab     @tab  X
358 358
     @tab Used in NHL 95 game.
359
+@item Electronic Arts Madcow video  @tab     @tab  X
359 360
 @item Electronic Arts TGV video  @tab     @tab  X
360 361
 @item Electronic Arts TGQ video  @tab     @tab  X
361 362
 @item Electronic Arts TQI video  @tab     @tab  X
... ...
@@ -76,6 +76,7 @@ OBJS-$(CONFIG_DVVIDEO_ENCODER)         += dv.o
76 76
 OBJS-$(CONFIG_DXA_DECODER)             += dxa.o
77 77
 OBJS-$(CONFIG_EAC3_DECODER)            += eac3dec.o ac3dec.o ac3tab.o ac3dec_data.o ac3.o
78 78
 OBJS-$(CONFIG_EACMV_DECODER)           += eacmv.o
79
+OBJS-$(CONFIG_EAMAD_DECODER)           += eamad.o eaidct.o
79 80
 OBJS-$(CONFIG_EATGQ_DECODER)           += eatgq.o eaidct.o
80 81
 OBJS-$(CONFIG_EATGV_DECODER)           += eatgv.o
81 82
 OBJS-$(CONFIG_EATQI_DECODER)           += eatqi.o eaidct.o mpeg12.o mpeg12data.o mpegvideo.o error_resilience.o
... ...
@@ -81,6 +81,7 @@ void avcodec_register_all(void)
81 81
     REGISTER_ENCDEC  (DVVIDEO, dvvideo);
82 82
     REGISTER_DECODER (DXA, dxa);
83 83
     REGISTER_DECODER (EACMV, eacmv);
84
+    REGISTER_DECODER (EAMAD, eamad);
84 85
     REGISTER_DECODER (EATGQ, eatgq);
85 86
     REGISTER_DECODER (EATGV, eatgv);
86 87
     REGISTER_DECODER (EATQI, eatqi);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 30
33
+#define LIBAVCODEC_VERSION_MINOR 31
34 34
 #define LIBAVCODEC_VERSION_MICRO  2
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -197,6 +197,7 @@ enum CodecID {
197 197
     CODEC_ID_TMV,
198 198
     CODEC_ID_V210,
199 199
     CODEC_ID_DPX,
200
+    CODEC_ID_MAD,
200 201
 
201 202
     /* various PCM "codecs" */
202 203
     CODEC_ID_PCM_S16LE= 0x10000,
203 204
new file mode 100644
... ...
@@ -0,0 +1,318 @@
0
+/*
1
+ * Electronic Arts Madcow Video Decoder
2
+ * Copyright (c) 2007-2009 Peter Ross
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
+ */
20
+
21
+/**
22
+ * @file libavcodec/eamad.c
23
+ * Electronic Arts Madcow Video Decoder
24
+ * by Peter Ross <pross@xvid.org>
25
+ *
26
+ * Technical details here:
27
+ * http://wiki.multimedia.cx/index.php?title=Electronic_Arts_MAD
28
+ */
29
+
30
+#include "avcodec.h"
31
+#include "get_bits.h"
32
+#include "dsputil.h"
33
+#include "aandcttab.h"
34
+#include "mpeg12.h"
35
+#include "mpeg12data.h"
36
+
37
+#define EA_PREAMBLE_SIZE    8
38
+#define MADk_TAG MKTAG('M', 'A', 'D', 'k')    /* MAD i-frame */
39
+#define MADm_TAG MKTAG('M', 'A', 'D', 'm')    /* MAD p-frame */
40
+#define MADe_TAG MKTAG('M', 'A', 'D', 'e')    /* MAD lqp-frame */
41
+
42
+typedef struct MadContext {
43
+    MpegEncContext s;
44
+    AVFrame frame;
45
+    AVFrame last_frame;
46
+    void *bitstream_buf;
47
+    unsigned int bitstream_buf_size;
48
+    DECLARE_ALIGNED_16(DCTELEM, block[64]);
49
+} MadContext;
50
+
51
+static void bswap16_buf(uint16_t *dst, const uint16_t *src, int count)
52
+{
53
+    int i;
54
+    for (i=0; i<count; i++)
55
+        dst[i] = bswap_16(src[i]);
56
+}
57
+
58
+static av_cold int decode_init(AVCodecContext *avctx)
59
+{
60
+    MadContext *t = avctx->priv_data;
61
+    MpegEncContext *s = &t->s;
62
+    s->avctx = avctx;
63
+    avctx->pix_fmt = PIX_FMT_YUV420P;
64
+    if (avctx->idct_algo == FF_IDCT_AUTO)
65
+        avctx->idct_algo = FF_IDCT_EA;
66
+    dsputil_init(&s->dsp, avctx);
67
+    ff_init_scantable(s->dsp.idct_permutation, &s->intra_scantable, ff_zigzag_direct);
68
+    ff_mpeg12_init_vlcs();
69
+    return 0;
70
+}
71
+
72
+static inline void comp(unsigned char *dst, int dst_stride,
73
+                        unsigned char *src, int src_stride, int add)
74
+{
75
+    int j, i;
76
+    for (j=0; j<8; j++)
77
+        for (i=0; i<8; i++)
78
+            dst[j*dst_stride + i] = av_clip_uint8(src[j*src_stride + i] + add);
79
+}
80
+
81
+static inline void comp_block(MadContext *t, int mb_x, int mb_y,
82
+                              int j, int mv_x, int mv_y, int add)
83
+{
84
+    MpegEncContext *s = &t->s;
85
+    if (j < 4) {
86
+        comp(t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
87
+             t->frame.linesize[0],
88
+             t->last_frame.data[0] + (mb_y*16 + ((j&2)<<2) + mv_y)*t->last_frame.linesize[0] + mb_x*16 + ((j&1)<<3) + mv_x,
89
+             t->last_frame.linesize[0], add);
90
+    } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
91
+        int index = j - 3;
92
+        comp(t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x * 8,
93
+             t->frame.linesize[index],
94
+             t->last_frame.data[index] + (mb_y * 8 + (mv_y/2))*t->last_frame.linesize[index] + mb_x * 8 + (mv_x/2),
95
+             t->last_frame.linesize[index], add);
96
+    }
97
+}
98
+
99
+static inline void idct_put(MadContext *t, DCTELEM *block, int mb_x, int mb_y, int j)
100
+{
101
+    MpegEncContext *s = &t->s;
102
+    if (j < 4) {
103
+        s->dsp.idct_put(
104
+            t->frame.data[0] + (mb_y*16 + ((j&2)<<2))*t->frame.linesize[0] + mb_x*16 + ((j&1)<<3),
105
+            t->frame.linesize[0], block);
106
+    } else if (!(s->avctx->flags & CODEC_FLAG_GRAY)) {
107
+        int index = j - 3;
108
+        s->dsp.idct_put(
109
+            t->frame.data[index] + (mb_y*8)*t->frame.linesize[index] + mb_x*8,
110
+            t->frame.linesize[index], block);
111
+    }
112
+}
113
+
114
+static inline void decode_block_intra(MadContext * t, DCTELEM * block)
115
+{
116
+    MpegEncContext *s = &t->s;
117
+    int level, i, j, run;
118
+    RLTable *rl = &ff_rl_mpeg1;
119
+    const uint8_t *scantable = s->intra_scantable.permutated;
120
+    int16_t *quant_matrix = s->intra_matrix;
121
+
122
+    block[0] = (128 + get_sbits(&s->gb, 8)) * quant_matrix[0];
123
+
124
+    /* The RL decoder is derived from mpeg1_decode_block_intra;
125
+       Escaped level and run values a decoded differently */
126
+    {
127
+        OPEN_READER(re, &s->gb);
128
+        /* now quantify & encode AC coefficients */
129
+        for (;;) {
130
+            UPDATE_CACHE(re, &s->gb);
131
+            GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TEX_VLC_BITS, 2, 0);
132
+
133
+            if (level == 127) {
134
+                break;
135
+            } else if (level != 0) {
136
+                i += run;
137
+                j = scantable[i];
138
+                level = (level*quant_matrix[j]) >> 4;
139
+                level = (level-1)|1;
140
+                level = (level ^ SHOW_SBITS(re, &s->gb, 1)) - SHOW_SBITS(re, &s->gb, 1);
141
+                LAST_SKIP_BITS(re, &s->gb, 1);
142
+            } else {
143
+                /* escape */
144
+                UPDATE_CACHE(re, &s->gb);
145
+                level = SHOW_SBITS(re, &s->gb, 10); SKIP_BITS(re, &s->gb, 10);
146
+
147
+                UPDATE_CACHE(re, &s->gb);
148
+                run = SHOW_UBITS(re, &s->gb, 6)+1; LAST_SKIP_BITS(re, &s->gb, 6);
149
+
150
+                i += run;
151
+                j = scantable[i];
152
+                if (level < 0) {
153
+                    level = -level;
154
+                    level = (level*quant_matrix[j]) >> 4;
155
+                    level = (level-1)|1;
156
+                    level = -level;
157
+                } else {
158
+                    level = (level*quant_matrix[j]) >> 4;
159
+                    level = (level-1)|1;
160
+                }
161
+            }
162
+            if (i > 63) {
163
+                av_log(s->avctx, AV_LOG_ERROR, "ac-tex damaged at %d %d\n", s->mb_x, s->mb_y);
164
+                return;
165
+            }
166
+
167
+            block[j] = level;
168
+        }
169
+        CLOSE_READER(re, &s->gb);
170
+    }
171
+}
172
+
173
+static int decode_motion(GetBitContext *gb)
174
+{
175
+    int value = 0;
176
+    if (get_bits1(gb)) {
177
+        if (get_bits1(gb))
178
+            value = -17;
179
+        value += get_bits(gb, 4) + 1;
180
+    }
181
+    return value;
182
+}
183
+
184
+static void decode_mb(MadContext *t, int inter)
185
+{
186
+    MpegEncContext *s = &t->s;
187
+    int mv_map = 0;
188
+    int mv_x, mv_y;
189
+    int j;
190
+
191
+    if (inter) {
192
+        int v = decode210(&s->gb);
193
+        if (v < 2) {
194
+            mv_map = v ? get_bits(&s->gb, 6) : 63;
195
+            mv_x = decode_motion(&s->gb);
196
+            mv_y = decode_motion(&s->gb);
197
+        } else {
198
+            mv_map = 0;
199
+        }
200
+    }
201
+
202
+    for (j=0; j<6; j++) {
203
+        if (mv_map & (1<<j)) {  // mv_x and mv_y are guarded by mv_map
204
+            int add = 2*decode_motion(&s->gb);
205
+            comp_block(t, s->mb_x, s->mb_y, j, mv_x, mv_y, add);
206
+        } else {
207
+            s->dsp.clear_block(t->block);
208
+            decode_block_intra(t, t->block);
209
+            idct_put(t, t->block, s->mb_x, s->mb_y, j);
210
+        }
211
+    }
212
+}
213
+
214
+static void calc_intra_matrix(MadContext *t, int qscale)
215
+{
216
+    MpegEncContext *s = &t->s;
217
+    int i;
218
+
219
+    if (s->avctx->idct_algo == FF_IDCT_EA) {
220
+        s->intra_matrix[0] = (ff_inv_aanscales[0]*ff_mpeg1_default_intra_matrix[0]) >> 11;
221
+        for (i=1; i<64; i++)
222
+            s->intra_matrix[i] = (ff_inv_aanscales[i]*ff_mpeg1_default_intra_matrix[i]*qscale + 32) >> 10;
223
+    } else {
224
+        s->intra_matrix[0] = ff_mpeg1_default_intra_matrix[0];
225
+        for (i=1; i<64; i++)
226
+            s->intra_matrix[i] = (ff_mpeg1_default_intra_matrix[i]*qscale) << 1;
227
+    }
228
+}
229
+
230
+static int decode_frame(AVCodecContext *avctx,
231
+                        void *data, int *data_size,
232
+                        AVPacket *avpkt)
233
+{
234
+    const uint8_t *buf = avpkt->data;
235
+    int buf_size       = avpkt->size;
236
+    const uint8_t *buf_end = buf+buf_size;
237
+    MadContext *t     = avctx->priv_data;
238
+    MpegEncContext *s = &t->s;
239
+    int chunk_type;
240
+    int inter;
241
+
242
+    if (buf_size < 17) {
243
+        av_log(avctx, AV_LOG_ERROR, "Input buffer too small\n");
244
+        *data_size = 0;
245
+        return -1;
246
+    }
247
+
248
+    chunk_type = AV_RL32(&buf[0]);
249
+    inter = (chunk_type == MADm_TAG || chunk_type == MADe_TAG);
250
+    buf += 8;
251
+
252
+    av_reduce(&avctx->time_base.num, &avctx->time_base.den,
253
+              AV_RL16(&buf[6]), 1000, 1<<30);
254
+
255
+    s->width  = AV_RL16(&buf[8]);
256
+    s->height = AV_RL16(&buf[10]);
257
+    calc_intra_matrix(t, buf[13]);
258
+    buf += 16;
259
+
260
+    if (avctx->width != s->width || avctx->height != s->height) {
261
+        if (avcodec_check_dimensions(avctx, s->width, s->height) < 0)
262
+            return -1;
263
+        avcodec_set_dimensions(avctx, s->width, s->height);
264
+        if (t->frame.data[0])
265
+            avctx->release_buffer(avctx, &t->frame);
266
+    }
267
+
268
+    t->frame.reference = 1;
269
+    if (!t->frame.data[0]) {
270
+        if (avctx->get_buffer(avctx, &t->frame) < 0) {
271
+            av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
272
+            return -1;
273
+        }
274
+    }
275
+
276
+    av_fast_malloc(&t->bitstream_buf, &t->bitstream_buf_size, (buf_end-buf) + FF_INPUT_BUFFER_PADDING_SIZE);
277
+    if (!t->bitstream_buf)
278
+        return AVERROR(ENOMEM);
279
+    bswap16_buf(t->bitstream_buf, (const uint16_t*)buf, (buf_end-buf)/2);
280
+    init_get_bits(&s->gb, t->bitstream_buf, 8*(buf_end-buf));
281
+
282
+    for (s->mb_y=0; s->mb_y < (avctx->height+15)/16; s->mb_y++)
283
+        for (s->mb_x=0; s->mb_x < (avctx->width +15)/16; s->mb_x++)
284
+            decode_mb(t, inter);
285
+
286
+    *data_size = sizeof(AVFrame);
287
+    *(AVFrame*)data = t->frame;
288
+
289
+    if (chunk_type != MADe_TAG)
290
+        FFSWAP(AVFrame, t->frame, t->last_frame);
291
+
292
+    return buf_size;
293
+}
294
+
295
+static av_cold int decode_end(AVCodecContext *avctx)
296
+{
297
+    MadContext *t = avctx->priv_data;
298
+    if (t->frame.data[0])
299
+        avctx->release_buffer(avctx, &t->frame);
300
+    if (t->last_frame.data[0])
301
+        avctx->release_buffer(avctx, &t->last_frame);
302
+    av_free(t->bitstream_buf);
303
+    return 0;
304
+}
305
+
306
+AVCodec eamad_decoder = {
307
+    "eamad",
308
+    CODEC_TYPE_VIDEO,
309
+    CODEC_ID_MAD,
310
+    sizeof(MadContext),
311
+    decode_init,
312
+    NULL,
313
+    decode_end,
314
+    decode_frame,
315
+    CODEC_CAP_DR1,
316
+    .long_name = NULL_IF_CONFIG_SMALL("Electronic Arts Madcow Video")
317
+};