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/imgutils.h"
29 #include "libavutil/intreadwrite.h"
34 #define JP2_SIG_TYPE 0x6A502020
35 #define JP2_SIG_VALUE 0x0D0A870A
38 opj_dparameters_t dec_params;
42 static int check_image_attributes(opj_image_t *image)
44 return image->comps[0].dx == image->comps[1].dx &&
45 image->comps[1].dx == image->comps[2].dx &&
46 image->comps[0].dy == image->comps[1].dy &&
47 image->comps[1].dy == image->comps[2].dy &&
48 image->comps[0].prec == image->comps[1].prec &&
49 image->comps[1].prec == image->comps[2].prec;
52 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
54 LibOpenJPEGContext *ctx = avctx->priv_data;
56 opj_set_default_decoder_parameters(&ctx->dec_params);
57 avctx->coded_frame = &ctx->image;
61 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
63 LibOpenJPEGContext *ctx = avctx->priv_data;
65 avctx->coded_frame = &ctx->image;
69 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
70 void *data, int *data_size,
73 uint8_t *buf = avpkt->data;
74 int buf_size = avpkt->size;
75 LibOpenJPEGContext *ctx = avctx->priv_data;
76 AVFrame *picture = &ctx->image, *output = data;
80 int width, height, has_alpha = 0, ret = -1;
87 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
88 if((AV_RB32(buf) == 12) &&
89 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
90 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
91 dec = opj_create_decompress(CODEC_JP2);
93 // If the AVPacket contains a jp2c box, then skip to
94 // the starting byte of the codestream.
95 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
97 dec = opj_create_decompress(CODEC_J2K);
101 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
104 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
106 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
107 // Tie decoder with decoding parameters
108 opj_setup_decoder(dec, &ctx->dec_params);
109 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
111 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
112 opj_destroy_decompress(dec);
116 // Decode the header only
117 image = opj_decode_with_info(dec, stream, NULL);
118 opj_cio_close(stream);
120 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
121 opj_destroy_decompress(dec);
124 width = image->x1 - image->x0;
125 height = image->y1 - image->y0;
126 if(av_image_check_size(width, height, 0, avctx) < 0) {
127 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
130 avcodec_set_dimensions(avctx, width, height);
132 switch(image->numcomps)
134 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
136 case 3: if(check_image_attributes(image)) {
137 avctx->pix_fmt = PIX_FMT_RGB24;
139 avctx->pix_fmt = PIX_FMT_GRAY8;
140 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
143 case 4: has_alpha = 1;
144 avctx->pix_fmt = PIX_FMT_RGBA;
146 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
151 ff_thread_release_buffer(avctx, picture);
153 if(ff_thread_get_buffer(avctx, picture) < 0){
154 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
158 ff_thread_finish_setup(avctx);
160 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
161 ctx->dec_params.cp_reduce = avctx->lowres;
162 // Tie decoder with decoding parameters
163 opj_setup_decoder(dec, &ctx->dec_params);
164 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
166 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
167 opj_destroy_decompress(dec);
171 // Decode the codestream
172 image = opj_decode_with_info(dec, stream, NULL);
173 opj_cio_close(stream);
175 for(x = 0; x < image->numcomps; x++) {
176 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
179 for(y = 0; y < avctx->height; y++) {
180 index = y*avctx->width;
181 img_ptr = picture->data[0] + y*picture->linesize[0];
182 for(x = 0; x < avctx->width; x++, index++) {
183 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
184 if(image->numcomps > 2 && check_image_attributes(image)) {
185 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
186 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
188 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
193 *output = ctx->image;
194 *data_size = sizeof(AVPicture);
198 opj_image_destroy(image);
199 opj_destroy_decompress(dec);
203 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
205 LibOpenJPEGContext *ctx = avctx->priv_data;
207 if(ctx->image.data[0])
208 ff_thread_release_buffer(avctx, &ctx->image);
213 AVCodec ff_libopenjpeg_decoder = {
214 .name = "libopenjpeg",
215 .type = AVMEDIA_TYPE_VIDEO,
216 .id = CODEC_ID_JPEG2000,
217 .priv_data_size = sizeof(LibOpenJPEGContext),
218 .init = libopenjpeg_decode_init,
219 .close = libopenjpeg_decode_close,
220 .decode = libopenjpeg_decode_frame,
221 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
223 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
224 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)