FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 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 decoder using libopenjpeg
25  */
26 
27 #define OPJ_STATIC
28 
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/pixfmt.h"
33 #include "libavutil/opt.h"
34 #include "avcodec.h"
35 #include "internal.h"
36 #include "thread.h"
37 
38 #if HAVE_OPENJPEG_1_5_OPENJPEG_H
39 # include <openjpeg-1.5/openjpeg.h>
40 #else
41 # include <openjpeg.h>
42 #endif
43 
44 #define JP2_SIG_TYPE 0x6A502020
45 #define JP2_SIG_VALUE 0x0D0A870A
46 
47 // pix_fmts with lower bpp have to be listed before
48 // similar pix_fmts with higher bpp.
49 #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
50 #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
51 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
52  AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
53  AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
54  AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
55  AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
56  AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
57  AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
58  AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
59  AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
60  AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
61  AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
62 
63 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
64 
72 
73 typedef struct {
74  AVClass *class;
75  opj_dparameters_t dec_params;
76  int lowqual;
78 
79 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
80 {
81  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
82  int match = 1;
83 
84  if (desc->nb_components != image->numcomps) {
85  return 0;
86  }
87 
88  switch (desc->nb_components) {
89  case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
90  1 == image->comps[3].dx &&
91  1 == image->comps[3].dy;
92  case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
93  1 << desc->log2_chroma_w == image->comps[2].dx &&
94  1 << desc->log2_chroma_h == image->comps[2].dy;
95  case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
96  1 << desc->log2_chroma_w == image->comps[1].dx &&
97  1 << desc->log2_chroma_h == image->comps[1].dy;
98  case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
99  1 == image->comps[0].dx &&
100  1 == image->comps[0].dy;
101  default:
102  break;
103  }
104 
105  return match;
106 }
107 
108 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
109  int index;
110  const enum AVPixelFormat *possible_fmts = NULL;
111  int possible_fmts_nb = 0;
112 
113  switch (image->color_space) {
114  case CLRSPC_SRGB:
115  possible_fmts = libopenjpeg_rgb_pix_fmts;
116  possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
117  break;
118  case CLRSPC_GRAY:
119  possible_fmts = libopenjpeg_gray_pix_fmts;
120  possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
121  break;
122  case CLRSPC_SYCC:
123  possible_fmts = libopenjpeg_yuv_pix_fmts;
124  possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
125  break;
126  default:
127  possible_fmts = libopenjpeg_all_pix_fmts;
128  possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
129  break;
130  }
131 
132  for (index = 0; index < possible_fmts_nb; ++index) {
133  if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
134  return possible_fmts[index];
135  }
136  }
137 
138  return AV_PIX_FMT_NONE;
139 }
140 
142 {
143  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
144  int i, component_plane;
145 
146  if (pix_fmt == AV_PIX_FMT_GRAY16)
147  return 0;
148 
149  component_plane = desc->comp[0].plane;
150  for (i = 1; i < desc->nb_components; i++) {
151  if (component_plane != desc->comp[i].plane)
152  return 0;
153  }
154  return 1;
155 }
156 
157 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
158  uint8_t *img_ptr;
159  int index, x, y, c;
160  for (y = 0; y < picture->height; y++) {
161  index = y*picture->width;
162  img_ptr = picture->data[0] + y*picture->linesize[0];
163  for (x = 0; x < picture->width; x++, index++) {
164  for (c = 0; c < image->numcomps; c++) {
165  *img_ptr++ = 0x80 * image->comps[c].sgnd + image->comps[c].data[index];
166  }
167  }
168  }
169 }
170 
171 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
172  uint16_t *img_ptr;
173  int index, x, y, c;
174  int adjust[4];
175  for (x = 0; x < image->numcomps; x++)
176  adjust[x] = FFMAX(FFMIN(av_pix_fmt_desc_get(picture->format)->comp[x].depth_minus1 + 1 - image->comps[x].prec, 8), 0);
177 
178  for (y = 0; y < picture->height; y++) {
179  index = y*picture->width;
180  img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
181  for (x = 0; x < picture->width; x++, index++) {
182  for (c = 0; c < image->numcomps; c++) {
183  *img_ptr++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
184  (unsigned)image->comps[c].data[index] << adjust[c];
185  }
186  }
187  }
188 }
189 
190 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
191  int *comp_data;
192  uint8_t *img_ptr;
193  int index, x, y;
194 
195  for (index = 0; index < image->numcomps; index++) {
196  comp_data = image->comps[index].data;
197  for (y = 0; y < image->comps[index].h; y++) {
198  img_ptr = picture->data[index] + y * picture->linesize[index];
199  for (x = 0; x < image->comps[index].w; x++) {
200  *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
201  img_ptr++;
202  comp_data++;
203  }
204  }
205  }
206 }
207 
208 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
209  int *comp_data;
210  uint16_t *img_ptr;
211  int index, x, y;
212  int adjust[4];
213  for (x = 0; x < image->numcomps; x++)
214  adjust[x] = FFMAX(FFMIN(av_pix_fmt_desc_get(picture->format)->comp[x].depth_minus1 + 1 - image->comps[x].prec, 8), 0);
215 
216  for (index = 0; index < image->numcomps; index++) {
217  comp_data = image->comps[index].data;
218  for (y = 0; y < image->comps[index].h; y++) {
219  img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
220  for (x = 0; x < image->comps[index].w; x++) {
221  *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
222  (unsigned)*comp_data << adjust[index];
223  img_ptr++;
224  comp_data++;
225  }
226  }
227  }
228 }
229 
231 {
232  LibOpenJPEGContext *ctx = avctx->priv_data;
233 
234  opj_set_default_decoder_parameters(&ctx->dec_params);
235  return 0;
236 }
237 
239  void *data, int *got_frame,
240  AVPacket *avpkt)
241 {
242  uint8_t *buf = avpkt->data;
243  int buf_size = avpkt->size;
244  LibOpenJPEGContext *ctx = avctx->priv_data;
245  ThreadFrame frame = { .f = data };
246  AVFrame *picture = data;
247  const AVPixFmtDescriptor *desc;
248  opj_dinfo_t *dec;
249  opj_cio_t *stream;
250  opj_image_t *image;
251  int width, height, ret;
252  int pixel_size = 0;
253  int ispacked = 0;
254  int i;
255 
256  *got_frame = 0;
257 
258  // Check if input is a raw jpeg2k codestream or in jp2 wrapping
259  if ((AV_RB32(buf) == 12) &&
260  (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
261  (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
262  dec = opj_create_decompress(CODEC_JP2);
263  } else {
264  /* If the AVPacket contains a jp2c box, then skip to
265  * the starting byte of the codestream. */
266  if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
267  buf += 8;
268  dec = opj_create_decompress(CODEC_J2K);
269  }
270 
271  if (!dec) {
272  av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
273  return AVERROR_UNKNOWN;
274  }
275  opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
276  ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
277  ctx->dec_params.cp_layer = ctx->lowqual;
278  // Tie decoder with decoding parameters
279  opj_setup_decoder(dec, &ctx->dec_params);
280  stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
281 
282  if (!stream) {
283  av_log(avctx, AV_LOG_ERROR,
284  "Codestream could not be opened for reading.\n");
285  opj_destroy_decompress(dec);
286  return AVERROR_UNKNOWN;
287  }
288 
289  // Decode the header only.
290  image = opj_decode_with_info(dec, stream, NULL);
291  opj_cio_close(stream);
292 
293  if (!image) {
294  av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
295  opj_destroy_decompress(dec);
296  return AVERROR_UNKNOWN;
297  }
298 
299  width = image->x1 - image->x0;
300  height = image->y1 - image->y0;
301 
302  ret = ff_set_dimensions(avctx, width, height);
303  if (ret < 0)
304  goto done;
305 
306  if (avctx->pix_fmt != AV_PIX_FMT_NONE)
307  if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
308  avctx->pix_fmt = AV_PIX_FMT_NONE;
309 
310  if (avctx->pix_fmt == AV_PIX_FMT_NONE)
311  avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
312 
313  if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
314  av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
315  goto done;
316  }
317  for (i = 0; i < image->numcomps; i++)
318  if (image->comps[i].prec > avctx->bits_per_raw_sample)
319  avctx->bits_per_raw_sample = image->comps[i].prec;
320 
321  if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
322  goto done;
323 
324  ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
325  ctx->dec_params.cp_reduce = avctx->lowres;
326  // Tie decoder with decoding parameters.
327  opj_setup_decoder(dec, &ctx->dec_params);
328  stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
329  if (!stream) {
330  av_log(avctx, AV_LOG_ERROR,
331  "Codestream could not be opened for reading.\n");
332  ret = AVERROR_UNKNOWN;
333  goto done;
334  }
335 
336  opj_image_destroy(image);
337  // Decode the codestream
338  image = opj_decode_with_info(dec, stream, NULL);
339  opj_cio_close(stream);
340 
341  if (!image) {
342  av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
343  ret = AVERROR_UNKNOWN;
344  goto done;
345  }
346 
347  desc = av_pix_fmt_desc_get(avctx->pix_fmt);
348  pixel_size = desc->comp[0].step_minus1 + 1;
349  ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
350 
351  switch (pixel_size) {
352  case 1:
353  if (ispacked) {
354  libopenjpeg_copy_to_packed8(picture, image);
355  } else {
356  libopenjpeg_copyto8(picture, image);
357  }
358  break;
359  case 2:
360  if (ispacked) {
361  libopenjpeg_copy_to_packed8(picture, image);
362  } else {
363  libopenjpeg_copyto16(picture, image);
364  }
365  break;
366  case 3:
367  case 4:
368  if (ispacked) {
369  libopenjpeg_copy_to_packed8(picture, image);
370  }
371  break;
372  case 6:
373  case 8:
374  if (ispacked) {
375  libopenjpeg_copy_to_packed16(picture, image);
376  }
377  break;
378  default:
379  av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
380  ret = AVERROR_PATCHWELCOME;
381  goto done;
382  }
383 
384  *got_frame = 1;
385  ret = buf_size;
386 
387 done:
388  opj_image_destroy(image);
389  opj_destroy_decompress(dec);
390  return ret;
391 }
392 
393 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
394 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
395 
396 static const AVOption options[] = {
397  { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
398  { NULL },
399 };
400 
401 static const AVClass openjpeg_class = {
402  .class_name = "libopenjpeg",
403  .item_name = av_default_item_name,
404  .option = options,
405  .version = LIBAVUTIL_VERSION_INT,
406 };
407 
409  .name = "libopenjpeg",
410  .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
411  .type = AVMEDIA_TYPE_VIDEO,
412  .id = AV_CODEC_ID_JPEG2000,
413  .priv_data_size = sizeof(LibOpenJPEGContext),
416  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
417  .max_lowres = 31,
418  .priv_class = &openjpeg_class,
419 };