Browse code

Use libopenjpeg for jpeg2k decoding.

Patch by Jaikrishnan Menon

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

Jai Menon authored on 2009/02/06 20:49:31
Showing 8 changed files
... ...
@@ -147,6 +147,7 @@ version <next>
147 147
 - R3D REDCODE demuxer
148 148
 - ALSA support for playback and record
149 149
 - Electronic Arts TQI decoder
150
+- OpenJPEG based JPEG 2000 decoder
150 151
 
151 152
 version 0.4.9-pre1:
152 153
 
... ...
@@ -145,6 +145,7 @@ Codecs:
145 145
   kmvc.c                                Kostya Shishkov
146 146
   lcl*.c                                Roberto Togni
147 147
   libgsm.c                              Michel Bardiaux
148
+  libopenjpeg.c                         Jaikrishnan Menon
148 149
   libx264.c                             Mans Rullgard, Jason Garrett-Glaser
149 150
   loco.c                                Kostya Shishkov
150 151
   lzo.h, lzo.c                          Reimar Doeffinger
... ...
@@ -159,6 +159,7 @@ show_help(){
159 159
   echo "  --enable-libmp3lame      enable MP3 encoding via libmp3lame [no]"
160 160
   echo "  --enable-libnut          enable NUT (de)muxing via libnut,"
161 161
   echo "                           native (de)muxer exists [no]"
162
+  echo "  --enable-libopenjpeg     enable JPEG 2000 decoding via OpenJPEG [no]"
162 163
   echo "  --enable-libschroedinger enable Dirac support via libschroedinger [no]"
163 164
   echo "  --enable-libspeex        enable Speex decoding via libspeex [no]"
164 165
   echo "  --enable-libtheora       enable Theora encoding via libtheora [no]"
... ...
@@ -769,6 +770,7 @@ CONFIG_LIST="
769 769
     libgsm
770 770
     libmp3lame
771 771
     libnut
772
+    libopenjpeg
772 773
     libschroedinger
773 774
     libspeex
774 775
     libtheora
... ...
@@ -1062,6 +1064,7 @@ libgsm_encoder_deps="libgsm"
1062 1062
 libgsm_ms_decoder_deps="libgsm"
1063 1063
 libgsm_ms_encoder_deps="libgsm"
1064 1064
 libmp3lame_encoder_deps="libmp3lame"
1065
+libopenjpeg_decoder_deps="libopenjpeg"
1065 1066
 libschroedinger_decoder_deps="libschroedinger"
1066 1067
 libschroedinger_encoder_deps="libschroedinger"
1067 1068
 libspeex_decoder_deps="libspeex"
... ...
@@ -1928,6 +1931,7 @@ enabled libfaad    && require2 libfaad faad.h faacDecOpen -lfaad
1928 1928
 enabled libgsm     && require  libgsm gsm.h gsm_create -lgsm
1929 1929
 enabled libmp3lame && require  libmp3lame lame/lame.h lame_init -lmp3lame -lm
1930 1930
 enabled libnut     && require  libnut libnut.h nut_demuxer_init -lnut
1931
+enabled libopenjpeg && require libopenjpeg libopenjpeg/openjpeg.h opj_version -lopenjpeg
1931 1932
 enabled libschroedinger && add_cflags $(pkg-config --cflags schroedinger-1.0) &&
1932 1933
                            require libschroedinger schroedinger/schro.h schro_init $(pkg-config --libs schroedinger-1.0)
1933 1934
 enabled libspeex   && require  libspeex speex/speex.h speex_decoder_init -lspeex
... ...
@@ -2243,6 +2247,7 @@ echo "libfaad dlopened          ${libfaadbin-no}"
2243 2243
 echo "libgsm enabled            ${libgsm-no}"
2244 2244
 echo "libmp3lame enabled        ${libmp3lame-no}"
2245 2245
 echo "libnut enabled            ${libnut-no}"
2246
+echo "libopenjpeg enabled       ${libopenjpeg-no}"
2246 2247
 echo "libschroedinger enabled   ${libschroedinger-no}"
2247 2248
 echo "libspeex enabled          ${libspeex-no}"
2248 2249
 echo "libtheora enabled         ${libtheora-no}"
... ...
@@ -162,6 +162,7 @@ following image formats are supported:
162 162
 @item .Y.U.V       @tab X @tab X @tab one raw file per component
163 163
 @item animated GIF @tab X @tab X @tab Only uncompressed GIFs are generated.
164 164
 @item JPEG         @tab X @tab X @tab Progressive JPEG is not supported.
165
+@item JPEG 2000    @tab   @tab E @tab decoding supported through external library libopenjpeg
165 166
 @item PAM          @tab X @tab X @tab PAM is a PNM extension with alpha support.
166 167
 @item PCX          @tab   @tab X @tab PC Paintbrush
167 168
 @item PGM, PPM     @tab X @tab X
... ...
@@ -176,6 +177,8 @@ following image formats are supported:
176 176
 
177 177
 @code{X} means that encoding (resp. decoding) is supported.
178 178
 
179
+@code{E} means that support is provided through an external library.
180
+
179 181
 @section Video Codecs
180 182
 
181 183
 @multitable @columnfractions .4 .1 .1 .4
... ...
@@ -353,6 +353,7 @@ OBJS-$(CONFIG_LIBFAAC)                 += libfaac.o
353 353
 OBJS-$(CONFIG_LIBFAAD)                 += libfaad.o
354 354
 OBJS-$(CONFIG_LIBGSM)                  += libgsm.o
355 355
 OBJS-$(CONFIG_LIBMP3LAME)              += libmp3lame.o
356
+OBJS-$(CONFIG_LIBOPENJPEG)             += libopenjpeg.o
356 357
 OBJS-$(CONFIG_LIBSCHROEDINGER_DECODER) += libschroedingerdec.o libschroedinger.o libdirac_libschro.o
357 358
 OBJS-$(CONFIG_LIBSCHROEDINGER_ENCODER) += libschroedingerenc.o libschroedinger.o libdirac_libschro.o
358 359
 OBJS-$(CONFIG_LIBSPEEX)                += libspeexdec.o
... ...
@@ -294,6 +294,7 @@ void avcodec_register_all(void)
294 294
     REGISTER_ENCDEC  (LIBGSM, libgsm);
295 295
     REGISTER_ENCDEC  (LIBGSM_MS, libgsm_ms);
296 296
     REGISTER_ENCODER (LIBMP3LAME, libmp3lame);
297
+    REGISTER_DECODER (LIBOPENJPEG, libopenjpeg);
297 298
     REGISTER_ENCDEC  (LIBSCHROEDINGER, libschroedinger);
298 299
     REGISTER_DECODER (LIBSPEEX, libspeex);
299 300
     REGISTER_ENCODER (LIBTHEORA, libtheora);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/avutil.h"
31 31
 
32 32
 #define LIBAVCODEC_VERSION_MAJOR 52
33
-#define LIBAVCODEC_VERSION_MINOR 12
33
+#define LIBAVCODEC_VERSION_MINOR 13
34 34
 #define LIBAVCODEC_VERSION_MICRO  0
35 35
 
36 36
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,190 @@
0
+/*
1
+ * JPEG 2000 decoding support via OpenJPEG
2
+ * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
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 libavcodec/libopenjpeg.c
23
+* JPEG 2000 decoder using libopenjpeg
24
+*/
25
+
26
+#include "avcodec.h"
27
+#include "libavutil/intreadwrite.h"
28
+#define  OPJ_STATIC
29
+#include <libopenjpeg/openjpeg.h>
30
+
31
+#define JP2_SIG_TYPE    0x6A502020
32
+#define JP2_SIG_VALUE   0x0D0A870A
33
+
34
+typedef struct {
35
+    opj_dparameters_t dec_params;
36
+    AVFrame image;
37
+} LibOpenJPEGContext;
38
+
39
+static int check_image_attributes(opj_image_t *image)
40
+{
41
+    return(image->comps[0].dx == image->comps[1].dx &&
42
+           image->comps[1].dx == image->comps[2].dx &&
43
+           image->comps[0].dy == image->comps[1].dy &&
44
+           image->comps[1].dy == image->comps[2].dy &&
45
+           image->comps[0].prec == image->comps[1].prec &&
46
+           image->comps[1].prec == image->comps[2].prec);
47
+}
48
+
49
+static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
50
+{
51
+    LibOpenJPEGContext *ctx = avctx->priv_data;
52
+
53
+    opj_set_default_decoder_parameters(&ctx->dec_params);
54
+    avctx->coded_frame = &ctx->image;
55
+    return 0;
56
+}
57
+
58
+static int libopenjpeg_decode_frame(AVCodecContext *avctx,
59
+                                    void *data, int *data_size,
60
+                                    const uint8_t *buf, int buf_size)
61
+{
62
+    LibOpenJPEGContext *ctx = avctx->priv_data;
63
+    AVFrame *picture = &ctx->image, *output = data;
64
+    opj_dinfo_t *dec;
65
+    opj_cio_t *stream;
66
+    opj_image_t *image;
67
+    int width, height, has_alpha = 0, ret = -1;
68
+    int x, y, index;
69
+    uint8_t *img_ptr;
70
+    int adjust[4];
71
+
72
+    *data_size = 0;
73
+
74
+    // Check if input is a raw jpeg2k codestream or in jp2 wrapping
75
+    if((AV_RB32(buf) == 12) &&
76
+       (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
77
+       (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
78
+         dec = opj_create_decompress(CODEC_JP2);
79
+    } else {
80
+         dec = opj_create_decompress(CODEC_J2K);
81
+    }
82
+
83
+    if(!dec) {
84
+        av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
85
+        return -1;
86
+    }
87
+    opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
88
+
89
+    // Tie decoder with decoding parameters
90
+    opj_setup_decoder(dec, &ctx->dec_params);
91
+    stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
92
+    if(!stream) {
93
+        av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
94
+        opj_destroy_decompress(dec);
95
+        return -1;
96
+    }
97
+
98
+    // Decode the codestream
99
+    image = opj_decode_with_info(dec, stream, NULL);
100
+    opj_cio_close(stream);
101
+    if(!image) {
102
+        av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
103
+        opj_destroy_decompress(dec);
104
+        return -1;
105
+    }
106
+    width  = image->comps[0].w;
107
+    height = image->comps[0].h;
108
+    if(avcodec_check_dimensions(avctx, width, height) < 0) {
109
+        av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
110
+        goto done;
111
+    }
112
+    avcodec_set_dimensions(avctx, width, height);
113
+
114
+    switch(image->numcomps)
115
+    {
116
+        case 1:  avctx->pix_fmt = PIX_FMT_GRAY8;
117
+                 break;
118
+        case 3:  if(check_image_attributes(image)) {
119
+                     avctx->pix_fmt = PIX_FMT_RGB24;
120
+                 } else {
121
+                     avctx->pix_fmt = PIX_FMT_GRAY8;
122
+                     av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
123
+                 }
124
+                 break;
125
+        case 4:  has_alpha = 1;
126
+                 avctx->pix_fmt = PIX_FMT_RGB32;
127
+                 break;
128
+        default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
129
+                 goto done;
130
+    }
131
+
132
+    if(picture->data[0])
133
+        avctx->release_buffer(avctx, picture);
134
+
135
+    if(avctx->get_buffer(avctx, picture) < 0) {
136
+        av_log(avctx, AV_LOG_ERROR, "Couldn't allocate image buffer.\n");
137
+        return -1;
138
+    }
139
+
140
+    for(x = 0; x < image->numcomps; x++) {
141
+        adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
142
+    }
143
+
144
+    for(y = 0; y < height; y++) {
145
+        index = y*width;
146
+        img_ptr = picture->data[0] + y*picture->linesize[0];
147
+        for(x = 0; x < width; x++, index++) {
148
+            *img_ptr++ = image->comps[0].data[index] >> adjust[0];
149
+            if(image->numcomps > 2 && check_image_attributes(image)) {
150
+                *img_ptr++ = image->comps[1].data[index] >> adjust[1];
151
+                *img_ptr++ = image->comps[2].data[index] >> adjust[2];
152
+                if(has_alpha)
153
+                    *img_ptr++ = image->comps[3].data[index] >> adjust[3];
154
+            }
155
+        }
156
+    }
157
+
158
+    *output    = ctx->image;
159
+    *data_size = sizeof(AVPicture);
160
+    ret = buf_size;
161
+
162
+done:
163
+    opj_image_destroy(image);
164
+    opj_destroy_decompress(dec);
165
+    return ret;
166
+}
167
+
168
+static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
169
+{
170
+    LibOpenJPEGContext *ctx = avctx->priv_data;
171
+
172
+    if(ctx->image.data[0])
173
+        avctx->release_buffer(avctx, &ctx->image);
174
+    return 0 ;
175
+}
176
+
177
+
178
+AVCodec libopenjpeg_decoder = {
179
+    "libopenjpeg",
180
+    CODEC_TYPE_VIDEO,
181
+    CODEC_ID_JPEG2000,
182
+    sizeof(LibOpenJPEGContext),
183
+    libopenjpeg_decode_init,
184
+    NULL,
185
+    libopenjpeg_decode_close,
186
+    libopenjpeg_decode_frame,
187
+    NULL,
188
+    .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
189
+} ;