2 * JPEG 2000 encoding support via OpenJPEG
3 * Copyright (c) 2011 Michael Bradshaw <mbradshaw@sorensonmedia.com>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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
30 #include "libavutil/common.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/opt.h"
40 opj_cparameters_t enc_params;
41 opj_cinfo_t *compress;
42 opj_event_mgr_t event_mgr;
54 static void error_callback(const char *msg, void *data)
56 av_log(data, AV_LOG_ERROR, "%s\n", msg);
59 static void warning_callback(const char *msg, void *data)
61 av_log(data, AV_LOG_WARNING, "%s\n", msg);
64 static void info_callback(const char *msg, void *data)
66 av_log(data, AV_LOG_DEBUG, "%s\n", msg);
69 static opj_image_t *libopenjpeg_create_image(AVCodecContext *avctx,
70 opj_cparameters_t *parameters)
72 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
73 opj_image_cmptparm_t *cmptparm;
74 OPJ_COLOR_SPACE color_space;
79 int numcomps = desc->nb_components;
86 sub_dx[2] = 1 << desc->log2_chroma_w;
88 sub_dy[2] = 1 << desc->log2_chroma_h;
90 switch (avctx->pix_fmt) {
91 case AV_PIX_FMT_GRAY8:
92 case AV_PIX_FMT_GRAY16:
93 case AV_PIX_FMT_Y400A:
94 color_space = CLRSPC_GRAY;
96 case AV_PIX_FMT_RGB24:
98 case AV_PIX_FMT_RGB48:
99 color_space = CLRSPC_SRGB;
101 case AV_PIX_FMT_YUV410P:
102 case AV_PIX_FMT_YUV411P:
103 case AV_PIX_FMT_YUV420P:
104 case AV_PIX_FMT_YUV422P:
105 case AV_PIX_FMT_YUV440P:
106 case AV_PIX_FMT_YUV444P:
107 case AV_PIX_FMT_YUVA420P:
108 case AV_PIX_FMT_YUV420P9:
109 case AV_PIX_FMT_YUV422P9:
110 case AV_PIX_FMT_YUV444P9:
111 case AV_PIX_FMT_YUV420P10:
112 case AV_PIX_FMT_YUV422P10:
113 case AV_PIX_FMT_YUV444P10:
114 case AV_PIX_FMT_YUV420P16:
115 case AV_PIX_FMT_YUV422P16:
116 case AV_PIX_FMT_YUV444P16:
117 color_space = CLRSPC_SYCC;
120 av_log(avctx, AV_LOG_ERROR,
121 "The requested pixel format '%s' is not supported\n",
122 av_get_pix_fmt_name(avctx->pix_fmt));
126 cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
128 av_log(avctx, AV_LOG_ERROR, "Not enough memory");
132 for (i = 0; i < numcomps; i++) {
133 cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
134 cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
135 cmptparm[i].sgnd = 0;
136 cmptparm[i].dx = sub_dx[i];
137 cmptparm[i].dy = sub_dy[i];
138 cmptparm[i].w = avctx->width / sub_dx[i];
139 cmptparm[i].h = avctx->height / sub_dy[i];
142 img = opj_image_create(numcomps, cmptparm, color_space);
147 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
149 LibOpenJPEGContext *ctx = avctx->priv_data;
150 int err = AVERROR(ENOMEM);
152 opj_set_default_encoder_parameters(&ctx->enc_params);
154 ctx->enc_params.cp_rsiz = ctx->profile;
155 ctx->enc_params.mode = !!avctx->global_quality;
156 ctx->enc_params.cp_cinema = ctx->cinema_mode;
157 ctx->enc_params.prog_order = ctx->prog_order;
158 ctx->enc_params.numresolution = ctx->numresolution;
159 ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
160 ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
161 ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
162 ctx->enc_params.tcp_numlayers = ctx->numlayers;
163 ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
165 ctx->compress = opj_create_compress(ctx->format);
166 if (!ctx->compress) {
167 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
168 return AVERROR(ENOMEM);
171 avctx->coded_frame = av_frame_alloc();
172 if (!avctx->coded_frame) {
173 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
177 ctx->image = libopenjpeg_create_image(avctx, &ctx->enc_params);
179 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
180 err = AVERROR(EINVAL);
184 ctx->event_mgr.info_handler = info_callback;
185 ctx->event_mgr.error_handler = error_callback;
186 ctx->event_mgr.warning_handler = warning_callback;
187 opj_set_event_mgr((opj_common_ptr) ctx->compress, &ctx->event_mgr, avctx);
192 av_freep(&ctx->compress);
193 av_freep(&avctx->coded_frame);
197 static void libopenjpeg_copy_packed8(AVCodecContext *avctx,
198 const AVFrame *frame, opj_image_t *image)
202 int image_index, frame_index;
203 const int numcomps = image->numcomps;
205 for (compno = 0; compno < numcomps; ++compno)
206 for (y = 0; y < avctx->height; ++y) {
207 image_index = y * avctx->width;
208 frame_index = y * frame->linesize[0] + compno;
209 for (x = 0; x < avctx->width; ++x) {
210 image->comps[compno].data[image_index++] =
211 frame->data[0][frame_index];
212 frame_index += numcomps;
217 static void libopenjpeg_copy_packed16(AVCodecContext *avctx,
218 const AVFrame *frame, opj_image_t *image)
222 int image_index, frame_index;
223 const int numcomps = image->numcomps;
224 uint16_t *frame_ptr = (uint16_t *)frame->data[0];
226 for (compno = 0; compno < numcomps; ++compno)
227 for (y = 0; y < avctx->height; ++y) {
228 image_index = y * avctx->width;
229 frame_index = y * (frame->linesize[0] / 2) + compno;
230 for (x = 0; x < avctx->width; ++x) {
231 image->comps[compno].data[image_index++] =
232 frame_ptr[frame_index];
233 frame_index += numcomps;
238 static void libopenjpeg_copy_unpacked8(AVCodecContext *avctx,
239 const AVFrame *frame, opj_image_t *image)
244 int image_index, frame_index;
245 const int numcomps = image->numcomps;
247 for (compno = 0; compno < numcomps; ++compno) {
248 width = avctx->width / image->comps[compno].dx;
249 height = avctx->height / image->comps[compno].dy;
250 for (y = 0; y < height; ++y) {
251 image_index = y * width;
252 frame_index = y * frame->linesize[compno];
253 for (x = 0; x < width; ++x)
254 image->comps[compno].data[image_index++] =
255 frame->data[compno][frame_index++];
260 static void libopenjpeg_copy_unpacked16(AVCodecContext *avctx,
261 const AVFrame *frame,
267 int image_index, frame_index;
268 const int numcomps = image->numcomps;
271 for (compno = 0; compno < numcomps; ++compno) {
272 width = avctx->width / image->comps[compno].dx;
273 height = avctx->height / image->comps[compno].dy;
274 frame_ptr = (uint16_t *)frame->data[compno];
275 for (y = 0; y < height; ++y) {
276 image_index = y * width;
277 frame_index = y * (frame->linesize[compno] / 2);
278 for (x = 0; x < width; ++x)
279 image->comps[compno].data[image_index++] =
280 frame_ptr[frame_index++];
285 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
286 const AVFrame *frame, int *got_packet)
288 LibOpenJPEGContext *ctx = avctx->priv_data;
289 opj_cinfo_t *compress = ctx->compress;
290 opj_image_t *image = ctx->image;
294 // x0, y0 is the top left corner of the image
295 // x1, y1 is the width, height of the reference grid
298 image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
299 image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
301 switch (avctx->pix_fmt) {
302 case AV_PIX_FMT_RGB24:
303 case AV_PIX_FMT_RGBA:
304 case AV_PIX_FMT_Y400A:
305 libopenjpeg_copy_packed8(avctx, frame, image);
307 case AV_PIX_FMT_RGB48:
308 libopenjpeg_copy_packed16(avctx, frame, image);
310 case AV_PIX_FMT_GRAY8:
311 case AV_PIX_FMT_YUV410P:
312 case AV_PIX_FMT_YUV411P:
313 case AV_PIX_FMT_YUV420P:
314 case AV_PIX_FMT_YUV422P:
315 case AV_PIX_FMT_YUV440P:
316 case AV_PIX_FMT_YUV444P:
317 case AV_PIX_FMT_YUVA420P:
318 libopenjpeg_copy_unpacked8(avctx, frame, image);
320 case AV_PIX_FMT_GRAY16:
321 case AV_PIX_FMT_YUV420P9:
322 case AV_PIX_FMT_YUV422P9:
323 case AV_PIX_FMT_YUV444P9:
324 case AV_PIX_FMT_YUV444P10:
325 case AV_PIX_FMT_YUV422P10:
326 case AV_PIX_FMT_YUV420P10:
327 case AV_PIX_FMT_YUV444P16:
328 case AV_PIX_FMT_YUV422P16:
329 case AV_PIX_FMT_YUV420P16:
330 libopenjpeg_copy_unpacked16(avctx, frame, image);
333 av_log(avctx, AV_LOG_ERROR,
334 "The frame's pixel format '%s' is not supported\n",
335 av_get_pix_fmt_name(avctx->pix_fmt));
336 return AVERROR(EINVAL);
340 opj_setup_encoder(compress, &ctx->enc_params, image);
341 stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
343 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
344 return AVERROR(ENOMEM);
347 if (!opj_encode(compress, stream, image, NULL)) {
348 opj_cio_close(stream);
349 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
353 len = cio_tell(stream);
354 if ((ret = ff_alloc_packet(pkt, len)) < 0) {
355 opj_cio_close(stream);
359 memcpy(pkt->data, stream->buffer, len);
360 pkt->flags |= AV_PKT_FLAG_KEY;
362 opj_cio_close(stream);
366 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
368 LibOpenJPEGContext *ctx = avctx->priv_data;
370 opj_destroy_compress(ctx->compress);
371 opj_image_destroy(ctx->image);
372 av_freep(&avctx->coded_frame);
376 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
377 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
378 static const AVOption options[] = {
379 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
380 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
381 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
382 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
383 { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
384 { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
385 { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
386 { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
387 { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
388 { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
389 { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
390 { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
391 { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
392 { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
393 { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
394 { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
395 { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
396 { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
397 { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 10, VE },
398 { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
399 { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
400 { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
401 { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
405 static const AVClass class = {
406 .class_name = "libopenjpeg",
407 .item_name = av_default_item_name,
409 .version = LIBAVUTIL_VERSION_INT,
412 AVCodec ff_libopenjpeg_encoder = {
413 .name = "libopenjpeg",
414 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
415 .type = AVMEDIA_TYPE_VIDEO,
416 .id = AV_CODEC_ID_JPEG2000,
417 .priv_data_size = sizeof(LibOpenJPEGContext),
418 .init = libopenjpeg_encode_init,
419 .encode2 = libopenjpeg_encode_frame,
420 .close = libopenjpeg_encode_close,
422 .pix_fmts = (const enum AVPixelFormat[]) {
423 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
424 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_Y400A,
425 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
426 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
427 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
428 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
429 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
430 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
433 .priv_class = &class,