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 #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_2_1_OPENJPEG_H
38 # include <openjpeg-2.1/openjpeg.h>
39 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
40 # include <openjpeg-2.0/openjpeg.h>
41 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
42 # include <openjpeg-1.5/openjpeg.h>
43 #else
44 # include <openjpeg.h>
45 #endif
46 
47 #if HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
48 # define OPENJPEG_MAJOR_VERSION 2
49 # define OPJ(x) OPJ_##x
50 #else
51 # define OPENJPEG_MAJOR_VERSION 1
52 # define OPJ(x) x
53 #endif
54 
55 typedef struct LibOpenJPEGContext {
57  opj_image_t *image;
58  opj_cparameters_t enc_params;
59 #if OPENJPEG_MAJOR_VERSION == 1
60  opj_event_mgr_t event_mgr;
61 #endif // OPENJPEG_MAJOR_VERSION == 1
62  int format;
63  int profile;
67  int numlayers;
72 
73 static void error_callback(const char *msg, void *data)
74 {
75  av_log(data, AV_LOG_ERROR, "%s\n", msg);
76 }
77 
78 static void warning_callback(const char *msg, void *data)
79 {
80  av_log(data, AV_LOG_WARNING, "%s\n", msg);
81 }
82 
83 static void info_callback(const char *msg, void *data)
84 {
85  av_log(data, AV_LOG_DEBUG, "%s\n", msg);
86 }
87 
88 #if OPENJPEG_MAJOR_VERSION == 2
89 typedef struct PacketWriter {
90  int pos;
91  AVPacket *packet;
92 } PacketWriter;
93 
94 static OPJ_SIZE_T stream_write(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
95 {
96  PacketWriter *writer = user_data;
97  AVPacket *packet = writer->packet;
98  int remaining = packet->size - writer->pos;
99  if (nb_bytes > remaining) {
100  OPJ_SIZE_T needed = nb_bytes - remaining;
101  int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
102  if (needed > max_growth) {
103  return (OPJ_SIZE_T)-1;
104  }
105  if (av_grow_packet(packet, (int)needed)) {
106  return (OPJ_SIZE_T)-1;
107  }
108  }
109  memcpy(packet->data + writer->pos, out_buffer, nb_bytes);
110  writer->pos += (int)nb_bytes;
111  return nb_bytes;
112 }
113 
114 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
115 {
116  PacketWriter *writer = user_data;
117  AVPacket *packet = writer->packet;
118  if (nb_bytes < 0) {
119  if (writer->pos == 0) {
120  return (OPJ_SIZE_T)-1;
121  }
122  if (nb_bytes + writer->pos < 0) {
123  nb_bytes = -writer->pos;
124  }
125  } else {
126  int remaining = packet->size - writer->pos;
127  if (nb_bytes > remaining) {
128  OPJ_SIZE_T needed = nb_bytes - remaining;
129  int max_growth = INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE - packet->size;
130  if (needed > max_growth) {
131  return (OPJ_SIZE_T)-1;
132  }
133  if (av_grow_packet(packet, (int)needed)) {
134  return (OPJ_SIZE_T)-1;
135  }
136  }
137  }
138  writer->pos += (int)nb_bytes;
139  return nb_bytes;
140 }
141 
142 static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
143 {
144  PacketWriter *writer = user_data;
145  AVPacket *packet = writer->packet;
146  if (nb_bytes < 0) {
147  return OPJ_FALSE;
148  }
149  if (nb_bytes > packet->size) {
150  if (nb_bytes > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE ||
151  av_grow_packet(packet, (int)nb_bytes - packet->size)) {
152  return OPJ_FALSE;
153  }
154  }
155  writer->pos = (int)nb_bytes;
156  return OPJ_TRUE;
157 }
158 #endif // OPENJPEG_MAJOR_VERSION == 2
159 
160 static void cinema_parameters(opj_cparameters_t *p)
161 {
162  p->tile_size_on = 0;
163  p->cp_tdx = 1;
164  p->cp_tdy = 1;
165 
166  /* Tile part */
167  p->tp_flag = 'C';
168  p->tp_on = 1;
169 
170  /* Tile and Image shall be at (0, 0) */
171  p->cp_tx0 = 0;
172  p->cp_ty0 = 0;
173  p->image_offset_x0 = 0;
174  p->image_offset_y0 = 0;
175 
176  /* Codeblock size= 32 * 32 */
177  p->cblockw_init = 32;
178  p->cblockh_init = 32;
179  p->csty |= 0x01;
180 
181  /* The progression order shall be CPRL */
182  p->prog_order = OPJ(CPRL);
183 
184  /* No ROI */
185  p->roi_compno = -1;
186 
187  /* No subsampling */
188  p->subsampling_dx = 1;
189  p->subsampling_dy = 1;
190 
191  /* 9-7 transform */
192  p->irreversible = 1;
193 
194  p->tcp_mct = 1;
195 }
196 
197 static opj_image_t *mj2_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
198 {
199  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
200  opj_image_cmptparm_t cmptparm[4] = {{0}};
201  opj_image_t *img;
202  int i;
203  int sub_dx[4];
204  int sub_dy[4];
205  int numcomps;
206  OPJ_COLOR_SPACE color_space = OPJ(CLRSPC_UNKNOWN);
207 
208  sub_dx[0] = sub_dx[3] = 1;
209  sub_dy[0] = sub_dy[3] = 1;
210  sub_dx[1] = sub_dx[2] = 1 << desc->log2_chroma_w;
211  sub_dy[1] = sub_dy[2] = 1 << desc->log2_chroma_h;
212 
213  numcomps = desc->nb_components;
214 
215  switch (avctx->pix_fmt) {
216  case AV_PIX_FMT_GRAY8:
217  case AV_PIX_FMT_YA8:
218  case AV_PIX_FMT_GRAY16:
219  case AV_PIX_FMT_YA16:
220  color_space = OPJ(CLRSPC_GRAY);
221  break;
222  case AV_PIX_FMT_RGB24:
223  case AV_PIX_FMT_RGBA:
224  case AV_PIX_FMT_RGB48:
225  case AV_PIX_FMT_RGBA64:
226  case AV_PIX_FMT_GBR24P:
227  case AV_PIX_FMT_GBRP9:
228  case AV_PIX_FMT_GBRP10:
229  case AV_PIX_FMT_GBRP12:
230  case AV_PIX_FMT_GBRP14:
231  case AV_PIX_FMT_GBRP16:
232  case AV_PIX_FMT_XYZ12:
233  color_space = OPJ(CLRSPC_SRGB);
234  break;
235  case AV_PIX_FMT_YUV410P:
236  case AV_PIX_FMT_YUV411P:
237  case AV_PIX_FMT_YUV420P:
238  case AV_PIX_FMT_YUV422P:
239  case AV_PIX_FMT_YUV440P:
240  case AV_PIX_FMT_YUV444P:
241  case AV_PIX_FMT_YUVA420P:
242  case AV_PIX_FMT_YUVA422P:
243  case AV_PIX_FMT_YUVA444P:
244  case AV_PIX_FMT_YUV420P9:
245  case AV_PIX_FMT_YUV422P9:
246  case AV_PIX_FMT_YUV444P9:
268  color_space = OPJ(CLRSPC_SYCC);
269  break;
270  default:
271  av_log(avctx, AV_LOG_ERROR,
272  "The requested pixel format '%s' is not supported\n",
273  av_get_pix_fmt_name(avctx->pix_fmt));
274  return NULL;
275  }
276 
277  for (i = 0; i < numcomps; i++) {
278  cmptparm[i].prec = desc->comp[i].depth;
279  cmptparm[i].bpp = desc->comp[i].depth;
280  cmptparm[i].sgnd = 0;
281  cmptparm[i].dx = sub_dx[i];
282  cmptparm[i].dy = sub_dy[i];
283  cmptparm[i].w = (avctx->width + sub_dx[i] - 1) / sub_dx[i];
284  cmptparm[i].h = (avctx->height + sub_dy[i] - 1) / sub_dy[i];
285  }
286 
287  img = opj_image_create(numcomps, cmptparm, color_space);
288 
289  if (!img)
290  return NULL;
291 
292  // x0, y0 is the top left corner of the image
293  // x1, y1 is the width, height of the reference grid
294  img->x0 = 0;
295  img->y0 = 0;
296  img->x1 = (avctx->width - 1) * parameters->subsampling_dx + 1;
297  img->y1 = (avctx->height - 1) * parameters->subsampling_dy + 1;
298 
299  return img;
300 }
301 
303 {
304  LibOpenJPEGContext *ctx = avctx->priv_data;
305  int err = 0;
306 
307  opj_set_default_encoder_parameters(&ctx->enc_params);
308 
309 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
310  switch (ctx->cinema_mode) {
311  case OPJ_CINEMA2K_24:
312  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
313  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
314  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
315  break;
316  case OPJ_CINEMA2K_48:
317  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
318  ctx->enc_params.max_cs_size = OPJ_CINEMA_48_CS;
319  ctx->enc_params.max_comp_size = OPJ_CINEMA_48_COMP;
320  break;
321  case OPJ_CINEMA4K_24:
322  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
323  ctx->enc_params.max_cs_size = OPJ_CINEMA_24_CS;
324  ctx->enc_params.max_comp_size = OPJ_CINEMA_24_COMP;
325  break;
326  }
327 
328  switch (ctx->profile) {
329  case OPJ_CINEMA2K:
330  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_4K) {
331  err = AVERROR(EINVAL);
332  break;
333  }
334  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_2K;
335  break;
336  case OPJ_CINEMA4K:
337  if (ctx->enc_params.rsiz == OPJ_PROFILE_CINEMA_2K) {
338  err = AVERROR(EINVAL);
339  break;
340  }
341  ctx->enc_params.rsiz = OPJ_PROFILE_CINEMA_4K;
342  break;
343  }
344 
345  if (err) {
346  av_log(avctx, AV_LOG_ERROR,
347  "Invalid parameter pairing: cinema_mode and profile conflict.\n");
348  goto fail;
349  }
350 #else
351  ctx->enc_params.cp_rsiz = ctx->profile;
352  ctx->enc_params.cp_cinema = ctx->cinema_mode;
353 #endif
354 
355  ctx->enc_params.mode = !!avctx->global_quality;
356  ctx->enc_params.prog_order = ctx->prog_order;
357  ctx->enc_params.numresolution = ctx->numresolution;
358  ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
359  ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
360  ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
361  ctx->enc_params.tcp_numlayers = ctx->numlayers;
362  ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
363 
364  if (ctx->cinema_mode > 0) {
366  }
367 
368  ctx->image = mj2_create_image(avctx, &ctx->enc_params);
369  if (!ctx->image) {
370  av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
371  err = AVERROR(EINVAL);
372  goto fail;
373  }
374 
375  return 0;
376 
377 fail:
378  opj_image_destroy(ctx->image);
379  ctx->image = NULL;
380  return err;
381 }
382 
383 static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
384 {
385  int compno;
386  int x;
387  int y;
388  int *image_line;
389  int frame_index;
390  const int numcomps = image->numcomps;
391 
392  for (compno = 0; compno < numcomps; ++compno) {
393  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
394  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
395  return 0;
396  }
397  }
398 
399  for (compno = 0; compno < numcomps; ++compno) {
400  for (y = 0; y < avctx->height; ++y) {
401  image_line = image->comps[compno].data + y * image->comps[compno].w;
402  frame_index = y * frame->linesize[0] + compno;
403  for (x = 0; x < avctx->width; ++x) {
404  image_line[x] = frame->data[0][frame_index];
405  frame_index += numcomps;
406  }
407  for (; x < image->comps[compno].w; ++x) {
408  image_line[x] = image_line[x - 1];
409  }
410  }
411  for (; y < image->comps[compno].h; ++y) {
412  image_line = image->comps[compno].data + y * image->comps[compno].w;
413  for (x = 0; x < image->comps[compno].w; ++x) {
414  image_line[x] = image_line[x - image->comps[compno].w];
415  }
416  }
417  }
418 
419  return 1;
420 }
421 
422 // for XYZ 12 bit
423 static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
424 {
425  int compno;
426  int x, y;
427  int *image_line;
428  int frame_index;
429  const int numcomps = image->numcomps;
430  uint16_t *frame_ptr = (uint16_t *)frame->data[0];
431 
432  for (compno = 0; compno < numcomps; ++compno) {
433  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
434  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
435  return 0;
436  }
437  }
438 
439  for (compno = 0; compno < numcomps; ++compno) {
440  for (y = 0; y < avctx->height; ++y) {
441  image_line = image->comps[compno].data + y * image->comps[compno].w;
442  frame_index = y * (frame->linesize[0] / 2) + compno;
443  for (x = 0; x < avctx->width; ++x) {
444  image_line[x] = frame_ptr[frame_index] >> 4;
445  frame_index += numcomps;
446  }
447  for (; x < image->comps[compno].w; ++x) {
448  image_line[x] = image_line[x - 1];
449  }
450  }
451  for (; y < image->comps[compno].h; ++y) {
452  image_line = image->comps[compno].data + y * image->comps[compno].w;
453  for (x = 0; x < image->comps[compno].w; ++x) {
454  image_line[x] = image_line[x - image->comps[compno].w];
455  }
456  }
457  }
458 
459  return 1;
460 }
461 
462 static int libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
463 {
464  int compno;
465  int x;
466  int y;
467  int *image_line;
468  int frame_index;
469  const int numcomps = image->numcomps;
470  uint16_t *frame_ptr = (uint16_t*)frame->data[0];
471 
472  for (compno = 0; compno < numcomps; ++compno) {
473  if (image->comps[compno].w > frame->linesize[0] / numcomps) {
474  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
475  return 0;
476  }
477  }
478 
479  for (compno = 0; compno < numcomps; ++compno) {
480  for (y = 0; y < avctx->height; ++y) {
481  image_line = image->comps[compno].data + y * image->comps[compno].w;
482  frame_index = y * (frame->linesize[0] / 2) + compno;
483  for (x = 0; x < avctx->width; ++x) {
484  image_line[x] = frame_ptr[frame_index];
485  frame_index += numcomps;
486  }
487  for (; x < image->comps[compno].w; ++x) {
488  image_line[x] = image_line[x - 1];
489  }
490  }
491  for (; y < image->comps[compno].h; ++y) {
492  image_line = image->comps[compno].data + y * image->comps[compno].w;
493  for (x = 0; x < image->comps[compno].w; ++x) {
494  image_line[x] = image_line[x - image->comps[compno].w];
495  }
496  }
497  }
498 
499  return 1;
500 }
501 
502 static int libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
503 {
504  int compno;
505  int x;
506  int y;
507  int width;
508  int height;
509  int *image_line;
510  int frame_index;
511  const int numcomps = image->numcomps;
512 
513  for (compno = 0; compno < numcomps; ++compno) {
514  if (image->comps[compno].w > frame->linesize[compno]) {
515  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
516  return 0;
517  }
518  }
519 
520  for (compno = 0; compno < numcomps; ++compno) {
521  width = avctx->width / image->comps[compno].dx;
522  height = avctx->height / image->comps[compno].dy;
523  for (y = 0; y < height; ++y) {
524  image_line = image->comps[compno].data + y * image->comps[compno].w;
525  frame_index = y * frame->linesize[compno];
526  for (x = 0; x < width; ++x)
527  image_line[x] = frame->data[compno][frame_index++];
528  for (; x < image->comps[compno].w; ++x) {
529  image_line[x] = image_line[x - 1];
530  }
531  }
532  for (; y < image->comps[compno].h; ++y) {
533  image_line = image->comps[compno].data + y * image->comps[compno].w;
534  for (x = 0; x < image->comps[compno].w; ++x) {
535  image_line[x] = image_line[x - image->comps[compno].w];
536  }
537  }
538  }
539 
540  return 1;
541 }
542 
543 static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
544 {
545  int compno;
546  int x;
547  int y;
548  int width;
549  int height;
550  int *image_line;
551  int frame_index;
552  const int numcomps = image->numcomps;
553  uint16_t *frame_ptr;
554 
555  for (compno = 0; compno < numcomps; ++compno) {
556  if (image->comps[compno].w > frame->linesize[compno]) {
557  av_log(avctx, AV_LOG_ERROR, "Error: frame's linesize is too small for the image\n");
558  return 0;
559  }
560  }
561 
562  for (compno = 0; compno < numcomps; ++compno) {
563  width = avctx->width / image->comps[compno].dx;
564  height = avctx->height / image->comps[compno].dy;
565  frame_ptr = (uint16_t *)frame->data[compno];
566  for (y = 0; y < height; ++y) {
567  image_line = image->comps[compno].data + y * image->comps[compno].w;
568  frame_index = y * (frame->linesize[compno] / 2);
569  for (x = 0; x < width; ++x)
570  image_line[x] = frame_ptr[frame_index++];
571  for (; x < image->comps[compno].w; ++x) {
572  image_line[x] = image_line[x - 1];
573  }
574  }
575  for (; y < image->comps[compno].h; ++y) {
576  image_line = image->comps[compno].data + y * image->comps[compno].w;
577  for (x = 0; x < image->comps[compno].w; ++x) {
578  image_line[x] = image_line[x - image->comps[compno].w];
579  }
580  }
581  }
582 
583  return 1;
584 }
585 
587  const AVFrame *frame, int *got_packet)
588 {
589  LibOpenJPEGContext *ctx = avctx->priv_data;
590  opj_image_t *image = ctx->image;
591 #if OPENJPEG_MAJOR_VERSION == 1
592  opj_cinfo_t *compress = NULL;
593  opj_cio_t *stream = NULL;
594  int len;
595 #else // OPENJPEG_MAJOR_VERSION == 2
596  opj_codec_t *compress = NULL;
597  opj_stream_t *stream = NULL;
598  PacketWriter writer = { 0 };
599 #endif // OPENJPEG_MAJOR_VERSION == 1
600  int cpyresult = 0;
601  int ret;
602  AVFrame *gbrframe;
603 
604  switch (avctx->pix_fmt) {
605  case AV_PIX_FMT_RGB24:
606  case AV_PIX_FMT_RGBA:
607  case AV_PIX_FMT_YA8:
608  cpyresult = libopenjpeg_copy_packed8(avctx, frame, image);
609  break;
610  case AV_PIX_FMT_XYZ12:
611  cpyresult = libopenjpeg_copy_packed12(avctx, frame, image);
612  break;
613  case AV_PIX_FMT_RGB48:
614  case AV_PIX_FMT_RGBA64:
615  case AV_PIX_FMT_YA16:
616  cpyresult = libopenjpeg_copy_packed16(avctx, frame, image);
617  break;
618  case AV_PIX_FMT_GBR24P:
619  case AV_PIX_FMT_GBRP9:
620  case AV_PIX_FMT_GBRP10:
621  case AV_PIX_FMT_GBRP12:
622  case AV_PIX_FMT_GBRP14:
623  case AV_PIX_FMT_GBRP16:
624  gbrframe = av_frame_clone(frame);
625  if (!gbrframe)
626  return AVERROR(ENOMEM);
627  gbrframe->data[0] = frame->data[2]; // swap to be rgb
628  gbrframe->data[1] = frame->data[0];
629  gbrframe->data[2] = frame->data[1];
630  gbrframe->linesize[0] = frame->linesize[2];
631  gbrframe->linesize[1] = frame->linesize[0];
632  gbrframe->linesize[2] = frame->linesize[1];
633  if (avctx->pix_fmt == AV_PIX_FMT_GBR24P) {
634  cpyresult = libopenjpeg_copy_unpacked8(avctx, gbrframe, image);
635  } else {
636  cpyresult = libopenjpeg_copy_unpacked16(avctx, gbrframe, image);
637  }
638  av_frame_free(&gbrframe);
639  break;
640  case AV_PIX_FMT_GRAY8:
641  case AV_PIX_FMT_YUV410P:
642  case AV_PIX_FMT_YUV411P:
643  case AV_PIX_FMT_YUV420P:
644  case AV_PIX_FMT_YUV422P:
645  case AV_PIX_FMT_YUV440P:
646  case AV_PIX_FMT_YUV444P:
647  case AV_PIX_FMT_YUVA420P:
648  case AV_PIX_FMT_YUVA422P:
649  case AV_PIX_FMT_YUVA444P:
650  cpyresult = libopenjpeg_copy_unpacked8(avctx, frame, image);
651  break;
652  case AV_PIX_FMT_GRAY16:
653  case AV_PIX_FMT_YUV420P9:
654  case AV_PIX_FMT_YUV422P9:
655  case AV_PIX_FMT_YUV444P9:
677  cpyresult = libopenjpeg_copy_unpacked16(avctx, frame, image);
678  break;
679  default:
680  av_log(avctx, AV_LOG_ERROR,
681  "The frame's pixel format '%s' is not supported\n",
682  av_get_pix_fmt_name(avctx->pix_fmt));
683  return AVERROR(EINVAL);
684  break;
685  }
686 
687  if (!cpyresult) {
688  av_log(avctx, AV_LOG_ERROR,
689  "Could not copy the frame data to the internal image buffer\n");
690  return -1;
691  }
692 
693 #if OPENJPEG_MAJOR_VERSION == 2
694  if ((ret = ff_alloc_packet2(avctx, pkt, 1024, 0)) < 0) {
695  return ret;
696  }
697 #endif // OPENJPEG_MAJOR_VERSION == 2
698 
699  compress = opj_create_compress(ctx->format);
700  if (!compress) {
701  av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
702  ret = AVERROR(ENOMEM);
703  goto done;
704  }
705 
706 #if OPENJPEG_MAJOR_VERSION == 1
707  opj_setup_encoder(compress, &ctx->enc_params, image);
708  stream = opj_cio_open((opj_common_ptr) compress, NULL, 0);
709 #else // OPENJPEG_MAJOR_VERSION == 2
710  if (!opj_set_error_handler(compress, error_callback, avctx) ||
711  !opj_set_warning_handler(compress, warning_callback, avctx) ||
712  !opj_set_info_handler(compress, info_callback, avctx)) {
713  av_log(avctx, AV_LOG_ERROR, "Error setting the compressor handlers\n");
714  ret = AVERROR_EXTERNAL;
715  goto done;
716  }
717 
718  if (!opj_setup_encoder(compress, &ctx->enc_params, image)) {
719  av_log(avctx, AV_LOG_ERROR, "Error setting up the compressor\n");
720  ret = AVERROR_EXTERNAL;
721  goto done;
722  }
723  stream = opj_stream_default_create(OPJ_STREAM_WRITE);
724 #endif // OPENJPEG_MAJOR_VERSION == 1
725 
726  if (!stream) {
727  av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
728  ret = AVERROR(ENOMEM);
729  goto done;
730  }
731 #if OPENJPEG_MAJOR_VERSION == 1
732  memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
733  ctx->event_mgr.info_handler = info_callback;
734  ctx->event_mgr.error_handler = error_callback;
735  ctx->event_mgr.warning_handler = warning_callback;
736  opj_set_event_mgr((opj_common_ptr) compress, &ctx->event_mgr, avctx);
737  if (!opj_encode(compress, stream, image, NULL)) {
738  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
739  ret = AVERROR_EXTERNAL;
740  goto done;
741  }
742 
743  len = cio_tell(stream);
744  if ((ret = ff_alloc_packet2(avctx, pkt, len, 0)) < 0) {
745  goto done;
746  }
747 
748  memcpy(pkt->data, stream->buffer, len);
749 #else // OPENJPEG_MAJOR_VERSION == 2
750  writer.packet = pkt;
751  opj_stream_set_write_function(stream, stream_write);
752  opj_stream_set_skip_function(stream, stream_skip);
753  opj_stream_set_seek_function(stream, stream_seek);
754 #if HAVE_OPENJPEG_2_1_OPENJPEG_H
755  opj_stream_set_user_data(stream, &writer, NULL);
756 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
757  opj_stream_set_user_data(stream, &writer);
758 #else
759 #error Missing call to opj_stream_set_user_data
760 #endif
761 
762  if (!opj_start_compress(compress, ctx->image, stream) ||
763  !opj_encode(compress, stream) ||
764  !opj_end_compress(compress, stream)) {
765  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
766  ret = AVERROR_EXTERNAL;
767  goto done;
768  }
769 
770  av_shrink_packet(pkt, writer.pos);
771 #endif // OPENJPEG_MAJOR_VERSION == 1
772 
773  pkt->flags |= AV_PKT_FLAG_KEY;
774  *got_packet = 1;
775  ret = 0;
776 
777 done:
778 #if OPENJPEG_MAJOR_VERSION == 2
779  opj_stream_destroy(stream);
780  opj_destroy_codec(compress);
781 #else
782  opj_cio_close(stream);
783  opj_destroy_compress(compress);
784 #endif
785  return ret;
786 }
787 
789 {
790  LibOpenJPEGContext *ctx = avctx->priv_data;
791 
792  opj_image_destroy(ctx->image);
793  ctx->image = NULL;
794  return 0;
795 }
796 
797 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
798 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
799 static const AVOption options[] = {
800  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = OPJ(CODEC_JP2) }, OPJ(CODEC_J2K), OPJ(CODEC_JP2), VE, "format" },
801  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_J2K) }, 0, 0, VE, "format" },
802  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CODEC_JP2) }, 0, 0, VE, "format" },
803  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = OPJ(STD_RSIZ) }, OPJ(STD_RSIZ), OPJ(CINEMA4K), VE, "profile" },
804  { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(STD_RSIZ) }, 0, 0, VE, "profile" },
805  { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K) }, 0, 0, VE, "profile" },
806  { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K) }, 0, 0, VE, "profile" },
807  { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OPJ(OFF) }, OPJ(OFF), OPJ(CINEMA4K_24), VE, "cinema_mode" },
808  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(OFF) }, 0, 0, VE, "cinema_mode" },
809  { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_24) }, 0, 0, VE, "cinema_mode" },
810  { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA2K_48) }, 0, 0, VE, "cinema_mode" },
811  { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CINEMA4K_24) }, 0, 0, VE, "cinema_mode" },
812  { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = OPJ(LRCP) }, OPJ(LRCP), OPJ(CPRL), VE, "prog_order" },
813  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(LRCP) }, 0, 0, VE, "prog_order" },
814  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RLCP) }, 0, 0, VE, "prog_order" },
815  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(RPCL) }, 0, 0, VE, "prog_order" },
816  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(PCRL) }, 0, 0, VE, "prog_order" },
817  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OPJ(CPRL) }, 0, 0, VE, "prog_order" },
818  { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, INT_MAX, VE },
819  { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
820  { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
821  { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
822  { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
823  { NULL },
824 };
825 
826 static const AVClass openjpeg_class = {
827  .class_name = "libopenjpeg",
828  .item_name = av_default_item_name,
829  .option = options,
830  .version = LIBAVUTIL_VERSION_INT,
831 };
832 
834  .name = "libopenjpeg",
835  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
836  .type = AVMEDIA_TYPE_VIDEO,
837  .id = AV_CODEC_ID_JPEG2000,
838  .priv_data_size = sizeof(LibOpenJPEGContext),
840  .encode2 = libopenjpeg_encode_frame,
841  .close = libopenjpeg_encode_close,
843  .pix_fmts = (const enum AVPixelFormat[]) {
861  },
862  .priv_class = &openjpeg_class,
863 };
#define NULL
Definition: coverity.c:32
#define AV_PIX_FMT_YUVA422P16
Definition: pixfmt.h:374
static int libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
#define AV_PIX_FMT_YUVA422P9
Definition: pixfmt.h:368
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2193
This structure describes decoded (raw) audio or video data.
Definition: frame.h:180
AVOption.
Definition: opt.h:245
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
#define AV_PIX_FMT_YUVA420P10
Definition: pixfmt.h:370
#define AV_PIX_FMT_YUV444P14
Definition: pixfmt.h:348
8bit gray, 8bit alpha
Definition: pixfmt.h:154
AVFormatContext * ctx
Definition: movenc-test.c:48
#define AV_PIX_FMT_YUVA422P10
Definition: pixfmt.h:371
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:70
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
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
#define AV_PIX_FMT_RGBA64
Definition: pixfmt.h:328
AVCodec ff_libopenjpeg_encoder
int size
Definition: avcodec.h:1575
#define AV_PIX_FMT_GBRP10
Definition: pixfmt.h:354
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
Definition: ffplay.c:1410
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1867
static const AVClass openjpeg_class
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:342
static int libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
static AVPacket pkt
AVCodec.
Definition: avcodec.h:3531
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:92
static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
#define AV_CODEC_CAP_INTRA_ONLY
Codec is intra only.
Definition: avcodec.h:1033
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
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:102
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:367
#define AV_PIX_FMT_GBRP9
Definition: pixfmt.h:353
static void warning_callback(const char *msg, void *data)
static AVFrame * frame
uint8_t * data
Definition: avcodec.h:1574
#define AV_PIX_FMT_YUV444P16
Definition: pixfmt.h:351
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:343
#define AV_PIX_FMT_YUVA420P16
Definition: pixfmt.h:373
#define av_log(a,...)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1606
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition: pixfmt.h:187
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
av_default_item_name
#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:153
#define CODEC_J2K
Definition: j2kenc.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:176
AVS_Value void * user_data
Definition: avisynth_c.h:562
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define AV_PIX_FMT_YUVA444P16
Definition: pixfmt.h:375
#define AV_PIX_FMT_YA16
Definition: pixfmt.h:323
#define AV_PIX_FMT_RGB48
Definition: pixfmt.h:324
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)
opj_image_t * image
const char * name
Name of the codec implementation.
Definition: avcodec.h:3538
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:341
#define FFMAX(a, b)
Definition: common.h:94
#define fail()
Definition: checkasm.h:81
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:1013
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1580
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:336
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:357
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:322
#define VE
int width
picture width / height.
Definition: avcodec.h:1826
#define CODEC_JP2
Definition: j2kenc.c:79
#define AV_PIX_FMT_YUVA444P10
Definition: pixfmt.h:372
#define AV_PIX_FMT_YUV444P9
Definition: pixfmt.h:337
#define AV_PIX_FMT_GBRP14
Definition: pixfmt.h:356
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:468
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
#define AV_PIX_FMT_YUV420P16
Definition: pixfmt.h:349
#define AV_PIX_FMT_YUV420P14
Definition: pixfmt.h:346
Libavcodec external API header.
int compression_level
Definition: avcodec.h:1726
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:211
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition: pixfmt.h:188
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:1639
static const char * format
Definition: movenc-test.c:47
static int libopenjpeg_copy_packed12(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:338
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:377
Describe the class of an AVClass context structure.
Definition: log.h:67
opj_event_mgr_t event_mgr
#define OPJ(x)
#define AV_PIX_FMT_YUV420P9
Definition: pixfmt.h:335
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: utils.c:1683
#define AV_PIX_FMT_GBR24P
Definition: pixfmt.h:307
static void info_callback(const char *msg, void *data)
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:262
mfxU16 profile
Definition: qsvenc.c:42
#define AV_PIX_FMT_YUV422P14
Definition: pixfmt.h:347
#define AV_PIX_FMT_GBRP12
Definition: pixfmt.h:355
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1720
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:339
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:345
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:194
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:369
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:725
void * priv_data
Definition: avcodec.h:1681
int len
static const AVOption options[]
static void error_callback(const char *msg, void *data)
#define OFFSET(x)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition: pixfmt.h:100
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:2109
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:1551
#define AV_PIX_FMT_YUV422P16
Definition: pixfmt.h:350
for(j=16;j >0;--j)
static int width