* JPEG 2000 encoder using libopenjpeg
*/
+#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/avassert.h"
#include "avcodec.h"
#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)
opj_image_cmptparm_t *cmptparm;
opj_image_t *img;
int i;
- int bpp = 8;
int sub_dx[4];
int sub_dy[4];
int numcomps;
switch (avctx->pix_fmt) {
case PIX_FMT_GRAY8:
- color_space = CLRSPC_GRAY;
- break;
case PIX_FMT_GRAY8A:
- color_space = CLRSPC_GRAY;
- break;
case PIX_FMT_GRAY16:
color_space = CLRSPC_GRAY;
- bpp = 16;
break;
case PIX_FMT_RGB24:
- color_space = CLRSPC_SRGB;
- break;
case PIX_FMT_RGBA:
- color_space = CLRSPC_SRGB;
- break;
case PIX_FMT_RGB48:
- color_space = CLRSPC_SRGB;
- bpp = 16;
- break;
case PIX_FMT_RGBA64:
color_space = CLRSPC_SRGB;
- bpp = 16;
break;
case PIX_FMT_YUV420P:
- color_space = CLRSPC_SYCC;
- break;
case PIX_FMT_YUV422P:
- color_space = CLRSPC_SYCC;
- break;
case PIX_FMT_YUV440P:
- color_space = CLRSPC_SYCC;
- break;
case PIX_FMT_YUV444P:
- color_space = CLRSPC_SYCC;
- break;
case PIX_FMT_YUVA420P:
- color_space = CLRSPC_SYCC;
- break;
case PIX_FMT_YUV420P9:
case PIX_FMT_YUV422P9:
case PIX_FMT_YUV444P9:
- color_space = CLRSPC_SYCC;
- bpp = 9;
- break;
case PIX_FMT_YUV420P10:
case PIX_FMT_YUV422P10:
case PIX_FMT_YUV444P10:
- color_space = CLRSPC_SYCC;
- bpp = 10;
- break;
case PIX_FMT_YUV420P16:
case PIX_FMT_YUV422P16:
case PIX_FMT_YUV444P16:
color_space = CLRSPC_SYCC;
- 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));
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];
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);
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",
PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
PIX_FMT_NONE},
.long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
-} ;
+ .priv_class = &class,
+};