2 * JPEG 2000 decoding support via OpenJPEG
3 * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
5 * This file is part of FFmpeg.
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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 avcodec_get_frame_defaults(&ctx->image);
58 avctx->coded_frame = &ctx->image;
62 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
64 LibOpenJPEGContext *ctx = avctx->priv_data;
66 avctx->coded_frame = &ctx->image;
70 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
71 void *data, int *data_size,
74 uint8_t *buf = avpkt->data;
75 int buf_size = avpkt->size;
76 LibOpenJPEGContext *ctx = avctx->priv_data;
77 AVFrame *picture = &ctx->image, *output = data;
81 int width, height, has_alpha = 0, ret = -1;
88 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
89 if((AV_RB32(buf) == 12) &&
90 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
91 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
92 dec = opj_create_decompress(CODEC_JP2);
94 // If the AVPacket contains a jp2c box, then skip to
95 // the starting byte of the codestream.
96 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
98 dec = opj_create_decompress(CODEC_J2K);
102 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
105 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
107 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
108 // Tie decoder with decoding parameters
109 opj_setup_decoder(dec, &ctx->dec_params);
110 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
112 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
113 opj_destroy_decompress(dec);
117 // Decode the header only
118 image = opj_decode_with_info(dec, stream, NULL);
119 opj_cio_close(stream);
121 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
122 opj_destroy_decompress(dec);
125 width = image->x1 - image->x0;
126 height = image->y1 - image->y0;
127 if(av_image_check_size(width, height, 0, avctx) < 0) {
128 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
131 avcodec_set_dimensions(avctx, width, height);
133 switch(image->numcomps)
135 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
137 case 3: if(check_image_attributes(image)) {
138 avctx->pix_fmt = PIX_FMT_RGB24;
140 avctx->pix_fmt = PIX_FMT_GRAY8;
141 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
144 case 4: has_alpha = 1;
145 avctx->pix_fmt = PIX_FMT_RGBA;
147 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
152 ff_thread_release_buffer(avctx, picture);
154 if(ff_thread_get_buffer(avctx, picture) < 0){
155 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
159 ff_thread_finish_setup(avctx);
161 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
162 ctx->dec_params.cp_reduce = avctx->lowres;
163 // Tie decoder with decoding parameters
164 opj_setup_decoder(dec, &ctx->dec_params);
165 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
167 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
168 opj_destroy_decompress(dec);
172 // Decode the codestream
173 image = opj_decode_with_info(dec, stream, NULL);
174 opj_cio_close(stream);
176 for(x = 0; x < image->numcomps; x++) {
177 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
180 for(y = 0; y < avctx->height; y++) {
181 index = y*avctx->width;
182 img_ptr = picture->data[0] + y*picture->linesize[0];
183 for(x = 0; x < avctx->width; x++, index++) {
184 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
185 if(image->numcomps > 2 && check_image_attributes(image)) {
186 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
187 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
189 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
194 *output = ctx->image;
195 *data_size = sizeof(AVPicture);
199 opj_image_destroy(image);
200 opj_destroy_decompress(dec);
204 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
206 LibOpenJPEGContext *ctx = avctx->priv_data;
208 if(ctx->image.data[0])
209 ff_thread_release_buffer(avctx, &ctx->image);
214 AVCodec ff_libopenjpeg_decoder = {
215 .name = "libopenjpeg",
216 .type = AVMEDIA_TYPE_VIDEO,
217 .id = CODEC_ID_JPEG2000,
218 .priv_data_size = sizeof(LibOpenJPEGContext),
219 .init = libopenjpeg_decode_init,
220 .close = libopenjpeg_decode_close,
221 .decode = libopenjpeg_decode_frame,
222 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
224 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
225 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)