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
28 #include "libavutil/intreadwrite.h"
32 #define JP2_SIG_TYPE 0x6A502020
33 #define JP2_SIG_VALUE 0x0D0A870A
36 opj_dparameters_t dec_params;
40 static int check_image_attributes(opj_image_t *image)
42 return image->comps[0].dx == image->comps[1].dx &&
43 image->comps[1].dx == image->comps[2].dx &&
44 image->comps[0].dy == image->comps[1].dy &&
45 image->comps[1].dy == image->comps[2].dy &&
46 image->comps[0].prec == image->comps[1].prec &&
47 image->comps[1].prec == image->comps[2].prec;
50 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
52 LibOpenJPEGContext *ctx = avctx->priv_data;
54 opj_set_default_decoder_parameters(&ctx->dec_params);
55 avctx->coded_frame = &ctx->image;
59 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
60 void *data, int *data_size,
63 const uint8_t *buf = avpkt->data;
64 int buf_size = avpkt->size;
65 LibOpenJPEGContext *ctx = avctx->priv_data;
66 AVFrame *picture = &ctx->image, *output = data;
70 int width, height, has_alpha = 0, ret = -1;
77 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
78 if((AV_RB32(buf) == 12) &&
79 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
80 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
81 dec = opj_create_decompress(CODEC_JP2);
83 // If the AVPacket contains a jp2c box, then skip to
84 // the starting byte of the codestream.
85 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
87 dec = opj_create_decompress(CODEC_J2K);
91 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
94 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
96 ctx->dec_params.cp_reduce = avctx->lowres;
97 // Tie decoder with decoding parameters
98 opj_setup_decoder(dec, &ctx->dec_params);
99 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
101 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
102 opj_destroy_decompress(dec);
106 // Decode the codestream
107 image = opj_decode_with_info(dec, stream, NULL);
108 opj_cio_close(stream);
110 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
111 opj_destroy_decompress(dec);
114 width = image->comps[0].w << avctx->lowres;
115 height = image->comps[0].h << avctx->lowres;
116 if(avcodec_check_dimensions(avctx, width, height) < 0) {
117 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
120 avcodec_set_dimensions(avctx, width, height);
122 switch(image->numcomps)
124 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
126 case 3: if(check_image_attributes(image)) {
127 avctx->pix_fmt = PIX_FMT_RGB24;
129 avctx->pix_fmt = PIX_FMT_GRAY8;
130 av_log(avctx, AV_LOG_ERROR, "Only first component will be used.\n");
133 case 4: has_alpha = 1;
134 avctx->pix_fmt = PIX_FMT_RGBA;
136 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
141 avctx->release_buffer(avctx, picture);
143 if(avctx->get_buffer(avctx, picture) < 0) {
144 av_log(avctx, AV_LOG_ERROR, "Couldn't allocate image buffer.\n");
148 for(x = 0; x < image->numcomps; x++) {
149 adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
152 for(y = 0; y < avctx->height; y++) {
153 index = y*avctx->width;
154 img_ptr = picture->data[0] + y*picture->linesize[0];
155 for(x = 0; x < avctx->width; x++, index++) {
156 *img_ptr++ = image->comps[0].data[index] >> adjust[0];
157 if(image->numcomps > 2 && check_image_attributes(image)) {
158 *img_ptr++ = image->comps[1].data[index] >> adjust[1];
159 *img_ptr++ = image->comps[2].data[index] >> adjust[2];
161 *img_ptr++ = image->comps[3].data[index] >> adjust[3];
166 *output = ctx->image;
167 *data_size = sizeof(AVPicture);
171 opj_image_destroy(image);
172 opj_destroy_decompress(dec);
176 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
178 LibOpenJPEGContext *ctx = avctx->priv_data;
180 if(ctx->image.data[0])
181 avctx->release_buffer(avctx, &ctx->image);
186 AVCodec libopenjpeg_decoder = {
190 sizeof(LibOpenJPEGContext),
191 libopenjpeg_decode_init,
193 libopenjpeg_decode_close,
194 libopenjpeg_decode_frame,
197 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),