2 * JPEG 2000 encoding support via OpenJPEG
3 * Copyright (c) 2011 Michael Bradshaw <mjbshaw gmail com>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * JPEG 2000 encoder using libopenjpeg
29 #include "libavutil/avassert.h"
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
37 #if HAVE_OPENJPEG_1_5_OPENJPEG_H
38 # include <openjpeg-1.5/openjpeg.h>
40 # include <openjpeg.h>
46 opj_cparameters_t enc_params;
47 opj_cinfo_t *compress;
48 opj_event_mgr_t event_mgr;
60 static void error_callback(const char *msg, void *data)
62 av_log(data, AV_LOG_ERROR, "%s\n", msg);
65 static void warning_callback(const char *msg, void *data)
67 av_log(data, AV_LOG_WARNING, "%s\n", msg);
70 static void info_callback(const char *msg, void *data)
72 av_log(data, AV_LOG_DEBUG, "%s\n", msg);
75 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
77 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
78 opj_image_cmptparm_t *cmptparm;
84 OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
86 sub_dx[0] = sub_dx[3] = 1;
87 sub_dy[0] = sub_dy[3] = 1;
88 sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
89 sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
91 numcomps = desc->nb_components;
93 switch (avctx->pix_fmt) {
94 case AV_PIX_FMT_GRAY8:
95 case AV_PIX_FMT_GRAY8A:
96 case AV_PIX_FMT_GRAY16:
97 color_space = CLRSPC_GRAY;
99 case AV_PIX_FMT_RGB24:
100 case AV_PIX_FMT_RGBA:
101 case AV_PIX_FMT_RGB48:
102 case AV_PIX_FMT_RGBA64:
103 color_space = CLRSPC_SRGB;
105 case AV_PIX_FMT_YUV410P:
106 case AV_PIX_FMT_YUV411P:
107 case AV_PIX_FMT_YUV420P:
108 case AV_PIX_FMT_YUV422P:
109 case AV_PIX_FMT_YUV440P:
110 case AV_PIX_FMT_YUV444P:
111 case AV_PIX_FMT_YUVA420P:
112 case AV_PIX_FMT_YUVA422P:
113 case AV_PIX_FMT_YUVA444P:
114 case AV_PIX_FMT_YUV420P9:
115 case AV_PIX_FMT_YUV422P9:
116 case AV_PIX_FMT_YUV444P9:
117 case AV_PIX_FMT_YUVA420P9:
118 case AV_PIX_FMT_YUVA422P9:
119 case AV_PIX_FMT_YUVA444P9:
120 case AV_PIX_FMT_YUV420P10:
121 case AV_PIX_FMT_YUV422P10:
122 case AV_PIX_FMT_YUV444P10:
123 case AV_PIX_FMT_YUVA420P10:
124 case AV_PIX_FMT_YUVA422P10:
125 case AV_PIX_FMT_YUVA444P10:
126 case AV_PIX_FMT_YUV420P12:
127 case AV_PIX_FMT_YUV422P12:
128 case AV_PIX_FMT_YUV444P12:
129 case AV_PIX_FMT_YUV420P14:
130 case AV_PIX_FMT_YUV422P14:
131 case AV_PIX_FMT_YUV444P14:
132 case AV_PIX_FMT_YUV420P16:
133 case AV_PIX_FMT_YUV422P16:
134 case AV_PIX_FMT_YUV444P16:
135 case AV_PIX_FMT_YUVA420P16:
136 case AV_PIX_FMT_YUVA422P16:
137 case AV_PIX_FMT_YUVA444P16:
138 color_space = CLRSPC_SYCC;
141 av_log(avctx, AV_LOG_ERROR,
142 "The requested pixel format '%s' is not supported\n",
143 av_get_pix_fmt_name(avctx->pix_fmt));
147 cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
149 av_log(avctx, AV_LOG_ERROR, "Not enough memory\n");
152 for (i = 0; i < numcomps; i++) {
153 cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
154 cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
155 cmptparm[i].sgnd = 0;
156 cmptparm[i].dx = sub_dx[i];
157 cmptparm[i].dy = sub_dy[i];
158 cmptparm[i].w = avctx->width / sub_dx[i];
159 cmptparm[i].h = avctx->height / sub_dy[i];
162 img = opj_image_create(numcomps, cmptparm, color_space);
167 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
169 LibOpenJPEGContext *ctx = avctx->priv_data;
170 int err = AVERROR(ENOMEM);
172 opj_set_default_encoder_parameters(&ctx->enc_params);
174 ctx->enc_params.cp_rsiz = ctx->profile;
175 ctx->enc_params.mode = !!avctx->global_quality;
176 ctx->enc_params.cp_cinema = ctx->cinema_mode;
177 ctx->enc_params.prog_order = ctx->prog_order;
178 ctx->enc_params.numresolution = ctx->numresolution;
179 ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
180 ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
181 ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
182 ctx->enc_params.tcp_numlayers = ctx->numlayers;
183 ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
185 ctx->compress = opj_create_compress(ctx->format);
186 if (!ctx->compress) {
187 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
188 return AVERROR(ENOMEM);
191 avctx->coded_frame = avcodec_alloc_frame();
192 if (!avctx->coded_frame) {
193 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
197 ctx->image = mj2_create_image(avctx, &ctx->enc_params);
199 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
200 err = AVERROR(EINVAL);
204 memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
205 ctx->event_mgr.info_handler = info_callback;
206 ctx->event_mgr.error_handler = error_callback;
207 ctx->event_mgr.warning_handler = warning_callback;
208 opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
213 av_freep(&ctx->compress);
214 av_freep(&avctx->coded_frame);
218 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
225 const int numcomps = image->numcomps;
227 for (compno = 0; compno < numcomps; ++compno) {
228 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
229 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
234 for (compno = 0; compno < numcomps; ++compno) {
235 for (y = 0; y < avctx->height; ++y) {
236 image_index = y * avctx->width;
237 frame_index = y * frame->linesize[0] + compno;
238 for (x = 0; x < avctx->width; ++x) {
239 image->comps[compno].data[image_index++] = frame->data[0][frame_index];
240 frame_index += numcomps;
248 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
255 const int numcomps = image->numcomps;
256 uint16_t *frame_ptr = (uint16_t*)frame->data[0];
258 for (compno = 0; compno < numcomps; ++compno) {
259 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
260 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
265 for (compno = 0; compno < numcomps; ++compno) {
266 for (y = 0; y < avctx->height; ++y) {
267 image_index = y * avctx->width;
268 frame_index = y * (frame->linesize[0] / 2) + compno;
269 for (x = 0; x < avctx->width; ++x) {
270 image->comps[compno].data[image_index++] = frame_ptr[frame_index];
271 frame_index += numcomps;
279 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
288 const int numcomps = image->numcomps;
290 for (compno = 0; compno < numcomps; ++compno) {
291 if (image->comps[compno].w > frame->linesize[compno]) {
292 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
297 for (compno = 0; compno < numcomps; ++compno) {
298 width = avctx->width / image->comps[compno].dx;
299 height = avctx->height / image->comps[compno].dy;
300 for (y = 0; y < height; ++y) {
301 image_index = y * width;
302 frame_index = y * frame->linesize[compno];
303 for (x = 0; x < width; ++x)
304 image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
311 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
320 const int numcomps = image->numcomps;
323 for (compno = 0; compno < numcomps; ++compno) {
324 if (image->comps[compno].w > frame->linesize[compno]) {
325 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
330 for (compno = 0; compno < numcomps; ++compno) {
331 width = avctx->width / image->comps[compno].dx;
332 height = avctx->height / image->comps[compno].dy;
333 frame_ptr = (uint16_t*)frame->data[compno];
334 for (y = 0; y < height; ++y) {
335 image_index = y * width;
336 frame_index = y * (frame->linesize[compno] / 2);
337 for (x = 0; x < width; ++x)
338 image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
345 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
346 const AVFrame *frame, int *got_packet)
348 LibOpenJPEGContext *ctx = avctx->priv_data;
349 opj_cinfo_t *compress = ctx->compress;
350 opj_image_t *image = ctx->image;
355 // x0, y0 is the top left corner of the image
356 // x1, y1 is the width, height of the reference grid
359 image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
360 image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
362 switch (avctx->pix_fmt) {
363 case AV_PIX_FMT_RGB24:
364 case AV_PIX_FMT_RGBA:
365 case AV_PIX_FMT_GRAY8A:
366 cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
368 case AV_PIX_FMT_RGB48:
369 case AV_PIX_FMT_RGBA64:
370 cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
372 case AV_PIX_FMT_GRAY8:
373 case AV_PIX_FMT_YUV410P:
374 case AV_PIX_FMT_YUV411P:
375 case AV_PIX_FMT_YUV420P:
376 case AV_PIX_FMT_YUV422P:
377 case AV_PIX_FMT_YUV440P:
378 case AV_PIX_FMT_YUV444P:
379 case AV_PIX_FMT_YUVA420P:
380 case AV_PIX_FMT_YUVA422P:
381 case AV_PIX_FMT_YUVA444P:
382 cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
384 case AV_PIX_FMT_GRAY16:
385 case AV_PIX_FMT_YUV420P9:
386 case AV_PIX_FMT_YUV422P9:
387 case AV_PIX_FMT_YUV444P9:
388 case AV_PIX_FMT_YUVA420P9:
389 case AV_PIX_FMT_YUVA422P9:
390 case AV_PIX_FMT_YUVA444P9:
391 case AV_PIX_FMT_YUV444P10:
392 case AV_PIX_FMT_YUV422P10:
393 case AV_PIX_FMT_YUV420P10:
394 case AV_PIX_FMT_YUVA444P10:
395 case AV_PIX_FMT_YUVA422P10:
396 case AV_PIX_FMT_YUVA420P10:
397 case AV_PIX_FMT_YUV420P12:
398 case AV_PIX_FMT_YUV422P12:
399 case AV_PIX_FMT_YUV444P12:
400 case AV_PIX_FMT_YUV420P14:
401 case AV_PIX_FMT_YUV422P14:
402 case AV_PIX_FMT_YUV444P14:
403 case AV_PIX_FMT_YUV444P16:
404 case AV_PIX_FMT_YUV422P16:
405 case AV_PIX_FMT_YUV420P16:
406 case AV_PIX_FMT_YUVA444P16:
407 case AV_PIX_FMT_YUVA422P16:
408 case AV_PIX_FMT_YUVA420P16:
409 cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
412 av_log(avctx, AV_LOG_ERROR,
413 "The frame's pixel format '%s' is not supported\n",
414 av_get_pix_fmt_name(avctx->pix_fmt));
415 return AVERROR(EINVAL);
420 av_log(avctx, AV_LOG_ERROR,
421 "Could not copy the frame data to the internal image buffer\n");
425 opj_setup_encoder(compress, &ctx->enc_params, image);
426 stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
428 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
429 return AVERROR(ENOMEM);
432 if (!opj_encode(compress, stream, image, NULL)) {
433 opj_cio_close(stream);
434 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
438 len = cio_tell(stream);
439 if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
440 opj_cio_close(stream);
444 memcpy(pkt->data, stream->buffer, len);
445 pkt->flags |= AV_PKT_FLAG_KEY;
447 opj_cio_close(stream);
451 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
453 LibOpenJPEGContext *ctx = avctx->priv_data;
455 opj_destroy_compress(ctx->compress);
456 opj_image_destroy(ctx->image);
457 av_freep(&avctx->coded_frame);
461 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
462 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
463 static const AVOption options[] = {
464 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
465 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
466 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
467 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
468 { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
469 { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
470 { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
471 { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
472 { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
473 { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
474 { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
475 { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
476 { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
477 { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
478 { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
479 { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
480 { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
481 { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
482 { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
483 { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
484 { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
485 { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
486 { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
490 static const AVClass class = {
491 .class_name = "libopenjpeg",
492 .item_name = av_default_item_name,
494 .version = LIBAVUTIL_VERSION_INT,
497 AVCodec ff_libopenjpeg_encoder = {
498 .name = "libopenjpeg",
499 .type = AVMEDIA_TYPE_VIDEO,
500 .id = AV_CODEC_ID_JPEG2000,
501 .priv_data_size = sizeof(LibOpenJPEGContext),
502 .init = libopenjpeg_encode_init,
503 .encode2 = libopenjpeg_encode_frame,
504 .close = libopenjpeg_encode_close,
506 .pix_fmts = (const enum AVPixelFormat[]) {
507 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64,
508 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY8A, AV_PIX_FMT_GRAY16,
509 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
510 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
511 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
512 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
513 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
514 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
515 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
516 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
517 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
518 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
519 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
522 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
523 .priv_class = &class,