FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libopenjpegenc.c
Go to the documentation of this file.
1 /*
2  * JPEG 2000 encoding support via OpenJPEG
3  * Copyright (c) 2011 Michael Bradshaw <mjbshaw gmail com>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * JPEG 2000 encoder using libopenjpeg
25  */
26 
27 #define OPJ_STATIC
28 
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"
34 #include "avcodec.h"
35 #include "internal.h"
36 
37 #if HAVE_OPENJPEG_1_5_OPENJPEG_H
38 # include <openjpeg-1.5/openjpeg.h>
39 #else
40 # include <openjpeg.h>
41 #endif
42 
43 typedef struct {
45  opj_image_t *image;
46  opj_cparameters_t enc_params;
47  opj_cinfo_t *compress;
48  opj_event_mgr_t event_mgr;
49  int format;
50  int profile;
54  int numlayers;
59 
60 static void error_callback(const char *msg, void *data)
61 {
62  av_log(data, AV_LOG_ERROR, "%s\n", msg);
63 }
64 
65 static void warning_callback(const char *msg, void *data)
66 {
67  av_log(data, AV_LOG_WARNING, "%s\n", msg);
68 }
69 
70 static void info_callback(const char *msg, void *data)
71 {
72  av_log(data, AV_LOG_DEBUG, "%s\n", msg);
73 }
74 
75 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
76 {
77  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
78  opj_image_cmptparm_t *cmptparm;
79  opj_image_t *img;
80  int i;
81  int sub_dx[4];
82  int sub_dy[4];
83  int numcomps;
84  OPJ_COLOR_SPACE color_space = CLRSPC_UNKNOWN;
85 
86  sub_dx[0] = sub_dx[3] = 1;
87  sub_dy[0] = sub_dy[3] = 1;
88  sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
89  sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
90 
91  numcomps = desc->nb_components;
92 
93  switch (avctx->pix_fmt) {
94  case AV_PIX_FMT_GRAY8:
95  case AV_PIX_FMT_GRAY8A:
96  case AV_PIX_FMT_GRAY16:
97  color_space = CLRSPC_GRAY;
98  break;
99  case AV_PIX_FMT_RGB24:
100  case AV_PIX_FMT_RGBA:
101  case AV_PIX_FMT_RGB48:
102  case AV_PIX_FMT_RGBA64:
103  case AV_PIX_FMT_GBR24P:
104  case AV_PIX_FMT_GBRP9:
105  case AV_PIX_FMT_GBRP10:
106  case AV_PIX_FMT_GBRP12:
107  case AV_PIX_FMT_GBRP14:
108  case AV_PIX_FMT_GBRP16:
109  color_space = CLRSPC_SRGB;
110  break;
111  case AV_PIX_FMT_YUV410P:
112  case AV_PIX_FMT_YUV411P:
113  case AV_PIX_FMT_YUV420P:
114  case AV_PIX_FMT_YUV422P:
115  case AV_PIX_FMT_YUV440P:
116  case AV_PIX_FMT_YUV444P:
117  case AV_PIX_FMT_YUVA420P:
118  case AV_PIX_FMT_YUVA422P:
119  case AV_PIX_FMT_YUVA444P:
120  case AV_PIX_FMT_YUV420P9:
121  case AV_PIX_FMT_YUV422P9:
122  case AV_PIX_FMT_YUV444P9:
144  color_space = CLRSPC_SYCC;
145  break;
146  default:
147  av_log(avctx, AV_LOG_ERROR,
148  "The requested pixel format '%s' is not supported\n",
149  av_get_pix_fmt_name(avctx->pix_fmt));
150  return NULL;
151  }
152 
153  cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
154  if (!cmptparm) {
155  av_log(avctx, AV_LOG_ERROR, "Not enough memory\n");
156  return NULL;
157  }
158  for (i = 0; i < numcomps; i++) {
159  cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
160  cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
161  cmptparm[i].sgnd = 0;
162  cmptparm[i].dx = sub_dx[i];
163  cmptparm[i].dy = sub_dy[i];
164  cmptparm[i].w = avctx->width / sub_dx[i];
165  cmptparm[i].h = avctx->height / sub_dy[i];
166  }
167 
168  img = opj_image_create(numcomps, cmptparm, color_space);
169  av_freep(&cmptparm);
170  return img;
171 }
172 
174 {
175  LibOpenJPEGContext *ctx = avctx->priv_data;
176  int err = AVERROR(ENOMEM);
177 
178  opj_set_default_encoder_parameters(&ctx->enc_params);
179 
180  ctx->enc_params.cp_rsiz = ctx->profile;
181  ctx->enc_params.mode = !!avctx->global_quality;
182  ctx->enc_params.cp_cinema = ctx->cinema_mode;
183  ctx->enc_params.prog_order = ctx->prog_order;
184  ctx->enc_params.numresolution = ctx->numresolution;
185  ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
186  ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
187  ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
188  ctx->enc_params.tcp_numlayers = ctx->numlayers;
189  ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
190 
191  if (ctx->cinema_mode > 0) {
192  ctx->enc_params.irreversible = 1;
193  ctx->enc_params.tcp_mct = 1;
194  ctx->enc_params.tile_size_on = 0;
195  /* no subsampling */
196  ctx->enc_params.cp_tdx=1;
197  ctx->enc_params.cp_tdy=1;
198  ctx->enc_params.subsampling_dx = 1;
199  ctx->enc_params.subsampling_dy = 1;
200  /* Tile and Image shall be at (0,0) */
201  ctx->enc_params.cp_tx0 = 0;
202  ctx->enc_params.cp_ty0 = 0;
203  ctx->enc_params.image_offset_x0 = 0;
204  ctx->enc_params.image_offset_y0 = 0;
205  /* Codeblock size= 32*32 */
206  ctx->enc_params.cblockw_init = 32;
207  ctx->enc_params.cblockh_init = 32;
208  ctx->enc_params.csty |= 0x01;
209  /* No ROI */
210  ctx->enc_params.roi_compno = -1;
211 
212  if (ctx->enc_params.prog_order != CPRL) {
213  av_log(avctx, AV_LOG_ERROR, "prog_order forced to CPRL\n");
214  ctx->enc_params.prog_order = CPRL;
215  }
216  ctx->enc_params.tp_flag = 'C';
217  ctx->enc_params.tp_on = 1;
218  }
219 
220  ctx->compress = opj_create_compress(ctx->format);
221  if (!ctx->compress) {
222  av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
223  return AVERROR(ENOMEM);
224  }
225 
226  avctx->coded_frame = avcodec_alloc_frame();
227  if (!avctx->coded_frame) {
228  av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
229  goto fail;
230  }
231 
232  ctx->image = mj2_create_image(avctx, &ctx->enc_params);
233  if (!ctx->image) {
234  av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
235  err = AVERROR(EINVAL);
236  goto fail;
237  }
238 
239  memset(&ctx->event_mgr, 0, sizeof(opj_event_mgr_t));
240  ctx->event_mgr.info_handler = info_callback;
241  ctx->event_mgr.error_handler = error_callback;
242  ctx->event_mgr.warning_handler = warning_callback;
243  opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
244 
245  return 0;
246 
247 fail:
248  av_freep(&ctx->compress);
249  av_freep(&avctx->coded_frame);
250  return err;
251 }
252 
253 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
254 {
255  int compno;
256  int x;
257  int y;
258  int image_index;
259  int frame_index;
260  const int numcomps = image->numcomps;
261 
262  for (compno = 0; compno < numcomps; ++compno) {
263  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
264  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
265  return 0;
266  }
267  }
268 
269  for (compno = 0; compno < numcomps; ++compno) {
270  for (y = 0; y < avctx->height; ++y) {
271  image_index = y * avctx->width;
272  frame_index = y * frame->linesize[0] + compno;
273  for (x = 0; x < avctx->width; ++x) {
274  image->comps[compno].data[image_index++] = frame->data[0][frame_index];
275  frame_index += numcomps;
276  }
277  }
278  }
279 
280  return 1;
281 }
282 
283 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
284 {
285  int compno;
286  int x;
287  int y;
288  int image_index;
289  int frame_index;
290  const int numcomps = image->numcomps;
291  uint16_t *frame_ptr = (uint16_t*)frame->data[0];
292 
293  for (compno = 0; compno < numcomps; ++compno) {
294  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
295  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
296  return 0;
297  }
298  }
299 
300  for (compno = 0; compno < numcomps; ++compno) {
301  for (y = 0; y < avctx->height; ++y) {
302  image_index = y * avctx->width;
303  frame_index = y * (frame->linesize[0] / 2) + compno;
304  for (x = 0; x < avctx->width; ++x) {
305  image->comps[compno].data[image_index++] = frame_ptr[frame_index];
306  frame_index += numcomps;
307  }
308  }
309  }
310 
311  return 1;
312 }
313 
314 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
315 {
316  int compno;
317  int x;
318  int y;
319  int width;
320  int height;
321  int image_index;
322  int frame_index;
323  const int numcomps = image->numcomps;
324 
325  for (compno = 0; compno < numcomps; ++compno) {
326  if (image->comps[compno].w > frame->linesize[compno]) {
327  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
328  return 0;
329  }
330  }
331 
332  for (compno = 0; compno < numcomps; ++compno) {
333  width = avctx->width / image->comps[compno].dx;
334  height = avctx->height / image->comps[compno].dy;
335  for (y = 0; y < height; ++y) {
336  image_index = y * width;
337  frame_index = y * frame->linesize[compno];
338  for (x = 0; x < width; ++x)
339  image->comps[compno].data[image_index++] = frame->data[compno][frame_index++];
340  }
341  }
342 
343  return 1;
344 }
345 
346 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
347 {
348  int compno;
349  int x;
350  int y;
351  int width;
352  int height;
353  int image_index;
354  int frame_index;
355  const int numcomps = image->numcomps;
356  uint16_t *frame_ptr;
357 
358  for (compno = 0; compno < numcomps; ++compno) {
359  if (image->comps[compno].w > frame->linesize[compno]) {
360  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
361  return 0;
362  }
363  }
364 
365  for (compno = 0; compno < numcomps; ++compno) {
366  width = avctx->width / image->comps[compno].dx;
367  height = avctx->height / image->comps[compno].dy;
368  frame_ptr = (uint16_t*)frame->data[compno];
369  for (y = 0; y < height; ++y) {
370  image_index = y * width;
371  frame_index = y * (frame->linesize[compno] / 2);
372  for (x = 0; x < width; ++x)
373  image->comps[compno].data[image_index++] = frame_ptr[frame_index++];
374  }
375  }
376 
377  return 1;
378 }
379 
381  const AVFrame *frame, int *got_packet)
382 {
383  LibOpenJPEGContext *ctx = avctx->priv_data;
384  opj_cinfo_t *compress = ctx->compress;
385  opj_image_t *image = ctx->image;
386  opj_cio_t *stream;
387  int cpyresult = 0;
388  int ret, len;
389  AVFrame gbrframe;
390 
391  // x0, y0 is the top left corner of the image
392  // x1, y1 is the width, height of the reference grid
393  image->x0 = 0;
394  image->y0 = 0;
395  image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
396  image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
397 
398  switch (avctx->pix_fmt) {
399  case AV_PIX_FMT_RGB24:
400  case AV_PIX_FMT_RGBA:
401  case AV_PIX_FMT_GRAY8A:
402  cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
403  break;
404  case AV_PIX_FMT_RGB48:
405  case AV_PIX_FMT_RGBA64:
406  cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
407  break;
408  case AV_PIX_FMT_GBR24P:
409  case AV_PIX_FMT_GBRP9:
410  case AV_PIX_FMT_GBRP10:
411  case AV_PIX_FMT_GBRP12:
412  case AV_PIX_FMT_GBRP14:
413  case AV_PIX_FMT_GBRP16:
414  gbrframe = *frame;
415  gbrframe.data[0] = frame->data[2]; // swap to be rgb
416  gbrframe.data[1] = frame->data[0];
417  gbrframe.data[2] = frame->data[1];
418  gbrframe.linesize[0] = frame->linesize[2];
419  gbrframe.linesize[1] = frame->linesize[0];
420  gbrframe.linesize[2] = frame->linesize[1];
421  if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
422  cpyresult = libopenjpeg_copy_unpacked8(avctx, &gbrframe, image);
423  } else {
424  cpyresult = libopenjpeg_copy_unpacked16(avctx, &gbrframe, image);
425  }
426  break;
427  case AV_PIX_FMT_GRAY8:
428  case AV_PIX_FMT_YUV410P:
429  case AV_PIX_FMT_YUV411P:
430  case AV_PIX_FMT_YUV420P:
431  case AV_PIX_FMT_YUV422P:
432  case AV_PIX_FMT_YUV440P:
433  case AV_PIX_FMT_YUV444P:
434  case AV_PIX_FMT_YUVA420P:
435  case AV_PIX_FMT_YUVA422P:
436  case AV_PIX_FMT_YUVA444P:
437  cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
438  break;
439  case AV_PIX_FMT_GRAY16:
440  case AV_PIX_FMT_YUV420P9:
441  case AV_PIX_FMT_YUV422P9:
442  case AV_PIX_FMT_YUV444P9:
464  cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
465  break;
466  default:
467  av_log(avctx, AV_LOG_ERROR,
468  "The frame's pixel format '%s' is not supported\n",
469  av_get_pix_fmt_name(avctx->pix_fmt));
470  return AVERROR(EINVAL);
471  break;
472  }
473 
474  if (!cpyresult) {
475  av_log(avctx, AV_LOG_ERROR,
476  "Could not copy the frame data to the internal image buffer\n");
477  return -1;
478  }
479 
480  opj_setup_encoder(compress, &ctx->enc_params, image);
481  stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
482  if (!stream) {
483  av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
484  return AVERROR(ENOMEM);
485  }
486 
487  if (!opj_encode(compress, stream, image, NULL)) {
488  opj_cio_close(stream);
489  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
490  return -1;
491  }
492 
493  len = cio_tell(stream);
494  if ((ret = ff_alloc_packet2(avctx, pkt, len)) < 0) {
495  opj_cio_close(stream);
496  return ret;
497  }
498 
499  memcpy(pkt->data, stream->buffer, len);
500  pkt->flags |= AV_PKT_FLAG_KEY;
501  *got_packet = 1;
502  opj_cio_close(stream);
503  return 0;
504 }
505 
507 {
508  LibOpenJPEGContext *ctx = avctx->priv_data;
509 
510  opj_destroy_compress(ctx->compress);
511  opj_image_destroy(ctx->image);
512  av_freep(&avctx->coded_frame);
513  return 0;
514 }
515 
516 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
517 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
518 static const AVOption options[] = {
519  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
520  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
521  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
522  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
523  { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
524  { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
525  { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
526  { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
527  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
528  { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
529  { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
530  { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
531  { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
532  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
533  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
534  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
535  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
536  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
537  { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
538  { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
539  { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
540  { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
541  { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
542  { NULL },
543 };
544 
545 static const AVClass class = {
546  .class_name = "libopenjpeg",
547  .item_name = av_default_item_name,
548  .option = options,
550 };
551 
553  .name = "libopenjpeg",
554  .type = AVMEDIA_TYPE_VIDEO,
555  .id = AV_CODEC_ID_JPEG2000,
556  .priv_data_size = sizeof(LibOpenJPEGContext),
558  .encode2 = libopenjpeg_encode_frame,
560  .capabilities = 0,
561  .pix_fmts = (const enum AVPixelFormat[]) {
578  },
579  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
580  .priv_class = &class,
581 };