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"
28 #include "libavutil/pixfmt.h"
30 #include "libavutil/intreadwrite.h"
35 #define JP2_SIG_TYPE 0x6A502020
36 #define JP2_SIG_VALUE 0x0D0A870A
39 opj_dparameters_t dec_params;
43 static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
45 opj_image_comp_t c0 = image->comps[0];
46 opj_image_comp_t c1 = image->comps[1];
47 opj_image_comp_t c2 = image->comps[2];
49 compRatio |= c0.dx << 15 | c0.dy << 12;
50 compRatio |= c1.dx << 9 | c1.dy << 6;
51 compRatio |= c2.dx << 3 | c2.dy;
54 case 0111111: goto libopenjpeg_yuv444_rgb;
55 case 0112121: goto libopenjpeg_yuv422;
56 case 0112222: goto libopenjpeg_yuv420;
57 default: return PIX_FMT_RGB24;
62 case 8: return PIX_FMT_YUV420P;
63 case 9: return PIX_FMT_YUV420P9;
64 case 10: return PIX_FMT_YUV420P10;
65 case 16: return PIX_FMT_YUV420P16;
70 case 8: return PIX_FMT_YUV422P;
71 case 9: return PIX_FMT_YUV422P9;
72 case 10: return PIX_FMT_YUV422P10;
73 case 16: return PIX_FMT_YUV422P16;
76 libopenjpeg_yuv444_rgb:
78 case 8: return PIX_FMT_RGB24;
79 case 9: return PIX_FMT_YUV444P9;
80 case 10: return PIX_FMT_YUV444P10;
81 case 16: return PIX_FMT_YUV444P16;
86 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
87 int i, component_plane;
88 component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
89 for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
90 if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
96 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
99 for(y = 0; y < picture->height; y++) {
100 index = y*picture->width;
101 img_ptr = picture->data[0] + y*picture->linesize[0];
102 for(x = 0; x < picture->width; x++, index++) {
103 for(c = 0; c < image->numcomps; c++) {
104 *img_ptr++ = image->comps[c].data[index];
110 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
115 for(index = 0; index < image->numcomps; index++) {
116 comp_data = image->comps[index].data;
117 img_ptr = picture->data[index];
118 for(y = 0; y < image->comps[index].h; y++) {
119 for(x = 0; x < image->comps[index].w; x++) {
120 *img_ptr = (uint8_t) *comp_data;
128 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
132 for(index = 0; index < image->numcomps; index++) {
133 comp_data = image->comps[index].data;
134 img_ptr = (uint16_t*) picture->data[index];
135 for(y = 0; y < image->comps[index].h; y++) {
136 for(x = 0; x < image->comps[index].w; x++) {
137 *img_ptr = *comp_data;
145 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
147 LibOpenJPEGContext *ctx = avctx->priv_data;
149 opj_set_default_decoder_parameters(&ctx->dec_params);
150 avcodec_get_frame_defaults(&ctx->image);
151 avctx->coded_frame = &ctx->image;
155 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
157 LibOpenJPEGContext *ctx = avctx->priv_data;
159 avctx->coded_frame = &ctx->image;
163 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
164 void *data, int *data_size,
167 uint8_t *buf = avpkt->data;
168 int buf_size = avpkt->size;
169 LibOpenJPEGContext *ctx = avctx->priv_data;
170 AVFrame *picture = &ctx->image, *output = data;
174 int width, height, ret = -1;
180 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
181 if((AV_RB32(buf) == 12) &&
182 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
183 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
184 dec = opj_create_decompress(CODEC_JP2);
186 // If the AVPacket contains a jp2c box, then skip to
187 // the starting byte of the codestream.
188 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
190 dec = opj_create_decompress(CODEC_J2K);
194 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
197 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
199 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
200 // Tie decoder with decoding parameters
201 opj_setup_decoder(dec, &ctx->dec_params);
202 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
204 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
205 opj_destroy_decompress(dec);
209 // Decode the header only
210 image = opj_decode_with_info(dec, stream, NULL);
211 opj_cio_close(stream);
213 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
214 opj_destroy_decompress(dec);
217 width = image->x1 - image->x0;
218 height = image->y1 - image->y0;
219 if(av_image_check_size(width, height, 0, avctx) < 0) {
220 av_log(avctx, AV_LOG_ERROR, "%dx%d dimension invalid.\n", width, height);
223 avcodec_set_dimensions(avctx, width, height);
225 switch (image->numcomps) {
226 case 1: avctx->pix_fmt = PIX_FMT_GRAY8;
228 case 3: avctx->pix_fmt = check_image_attributes(avctx, image);
230 case 4: avctx->pix_fmt = PIX_FMT_RGBA;
232 default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
237 ff_thread_release_buffer(avctx, picture);
239 if(ff_thread_get_buffer(avctx, picture) < 0){
240 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
244 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
245 ctx->dec_params.cp_reduce = avctx->lowres;
246 // Tie decoder with decoding parameters
247 opj_setup_decoder(dec, &ctx->dec_params);
248 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
250 av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
251 opj_destroy_decompress(dec);
255 // Decode the codestream
256 image = opj_decode_with_info(dec, stream, NULL);
257 opj_cio_close(stream);
259 pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
260 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
262 switch (pixel_size) {
265 libopenjpeg_copy_to_packed8(picture, image);
267 libopenjpeg_copyto8(picture, image);
271 libopenjpeg_copyto16(picture, image);
275 libopenjpeg_copy_to_packed8(picture, image);
279 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
283 *output = ctx->image;
284 *data_size = sizeof(AVPicture);
288 opj_image_destroy(image);
289 opj_destroy_decompress(dec);
293 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
295 LibOpenJPEGContext *ctx = avctx->priv_data;
297 if(ctx->image.data[0])
298 ff_thread_release_buffer(avctx, &ctx->image);
303 AVCodec ff_libopenjpeg_decoder = {
304 .name = "libopenjpeg",
305 .type = AVMEDIA_TYPE_VIDEO,
306 .id = CODEC_ID_JPEG2000,
307 .priv_data_size = sizeof(LibOpenJPEGContext),
308 .init = libopenjpeg_decode_init,
309 .close = libopenjpeg_decode_close,
310 .decode = libopenjpeg_decode_frame,
311 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
313 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
314 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)