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"
28 #include "libavutil/avassert.h"
30 #include "libavutil/intreadwrite.h"
36 opj_cparameters_t enc_params;
37 opj_cinfo_t *compress;
38 opj_event_mgr_t event_mgr;
41 static void error_callback(const char *msg, void *data)
43 av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
46 static void warning_callback(const char *msg, void *data)
48 av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
51 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
53 opj_image_cmptparm_t *cmptparm;
60 OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
62 sub_dx[0] = sub_dx[3] = 1;
63 sub_dy[0] = sub_dy[3] = 1;
64 sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
65 sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
67 switch (avctx->pix_fmt) {
69 color_space = CLRSPC_GRAY;
73 color_space = CLRSPC_SRGB;
77 color_space = CLRSPC_SRGB;
81 color_space = CLRSPC_SYCC;
85 color_space = CLRSPC_SYCC;
89 color_space = CLRSPC_SYCC;
93 color_space = CLRSPC_SYCC;
96 case PIX_FMT_YUVA420P:
97 color_space = CLRSPC_SYCC;
100 case PIX_FMT_YUV420P9:
101 case PIX_FMT_YUV422P9:
102 case PIX_FMT_YUV444P9:
103 color_space = CLRSPC_SYCC;
107 case PIX_FMT_YUV420P10:
108 case PIX_FMT_YUV422P10:
109 case PIX_FMT_YUV444P10:
110 color_space = CLRSPC_SYCC;
114 case PIX_FMT_YUV420P16:
115 case PIX_FMT_YUV422P16:
116 case PIX_FMT_YUV444P16:
117 color_space = CLRSPC_SYCC;
122 av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
126 cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
128 av_log(avctx, AV_LOG_ERROR, "Not enough memory");
131 for (i = 0; i < numcomps; i++) {
132 cmptparm[i].prec = bpp;
133 cmptparm[i].bpp = bpp;
134 cmptparm[i].sgnd = 0;
135 cmptparm[i].dx = sub_dx[i];
136 cmptparm[i].dy = sub_dy[i];
137 cmptparm[i].w = avctx->width / sub_dx[i];
138 cmptparm[i].h = avctx->height / sub_dy[i];
141 img = opj_image_create(numcomps, cmptparm, color_space);
146 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
148 LibOpenJPEGContext *ctx = avctx->priv_data;
150 opj_set_default_encoder_parameters(&ctx->enc_params);
151 ctx->enc_params.tcp_numlayers = 1;
152 ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
153 ctx->enc_params.cp_disto_alloc = 1;
155 ctx->compress = opj_create_compress(CODEC_J2K);
156 if (!ctx->compress) {
157 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
158 return AVERROR(ENOMEM);
161 avctx->coded_frame = avcodec_alloc_frame();
162 if (!avctx->coded_frame) {
163 av_freep(&ctx->compress);
164 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
165 return AVERROR(ENOMEM);
168 ctx->image = mj2_create_image(avctx, &ctx->enc_params);
170 av_freep(&ctx->compress);
171 av_freep(&avctx->coded_frame);
172 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
173 return AVERROR(EINVAL);
176 memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
177 ctx->event_mgr.error_handler = error_callback;
178 ctx->event_mgr.warning_handler = warning_callback;
179 ctx->event_mgr.info_handler = NULL;
180 opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
185 static int libopenjpeg_copy_rgba(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image, int numcomps)
191 av_assert0(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_yuv8(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
217 const int numcomps = avctx->pix_fmt == PIX_FMT_YUVA420P ? 4 : 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_copy_yuv16(AVCodecContext *avctx, AVFrame *frame, opj_image_t *image)
245 const int numcomps = 3;
248 for (compno = 0; compno < numcomps; ++compno) {
249 if (image->comps[compno].w > frame->linesize[compno]) {
254 for (compno = 0; compno < numcomps; ++compno) {
255 width = avctx->width / image->comps[compno].dx;
256 height = avctx->height / image->comps[compno].dy;
257 frame_ptr = (uint16_t*)frame->data[compno];
258 for (y = 0; y < height; ++y) {
259 for (x = 0; x < width; ++x) {
260 image->comps[compno].data[y * width + x] = frame_ptr[y * (frame->linesize[compno] / 2) + x];
268 static int libopenjpeg_encode_frame(AVCodecContext *avctx, uint8_t *buf, int buf_size, void *data)
270 AVFrame *frame = data;
271 LibOpenJPEGContext *ctx = avctx->priv_data;
272 opj_cinfo_t *compress = ctx->compress;
273 opj_image_t *image = ctx->image;
278 // x0, y0 is the top left corner of the image
279 // x1, y1 is the width, height of the reference grid
282 image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
283 image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
285 switch (avctx->pix_fmt) {
287 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 1);
290 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 3);
293 cpyresult = libopenjpeg_copy_rgba(avctx, frame, image, 4);
295 case PIX_FMT_YUV420P:
296 case PIX_FMT_YUV422P:
297 case PIX_FMT_YUV440P:
298 case PIX_FMT_YUV444P:
299 case PIX_FMT_YUVA420P:
300 cpyresult = libopenjpeg_copy_yuv8(avctx, frame, image);
302 case PIX_FMT_YUV420P9:
303 case PIX_FMT_YUV420P10:
304 case PIX_FMT_YUV420P16:
305 case PIX_FMT_YUV422P9:
306 case PIX_FMT_YUV422P10:
307 case PIX_FMT_YUV422P16:
308 case PIX_FMT_YUV444P9:
309 case PIX_FMT_YUV444P10:
310 case PIX_FMT_YUV444P16:
311 cpyresult = libopenjpeg_copy_yuv16(avctx, frame, image);
314 av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
315 return AVERROR(EINVAL);
320 av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
324 opj_setup_encoder(compress, &ctx->enc_params, image);
325 stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
327 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
328 return AVERROR(ENOMEM);
331 if (!opj_encode(compress, stream, image, NULL)) {
332 opj_cio_close(stream);
333 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
337 len = cio_tell(stream);
338 if (len > buf_size) {
339 opj_cio_close(stream);
340 av_log(avctx, AV_LOG_ERROR, "Error with buf_size, not large enough to hold the frame\n");
344 memcpy(buf, stream->buffer, len);
345 opj_cio_close(stream);
349 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
351 LibOpenJPEGContext *ctx = avctx->priv_data;
353 opj_destroy_compress(ctx->compress);
354 opj_image_destroy(ctx->image);
355 av_freep(&avctx->coded_frame);
360 AVCodec ff_libopenjpeg_encoder = {
361 .name = "libopenjpeg",
362 .type = AVMEDIA_TYPE_VIDEO,
363 .id = CODEC_ID_JPEG2000,
364 .priv_data_size = sizeof(LibOpenJPEGContext),
365 .init = libopenjpeg_encode_init,
366 .encode = libopenjpeg_encode_frame,
367 .close = libopenjpeg_encode_close,
370 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_GRAY8,
371 PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
372 PIX_FMT_YUV440P,PIX_FMT_YUV444P,
373 PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
374 PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
375 PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16},
376 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 encoder"),