2 * JPEG 2000 encoding support via OpenJPEG
3 * Copyright (c) 2011 Michael Bradshaw <mbradshaw@sorensonmedia.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
27 #include "libavutil/imgutils.h"
29 #include "libavutil/intreadwrite.h"
35 opj_cparameters_t enc_params;
36 opj_cinfo_t *compress;
37 opj_event_mgr_t event_mgr;
40 static void error_callback(const char *msg, void *data)
42 av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
45 static void warning_callback(const char *msg, void *data)
47 av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
50 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
52 opj_image_cmptparm_t *cmptparm;
59 OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
61 switch (avctx->pix_fmt) {
63 color_space = CLRSPC_GRAY;
70 color_space = CLRSPC_SRGB;
73 sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
74 sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
77 color_space = CLRSPC_SRGB;
80 sub_dx[0] = sub_dx[1] = sub_dx[2] = sub_dx[3] = 1;
81 sub_dy[0] = sub_dy[1] = sub_dy[2] = sub_dy[3] = 1;
84 color_space = CLRSPC_SYCC;
88 sub_dx[1] = sub_dx[2] = 2;
90 sub_dy[1] = sub_dy[2] = 2;
93 color_space = CLRSPC_SYCC;
97 sub_dx[1] = sub_dx[2] = 2;
98 sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
100 case PIX_FMT_YUV440P:
101 color_space = CLRSPC_SYCC;
104 sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
106 sub_dy[1] = sub_dy[2] = 2;
108 case PIX_FMT_YUV444P:
109 color_space = CLRSPC_SYCC;
112 sub_dx[0] = sub_dx[1] = sub_dx[2] = 1;
113 sub_dy[0] = sub_dy[1] = sub_dy[2] = 1;
116 av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
120 cmptparm = av_malloc(numcomps * sizeof(opj_image_cmptparm_t));
122 av_log(avctx, AV_LOG_ERROR, "Not enough memory");
125 memset(cmptparm, 0, numcomps * sizeof(opj_image_cmptparm_t));
126 for (i = 0; i < numcomps; i++) {
127 cmptparm[i].prec = 8;
128 cmptparm[i].bpp = bpp;
129 cmptparm[i].sgnd = 0;
130 cmptparm[i].dx = sub_dx[i];
131 cmptparm[i].dy = sub_dy[i];
132 cmptparm[i].w = avctx->width / sub_dx[i];
133 cmptparm[i].h = avctx->height / sub_dy[i];
136 img = opj_image_create(numcomps, cmptparm, color_space);
141 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
143 LibOpenJPEGContext *ctx = avctx->priv_data;
145 opj_set_default_encoder_parameters(&ctx->enc_params);
146 ctx->enc_params.tcp_numlayers = 1;
147 ctx->enc_params.tcp_rates[0] = avctx->compression_level > 0 ? avctx->compression_level : 0;
148 ctx->enc_params.cp_disto_alloc = 1;
150 ctx->compress = opj_create_compress(CODEC_J2K);
151 if (!ctx->compress) {
152 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
153 return AVERROR(ENOMEM);
156 avctx->coded_frame = avcodec_alloc_frame();
157 if (!avctx->coded_frame) {
158 av_freep(&ctx->compress);
159 ctx->compress = NULL;
160 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
161 return AVERROR(ENOMEM);
164 ctx->image = mj2_create_image(avctx, &ctx->enc_params);
166 av_freep(&ctx->compress);
167 ctx->compress = NULL;
168 av_freep(&avctx->coded_frame);
169 avctx->coded_frame = NULL;
170 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
171 return AVERROR(EINVAL);
174 memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
175 ctx->event_mgr.error_handler = error_callback;
176 ctx->event_mgr.warning_handler = warning_callback;
177 ctx->event_mgr.info_handler = NULL;
178 opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
183 static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
189 if (numcomps != 1 && numcomps != 3 && numcomps != 4) {
193 for (compno = 0; compno < numcomps; ++compno) {
194 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
199 for (compno = 0; compno < numcomps; ++compno) {
200 for (y = 0; y < avctx->height; ++y) {
201 for (x = 0; x < avctx->width; ++x) {
202 image->comps[compno].data[y * avctx->width + x] = frame->data[0][y * frame->linesize[0] + x * numcomps + compno];
210 static int libopenjpeg_copy_yuv(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
217 const int numcomps = 3;
219 for (compno = 0; compno < numcomps; ++compno) {
220 if (image->comps[compno].w > frame->linesize[compno]) {
225 for (compno = 0; compno < numcomps; ++compno) {
226 width = avctx->width / image->comps[compno].dx;
227 height = avctx->height / image->comps[compno].dy;
228 for (y = 0; y < height; ++y) {
229 for (x = 0; x < width; ++x) {
230 image->comps[compno].data[y * width + x] = frame->data[compno][y * frame->linesize[compno] + x];
238 static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
240 AVFrame *frame = data;
241 LibOpenJPEGContext *ctx = avctx->priv_data;
242 opj_cinfo_t *compress = ctx->compress;
243 opj_image_t *image = ctx->image;
248 // x0, y0 is the top left corner of the image
249 // x1, y1 is the width, height of the reference grid
252 image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
253 image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
255 switch (avctx->pix_fmt) {
257 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
260 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
263 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
265 case PIX_FMT_YUV420P:
266 case PIX_FMT_YUV422P:
267 case PIX_FMT_YUV440P:
268 case PIX_FMT_YUV444P:
269 cpyresult = libopenjpeg_copy_yuv(avctx, frame, image);
272 av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
273 return AVERROR(EINVAL);
278 av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
282 opj_setup_encoder(compress, &ctx->enc_params, image);
283 stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
285 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
286 return AVERROR(ENOMEM);
289 if (!opj_encode(compress, stream, image, NULL)) {
290 opj_cio_close(stream);
291 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
295 len = cio_tell(stream);
296 if (len > buf_size) {
297 opj_cio_close(stream);
298 av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
302 memcpy(buf, stream->buffer, len);
303 opj_cio_close(stream);
307 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
309 LibOpenJPEGContext *ctx = avctx->priv_data;
311 opj_destroy_compress(ctx->compress);
312 opj_image_destroy(ctx->image);
313 av_freep(&avctx->coded_frame);
318 AVCodec ff_libopenjpeg_encoder = {
319 .name = "libopenjpeg",
320 .type = AVMEDIA_TYPE_VIDEO,
321 .id = CODEC_ID_JPEG2000,
322 .priv_data_size = sizeof(LibOpenJPEGContext),
323 .init = libopenjpeg_encode_init,
324 .encode = libopenjpeg_encode_frame,
325 .close = libopenjpeg_encode_close,
328 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_GRAY8,PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUV440P,PIX_FMT_YUV444P},
329 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder/decoder"),