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
30 #include "libavutil/imgutils.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/opt.h"
36 #define JP2_SIG_TYPE 0x6A502020
37 #define JP2_SIG_VALUE 0x0D0A870A
41 opj_dparameters_t dec_params;
47 static int check_image_attributes(opj_image_t *image)
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;
57 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
59 LibOpenJPEGContext *ctx = avctx->priv_data;
61 opj_set_default_decoder_parameters(&ctx->dec_params);
62 avctx->coded_frame = &ctx->image;
66 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
68 LibOpenJPEGContext *ctx = avctx->priv_data;
70 avctx->coded_frame = &ctx->image;
74 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
75 void *data, int *data_size,
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;
85 int width, height, has_alpha = 0, ret = -1;
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);
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"))
102 dec = opj_create_decompress(CODEC_J2K);
106 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
109 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
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);
119 av_log(avctx, AV_LOG_ERROR,
120 "Codestream could not be opened for reading.\n");
121 opj_destroy_decompress(dec);
125 // Decode the header only.
126 image = opj_decode_with_info(dec, stream, NULL);
127 opj_cio_close(stream);
130 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
131 opj_destroy_decompress(dec);
135 width = image->x1 - image->x0;
136 height = image->y1 - image->y0;
139 width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
140 height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
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);
149 avcodec_set_dimensions(avctx, width, height);
151 switch (image->numcomps) {
153 avctx->pix_fmt = PIX_FMT_GRAY8;
156 if (check_image_attributes(image)) {
157 avctx->pix_fmt = PIX_FMT_RGB24;
159 avctx->pix_fmt = PIX_FMT_GRAY8;
160 av_log(avctx, AV_LOG_ERROR,
161 "Only first component will be used.\n");
166 avctx->pix_fmt = PIX_FMT_RGBA;
169 av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n",
174 if (picture->data[0])
175 ff_thread_release_buffer(avctx, picture);
177 if (ff_thread_get_buffer(avctx, picture) < 0) {
178 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
182 ff_thread_finish_setup(avctx);
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);
189 av_log(avctx, AV_LOG_ERROR,
190 "Codestream could not be opened for reading.\n");
191 opj_destroy_decompress(dec);
195 // Decode the codestream.
196 image = opj_decode_with_info(dec, stream, NULL);
197 opj_cio_close(stream);
199 for (x = 0; x < image->numcomps; x++)
200 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
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];
211 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
216 *output = ctx->image;
217 *data_size = sizeof(AVPicture);
221 opj_image_destroy(image);
222 opj_destroy_decompress(dec);
226 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
228 LibOpenJPEGContext *ctx = avctx->priv_data;
230 if (ctx->image.data[0])
231 ff_thread_release_buffer(avctx, &ctx->image);
235 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
236 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
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 },
244 static const AVClass class = {
245 .class_name = "libopenjpeg",
246 .item_name = av_default_item_name,
248 .version = LIBAVUTIL_VERSION_INT,
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),