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_GRAY10:
193  case AV_PIX_FMT_GRAY12:
194  case AV_PIX_FMT_GRAY14:
195  case AV_PIX_FMT_GRAY16:
196  case AV_PIX_FMT_YA16:
197  color_space = OPJ_CLRSPC_GRAY;
198  break;
199  case AV_PIX_FMT_RGB24:
200  case AV_PIX_FMT_RGBA:
201  case AV_PIX_FMT_RGB48:
202  case AV_PIX_FMT_RGBA64:
203  case AV_PIX_FMT_GBR24P:
204  case AV_PIX_FMT_GBRP9:
205  case AV_PIX_FMT_GBRP10:
206  case AV_PIX_FMT_GBRP12:
207  case AV_PIX_FMT_GBRP14:
208  case AV_PIX_FMT_GBRP16:
209  case AV_PIX_FMT_XYZ12:
210  color_space = OPJ_CLRSPC_SRGB;
211  break;
212  case AV_PIX_FMT_YUV410P:
213  case AV_PIX_FMT_YUV411P:
214  case AV_PIX_FMT_YUV420P:
215  case AV_PIX_FMT_YUV422P:
216  case AV_PIX_FMT_YUV440P:
217  case AV_PIX_FMT_YUV444P:
218  case AV_PIX_FMT_YUVA420P:
219  case AV_PIX_FMT_YUVA422P:
220  case AV_PIX_FMT_YUVA444P:
221  case AV_PIX_FMT_YUV420P9:
222  case AV_PIX_FMT_YUV422P9:
223  case AV_PIX_FMT_YUV444P9:
245  color_space = OPJ_CLRSPC_SYCC;
246  break;
247  default:
248  av_log(avctx, AV_LOG_ERROR,
249  "The requested pixel format '%s' is not supported\n",
250  av_get_pix_fmt_name(avctx->pix_fmt));
251  return NULL;
252  }
253 
254  for (i = 0; i < numcomps; i++) {
255  cmptparm[i].prec = desc->comp[i].depth;
256  cmptparm[i].bpp = desc->comp[i].depth;
257  cmptparm[i].sgnd = 0;
258  cmptparm[i].dx = sub_dx[i];
259  cmptparm[i].dy = sub_dy[i];
260  cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
261  cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
262  }
263 
264  img = opj_image_create(numcomps, cmptparm, color_space);
265 
266  if (!img)
267  return NULL;
268 
269  // x0, y0 is the top left corner of the image
270  // x1, y1 is the width, height of the reference grid
271  img->x0 = 0;
272  img->y0 = 0;
273  img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
274  img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
275 
276  return img;
277 }
278 
280 {
281  LibOpenJPEGContext *ctx = avctx->priv_data;
282  int err = 0;
283 
284  opj_set_default_encoder_parameters(&ctx->enc_params);
285 
286  switch (ctx->cinema_mode) {
287  case OPJ_CINEMA2K_24:
288  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
289  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
290  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
291  break;
292  case OPJ_CINEMA2K_48:
293  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
294  ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
295  ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
296  break;
297  case OPJ_CINEMA4K_24:
298  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
299  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
300  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
301  break;
302  }
303 
304  switch (ctx->profile) {
305  case OPJ_CINEMA2K:
306  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
307  err = AVERROR(EINVAL);
308  break;
309  }
310  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
311  break;
312  case OPJ_CINEMA4K:
313  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
314  err = AVERROR(EINVAL);
315  break;
316  }
317  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
318  break;
319  }
320 
321  if (err) {
322  av_log(avctx, AV_LOG_ERROR,
323  "Invalid parameter pairing: cinema_mode and profile conflict.\n");
324  return err;
325  }
326 
327  if (!ctx->numresolution) {
328  ctx->numresolution = 6;
329  while (FFMIN(avctx->width, avctx->height) >> ctx->numresolution < 1)
330  ctx->numresolution --;
331  }
332 
333  ctx->enc_params.prog_order = ctx->prog_order;
334  ctx->enc_params.numresolution = ctx->numresolution;
335  ctx->enc_params.irreversible = ctx->irreversible;
336  ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
337  ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
338  ctx->enc_params.tcp_numlayers = 1;
339  ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
340 
341  if (ctx->cinema_mode > 0) {
343  }
344 
345  return 0;
346 }
347 
348 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
349 {
350  int compno;
351  int x;
352  int y;
353  int *image_line;
354  int frame_index;
355  const int numcomps = image->numcomps;
356 
357  for (compno = 0; compno < numcomps; ++compno) {
358  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
359  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
360  return 0;
361  }
362  }
363 
364  for (compno = 0; compno < numcomps; ++compno) {
365  for (y = 0; y < avctx->height; ++y) {
366  image_line = image->comps[compno].data + y * image->comps[compno].w;
367  frame_index = y * frame->linesize[0] + compno;
368  for (x = 0; x < avctx->width; ++x) {
369  image_line[x] = frame->data[0][frame_index];
370  frame_index += numcomps;
371  }
372  for (; x < image->comps[compno].w; ++x) {
373  image_line[x] = image_line[x - 1];
374  }
375  }
376  for (; y < image->comps[compno].h; ++y) {
377  image_line = image->comps[compno].data + y * image->comps[compno].w;
378  for (x = 0; x < image->comps[compno].w; ++x) {
379  image_line[x] = image_line[x - (int)image->comps[compno].w];
380  }
381  }
382  }
383 
384  return 1;
385 }
386 
387 // for XYZ 12 bit
388 static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
389 {
390  int compno;
391  int x, y;
392  int *image_line;
393  int frame_index;
394  const int numcomps = image->numcomps;
395  uint16_t *frame_ptr = (uint16_t *)frame->data[0];
396 
397  for (compno = 0; compno < numcomps; ++compno) {
398  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
399  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
400  return 0;
401  }
402  }
403 
404  for (compno = 0; compno < numcomps; ++compno) {
405  for (y = 0; y < avctx->height; ++y) {
406  image_line = image->comps[compno].data + y * image->comps[compno].w;
407  frame_index = y * (frame->linesize[0] / 2) + compno;
408  for (x = 0; x < avctx->width; ++x) {
409  image_line[x] = frame_ptr[frame_index] >> 4;
410  frame_index += numcomps;
411  }
412  for (; x < image->comps[compno].w; ++x) {
413  image_line[x] = image_line[x - 1];
414  }
415  }
416  for (; y < image->comps[compno].h; ++y) {
417  image_line = image->comps[compno].data + y * image->comps[compno].w;
418  for (x = 0; x < image->comps[compno].w; ++x) {
419  image_line[x] = image_line[x - (int)image->comps[compno].w];
420  }
421  }
422  }
423 
424  return 1;
425 }
426 
427 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
428 {
429  int compno;
430  int x;
431  int y;
432  int *image_line;
433  int frame_index;
434  const int numcomps = image->numcomps;
435  uint16_t *frame_ptr = (uint16_t*)frame->data[0];
436 
437  for (compno = 0; compno < numcomps; ++compno) {
438  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
439  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
440  return 0;
441  }
442  }
443 
444  for (compno = 0; compno < numcomps; ++compno) {
445  for (y = 0; y < avctx->height; ++y) {
446  image_line = image->comps[compno].data + y * image->comps[compno].w;
447  frame_index = y * (frame->linesize[0] / 2) + compno;
448  for (x = 0; x < avctx->width; ++x) {
449  image_line[x] = frame_ptr[frame_index];
450  frame_index += numcomps;
451  }
452  for (; x < image->comps[compno].w; ++x) {
453  image_line[x] = image_line[x - 1];
454  }
455  }
456  for (; y < image->comps[compno].h; ++y) {
457  image_line = image->comps[compno].data + y * image->comps[compno].w;
458  for (x = 0; x < image->comps[compno].w; ++x) {
459  image_line[x] = image_line[x - (int)image->comps[compno].w];
460  }
461  }
462  }
463 
464  return 1;
465 }
466 
467 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
468 {
469  int compno;
470  int x;
471  int y;
472  int width;
473  int height;
474  int *image_line;
475  int frame_index;
476  const int numcomps = image->numcomps;
477 
478  for (compno = 0; compno < numcomps; ++compno) {
479  if (image->comps[compno].w > frame->linesize[compno]) {
480  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
481  return 0;
482  }
483  }
484 
485  for (compno = 0; compno < numcomps; ++compno) {
486  width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
487  height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
488  for (y = 0; y < height; ++y) {
489  image_line = image->comps[compno].data + y * image->comps[compno].w;
490  frame_index = y * frame->linesize[compno];
491  for (x = 0; x < width; ++x)
492  image_line[x] = frame->data[compno][frame_index++];
493  for (; x < image->comps[compno].w; ++x) {
494  image_line[x] = image_line[x - 1];
495  }
496  }
497  for (; y < image->comps[compno].h; ++y) {
498  image_line = image->comps[compno].data + y * image->comps[compno].w;
499  for (x = 0; x < image->comps[compno].w; ++x) {
500  image_line[x] = image_line[x - (int)image->comps[compno].w];
501  }
502  }
503  }
504 
505  return 1;
506 }
507 
508 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
509 {
510  int compno;
511  int x;
512  int y;
513  int width;
514  int height;
515  int *image_line;
516  int frame_index;
517  const int numcomps = image->numcomps;
518  uint16_t *frame_ptr;
519 
520  for (compno = 0; compno < numcomps; ++compno) {
521  if (image->comps[compno].w > frame->linesize[compno]) {
522  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
523  return 0;
524  }
525  }
526 
527  for (compno = 0; compno < numcomps; ++compno) {
528  width = (avctx->width + image->comps[compno].dx - 1) / image->comps[compno].dx;
529  height = (avctx->height + image->comps[compno].dy - 1) / image->comps[compno].dy;
530  frame_ptr = (uint16_t *)frame->data[compno];
531  for (y = 0; y < height; ++y) {
532  image_line = image->comps[compno].data + y * image->comps[compno].w;
533  frame_index = y * (frame->linesize[compno] / 2);
534  for (x = 0; x < width; ++x)
535  image_line[x] = frame_ptr[frame_index++];
536  for (; x < image->comps[compno].w; ++x) {
537  image_line[x] = image_line[x - 1];
538  }
539  }
540  for (; y < image->comps[compno].h; ++y) {
541  image_line = image->comps[compno].data + y * image->comps[compno].w;
542  for (x = 0; x < image->comps[compno].w; ++x) {
543  image_line[x] = image_line[x - (int)image->comps[compno].w];
544  }
545  }
546  }
547 
548  return 1;
549 }
550 
552  const AVFrame *frame, int *got_packet)
553 {
554  LibOpenJPEGContext *ctx = avctx->priv_data;
555  int ret;
556  AVFrame *gbrframe;
557  int cpyresult = 0;
558  PacketWriter writer = { 0 };
559  opj_codec_t *compress = NULL;
560  opj_stream_t *stream = NULL;
561  opj_image_t *image = mj2_create_image(avctx, &ctx->enc_params);
562  if (!image) {
563  av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
564  ret = AVERROR(EINVAL);
565  goto done;
566  }
567 
568  switch (avctx->pix_fmt) {
569  case AV_PIX_FMT_RGB24:
570  case AV_PIX_FMT_RGBA:
571  case AV_PIX_FMT_YA8:
572  cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
573  break;
574  case AV_PIX_FMT_XYZ12:
575  cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
576  break;
577  case AV_PIX_FMT_RGB48:
578  case AV_PIX_FMT_RGBA64:
579  case AV_PIX_FMT_YA16:
580  cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
581  break;
582  case AV_PIX_FMT_GBR24P:
583  case AV_PIX_FMT_GBRP9:
584  case AV_PIX_FMT_GBRP10:
585  case AV_PIX_FMT_GBRP12:
586  case AV_PIX_FMT_GBRP14:
587  case AV_PIX_FMT_GBRP16:
588  gbrframe = av_frame_clone(frame);
589  if (!gbrframe) {
590  ret = AVERROR(ENOMEM);
591  goto done;
592  }
593  gbrframe->data[0] = frame->data[2]; // swap to be rgb
594  gbrframe->data[1] = frame->data[0];
595  gbrframe->data[2] = frame->data[1];
596  gbrframe->linesize[0] = frame->linesize[2];
597  gbrframe->linesize[1] = frame->linesize[0];
598  gbrframe->linesize[2] = frame->linesize[1];
599  if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
600  cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
601  } else {
602  cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
603  }
604  av_frame_free(&gbrframe);
605  break;
606  case AV_PIX_FMT_GRAY8:
607  case AV_PIX_FMT_YUV410P:
608  case AV_PIX_FMT_YUV411P:
609  case AV_PIX_FMT_YUV420P:
610  case AV_PIX_FMT_YUV422P:
611  case AV_PIX_FMT_YUV440P:
612  case AV_PIX_FMT_YUV444P:
613  case AV_PIX_FMT_YUVA420P:
614  case AV_PIX_FMT_YUVA422P:
615  case AV_PIX_FMT_YUVA444P:
616  cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
617  break;
618  case AV_PIX_FMT_GRAY10:
619  case AV_PIX_FMT_GRAY12:
620  case AV_PIX_FMT_GRAY14:
621  case AV_PIX_FMT_GRAY16:
622  case AV_PIX_FMT_YUV420P9:
623  case AV_PIX_FMT_YUV422P9:
624  case AV_PIX_FMT_YUV444P9:
646  cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
647  break;
648  default:
649  av_log(avctx, AV_LOG_ERROR,
650  "The frame's pixel format '%s' is not supported\n",
651  av_get_pix_fmt_name(avctx->pix_fmt));
652  ret = AVERROR(EINVAL);
653  goto done;
654  break;
655  }
656 
657  if (!cpyresult) {
658  av_log(avctx, AV_LOG_ERROR,
659  "Could not copy the frame data to the internal image buffer\n");
660  ret = -1;
661  goto done;
662  }
663 
664  if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
665  goto done;
666  }
667 
668  compress = opj_create_compress(ctx->format);
669  if (!compress) {
670  av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
671  ret = AVERROR(ENOMEM);
672  goto done;
673  }
674 
675  if (!opj_set_error_handler(compress, error_callback, avctx) ||
676  !opj_set_warning_handler(compress, warning_callback, avctx) ||
677  !opj_set_info_handler(compress, info_callback, avctx)) {
678  av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
679  ret = AVERROR_EXTERNAL;
680  goto done;
681  }
682 
683  if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
684  av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
685  ret = AVERROR_EXTERNAL;
686  goto done;
687  }
688  stream = opj_stream_default_create(OPJ_STREAM_WRITE);
689 
690  if (!stream) {
691  av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
692  ret = AVERROR(ENOMEM);
693  goto done;
694  }
695 
696  writer.packet = pkt;
697  opj_stream_set_write_function(stream, stream_write);
698  opj_stream_set_skip_function(stream, stream_skip);
699  opj_stream_set_seek_function(stream, stream_seek);
700  opj_stream_set_user_data(stream, &writer, NULL);
701 
702  if (!opj_start_compress(compress, image, stream) ||
703  !opj_encode(compress, stream) ||
704  !opj_end_compress(compress, stream)) {
705  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
706  ret = AVERROR_EXTERNAL;
707  goto done;
708  }
709 
710  av_shrink_packet(pkt, writer.pos);
711 
712  pkt->flags |= AV_PKT_FLAG_KEY;
713  *got_packet = 1;
714  ret = 0;
715 
716 done:
717  opj_stream_destroy(stream);
718  opj_destroy_codec(compress);
719  opj_image_destroy(image);
720  return ret;
721 }
722 
723 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
724 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
725 static const AVOption options[] = {
726  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = OPJ_CODEC_JP2 }, OPJ_CODEC_J2K, OPJ_CODEC_JP2, VE, "format" },
727  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CODEC_J2K }, 0, 0, VE, "format" },
728  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CODEC_JP2 }, 0, 0, VE, "format" },
729  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = OPJ_STD_RSIZ }, OPJ_STD_RSIZ, OPJ_CINEMA4K, VE, "profile" },
730  { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_STD_RSIZ }, 0, 0, VE, "profile" },
731  { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K }, 0, 0, VE, "profile" },
732  { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA4K }, 0, 0, VE, "profile" },
733  { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OPJ_OFF }, OPJ_OFF, OPJ_CINEMA4K_24, VE, "cinema_mode" },
734  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_OFF }, 0, 0, VE, "cinema_mode" },
735  { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
736  { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
737  { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
738  { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = OPJ_LRCP }, OPJ_LRCP, OPJ_CPRL, VE, "prog_order" },
739  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_LRCP }, 0, 0, VE, "prog_order" },
740  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_RLCP }, 0, 0, VE, "prog_order" },
741  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_RPCL }, 0, 0, VE, "prog_order" },
742  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_PCRL }, 0, 0, VE, "prog_order" },
743  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ_CPRL }, 0, 0, VE, "prog_order" },
744  { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 0, 33, VE },
745  { "irreversible", NULL, OFFSET(irreversible), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
746  { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
747  { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
748  { NULL },
749 };
750 
751 static const AVClass openjpeg_class = {
752  .class_name = "libopenjpeg",
753  .item_name = av_default_item_name,
754  .option = options,
755  .version = LIBAVUTIL_VERSION_INT,
756 };
757 
759  .name = "libopenjpeg",
760  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
761  .type = AVMEDIA_TYPE_VIDEO,
762  .id = AV_CODEC_ID_JPEG2000,
763  .priv_data_size = sizeof(LibOpenJPEGContext),
765  .encode2 = libopenjpeg_encode_frame,
767  .pix_fmts = (const enum AVPixelFormat[]) {
786  },
787  .priv_class = &openjpeg_class,
788  .wrapper_name = "libopenjpeg",
789 };
#define NULL
Definition: coverity.c:32
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:420
static const char * format[]
Definition: af_aiir.c:330
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:414
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2446
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:416
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:389
8 bits gray, 8 bits alpha
Definition: pixfmt.h:143
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:417
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:71
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:68
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
const char * desc
Definition: nvenc.c:65
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:369
AVCodec ff_libopenjpeg_encoder
int size
Definition: avcodec.h:1446
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:395
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
AVPacket * packet
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1743
static const AVClass openjpeg_class
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:383
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3424
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:1054
#define AV_PIX_FMT_GRAY10
Definition: pixfmt.h:360
opj_cparameters_t enc_params
#define img
#define AV_PIX_FMT_GRAY12
Definition: pixfmt.h:361
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:101
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:413
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:394
static void warning_callback(const char *msg, void *data)
static AVFrame * frame
#define height
uint8_t * data
Definition: avcodec.h:1445
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:392
static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:384
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:419
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1477
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:176
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:202
#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:421
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:364
static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:365
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:3431
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:382
#define FFMAX(a, b)
Definition: common.h:94
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1024
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:93
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1451
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:70
#define AV_PIX_FMT_YUV422P9
Definition: pixfmt.h:377
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:398
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:363
#define VE
#define FFMIN(a, b)
Definition: common.h:96
#define width
int width
picture width / height.
Definition: avcodec.h:1706
AVFormatContext * ctx
Definition: movenc.c:48
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:418
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:378
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:397
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:390
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:387
Libavcodec external API header.
int compression_level
Definition: avcodec.h:1605
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:257
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:177
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
#define AV_PIX_FMT_GRAY14
Definition: pixfmt.h:362
main external API structure.
Definition: avcodec.h:1533
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:379
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:72
#define AV_PIX_FMT_XYZ12
Definition: pixfmt.h:423
Describe the class of an AVClass context structure.
Definition: log.h:67
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:376
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:388
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:396
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:380
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:386
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:240
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:66
Y , 8bpp.
Definition: pixfmt.h:74
common internal api header.
common internal and external API header
#define AV_PIX_FMT_YUVA444P9
Definition: pixfmt.h:415
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:73
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:782
void * priv_data
Definition: avcodec.h:1560
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:99
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:2362
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:64
This structure stores compressed data.
Definition: avcodec.h:1422
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:391
for(j=16;j >0;--j)