FFmpeg
 All Data Structures Namespaces 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 #include "libavutil/avassert.h"
28 #include "libavutil/common.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/opt.h"
32 #include "avcodec.h"
33 #include "internal.h"
34 #include <openjpeg.h>
35 
36 typedef struct LibOpenJPEGContext {
38  opj_cparameters_t enc_params;
39  int format;
40  int profile;
48 
49 static void error_callback(const char *msg, void *data)
50 {
51  av_log(data, AV_LOG_ERROR, "%s\n", msg);
52 }
53 
54 static void warning_callback(const char *msg, void *data)
55 {
56  av_log(data, AV_LOG_WARNING, "%s\n", msg);
57 }
58 
59 static void info_callback(const char *msg, void *data)
60 {
61  av_log(data, AV_LOG_DEBUG, "%s\n", msg);
62 }
63 
64 typedef struct PacketWriter {
65  int pos;
67 } PacketWriter;
68 
69 static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
70 {
71  PacketWriter *writer = user_data;
72  AVPacket *packet = writer->packet;
73  int remaining = packet->size - writer->pos;
74  if (nb_bytes > remaining) {
75  OPJ_SIZE_T needed = nb_bytes - remaining;
76  int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
77  if (needed > max_growth) {
78  return (OPJ_SIZE_T)-1;
79  }
80  if (av_grow_packet(packet, (int)needed)) {
81  return (OPJ_SIZE_T)-1;
82  }
83  }
84  memcpy(packet->data + writer->pos, out_buffer, nb_bytes);
85  writer->pos += (int)nb_bytes;
86  return nb_bytes;
87 }
88 
89 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
90 {
91  PacketWriter *writer = user_data;
92  AVPacket *packet = writer->packet;
93  if (nb_bytes < 0) {
94  if (writer->pos == 0) {
95  return (OPJ_SIZE_T)-1;
96  }
97  if (nb_bytes + writer->pos < 0) {
98  nb_bytes = -writer->pos;
99  }
100  } else {
101  int remaining = packet->size - writer->pos;
102  if (nb_bytes > remaining) {
103  OPJ_SIZE_T needed = nb_bytes - remaining;
104  int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
105  if (needed > max_growth) {
106  return (OPJ_SIZE_T)-1;
107  }
108  if (av_grow_packet(packet, (int)needed)) {
109  return (OPJ_SIZE_T)-1;
110  }
111  }
112  }
113  writer->pos += (int)nb_bytes;
114  return nb_bytes;
115 }
116 
117 static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
118 {
119  PacketWriter *writer = user_data;
120  AVPacket *packet = writer->packet;
121  if (nb_bytes < 0) {
122  return OPJ_FALSE;
123  }
124  if (nb_bytes > packet->size) {
125  if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
126  av_grow_packet(packet, (int)nb_bytes - packet->size)) {
127  return OPJ_FALSE;
128  }
129  }
130  writer->pos = (int)nb_bytes;
131  return OPJ_TRUE;
132 }
133 
134 static void cinema_parameters(opj_cparameters_t *p)
135 {
136  p->tile_size_on = 0;
137  p->cp_tdx = 1;
138  p->cp_tdy = 1;
139 
140  /* Tile part */
141  p->tp_flag = 'C';
142  p->tp_on = 1;
143 
144  /* Tile and Image shall be at (0, 0) */
145  p->cp_tx0 = 0;
146  p->cp_ty0 = 0;
147  p->image_offset_x0 = 0;
148  p->image_offset_y0 = 0;
149 
150  /* Codeblock size= 32 * 32 */
151  p->cblockw_init = 32;
152  p->cblockh_init = 32;
153  p->csty |= 0x01;
154 
155  /* The progression order shall be CPRL */
156  p->prog_order = OPJ_CPRL;
157 
158  /* No ROI */
159  p->roi_compno = -1;
160 
161  /* No subsampling */
162  p->subsampling_dx = 1;
163  p->subsampling_dy = 1;
164 
165  /* 9-7 transform */
166  p->irreversible = 1;
167 
168  p->tcp_mct = 1;
169 }
170 
171 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
172 {
174  opj_image_cmptparm_t cmptparm[4] = {{0}};
175  opj_image_t *img;
176  int i;
177  int sub_dx[4];
178  int sub_dy[4];
179  int numcomps;
180  OPJ_COLOR_SPACE color_space = OPJ_CLRSPC_UNKNOWN;
181 
182  sub_dx[0] = sub_dx[3] = 1;
183  sub_dy[0] = sub_dy[3] = 1;
184  sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
185  sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
186 
187  numcomps = desc->nb_components;
188 
189  switch (avctx->pix_fmt) {
190  case AV_PIX_FMT_GRAY8:
191  case AV_PIX_FMT_YA8:
192  case AV_PIX_FMT_GRAY16:
193  case AV_PIX_FMT_YA16:
194  color_space = OPJ_CLRSPC_GRAY;
195  break;
196  case AV_PIX_FMT_RGB24:
197  case AV_PIX_FMT_RGBA:
198  case AV_PIX_FMT_RGB48:
199  case AV_PIX_FMT_RGBA64:
200  case AV_PIX_FMT_GBR24P:
201  case AV_PIX_FMT_GBRP9:
202  case AV_PIX_FMT_GBRP10:
203  case AV_PIX_FMT_GBRP12:
204  case AV_PIX_FMT_GBRP14:
205  case AV_PIX_FMT_GBRP16:
206  case AV_PIX_FMT_XYZ12:
207  color_space = OPJ_CLRSPC_SRGB;
208  break;
209  case AV_PIX_FMT_YUV410P:
210  case AV_PIX_FMT_YUV411P:
211  case AV_PIX_FMT_YUV420P:
212  case AV_PIX_FMT_YUV422P:
213  case AV_PIX_FMT_YUV440P:
214  case AV_PIX_FMT_YUV444P:
215  case AV_PIX_FMT_YUVA420P:
216  case AV_PIX_FMT_YUVA422P:
217  case AV_PIX_FMT_YUVA444P:
218  case AV_PIX_FMT_YUV420P9:
219  case AV_PIX_FMT_YUV422P9:
220  case AV_PIX_FMT_YUV444P9:
242  color_space = OPJ_CLRSPC_SYCC;
243  break;
244  default:
245  av_log(avctx, AV_LOG_ERROR,
246  "The requested pixel format '%s' is not supported\n",
247  av_get_pix_fmt_name(avctx->pix_fmt));
248  return NULL;
249  }
250 
251  for (i = 0; i < numcomps; i++) {
252  cmptparm[i].prec = desc->comp[i].depth;
253  cmptparm[i].bpp = desc->comp[i].depth;
254  cmptparm[i].sgnd = 0;
255  cmptparm[i].dx = sub_dx[i];
256  cmptparm[i].dy = sub_dy[i];
257  cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
258  cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
259  }
260 
261  img = opj_image_create(numcomps, cmptparm, color_space);
262 
263  if (!img)
264  return NULL;
265 
266  // x0, y0 is the top left corner of the image
267  // x1, y1 is the width, height of the reference grid
268  img->x0 = 0;
269  img->y0 = 0;
270  img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
271  img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
272 
273  return img;
274 }
275 
277 {
278  LibOpenJPEGContext *ctx = avctx->priv_data;
279  int err = 0;
280 
281  opj_set_default_encoder_parameters(&ctx->enc_params);
282 
283  switch (ctx->cinema_mode) {
284  case OPJ_CINEMA2K_24:
285  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
286  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
287  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
288  break;
289  case OPJ_CINEMA2K_48:
290  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
291  ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
292  ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
293  break;
294  case OPJ_CINEMA4K_24:
295  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
296  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
297  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
298  break;
299  }
300 
301  switch (ctx->profile) {
302  case OPJ_CINEMA2K:
303  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
304  err = AVERROR(EINVAL);
305  break;
306  }
307  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
308  break;
309  case OPJ_CINEMA4K:
310  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
311  err = AVERROR(EINVAL);
312  break;
313  }
314  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
315  break;
316  }
317 
318  if (err) {
319  av_log(avctx, AV_LOG_ERROR,
320  "Invalid parameter pairing: cinema_mode and profile conflict.\n");
321  return err;
322  }
323 
324  if (!ctx->numresolution) {
325  ctx->numresolution = 6;
326  while (FFMIN(avctx->width, avctx->height) >> ctx->numresolution < 1)
327  ctx->numresolution --;
328  }
329 
330  ctx->enc_params.prog_order = ctx->prog_order;
331  ctx->enc_params.numresolution = ctx->numresolution;
332  ctx->enc_params.irreversible = ctx->irreversible;
333  ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
334  ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
335  ctx->enc_params.tcp_numlayers = 1;
336  ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
337 
338  if (ctx->cinema_mode > 0) {
340  }
341 
342  return 0;
343 }
344 
345 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
346 {
347  int compno;
348  int x;
349  int y;
350  int *image_line;
351  int frame_index;
352  const int numcomps = image->numcomps;
353 
354  for (compno = 0; compno < numcomps; ++compno) {
355  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
356  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
357  return 0;
358  }
359  }
360 
361  for (compno = 0; compno < numcomps; ++compno) {
362  for (y = 0; y < avctx->height; ++y) {
363  image_line = image->comps[compno].data + y * image->comps[compno].w;
364  frame_index = y * frame->linesize[0] + compno;
365  for (x = 0; x < avctx->width; ++x) {
366  image_line[x] = frame->data[0][frame_index];
367  frame_index += numcomps;
368  }
369  for (; x < image->comps[compno].w; ++x) {
370  image_line[x] = image_line[x - 1];
371  }
372  }
373  for (; y < image->comps[compno].h; ++y) {
374  image_line = image->comps[compno].data + y * image->comps[compno].w;
375  for (x = 0; x < image->comps[compno].w; ++x) {
376  image_line[x] = image_line[x - (int)image->comps[compno].w];
377  }
378  }
379  }
380 
381  return 1;
382 }
383 
384 // for XYZ 12 bit
385 static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
386 {
387  int compno;
388  int x, y;
389  int *image_line;
390  int frame_index;
391  const int numcomps = image->numcomps;
392  uint16_t *frame_ptr = (uint16_t *)frame->data[0];
393 
394  for (compno = 0; compno < numcomps; ++compno) {
395  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
396  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
397  return 0;
398  }
399  }
400 
401  for (compno = 0; compno < numcomps; ++compno) {
402  for (y = 0; y < avctx->height; ++y) {
403  image_line = image->comps[compno].data + y * image->comps[compno].w;
404  frame_index = y * (frame->linesize[0] / 2) + compno;
405  for (x = 0; x < avctx->width; ++x) {
406  image_line[x] = frame_ptr[frame_index] >> 4;
407  frame_index += numcomps;
408  }
409  for (; x < image->comps[compno].w; ++x) {
410  image_line[x] = image_line[x - 1];
411  }
412  }
413  for (; y < image->comps[compno].h; ++y) {
414  image_line = image->comps[compno].data + y * image->comps[compno].w;
415  for (x = 0; x < image->comps[compno].w; ++x) {
416  image_line[x] = image_line[x - (int)image->comps[compno].w];
417  }
418  }
419  }
420 
421  return 1;
422 }
423 
424 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
425 {
426  int compno;
427  int x;
428  int y;
429  int *image_line;
430  int frame_index;
431  const int numcomps = image->numcomps;
432  uint16_t *frame_ptr = (uint16_t*)frame->data[0];
433 
434  for (compno = 0; compno < numcomps; ++compno) {
435  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
436  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
437  return 0;
438  }
439  }
440 
441  for (compno = 0; compno < numcomps; ++compno) {
442  for (y = 0; y < avctx->height; ++y) {
443  image_line = image->comps[compno].data + y * image->comps[compno].w;
444  frame_index = y * (frame->linesize[0] / 2) + compno;
445  for (x = 0; x < avctx->width; ++x) {
446  image_line[x] = frame_ptr[frame_index];
447  frame_index += numcomps;
448  }
449  for (; x < image->comps[compno].w; ++x) {
450  image_line[x] = image_line[x - 1];
451  }
452  }
453  for (; y < image->comps[compno].h; ++y) {
454  image_line = image->comps[compno].data + y * image->comps[compno].w;
455  for (x = 0; x < image->comps[compno].w; ++x) {
456  image_line[x] = image_line[x - (int)image->comps[compno].w];
457  }
458  }
459  }
460 
461  return 1;
462 }
463 
464 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
465 {
466  int compno;
467  int x;
468  int y;
469  int width;
470  int height;
471  int *image_line;
472  int frame_index;
473  const int numcomps = image->numcomps;
474 
475  for (compno = 0; compno < numcomps; ++compno) {
476  if (image->comps[compno].w > frame->linesize[compno]) {
477  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
478  return 0;
479  }
480  }
481 
482  for (compno = 0; compno < numcomps; ++compno) {
483  width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
484  height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
485  for (y = 0; y < height; ++y) {
486  image_line = image->comps[compno].data + y * image->comps[compno].w;
487  frame_index = y * frame->linesize[compno];
488  for (x = 0; x < width; ++x)
489  image_line[x] = frame->data[compno][frame_index++];
490  for (; x < image->comps[compno].w; ++x) {
491  image_line[x] = image_line[x - 1];
492  }
493  }
494  for (; y < image->comps[compno].h; ++y) {
495  image_line = image->comps[compno].data + y * image->comps[compno].w;
496  for (x = 0; x < image->comps[compno].w; ++x) {
497  image_line[x] = image_line[x - (int)image->comps[compno].w];
498  }
499  }
500  }
501 
502  return 1;
503 }
504 
505 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
506 {
507  int compno;
508  int x;
509  int y;
510  int width;
511  int height;
512  int *image_line;
513  int frame_index;
514  const int numcomps = image->numcomps;
515  uint16_t *frame_ptr;
516 
517  for (compno = 0; compno < numcomps; ++compno) {
518  if (image->comps[compno].w > frame->linesize[compno]) {
519  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
520  return 0;
521  }
522  }
523 
524  for (compno = 0; compno < numcomps; ++compno) {
525  width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
526  height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
527  frame_ptr = (uint16_t *)frame->data[compno];
528  for (y = 0; y < height; ++y) {
529  image_line = image->comps[compno].data + y * image->comps[compno].w;
530  frame_index = y * (frame->linesize[compno] / 2);
531  for (x = 0; x < width; ++x)
532  image_line[x] = frame_ptr[frame_index++];
533  for (; x < image->comps[compno].w; ++x) {
534  image_line[x] = image_line[x - 1];
535  }
536  }
537  for (; y < image->comps[compno].h; ++y) {
538  image_line = image->comps[compno].data + y * image->comps[compno].w;
539  for (x = 0; x < image->comps[compno].w; ++x) {
540  image_line[x] = image_line[x - (int)image->comps[compno].w];
541  }
542  }
543  }
544 
545  return 1;
546 }
547 
549  const AVFrame *frame, int *got_packet)
550 {
551  LibOpenJPEGContext *ctx = avctx->priv_data;
552  int ret;
553  AVFrame *gbrframe;
554  int cpyresult = 0;
555  PacketWriter writer = { 0 };
556  opj_codec_t *compress = NULL;
557  opj_stream_t *stream = NULL;
558  opj_image_t *image = mj2_create_image(avctx, &ctx->enc_params);
559  if (!image) {
560  av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
561  ret = AVERROR(EINVAL);
562  goto done;
563  }
564 
565  switch (avctx->pix_fmt) {
566  case AV_PIX_FMT_RGB24:
567  case AV_PIX_FMT_RGBA:
568  case AV_PIX_FMT_YA8:
569  cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
570  break;
571  case AV_PIX_FMT_XYZ12:
572  cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
573  break;
574  case AV_PIX_FMT_RGB48:
575  case AV_PIX_FMT_RGBA64:
576  case AV_PIX_FMT_YA16:
577  cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
578  break;
579  case AV_PIX_FMT_GBR24P:
580  case AV_PIX_FMT_GBRP9:
581  case AV_PIX_FMT_GBRP10:
582  case AV_PIX_FMT_GBRP12:
583  case AV_PIX_FMT_GBRP14:
584  case AV_PIX_FMT_GBRP16:
585  gbrframe = av_frame_clone(frame);
586  if (!gbrframe) {
587  ret = AVERROR(ENOMEM);
588  goto done;
589  }
590  gbrframe->data[0] = frame->data[2]; // swap to be rgb
591  gbrframe->data[1] = frame->data[0];
592  gbrframe->data[2] = frame->data[1];
593  gbrframe->linesize[0] = frame->linesize[2];
594  gbrframe->linesize[1] = frame->linesize[0];
595  gbrframe->linesize[2] = frame->linesize[1];
596  if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
597  cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
598  } else {
599  cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
600  }
601  av_frame_free(&gbrframe);
602  break;
603  case AV_PIX_FMT_GRAY8:
604  case AV_PIX_FMT_YUV410P:
605  case AV_PIX_FMT_YUV411P:
606  case AV_PIX_FMT_YUV420P:
607  case AV_PIX_FMT_YUV422P:
608  case AV_PIX_FMT_YUV440P:
609  case AV_PIX_FMT_YUV444P:
610  case AV_PIX_FMT_YUVA420P:
611  case AV_PIX_FMT_YUVA422P:
612  case AV_PIX_FMT_YUVA444P:
613  cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
614  break;
615  case AV_PIX_FMT_GRAY16:
616  case AV_PIX_FMT_YUV420P9:
617  case AV_PIX_FMT_YUV422P9:
618  case AV_PIX_FMT_YUV444P9:
640  cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
641  break;
642  default:
643  av_log(avctx, AV_LOG_ERROR,
644  "The frame's pixel format '%s' is not supported\n",
645  av_get_pix_fmt_name(avctx->pix_fmt));
646  ret = AVERROR(EINVAL);
647  goto done;
648  break;
649  }
650 
651  if (!cpyresult) {
652  av_log(avctx, AV_LOG_ERROR,
653  "Could not copy the frame data to the internal image buffer\n");
654  ret = -1;
655  goto done;
656  }
657 
658  if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
659  goto done;
660  }
661 
662  compress = opj_create_compress(ctx->format);
663  if (!compress) {
664  av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
665  ret = AVERROR(ENOMEM);
666  goto done;
667  }
668 
669  if (!opj_set_error_handler(compress, error_callback, avctx) ||
670  !opj_set_warning_handler(compress, warning_callback, avctx) ||
671  !opj_set_info_handler(compress, info_callback, avctx)) {
672  av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
673  ret = AVERROR_EXTERNAL;
674  goto done;
675  }
676 
677  if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
678  av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
679  ret = AVERROR_EXTERNAL;
680  goto done;
681  }
682  stream = opj_stream_default_create(OPJ_STREAM_WRITE);
683 
684  if (!stream) {
685  av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
686  ret = AVERROR(ENOMEM);
687  goto done;
688  }
689 
690  writer.packet = pkt;
691  opj_stream_set_write_function(stream, stream_write);
692  opj_stream_set_skip_function(stream, stream_skip);
693  opj_stream_set_seek_function(stream, stream_seek);
694  opj_stream_set_user_data(stream, &writer, NULL);
695 
696  if (!opj_start_compress(compress, image, stream) ||
697  !opj_encode(compress, stream) ||
698  !opj_end_compress(compress, stream)) {
699  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
700  ret = AVERROR_EXTERNAL;
701  goto done;
702  }
703 
704  av_shrink_packet(pkt, writer.pos);
705 
706  pkt->flags |= AV_PKT_FLAG_KEY;
707  *got_packet = 1;
708  ret = 0;
709 
710 done:
711  opj_stream_destroy(stream);
712  opj_destroy_codec(compress);
713  opj_image_destroy(image);
714  return ret;
715 }
716 
717 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
718 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
719 static const AVOption options[] = {
720  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = OPJ_CODEC_JP2 }, OPJ_CODEC_J2K, OPJ_CODEC_JP2, VE, "format" },
721  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CODEC_J2K }, 0, 0, VE, "format" },
722  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CODEC_JP2 }, 0, 0, VE, "format" },
723  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = OPJ_STD_RSIZ }, OPJ_STD_RSIZ, OPJ_CINEMA4K, VE, "profile" },
724  { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_STD_RSIZ }, 0, 0, VE, "profile" },
725  { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K }, 0, 0, VE, "profile" },
726  { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA4K }, 0, 0, VE, "profile" },
727  { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OPJ_OFF }, OPJ_OFF, OPJ_CINEMA4K_24, VE, "cinema_mode" },
728  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_OFF }, 0, 0, VE, "cinema_mode" },
729  { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
730  { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
731  { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
732  { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = OPJ_LRCP }, OPJ_LRCP, OPJ_CPRL, VE, "prog_order" },
733  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_LRCP }, 0, 0, VE, "prog_order" },
734  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_RLCP }, 0, 0, VE, "prog_order" },
735  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_RPCL }, 0, 0, VE, "prog_order" },
736  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_PCRL }, 0, 0, VE, "prog_order" },
737  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CPRL }, 0, 0, VE, "prog_order" },
738  { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 0, 33, VE },
739  { "irreversible", NULL, OFFSET(irreversible), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
740  { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
741  { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
742  { NULL },
743 };
744 
745 static const AVClass openjpeg_class = {
746  .class_name = "libopenjpeg",
747  .item_name = av_default_item_name,
748  .option = options,
749  .version = LIBAVUTIL_VERSION_INT,
750 };
751 
753  .name = "libopenjpeg",
754  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
755  .type = AVMEDIA_TYPE_VIDEO,
756  .id = AV_CODEC_ID_JPEG2000,
757  .priv_data_size = sizeof(LibOpenJPEGContext),
759  .encode2 = libopenjpeg_encode_frame,
761  .pix_fmts = (const enum AVPixelFormat[]) {
779  },
780  .priv_class = &openjpeg_class,
781  .wrapper_name = "libopenjpeg",
782 };
#define NULL
Definition: coverity.c:32
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:407
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:401
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2363
This structure describes decoded (raw) audio or video data.
Definition: frame.h:201
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:403
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:378
8 bits gray, 8 bits alpha
Definition: pixfmt.h:139
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:404
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:67
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:64
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
const char * desc
Definition: nvenc.c:63
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:358
AVCodec ff_libopenjpeg_encoder
int size
Definition: avcodec.h:1415
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:384
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:193
AVPacket * packet
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1711
static const AVClass openjpeg_class
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:372
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3365
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:1042
opj_cparameters_t enc_params
#define img
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:32
static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:97
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:117
#define av_cold
Definition: attributes.h:82
AVOptions.
static void cinema_parameters(opj_cparameters_t *p)
#define AV_PIX_FMT_YUVA420P9
Definition: pixfmt.h:400
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:383
static void warning_callback(const char *msg, void *data)
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1414
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:381
static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:373
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:406
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1446
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:172
static opj_image_t * mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:101
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:163
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
AVS_Value void * user_data
Definition: avisynth_c.h:699
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:408
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:353
static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
uint16_t width
Definition: gdv.c:47
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:354
simple assert() macros that are a bit more flexible than ISO C assert().
static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
const char * name
Name of the codec implementation.
Definition: avcodec.h:3372
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:371
#define FFMAX(a, b)
Definition: common.h:94
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1012
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:89
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1420
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:66
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:366
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:83
#define AV_PIX_FMT_GBRP16
Definition: pixfmt.h:387
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:352
#define VE
#define FFMIN(a, b)
Definition: common.h:96
int width
picture width / height.
Definition: avcodec.h:1674
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:405
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:367
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:386
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:497
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:379
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:376
Libavcodec external API header.
int compression_level
Definition: avcodec.h:1574
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:232
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:173
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
main external API structure.
Definition: avcodec.h:1502
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:368
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:68
#define AV_PIX_FMT_XYZ12
Definition: pixfmt.h:410
static const char * format
Definition: movenc.c:47
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:365
static void info_callback(const char *msg, void *data)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:266
mfxU16 profile
Definition: qsvenc.c:44
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:377
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:385
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:369
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:375
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:215
int
static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:62
Y , 8bpp.
Definition: pixfmt.h:70
common internal api header.
common internal and external API header
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:402
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:109
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:69
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:770
void * priv_data
Definition: avcodec.h:1529
static const AVOption options[]
static void error_callback(const char *msg, void *data)
static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
#define OFFSET(x)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:95
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2279
int depth
Number of bits in the component.
Definition: pixdesc.h:58
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
AVPixelFormat
Pixel format.
Definition: pixfmt.h:60
This structure stores compressed data.
Definition: avcodec.h:1391
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:380
for(j=16;j >0;--j)