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
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/opt.h"
38 #if HAVE_OPENJPEG_1_5_OPENJPEG_H
39 # include <openjpeg-1.5/openjpeg.h>
41 # include <openjpeg.h>
44 #define JP2_SIG_TYPE 0x6A502020
45 #define JP2_SIG_VALUE 0x0D0A870A
47 // pix_fmts with lower bpp have to be listed before
48 // similar pix_fmts with higher bpp.
49 #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
50 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
51 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
52 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
53 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
54 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
55 AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
56 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
57 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
58 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
59 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
60 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
61 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
63 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
65 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
66 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
67 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
68 static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,
75 opj_dparameters_t dec_params;
79 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
81 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
84 if (desc->nb_components != image->numcomps) {
88 switch (desc->nb_components) {
89 case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
90 1 == image->comps[3].dx &&
91 1 == image->comps[3].dy;
92 case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
93 1 << desc->log2_chroma_w == image->comps[2].dx &&
94 1 << desc->log2_chroma_h == image->comps[2].dy;
95 case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
96 1 << desc->log2_chroma_w == image->comps[1].dx &&
97 1 << desc->log2_chroma_h == image->comps[1].dy;
98 case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
99 1 == image->comps[0].dx &&
100 1 == image->comps[0].dy;
108 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
110 const enum AVPixelFormat *possible_fmts = NULL;
111 int possible_fmts_nb = 0;
113 switch (image->color_space) {
115 possible_fmts = libopenjpeg_rgb_pix_fmts;
116 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
119 possible_fmts = libopenjpeg_gray_pix_fmts;
120 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
123 possible_fmts = libopenjpeg_yuv_pix_fmts;
124 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
127 possible_fmts = libopenjpeg_all_pix_fmts;
128 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
132 for (index = 0; index < possible_fmts_nb; ++index) {
133 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
134 return possible_fmts[index];
138 return AV_PIX_FMT_NONE;
141 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
143 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
144 int i, component_plane;
146 if (pix_fmt == AV_PIX_FMT_GRAY16)
149 component_plane = desc->comp[0].plane;
150 for (i = 1; i < desc->nb_components; i++) {
151 if (component_plane != desc->comp[i].plane)
157 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
160 for (y = 0; y < picture->height; y++) {
161 index = y*picture->width;
162 img_ptr = picture->data[0] + y*picture->linesize[0];
163 for (x = 0; x < picture->width; x++, index++) {
164 for (c = 0; c < image->numcomps; c++) {
165 *img_ptr++ = image->comps[c].data[index];
171 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
175 for (x = 0; x < image->numcomps; x++)
176 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
178 for (y = 0; y < picture->height; y++) {
179 index = y*picture->width;
180 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
181 for (x = 0; x < picture->width; x++, index++) {
182 for (c = 0; c < image->numcomps; c++) {
183 *img_ptr++ = image->comps[c].data[index] << adjust[c];
189 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
194 for (index = 0; index < image->numcomps; index++) {
195 comp_data = image->comps[index].data;
196 for (y = 0; y < image->comps[index].h; y++) {
197 img_ptr = picture->data[index] + y * picture->linesize[index];
198 for (x = 0; x < image->comps[index].w; x++) {
199 *img_ptr = (uint8_t) *comp_data;
207 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
212 for (x = 0; x < image->numcomps; x++)
213 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
215 for (index = 0; index < image->numcomps; index++) {
216 comp_data = image->comps[index].data;
217 for (y = 0; y < image->comps[index].h; y++) {
218 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
219 for (x = 0; x < image->comps[index].w; x++) {
220 *img_ptr = *comp_data << adjust[index];
228 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
230 LibOpenJPEGContext *ctx = avctx->priv_data;
232 opj_set_default_decoder_parameters(&ctx->dec_params);
236 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
237 void *data, int *got_frame,
240 uint8_t *buf = avpkt->data;
241 int buf_size = avpkt->size;
242 LibOpenJPEGContext *ctx = avctx->priv_data;
243 ThreadFrame frame = { .f = data };
244 AVFrame *picture = data;
245 const AVPixFmtDescriptor *desc;
249 int width, height, ret;
256 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
257 if ((AV_RB32(buf) == 12) &&
258 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
259 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
260 dec = opj_create_decompress(CODEC_JP2);
262 /* If the AVPacket contains a jp2c box, then skip to
263 * the starting byte of the codestream. */
264 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
266 dec = opj_create_decompress(CODEC_J2K);
270 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
271 return AVERROR_UNKNOWN;
273 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
274 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
275 ctx->dec_params.cp_layer = ctx->lowqual;
276 // Tie decoder with decoding parameters
277 opj_setup_decoder(dec, &ctx->dec_params);
278 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
281 av_log(avctx, AV_LOG_ERROR,
282 "Codestream could not be opened for reading.\n");
283 opj_destroy_decompress(dec);
284 return AVERROR_UNKNOWN;
287 // Decode the header only.
288 image = opj_decode_with_info(dec, stream, NULL);
289 opj_cio_close(stream);
292 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
293 opj_destroy_decompress(dec);
294 return AVERROR_UNKNOWN;
297 width = image->x1 - image->x0;
298 height = image->y1 - image->y0;
300 ret = ff_set_dimensions(avctx, width, height);
304 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
305 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
306 avctx->pix_fmt = AV_PIX_FMT_NONE;
308 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
309 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
311 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
312 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
315 for (i = 0; i < image->numcomps; i++)
316 if (image->comps[i].prec > avctx->bits_per_raw_sample)
317 avctx->bits_per_raw_sample = image->comps[i].prec;
319 if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
322 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
323 ctx->dec_params.cp_reduce = avctx->lowres;
324 // Tie decoder with decoding parameters.
325 opj_setup_decoder(dec, &ctx->dec_params);
326 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
328 av_log(avctx, AV_LOG_ERROR,
329 "Codestream could not be opened for reading.\n");
330 ret = AVERROR_UNKNOWN;
334 opj_image_destroy(image);
335 // Decode the codestream
336 image = opj_decode_with_info(dec, stream, NULL);
337 opj_cio_close(stream);
340 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
341 ret = AVERROR_UNKNOWN;
345 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
346 pixel_size = desc->comp[0].step_minus1 + 1;
347 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
349 switch (pixel_size) {
352 libopenjpeg_copy_to_packed8(picture, image);
354 libopenjpeg_copyto8(picture, image);
359 libopenjpeg_copy_to_packed8(picture, image);
361 libopenjpeg_copyto16(picture, image);
367 libopenjpeg_copy_to_packed8(picture, image);
373 libopenjpeg_copy_to_packed16(picture, image);
377 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
378 ret = AVERROR_PATCHWELCOME;
386 opj_image_destroy(image);
387 opj_destroy_decompress(dec);
391 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
392 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
394 static const AVOption options[] = {
395 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
399 static const AVClass openjpeg_class = {
400 .class_name = "libopenjpeg",
401 .item_name = av_default_item_name,
403 .version = LIBAVUTIL_VERSION_INT,
406 AVCodec ff_libopenjpeg_decoder = {
407 .name = "libopenjpeg",
408 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
409 .type = AVMEDIA_TYPE_VIDEO,
410 .id = AV_CODEC_ID_JPEG2000,
411 .priv_data_size = sizeof(LibOpenJPEGContext),
412 .init = libopenjpeg_decode_init,
413 .decode = libopenjpeg_decode_frame,
414 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
416 .priv_class = &openjpeg_class,