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
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,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
44 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
45 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
46 AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
47 AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
48 AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
49 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
50 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
51 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
52 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16
54 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
55 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
56 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
57 static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
61 opj_dparameters_t dec_params;
66 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
68 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
71 if (desc->nb_components != image->numcomps) {
75 switch (desc->nb_components) {
76 case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
77 1 == image->comps[3].dx &&
78 1 == image->comps[3].dy;
79 case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
80 1 << desc->log2_chroma_w == image->comps[2].dx &&
81 1 << desc->log2_chroma_h == image->comps[2].dy;
82 case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
83 1 << desc->log2_chroma_w == image->comps[1].dx &&
84 1 << desc->log2_chroma_h == image->comps[1].dy;
85 case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
86 1 == image->comps[0].dx &&
87 1 == image->comps[0].dy;
95 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
97 const enum AVPixelFormat *possible_fmts = NULL;
98 int possible_fmts_nb = 0;
100 switch (image->color_space) {
102 possible_fmts = libopenjpeg_rgb_pix_fmts;
103 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
106 possible_fmts = libopenjpeg_gray_pix_fmts;
107 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
110 possible_fmts = libopenjpeg_yuv_pix_fmts;
111 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
114 possible_fmts = libopenjpeg_all_pix_fmts;
115 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
119 for (index = 0; index < possible_fmts_nb; ++index) {
120 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
121 return possible_fmts[index];
125 return AV_PIX_FMT_NONE;
128 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
130 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
131 int i, component_plane;
133 if (pix_fmt == AV_PIX_FMT_GRAY16)
136 component_plane = desc->comp[0].plane;
137 for (i = 1; i < desc->nb_components; i++) {
138 if (component_plane != desc->comp[i].plane)
144 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
147 for (y = 0; y < picture->height; y++) {
148 index = y*picture->width;
149 img_ptr = picture->data[0] + y*picture->linesize[0];
150 for (x = 0; x < picture->width; x++, index++) {
151 for (c = 0; c < image->numcomps; c++) {
152 *img_ptr++ = image->comps[c].data[index];
158 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
162 for (x = 0; x < image->numcomps; x++)
163 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
165 for (y = 0; y < picture->height; y++) {
166 index = y*picture->width;
167 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
168 for (x = 0; x < picture->width; x++, index++) {
169 for (c = 0; c < image->numcomps; c++) {
170 *img_ptr++ = image->comps[c].data[index] << adjust[c];
176 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
181 for (index = 0; index < image->numcomps; index++) {
182 comp_data = image->comps[index].data;
183 for (y = 0; y < image->comps[index].h; y++) {
184 img_ptr = picture->data[index] + y * picture->linesize[index];
185 for (x = 0; x < image->comps[index].w; x++) {
186 *img_ptr = (uint8_t) *comp_data;
194 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
198 for (index = 0; index < image->numcomps; index++) {
199 comp_data = image->comps[index].data;
200 for (y = 0; y < image->comps[index].h; y++) {
201 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
202 for (x = 0; x < image->comps[index].w; x++) {
203 *img_ptr = *comp_data;
211 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
213 LibOpenJPEGContext *ctx = avctx->priv_data;
215 opj_set_default_decoder_parameters(&ctx->dec_params);
216 avcodec_get_frame_defaults(&ctx->image);
217 avctx->coded_frame = &ctx->image;
221 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
223 LibOpenJPEGContext *ctx = avctx->priv_data;
225 avctx->coded_frame = &ctx->image;
229 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
230 void *data, int *data_size,
233 uint8_t *buf = avpkt->data;
234 int buf_size = avpkt->size;
235 LibOpenJPEGContext *ctx = avctx->priv_data;
236 AVFrame *picture = &ctx->image, *output = data;
237 const AVPixFmtDescriptor *desc;
241 int width, height, ret = -1;
248 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
249 if ((AV_RB32(buf) == 12) &&
250 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
251 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
252 dec = opj_create_decompress(CODEC_JP2);
254 /* If the AVPacket contains a jp2c box, then skip to
255 * the starting byte of the codestream. */
256 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
258 dec = opj_create_decompress(CODEC_J2K);
262 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
265 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
266 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
267 ctx->dec_params.cp_layer = ctx->lowqual;
268 // Tie decoder with decoding parameters
269 opj_setup_decoder(dec, &ctx->dec_params);
270 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
273 av_log(avctx, AV_LOG_ERROR,
274 "Codestream could not be opened for reading.\n");
275 opj_destroy_decompress(dec);
279 // Decode the header only.
280 image = opj_decode_with_info(dec, stream, NULL);
281 opj_cio_close(stream);
284 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
285 opj_destroy_decompress(dec);
289 width = image->x1 - image->x0;
290 height = image->y1 - image->y0;
292 if (av_image_check_size(width, height, 0, avctx) < 0) {
293 av_log(avctx, AV_LOG_ERROR,
294 "%dx%d dimension invalid.\n", width, height);
298 avcodec_set_dimensions(avctx, width, height);
300 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
301 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
302 avctx->pix_fmt = AV_PIX_FMT_NONE;
304 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
305 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
307 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
308 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
311 for (i = 0; i < image->numcomps; i++)
312 if (image->comps[i].prec > avctx->bits_per_raw_sample)
313 avctx->bits_per_raw_sample = image->comps[i].prec;
315 if (picture->data[0])
316 ff_thread_release_buffer(avctx, picture);
318 if (ff_thread_get_buffer(avctx, picture) < 0) {
319 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
323 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
324 ctx->dec_params.cp_reduce = avctx->lowres;
325 // Tie decoder with decoding parameters.
326 opj_setup_decoder(dec, &ctx->dec_params);
327 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
329 av_log(avctx, AV_LOG_ERROR,
330 "Codestream could not be opened for reading.\n");
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");
344 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
345 pixel_size = desc->comp[0].step_minus1 + 1;
346 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
348 switch (pixel_size) {
351 libopenjpeg_copy_to_packed8(picture, image);
353 libopenjpeg_copyto8(picture, image);
358 libopenjpeg_copy_to_packed8(picture, image);
360 libopenjpeg_copyto16(picture, image);
366 libopenjpeg_copy_to_packed8(picture, image);
372 libopenjpeg_copy_to_packed16(picture, image);
376 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
380 *output = ctx->image;
381 *data_size = sizeof(AVPicture);
385 opj_image_destroy(image);
386 opj_destroy_decompress(dec);
390 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
392 LibOpenJPEGContext *ctx = avctx->priv_data;
394 if (ctx->image.data[0])
395 ff_thread_release_buffer(avctx, &ctx->image);
399 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
400 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
402 static const AVOption options[] = {
403 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
407 static const AVClass class = {
408 .class_name = "libopenjpeg",
409 .item_name = av_default_item_name,
411 .version = LIBAVUTIL_VERSION_INT,
414 AVCodec ff_libopenjpeg_decoder = {
415 .name = "libopenjpeg",
416 .type = AVMEDIA_TYPE_VIDEO,
417 .id = AV_CODEC_ID_JPEG2000,
418 .priv_data_size = sizeof(LibOpenJPEGContext),
419 .init = libopenjpeg_decode_init,
420 .close = libopenjpeg_decode_close,
421 .decode = libopenjpeg_decode_frame,
422 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
424 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
425 .priv_class = &class,
426 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),