libopenjpegenc.c
Go to the documentation of this file.
1 /*
2  * JPEG 2000 encoding support via OpenJPEG
3  * Copyright (c) 2011 Michael Bradshaw <mbradshaw@sorensonmedia.com>
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
27 #define OPJ_STATIC
28 #include <openjpeg.h>
29 
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 typedef struct {
39  opj_image_t *image;
40  opj_cparameters_t enc_params;
41  opj_cinfo_t *compress;
42  opj_event_mgr_t event_mgr;
43  int format;
44  int profile;
48  int numlayers;
53 
54 static void error_callback(const char *msg, void *data)
55 {
56  av_log(data, AV_LOG_ERROR, "%s\n", msg);
57 }
58 
59 static void warning_callback(const char *msg, void *data)
60 {
61  av_log(data, AV_LOG_WARNING, "%s\n", msg);
62 }
63 
64 static void info_callback(const char *msg, void *data)
65 {
66  av_log(data, AV_LOG_DEBUG, "%s\n", msg);
67 }
68 
69 static opj_image_t *libopenjpeg_create_image(AVCodecContext *avctx,
70  opj_cparameters_t *parameters)
71 {
72  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt);
73  opj_image_cmptparm_t *cmptparm;
74  OPJ_COLOR_SPACE color_space;
75  opj_image_t *img;
76  int i;
77  int sub_dx[4];
78  int sub_dy[4];
79  int numcomps = desc->nb_components;
80 
81  sub_dx[0] =
82  sub_dx[3] = 1;
83  sub_dy[0] =
84  sub_dy[3] = 1;
85  sub_dx[1] =
86  sub_dx[2] = 1 << desc->log2_chroma_w;
87  sub_dy[1] =
88  sub_dy[2] = 1 << desc->log2_chroma_h;
89 
90  switch (avctx->pix_fmt) {
91  case AV_PIX_FMT_GRAY8:
92  case AV_PIX_FMT_GRAY16:
93  case AV_PIX_FMT_Y400A:
94  color_space = CLRSPC_GRAY;
95  break;
96  case AV_PIX_FMT_RGB24:
97  case AV_PIX_FMT_RGBA:
98  case AV_PIX_FMT_RGB48:
99  color_space = CLRSPC_SRGB;
100  break;
101  case AV_PIX_FMT_YUV410P:
102  case AV_PIX_FMT_YUV411P:
103  case AV_PIX_FMT_YUV420P:
104  case AV_PIX_FMT_YUV422P:
105  case AV_PIX_FMT_YUV440P:
106  case AV_PIX_FMT_YUV444P:
107  case AV_PIX_FMT_YUVA420P:
108  case AV_PIX_FMT_YUV420P9:
109  case AV_PIX_FMT_YUV422P9:
110  case AV_PIX_FMT_YUV444P9:
117  color_space = CLRSPC_SYCC;
118  break;
119  default:
120  av_log(avctx, AV_LOG_ERROR,
121  "The requested pixel format '%s' is not supported\n",
122  av_get_pix_fmt_name(avctx->pix_fmt));
123  return NULL;
124  }
125 
126  cmptparm = av_mallocz(numcomps * sizeof(*cmptparm));
127  if (!cmptparm) {
128  av_log(avctx, AV_LOG_ERROR, "Not enough memory");
129  return NULL;
130  }
131 
132  for (i = 0; i < numcomps; i++) {
133  cmptparm[i].prec = desc->comp[i].depth_minus1 + 1;
134  cmptparm[i].bpp = desc->comp[i].depth_minus1 + 1;
135  cmptparm[i].sgnd = 0;
136  cmptparm[i].dx = sub_dx[i];
137  cmptparm[i].dy = sub_dy[i];
138  cmptparm[i].w = avctx->width / sub_dx[i];
139  cmptparm[i].h = avctx->height / sub_dy[i];
140  }
141 
142  img = opj_image_create(numcomps, cmptparm, color_space);
143  av_freep(&cmptparm);
144  return img;
145 }
146 
148 {
149  LibOpenJPEGContext *ctx = avctx->priv_data;
150  int err = AVERROR(ENOMEM);
151 
152  opj_set_default_encoder_parameters(&ctx->enc_params);
153 
154  ctx->enc_params.cp_rsiz = ctx->profile;
155  ctx->enc_params.mode = !!avctx->global_quality;
156  ctx->enc_params.cp_cinema = ctx->cinema_mode;
157  ctx->enc_params.prog_order = ctx->prog_order;
158  ctx->enc_params.numresolution = ctx->numresolution;
159  ctx->enc_params.cp_disto_alloc = ctx->disto_alloc;
160  ctx->enc_params.cp_fixed_alloc = ctx->fixed_alloc;
161  ctx->enc_params.cp_fixed_quality = ctx->fixed_quality;
162  ctx->enc_params.tcp_numlayers = ctx->numlayers;
163  ctx->enc_params.tcp_rates[0] = FFMAX(avctx->compression_level, 0) * 2;
164 
165  ctx->compress = opj_create_compress(ctx->format);
166  if (!ctx->compress) {
167  av_log(avctx, AV_LOG_ERROR, "Error creating the compressor\n");
168  return AVERROR(ENOMEM);
169  }
170 
171  avctx->coded_frame = avcodec_alloc_frame();
172  if (!avctx->coded_frame) {
173  av_log(avctx, AV_LOG_ERROR, "Error allocating coded frame\n");
174  goto fail;
175  }
176 
177  ctx->image = libopenjpeg_create_image(avctx, &ctx->enc_params);
178  if (!ctx->image) {
179  av_log(avctx, AV_LOG_ERROR, "Error creating the mj2 image\n");
180  err = AVERROR(EINVAL);
181  goto fail;
182  }
183 
184  ctx->event_mgr.info_handler = info_callback;
185  ctx->event_mgr.error_handler = error_callback;
186  ctx->event_mgr.warning_handler = warning_callback;
187  opj_set_event_mgr((opj_common_ptr)ctx->compress, &ctx->event_mgr, avctx);
188 
189  return 0;
190 
191 fail:
192  av_freep(&ctx->compress);
193  av_freep(&avctx->coded_frame);
194  return err;
195 }
196 
198  const AVFrame *frame, opj_image_t *image)
199 {
200  int compno;
201  int x, y;
202  int image_index, frame_index;
203  const int numcomps = image->numcomps;
204 
205  for (compno = 0; compno < numcomps; ++compno) {
206  for (y = 0; y < avctx->height; ++y) {
207  image_index = y * avctx->width;
208  frame_index = y * frame->linesize[0] + compno;
209  for (x = 0; x < avctx->width; ++x) {
210  image->comps[compno].data[image_index++] =
211  frame->data[0][frame_index];
212  frame_index += numcomps;
213  }
214  }
215  }
216 }
217 
219  const AVFrame *frame, opj_image_t *image)
220 {
221  int compno;
222  int x, y;
223  int image_index, frame_index;
224  const int numcomps = image->numcomps;
225  uint16_t *frame_ptr = (uint16_t*)frame->data[0];
226 
227  for (compno = 0; compno < numcomps; ++compno) {
228  for (y = 0; y < avctx->height; ++y) {
229  image_index = y * avctx->width;
230  frame_index = y * (frame->linesize[0] / 2) + compno;
231  for (x = 0; x < avctx->width; ++x) {
232  image->comps[compno].data[image_index++] =
233  frame_ptr[frame_index];
234  frame_index += numcomps;
235  }
236  }
237  }
238 }
239 
241  const AVFrame *frame, opj_image_t *image)
242 {
243  int compno;
244  int x, y;
245  int width, height;
246  int image_index, frame_index;
247  const int numcomps = image->numcomps;
248 
249  for (compno = 0; compno < numcomps; ++compno) {
250  width = avctx->width / image->comps[compno].dx;
251  height = avctx->height / image->comps[compno].dy;
252  for (y = 0; y < height; ++y) {
253  image_index = y * width;
254  frame_index = y * frame->linesize[compno];
255  for (x = 0; x < width; ++x)
256  image->comps[compno].data[image_index++] =
257  frame->data[compno][frame_index++];
258  }
259  }
260 }
261 
263  const AVFrame *frame,
264  opj_image_t *image)
265 {
266  int compno;
267  int x, y;
268  int width, height;
269  int image_index, frame_index;
270  const int numcomps = image->numcomps;
271  uint16_t *frame_ptr;
272 
273  for (compno = 0; compno < numcomps; ++compno) {
274  width = avctx->width / image->comps[compno].dx;
275  height = avctx->height / image->comps[compno].dy;
276  frame_ptr = (uint16_t*)frame->data[compno];
277  for (y = 0; y < height; ++y) {
278  image_index = y * width;
279  frame_index = y * (frame->linesize[compno] / 2);
280  for (x = 0; x < width; ++x)
281  image->comps[compno].data[image_index++] =
282  frame_ptr[frame_index++];
283  }
284  }
285 }
286 
288  const AVFrame *frame, int *got_packet)
289 {
290  LibOpenJPEGContext *ctx = avctx->priv_data;
291  opj_cinfo_t *compress = ctx->compress;
292  opj_image_t *image = ctx->image;
293  opj_cio_t *stream;
294  int ret, len;
295 
296  // x0, y0 is the top left corner of the image
297  // x1, y1 is the width, height of the reference grid
298  image->x0 = 0;
299  image->y0 = 0;
300  image->x1 = (avctx->width - 1) * ctx->enc_params.subsampling_dx + 1;
301  image->y1 = (avctx->height - 1) * ctx->enc_params.subsampling_dy + 1;
302 
303  switch (avctx->pix_fmt) {
304  case AV_PIX_FMT_RGB24:
305  case AV_PIX_FMT_RGBA:
306  case AV_PIX_FMT_Y400A:
307  libopenjpeg_copy_packed8(avctx, frame, image);
308  break;
309  case AV_PIX_FMT_RGB48:
310  libopenjpeg_copy_packed16(avctx, frame, image);
311  break;
312  case AV_PIX_FMT_GRAY8:
313  case AV_PIX_FMT_YUV410P:
314  case AV_PIX_FMT_YUV411P:
315  case AV_PIX_FMT_YUV420P:
316  case AV_PIX_FMT_YUV422P:
317  case AV_PIX_FMT_YUV440P:
318  case AV_PIX_FMT_YUV444P:
319  case AV_PIX_FMT_YUVA420P:
320  libopenjpeg_copy_unpacked8(avctx, frame, image);
321  break;
322  case AV_PIX_FMT_GRAY16:
323  case AV_PIX_FMT_YUV420P9:
324  case AV_PIX_FMT_YUV422P9:
325  case AV_PIX_FMT_YUV444P9:
332  libopenjpeg_copy_unpacked16(avctx, frame, image);
333  break;
334  default:
335  av_log(avctx, AV_LOG_ERROR,
336  "The frame's pixel format '%s' is not supported\n",
337  av_get_pix_fmt_name(avctx->pix_fmt));
338  return AVERROR(EINVAL);
339  break;
340  }
341 
342  opj_setup_encoder(compress, &ctx->enc_params, image);
343  stream = opj_cio_open((opj_common_ptr)compress, NULL, 0);
344  if (!stream) {
345  av_log(avctx, AV_LOG_ERROR, "Error creating the cio stream\n");
346  return AVERROR(ENOMEM);
347  }
348 
349  if (!opj_encode(compress, stream, image, NULL)) {
350  opj_cio_close(stream);
351  av_log(avctx, AV_LOG_ERROR, "Error during the opj encode\n");
352  return -1;
353  }
354 
355  len = cio_tell(stream);
356  if ((ret = ff_alloc_packet(pkt, len)) < 0) {
357  opj_cio_close(stream);
358  return ret;
359  }
360 
361  memcpy(pkt->data, stream->buffer, len);
362  pkt->flags |= AV_PKT_FLAG_KEY;
363  *got_packet = 1;
364  opj_cio_close(stream);
365  return 0;
366 }
367 
369 {
370  LibOpenJPEGContext *ctx = avctx->priv_data;
371 
372  opj_destroy_compress(ctx->compress);
373  opj_image_destroy(ctx->image);
374  av_freep(&avctx->coded_frame);
375  return 0;
376 }
377 
378 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
379 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
380 static const AVOption options[] = {
381  { "format", "Codec Format", OFFSET(format), AV_OPT_TYPE_INT, { .i64 = CODEC_JP2 }, CODEC_J2K, CODEC_JP2, VE, "format" },
382  { "j2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_J2K }, 0, 0, VE, "format" },
383  { "jp2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CODEC_JP2 }, 0, 0, VE, "format" },
384  { "profile", NULL, OFFSET(profile), AV_OPT_TYPE_INT, { .i64 = STD_RSIZ }, STD_RSIZ, CINEMA4K, VE, "profile" },
385  { "jpeg2000", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = STD_RSIZ }, 0, 0, VE, "profile" },
386  { "cinema2k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K }, 0, 0, VE, "profile" },
387  { "cinema4k", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K }, 0, 0, VE, "profile" },
388  { "cinema_mode", "Digital Cinema", OFFSET(cinema_mode), AV_OPT_TYPE_INT, { .i64 = OFF }, OFF, CINEMA4K_24, VE, "cinema_mode" },
389  { "off", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = OFF }, 0, 0, VE, "cinema_mode" },
390  { "2k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_24 }, 0, 0, VE, "cinema_mode" },
391  { "2k_48", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA2K_48 }, 0, 0, VE, "cinema_mode" },
392  { "4k_24", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CINEMA4K_24 }, 0, 0, VE, "cinema_mode" },
393  { "prog_order", "Progression Order", OFFSET(prog_order), AV_OPT_TYPE_INT, { .i64 = LRCP }, LRCP, CPRL, VE, "prog_order" },
394  { "lrcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = LRCP }, 0, 0, VE, "prog_order" },
395  { "rlcp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RLCP }, 0, 0, VE, "prog_order" },
396  { "rpcl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = RPCL }, 0, 0, VE, "prog_order" },
397  { "pcrl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = PCRL }, 0, 0, VE, "prog_order" },
398  { "cprl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPRL }, 0, 0, VE, "prog_order" },
399  { "numresolution", NULL, OFFSET(numresolution), AV_OPT_TYPE_INT, { .i64 = 6 }, 1, 10, VE },
400  { "numlayers", NULL, OFFSET(numlayers), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 10, VE },
401  { "disto_alloc", NULL, OFFSET(disto_alloc), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, VE },
402  { "fixed_alloc", NULL, OFFSET(fixed_alloc), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
403  { "fixed_quality", NULL, OFFSET(fixed_quality), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
404  { NULL },
405 };
406 
407 static const AVClass class = {
408  .class_name = "libopenjpeg",
409  .item_name = av_default_item_name,
410  .option = options,
412 };
413 
415  .name = "libopenjpeg",
416  .type = AVMEDIA_TYPE_VIDEO,
417  .id = AV_CODEC_ID_JPEG2000,
418  .priv_data_size = sizeof(LibOpenJPEGContext),
420  .encode2 = libopenjpeg_encode_frame,
422  .capabilities = 0,
423  .pix_fmts = (const enum AVPixelFormat[]) {
433  },
434  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
435  .priv_class = &class,
436 };
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
AVOption.
Definition: opt.h:233
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
packed RGB 8:8:8, 24bpp, RGBRGB...
Definition: pixfmt.h:67
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2725
AVCodec ff_libopenjpeg_encoder
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
8bit gray, 8bit alpha
Definition: pixfmt.h:138
AVCodec.
Definition: avcodec.h:2960
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
static av_cold int libopenjpeg_encode_close(AVCodecContext *avctx)
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:151
opj_cparameters_t enc_params
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:38
opj_cinfo_t * compress
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:83
static opj_image_t * libopenjpeg_create_image(AVCodecContext *avctx, opj_cparameters_t *parameters)
static void libopenjpeg_copy_packed16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
AVOptions.
static void warning_callback(const char *msg, void *data)
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:915
static int init(AVCodecParserContext *s)
Definition: h264_parser.c:335
uint16_t depth_minus1
number of bits in the component minus 1
Definition: pixdesc.h:43
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:88
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
opj_image_t * image
const char * name
Name of the codec implementation.
Definition: avcodec.h:2967
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
Definition: pixfmt.h:94
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:57
#define VE
int width
picture width / height.
Definition: avcodec.h:1508
int ff_alloc_packet(AVPacket *avpkt, int size)
Check AVPacket size and/or allocate data.
Definition: utils.c:880
static void libopenjpeg_copy_packed8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
static av_cold int libopenjpeg_encode_init(AVCodecContext *avctx)
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
external API header
int compression_level
Definition: avcodec.h:1426
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
Definition: avcodec.h:1008
av_default_item_name
Definition: dnxhdenc.c:43
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
main external API structure.
Definition: avcodec.h:1339
static void close(AVCodecParserContext *s)
Definition: h264_parser.c:326
static void libopenjpeg_copy_unpacked16(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
opj_event_mgr_t event_mgr
static void libopenjpeg_copy_unpacked8(AVCodecContext *avctx, const AVFrame *frame, opj_image_t *image)
static void info_callback(const char *msg, void *data)
int global_quality
Global quality for codecs which cannot change it per frame.
Definition: avcodec.h:1420
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: avcodec.h:997
int height
Definition: gxfenc.c:72
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:65
Y , 8bpp.
Definition: pixfmt.h:73
common internal api header.
common internal and external API header
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:72
void * priv_data
Definition: avcodec.h:1382
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:1377
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:898
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:158
for(j=16;j >0;--j)