Browse code

V210 Uncompressed 4:2:2 10-bit encoder and decoder

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

Baptiste Coudurier authored on 2009/05/13 04:56:48
Showing 7 changed files
... ...
@@ -17,6 +17,7 @@ version <next>:
17 17
 - introduced avlanguage helpers in libavformat
18 18
 - 8088flex TMV demuxer and decoder
19 19
 - per-stream language-tags extraction in asfdec
20
+- V210 decoder and encoder
20 21
 
21 22
 
22 23
 version 0.5:
... ...
@@ -442,6 +442,7 @@ following image formats are supported:
442 442
     @tab encoding supported through external library libtheora
443 443
 @item Tiertex Limited SEQ video  @tab     @tab  X
444 444
     @tab Codec used in DOS CD-ROM FlashBack game.
445
+@item V210 Quicktime Uncompressed 4:2:2 10-bit     @tab  X  @tab  X
445 446
 @item VMware Screen Codec / VMware Video  @tab     @tab  X
446 447
     @tab Codec used in videos captured by VMware.
447 448
 @item Westwood Studios VQA (Vector Quantized Animation) video  @tab     @tab  X
... ...
@@ -230,6 +230,8 @@ OBJS-$(CONFIG_TSCC_DECODER)            += tscc.o msrledec.o
230 230
 OBJS-$(CONFIG_TTA_DECODER)             += tta.o
231 231
 OBJS-$(CONFIG_TXD_DECODER)             += txd.o s3tc.o
232 232
 OBJS-$(CONFIG_ULTI_DECODER)            += ulti.o
233
+OBJS-$(CONFIG_V210_DECODER)            += v210dec.o
234
+OBJS-$(CONFIG_V210_ENCODER)            += v210enc.o
233 235
 OBJS-$(CONFIG_V210X_DECODER)           += v210x.o
234 236
 OBJS-$(CONFIG_VB_DECODER)              += vb.o
235 237
 OBJS-$(CONFIG_VC1_DECODER)             += vc1.o vc1data.o vc1dsp.o msmpeg4data.o h263dec.o h263.o intrax8.o intrax8dsp.o error_resilience.o mpegvideo.o msmpeg4.o
... ...
@@ -168,6 +168,7 @@ void avcodec_register_all(void)
168 168
     REGISTER_DECODER (TSCC, tscc);
169 169
     REGISTER_DECODER (TXD, txd);
170 170
     REGISTER_DECODER (ULTI, ulti);
171
+    REGISTER_ENCDEC  (V210,  v210);
171 172
     REGISTER_DECODER (V210X, v210x);
172 173
     REGISTER_DECODER (VB, vb);
173 174
     REGISTER_DECODER (VC1, vc1);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 28
33
+#define LIBAVCODEC_VERSION_MINOR 29
34 34
 #define LIBAVCODEC_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -195,6 +195,7 @@ enum CodecID {
195 195
     CODEC_ID_AURA2,
196 196
     CODEC_ID_V210X,
197 197
     CODEC_ID_TMV,
198
+    CODEC_ID_V210,
198 199
 
199 200
     /* various PCM "codecs" */
200 201
     CODEC_ID_PCM_S16LE= 0x10000,
201 202
new file mode 100644
... ...
@@ -0,0 +1,132 @@
0
+/*
1
+ * V210 decoder
2
+ *
3
+ * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
4
+ * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+#include "avcodec.h"
24
+#include "libavutil/bswap.h"
25
+
26
+static av_cold int decode_init(AVCodecContext *avctx)
27
+{
28
+    if (avctx->width & 1) {
29
+        av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
30
+        return -1;
31
+    }
32
+    if (avcodec_check_dimensions(avctx, avctx->width, avctx->height) < 0)
33
+        return -1;
34
+    avctx->pix_fmt = PIX_FMT_YUV422P16;
35
+    avctx->bits_per_raw_sample = 10;
36
+
37
+    avctx->coded_frame = avcodec_alloc_frame();
38
+
39
+    return 0;
40
+}
41
+
42
+static int decode_frame(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
43
+{
44
+    int h, w;
45
+    AVFrame *pic = avctx->coded_frame;
46
+    const uint8_t *psrc = avpkt->data;
47
+    uint16_t *y, *u, *v;
48
+    int aligned_width = ((avctx->width + 47) / 48) * 48;
49
+    int stride = aligned_width * 8 / 3;
50
+
51
+    if (pic->data[0])
52
+        avctx->release_buffer(avctx, pic);
53
+
54
+    if (avpkt->size < stride * avctx->height) {
55
+        av_log(avctx, AV_LOG_ERROR, "packet too small\n");
56
+        return -1;
57
+    }
58
+
59
+    pic->reference = 0;
60
+    if (avctx->get_buffer(avctx, pic) < 0)
61
+        return -1;
62
+
63
+    y = pic->data[0];
64
+    u = pic->data[1];
65
+    v = pic->data[2];
66
+    pic->pict_type = FF_I_TYPE;
67
+    pic->key_frame = 1;
68
+
69
+#define READ_PIXELS(a, b, c)       \
70
+    do {                           \
71
+        val = le2me_32(*src++);      \
72
+        *a++ =  val <<  6;           \
73
+        *b++ = (val >>  4) & 0xFFC0; \
74
+        *c++ = (val >> 14) & 0xFFC0; \
75
+    } while (0);
76
+
77
+    for (h = 0; h < avctx->height; h++) {
78
+        const uint32_t *src = psrc;
79
+        uint32_t val;
80
+        for (w = 0; w < avctx->width - 5; w += 6) {
81
+            READ_PIXELS(u, y, v);
82
+            READ_PIXELS(y, u, y);
83
+            READ_PIXELS(v, y, u);
84
+            READ_PIXELS(y, v, y);
85
+        }
86
+        if (w < avctx->width - 1) {
87
+            READ_PIXELS(u, y, v);
88
+
89
+            val  = le2me_32(*src++);
90
+            *y++ =  val <<  6;
91
+        }
92
+        if (w < avctx->width - 3) {
93
+            *u++ = (val >>  4) & 0xFFC0;
94
+            *y++ = (val >> 14) & 0xFFC0;
95
+
96
+            val  = le2me_32(*src++);
97
+            *v++ =  val <<  6;
98
+            *y++ = (val >>  4) & 0xFFC0;
99
+        }
100
+
101
+        psrc += stride;
102
+        y += pic->linesize[0]/2 - avctx->width;
103
+        u += pic->linesize[1]/2 - avctx->width/2;
104
+        v += pic->linesize[2]/2 - avctx->width/2;
105
+    }
106
+
107
+    *data_size = sizeof(AVFrame);
108
+    *(AVFrame*)data = *avctx->coded_frame;
109
+
110
+    return avpkt->size;
111
+}
112
+
113
+static av_cold int decode_close(AVCodecContext *avctx)
114
+{
115
+    av_freep(&avctx->coded_frame);
116
+
117
+    return 0;
118
+}
119
+
120
+AVCodec v210_decoder = {
121
+    "v210",
122
+    CODEC_TYPE_VIDEO,
123
+    CODEC_ID_V210,
124
+    0,
125
+    decode_init,
126
+    NULL,
127
+    decode_close,
128
+    decode_frame,
129
+    CODEC_CAP_DR1,
130
+    .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
131
+};
0 132
new file mode 100644
... ...
@@ -0,0 +1,129 @@
0
+/*
1
+ * V210 encoder
2
+ *
3
+ * Copyright (C) 2009 Michael Niedermayer <michaelni@gmx.at>
4
+ * Copyright (c) 2009 Baptiste Coudurier <baptiste dot coudurier at gmail dot com>
5
+ *
6
+ * This file is part of FFmpeg.
7
+ *
8
+ * FFmpeg is free software; you can redistribute it and/or
9
+ * modify it under the terms of the GNU Lesser General Public
10
+ * License as published by the Free Software Foundation; either
11
+ * version 2.1 of the License, or (at your option) any later version.
12
+ *
13
+ * FFmpeg is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
+ * Lesser General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU Lesser General Public
19
+ * License along with FFmpeg; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
+ */
22
+
23
+#include "avcodec.h"
24
+#include "libavcodec/bytestream.h"
25
+
26
+static av_cold int encode_init(AVCodecContext *avctx)
27
+{
28
+    if (avctx->width & 1) {
29
+        av_log(avctx, AV_LOG_ERROR, "v210 needs even width\n");
30
+        return -1;
31
+    }
32
+
33
+    if (avctx->pix_fmt != PIX_FMT_YUV422P16) {
34
+        av_log(avctx, AV_LOG_ERROR, "v210 needs YUV422P16\n");
35
+        return -1;
36
+    }
37
+
38
+    if (avctx->bits_per_raw_sample != 10)
39
+        av_log(avctx, AV_LOG_WARNING, "bits per raw sample: %d != 10-bit\n",
40
+               avctx->bits_per_raw_sample);
41
+
42
+    avctx->coded_frame = avcodec_alloc_frame();
43
+
44
+    avctx->coded_frame->key_frame = 1;
45
+    avctx->coded_frame->pict_type = FF_I_TYPE;
46
+
47
+    return 0;
48
+}
49
+
50
+static int encode_frame(AVCodecContext *avctx, unsigned char *buf, int buf_size, void *data)
51
+{
52
+    const AVFrame *pic = data;
53
+    int aligned_width = ((avctx->width + 47) / 48) * 48;
54
+    int stride = aligned_width * 8 / 3;
55
+    int h, w;
56
+    const uint16_t *y = pic->data[0];
57
+    const uint16_t *u = pic->data[1];
58
+    const uint16_t *v = pic->data[2];
59
+    uint8_t *p = buf;
60
+    uint8_t *pdst = buf;
61
+
62
+    if (buf_size < aligned_width * avctx->height * 8 / 3) {
63
+        av_log(avctx, AV_LOG_ERROR, "output buffer too small\n");
64
+        return -1;
65
+    }
66
+
67
+#define WRITE_PIXELS(a, b, c)           \
68
+    do {                                \
69
+        val =  (*a++           >>  6) | \
70
+              ((*b++ & 0xFFC0) <<  4);  \
71
+        val|=  (*c++ & 0xFFC0) << 14;   \
72
+        bytestream_put_le32(&p, val);   \
73
+    } while (0);
74
+
75
+    for (h = 0; h < avctx->height; h++) {
76
+        uint32_t val;
77
+        for (w = 0; w < avctx->width - 5; w += 6) {
78
+            WRITE_PIXELS(u, y, v);
79
+            WRITE_PIXELS(y, u, y);
80
+            WRITE_PIXELS(v, y, u);
81
+            WRITE_PIXELS(y, v, y);
82
+        }
83
+        if (w < avctx->width - 1) {
84
+            WRITE_PIXELS(u, y, v);
85
+
86
+            val =   *y++           >>  6;
87
+            if (w == avctx->width - 2)
88
+                bytestream_put_le32(&p, val);
89
+        }
90
+        if (w < avctx->width - 3) {
91
+            val |=((*u++ & 0xFFC0) <<  4) |
92
+                  ((*y++ & 0xFFC0) << 14);
93
+            bytestream_put_le32(&p, val);
94
+
95
+            val =  (*v++           >>  6) |
96
+                   (*y++ & 0xFFC0) <<  4;
97
+            bytestream_put_le32(&p, val);
98
+        }
99
+
100
+        pdst += stride;
101
+        memset(p, 0, pdst - p);
102
+        p = pdst;
103
+        y += pic->linesize[0]/2 - avctx->width;
104
+        u += pic->linesize[1]/2 - avctx->width/2;
105
+        v += pic->linesize[2]/2 - avctx->width/2;
106
+    }
107
+
108
+    return p - buf;
109
+}
110
+
111
+static av_cold int encode_close(AVCodecContext *avctx)
112
+{
113
+    av_freep(&avctx->coded_frame);
114
+
115
+    return 0;
116
+}
117
+
118
+AVCodec v210_encoder = {
119
+    "v210",
120
+    CODEC_TYPE_VIDEO,
121
+    CODEC_ID_V210,
122
+    0,
123
+    encode_init,
124
+    encode_frame,
125
+    encode_close,
126
+    .pix_fmts = (enum PixelFormat[]){PIX_FMT_YUV422P16, -1},
127
+    .long_name = NULL_IF_CONFIG_SMALL("Uncompressed 4:2:2 10-bit"),
128
+};