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_2_1_OPENJPEG_H
38 # include <openjpeg-2.1/openjpeg.h>
39 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
40 # include <openjpeg-2.0/openjpeg.h>
41 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
42 # include <openjpeg-1.5/openjpeg.h>
44 # include <openjpeg.h>
47 #if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
48 # define OPENJPEG_MAJOR_VERSION 2
49 # define OPJ(x) OPJ_##x
51 # define OPENJPEG_MAJOR_VERSION 1
55 typedef struct LibOpenJPEGContext {
58 opj_cparameters_t enc_params;
59 #if OPENJPEG_MAJOR_VERSION == 1
60 opj_event_mgr_t event_mgr;
61 #endif // OPENJPEG_MAJOR_VERSION == 1
73 static void error_callback(const char *msg, void *data)
75 av_log(data, AV_LOG_ERROR, "%s\n", msg);
78 static void warning_callback(const char *msg, void *data)
80 av_log(data, AV_LOG_WARNING, "%s\n", msg);
83 static void info_callback(const char *msg, void *data)
85 av_log(data, AV_LOG_DEBUG, "%s\n", msg);
88 #if OPENJPEG_MAJOR_VERSION == 2
89 typedef struct PacketWriter {
94 static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
96 PacketWriter *writer = user_data;
97 AVPacket *packet = writer->packet;
98 int remaining = packet->size - writer->pos;
99 if (nb_bytes > remaining) {
100 OPJ_SIZE_T needed = nb_bytes - remaining;
101 int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
102 if (needed > max_growth) {
103 return (OPJ_SIZE_T)-1;
105 if (av_grow_packet(packet, (int)needed)) {
106 return (OPJ_SIZE_T)-1;
109 memcpy(packet->data + writer->pos, out_buffer, nb_bytes);
110 writer->pos += (int)nb_bytes;
114 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
116 PacketWriter *writer = user_data;
117 AVPacket *packet = writer->packet;
119 if (writer->pos == 0) {
120 return (OPJ_SIZE_T)-1;
122 if (nb_bytes + writer->pos < 0) {
123 nb_bytes = -writer->pos;
126 int remaining = packet->size - writer->pos;
127 if (nb_bytes > remaining) {
128 OPJ_SIZE_T needed = nb_bytes - remaining;
129 int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
130 if (needed > max_growth) {
131 return (OPJ_SIZE_T)-1;
133 if (av_grow_packet(packet, (int)needed)) {
134 return (OPJ_SIZE_T)-1;
138 writer->pos += (int)nb_bytes;
142 static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
144 PacketWriter *writer = user_data;
145 AVPacket *packet = writer->packet;
149 if (nb_bytes > packet->size) {
150 if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
151 av_grow_packet(packet, (int)nb_bytes - packet->size)) {
155 writer->pos = (int)nb_bytes;
158 #endif // OPENJPEG_MAJOR_VERSION == 2
160 static void cinema_parameters(opj_cparameters_t *p)
170 /* Tile and Image shall be at (0, 0) */
173 p->image_offset_x0 = 0;
174 p->image_offset_y0 = 0;
176 /* Codeblock size= 32 * 32 */
177 p->cblockw_init = 32;
178 p->cblockh_init = 32;
181 /* The progression order shall be CPRL */
182 p->prog_order = OPJ(CPRL);
188 p->subsampling_dx = 1;
189 p->subsampling_dy = 1;
197 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
199 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
200 opj_image_cmptparm_t cmptparm[4] = {{0}};
206 OPJ_COLOR_SPACE color_space = OPJ(CLRSPC_UNKNOWN);
208 sub_dx[0] = sub_dx[3] = 1;
209 sub_dy[0] = sub_dy[3] = 1;
210 sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
211 sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
213 numcomps = desc->nb_components;
215 switch (avctx->pix_fmt) {
216 case AV_PIX_FMT_GRAY8:
218 case AV_PIX_FMT_GRAY16:
219 case AV_PIX_FMT_YA16:
220 color_space = OPJ(CLRSPC_GRAY);
222 case AV_PIX_FMT_RGB24:
223 case AV_PIX_FMT_RGBA:
224 case AV_PIX_FMT_RGB48:
225 case AV_PIX_FMT_RGBA64:
226 case AV_PIX_FMT_GBR24P:
227 case AV_PIX_FMT_GBRP9:
228 case AV_PIX_FMT_GBRP10:
229 case AV_PIX_FMT_GBRP12:
230 case AV_PIX_FMT_GBRP14:
231 case AV_PIX_FMT_GBRP16:
232 case AV_PIX_FMT_XYZ12:
233 color_space = OPJ(CLRSPC_SRGB);
235 case AV_PIX_FMT_YUV410P:
236 case AV_PIX_FMT_YUV411P:
237 case AV_PIX_FMT_YUV420P:
238 case AV_PIX_FMT_YUV422P:
239 case AV_PIX_FMT_YUV440P:
240 case AV_PIX_FMT_YUV444P:
241 case AV_PIX_FMT_YUVA420P:
242 case AV_PIX_FMT_YUVA422P:
243 case AV_PIX_FMT_YUVA444P:
244 case AV_PIX_FMT_YUV420P9:
245 case AV_PIX_FMT_YUV422P9:
246 case AV_PIX_FMT_YUV444P9:
247 case AV_PIX_FMT_YUVA420P9:
248 case AV_PIX_FMT_YUVA422P9:
249 case AV_PIX_FMT_YUVA444P9:
250 case AV_PIX_FMT_YUV420P10:
251 case AV_PIX_FMT_YUV422P10:
252 case AV_PIX_FMT_YUV444P10:
253 case AV_PIX_FMT_YUVA420P10:
254 case AV_PIX_FMT_YUVA422P10:
255 case AV_PIX_FMT_YUVA444P10:
256 case AV_PIX_FMT_YUV420P12:
257 case AV_PIX_FMT_YUV422P12:
258 case AV_PIX_FMT_YUV444P12:
259 case AV_PIX_FMT_YUV420P14:
260 case AV_PIX_FMT_YUV422P14:
261 case AV_PIX_FMT_YUV444P14:
262 case AV_PIX_FMT_YUV420P16:
263 case AV_PIX_FMT_YUV422P16:
264 case AV_PIX_FMT_YUV444P16:
265 case AV_PIX_FMT_YUVA420P16:
266 case AV_PIX_FMT_YUVA422P16:
267 case AV_PIX_FMT_YUVA444P16:
268 color_space = OPJ(CLRSPC_SYCC);
271 av_log(avctx, AV_LOG_ERROR,
272 "The requested pixel format '%s' is not supported\n",
273 av_get_pix_fmt_name(avctx->pix_fmt));
277 for (i = 0; i < numcomps; i++) {
278 cmptparm[i].prec = desc->comp[i].depth;
279 cmptparm[i].bpp = desc->comp[i].depth;
280 cmptparm[i].sgnd = 0;
281 cmptparm[i].dx = sub_dx[i];
282 cmptparm[i].dy = sub_dy[i];
283 cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
284 cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
287 img = opj_image_create(numcomps, cmptparm, color_space);
292 // x0, y0 is the top left corner of the image
293 // x1, y1 is the width, height of the reference grid
296 img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
297 img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
302 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
304 LibOpenJPEGContext *ctx = avctx->priv_data;
307 opj_set_default_encoder_parameters(&ctx->enc_params);
309 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
310 switch (ctx->cinema_mode) {
311 case OPJ_CINEMA2K_24:
312 ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
313 ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
314 ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
316 case OPJ_CINEMA2K_48:
317 ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
318 ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
319 ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
321 case OPJ_CINEMA4K_24:
322 ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
323 ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
324 ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
328 switch (ctx->profile) {
330 if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
331 err = AVERROR(EINVAL);
334 ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
337 if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
338 err = AVERROR(EINVAL);
341 ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
346 av_log(avctx, AV_LOG_ERROR,
347 "Invalid parameter pairing: cinema_mode and profile conflict.\n");
351 ctx->enc_params.cp_rsiz = ctx->profile;
352 ctx->enc_params.cp_cinema = ctx->cinema_mode;
355 ctx->enc_params.mode = !!avctx->global_quality;
356 ctx->enc_params.prog_order = ctx->prog_order;
357 ctx->enc_params.numresolution = ctx->numresolution;
358 ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
359 ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
360 ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
361 ctx->enc_params.tcp_numlayers = ctx->numlayers;
362 ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
364 if (ctx->cinema_mode > 0) {
365 cinema_parameters(&ctx->enc_params);
368 ctx->image = mj2_create_image(avctx, &ctx->enc_params);
370 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
371 err = AVERROR(EINVAL);
378 opj_image_destroy(ctx->image);
383 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
390 const int numcomps = image->numcomps;
392 for (compno = 0; compno < numcomps; ++compno) {
393 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
394 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
399 for (compno = 0; compno < numcomps; ++compno) {
400 for (y = 0; y < avctx->height; ++y) {
401 image_line = image->comps[compno].data + y * image->comps[compno].w;
402 frame_index = y * frame->linesize[0] + compno;
403 for (x = 0; x < avctx->width; ++x) {
404 image_line[x] = frame->data[0][frame_index];
405 frame_index += numcomps;
407 for (; x < image->comps[compno].w; ++x) {
408 image_line[x] = image_line[x - 1];
411 for (; y < image->comps[compno].h; ++y) {
412 image_line = image->comps[compno].data + y * image->comps[compno].w;
413 for (x = 0; x < image->comps[compno].w; ++x) {
414 image_line[x] = image_line[x - image->comps[compno].w];
423 static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
429 const int numcomps = image->numcomps;
430 uint16_t *frame_ptr = (uint16_t *)frame->data[0];
432 for (compno = 0; compno < numcomps; ++compno) {
433 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
434 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
439 for (compno = 0; compno < numcomps; ++compno) {
440 for (y = 0; y < avctx->height; ++y) {
441 image_line = image->comps[compno].data + y * image->comps[compno].w;
442 frame_index = y * (frame->linesize[0] / 2) + compno;
443 for (x = 0; x < avctx->width; ++x) {
444 image_line[x] = frame_ptr[frame_index] >> 4;
445 frame_index += numcomps;
447 for (; x < image->comps[compno].w; ++x) {
448 image_line[x] = image_line[x - 1];
451 for (; y < image->comps[compno].h; ++y) {
452 image_line = image->comps[compno].data + y * image->comps[compno].w;
453 for (x = 0; x < image->comps[compno].w; ++x) {
454 image_line[x] = image_line[x - image->comps[compno].w];
462 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
469 const int numcomps = image->numcomps;
470 uint16_t *frame_ptr = (uint16_t*)frame->data[0];
472 for (compno = 0; compno < numcomps; ++compno) {
473 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
474 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
479 for (compno = 0; compno < numcomps; ++compno) {
480 for (y = 0; y < avctx->height; ++y) {
481 image_line = image->comps[compno].data + y * image->comps[compno].w;
482 frame_index = y * (frame->linesize[0] / 2) + compno;
483 for (x = 0; x < avctx->width; ++x) {
484 image_line[x] = frame_ptr[frame_index];
485 frame_index += numcomps;
487 for (; x < image->comps[compno].w; ++x) {
488 image_line[x] = image_line[x - 1];
491 for (; y < image->comps[compno].h; ++y) {
492 image_line = image->comps[compno].data + y * image->comps[compno].w;
493 for (x = 0; x < image->comps[compno].w; ++x) {
494 image_line[x] = image_line[x - image->comps[compno].w];
502 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
511 const int numcomps = image->numcomps;
513 for (compno = 0; compno < numcomps; ++compno) {
514 if (image->comps[compno].w > frame->linesize[compno]) {
515 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
520 for (compno = 0; compno < numcomps; ++compno) {
521 width = avctx->width / image->comps[compno].dx;
522 height = avctx->height / image->comps[compno].dy;
523 for (y = 0; y < height; ++y) {
524 image_line = image->comps[compno].data + y * image->comps[compno].w;
525 frame_index = y * frame->linesize[compno];
526 for (x = 0; x < width; ++x)
527 image_line[x] = frame->data[compno][frame_index++];
528 for (; x < image->comps[compno].w; ++x) {
529 image_line[x] = image_line[x - 1];
532 for (; y < image->comps[compno].h; ++y) {
533 image_line = image->comps[compno].data + y * image->comps[compno].w;
534 for (x = 0; x < image->comps[compno].w; ++x) {
535 image_line[x] = image_line[x - image->comps[compno].w];
543 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
552 const int numcomps = image->numcomps;
555 for (compno = 0; compno < numcomps; ++compno) {
556 if (image->comps[compno].w > frame->linesize[compno]) {
557 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
562 for (compno = 0; compno < numcomps; ++compno) {
563 width = avctx->width / image->comps[compno].dx;
564 height = avctx->height / image->comps[compno].dy;
565 frame_ptr = (uint16_t *)frame->data[compno];
566 for (y = 0; y < height; ++y) {
567 image_line = image->comps[compno].data + y * image->comps[compno].w;
568 frame_index = y * (frame->linesize[compno] / 2);
569 for (x = 0; x < width; ++x)
570 image_line[x] = frame_ptr[frame_index++];
571 for (; x < image->comps[compno].w; ++x) {
572 image_line[x] = image_line[x - 1];
575 for (; y < image->comps[compno].h; ++y) {
576 image_line = image->comps[compno].data + y * image->comps[compno].w;
577 for (x = 0; x < image->comps[compno].w; ++x) {
578 image_line[x] = image_line[x - image->comps[compno].w];
586 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
587 const AVFrame *frame, int *got_packet)
589 LibOpenJPEGContext *ctx = avctx->priv_data;
590 opj_image_t *image = ctx->image;
591 #if OPENJPEG_MAJOR_VERSION == 1
592 opj_cinfo_t *compress = NULL;
593 opj_cio_t *stream = NULL;
595 #else // OPENJPEG_MAJOR_VERSION == 2
596 opj_codec_t *compress = NULL;
597 opj_stream_t *stream = NULL;
598 #endif // OPENJPEG_MAJOR_VERSION == 1
603 switch (avctx->pix_fmt) {
604 case AV_PIX_FMT_RGB24:
605 case AV_PIX_FMT_RGBA:
607 cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
609 case AV_PIX_FMT_XYZ12:
610 cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
612 case AV_PIX_FMT_RGB48:
613 case AV_PIX_FMT_RGBA64:
614 case AV_PIX_FMT_YA16:
615 cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
617 case AV_PIX_FMT_GBR24P:
618 case AV_PIX_FMT_GBRP9:
619 case AV_PIX_FMT_GBRP10:
620 case AV_PIX_FMT_GBRP12:
621 case AV_PIX_FMT_GBRP14:
622 case AV_PIX_FMT_GBRP16:
623 gbrframe = av_frame_clone(frame);
625 return AVERROR(ENOMEM);
626 gbrframe->data[0] = frame->data[2]; // swap to be rgb
627 gbrframe->data[1] = frame->data[0];
628 gbrframe->data[2] = frame->data[1];
629 gbrframe->linesize[0] = frame->linesize[2];
630 gbrframe->linesize[1] = frame->linesize[0];
631 gbrframe->linesize[2] = frame->linesize[1];
632 if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
633 cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
635 cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
637 av_frame_free(&gbrframe);
639 case AV_PIX_FMT_GRAY8:
640 case AV_PIX_FMT_YUV410P:
641 case AV_PIX_FMT_YUV411P:
642 case AV_PIX_FMT_YUV420P:
643 case AV_PIX_FMT_YUV422P:
644 case AV_PIX_FMT_YUV440P:
645 case AV_PIX_FMT_YUV444P:
646 case AV_PIX_FMT_YUVA420P:
647 case AV_PIX_FMT_YUVA422P:
648 case AV_PIX_FMT_YUVA444P:
649 cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
651 case AV_PIX_FMT_GRAY16:
652 case AV_PIX_FMT_YUV420P9:
653 case AV_PIX_FMT_YUV422P9:
654 case AV_PIX_FMT_YUV444P9:
655 case AV_PIX_FMT_YUVA420P9:
656 case AV_PIX_FMT_YUVA422P9:
657 case AV_PIX_FMT_YUVA444P9:
658 case AV_PIX_FMT_YUV444P10:
659 case AV_PIX_FMT_YUV422P10:
660 case AV_PIX_FMT_YUV420P10:
661 case AV_PIX_FMT_YUVA444P10:
662 case AV_PIX_FMT_YUVA422P10:
663 case AV_PIX_FMT_YUVA420P10:
664 case AV_PIX_FMT_YUV420P12:
665 case AV_PIX_FMT_YUV422P12:
666 case AV_PIX_FMT_YUV444P12:
667 case AV_PIX_FMT_YUV420P14:
668 case AV_PIX_FMT_YUV422P14:
669 case AV_PIX_FMT_YUV444P14:
670 case AV_PIX_FMT_YUV444P16:
671 case AV_PIX_FMT_YUV422P16:
672 case AV_PIX_FMT_YUV420P16:
673 case AV_PIX_FMT_YUVA444P16:
674 case AV_PIX_FMT_YUVA422P16:
675 case AV_PIX_FMT_YUVA420P16:
676 cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
679 av_log(avctx, AV_LOG_ERROR,
680 "The frame's pixel format '%s' is not supported\n",
681 av_get_pix_fmt_name(avctx->pix_fmt));
682 return AVERROR(EINVAL);
687 av_log(avctx, AV_LOG_ERROR,
688 "Could not copy the frame data to the internal image buffer\n");
692 #if OPENJPEG_MAJOR_VERSION == 2
693 if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
696 #endif // OPENJPEG_MAJOR_VERSION == 2
698 compress = opj_create_compress(ctx->format);
700 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
701 ret = AVERROR(ENOMEM);
705 #if OPENJPEG_MAJOR_VERSION == 1
706 opj_setup_encoder(compress, &ctx->enc_params, image);
707 stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
708 #else // OPENJPEG_MAJOR_VERSION == 2
709 if (!opj_set_error_handler(compress, error_callback, avctx) ||
710 !opj_set_warning_handler(compress, warning_callback, avctx) ||
711 !opj_set_info_handler(compress, info_callback, avctx)) {
712 av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
713 ret = AVERROR_EXTERNAL;
717 if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
718 av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
719 ret = AVERROR_EXTERNAL;
722 stream = opj_stream_default_create(OPJ_STREAM_WRITE);
723 #endif // OPENJPEG_MAJOR_VERSION == 1
726 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
727 ret = AVERROR(ENOMEM);
730 #if OPENJPEG_MAJOR_VERSION == 1
731 memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
732 ctx->event_mgr.info_handler = info_callback;
733 ctx->event_mgr.error_handler = error_callback;
734 ctx->event_mgr.warning_handler = warning_callback;
735 opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
736 if (!opj_encode(compress, stream, image, NULL)) {
737 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
738 ret = AVERROR_EXTERNAL;
742 len = cio_tell(stream);
743 if ((ret = ff_alloc_packet2(avctx, pkt, len, 0)) < 0) {
747 memcpy(pkt->data, stream->buffer, len);
748 #else // OPENJPEG_MAJOR_VERSION == 2
749 PacketWriter writer = {0, pkt};
750 opj_stream_set_write_function(stream, stream_write);
751 opj_stream_set_skip_function(stream, stream_skip);
752 opj_stream_set_seek_function(stream, stream_seek);
753 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
754 opj_stream_set_user_data(stream, &writer, NULL);
755 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
756 opj_stream_set_user_data(stream, &writer);
758 #error Missing call to opj_stream_set_user_data
761 if (!opj_start_compress(compress, ctx->image, stream) ||
762 !opj_encode(compress, stream) ||
763 !opj_end_compress(compress, stream)) {
764 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
765 ret = AVERROR_EXTERNAL;
769 av_shrink_packet(pkt, writer.pos);
770 #endif // OPENJPEG_MAJOR_VERSION == 1
772 pkt->flags |= AV_PKT_FLAG_KEY;
777 #if OPENJPEG_MAJOR_VERSION == 2
778 opj_stream_destroy(stream);
779 opj_destroy_codec(compress);
781 opj_cio_close(stream);
782 opj_destroy_compress(compress);
787 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
789 LibOpenJPEGContext *ctx = avctx->priv_data;
791 opj_image_destroy(ctx->image);
796 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
797 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
798 static const AVOption options[] = {
799 { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = OPJ(CODEC_JP2) }, OPJ(CODEC_J2K), OPJ(CODEC_JP2), VE, "format" },
800 { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_J2K) }, 0, 0, VE, "format" },
801 { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_JP2) }, 0, 0, VE, "format" },
802 { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = OPJ(STD_RSIZ) }, OPJ(STD_RSIZ), OPJ(CINEMA4K), VE, "profile" },
803 { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(STD_RSIZ) }, 0, 0, VE, "profile" },
804 { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K) }, 0, 0, VE, "profile" },
805 { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K) }, 0, 0, VE, "profile" },
806 { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OPJ(OFF) }, OPJ(OFF), OPJ(CINEMA4K_24), VE, "cinema_mode" },
807 { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(OFF) }, 0, 0, VE, "cinema_mode" },
808 { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_24) }, 0, 0, VE, "cinema_mode" },
809 { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_48) }, 0, 0, VE, "cinema_mode" },
810 { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K_24) }, 0, 0, VE, "cinema_mode" },
811 { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = OPJ(LRCP) }, OPJ(LRCP), OPJ(CPRL), VE, "prog_order" },
812 { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(LRCP) }, 0, 0, VE, "prog_order" },
813 { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RLCP) }, 0, 0, VE, "prog_order" },
814 { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RPCL) }, 0, 0, VE, "prog_order" },
815 { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(PCRL) }, 0, 0, VE, "prog_order" },
816 { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CPRL) }, 0, 0, VE, "prog_order" },
817 { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
818 { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
819 { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
820 { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
821 { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
825 static const AVClass openjpeg_class = {
826 .class_name = "libopenjpeg",
827 .item_name = av_default_item_name,
829 .version = LIBAVUTIL_VERSION_INT,
832 AVCodec ff_libopenjpeg_encoder = {
833 .name = "libopenjpeg",
834 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
835 .type = AVMEDIA_TYPE_VIDEO,
836 .id = AV_CODEC_ID_JPEG2000,
837 .priv_data_size = sizeof(LibOpenJPEGContext),
838 .init = libopenjpeg_encode_init,
839 .encode2 = libopenjpeg_encode_frame,
840 .close = libopenjpeg_encode_close,
841 .capabilities = AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_INTRA_ONLY,
842 .pix_fmts = (const enum AVPixelFormat[]) {
843 AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, AV_PIX_FMT_RGB48,
844 AV_PIX_FMT_RGBA64, AV_PIX_FMT_GBR24P,
845 AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
846 AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16,
847 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P,
848 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA422P,
849 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUVA444P,
850 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
851 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
852 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
853 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
854 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
855 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
856 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
857 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
861 .priv_class = &openjpeg_class,