]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/libopenjpegenc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / libopenjpegenc.c
index 30cc022f9305c2876957e97526fc3422a7d4e58b..b357fcdde6d61b611e09fbc93de4ac6e5c648567 100644 (file)
 * JPEG 2000 encoder using libopenjpeg
 */
 
+#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;
     opj_image_t *image;
     opj_cparameters_t enc_params;
     opj_cinfo_t *compress;
     opj_event_mgr_t event_mgr;
+    int format;
+    int profile;
+    int cinema_mode;
+    int prog_order;
+    int numresolution;
+    int numlayers;
+    int disto_alloc;
+    int fixed_alloc;
+    int fixed_quality;
 } LibOpenJPEGContext;
 
 static void error_callback(const char *msg, void *data)
@@ -53,10 +65,9 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
     opj_image_cmptparm_t *cmptparm;
     opj_image_t *img;
     int i;
-    int bpp = 8;
     int sub_dx[4];
     int sub_dy[4];
-    int numcomps = 0;
+    int numcomps;
     OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
 
     sub_dx[0] = sub_dx[3] = 1;
@@ -64,78 +75,35 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
     sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
     sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
 
+    numcomps = av_pix_fmt_descriptors[avctx->pix_fmt].nb_components;
+
     switch (avctx->pix_fmt) {
     case PIX_FMT_GRAY8:
-        color_space = CLRSPC_GRAY;
-        numcomps = 1;
-        break;
     case PIX_FMT_GRAY8A:
-        color_space = CLRSPC_GRAY;
-        numcomps = 2;
-        break;
     case PIX_FMT_GRAY16:
         color_space = CLRSPC_GRAY;
-        numcomps = 1;
-        bpp = 16;
         break;
     case PIX_FMT_RGB24:
-        color_space = CLRSPC_SRGB;
-        numcomps = 3;
-        break;
     case PIX_FMT_RGBA:
-        color_space = CLRSPC_SRGB;
-        numcomps = 4;
-        break;
     case PIX_FMT_RGB48:
-        color_space = CLRSPC_SRGB;
-        numcomps = 3;
-        bpp = 16;
-        break;
     case PIX_FMT_RGBA64:
         color_space = CLRSPC_SRGB;
-        numcomps = 4;
-        bpp = 16;
         break;
     case PIX_FMT_YUV420P:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        break;
     case PIX_FMT_YUV422P:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        break;
     case PIX_FMT_YUV440P:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        break;
     case PIX_FMT_YUV444P:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        break;
     case PIX_FMT_YUVA420P:
-        color_space = CLRSPC_SYCC;
-        numcomps = 4;
-        break;
     case PIX_FMT_YUV420P9:
     case PIX_FMT_YUV422P9:
     case PIX_FMT_YUV444P9:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        bpp = 9;
-        break;
     case PIX_FMT_YUV420P10:
     case PIX_FMT_YUV422P10:
     case PIX_FMT_YUV444P10:
-        color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        bpp = 10;
-        break;
     case PIX_FMT_YUV420P16:
     case PIX_FMT_YUV422P16:
     case PIX_FMT_YUV444P16:
         color_space = CLRSPC_SYCC;
-        numcomps = 3;
-        bpp = 16;
         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));
@@ -148,8 +116,8 @@ static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *p
         return NULL;
     }
     for (i = 0; i < numcomps; i++) {
-        cmptparm[i].prec = bpp;
-        cmptparm[i].bpp = bpp;
+        cmptparm[i].prec = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
+        cmptparm[i].bpp = av_pix_fmt_descriptors[avctx->pix_fmt].comp[i].depth_minus1 + 1;
         cmptparm[i].sgnd = 0;
         cmptparm[i].dx = sub_dx[i];
         cmptparm[i].dy = sub_dy[i];
@@ -167,11 +135,18 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
     LibOpenJPEGContext *ctx = avctx->priv_data;
 
     opj_set_default_encoder_parameters(&ctx->enc_params);
-    ctx->enc_params.tcp_numlayers = 1;
+    ctx->enc_params.cp_rsiz = ctx->profile;
+    ctx->enc_params.mode = !!avctx->global_quality;
+    ctx->enc_params.cp_cinema = ctx->cinema_mode;
+    ctx->enc_params.prog_order = ctx->prog_order;
+    ctx->enc_params.numresolution = ctx->numresolution;
+    ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
+    ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
+    ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
+    ctx->enc_params.tcp_numlayers = ctx->numlayers;
     ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
-    ctx->enc_params.cp_disto_alloc = 1;
 
-    ctx->compress = opj_create_compress(CODEC_J2K);
+    ctx->compress = opj_create_compress(ctx->format);
     if (!ctx->compress) {
         av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
         return AVERROR(ENOMEM);
@@ -201,7 +176,7 @@ static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
     return 0;
 }
 
-static int libopenjpeg_copy_packed8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
+static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
 {
     int compno;
     int x;
@@ -231,7 +206,7 @@ static int libopenjpeg_copy_packed8(AVCodecContext *avctx, AVFrame *frame, opj_i
     return 1;
 }
 
-static int libopenjpeg_copy_packed16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
+static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
 {
     int compno;
     int x;
@@ -262,7 +237,7 @@ static int libopenjpeg_copy_packed16(AVCodecContext *avctx, AVFrame *frame, opj_
     return 1;
 }
 
-static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
+static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
 {
     int compno;
     int x;
@@ -295,7 +270,7 @@ static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, AVFrame *frame, opj
     return 1;
 }
 
-static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
+static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
 {
     int compno;
     int x;
@@ -330,15 +305,15 @@ static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, AVFrame *frame, op
     return 1;
 }
 
-static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
+static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
+                                    const AVFrame *frame, int *got_packet)
 {
-    AVFrame *frame = data;
     LibOpenJPEGContext *ctx = avctx->priv_data;
     opj_cinfo_t *compress = ctx->compress;
     opj_image_t *image = ctx->image;
     opj_cio_t *stream;
     int cpyresult = 0;
-    int len = 0;
+    int ret, len;
 
     // x0, y0 is the top left corner of the image
     // x1, y1 is the width, height of the reference grid
@@ -402,15 +377,16 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf
     }
 
     len = cio_tell(stream);
-    if (len > buf_size) {
+    if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
         opj_cio_close(stream);
-        av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
-        return -1;
+        return ret;
     }
 
-    memcpy(buf, stream->buffer, len);
+    memcpy(pkt->data, stream->buffer, len);
+    pkt->flags |= AV_PKT_FLAG_KEY;
+    *got_packet = 1;
     opj_cio_close(stream);
-    return len;
+    return 0;
 }
 
 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
@@ -423,6 +399,41 @@ static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
     return 0 ;
 }
 
+#define OFFSET(x) offsetof(LibOpenJPEGContext, x)
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    { "format",        "Codec Format",      OFFSET(format),        AV_OPT_TYPE_INT,   { CODEC_JP2   }, CODEC_J2K, CODEC_JP2,   VE, "format"      },
+    { "j2k",           NULL,                0,                     AV_OPT_TYPE_CONST, { CODEC_J2K   }, 0,         0,           VE, "format"      },
+    { "jp2",           NULL,                0,                     AV_OPT_TYPE_CONST, { CODEC_JP2   }, 0,         0,           VE, "format"      },
+    { "profile",       NULL,                OFFSET(profile),       AV_OPT_TYPE_INT,   { STD_RSIZ    }, STD_RSIZ,  CINEMA4K,    VE, "profile"     },
+    { "jpeg2000",      NULL,                0,                     AV_OPT_TYPE_CONST, { STD_RSIZ    }, 0,         0,           VE, "profile"     },
+    { "cinema2k",      NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K    }, 0,         0,           VE, "profile"     },
+    { "cinema4k",      NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA4K    }, 0,         0,           VE, "profile"     },
+    { "cinema_mode",   "Digital Cinema",    OFFSET(cinema_mode),   AV_OPT_TYPE_INT,   { OFF         }, OFF,       CINEMA4K_24, VE, "cinema_mode" },
+    { "off",           NULL,                0,                     AV_OPT_TYPE_CONST, { OFF         }, 0,         0,           VE, "cinema_mode" },
+    { "2k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K_24 }, 0,         0,           VE, "cinema_mode" },
+    { "2k_48",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA2K_48 }, 0,         0,           VE, "cinema_mode" },
+    { "4k_24",         NULL,                0,                     AV_OPT_TYPE_CONST, { CINEMA4K_24 }, 0,         0,           VE, "cinema_mode" },
+    { "prog_order",    "Progression Order", OFFSET(prog_order),    AV_OPT_TYPE_INT,   { LRCP        }, LRCP,      CPRL,        VE, "prog_order"  },
+    { "lrcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { LRCP        }, 0,         0,           VE, "prog_order"  },
+    { "rlcp",          NULL,                0,                     AV_OPT_TYPE_CONST, { RLCP        }, 0,         0,           VE, "prog_order"  },
+    { "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                },
+    { "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                },
+    { "fixed_quality", NULL,                OFFSET(fixed_quality), AV_OPT_TYPE_INT,   { 0           }, 0,         1,           VE                },
+    { NULL },
+};
+
+static const AVClass class = {
+    .class_name = "libopenjpeg",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
 
 AVCodec ff_libopenjpeg_encoder = {
     .name           = "libopenjpeg",
@@ -430,7 +441,7 @@ AVCodec ff_libopenjpeg_encoder = {
     .id             = CODEC_ID_JPEG2000,
     .priv_data_size = sizeof(LibOpenJPEGContext),
     .init           = libopenjpeg_encode_init,
-    .encode         = libopenjpeg_encode_frame,
+    .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,
@@ -441,5 +452,6 @@ AVCodec ff_libopenjpeg_encoder = {
                                            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 based JPEG 2000 encoder"),
-} ;
+    .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
+    .priv_class     = &class,
+};