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/intreadwrite.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/opt.h"
37 #define JP2_SIG_TYPE 0x6A502020
38 #define JP2_SIG_VALUE 0x0D0A870A
40 // pix_fmts with lower bpp have to be listed before
41 // similar pix_fmts with higher bpp.
42 #define RGB_PIXEL_FORMATS PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64
43 #define GRAY_PIXEL_FORMATS PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16
44 #define YUV_PIXEL_FORMATS PIX_FMT_YUV410P,PIX_FMT_YUV411P,PIX_FMT_YUVA420P, \
45 PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA422P, \
46 PIX_FMT_YUV440P,PIX_FMT_YUV444P,PIX_FMT_YUVA444P, \
47 PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9, \
48 PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10, \
49 PIX_FMT_YUV420P12,PIX_FMT_YUV422P12,PIX_FMT_YUV444P12, \
50 PIX_FMT_YUV420P14,PIX_FMT_YUV422P14,PIX_FMT_YUV444P14, \
51 PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16
53 static const enum PixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
54 static const enum PixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
55 static const enum PixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
56 static const enum PixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
60 opj_dparameters_t dec_params;
65 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum PixelFormat pix_fmt)
67 AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
70 if (descriptor.nb_components != image->numcomps) {
74 switch (descriptor.nb_components) {
75 case 4: match = match && descriptor.comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
76 1 == image->comps[3].dx &&
77 1 == image->comps[3].dy;
78 case 3: match = match && descriptor.comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
79 1 << descriptor.log2_chroma_w == image->comps[2].dx &&
80 1 << descriptor.log2_chroma_h == image->comps[2].dy;
81 case 2: match = match && descriptor.comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
82 1 << descriptor.log2_chroma_w == image->comps[1].dx &&
83 1 << descriptor.log2_chroma_h == image->comps[1].dy;
84 case 1: match = match && descriptor.comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
85 1 == image->comps[0].dx &&
86 1 == image->comps[0].dy;
94 static inline enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
96 const enum PixelFormat *possible_fmts = NULL;
97 int possible_fmts_nb = 0;
99 switch (image->color_space) {
101 possible_fmts = libopenjpeg_rgb_pix_fmts;
102 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
105 possible_fmts = libopenjpeg_gray_pix_fmts;
106 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
109 possible_fmts = libopenjpeg_yuv_pix_fmts;
110 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
113 possible_fmts = libopenjpeg_all_pix_fmts;
114 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
118 for (index = 0; index < possible_fmts_nb; ++index) {
119 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
120 return possible_fmts[index];
127 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
128 int i, component_plane;
130 if (pix_fmt == PIX_FMT_GRAY16)
133 component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
134 for (i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
135 if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
141 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
144 for (y = 0; y < picture->height; y++) {
145 index = y*picture->width;
146 img_ptr = picture->data[0] + y*picture->linesize[0];
147 for (x = 0; x < picture->width; x++, index++) {
148 for (c = 0; c < image->numcomps; c++) {
149 *img_ptr++ = image->comps[c].data[index];
155 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
159 for (x = 0; x < image->numcomps; x++)
160 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
162 for (y = 0; y < picture->height; y++) {
163 index = y*picture->width;
164 img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
165 for (x = 0; x < picture->width; x++, index++) {
166 for (c = 0; c < image->numcomps; c++) {
167 *img_ptr++ = image->comps[c].data[index] << adjust[c];
173 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
178 for (index = 0; index < image->numcomps; index++) {
179 comp_data = image->comps[index].data;
180 for (y = 0; y < image->comps[index].h; y++) {
181 img_ptr = picture->data[index] + y * picture->linesize[index];
182 for (x = 0; x < image->comps[index].w; x++) {
183 *img_ptr = (uint8_t) *comp_data;
191 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
195 for (index = 0; index < image->numcomps; index++) {
196 comp_data = image->comps[index].data;
197 for (y = 0; y < image->comps[index].h; y++) {
198 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
199 for (x = 0; x < image->comps[index].w; x++) {
200 *img_ptr = *comp_data;
208 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
210 LibOpenJPEGContext *ctx = avctx->priv_data;
212 opj_set_default_decoder_parameters(&ctx->dec_params);
213 avcodec_get_frame_defaults(&ctx->image);
214 avctx->coded_frame = &ctx->image;
218 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
220 LibOpenJPEGContext *ctx = avctx->priv_data;
222 avctx->coded_frame = &ctx->image;
226 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
227 void *data, int *data_size,
230 uint8_t *buf = avpkt->data;
231 int buf_size = avpkt->size;
232 LibOpenJPEGContext *ctx = avctx->priv_data;
233 AVFrame *picture = &ctx->image, *output = data;
237 int width, height, ret = -1;
244 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
245 if ((AV_RB32(buf) == 12) &&
246 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
247 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
248 dec = opj_create_decompress(CODEC_JP2);
250 /* If the AVPacket contains a jp2c box, then skip to
251 * the starting byte of the codestream. */
252 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
254 dec = opj_create_decompress(CODEC_J2K);
258 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
261 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
262 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
263 ctx->dec_params.cp_layer = ctx->lowqual;
264 // Tie decoder with decoding parameters
265 opj_setup_decoder(dec, &ctx->dec_params);
266 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
269 av_log(avctx, AV_LOG_ERROR,
270 "Codestream could not be opened for reading.\n");
271 opj_destroy_decompress(dec);
275 // Decode the header only.
276 image = opj_decode_with_info(dec, stream, NULL);
277 opj_cio_close(stream);
280 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
281 opj_destroy_decompress(dec);
285 width = image->x1 - image->x0;
286 height = image->y1 - image->y0;
288 if (av_image_check_size(width, height, 0, avctx) < 0) {
289 av_log(avctx, AV_LOG_ERROR,
290 "%dx%d dimension invalid.\n", width, height);
294 avcodec_set_dimensions(avctx, width, height);
296 if (avctx->pix_fmt != PIX_FMT_NONE)
297 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
298 avctx->pix_fmt = PIX_FMT_NONE;
300 if (avctx->pix_fmt == PIX_FMT_NONE)
301 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
303 if (avctx->pix_fmt == PIX_FMT_NONE) {
304 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
307 for (i = 0; i < image->numcomps; i++)
308 if (image->comps[i].prec > avctx->bits_per_raw_sample)
309 avctx->bits_per_raw_sample = image->comps[i].prec;
311 if (picture->data[0])
312 ff_thread_release_buffer(avctx, picture);
314 if (ff_thread_get_buffer(avctx, picture) < 0) {
315 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
319 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
320 ctx->dec_params.cp_reduce = avctx->lowres;
321 // Tie decoder with decoding parameters.
322 opj_setup_decoder(dec, &ctx->dec_params);
323 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
325 av_log(avctx, AV_LOG_ERROR,
326 "Codestream could not be opened for reading.\n");
330 opj_image_destroy(image);
331 // Decode the codestream
332 image = opj_decode_with_info(dec, stream, NULL);
333 opj_cio_close(stream);
336 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
340 pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
341 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
343 switch (pixel_size) {
346 libopenjpeg_copy_to_packed8(picture, image);
348 libopenjpeg_copyto8(picture, image);
353 libopenjpeg_copy_to_packed8(picture, image);
355 libopenjpeg_copyto16(picture, image);
361 libopenjpeg_copy_to_packed8(picture, image);
367 libopenjpeg_copy_to_packed16(picture, image);
371 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
375 *output = ctx->image;
376 *data_size = sizeof(AVPicture);
380 opj_image_destroy(image);
381 opj_destroy_decompress(dec);
385 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
387 LibOpenJPEGContext *ctx = avctx->priv_data;
389 if (ctx->image.data[0])
390 ff_thread_release_buffer(avctx, &ctx->image);
394 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
395 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
397 static const AVOption options[] = {
398 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
402 static const AVClass class = {
403 .class_name = "libopenjpeg",
404 .item_name = av_default_item_name,
406 .version = LIBAVUTIL_VERSION_INT,
409 AVCodec ff_libopenjpeg_decoder = {
410 .name = "libopenjpeg",
411 .type = AVMEDIA_TYPE_VIDEO,
412 .id = CODEC_ID_JPEG2000,
413 .priv_data_size = sizeof(LibOpenJPEGContext),
414 .init = libopenjpeg_decode_init,
415 .close = libopenjpeg_decode_close,
416 .decode = libopenjpeg_decode_frame,
417 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
419 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
420 .priv_class = &class,
421 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),