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;
74 static int libopenjpeg_matches_pix_fmt(const opj_image_t *img,
75 enum AVPixelFormat pix_fmt)
77 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
80 if (desc->nb_components != img->numcomps) {
84 switch (desc->nb_components) {
87 desc->comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
88 1 == img->comps[3].dx &&
89 1 == img->comps[3].dy;
92 desc->comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
93 1 << desc->log2_chroma_w == img->comps[2].dx &&
94 1 << desc->log2_chroma_h == img->comps[2].dy;
97 desc->comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
98 1 << desc->log2_chroma_w == img->comps[1].dx &&
99 1 << desc->log2_chroma_h == img->comps[1].dy;
102 desc->comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
103 1 == img->comps[0].dx &&
104 1 == img->comps[0].dy;
112 static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
115 const enum AVPixelFormat *possible_fmts = NULL;
116 int possible_fmts_nb = 0;
118 switch (image->color_space) {
120 possible_fmts = rgb_pix_fmts;
121 possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
124 possible_fmts = gray_pix_fmts;
125 possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
128 possible_fmts = yuv_pix_fmts;
129 possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
132 possible_fmts = any_pix_fmts;
133 possible_fmts_nb = FF_ARRAY_ELEMS(any_pix_fmts);
137 for (index = 0; index < possible_fmts_nb; ++index) {
138 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
139 return possible_fmts[index];
143 return AV_PIX_FMT_NONE;
146 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
148 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
149 int i, component_plane;
151 if (pix_fmt == AV_PIX_FMT_GRAY16)
154 component_plane = desc->comp[0].plane;
155 for (i = 1; i < desc->nb_components; i++) {
156 if (component_plane != desc->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);
244 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
245 void *data, int *got_frame,
248 uint8_t *buf = avpkt->data;
249 int buf_size = avpkt->size;
250 LibOpenJPEGContext *ctx = avctx->priv_data;
251 ThreadFrame frame = { .f = data };
252 AVFrame *picture = data;
253 const AVPixFmtDescriptor *desc;
257 int width, height, ret = -1;
264 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
265 if ((AV_RB32(buf) == 12) &&
266 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
267 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
268 dec = opj_create_decompress(CODEC_JP2);
270 /* If the AVPacket contains a jp2c box, then skip to
271 * the starting byte of the codestream. */
272 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
274 dec = opj_create_decompress(CODEC_J2K);
278 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
281 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
283 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
284 ctx->dec_params.cp_reduce = ctx->lowres;
285 ctx->dec_params.cp_layer = ctx->lowqual;
286 // Tie decoder with decoding parameters
287 opj_setup_decoder(dec, &ctx->dec_params);
288 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
291 av_log(avctx, AV_LOG_ERROR,
292 "Codestream could not be opened for reading.\n");
293 opj_destroy_decompress(dec);
297 // Decode the header only.
298 image = opj_decode_with_info(dec, stream, NULL);
299 opj_cio_close(stream);
302 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
303 opj_destroy_decompress(dec);
307 width = image->x1 - image->x0;
308 height = image->y1 - image->y0;
311 width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
312 height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
315 if (av_image_check_size(width, height, 0, avctx) < 0) {
316 av_log(avctx, AV_LOG_ERROR,
317 "%dx%d dimension invalid.\n", width, height);
321 avcodec_set_dimensions(avctx, width, height);
323 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
324 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
325 avctx->pix_fmt = AV_PIX_FMT_NONE;
327 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
328 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
330 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
331 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
332 ret = AVERROR_INVALIDDATA;
336 for (i = 0; i < image->numcomps; i++)
337 if (image->comps[i].prec > avctx->bits_per_raw_sample)
338 avctx->bits_per_raw_sample = image->comps[i].prec;
340 if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
341 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
345 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
346 // Tie decoder with decoding parameters.
347 opj_setup_decoder(dec, &ctx->dec_params);
348 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
350 av_log(avctx, AV_LOG_ERROR,
351 "Codestream could not be opened for reading.\n");
355 opj_image_destroy(image);
356 // Decode the codestream
357 image = opj_decode_with_info(dec, stream, NULL);
358 opj_cio_close(stream);
361 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
365 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
366 pixel_size = desc->comp[0].step_minus1 + 1;
367 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
369 switch (pixel_size) {
372 libopenjpeg_copy_to_packed8(picture, image);
374 libopenjpeg_copyto8(picture, image);
379 libopenjpeg_copy_to_packed8(picture, image);
381 libopenjpeg_copyto16(picture, image);
387 libopenjpeg_copy_to_packed8(picture, image);
393 libopenjpeg_copy_to_packed16(picture, image);
397 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
405 opj_image_destroy(image);
406 opj_destroy_decompress(dec);
410 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
411 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
413 static const AVOption options[] = {
414 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
415 { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
419 static const AVClass class = {
420 .class_name = "libopenjpeg",
421 .item_name = av_default_item_name,
423 .version = LIBAVUTIL_VERSION_INT,
426 AVCodec ff_libopenjpeg_decoder = {
427 .name = "libopenjpeg",
428 .type = AVMEDIA_TYPE_VIDEO,
429 .id = AV_CODEC_ID_JPEG2000,
430 .priv_data_size = sizeof(LibOpenJPEGContext),
431 .init = libopenjpeg_decode_init,
432 .decode = libopenjpeg_decode_frame,
433 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
434 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
435 .priv_class = &class,