]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libopenjpegdec.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libopenjpegdec.c
index 89bfb34913f602f604438c99e0250a36290f87d1..9928adba6ae25583cffca3ba623a15ca8051634d 100644 (file)
 #define JP2_SIG_TYPE    0x6A502020
 #define JP2_SIG_VALUE   0x0D0A870A
 
+// pix_fmts with lower bpp have to be listed before
+// similar pix_fmts with higher bpp.
+#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 {
     opj_dparameters_t dec_params;
     AVFrame image;
 } LibOpenJPEGContext;
 
-static enum PixelFormat check_image_attributes(AVCodecContext *avctx, opj_image_t *image)
-{
-    opj_image_comp_t c0 = image->comps[0];
-    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;
-        }
-    }
+static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum PixelFormat pix_fmt){
+    AVPixFmtDescriptor descriptor = av_pix_fmt_descriptors[pix_fmt];
+    int match = 1;
 
-    switch (compRatio) {
-    case 0111111: goto libopenjpeg_yuv444_rgb;
-    case 0111212: return PIX_FMT_YUV440P;
-    case 0112121: goto libopenjpeg_yuv422;
-    case 0112222: goto libopenjpeg_yuv420;
-    default: goto libopenjpeg_rgb;
+    if (descriptor.nb_components != image->numcomps) {
+        return 0;
     }
 
-libopenjpeg_yuv420:
-    switch (c0.prec) {
-    case 8:  return PIX_FMT_YUV420P;
-    case 9:  return PIX_FMT_YUV420P9;
-    case 10: return PIX_FMT_YUV420P10;
-    case 16: return PIX_FMT_YUV420P16;
+    switch (descriptor.nb_components) {
+    case 4: match = match && descriptor.comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
+                             1 << descriptor.log2_chroma_w == image->comps[3].dx &&
+                             1 << descriptor.log2_chroma_h == image->comps[3].dy;
+    case 3: match = match && descriptor.comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
+                             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:
-    switch (c0.prec) {
-    case 8:  return PIX_FMT_YUV422P;
-    case 9:  return PIX_FMT_YUV422P9;
-    case 10: return PIX_FMT_YUV422P10;
-    case 16: return PIX_FMT_YUV422P16;
-    }
+    return match;
+}
+
+static inline enum PixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
+    int index;
+    const enum PixelFormat *possible_fmts = NULL;
+    int possible_fmts_nb = 0;
 
-libopenjpeg_yuv444_rgb:
-    switch (c0.prec) {
-    case 8:  return PIX_FMT_RGB24;
-    case 9:  return PIX_FMT_YUV444P9;
-    case 10: return PIX_FMT_YUV444P10;
-    case 16: return PIX_FMT_YUV444P16;
+    switch (image->color_space) {
+    case CLRSPC_SRGB:
+        possible_fmts = libopenjpeg_rgb_pix_fmts;
+        possible_fmts_nb = sizeof(libopenjpeg_rgb_pix_fmts) / sizeof(enum PixelFormat);
+        break;
+    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:
-    switch (c0.prec) {
-    case 8: return PIX_FMT_RGB24;
-    default: return PIX_FMT_RGB48;
+    for (index = 0; index < possible_fmts_nb; ++index) {
+        if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
+            return possible_fmts[index];
+        }
     }
 
-    return PIX_FMT_RGB24;
+    return PIX_FMT_NONE;
 }
 
 static inline int libopenjpeg_ispacked(enum PixelFormat pix_fmt) {
     int i, component_plane;
+
+    if (pix_fmt == PIX_FMT_GRAY16)
+        return 0;
+
     component_plane = av_pix_fmt_descriptors[pix_fmt].comp[0].plane;
     for(i = 1; i < av_pix_fmt_descriptors[pix_fmt].nb_components; i++) {
         if (component_plane != av_pix_fmt_descriptors[pix_fmt].comp[i].plane)
@@ -213,6 +229,7 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
     int width, height, ret = -1;
     int pixel_size = 0;
     int ispacked = 0;
+    int i;
 
     *data_size = 0;
 
@@ -261,24 +278,30 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
     }
     avcodec_set_dimensions(avctx, width, height);
 
-    switch (image->numcomps) {
-    case 1:  avctx->pix_fmt = (image->comps[0].bpp == 8) ? PIX_FMT_GRAY8 : PIX_FMT_GRAY16;
-             break;
-    case 2:  avctx->pix_fmt = PIX_FMT_GRAY8A;
-             break;
-    case 3:
-    case 4:  avctx->pix_fmt = check_image_attributes(avctx, image);
-             break;
-    default: av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n", image->numcomps);
-             goto done;
+    if (avctx->pix_fmt != PIX_FMT_NONE) {
+        if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt)) {
+            avctx->pix_fmt = PIX_FMT_NONE;
+        }
+    }
+
+    if (avctx->pix_fmt == PIX_FMT_NONE) {
+        avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
     }
 
+    if (avctx->pix_fmt == PIX_FMT_NONE) {
+        av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
+        goto done;
+    }
+    for (i = 0; i < image->numcomps; i++)
+        if (image->comps[i].prec > avctx->bits_per_raw_sample)
+            avctx->bits_per_raw_sample = image->comps[i].prec;
+
     if(picture->data[0])
         ff_thread_release_buffer(avctx, picture);
 
     if(ff_thread_get_buffer(avctx, picture) < 0){
         av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
-        return -1;
+        goto done;
     }
 
     ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
@@ -288,13 +311,17 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
     stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
     if(!stream) {
         av_log(avctx, AV_LOG_ERROR, "Codestream could not be opened for reading.\n");
-        opj_destroy_decompress(dec);
-        return -1;
+        goto done;
     }
 
+    opj_image_destroy(image);
     // Decode the codestream
     image = opj_decode_with_info(dec, stream, NULL);
     opj_cio_close(stream);
+    if(!image) {
+        av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
+        goto done;
+    }
 
     pixel_size = av_pix_fmt_descriptors[avctx->pix_fmt].comp[0].step_minus1 + 1;
     ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
@@ -352,15 +379,15 @@ static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
 
 
 AVCodec ff_libopenjpeg_decoder = {
-    .name           = "libopenjpeg",
-    .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_JPEG2000,
-    .priv_data_size = sizeof(LibOpenJPEGContext),
-    .init           = libopenjpeg_decode_init,
-    .close          = libopenjpeg_decode_close,
-    .decode         = libopenjpeg_decode_frame,
-    .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
-    .max_lowres     = 5,
-    .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
-    .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy)
+    .name             = "libopenjpeg",
+    .type             = AVMEDIA_TYPE_VIDEO,
+    .id               = CODEC_ID_JPEG2000,
+    .priv_data_size   = sizeof(LibOpenJPEGContext),
+    .init             = libopenjpeg_decode_init,
+    .close            = libopenjpeg_decode_close,
+    .decode           = libopenjpeg_decode_frame,
+    .capabilities     = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
+    .max_lowres       = 5,
+    .long_name        = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
+    .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
 };