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"
37 opj_cparameters_t enc_params;
38 opj_cinfo_t *compress;
39 opj_event_mgr_t event_mgr;
42 static void error_callback(const char *msg, void *data)
44 av_log((AVCodecContext*)data, AV_LOG_ERROR, "libopenjpeg: %s\n", msg);
47 static void warning_callback(const char *msg, void *data)
49 av_log((AVCodecContext*)data, AV_LOG_WARNING, "libopenjpeg: %s\n", msg);
52 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
54 opj_image_cmptparm_t *cmptparm;
61 OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
63 sub_dx[0] = sub_dx[3] = 1;
64 sub_dy[0] = sub_dy[3] = 1;
65 sub_dx[1] = sub_dx[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_w;
66 sub_dy[1] = sub_dy[2] = 1<<av_pix_fmt_descriptors[avctx->pix_fmt].log2_chroma_h;
68 switch (avctx->pix_fmt) {
70 color_space = CLRSPC_GRAY;
74 color_space = CLRSPC_GRAY;
78 color_space = CLRSPC_GRAY;
83 color_space = CLRSPC_SRGB;
87 color_space = CLRSPC_SRGB;
91 color_space = CLRSPC_SRGB;
96 color_space = CLRSPC_SRGB;
100 case PIX_FMT_YUV420P:
101 color_space = CLRSPC_SYCC;
104 case PIX_FMT_YUV422P:
105 color_space = CLRSPC_SYCC;
108 case PIX_FMT_YUV440P:
109 color_space = CLRSPC_SYCC;
112 case PIX_FMT_YUV444P:
113 color_space = CLRSPC_SYCC;
116 case PIX_FMT_YUVA420P:
117 color_space = CLRSPC_SYCC;
120 case PIX_FMT_YUV420P9:
121 case PIX_FMT_YUV422P9:
122 case PIX_FMT_YUV444P9:
123 color_space = CLRSPC_SYCC;
127 case PIX_FMT_YUV420P10:
128 case PIX_FMT_YUV422P10:
129 case PIX_FMT_YUV444P10:
130 color_space = CLRSPC_SYCC;
134 case PIX_FMT_YUV420P16:
135 case PIX_FMT_YUV422P16:
136 case PIX_FMT_YUV444P16:
137 color_space = CLRSPC_SYCC;
142 av_log(avctx, AV_LOG_ERROR, "The requested pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
146 cmptparm = av_mallocz(numcomps * sizeof(opj_image_cmptparm_t));
148 av_log(avctx, AV_LOG_ERROR, "Not enough memory");
151 for (i = 0; i < numcomps; i++) {
152 cmptparm[i].prec = bpp;
153 cmptparm[i].bpp = bpp;
154 cmptparm[i].sgnd = 0;
155 cmptparm[i].dx = sub_dx[i];
156 cmptparm[i].dy = sub_dy[i];
157 cmptparm[i].w = avctx->width / sub_dx[i];
158 cmptparm[i].h = avctx->height / sub_dy[i];
161 img = opj_image_create(numcomps, cmptparm, color_space);
166 static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
168 LibOpenJPEGContext *ctx = avctx->priv_data;
170 opj_set_default_encoder_parameters(&ctx->enc_params);
171 ctx->enc_params.tcp_numlayers = 1;
172 ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
173 ctx->enc_params.cp_disto_alloc = 1;
175 ctx->compress = opj_create_compress(CODEC_J2K);
176 if (!ctx->compress) {
177 av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
178 return AVERROR(ENOMEM);
181 avctx->coded_frame = avcodec_alloc_frame();
182 if (!avctx->coded_frame) {
183 av_freep(&ctx->compress);
184 av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
185 return AVERROR(ENOMEM);
188 ctx->image = mj2_create_image(avctx, &ctx->enc_params);
190 av_freep(&ctx->compress);
191 av_freep(&avctx->coded_frame);
192 av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
193 return AVERROR(EINVAL);
196 memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
197 ctx->event_mgr.error_handler = error_callback;
198 ctx->event_mgr.warning_handler = warning_callback;
199 ctx->event_mgr.info_handler = NULL;
200 opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
205 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
212 const int numcomps = image->numcomps;
214 for (compno = 0; compno < numcomps; ++compno) {
215 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
216 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
221 for (compno = 0; compno < numcomps; ++compno) {
222 for (y = 0; y < avctx->height; ++y) {
223 image_index = y * avctx->width;
224 frame_index = y * frame->linesize[0] + compno;
225 for (x = 0; x < avctx->width; ++x) {
226 image->comps[compno].data[image_index++] = frame->data[0][frame_index];
227 frame_index += numcomps;
235 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
242 const int numcomps = image->numcomps;
243 uint16_t *frame_ptr = (uint16_t*)frame->data[0];
245 for (compno = 0; compno < numcomps; ++compno) {
246 if (image->comps[compno].w > frame->linesize[0] / numcomps) {
247 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
252 for (compno = 0; compno < numcomps; ++compno) {
253 for (y = 0; y < avctx->height; ++y) {
254 image_index = y * avctx->width;
255 frame_index = y * (frame->linesize[0] / 2) + compno;
256 for (x = 0; x < avctx->width; ++x) {
257 image->comps[compno].data[image_index++] = frame_ptr[frame_index];
258 frame_index += numcomps;
266 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
275 const int numcomps = image->numcomps;
277 for (compno = 0; compno < numcomps; ++compno) {
278 if (image->comps[compno].w > frame->linesize[compno]) {
279 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
284 for (compno = 0; compno < numcomps; ++compno) {
285 width = avctx->width / image->comps[compno].dx;
286 height = avctx->height / image->comps[compno].dy;
287 for (y = 0; y < height; ++y) {
288 image_index = y * width;
289 frame_index = y * frame->linesize[compno];
290 for (x = 0; x < width; ++x) {
291 image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
299 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
308 const int numcomps = image->numcomps;
311 for (compno = 0; compno < numcomps; ++compno) {
312 if (image->comps[compno].w > frame->linesize[compno]) {
313 av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
318 for (compno = 0; compno < numcomps; ++compno) {
319 width = avctx->width / image->comps[compno].dx;
320 height = avctx->height / image->comps[compno].dy;
321 frame_ptr = (uint16_t*)frame->data[compno];
322 for (y = 0; y < height; ++y) {
323 image_index = y * width;
324 frame_index = y * (frame->linesize[compno] / 2);
325 for (x = 0; x < width; ++x) {
326 image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
334 static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
335 const AVFrame *frame, int *got_packet)
337 LibOpenJPEGContext *ctx = avctx->priv_data;
338 opj_cinfo_t *compress = ctx->compress;
339 opj_image_t *image = ctx->image;
344 // x0, y0 is the top left corner of the image
345 // x1, y1 is the width, height of the reference grid
348 image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
349 image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
351 switch (avctx->pix_fmt) {
355 cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
359 cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
362 case PIX_FMT_YUV420P:
363 case PIX_FMT_YUV422P:
364 case PIX_FMT_YUV440P:
365 case PIX_FMT_YUV444P:
366 case PIX_FMT_YUVA420P:
367 cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
370 case PIX_FMT_YUV420P9:
371 case PIX_FMT_YUV420P10:
372 case PIX_FMT_YUV420P16:
373 case PIX_FMT_YUV422P9:
374 case PIX_FMT_YUV422P10:
375 case PIX_FMT_YUV422P16:
376 case PIX_FMT_YUV444P9:
377 case PIX_FMT_YUV444P10:
378 case PIX_FMT_YUV444P16:
379 cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
382 av_log(avctx, AV_LOG_ERROR, "The frame's pixel format '%s' is not supported\n", av_get_pix_fmt_name(avctx->pix_fmt));
383 return AVERROR(EINVAL);
388 av_log(avctx, AV_LOG_ERROR, "Could not copy the frame data to the internal image buffer\n");
392 opj_setup_encoder(compress, &ctx->enc_params, image);
393 stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
395 av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
396 return AVERROR(ENOMEM);
399 if (!opj_encode(compress, stream, image, NULL)) {
400 opj_cio_close(stream);
401 av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
405 len = cio_tell(stream);
406 if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
407 opj_cio_close(stream);
411 memcpy(pkt->data, stream->buffer, len);
412 pkt->flags |= AV_PKT_FLAG_KEY;
414 opj_cio_close(stream);
418 static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
420 LibOpenJPEGContext *ctx = avctx->priv_data;
422 opj_destroy_compress(ctx->compress);
423 opj_image_destroy(ctx->image);
424 av_freep(&avctx->coded_frame);
429 AVCodec ff_libopenjpeg_encoder = {
430 .name = "libopenjpeg",
431 .type = AVMEDIA_TYPE_VIDEO,
432 .id = CODEC_ID_JPEG2000,
433 .priv_data_size = sizeof(LibOpenJPEGContext),
434 .init = libopenjpeg_encode_init,
435 .encode2 = libopenjpeg_encode_frame,
436 .close = libopenjpeg_encode_close,
438 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_RGB24,PIX_FMT_RGBA,PIX_FMT_RGB48,PIX_FMT_RGBA64,
439 PIX_FMT_GRAY8,PIX_FMT_GRAY8A,PIX_FMT_GRAY16,
440 PIX_FMT_YUV420P,PIX_FMT_YUV422P,PIX_FMT_YUVA420P,
441 PIX_FMT_YUV440P,PIX_FMT_YUV444P,
442 PIX_FMT_YUV420P9,PIX_FMT_YUV422P9,PIX_FMT_YUV444P9,
443 PIX_FMT_YUV420P10,PIX_FMT_YUV422P10,PIX_FMT_YUV444P10,
444 PIX_FMT_YUV420P16,PIX_FMT_YUV422P16,PIX_FMT_YUV444P16,
446 .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),