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/common.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/imgutils.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/opt.h"
38 #define JP2_SIG_TYPE 0x6A502020
39 #define JP2_SIG_VALUE 0x0D0A870A
41 // pix_fmts with lower bpp have to be listed before
42 // similar pix_fmts with higher bpp.
43 #define RGB_PIXEL_FORMATS PIX_FMT_RGB24, PIX_FMT_RGBA, \
46 #define GRAY_PIXEL_FORMATS PIX_FMT_GRAY8, PIX_FMT_Y400A, \
49 #define YUV_PIXEL_FORMATS PIX_FMT_YUV410P, PIX_FMT_YUV411P, \
51 PIX_FMT_YUV420P, PIX_FMT_YUV422P, \
52 PIX_FMT_YUV440P, PIX_FMT_YUV444P, \
53 PIX_FMT_YUV420P9, PIX_FMT_YUV422P9, \
55 PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, \
57 PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, \
60 static const enum PixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
61 static const enum PixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
62 static const enum PixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
63 static const enum PixelFormat any_pix_fmts[] = {RGB_PIXEL_FORMATS,
69 opj_dparameters_t dec_params;
75 static int libopenjpeg_matches_pix_fmt(const opj_image_t *img,
76 enum PixelFormat pix_fmt)
78 AVPixFmtDescriptor des = av_pix_fmt_descriptors[pix_fmt];
81 if (des.nb_components != img->numcomps) {
85 switch (des.nb_components) {
88 des.comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
89 1 == img->comps[3].dx &&
90 1 == img->comps[3].dy;
93 des.comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
94 1 << des.log2_chroma_w == img->comps[2].dx &&
95 1 << des.log2_chroma_h == img->comps[2].dy;
98 des.comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
99 1 << des.log2_chroma_w == img->comps[1].dx &&
100 1 << des.log2_chroma_h == img->comps[1].dy;
103 des.comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
104 1 == img->comps[0].dx &&
105 1 == img->comps[0].dy;
113 static enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
116 const enum PixelFormat *possible_fmts = NULL;
117 int possible_fmts_nb = 0;
119 switch (image->color_space) {
121 possible_fmts = rgb_pix_fmts;
122 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
125 possible_fmts = gray_pix_fmts;
126 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
129 possible_fmts = yuv_pix_fmts;
130 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
133 possible_fmts = any_pix_fmts;
134 possible_fmts_nb = FF_ARRAY_ELEMS(any_pix_fmts);
138 for (index = 0; index < possible_fmts_nb; ++index) {
139 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
140 return possible_fmts[index];
147 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt)
149 int i, component_plane;
151 if (pix_fmt == PIX_FMT_GRAY16)
154 component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
155 for (i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
156 if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
162 static void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image)
167 for (y = 0; y < picture->height; y++) {
168 index = y*picture->width;
169 img_ptr = picture->data[0] + y*picture->linesize[0];
170 for (x = 0; x < picture->width; x++, index++) {
171 for (c = 0; c < image->numcomps; c++) {
172 *img_ptr++ = image->comps[c].data[index];
178 static void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image)
184 for (x = 0; x < image->numcomps; x++)
185 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
187 for (y = 0; y < picture->height; y++) {
188 index = y*picture->width;
189 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
190 for (x = 0; x < picture->width; x++, index++) {
191 for (c = 0; c < image->numcomps; c++) {
192 *img_ptr++ = image->comps[c].data[index] << adjust[c];
198 static void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image)
204 for (index = 0; index < image->numcomps; index++) {
205 comp_data = image->comps[index].data;
206 for (y = 0; y < image->comps[index].h; y++) {
207 img_ptr = picture->data[index] + y * picture->linesize[index];
208 for (x = 0; x < image->comps[index].w; x++) {
209 *img_ptr = (uint8_t) *comp_data;
217 static void libopenjpeg_copyto16(AVFrame *p, opj_image_t *image)
223 for (index = 0; index < image->numcomps; index++) {
224 comp_data = image->comps[index].data;
225 for (y = 0; y < image->comps[index].h; y++) {
226 img_ptr = (uint16_t*) (p->data[index] + y * p->linesize[index]);
227 for (x = 0; x < image->comps[index].w; x++) {
228 *img_ptr = *comp_data;
236 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
238 LibOpenJPEGContext *ctx = avctx->priv_data;
240 opj_set_default_decoder_parameters(&ctx->dec_params);
241 avcodec_get_frame_defaults(&ctx->image);
242 avctx->coded_frame = &ctx->image;
246 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
248 LibOpenJPEGContext *ctx = avctx->priv_data;
250 avctx->coded_frame = &ctx->image;
254 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
255 void *data, int *data_size,
258 uint8_t *buf = avpkt->data;
259 int buf_size = avpkt->size;
260 LibOpenJPEGContext *ctx = avctx->priv_data;
261 AVFrame *picture = &ctx->image, *output = data;
265 int width, height, ret = -1;
272 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
273 if ((AV_RB32(buf) == 12) &&
274 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
275 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
276 dec = opj_create_decompress(CODEC_JP2);
278 /* If the AVPacket contains a jp2c box, then skip to
279 * the starting byte of the codestream. */
280 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
282 dec = opj_create_decompress(CODEC_J2K);
286 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
289 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
291 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
292 ctx->dec_params.cp_reduce = ctx->lowres;
293 ctx->dec_params.cp_layer = ctx->lowqual;
294 // Tie decoder with decoding parameters
295 opj_setup_decoder(dec, &ctx->dec_params);
296 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
299 av_log(avctx, AV_LOG_ERROR,
300 "Codestream could not be opened for reading.\n");
301 opj_destroy_decompress(dec);
305 // Decode the header only.
306 image = opj_decode_with_info(dec, stream, NULL);
307 opj_cio_close(stream);
310 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
311 opj_destroy_decompress(dec);
315 width = image->x1 - image->x0;
316 height = image->y1 - image->y0;
319 width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
320 height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
323 if (av_image_check_size(width, height, 0, avctx) < 0) {
324 av_log(avctx, AV_LOG_ERROR,
325 "%dx%d dimension invalid.\n", width, height);
329 avcodec_set_dimensions(avctx, width, height);
331 if (avctx->pix_fmt != PIX_FMT_NONE)
332 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
333 avctx->pix_fmt = PIX_FMT_NONE;
335 if (avctx->pix_fmt == PIX_FMT_NONE)
336 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
338 if (avctx->pix_fmt == PIX_FMT_NONE) {
339 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
340 ret = AVERROR_INVALIDDATA;
344 for (i = 0; i < image->numcomps; i++)
345 if (image->comps[i].prec > avctx->bits_per_raw_sample)
346 avctx->bits_per_raw_sample = image->comps[i].prec;
348 if (picture->data[0])
349 ff_thread_release_buffer(avctx, picture);
351 if (ff_thread_get_buffer(avctx, picture) < 0) {
352 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
356 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
357 // Tie decoder with decoding parameters.
358 opj_setup_decoder(dec, &ctx->dec_params);
359 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
361 av_log(avctx, AV_LOG_ERROR,
362 "Codestream could not be opened for reading.\n");
366 opj_image_destroy(image);
367 // Decode the codestream
368 image = opj_decode_with_info(dec, stream, NULL);
369 opj_cio_close(stream);
372 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
377 av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
378 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
380 switch (pixel_size) {
383 libopenjpeg_copy_to_packed8(picture, image);
385 libopenjpeg_copyto8(picture, image);
390 libopenjpeg_copy_to_packed8(picture, image);
392 libopenjpeg_copyto16(picture, image);
398 libopenjpeg_copy_to_packed8(picture, image);
404 libopenjpeg_copy_to_packed16(picture, image);
408 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
412 *output = ctx->image;
413 *data_size = sizeof(AVPicture);
417 opj_image_destroy(image);
418 opj_destroy_decompress(dec);
422 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
424 LibOpenJPEGContext *ctx = avctx->priv_data;
426 if (ctx->image.data[0])
427 ff_thread_release_buffer(avctx, &ctx->image);
431 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
432 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
434 static const AVOption options[] = {
435 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
436 { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
440 static const AVClass class = {
441 .class_name = "libopenjpeg",
442 .item_name = av_default_item_name,
444 .version = LIBAVUTIL_VERSION_INT,
447 AVCodec ff_libopenjpeg_decoder = {
448 .name = "libopenjpeg",
449 .type = AVMEDIA_TYPE_VIDEO,
450 .id = AV_CODEC_ID_JPEG2000,
451 .priv_data_size = sizeof(LibOpenJPEGContext),
452 .init = libopenjpeg_decode_init,
453 .close = libopenjpeg_decode_close,
454 .decode = libopenjpeg_decode_frame,
455 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
456 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
457 .priv_class = &class,
458 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),