Browse code

Silicon Graphics Motion Video Compressor 1 & 2 decoder

Signed-off-by: Peter Ross <pross@xvid.org>

Peter Ross authored on 2012/12/17 18:37:54
Showing 7 changed files
... ...
@@ -43,6 +43,7 @@ version <next>:
43 43
 - SOX Resampler support in libswresample
44 44
 - aselect filter
45 45
 - SGI RLE 8-bit decoder
46
+- Silicon Graphics Motion Video Compressor 1 & 2 decoder
46 47
 
47 48
 
48 49
 version 1.0:
... ...
@@ -667,6 +667,8 @@ following image formats are supported:
667 667
 @item SGI RLE 8-bit          @tab     @tab  X
668 668
 @item Sierra VMD video       @tab     @tab  X
669 669
     @tab Used in Sierra VMD files.
670
+@item Silicon Graphics Motion Video Compressor 1 (MVC1)  @tab     @tab  X
671
+@item Silicon Graphics Motion Video Compressor 2 (MVC2)  @tab     @tab  X
670 672
 @item Smacker video          @tab     @tab  X
671 673
     @tab Video encoding used in Smacker.
672 674
 @item SMPTE VC-1             @tab     @tab  X
... ...
@@ -310,6 +310,8 @@ OBJS-$(CONFIG_MSVIDEO1_DECODER)        += msvideo1.o
310 310
 OBJS-$(CONFIG_MSVIDEO1_ENCODER)        += msvideo1enc.o elbg.o
311 311
 OBJS-$(CONFIG_MSZH_DECODER)            += lcldec.o
312 312
 OBJS-$(CONFIG_MTS2_DECODER)            += mss4.o mss34dsp.o
313
+OBJS-$(CONFIG_MVC1_DECODER)            += mvcdec.o
314
+OBJS-$(CONFIG_MVC2_DECODER)            += mvcdec.o
313 315
 OBJS-$(CONFIG_MXPEG_DECODER)           += mxpegdec.o mjpegdec.o mjpeg.o
314 316
 OBJS-$(CONFIG_NELLYMOSER_DECODER)      += nellymoserdec.o nellymoser.o
315 317
 OBJS-$(CONFIG_NELLYMOSER_ENCODER)      += nellymoserenc.o nellymoser.o \
... ...
@@ -183,6 +183,8 @@ void avcodec_register_all(void)
183 183
     REGISTER_ENCDEC  (MSVIDEO1, msvideo1);
184 184
     REGISTER_DECODER (MSZH, mszh);
185 185
     REGISTER_DECODER (MTS2, mts2);
186
+    REGISTER_DECODER (MVC1, mvc1);
187
+    REGISTER_DECODER (MVC2, mvc2);
186 188
     REGISTER_DECODER (MXPEG, mxpeg);
187 189
     REGISTER_DECODER (NUV, nuv);
188 190
     REGISTER_DECODER (PAF_VIDEO, paf_video);
... ...
@@ -286,6 +286,8 @@ enum AVCodecID {
286 286
     AV_CODEC_ID_CPIA       = MKBETAG('C','P','I','A'),
287 287
     AV_CODEC_ID_XFACE      = MKBETAG('X','F','A','C'),
288 288
     AV_CODEC_ID_SGIRLE     = MKBETAG('S','G','I','R'),
289
+    AV_CODEC_ID_MVC1       = MKBETAG('M','V','C','1'),
290
+    AV_CODEC_ID_MVC2       = MKBETAG('M','V','C','2'),
289 291
 
290 292
     /* various PCM "codecs" */
291 293
     AV_CODEC_ID_FIRST_AUDIO = 0x10000,     ///< A dummy id pointing at the start of audio codecs
... ...
@@ -1066,6 +1066,20 @@ static const AVCodecDescriptor codec_descriptors[] = {
1066 1066
         .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSLESS,
1067 1067
     },
1068 1068
     {
1069
+        .id        = AV_CODEC_ID_MVC1,
1070
+        .type      = AVMEDIA_TYPE_VIDEO,
1071
+        .name      = "mvc1",
1072
+        .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 1"),
1073
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
1074
+    },
1075
+    {
1076
+        .id        = AV_CODEC_ID_MVC2,
1077
+        .type      = AVMEDIA_TYPE_VIDEO,
1078
+        .name      = "mvc2",
1079
+        .long_name = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 2"),
1080
+        .props     = AV_CODEC_PROP_INTRA_ONLY | AV_CODEC_PROP_LOSSY,
1081
+    },
1082
+    {
1069 1083
         .id        = AV_CODEC_ID_MXPEG,
1070 1084
         .type      = AVMEDIA_TYPE_VIDEO,
1071 1085
         .name      = "mxpeg",
1072 1086
new file mode 100644
... ...
@@ -0,0 +1,287 @@
0
+/*
1
+ * Silicon Graphics Motion Video Compressor 1 & 2 decoder
2
+ * Copyright (c) 2012 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * Silicon Graphics Motion Video Compressor 1 & 2 decoder
24
+ */
25
+
26
+#include "libavutil/intreadwrite.h"
27
+#include "avcodec.h"
28
+#include "bytestream.h"
29
+
30
+typedef struct MvcContext {
31
+    int vflip;
32
+} MvcContext;
33
+
34
+static av_cold int mvc_decode_init(AVCodecContext *avctx)
35
+{
36
+    MvcContext *s = avctx->priv_data;
37
+    int width  = avctx->width;
38
+    int height = avctx->height;
39
+
40
+    if (avctx->codec_id == AV_CODEC_ID_MVC1) {
41
+        width  += 3;
42
+        height += 3;
43
+    }
44
+    width  &= ~3;
45
+    height &= ~3;
46
+    if (width != avctx->width || height != avctx->height)
47
+        avcodec_set_dimensions(avctx, width, height);
48
+
49
+    avctx->pix_fmt = (avctx->codec_id == AV_CODEC_ID_MVC1) ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_BGRA;
50
+    avctx->coded_frame = avcodec_alloc_frame();
51
+    if (!avctx->coded_frame)
52
+        return AVERROR(ENOMEM);
53
+
54
+    s->vflip = avctx->extradata_size >= 9 && !memcmp(avctx->extradata + avctx->extradata_size - 9, "BottomUp", 9);
55
+    return 0;
56
+}
57
+
58
+static int decode_mvc1(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize)
59
+{
60
+    uint8_t *dst;
61
+    uint16_t v[8];
62
+    int mask, x, y, i;
63
+
64
+    x = y= 0;
65
+    while (bytestream2_get_bytes_left(gb) >= 6) {
66
+        mask = bytestream2_get_be16u(gb);
67
+        v[0] = bytestream2_get_be16u(gb);
68
+        v[1] = bytestream2_get_be16u(gb);
69
+        if ((v[0] & 0x8000)) {
70
+            if (bytestream2_get_bytes_left(gb) < 12) {
71
+                av_log(avctx, AV_LOG_WARNING, "buffer overflow\n");
72
+                return AVERROR_INVALIDDATA;
73
+            }
74
+            for (i = 2; i < 8; i++)
75
+                v[i] = bytestream2_get_be16u(gb);
76
+        } else {
77
+            v[2] = v[4] = v[6] = v[0];
78
+            v[3] = v[5] = v[7] = v[1];
79
+        }
80
+
81
+#define PIX16(target, true, false) \
82
+        i = (mask & target) ? true : false; \
83
+        AV_WN16A(dst, (v[i] & 0x7C00) | (v[i] & 0x3E0) | (v[i] & 0x1F)); \
84
+        dst += 2;
85
+
86
+#define ROW16(row, a1, a0, b1, b0) \
87
+        dst = dst_start + (y + row) * linesize + x * 2; \
88
+        PIX16(1 << (row * 4),     a1, a0) \
89
+        PIX16(1 << (row * 4 + 1), a1, a0) \
90
+        PIX16(1 << (row * 4 + 2), b1, b0) \
91
+        PIX16(1 << (row * 4 + 3), b1, b0)
92
+
93
+        ROW16(0, 0, 1, 2, 3);
94
+        ROW16(1, 0, 1, 2, 3);
95
+        ROW16(2, 4, 5, 6, 7);
96
+        ROW16(3, 4, 5, 6, 7);
97
+
98
+        x += 4;
99
+        if (x >= width) {
100
+            y += 4;
101
+            if (y >= height) {
102
+                break;
103
+            }
104
+            x = 0;
105
+        }
106
+    }
107
+    return 0;
108
+}
109
+
110
+static void set_4x4_block(uint8_t *dst, int linesize, uint32_t pixel)
111
+{
112
+    int i, j;
113
+    for (j = 0; j < 4; j++)
114
+        for (i = 0; i < 4; i++)
115
+            AV_WN32A(dst + j * linesize + i * 4, pixel);
116
+}
117
+
118
+#define PIX32(target, true, false) \
119
+    AV_WN32A(dst, (mask & target) ? v[true] : v[false]); \
120
+    dst += 4;
121
+
122
+#define ROW32(row, a1, a0, b1, b0) \
123
+    dst = dst_start + (y + row) * linesize + x * 4; \
124
+    PIX32(1 << (row * 4),     a1, a0) \
125
+    PIX32(1 << (row * 4 + 1), a1, a0) \
126
+    PIX32(1 << (row * 4 + 2), b1, b0) \
127
+    PIX32(1 << (row * 4 + 3), b1, b0)
128
+
129
+#define MVC2_BLOCK \
130
+    ROW32(0, 1, 0, 3, 2); \
131
+    ROW32(1, 1, 0, 3, 2); \
132
+    ROW32(2, 5, 4, 7, 6); \
133
+    ROW32(3, 5, 4, 7, 6);
134
+
135
+static int decode_mvc2(AVCodecContext *avctx, GetByteContext *gb, uint8_t *dst_start, int width, int height, int linesize, int vflip)
136
+{
137
+    uint8_t *dst;
138
+    uint32_t color[128], v[8];
139
+    int w, h, nb_colors, i, x, y, p0, p1, mask;
140
+
141
+    if (bytestream2_get_bytes_left(gb) < 6)
142
+        return AVERROR_INVALIDDATA;
143
+
144
+    w = bytestream2_get_be16u(gb);
145
+    h = bytestream2_get_be16u(gb);
146
+    if ((w & ~3) != width || (h & ~3) != height)
147
+        av_log(avctx, AV_LOG_WARNING, "dimension mismatch\n");
148
+
149
+    if (bytestream2_get_byteu(gb)) {
150
+        av_log_ask_for_sample(avctx, "bitmap feature\n");
151
+        return AVERROR_PATCHWELCOME;
152
+    }
153
+
154
+    nb_colors = bytestream2_get_byteu(gb);
155
+    if (bytestream2_get_bytes_left(gb) < nb_colors * 3)
156
+        return AVERROR_INVALIDDATA;
157
+    for (i = 0; i < FFMIN(nb_colors, 128); i++)
158
+        color[i] = 0xFF000000 | bytestream2_get_be24u(gb);
159
+    if (nb_colors > 128)
160
+        bytestream2_skip(gb, (nb_colors - 128) * 3);
161
+
162
+    if (vflip) {
163
+        dst_start += (height - 1) * linesize;
164
+        linesize = -linesize;
165
+    }
166
+    x = y = 0;
167
+    while (bytestream2_get_bytes_left(gb) >= 1) {
168
+        p0 = bytestream2_get_byteu(gb);
169
+        if ((p0 & 0x80)) {
170
+            if ((p0 & 0x40)) {
171
+                p0 &= 0x3F;
172
+                p0 = (p0 << 2) | (p0 >> 4);
173
+                set_4x4_block(dst_start + y * linesize + x * 4, linesize, 0xFF000000 | (p0 << 16) | (p0 << 8) | p0);
174
+            } else {
175
+                int g, r;
176
+                p0 &= 0x3F;
177
+                p0 = (p0 << 2) | (p0 >> 4);
178
+                if (bytestream2_get_bytes_left(gb) < 2)
179
+                    return AVERROR_INVALIDDATA;
180
+                g = bytestream2_get_byteu(gb);
181
+                r = bytestream2_get_byteu(gb);
182
+                set_4x4_block(dst_start + y * linesize + x * 4, linesize, 0xFF000000 | (r << 16) | (g << 8) | p0);
183
+            }
184
+        } else {
185
+            if (bytestream2_get_bytes_left(gb) < 1)
186
+                return AVERROR_INVALIDDATA;
187
+            p1 = bytestream2_get_byteu(gb);
188
+            if ((p1 & 0x80)) {
189
+                if ((p0 & 0x7F) == (p1 & 0x7F)) {
190
+                    set_4x4_block(dst_start + y * linesize + x * 4, linesize, color[p0 & 0x7F]);
191
+                } else {
192
+                    if (bytestream2_get_bytes_left(gb) < 2)
193
+                        return AVERROR_INVALIDDATA;
194
+                    v[0] = v[2] = v[4] = v[6] = color[p0 & 0x7F];
195
+                    v[1] = v[3] = v[5] = v[7] = color[p1 & 0x7F];
196
+                    mask = bytestream2_get_le16u(gb);
197
+                    MVC2_BLOCK
198
+                }
199
+            } else {
200
+                if (bytestream2_get_bytes_left(gb) < 8)
201
+                    return AVERROR_INVALIDDATA;
202
+                v[0] = color[p0 & 0x7F];
203
+                v[1] = color[p1 & 0x7F];
204
+                for (i = 2; i < 8; i++)
205
+                    v[i] = color[bytestream2_get_byteu(gb) & 0x7F];
206
+                mask = bytestream2_get_le16u(gb);
207
+                MVC2_BLOCK
208
+            }
209
+        }
210
+
211
+        x += 4;
212
+        if (x >= width) {
213
+            y += 4;
214
+            if (y >= height)
215
+                break;
216
+            x = 0;
217
+        }
218
+    }
219
+    return 0;
220
+}
221
+
222
+static int mvc_decode_frame(AVCodecContext *avctx,
223
+                            void *data, int *got_frame,
224
+                            AVPacket *avpkt)
225
+{
226
+    MvcContext *s = avctx->priv_data;
227
+    GetByteContext gb;
228
+    int ret;
229
+
230
+    avctx->coded_frame->reference = 3;
231
+    avctx->coded_frame->buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
232
+                            FF_BUFFER_HINTS_REUSABLE | FF_BUFFER_HINTS_READABLE;
233
+    ret = avctx->reget_buffer(avctx, avctx->coded_frame);
234
+    if (ret < 0) {
235
+        av_log (avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
236
+        return AVERROR(ENOMEM);
237
+    }
238
+
239
+    bytestream2_init(&gb, avpkt->data, avpkt->size);
240
+    if (avctx->codec_id == AV_CODEC_ID_MVC1)
241
+        ret = decode_mvc1(avctx, &gb, avctx->coded_frame->data[0], avctx->width, avctx->height, avctx->coded_frame->linesize[0]);
242
+    else
243
+        ret = decode_mvc2(avctx, &gb, avctx->coded_frame->data[0], avctx->width, avctx->height, avctx->coded_frame->linesize[0], s->vflip);
244
+    if (ret < 0)
245
+        return ret;
246
+
247
+    *got_frame      = 1;
248
+    *(AVFrame*)data = *avctx->coded_frame;
249
+    return avpkt->size;
250
+}
251
+
252
+static av_cold int mvc_decode_end(AVCodecContext *avctx)
253
+{
254
+    if (avctx->coded_frame->data[0])
255
+        avctx->release_buffer(avctx, avctx->coded_frame);
256
+    av_freep(&avctx->coded_frame);
257
+    return 0;
258
+}
259
+
260
+#if CONFIG_MVC1_DECODER
261
+AVCodec ff_mvc1_decoder = {
262
+    .name           = "mvc1",
263
+    .type           = AVMEDIA_TYPE_VIDEO,
264
+    .id             = AV_CODEC_ID_MVC1,
265
+    .priv_data_size = sizeof(MvcContext),
266
+    .init           = mvc_decode_init,
267
+    .close          = mvc_decode_end,
268
+    .decode         = mvc_decode_frame,
269
+    .capabilities   = CODEC_CAP_DR1,
270
+    .long_name      = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 1"),
271
+};
272
+#endif
273
+
274
+#if CONFIG_MVC1_DECODER
275
+AVCodec ff_mvc2_decoder = {
276
+    .name           = "mvc2",
277
+    .type           = AVMEDIA_TYPE_VIDEO,
278
+    .id             = AV_CODEC_ID_MVC2,
279
+    .priv_data_size = sizeof(MvcContext),
280
+    .init           = mvc_decode_init,
281
+    .close          = mvc_decode_end,
282
+    .decode         = mvc_decode_frame,
283
+    .capabilities   = CODEC_CAP_DR1,
284
+    .long_name      = NULL_IF_CONFIG_SMALL("Silicon Graphics Motion Video Compressor 2"),
285
+};
286
+#endif