Merge remote-tracking branch 'qatar/master'
[ffmpeg.git] / libavcodec / libopenjpegenc.c
index da12498..bd741df 100644 (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/intreadwrite.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)
@@ -96,23 +102,33 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
     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;
@@ -135,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;
@@ -156,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)
@@ -342,28 +363,39 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     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;
     }
 
@@ -448,15 +480,19 @@ AVCodec ff_libopenjpeg_encoder = {
     .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_YUV411P,PIX_FMT_YUV410P,
-                                           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,
 };