Commit b7a928b2 authored by Michael Bradshaw's avatar Michael Bradshaw

libopenjpegdec: respect JP2 color space, fix ticket 1179

Signed-off-by: 's avatarMichael Bradshaw <mbradshaw@sorensonmedia.com>
parent af58a77f
...@@ -35,73 +35,83 @@ ...@@ -35,73 +35,83 @@
#define JP2_SIG_TYPE 0x6A502020 #define JP2_SIG_TYPE 0x6A502020
#define JP2_SIG_VALUE 0x0D0A870A #define JP2_SIG_VALUE 0x0D0A870A
#define RGB_PIXEL_FORMATS PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64
#define GRAY_PIXEL_FORMATS PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16
#define YUV_PIXEL_FORMATS PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P, \
PIX_FMT_YUV440P,PIX_FMT_YUV444P, \
PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9, \
PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10, \
PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16
static const enum PixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
static const enum PixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
static const enum PixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
static const enum PixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
typedef struct { typedef struct {
opj_dparameters_t dec_params; opj_dparameters_t dec_params;
AVFrame image; AVFrame image;
} LibOpenJPEGContext; } LibOpenJPEGContext;
static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image) static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum PixelFormat pix_fmt){
{ AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
opj_image_comp_t c0 = image->comps[0]; int match = 1;
opj_image_comp_t c1 = image->comps[1];
opj_image_comp_t c2 = image->comps[2];
int compRatio = 0;
compRatio |= c0.dx << 15 | c0.dy << 12;
compRatio |= c1.dx << 9 | c1.dy << 6;
compRatio |= c2.dx << 3 | c2.dy;
if (image->numcomps == 4) {
if (c0.prec == 8) {
if (compRatio == 0112222 &&
image->comps[3].dx == 1 && image->comps[3].dy == 1) {
return PIX_FMT_YUVA420P;
} else {
return PIX_FMT_RGBA;
}
} else {
return PIX_FMT_RGBA64;
}
}
switch (compRatio) { if (descriptor.nb_components != image->numcomps) {
case 0111111: goto libopenjpeg_yuv444_rgb; return 0;
case 0111212: return PIX_FMT_YUV440P;
case 0112121: goto libopenjpeg_yuv422;
case 0112222: goto libopenjpeg_yuv420;
default: goto libopenjpeg_rgb;
} }
libopenjpeg_yuv420: switch (descriptor.nb_components) {
switch (c0.prec) { case 4: match = match && descriptor.comp[3].depth_minus1 + 1 == image->comps[3].prec &&
case 8: return PIX_FMT_YUV420P; 1 << descriptor.log2_chroma_w == image->comps[3].dx &&
case 9: return PIX_FMT_YUV420P9; 1 << descriptor.log2_chroma_h == image->comps[3].dy;
case 10: return PIX_FMT_YUV420P10; case 3: match = match && descriptor.comp[2].depth_minus1 + 1 == image->comps[2].prec &&
case 16: return PIX_FMT_YUV420P16; 1 << descriptor.log2_chroma_w == image->comps[2].dx &&
1 << descriptor.log2_chroma_h == image->comps[2].dy;
case 2: match = match && descriptor.comp[1].depth_minus1 + 1 == image->comps[1].prec &&
1 << descriptor.log2_chroma_w == image->comps[1].dx &&
1 << descriptor.log2_chroma_h == image->comps[1].dy;
case 1: match = match && descriptor.comp[0].depth_minus1 + 1 == image->comps[0].prec &&
1 == image->comps[0].dx &&
1 == image->comps[0].dy;
default:
break;
} }
libopenjpeg_yuv422: return match;
switch (c0.prec) { }
case 8: return PIX_FMT_YUV422P;
case 9: return PIX_FMT_YUV422P9; static inline enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
case 10: return PIX_FMT_YUV422P10; int index;
case 16: return PIX_FMT_YUV422P16; const enum PixelFormat *possible_fmts = NULL;
} int possible_fmts_nb = 0;
libopenjpeg_yuv444_rgb: switch (image->color_space) {
switch (c0.prec) { case CLRSPC_SRGB:
case 8: return PIX_FMT_RGB24; possible_fmts = libopenjpeg_rgb_pix_fmts;
case 9: return PIX_FMT_YUV444P9; possible_fmts_nb = sizeof(libopenjpeg_rgb_pix_fmts) / sizeof(enum PixelFormat);
case 10: return PIX_FMT_YUV444P10; break;
case 16: return PIX_FMT_YUV444P16; case CLRSPC_GRAY:
possible_fmts = libopenjpeg_gray_pix_fmts;
possible_fmts_nb = sizeof(libopenjpeg_gray_pix_fmts) / sizeof(enum PixelFormat);
break;
case CLRSPC_SYCC:
possible_fmts = libopenjpeg_yuv_pix_fmts;
possible_fmts_nb = sizeof(libopenjpeg_yuv_pix_fmts) / sizeof(enum PixelFormat);
break;
default:
possible_fmts = libopenjpeg_all_pix_fmts;
possible_fmts_nb = sizeof(libopenjpeg_all_pix_fmts) / sizeof(enum PixelFormat);
break;
} }
libopenjpeg_rgb: for (index = 0; index < possible_fmts_nb; ++index) {
switch (c0.prec) { if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
case 8: return PIX_FMT_RGB24; return possible_fmts[index];
default: return PIX_FMT_RGB48; }
} }
return PIX_FMT_RGB24; return PIX_FMT_NONE;
} }
static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) { static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
...@@ -265,16 +275,19 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx, ...@@ -265,16 +275,19 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
} }
avcodec_set_dimensions(avctx, width, height); avcodec_set_dimensions(avctx, width, height);
switch (image->numcomps) { if (avctx->pix_fmt != PIX_FMT_NONE) {
case 1: avctx->pix_fmt = (image->comps[0].prec == 8) ? PIX_FMT_GRAY8 : PIX_FMT_GRAY16; if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt)) {
break; avctx->pix_fmt = PIX_FMT_NONE;
case 2: avctx->pix_fmt = PIX_FMT_GRAY8A; }
break; }
case 3:
case 4: avctx->pix_fmt = check_image_attributes(avctx, image); if (avctx->pix_fmt == PIX_FMT_NONE) {
break; avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps); }
goto done;
if (avctx->pix_fmt == PIX_FMT_NONE) {
av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
goto done;
} }
if(picture->data[0]) if(picture->data[0])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment