Libav
libopenjpegdec.c
Go to the documentation of this file.
1 /*
2  * JPEG 2000 decoding support via OpenJPEG
3  * Copyright (c) 2009 Jaikrishnan Menon <realityman@gmx.net>
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 "libavutil/pixfmt.h"
35 
36 #include "avcodec.h"
37 #include "internal.h"
38 #include "thread.h"
39 
40 #define JP2_SIG_TYPE 0x6A502020
41 #define JP2_SIG_VALUE 0x0D0A870A
42 
43 // pix_fmts with lower bpp have to be listed before
44 // similar pix_fmts with higher bpp.
45 #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, \
46  AV_PIX_FMT_RGB48, AV_PIX_FMT_RGBA64
47 
48 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8, AV_PIX_FMT_YA8, \
49  AV_PIX_FMT_GRAY16
50 
51 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, \
52  AV_PIX_FMT_YUVA420P, \
53  AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, \
54  AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, \
55  AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, \
56  AV_PIX_FMT_YUV444P9, \
57  AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, \
58  AV_PIX_FMT_YUV444P10, \
59  AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, \
60  AV_PIX_FMT_YUV444P16
61 
62 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
63 
64 static const enum AVPixelFormat rgb_pix_fmts[] = {
66 };
67 static const enum AVPixelFormat gray_pix_fmts[] = {
69 };
70 static const enum AVPixelFormat yuv_pix_fmts[] = {
72 };
73 static const enum AVPixelFormat any_pix_fmts[] = {
75 };
76 
77 typedef struct {
78  AVClass *class;
79  opj_dparameters_t dec_params;
80  int lowres;
81  int lowqual;
83 
84 static int libopenjpeg_matches_pix_fmt(const opj_image_t *img,
86 {
87  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
88  int match = 1;
89 
90  if (desc->nb_components != img->numcomps) {
91  return 0;
92  }
93 
94  switch (desc->nb_components) {
95  case 4:
96  match = match &&
97  desc->comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
98  1 == img->comps[3].dx &&
99  1 == img->comps[3].dy;
100  case 3:
101  match = match &&
102  desc->comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
103  1 << desc->log2_chroma_w == img->comps[2].dx &&
104  1 << desc->log2_chroma_h == img->comps[2].dy;
105  case 2:
106  match = match &&
107  desc->comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
108  1 << desc->log2_chroma_w == img->comps[1].dx &&
109  1 << desc->log2_chroma_h == img->comps[1].dy;
110  case 1:
111  match = match &&
112  desc->comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
113  1 == img->comps[0].dx &&
114  1 == img->comps[0].dy;
115  default:
116  break;
117  }
118 
119  return match;
120 }
121 
122 static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
123 {
124  int index;
125  const enum AVPixelFormat *possible_fmts = NULL;
126  int possible_fmts_nb = 0;
127 
128  switch (image->color_space) {
129  case CLRSPC_SRGB:
130  possible_fmts = rgb_pix_fmts;
131  possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
132  break;
133  case CLRSPC_GRAY:
134  possible_fmts = gray_pix_fmts;
135  possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
136  break;
137  case CLRSPC_SYCC:
138  possible_fmts = yuv_pix_fmts;
139  possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
140  break;
141  default:
142  possible_fmts = any_pix_fmts;
143  possible_fmts_nb = FF_ARRAY_ELEMS(any_pix_fmts);
144  break;
145  }
146 
147  for (index = 0; index < possible_fmts_nb; ++index)
148  if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
149  return possible_fmts[index];
150  }
151 
152  return AV_PIX_FMT_NONE;
153 }
154 
156 {
157  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
158  int i, component_plane;
159 
160  if (pix_fmt == AV_PIX_FMT_GRAY16)
161  return 0;
162 
163  component_plane = desc->comp[0].plane;
164  for (i = 1; i < desc->nb_components; i++)
165  if (component_plane != desc->comp[i].plane)
166  return 0;
167  return 1;
168 }
169 
170 static void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image)
171 {
172  uint8_t *img_ptr;
173  int index, x, y, c;
174 
175  for (y = 0; y < picture->height; y++) {
176  index = y * picture->width;
177  img_ptr = picture->data[0] + y * picture->linesize[0];
178  for (x = 0; x < picture->width; x++, index++)
179  for (c = 0; c < image->numcomps; c++)
180  *img_ptr++ = image->comps[c].data[index];
181  }
182 }
183 
184 static void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image)
185 {
186  uint16_t *img_ptr;
187  int index, x, y, c;
188  int adjust[4];
189 
190  for (x = 0; x < image->numcomps; x++)
191  adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
192 
193  for (y = 0; y < picture->height; y++) {
194  index = y * picture->width;
195  img_ptr = (uint16_t *) (picture->data[0] + y * picture->linesize[0]);
196  for (x = 0; x < picture->width; x++, index++)
197  for (c = 0; c < image->numcomps; c++)
198  *img_ptr++ = image->comps[c].data[index] << adjust[c];
199  }
200 }
201 
202 static void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image)
203 {
204  int *comp_data;
205  uint8_t *img_ptr;
206  int index, x, y;
207 
208  for (index = 0; index < image->numcomps; index++) {
209  comp_data = image->comps[index].data;
210  for (y = 0; y < image->comps[index].h; y++) {
211  img_ptr = picture->data[index] + y * picture->linesize[index];
212  for (x = 0; x < image->comps[index].w; x++) {
213  *img_ptr = (uint8_t) *comp_data;
214  img_ptr++;
215  comp_data++;
216  }
217  }
218  }
219 }
220 
221 static void libopenjpeg_copyto16(AVFrame *p, opj_image_t *image)
222 {
223  int *comp_data;
224  uint16_t *img_ptr;
225  int index, x, y;
226 
227  for (index = 0; index < image->numcomps; index++) {
228  comp_data = image->comps[index].data;
229  for (y = 0; y < image->comps[index].h; y++) {
230  img_ptr = (uint16_t *)(p->data[index] + y * p->linesize[index]);
231  for (x = 0; x < image->comps[index].w; x++) {
232  *img_ptr = *comp_data;
233  img_ptr++;
234  comp_data++;
235  }
236  }
237  }
238 }
239 
241 {
242  LibOpenJPEGContext *ctx = avctx->priv_data;
243 
244  opj_set_default_decoder_parameters(&ctx->dec_params);
245  return 0;
246 }
247 
249  void *data, int *got_frame,
250  AVPacket *avpkt)
251 {
252  uint8_t *buf = avpkt->data;
253  int buf_size = avpkt->size;
254  LibOpenJPEGContext *ctx = avctx->priv_data;
255  ThreadFrame frame = { .f = data };
256  AVFrame *picture = data;
257  const AVPixFmtDescriptor *desc;
258  opj_dinfo_t *dec;
259  opj_cio_t *stream;
260  opj_image_t *image;
261  int width, height, ret;
262  int pixel_size = 0;
263  int ispacked = 0;
264  int i;
265 
266  *got_frame = 0;
267 
268  // Check if input is a raw jpeg2k codestream or in jp2 wrapping
269  if ((AV_RB32(buf) == 12) &&
270  (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
271  (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
272  dec = opj_create_decompress(CODEC_JP2);
273  } else {
274  /* If the AVPacket contains a jp2c box, then skip to
275  * the starting byte of the codestream. */
276  if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
277  buf += 8;
278  dec = opj_create_decompress(CODEC_J2K);
279  }
280 
281  if (!dec) {
282  av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
283  return AVERROR_UNKNOWN;
284  }
285  opj_set_event_mgr((opj_common_ptr) dec, NULL, NULL);
286 
287  ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
288  ctx->dec_params.cp_reduce = ctx->lowres;
289  ctx->dec_params.cp_layer = ctx->lowqual;
290  // Tie decoder with decoding parameters
291  opj_setup_decoder(dec, &ctx->dec_params);
292  stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
293 
294  if (!stream) {
295  av_log(avctx, AV_LOG_ERROR,
296  "Codestream could not be opened for reading.\n");
297  opj_destroy_decompress(dec);
298  return AVERROR_UNKNOWN;
299  }
300 
301  // Decode the header only.
302  image = opj_decode_with_info(dec, stream, NULL);
303  opj_cio_close(stream);
304 
305  if (!image) {
306  av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
307  opj_destroy_decompress(dec);
308  return AVERROR_UNKNOWN;
309  }
310 
311  width = image->x1 - image->x0;
312  height = image->y1 - image->y0;
313 
314  if (ctx->lowres) {
315  width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
316  height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
317  }
318 
319  ret = ff_set_dimensions(avctx, width, height);
320  if (ret < 0)
321  goto done;
322 
323  if (avctx->pix_fmt != AV_PIX_FMT_NONE)
324  if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
325  avctx->pix_fmt = AV_PIX_FMT_NONE;
326 
327  if (avctx->pix_fmt == AV_PIX_FMT_NONE)
328  avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
329 
330  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
331  av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
332  ret = AVERROR_INVALIDDATA;
333  goto done;
334  }
335 
336  for (i = 0; i < image->numcomps; i++)
337  if (image->comps[i].prec > avctx->bits_per_raw_sample)
338  avctx->bits_per_raw_sample = image->comps[i].prec;
339 
340  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0) {
341  av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
342  goto done;
343  }
344 
345  ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
346  // Tie decoder with decoding parameters.
347  opj_setup_decoder(dec, &ctx->dec_params);
348  stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
349  if (!stream) {
350  av_log(avctx, AV_LOG_ERROR,
351  "Codestream could not be opened for reading.\n");
352  ret = AVERROR_UNKNOWN;
353  goto done;
354  }
355 
356  opj_image_destroy(image);
357  // Decode the codestream
358  image = opj_decode_with_info(dec, stream, NULL);
359  opj_cio_close(stream);
360 
361  if (!image) {
362  av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
363  ret = AVERROR_UNKNOWN;
364  goto done;
365  }
366 
367  desc = av_pix_fmt_desc_get(avctx->pix_fmt);
368  pixel_size = desc->comp[0].step_minus1 + 1;
369  ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
370 
371  switch (pixel_size) {
372  case 1:
373  if (ispacked) {
374  libopenjpeg_copy_to_packed8(picture, image);
375  } else {
376  libopenjpeg_copyto8(picture, image);
377  }
378  break;
379  case 2:
380  if (ispacked) {
381  libopenjpeg_copy_to_packed8(picture, image);
382  } else {
383  libopenjpeg_copyto16(picture, image);
384  }
385  break;
386  case 3:
387  case 4:
388  if (ispacked) {
389  libopenjpeg_copy_to_packed8(picture, image);
390  }
391  break;
392  case 6:
393  case 8:
394  if (ispacked) {
395  libopenjpeg_copy_to_packed16(picture, image);
396  }
397  break;
398  default:
399  av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
400  ret = AVERROR_PATCHWELCOME;
401  goto done;
402  }
403 
404  *got_frame = 1;
405  ret = buf_size;
406 
407 done:
408  opj_image_destroy(image);
409  opj_destroy_decompress(dec);
410  return ret;
411 }
412 
413 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
414 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
415 
416 static const AVOption options[] = {
417  { "lowqual", "Limit the number of layers used for decoding",
418  OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
419  { "lowres", "Lower the decoding resolution by a power of two",
420  OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
421  { NULL },
422 };
423 
424 static const AVClass class = {
425  .class_name = "libopenjpeg",
426  .item_name = av_default_item_name,
427  .option = options,
429 };
430 
432  .name = "libopenjpeg",
433  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
434  .type = AVMEDIA_TYPE_VIDEO,
435  .id = AV_CODEC_ID_JPEG2000,
436  .priv_data_size = sizeof(LibOpenJPEGContext),
439  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
440  .priv_class = &class,
441 };
static void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image)
static int libopenjpeg_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
static int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
misc image utilities
AVFrame * f
Definition: thread.h:36
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:133
int size
Definition: avcodec.h:968
static enum AVPixelFormat rgb_pix_fmts[]
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1248
#define FF_ARRAY_ELEMS(a)
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2508
#define JP2_SIG_TYPE
AVCodec.
Definition: avcodec.h:2790
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
static void libopenjpeg_copyto16(AVFrame *p, opj_image_t *image)
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
static int decode(MimicContext *ctx, int quality, int num_coeffs, int is_iframe)
Definition: mimic.c:275
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:97
uint8_t
#define av_cold
Definition: attributes.h:66
AVOptions.
#define XYZ_PIXEL_FORMATS
#define AV_RB32
Definition: intreadwrite.h:130
Multithreading support functions.
static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
#define CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: avcodec.h:684
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:967
static void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image)
#define YUV_PIXEL_FORMATS
#define JP2_SIG_VALUE
uint16_t depth_minus1
Number of bits in the component minus 1.
Definition: pixdesc.h:57
int width
width and height of the video frame
Definition: frame.h:174
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static int libopenjpeg_matches_pix_fmt(const opj_image_t *img, enum AVPixelFormat pix_fmt)
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:168
const char * name
Name of the codec implementation.
Definition: avcodec.h:2797
#define FFMAX(a, b)
Definition: common.h:55
static void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image)
#define RGB_PIXEL_FORMATS
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
#define AV_PIX_FMT_GRAY16
Definition: pixfmt.h:227
#define FFMIN(a, b)
Definition: common.h:57
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
static enum AVPixelFormat gray_pix_fmts[]
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
Libavcodec external API header.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1044
Describe the class of an AVClass context structure.
Definition: log.h:33
static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
int index
Definition: gxfenc.c:72
uint16_t step_minus1
Number of elements between 2 horizontally consecutive pixels minus 1.
Definition: pixdesc.h:40
#define VD
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int height
Definition: gxfenc.c:72
static enum AVPixelFormat any_pix_fmts[]
uint16_t plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:34
common internal api header.
common internal and external API header
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:755
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:61
static av_cold int init(AVCodecParserContext *s)
Definition: h264_parser.c:499
void * priv_data
Definition: avcodec.h:1086
pixel format definitions
static const AVOption options[]
static enum AVPixelFormat yuv_pix_fmts[]
opj_dparameters_t dec_params
int height
Definition: frame.h:174
AVCodec ff_libopenjpeg_decoder
#define OFFSET(x)
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
This structure stores compressed data.
Definition: avcodec.h:944
#define GRAY_PIXEL_FORMATS