]> git.sesse.net Git - ffmpeg/blob - libavcodec/libopenjpegdec.c
libopenjpeg: K&R formatting cosmetics
[ffmpeg] / libavcodec / libopenjpegdec.c
1 /*
2  * JPEG 2000 decoding support via OpenJPEG
3  * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
4  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * JPEG 2000 decoder using libopenjpeg
25  */
26
27 #define  OPJ_STATIC
28 #include <openjpeg.h>
29
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
33 #include "avcodec.h"
34 #include "thread.h"
35
36 #define JP2_SIG_TYPE    0x6A502020
37 #define JP2_SIG_VALUE   0x0D0A870A
38
39 typedef struct {
40     AVClass *class;
41     opj_dparameters_t dec_params;
42     AVFrame image;
43     int lowres;
44     int lowqual;
45 } LibOpenJPEGContext;
46
47 static int check_image_attributes(opj_image_t *image)
48 {
49     return image->comps[0].dx   == image->comps[1].dx   &&
50            image->comps[1].dx   == image->comps[2].dx   &&
51            image->comps[0].dy   == image->comps[1].dy   &&
52            image->comps[1].dy   == image->comps[2].dy   &&
53            image->comps[0].prec == image->comps[1].prec &&
54            image->comps[1].prec == image->comps[2].prec;
55 }
56
57 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
58 {
59     LibOpenJPEGContext *ctx = avctx->priv_data;
60
61     opj_set_default_decoder_parameters(&ctx->dec_params);
62     avctx->coded_frame = &ctx->image;
63     return 0;
64 }
65
66 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
67 {
68     LibOpenJPEGContext *ctx = avctx->priv_data;
69
70     avctx->coded_frame = &ctx->image;
71     return 0;
72 }
73
74 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
75                                     void *data, int *data_size,
76                                     AVPacket *avpkt)
77 {
78     uint8_t *buf = avpkt->data;
79     int buf_size = avpkt->size;
80     LibOpenJPEGContext *ctx = avctx->priv_data;
81     AVFrame *picture = &ctx->image, *output = data;
82     opj_dinfo_t *dec;
83     opj_cio_t *stream;
84     opj_image_t *image;
85     int width, height, has_alpha = 0, ret = -1;
86     int x, y, index;
87     uint8_t *img_ptr;
88     int adjust[4];
89
90     *data_size = 0;
91
92     // Check if input is a raw jpeg2k codestream or in jp2 wrapping
93     if ((AV_RB32(buf)     == 12)           &&
94         (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
95         (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
96         dec = opj_create_decompress(CODEC_JP2);
97     } else {
98         /* If the AVPacket contains a jp2c box, then skip to
99          * the starting byte of the codestream. */
100         if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
101             buf += 8;
102         dec = opj_create_decompress(CODEC_J2K);
103     }
104
105     if (!dec) {
106         av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
107         return -1;
108     }
109     opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
110
111     ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
112     ctx->dec_params.cp_reduce         = ctx->lowres;
113     ctx->dec_params.cp_layer          = ctx->lowqual;
114     // Tie decoder with decoding parameters
115     opj_setup_decoder(dec, &ctx->dec_params);
116     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
117
118     if (!stream) {
119         av_log(avctx, AV_LOG_ERROR,
120                "Codestream could not be opened for reading.\n");
121         opj_destroy_decompress(dec);
122         return -1;
123     }
124
125     // Decode the header only.
126     image = opj_decode_with_info(dec, stream, NULL);
127     opj_cio_close(stream);
128
129     if (!image) {
130         av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
131         opj_destroy_decompress(dec);
132         return -1;
133     }
134
135     width  = image->x1 - image->x0;
136     height = image->y1 - image->y0;
137
138     if (ctx->lowres) {
139         width  = (width  + (1 << ctx->lowres) - 1) >> ctx->lowres;
140         height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
141     }
142
143     if (av_image_check_size(width, height, 0, avctx) < 0) {
144         av_log(avctx, AV_LOG_ERROR,
145                "%dx%d dimension invalid.\n", width, height);
146         goto done;
147     }
148
149     avcodec_set_dimensions(avctx, width, height);
150
151     switch (image->numcomps) {
152     case 1:
153         avctx->pix_fmt = PIX_FMT_GRAY8;
154         break;
155     case 3:
156         if (check_image_attributes(image)) {
157             avctx->pix_fmt = PIX_FMT_RGB24;
158         } else {
159             avctx->pix_fmt = PIX_FMT_GRAY8;
160             av_log(avctx, AV_LOG_ERROR,
161                    "Only first component will be used.\n");
162         }
163         break;
164     case 4:
165         has_alpha      = 1;
166         avctx->pix_fmt = PIX_FMT_RGBA;
167         break;
168     default:
169         av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n",
170                image->numcomps);
171         goto done;
172     }
173
174     if (picture->data[0])
175         ff_thread_release_buffer(avctx, picture);
176
177     if (ff_thread_get_buffer(avctx, picture) < 0) {
178         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
179         return -1;
180     }
181
182     ff_thread_finish_setup(avctx);
183
184     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
185     // Tie decoder with decoding parameters.
186     opj_setup_decoder(dec, &ctx->dec_params);
187     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
188     if (!stream) {
189         av_log(avctx, AV_LOG_ERROR,
190                "Codestream could not be opened for reading.\n");
191         opj_destroy_decompress(dec);
192         return -1;
193     }
194
195     // Decode the codestream.
196     image = opj_decode_with_info(dec, stream, NULL);
197     opj_cio_close(stream);
198
199     for (x = 0; x < image->numcomps; x++)
200         adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
201
202     for (y = 0; y < avctx->height; y++) {
203         index   = y * avctx->width;
204         img_ptr = picture->data[0] + y * picture->linesize[0];
205         for (x = 0; x < avctx->width; x++, index++) {
206             *img_ptr++ = image->comps[0].data[index] >> adjust[0];
207             if (image->numcomps > 2 && check_image_attributes(image)) {
208                 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
209                 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
210                 if (has_alpha)
211                     *img_ptr++ = image->comps[3].data[index] >> adjust[3];
212             }
213         }
214     }
215
216     *output    = ctx->image;
217     *data_size = sizeof(AVPicture);
218     ret        = buf_size;
219
220 done:
221     opj_image_destroy(image);
222     opj_destroy_decompress(dec);
223     return ret;
224 }
225
226 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
227 {
228     LibOpenJPEGContext *ctx = avctx->priv_data;
229
230     if (ctx->image.data[0])
231         ff_thread_release_buffer(avctx, &ctx->image);
232     return 0;
233 }
234
235 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
236 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
237
238 static const AVOption options[] = {
239     { "lowqual", "Limit the number of layers used for decoding",    OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
240     { "lowres",  "Lower the decoding resolution by a power of two", OFFSET(lowres),  AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
241     { NULL },
242 };
243
244 static const AVClass class = {
245     .class_name = "libopenjpeg",
246     .item_name  = av_default_item_name,
247     .option     = options,
248     .version    = LIBAVUTIL_VERSION_INT,
249 };
250
251 AVCodec ff_libopenjpeg_decoder = {
252     .name             = "libopenjpeg",
253     .type             = AVMEDIA_TYPE_VIDEO,
254     .id               = CODEC_ID_JPEG2000,
255     .priv_data_size   = sizeof(LibOpenJPEGContext),
256     .init             = libopenjpeg_decode_init,
257     .close            = libopenjpeg_decode_close,
258     .decode           = libopenjpeg_decode_frame,
259     .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
260     .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
261     .priv_class       = &class,
262     .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
263 };