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 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, \
46 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8, AV_PIX_FMT_Y400A, \
49 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, \
50 AV_PIX_FMT_YUVA420P, \
51 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, \
52 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, \
53 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, \
54 AV_PIX_FMT_YUV444P9, \
55 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, \
56 AV_PIX_FMT_YUV444P10, \
57 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, \
60 static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
61 static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
62 static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
63 static const enum AVPixelFormat 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 AVPixelFormat pix_fmt)
78 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
81 if (desc->nb_components != img->numcomps) {
85 switch (desc->nb_components) {
88 desc->comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
89 1 == img->comps[3].dx &&
90 1 == img->comps[3].dy;
93 desc->comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
94 1 << desc->log2_chroma_w == img->comps[2].dx &&
95 1 << desc->log2_chroma_h == img->comps[2].dy;
98 desc->comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
99 1 << desc->log2_chroma_w == img->comps[1].dx &&
100 1 << desc->log2_chroma_h == img->comps[1].dy;
103 desc->comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
104 1 == img->comps[0].dx &&
105 1 == img->comps[0].dy;
113 static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
116 const enum AVPixelFormat *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];
144 return AV_PIX_FMT_NONE;
147 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
149 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
150 int i, component_plane;
152 if (pix_fmt == AV_PIX_FMT_GRAY16)
155 component_plane = desc->comp[0].plane;
156 for (i = 1; i < desc->nb_components; i++) {
157 if (component_plane != desc->comp[i].plane)
163 static void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image)
168 for (y = 0; y < picture->height; y++) {
169 index = y*picture->width;
170 img_ptr = picture->data[0] + y*picture->linesize[0];
171 for (x = 0; x < picture->width; x++, index++) {
172 for (c = 0; c < image->numcomps; c++) {
173 *img_ptr++ = image->comps[c].data[index];
179 static void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image)
185 for (x = 0; x < image->numcomps; x++)
186 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
188 for (y = 0; y < picture->height; y++) {
189 index = y*picture->width;
190 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
191 for (x = 0; x < picture->width; x++, index++) {
192 for (c = 0; c < image->numcomps; c++) {
193 *img_ptr++ = image->comps[c].data[index] << adjust[c];
199 static void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image)
205 for (index = 0; index < image->numcomps; index++) {
206 comp_data = image->comps[index].data;
207 for (y = 0; y < image->comps[index].h; y++) {
208 img_ptr = picture->data[index] + y * picture->linesize[index];
209 for (x = 0; x < image->comps[index].w; x++) {
210 *img_ptr = (uint8_t) *comp_data;
218 static void libopenjpeg_copyto16(AVFrame *p, opj_image_t *image)
224 for (index = 0; index < image->numcomps; index++) {
225 comp_data = image->comps[index].data;
226 for (y = 0; y < image->comps[index].h; y++) {
227 img_ptr = (uint16_t*) (p->data[index] + y * p->linesize[index]);
228 for (x = 0; x < image->comps[index].w; x++) {
229 *img_ptr = *comp_data;
237 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
239 LibOpenJPEGContext *ctx = avctx->priv_data;
241 opj_set_default_decoder_parameters(&ctx->dec_params);
242 avcodec_get_frame_defaults(&ctx->image);
243 avctx->coded_frame = &ctx->image;
247 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
249 LibOpenJPEGContext *ctx = avctx->priv_data;
251 avctx->coded_frame = &ctx->image;
255 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
256 void *data, int *data_size,
259 uint8_t *buf = avpkt->data;
260 int buf_size = avpkt->size;
261 LibOpenJPEGContext *ctx = avctx->priv_data;
262 AVFrame *picture = &ctx->image, *output = data;
263 const AVPixFmtDescriptor *desc;
267 int width, height, ret = -1;
274 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
275 if ((AV_RB32(buf) == 12) &&
276 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
277 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
278 dec = opj_create_decompress(CODEC_JP2);
280 /* If the AVPacket contains a jp2c box, then skip to
281 * the starting byte of the codestream. */
282 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
284 dec = opj_create_decompress(CODEC_J2K);
288 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
291 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
293 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
294 ctx->dec_params.cp_reduce = ctx->lowres;
295 ctx->dec_params.cp_layer = ctx->lowqual;
296 // Tie decoder with decoding parameters
297 opj_setup_decoder(dec, &ctx->dec_params);
298 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
301 av_log(avctx, AV_LOG_ERROR,
302 "Codestream could not be opened for reading.\n");
303 opj_destroy_decompress(dec);
307 // Decode the header only.
308 image = opj_decode_with_info(dec, stream, NULL);
309 opj_cio_close(stream);
312 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
313 opj_destroy_decompress(dec);
317 width = image->x1 - image->x0;
318 height = image->y1 - image->y0;
321 width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
322 height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
325 if (av_image_check_size(width, height, 0, avctx) < 0) {
326 av_log(avctx, AV_LOG_ERROR,
327 "%dx%d dimension invalid.\n", width, height);
331 avcodec_set_dimensions(avctx, width, height);
333 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
334 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
335 avctx->pix_fmt = AV_PIX_FMT_NONE;
337 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
338 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
340 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
341 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
342 ret = AVERROR_INVALIDDATA;
346 for (i = 0; i < image->numcomps; i++)
347 if (image->comps[i].prec > avctx->bits_per_raw_sample)
348 avctx->bits_per_raw_sample = image->comps[i].prec;
350 if (picture->data[0])
351 ff_thread_release_buffer(avctx, picture);
353 if (ff_thread_get_buffer(avctx, picture) < 0) {
354 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
358 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
359 // Tie decoder with decoding parameters.
360 opj_setup_decoder(dec, &ctx->dec_params);
361 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
363 av_log(avctx, AV_LOG_ERROR,
364 "Codestream could not be opened for reading.\n");
368 opj_image_destroy(image);
369 // Decode the codestream
370 image = opj_decode_with_info(dec, stream, NULL);
371 opj_cio_close(stream);
374 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
378 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
379 pixel_size = desc->comp[0].step_minus1 + 1;
380 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
382 switch (pixel_size) {
385 libopenjpeg_copy_to_packed8(picture, image);
387 libopenjpeg_copyto8(picture, image);
392 libopenjpeg_copy_to_packed8(picture, image);
394 libopenjpeg_copyto16(picture, image);
400 libopenjpeg_copy_to_packed8(picture, image);
406 libopenjpeg_copy_to_packed16(picture, image);
410 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
414 *output = ctx->image;
415 *data_size = sizeof(AVPicture);
419 opj_image_destroy(image);
420 opj_destroy_decompress(dec);
424 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
426 LibOpenJPEGContext *ctx = avctx->priv_data;
428 if (ctx->image.data[0])
429 ff_thread_release_buffer(avctx, &ctx->image);
433 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
434 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
436 static const AVOption options[] = {
437 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
438 { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
442 static const AVClass class = {
443 .class_name = "libopenjpeg",
444 .item_name = av_default_item_name,
446 .version = LIBAVUTIL_VERSION_INT,
449 AVCodec ff_libopenjpeg_decoder = {
450 .name = "libopenjpeg",
451 .type = AVMEDIA_TYPE_VIDEO,
452 .id = AV_CODEC_ID_JPEG2000,
453 .priv_data_size = sizeof(LibOpenJPEGContext),
454 .init = libopenjpeg_decode_init,
455 .close = libopenjpeg_decode_close,
456 .decode = libopenjpeg_decode_frame,
457 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
458 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
459 .priv_class = &class,
460 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),