2 * JPEG 2000 decoding support via OpenJPEG
3 * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
5 * This file is part of Libav.
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.
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.
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
24 * JPEG 2000 decoder using libopenjpeg
27 #include "libavutil/opt.h"
28 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
35 #define JP2_SIG_TYPE 0x6A502020
36 #define JP2_SIG_VALUE 0x0D0A870A
40 opj_dparameters_t dec_params;
46 static int check_image_attributes(opj_image_t *image)
48 return image->comps[0].dx == image->comps[1].dx &&
49 image->comps[1].dx == image->comps[2].dx &&
50 image->comps[0].dy == image->comps[1].dy &&
51 image->comps[1].dy == image->comps[2].dy &&
52 image->comps[0].prec == image->comps[1].prec &&
53 image->comps[1].prec == image->comps[2].prec;
56 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
58 LibOpenJPEGContext *ctx = avctx->priv_data;
60 opj_set_default_decoder_parameters(&ctx->dec_params);
61 avctx->coded_frame = &ctx->image;
65 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
67 LibOpenJPEGContext *ctx = avctx->priv_data;
69 avctx->coded_frame = &ctx->image;
73 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
74 void *data, int *data_size,
77 uint8_t *buf = avpkt->data;
78 int buf_size = avpkt->size;
79 LibOpenJPEGContext *ctx = avctx->priv_data;
80 AVFrame *picture = &ctx->image, *output = data;
84 int width, height, has_alpha = 0, ret = -1;
91 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
92 if((AV_RB32(buf) == 12) &&
93 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
94 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
95 dec = opj_create_decompress(CODEC_JP2);
97 // If the AVPacket contains a jp2c box, then skip to
98 // the starting byte of the codestream.
99 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
101 dec = opj_create_decompress(CODEC_J2K);
105 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
108 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
110 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
111 ctx->dec_params.cp_reduce = ctx->lowres;
112 ctx->dec_params.cp_layer = ctx->lowqual;
113 // Tie decoder with decoding parameters
114 opj_setup_decoder(dec, &ctx->dec_params);
115 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
117 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
118 opj_destroy_decompress(dec);
122 // Decode the header only
123 image = opj_decode_with_info(dec, stream, NULL);
124 opj_cio_close(stream);
126 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
127 opj_destroy_decompress(dec);
130 width = image->x1 - image->x0;
131 height = image->y1 - image->y0;
134 width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
135 height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
138 if(av_image_check_size(width, height, 0, avctx) < 0) {
139 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
142 avcodec_set_dimensions(avctx, width, height);
144 switch(image->numcomps)
146 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
148 case 3: if(check_image_attributes(image)) {
149 avctx->pix_fmt = PIX_FMT_RGB24;
151 avctx->pix_fmt = PIX_FMT_GRAY8;
152 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
155 case 4: has_alpha = 1;
156 avctx->pix_fmt = PIX_FMT_RGBA;
158 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
163 ff_thread_release_buffer(avctx, picture);
165 if(ff_thread_get_buffer(avctx, picture) < 0){
166 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
170 ff_thread_finish_setup(avctx);
172 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
173 // Tie decoder with decoding parameters
174 opj_setup_decoder(dec, &ctx->dec_params);
175 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
177 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
178 opj_destroy_decompress(dec);
182 // Decode the codestream
183 image = opj_decode_with_info(dec, stream, NULL);
184 opj_cio_close(stream);
186 for(x = 0; x < image->numcomps; x++) {
187 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
190 for(y = 0; y < avctx->height; y++) {
191 index = y*avctx->width;
192 img_ptr = picture->data[0] + y*picture->linesize[0];
193 for(x = 0; x < avctx->width; x++, index++) {
194 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
195 if(image->numcomps > 2 && check_image_attributes(image)) {
196 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
197 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
199 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
204 *output = ctx->image;
205 *data_size = sizeof(AVPicture);
209 opj_image_destroy(image);
210 opj_destroy_decompress(dec);
214 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
216 LibOpenJPEGContext *ctx = avctx->priv_data;
218 if(ctx->image.data[0])
219 ff_thread_release_buffer(avctx, &ctx->image);
223 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
224 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
226 static const AVOption options[] = {
227 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
228 { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
232 static const AVClass class = {
233 .class_name = "libopenjpeg",
234 .item_name = av_default_item_name,
236 .version = LIBAVUTIL_VERSION_INT,
239 AVCodec ff_libopenjpeg_decoder = {
240 .name = "libopenjpeg",
241 .type = AVMEDIA_TYPE_VIDEO,
242 .id = CODEC_ID_JPEG2000,
243 .priv_data_size = sizeof(LibOpenJPEGContext),
244 .init = libopenjpeg_decode_init,
245 .close = libopenjpeg_decode_close,
246 .decode = libopenjpeg_decode_frame,
247 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
248 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
249 .priv_class = &class,
250 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),