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