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_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
50 AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
51 AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
52 AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
53 AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
54 AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
55 AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
57 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
58 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
59 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
60 static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
64 opj_dparameters_t dec_params;
69 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
71 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
74 if (desc->nb_components != image->numcomps) {
78 switch (desc->nb_components) {
79 case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
80 1 == image->comps[3].dx &&
81 1 == image->comps[3].dy;
82 case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
83 1 << desc->log2_chroma_w == image->comps[2].dx &&
84 1 << desc->log2_chroma_h == image->comps[2].dy;
85 case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
86 1 << desc->log2_chroma_w == image->comps[1].dx &&
87 1 << desc->log2_chroma_h == image->comps[1].dy;
88 case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
89 1 == image->comps[0].dx &&
90 1 == image->comps[0].dy;
98 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
100 const enum AVPixelFormat *possible_fmts = NULL;
101 int possible_fmts_nb = 0;
103 switch (image->color_space) {
105 possible_fmts = libopenjpeg_rgb_pix_fmts;
106 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
109 possible_fmts = libopenjpeg_gray_pix_fmts;
110 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
113 possible_fmts = libopenjpeg_yuv_pix_fmts;
114 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
117 possible_fmts = libopenjpeg_all_pix_fmts;
118 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
122 for (index = 0; index < possible_fmts_nb; ++index) {
123 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
124 return possible_fmts[index];
128 return AV_PIX_FMT_NONE;
131 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
133 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
134 int i, component_plane;
136 if (pix_fmt == AV_PIX_FMT_GRAY16)
139 component_plane = desc->comp[0].plane;
140 for (i = 1; i < desc->nb_components; i++) {
141 if (component_plane != desc->comp[i].plane)
147 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
150 for (y = 0; y < picture->height; y++) {
151 index = y*picture->width;
152 img_ptr = picture->data[0] + y*picture->linesize[0];
153 for (x = 0; x < picture->width; x++, index++) {
154 for (c = 0; c < image->numcomps; c++) {
155 *img_ptr++ = image->comps[c].data[index];
161 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
165 for (x = 0; x < image->numcomps; x++)
166 adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
168 for (y = 0; y < picture->height; y++) {
169 index = y*picture->width;
170 img_ptr = (uint16_t*) (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] << adjust[c];
179 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
184 for (index = 0; index < image->numcomps; index++) {
185 comp_data = image->comps[index].data;
186 for (y = 0; y < image->comps[index].h; y++) {
187 img_ptr = picture->data[index] + y * picture->linesize[index];
188 for (x = 0; x < image->comps[index].w; x++) {
189 *img_ptr = (uint8_t) *comp_data;
197 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
201 for (index = 0; index < image->numcomps; index++) {
202 comp_data = image->comps[index].data;
203 for (y = 0; y < image->comps[index].h; y++) {
204 img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
205 for (x = 0; x < image->comps[index].w; x++) {
206 *img_ptr = *comp_data;
214 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
216 LibOpenJPEGContext *ctx = avctx->priv_data;
218 opj_set_default_decoder_parameters(&ctx->dec_params);
219 avcodec_get_frame_defaults(&ctx->image);
220 avctx->coded_frame = &ctx->image;
224 static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
226 LibOpenJPEGContext *ctx = avctx->priv_data;
228 avctx->coded_frame = &ctx->image;
232 static int libopenjpeg_decode_frame(AVCodecContext *avctx,
233 void *data, int *got_frame,
236 uint8_t *buf = avpkt->data;
237 int buf_size = avpkt->size;
238 LibOpenJPEGContext *ctx = avctx->priv_data;
239 AVFrame *picture = &ctx->image, *output = data;
240 const AVPixFmtDescriptor *desc;
244 int width, height, ret = -1;
251 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
252 if ((AV_RB32(buf) == 12) &&
253 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
254 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
255 dec = opj_create_decompress(CODEC_JP2);
257 /* If the AVPacket contains a jp2c box, then skip to
258 * the starting byte of the codestream. */
259 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
261 dec = opj_create_decompress(CODEC_J2K);
265 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
268 opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
269 ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
270 ctx->dec_params.cp_layer = ctx->lowqual;
271 // Tie decoder with decoding parameters
272 opj_setup_decoder(dec, &ctx->dec_params);
273 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
276 av_log(avctx, AV_LOG_ERROR,
277 "Codestream could not be opened for reading.\n");
278 opj_destroy_decompress(dec);
282 // Decode the header only.
283 image = opj_decode_with_info(dec, stream, NULL);
284 opj_cio_close(stream);
287 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
288 opj_destroy_decompress(dec);
292 width = image->x1 - image->x0;
293 height = image->y1 - image->y0;
295 if (av_image_check_size(width, height, 0, avctx) < 0) {
296 av_log(avctx, AV_LOG_ERROR,
297 "%dx%d dimension invalid.\n", width, height);
301 avcodec_set_dimensions(avctx, width, height);
303 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
304 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
305 avctx->pix_fmt = AV_PIX_FMT_NONE;
307 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
308 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
310 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
311 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
314 for (i = 0; i < image->numcomps; i++)
315 if (image->comps[i].prec > avctx->bits_per_raw_sample)
316 avctx->bits_per_raw_sample = image->comps[i].prec;
318 if (picture->data[0])
319 ff_thread_release_buffer(avctx, picture);
321 if (ff_thread_get_buffer(avctx, picture) < 0) {
322 av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
326 ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
327 ctx->dec_params.cp_reduce = avctx->lowres;
328 // Tie decoder with decoding parameters.
329 opj_setup_decoder(dec, &ctx->dec_params);
330 stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
332 av_log(avctx, AV_LOG_ERROR,
333 "Codestream could not be opened for reading.\n");
337 opj_image_destroy(image);
338 // Decode the codestream
339 image = opj_decode_with_info(dec, stream, NULL);
340 opj_cio_close(stream);
343 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
347 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
348 pixel_size = desc->comp[0].step_minus1 + 1;
349 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
351 switch (pixel_size) {
354 libopenjpeg_copy_to_packed8(picture, image);
356 libopenjpeg_copyto8(picture, image);
361 libopenjpeg_copy_to_packed8(picture, image);
363 libopenjpeg_copyto16(picture, image);
369 libopenjpeg_copy_to_packed8(picture, image);
375 libopenjpeg_copy_to_packed16(picture, image);
379 av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
383 *output = ctx->image;
388 opj_image_destroy(image);
389 opj_destroy_decompress(dec);
393 static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
395 LibOpenJPEGContext *ctx = avctx->priv_data;
397 if (ctx->image.data[0])
398 ff_thread_release_buffer(avctx, &ctx->image);
402 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
403 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
405 static const AVOption options[] = {
406 { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
410 static const AVClass class = {
411 .class_name = "libopenjpeg",
412 .item_name = av_default_item_name,
414 .version = LIBAVUTIL_VERSION_INT,
417 AVCodec ff_libopenjpeg_decoder = {
418 .name = "libopenjpeg",
419 .type = AVMEDIA_TYPE_VIDEO,
420 .id = AV_CODEC_ID_JPEG2000,
421 .priv_data_size = sizeof(LibOpenJPEGContext),
422 .init = libopenjpeg_decode_init,
423 .close = libopenjpeg_decode_close,
424 .decode = libopenjpeg_decode_frame,
425 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
427 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
428 .priv_class = &class,
429 .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),