diff options
author | Michael Bradshaw <mjbshaw@google.com> | 2015-11-01 19:11:12 -0800 |
---|---|---|
committer | Michael Niedermayer <michael@niedermayer.cc> | 2015-12-25 10:55:34 +0100 |
commit | 99eabcdd5f904e98a02184889ab84cc9d5c17a8c (patch) | |
tree | b2c7824ca28da8eeccaf367bf3e78816cb3a67c7 /libavcodec/libopenjpegdec.c | |
parent | 9cc1e644f39ed6a32e3a9bf5d0d6d663ca5791ab (diff) | |
download | ffmpeg-99eabcdd5f904e98a02184889ab84cc9d5c17a8c.tar.gz |
avcodec: add OpenJPEG 2.x compatibility
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
Diffstat (limited to 'libavcodec/libopenjpegdec.c')
-rw-r--r-- | libavcodec/libopenjpegdec.c | 173 |
1 files changed, 149 insertions, 24 deletions
diff --git a/libavcodec/libopenjpegdec.c b/libavcodec/libopenjpegdec.c index 8539e2c9bd..cae3d20986 100644 --- a/libavcodec/libopenjpegdec.c +++ b/libavcodec/libopenjpegdec.c @@ -36,10 +36,22 @@ #include "internal.h" #include "thread.h" -#if HAVE_OPENJPEG_1_5_OPENJPEG_H -# include <openjpeg-1.5/openjpeg.h> +#if HAVE_OPENJPEG_2_1_OPENJPEG_H +# include <openjpeg-2.1/openjpeg.h> +#elif HAVE_OPENJPEG_2_0_OPENJPEG_H +# include <openjpeg-2.0/openjpeg.h> +#elif HAVE_OPENJPEG_1_5_OPENJPEG_H +# include <openjpeg-1.5/openjpeg.h> #else -# include <openjpeg.h> +# include <openjpeg.h> +#endif + +#if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H +# define OPENJPEG_MAJOR_VERSION 2 +# define OPJ(x) OPJ_##x +#else +# define OPENJPEG_MAJOR_VERSION 1 +# define OPJ(x) x #endif #define JP2_SIG_TYPE 0x6A502020 @@ -83,7 +95,9 @@ static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = { typedef struct LibOpenJPEGContext { AVClass *class; opj_dparameters_t dec_params; +#if OPENJPEG_MAJOR_VERSION == 1 opj_event_mgr_t event_mgr; +#endif // OPENJPEG_MAJOR_VERSION == 1 int lowqual; } LibOpenJPEGContext; @@ -102,6 +116,62 @@ static void info_callback(const char *msg, void *data) av_log(data, AV_LOG_DEBUG, "%s", msg); } +#if OPENJPEG_MAJOR_VERSION == 2 +typedef struct BufferReader { + int pos; + int size; + const uint8_t *buffer; +} BufferReader; + +static OPJ_SIZE_T stream_read(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data) +{ + BufferReader *reader = user_data; + if (reader->pos == reader->size) { + return (OPJ_SIZE_T)-1; + } + int remaining = reader->size - reader->pos; + if (nb_bytes > remaining) { + nb_bytes = remaining; + } + memcpy(out_buffer, reader->buffer + reader->pos, nb_bytes); + reader->pos += (int)nb_bytes; + return nb_bytes; +} + +static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data) +{ + BufferReader *reader = user_data; + if (nb_bytes < 0) { + if (reader->pos == 0) { + return (OPJ_SIZE_T)-1; + } + if (nb_bytes + reader->pos < 0) { + nb_bytes = -reader->pos; + } + } else { + if (reader->pos == reader->size) { + return (OPJ_SIZE_T)-1; + } + int remaining = reader->size - reader->pos; + if (nb_bytes > remaining) { + nb_bytes = remaining; + } + } + reader->pos += (int)nb_bytes; + return nb_bytes; +} + +static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data) +{ + BufferReader *reader = user_data; + if (nb_bytes < 0 || nb_bytes > reader->size) { + return OPJ_FALSE; + } + reader->pos = (int)nb_bytes; + return OPJ_TRUE; +} +#endif // OPENJPEG_MAJOR_VERSION == 2 + static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt) { const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt); @@ -145,15 +215,15 @@ static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *im int possible_fmts_nb = 0; switch (image->color_space) { - case CLRSPC_SRGB: + case OPJ(CLRSPC_SRGB): possible_fmts = libopenjpeg_rgb_pix_fmts; possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts); break; - case CLRSPC_GRAY: + case OPJ(CLRSPC_GRAY): possible_fmts = libopenjpeg_gray_pix_fmts; possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts); break; - case CLRSPC_SYCC: + case OPJ(CLRSPC_SYCC): possible_fmts = libopenjpeg_yuv_pix_fmts; possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts); break; @@ -275,13 +345,19 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, ThreadFrame frame = { .f = data }; AVFrame *picture = data; const AVPixFmtDescriptor *desc; - opj_dinfo_t *dec; - opj_cio_t *stream; - opj_image_t *image; int width, height, ret; int pixel_size = 0; int ispacked = 0; int i; + opj_image_t *image = NULL; +#if OPENJPEG_MAJOR_VERSION == 1 + opj_dinfo_t *dec = NULL; + opj_cio_t *stream = NULL; +#else // OPENJPEG_MAJOR_VERSION == 2 + BufferReader reader = {0, avpkt->size, avpkt->data}; + opj_codec_t *dec = NULL; + opj_stream_t *stream = NULL; +#endif // OPENJPEG_MAJOR_VERSION == 1 *got_frame = 0; @@ -289,19 +365,22 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, if ((AV_RB32(buf) == 12) && (AV_RB32(buf + 4) == JP2_SIG_TYPE) && (AV_RB32(buf + 8) == JP2_SIG_VALUE)) { - dec = opj_create_decompress(CODEC_JP2); + dec = opj_create_decompress(OPJ(CODEC_JP2)); } else { /* If the AVPacket contains a jp2c box, then skip to * the starting byte of the codestream. */ if (AV_RB32(buf + 4) == AV_RB32("jp2c")) buf += 8; - dec = opj_create_decompress(CODEC_J2K); + dec = opj_create_decompress(OPJ(CODEC_J2K)); } if (!dec) { av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n"); - return AVERROR_UNKNOWN; + ret = AVERROR_EXTERNAL; + goto done; } + +#if OPENJPEG_MAJOR_VERSION == 1 memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr)); ctx->event_mgr.info_handler = info_callback; ctx->event_mgr.error_handler = error_callback; @@ -309,25 +388,61 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, opj_set_event_mgr((opj_common_ptr) dec, &ctx->event_mgr, avctx); ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER; ctx->dec_params.cp_layer = ctx->lowqual; +#else // OPENJPEG_MAJOR_VERSION == 2 + if (!opj_set_error_handler(dec, error_callback, avctx) || + !opj_set_warning_handler(dec, warning_callback, avctx) || + !opj_set_info_handler(dec, info_callback, avctx)) { + av_log(avctx, AV_LOG_ERROR, "Error setting decoder handlers.\n"); + ret = AVERROR_EXTERNAL; + goto done; + } + + ctx->dec_params.cp_layer = ctx->lowqual; + ctx->dec_params.cp_reduce = avctx->lowres; +#endif // OPENJPEG_MAJOR_VERSION == 1 + // Tie decoder with decoding parameters opj_setup_decoder(dec, &ctx->dec_params); + +#if OPENJPEG_MAJOR_VERSION == 1 stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size); +#else // OPENJPEG_MAJOR_VERSION == 2 + stream = opj_stream_default_create(OPJ_STREAM_READ); +#endif // OPENJPEG_MAJOR_VERSION == 1 if (!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); - opj_destroy_decompress(dec); - return AVERROR_UNKNOWN; + ret = AVERROR_EXTERNAL; + goto done; } +#if OPENJPEG_MAJOR_VERSION == 1 // Decode the header only. image = opj_decode_with_info(dec, stream, NULL); opj_cio_close(stream); + stream = NULL; + ret = !image; +#else // OPENJPEG_MAJOR_VERSION == 2 + opj_stream_set_read_function(stream, stream_read); + opj_stream_set_skip_function(stream, stream_skip); + opj_stream_set_seek_function(stream, stream_seek); +#if HAVE_OPENJPEG_2_1_OPENJPEG_H + opj_stream_set_user_data(stream, &reader, NULL); +#elif HAVE_OPENJPEG_2_0_OPENJPEG_H + opj_stream_set_user_data(stream, &reader); +#else +#error Missing call to opj_stream_set_user_data +#endif + opj_stream_set_user_data_length(stream, avpkt->size); + // Decode the header only. + ret = !opj_read_header(stream, dec, &image); +#endif // OPENJPEG_MAJOR_VERSION == 1 - if (!image) { - av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); - opj_destroy_decompress(dec); - return AVERROR_UNKNOWN; + if (ret) { + av_log(avctx, AV_LOG_ERROR, "Error decoding codestream header.\n"); + ret = AVERROR_EXTERNAL; + goto done; } width = image->x1 - image->x0; @@ -345,7 +460,8 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image); if (avctx->pix_fmt == AV_PIX_FMT_NONE) { - av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n"); + av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format.\n"); + ret = AVERROR_UNKNOWN; goto done; } for (i = 0; i < image->numcomps; i++) @@ -355,6 +471,7 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) goto done; +#if OPENJPEG_MAJOR_VERSION == 1 ctx->dec_params.cp_limit_decoding = NO_LIMITATION; ctx->dec_params.cp_reduce = avctx->lowres; // Tie decoder with decoding parameters. @@ -363,18 +480,20 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, if (!stream) { av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n"); - ret = AVERROR_UNKNOWN; + ret = AVERROR_EXTERNAL; goto done; } - opj_image_destroy(image); // Decode the codestream image = opj_decode_with_info(dec, stream, NULL); - opj_cio_close(stream); + ret = !image; +#else // OPENJPEG_MAJOR_VERSION == 2 + ret = !opj_decode(dec, stream, image); +#endif // OPENJPEG_MAJOR_VERSION == 1 - if (!image) { + if (ret) { av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n"); - ret = AVERROR_UNKNOWN; + ret = AVERROR_EXTERNAL; goto done; } @@ -429,7 +548,13 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, done: opj_image_destroy(image); +#if OPENJPEG_MAJOR_VERSION == 2 + opj_stream_destroy(stream); + opj_destroy_codec(dec); +#else + opj_cio_close(stream); opj_destroy_decompress(dec); +#endif return ret; } |