]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libopenjpegenc.c
Merge commit '0c00fd80ee4791bd70b634084307fc9f179e0412'
[ffmpeg] / libavcodec / libopenjpegenc.c
index b357fcdde6d61b611e09fbc93de4ac6e5c648567..0388fc513126989089ac95008e7808c3c087c711 100644 (file)
  */
 
 /**
-* @file
-* JPEG 2000 encoder using libopenjpeg
-*/
+ * @file
+ * JPEG 2000 encoder using libopenjpeg
+ */
+
+#define  OPJ_STATIC
+#include <openjpeg.h>
 
-#include "libavutil/opt.h"
-#include "libavutil/imgutils.h"
 #include "libavutil/avassert.h"
-#include "avcodec.h"
+#include "libavutil/imgutils.h"
 #include "libavutil/intreadwrite.h"
+#include "libavutil/opt.h"
+#include "avcodec.h"
 #include "internal.h"
-#define  OPJ_STATIC
-#include <openjpeg.h>
 
 typedef struct {
     AVClass *avclass;
@@ -41,8 +42,8 @@ typedef struct {
     opj_event_mgr_t event_mgr;
     int format;
     int profile;
-    int cinema_mode;
     int prog_order;
+    int cinema_mode;
     int numresolution;
     int numlayers;
     int disto_alloc;
@@ -52,12 +53,17 @@ typedef struct {
 
 static void error_callback(const char *msg, void *data)
 {
-    av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
+    av_log(data, AV_LOG_ERROR, "%s\n", msg);
 }
 
 static void warning_callback(const char *msg, void *data)
 {
-    av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
+    av_log(data, AV_LOG_WARNING, "%s\n", msg);
+}
+
+static void info_callback(const char *msg, void *data)
+{
+    av_log(data, AV_LOG_DEBUG, "%s\n", msg);
 }
 
 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
@@ -89,28 +95,40 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
     case PIX_FMT_RGBA64:
         color_space = CLRSPC_SRGB;
         break;
+    case PIX_FMT_YUV410P:
+    case PIX_FMT_YUV411P:
     case PIX_FMT_YUV420P:
     case PIX_FMT_YUV422P:
     case PIX_FMT_YUV440P:
     case PIX_FMT_YUV444P:
     case PIX_FMT_YUVA420P:
+    case PIX_FMT_YUVA422P:
+    case PIX_FMT_YUVA444P:
     case PIX_FMT_YUV420P9:
     case PIX_FMT_YUV422P9:
     case PIX_FMT_YUV444P9:
     case PIX_FMT_YUV420P10:
     case PIX_FMT_YUV422P10:
     case PIX_FMT_YUV444P10:
+    case PIX_FMT_YUV420P12:
+    case PIX_FMT_YUV422P12:
+    case PIX_FMT_YUV444P12:
+    case PIX_FMT_YUV420P14:
+    case PIX_FMT_YUV422P14:
+    case PIX_FMT_YUV444P14:
     case PIX_FMT_YUV420P16:
     case PIX_FMT_YUV422P16:
     case PIX_FMT_YUV444P16:
         color_space = CLRSPC_SYCC;
         break;
     default:
-        av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
+        av_log(avctx, AV_LOG_ERROR,
+               "The requested pixel format '%s' is not supported\n",
+               av_get_pix_fmt_name(avctx->pix_fmt));
         return NULL;
     }
 
-    cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
+    cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
     if (!cmptparm) {
         av_log(avctx, AV_LOG_ERROR, "Not enough memory");
         return NULL;
@@ -133,8 +151,10 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
 {
     LibOpenJPEGContext *ctx = avctx->priv_data;
+    int err = AVERROR(ENOMEM);
 
     opj_set_default_encoder_parameters(&ctx->enc_params);
+
     ctx->enc_params.cp_rsiz = ctx->profile;
     ctx->enc_params.mode = !!avctx->global_quality;
     ctx->enc_params.cp_cinema = ctx->cinema_mode;
@@ -154,26 +174,29 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
 
     avctx->coded_frame = avcodec_alloc_frame();
     if (!avctx->coded_frame) {
-        av_freep(&ctx->compress);
         av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
-        return AVERROR(ENOMEM);
+        goto fail;
     }
 
     ctx->image = mj2_create_image(avctx, &ctx->enc_params);
     if (!ctx->image) {
-        av_freep(&ctx->compress);
-        av_freep(&avctx->coded_frame);
         av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
-        return AVERROR(EINVAL);
+        err = AVERROR(EINVAL);
+        goto fail;
     }
 
     memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
+    ctx->event_mgr.info_handler    = info_callback;
     ctx->event_mgr.error_handler = error_callback;
     ctx->event_mgr.warning_handler = warning_callback;
-    ctx->event_mgr.info_handler = NULL;
     opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
 
     return 0;
+
+fail:
+    av_freep(&ctx->compress);
+    av_freep(&avctx->coded_frame);
+    return err;
 }
 
 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
@@ -261,9 +284,8 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *fram
         for (y = 0; y < height; ++y) {
             image_index = y * width;
             frame_index = y * frame->linesize[compno];
-            for (x = 0; x < width; ++x) {
+            for (x = 0; x < width; ++x)
                 image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
-            }
         }
     }
 
@@ -296,9 +318,8 @@ static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *fra
         for (y = 0; y < height; ++y) {
             image_index = y * width;
             frame_index = y * (frame->linesize[compno] / 2);
-            for (x = 0; x < width; ++x) {
+            for (x = 0; x < width; ++x)
                 image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
-            }
         }
     }
 
@@ -310,7 +331,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
 {
     LibOpenJPEGContext *ctx = avctx->priv_data;
     opj_cinfo_t *compress = ctx->compress;
-    opj_image_t *image = ctx->image;
+    opj_image_t *image    = ctx->image;
     opj_cio_t *stream;
     int cpyresult = 0;
     int ret, len;
@@ -319,7 +340,7 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     // x1, y1 is the width, height of the reference grid
     image->x0 = 0;
     image->y0 = 0;
-    image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
+    image->x1 = (avctx->width  - 1) * ctx->enc_params.subsampling_dx + 1;
     image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
 
     switch (avctx->pix_fmt) {
@@ -333,33 +354,46 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
         cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
         break;
     case PIX_FMT_GRAY8:
+    case PIX_FMT_YUV410P:
+    case PIX_FMT_YUV411P:
     case PIX_FMT_YUV420P:
     case PIX_FMT_YUV422P:
     case PIX_FMT_YUV440P:
     case PIX_FMT_YUV444P:
     case PIX_FMT_YUVA420P:
+    case PIX_FMT_YUVA422P:
+    case PIX_FMT_YUVA444P:
         cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
         break;
     case PIX_FMT_GRAY16:
     case PIX_FMT_YUV420P9:
-    case PIX_FMT_YUV420P10:
-    case PIX_FMT_YUV420P16:
     case PIX_FMT_YUV422P9:
-    case PIX_FMT_YUV422P10:
-    case PIX_FMT_YUV422P16:
     case PIX_FMT_YUV444P9:
     case PIX_FMT_YUV444P10:
+    case PIX_FMT_YUV422P10:
+    case PIX_FMT_YUV420P10:
+    case PIX_FMT_YUV420P12:
+    case PIX_FMT_YUV422P12:
+    case PIX_FMT_YUV444P12:
+    case PIX_FMT_YUV420P14:
+    case PIX_FMT_YUV422P14:
+    case PIX_FMT_YUV444P14:
     case PIX_FMT_YUV444P16:
+    case PIX_FMT_YUV422P16:
+    case PIX_FMT_YUV420P16:
         cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
         break;
     default:
-        av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
+        av_log(avctx, AV_LOG_ERROR,
+               "The frame's pixel format '%s' is not supported\n",
+               av_get_pix_fmt_name(avctx->pix_fmt));
         return AVERROR(EINVAL);
         break;
     }
 
     if (!cpyresult) {
-        av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
+        av_log(avctx, AV_LOG_ERROR,
+               "Could not copy the frame data to the internal image buffer\n");
         return -1;
     }
 
@@ -396,7 +430,7 @@ static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
     opj_destroy_compress(ctx->compress);
     opj_image_destroy(ctx->image);
     av_freep(&avctx->coded_frame);
-    return 0 ;
+    return 0;
 }
 
 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
@@ -420,7 +454,7 @@ static const AVOption options[] = {
     { "rpcl",          NULL,                0,                     AV_OPT_TYPE_CONST, { RPCL        }, 0,         0,           VE, "prog_order"  },
     { "pcrl",          NULL,                0,                     AV_OPT_TYPE_CONST, { PCRL        }, 0,         0,           VE, "prog_order"  },
     { "cprl",          NULL,                0,                     AV_OPT_TYPE_CONST, { CPRL        }, 0,         0,           VE, "prog_order"  },
-    { "numresolution", NULL,                OFFSET(numresolution), AV_OPT_TYPE_INT,   { 6           }, 1,         10,          VE                },
+    { "numresolution", NULL,                OFFSET(numresolution), AV_OPT_TYPE_INT,   { 6           }, 1,         INT_MAX,     VE                },
     { "numlayers",     NULL,                OFFSET(numlayers),     AV_OPT_TYPE_INT,   { 1           }, 1,         10,          VE                },
     { "disto_alloc",   NULL,                OFFSET(disto_alloc),   AV_OPT_TYPE_INT,   { 1           }, 0,         1,           VE                },
     { "fixed_alloc",   NULL,                OFFSET(fixed_alloc),   AV_OPT_TYPE_INT,   { 0           }, 0,         1,           VE                },
@@ -438,20 +472,25 @@ static const AVClass class = {
 AVCodec ff_libopenjpeg_encoder = {
     .name           = "libopenjpeg",
     .type           = AVMEDIA_TYPE_VIDEO,
-    .id             = CODEC_ID_JPEG2000,
+    .id             = AV_CODEC_ID_JPEG2000,
     .priv_data_size = sizeof(LibOpenJPEGContext),
     .init           = libopenjpeg_encode_init,
     .encode2        = libopenjpeg_encode_frame,
     .close          = libopenjpeg_encode_close,
     .capabilities   = 0,
-    .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
-                                           PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16,
-                                           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,
-                                           PIX_FMT_NONE},
-    .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
+    .pix_fmts       = (const enum PixelFormat[]) {
+        PIX_FMT_RGB24, PIX_FMT_RGBA, PIX_FMT_RGB48, PIX_FMT_RGBA64,
+        PIX_FMT_GRAY8, PIX_FMT_GRAY8A, PIX_FMT_GRAY16,
+        PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUVA420P,
+        PIX_FMT_YUV440P, PIX_FMT_YUV444P, PIX_FMT_YUVA422P,
+        PIX_FMT_YUV411P, PIX_FMT_YUV410P, PIX_FMT_YUVA444P,
+        PIX_FMT_YUV420P9, PIX_FMT_YUV422P9, PIX_FMT_YUV444P9,
+        PIX_FMT_YUV420P10, PIX_FMT_YUV422P10, PIX_FMT_YUV444P10,
+        PIX_FMT_YUV420P12, PIX_FMT_YUV422P12, PIX_FMT_YUV444P12,
+        PIX_FMT_YUV420P14, PIX_FMT_YUV422P14, PIX_FMT_YUV444P14,
+        PIX_FMT_YUV420P16, PIX_FMT_YUV422P16, PIX_FMT_YUV444P16,
+        PIX_FMT_NONE
+    },
+    .long_name      = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
     .priv_class     = &class,
 };