Browse code

DPX image encoder

Peter Ross authored on 2011/03/26 13:12:35
Showing 6 changed files
... ...
@@ -6,6 +6,8 @@ version <next>:
6 6
 
7 7
 - Lots of deprecated API cruft removed
8 8
 - fft and imdct optimizations for AVX (Sandy Bridge) processors
9
+- DPX image encoder
10
+
9 11
 
10 12
 version 0.7_beta1:
11 13
 
... ...
@@ -279,7 +279,7 @@ following image formats are supported:
279 279
     @tab Only uncompressed GIFs are generated.
280 280
 @item BMP          @tab X @tab X
281 281
     @tab Microsoft BMP image
282
-@item DPX          @tab   @tab X
282
+@item DPX          @tab X @tab X
283 283
     @tab Digital Picture Exchange
284 284
 @item JPEG         @tab X @tab X
285 285
     @tab Progressive JPEG is not supported.
... ...
@@ -110,6 +110,7 @@ OBJS-$(CONFIG_DNXHD_ENCODER)           += dnxhdenc.o dnxhddata.o       \
110 110
                                           ratecontrol.o mpeg12data.o   \
111 111
                                           mpegvideo.o
112 112
 OBJS-$(CONFIG_DPX_DECODER)             += dpx.o
113
+OBJS-$(CONFIG_DPX_ENCODER)             += dpxenc.o
113 114
 OBJS-$(CONFIG_DSICINAUDIO_DECODER)     += dsicinav.o
114 115
 OBJS-$(CONFIG_DSICINVIDEO_DECODER)     += dsicinav.o
115 116
 OBJS-$(CONFIG_DVBSUB_DECODER)          += dvbsubdec.o
... ...
@@ -90,7 +90,7 @@ void avcodec_register_all(void)
90 90
     REGISTER_DECODER (CYUV, cyuv);
91 91
     REGISTER_DECODER (DFA, dfa);
92 92
     REGISTER_ENCDEC  (DNXHD, dnxhd);
93
-    REGISTER_DECODER (DPX, dpx);
93
+    REGISTER_ENCDEC  (DPX, dpx);
94 94
     REGISTER_DECODER (DSICINVIDEO, dsicinvideo);
95 95
     REGISTER_ENCDEC  (DVVIDEO, dvvideo);
96 96
     REGISTER_DECODER (DXA, dxa);
97 97
new file mode 100644
... ...
@@ -0,0 +1,178 @@
0
+/*
1
+ * DPX (.dpx) image encoder
2
+ * Copyright (c) 2011 Peter Ross <pross@xvid.org>
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/intreadwrite.h"
22
+#include "libavutil/imgutils.h"
23
+#include "avcodec.h"
24
+
25
+typedef struct DPXContext {
26
+    AVFrame picture;
27
+    int big_endian;
28
+    int bits_per_component;
29
+    int descriptor;
30
+} DPXContext;
31
+
32
+static av_cold int encode_init(AVCodecContext *avctx)
33
+{
34
+    DPXContext *s = avctx->priv_data;
35
+
36
+    avctx->coded_frame = &s->picture;
37
+    avctx->coded_frame->pict_type = FF_I_TYPE;
38
+    avctx->coded_frame->key_frame = 1;
39
+
40
+    s->big_endian         = 1;
41
+    s->bits_per_component = 8;
42
+    s->descriptor         = 50; /* RGB */
43
+
44
+    switch (avctx->pix_fmt) {
45
+    case PIX_FMT_RGB24:
46
+        break;
47
+    case PIX_FMT_RGBA:
48
+        s->descriptor = 51; /* RGBA */
49
+        break;
50
+    case PIX_FMT_RGB48LE:
51
+        s->big_endian = 0;
52
+    case PIX_FMT_RGB48BE:
53
+        s->bits_per_component = avctx->bits_per_raw_sample ? avctx->bits_per_raw_sample : 16;
54
+        break;
55
+    default:
56
+        av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
57
+        return -1;
58
+    }
59
+
60
+    return 0;
61
+}
62
+
63
+#define write16(p, value) \
64
+do { \
65
+    if (s->big_endian) AV_WB16(p, value); \
66
+    else               AV_WL16(p, value); \
67
+} while(0)
68
+
69
+#define write32(p, value) \
70
+do { \
71
+    if (s->big_endian) AV_WB32(p, value); \
72
+    else               AV_WL32(p, value); \
73
+} while(0)
74
+
75
+static void encode_rgb48_10bit(AVCodecContext *avctx, const AVPicture *pic,
76
+                               uint8_t *dst)
77
+{
78
+    DPXContext *s = avctx->priv_data;
79
+    const uint8_t *src = pic->data[0];
80
+    int x, y;
81
+
82
+    for (y = 0; y < avctx->height; y++) {
83
+        for (x = 0; x < avctx->width; x++) {
84
+            int value;
85
+            if ((avctx->pix_fmt & 1)) {
86
+                value = ((AV_RB16(src + 6*x + 4) & 0xFFC0) >> 4)
87
+                      | ((AV_RB16(src + 6*x + 2) & 0xFFC0) << 6)
88
+                      | ((AV_RB16(src + 6*x + 0) & 0xFFC0) << 16);
89
+            } else {
90
+                value = ((AV_RL16(src + 6*x + 4) & 0xFFC0) >> 4)
91
+                      | ((AV_RL16(src + 6*x + 2) & 0xFFC0) << 6)
92
+                      | ((AV_RL16(src + 6*x + 0) & 0xFFC0) << 16);
93
+            }
94
+            write32(dst, value);
95
+            dst += 4;
96
+        }
97
+        src += pic->linesize[0];
98
+    }
99
+}
100
+
101
+static int encode_frame(AVCodecContext *avctx, unsigned char *buf,
102
+                        int buf_size, void *data)
103
+{
104
+    DPXContext *s = avctx->priv_data;
105
+    int size;
106
+
107
+#define HEADER_SIZE 1664  /* DPX Generic header */
108
+    if (buf_size < HEADER_SIZE)
109
+        return -1;
110
+
111
+    memset(buf, 0, HEADER_SIZE);
112
+
113
+    /* File information header */
114
+    write32(buf,       MKBETAG('S','D','P','X'));
115
+    write32(buf +   4, HEADER_SIZE);
116
+    memcpy (buf +   8, "V1.0", 4);
117
+    write32(buf +  20, 1); /* new image */
118
+    write32(buf +  24, HEADER_SIZE);
119
+    memcpy (buf + 160, LIBAVCODEC_IDENT, FFMIN(sizeof(LIBAVCODEC_IDENT), 100));
120
+    write32(buf + 660, 0xFFFFFFFF); /* unencrypted */
121
+
122
+    /* Image information header */
123
+    write16(buf + 768, 0); /* orientation; left to right, top to bottom */
124
+    write16(buf + 770, 1); /* number of elements */
125
+    write32(buf + 772, avctx->width);
126
+    write32(buf + 776, avctx->height);
127
+    buf[800] = s->descriptor;
128
+    buf[801] = 2; /* linear transfer */
129
+    buf[802] = 2; /* linear colorimetric */
130
+    buf[803] = s->bits_per_component;
131
+    write16(buf + 804, s->bits_per_component == 10 ? 1 : 0); /* packing method */
132
+
133
+    /* Image source information header */
134
+    write32(buf + 1628, avctx->sample_aspect_ratio.num);
135
+    write32(buf + 1632, avctx->sample_aspect_ratio.den);
136
+
137
+    switch (s->bits_per_component) {
138
+    case 8:
139
+    case 16:
140
+        size = avpicture_layout(data, avctx->pix_fmt,
141
+                                avctx->width, avctx->height,
142
+                                buf + HEADER_SIZE, buf_size - HEADER_SIZE);
143
+        if (size < 0)
144
+            return size;
145
+        break;
146
+    case 10:
147
+        size = avctx->height * avctx->width * 4;
148
+        if (buf_size < HEADER_SIZE + size)
149
+            return -1;
150
+        encode_rgb48_10bit(avctx, data, buf + HEADER_SIZE);
151
+        break;
152
+    default:
153
+        av_log(avctx, AV_LOG_ERROR, "Unsupported bit depth: %d\n", s->bits_per_component);
154
+        return -1;
155
+    }
156
+
157
+    size += HEADER_SIZE;
158
+
159
+    write32(buf + 16, size); /* file size */
160
+    return size;
161
+}
162
+
163
+AVCodec ff_dpx_encoder = {
164
+    .name = "dpx",
165
+    .type = AVMEDIA_TYPE_VIDEO,
166
+    .id   = CODEC_ID_DPX,
167
+    .priv_data_size = sizeof(DPXContext),
168
+    .init   = encode_init,
169
+    .decode = encode_frame,
170
+    .pix_fmts = (const enum PixelFormat[]){
171
+        PIX_FMT_RGB24,
172
+        PIX_FMT_RGBA,
173
+        PIX_FMT_RGB48LE,
174
+        PIX_FMT_RGB48BE,
175
+        PIX_FMT_NONE},
176
+    .long_name = NULL_IF_CONFIG_SMALL("DPX image"),
177
+};
... ...
@@ -21,8 +21,8 @@
21 21
 #define AVCODEC_VERSION_H
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24
-#define LIBAVCODEC_VERSION_MINOR  1
25
-#define LIBAVCODEC_VERSION_MICRO  1
24
+#define LIBAVCODEC_VERSION_MINOR  2
25
+#define LIBAVCODEC_VERSION_MICRO  0
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \