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 #include "libavutil/common.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/intreadwrite.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixfmt.h"
32
33 #include "avcodec.h"
34 #include "codec_internal.h"
35 #include "internal.h"
36 #include "thread.h"
37
38 #include <openjpeg.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_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, \
50 AV_PIX_FMT_GRAY16, AV_PIX_FMT_YA16
51
52 #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUVA420P, \
53 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA422P, \
54 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVA444P, \
55 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, \
56 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, \
57 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, \
58 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, \
59 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, \
60 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, \
61 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, \
62 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16
63
64 #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
65
66 static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {
67 RGB_PIXEL_FORMATS
68 };
69 static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {
70 GRAY_PIXEL_FORMATS
71 };
72 static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {
73 YUV_PIXEL_FORMATS
74 };
75 static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {
76 RGB_PIXEL_FORMATS, GRAY_PIXEL_FORMATS, YUV_PIXEL_FORMATS, XYZ_PIXEL_FORMATS
77 };
78
79 typedef struct LibOpenJPEGContext {
80 AVClass *class;
81 opj_dparameters_t dec_params;
82 int lowqual;
83 } LibOpenJPEGContext;
84
error_callback(const char * msg,void * data)85 static void error_callback(const char *msg, void *data)
86 {
87 av_log(data, AV_LOG_ERROR, "%s", msg);
88 }
89
warning_callback(const char * msg,void * data)90 static void warning_callback(const char *msg, void *data)
91 {
92 av_log(data, AV_LOG_WARNING, "%s", msg);
93 }
94
info_callback(const char * msg,void * data)95 static void info_callback(const char *msg, void *data)
96 {
97 av_log(data, AV_LOG_DEBUG, "%s", msg);
98 }
99
100 typedef struct BufferReader {
101 int pos;
102 int size;
103 const uint8_t *buffer;
104 } BufferReader;
105
stream_read(void * out_buffer,OPJ_SIZE_T nb_bytes,void * user_data)106 static OPJ_SIZE_T stream_read(void *out_buffer, OPJ_SIZE_T nb_bytes, void *user_data)
107 {
108 BufferReader *reader = user_data;
109 int remaining;
110
111 if (reader->pos == reader->size) {
112 return (OPJ_SIZE_T)-1;
113 }
114 remaining = reader->size - reader->pos;
115 if (nb_bytes > remaining) {
116 nb_bytes = remaining;
117 }
118 memcpy(out_buffer, reader->buffer + reader->pos, nb_bytes);
119 reader->pos += (int)nb_bytes;
120 return nb_bytes;
121 }
122
stream_skip(OPJ_OFF_T nb_bytes,void * user_data)123 static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
124 {
125 BufferReader *reader = user_data;
126 if (nb_bytes < 0) {
127 if (reader->pos == 0) {
128 return (OPJ_SIZE_T)-1;
129 }
130 if (nb_bytes + reader->pos < 0) {
131 nb_bytes = -reader->pos;
132 }
133 } else {
134 int remaining;
135
136 if (reader->pos == reader->size) {
137 return (OPJ_SIZE_T)-1;
138 }
139 remaining = reader->size - reader->pos;
140 if (nb_bytes > remaining) {
141 nb_bytes = remaining;
142 }
143 }
144 reader->pos += (int)nb_bytes;
145 return nb_bytes;
146 }
147
stream_seek(OPJ_OFF_T nb_bytes,void * user_data)148 static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
149 {
150 BufferReader *reader = user_data;
151 if (nb_bytes < 0 || nb_bytes > reader->size) {
152 return OPJ_FALSE;
153 }
154 reader->pos = (int)nb_bytes;
155 return OPJ_TRUE;
156 }
157
libopenjpeg_matches_pix_fmt(const opj_image_t * image,enum AVPixelFormat pix_fmt)158 static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
159 {
160 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
161 int match = 1;
162
163 if (desc->nb_components != image->numcomps) {
164 return 0;
165 }
166
167 switch (desc->nb_components) {
168 case 4:
169 match = match &&
170 desc->comp[3].depth >= image->comps[3].prec &&
171 1 == image->comps[3].dx &&
172 1 == image->comps[3].dy;
173 case 3:
174 match = match &&
175 desc->comp[2].depth >= image->comps[2].prec &&
176 1 << desc->log2_chroma_w == image->comps[2].dx &&
177 1 << desc->log2_chroma_h == image->comps[2].dy;
178 case 2:
179 match = match &&
180 desc->comp[1].depth >= image->comps[1].prec &&
181 1 << desc->log2_chroma_w == image->comps[1].dx &&
182 1 << desc->log2_chroma_h == image->comps[1].dy;
183 case 1:
184 match = match &&
185 desc->comp[0].depth >= image->comps[0].prec &&
186 1 == image->comps[0].dx &&
187 1 == image->comps[0].dy;
188 default:
189 break;
190 }
191
192 return match;
193 }
194
libopenjpeg_guess_pix_fmt(const opj_image_t * image)195 static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
196 int index;
197 const enum AVPixelFormat *possible_fmts = NULL;
198 int possible_fmts_nb = 0;
199
200 switch (image->color_space) {
201 case OPJ_CLRSPC_SRGB:
202 possible_fmts = libopenjpeg_rgb_pix_fmts;
203 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
204 break;
205 case OPJ_CLRSPC_GRAY:
206 possible_fmts = libopenjpeg_gray_pix_fmts;
207 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
208 break;
209 case OPJ_CLRSPC_SYCC:
210 possible_fmts = libopenjpeg_yuv_pix_fmts;
211 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
212 break;
213 default:
214 possible_fmts = libopenjpeg_all_pix_fmts;
215 possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
216 break;
217 }
218
219 for (index = 0; index < possible_fmts_nb; ++index)
220 if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
221 return possible_fmts[index];
222 }
223
224 return AV_PIX_FMT_NONE;
225 }
226
libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)227 static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
228 {
229 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
230 int i, component_plane;
231
232 if (pix_fmt == AV_PIX_FMT_GRAY16)
233 return 0;
234
235 component_plane = desc->comp[0].plane;
236 for (i = 1; i < desc->nb_components; i++)
237 if (component_plane != desc->comp[i].plane)
238 return 0;
239 return 1;
240 }
241
libopenjpeg_copy_to_packed8(AVFrame * picture,opj_image_t * image)242 static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
243 uint8_t *img_ptr;
244 int index, x, y, c;
245 for (y = 0; y < picture->height; y++) {
246 index = y * picture->width;
247 img_ptr = picture->data[0] + y * picture->linesize[0];
248 for (x = 0; x < picture->width; x++, index++)
249 for (c = 0; c < image->numcomps; c++)
250 *img_ptr++ = 0x80 * image->comps[c].sgnd + image->comps[c].data[index];
251 }
252 }
253
libopenjpeg_copy_to_packed16(AVFrame * picture,opj_image_t * image)254 static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
255 uint16_t *img_ptr;
256 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
257 int index, x, y, c;
258 int adjust[4];
259 for (x = 0; x < image->numcomps; x++)
260 adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
261
262 for (y = 0; y < picture->height; y++) {
263 index = y * picture->width;
264 img_ptr = (uint16_t *) (picture->data[0] + y * picture->linesize[0]);
265 for (x = 0; x < picture->width; x++, index++)
266 for (c = 0; c < image->numcomps; c++)
267 *img_ptr++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
268 (unsigned)image->comps[c].data[index] << adjust[c];
269 }
270 }
271
libopenjpeg_copyto8(AVFrame * picture,opj_image_t * image)272 static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
273 int *comp_data;
274 uint8_t *img_ptr;
275 int index, x, y;
276
277 for (index = 0; index < image->numcomps; index++) {
278 comp_data = image->comps[index].data;
279 for (y = 0; y < image->comps[index].h; y++) {
280 img_ptr = picture->data[index] + y * picture->linesize[index];
281 for (x = 0; x < image->comps[index].w; x++) {
282 *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
283 img_ptr++;
284 comp_data++;
285 }
286 }
287 }
288 }
289
libopenjpeg_copyto16(AVFrame * picture,opj_image_t * image)290 static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
291 int *comp_data;
292 uint16_t *img_ptr;
293 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
294 int index, x, y;
295 int adjust[4];
296 for (x = 0; x < image->numcomps; x++)
297 adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
298
299 for (index = 0; index < image->numcomps; index++) {
300 comp_data = image->comps[index].data;
301 for (y = 0; y < image->comps[index].h; y++) {
302 img_ptr = (uint16_t *)(picture->data[index] + y * picture->linesize[index]);
303 for (x = 0; x < image->comps[index].w; x++) {
304 *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
305 (unsigned)*comp_data << adjust[index];
306 img_ptr++;
307 comp_data++;
308 }
309 }
310 }
311 }
312
libopenjpeg_decode_init(AVCodecContext * avctx)313 static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
314 {
315 LibOpenJPEGContext *ctx = avctx->priv_data;
316
317 opj_set_default_decoder_parameters(&ctx->dec_params);
318 return 0;
319 }
320
libopenjpeg_decode_frame(AVCodecContext * avctx,AVFrame * picture,int * got_frame,AVPacket * avpkt)321 static int libopenjpeg_decode_frame(AVCodecContext *avctx, AVFrame *picture,
322 int *got_frame, AVPacket *avpkt)
323 {
324 const uint8_t *buf = avpkt->data;
325 int buf_size = avpkt->size;
326 LibOpenJPEGContext *ctx = avctx->priv_data;
327 const AVPixFmtDescriptor *desc;
328 int width, height, ret;
329 int pixel_size = 0;
330 int ispacked = 0;
331 int i;
332 opj_image_t *image = NULL;
333 BufferReader reader = {0, avpkt->size, avpkt->data};
334 opj_codec_t *dec = NULL;
335 opj_stream_t *stream = NULL;
336
337 *got_frame = 0;
338
339 // Check if input is a raw jpeg2k codestream or in jp2 wrapping
340 if ((AV_RB32(buf) == 12) &&
341 (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
342 (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
343 dec = opj_create_decompress(OPJ_CODEC_JP2);
344 } else {
345 /* If the AVPacket contains a jp2c box, then skip to
346 * the starting byte of the codestream. */
347 if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
348 buf += 8;
349 dec = opj_create_decompress(OPJ_CODEC_J2K);
350 }
351
352 if (!dec) {
353 av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
354 ret = AVERROR_EXTERNAL;
355 goto done;
356 }
357
358 if (!opj_set_error_handler(dec, error_callback, avctx) ||
359 !opj_set_warning_handler(dec, warning_callback, avctx) ||
360 !opj_set_info_handler(dec, info_callback, avctx)) {
361 av_log(avctx, AV_LOG_ERROR, "Error setting decoder handlers.\n");
362 ret = AVERROR_EXTERNAL;
363 goto done;
364 }
365
366 ctx->dec_params.cp_layer = ctx->lowqual;
367 ctx->dec_params.cp_reduce = avctx->lowres;
368
369 // Tie decoder with decoding parameters
370 opj_setup_decoder(dec, &ctx->dec_params);
371
372 stream = opj_stream_default_create(OPJ_STREAM_READ);
373
374 if (!stream) {
375 av_log(avctx, AV_LOG_ERROR,
376 "Codestream could not be opened for reading.\n");
377 ret = AVERROR_EXTERNAL;
378 goto done;
379 }
380
381 opj_stream_set_read_function(stream, stream_read);
382 opj_stream_set_skip_function(stream, stream_skip);
383 opj_stream_set_seek_function(stream, stream_seek);
384 opj_stream_set_user_data(stream, &reader, NULL);
385 opj_stream_set_user_data_length(stream, avpkt->size);
386 // Decode the header only.
387 ret = !opj_read_header(stream, dec, &image);
388
389 if (ret) {
390 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream header.\n");
391 ret = AVERROR_EXTERNAL;
392 goto done;
393 }
394
395 width = image->x1 - image->x0;
396 height = image->y1 - image->y0;
397
398 ret = ff_set_dimensions(avctx, width, height);
399 if (ret < 0)
400 goto done;
401
402 if (avctx->pix_fmt != AV_PIX_FMT_NONE)
403 if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
404 avctx->pix_fmt = AV_PIX_FMT_NONE;
405
406 if (avctx->pix_fmt == AV_PIX_FMT_NONE)
407 avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
408
409 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
410 av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format.\n");
411 ret = AVERROR_UNKNOWN;
412 goto done;
413 }
414 for (i = 0; i < image->numcomps; i++)
415 if (image->comps[i].prec > avctx->bits_per_raw_sample)
416 avctx->bits_per_raw_sample = image->comps[i].prec;
417
418 if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
419 goto done;
420
421 ret = !opj_decode(dec, stream, image);
422
423 if (ret) {
424 av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
425 ret = AVERROR_EXTERNAL;
426 goto done;
427 }
428
429 for (i = 0; i < image->numcomps; i++) {
430 if (!image->comps[i].data) {
431 av_log(avctx, AV_LOG_ERROR,
432 "Image component %d contains no data.\n", i);
433 ret = AVERROR_INVALIDDATA;
434 goto done;
435 }
436 }
437
438 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
439 pixel_size = desc->comp[0].step;
440 ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
441
442 switch (pixel_size) {
443 case 1:
444 if (ispacked) {
445 libopenjpeg_copy_to_packed8(picture, image);
446 } else {
447 libopenjpeg_copyto8(picture, image);
448 }
449 break;
450 case 2:
451 if (ispacked) {
452 libopenjpeg_copy_to_packed8(picture, image);
453 } else {
454 libopenjpeg_copyto16(picture, image);
455 }
456 break;
457 case 3:
458 case 4:
459 if (ispacked) {
460 libopenjpeg_copy_to_packed8(picture, image);
461 }
462 break;
463 case 6:
464 case 8:
465 if (ispacked) {
466 libopenjpeg_copy_to_packed16(picture, image);
467 }
468 break;
469 default:
470 avpriv_report_missing_feature(avctx, "Pixel size %d", pixel_size);
471 ret = AVERROR_PATCHWELCOME;
472 goto done;
473 }
474
475 *got_frame = 1;
476 picture->pict_type = AV_PICTURE_TYPE_I;
477 picture->key_frame = 1;
478 ret = buf_size;
479
480 done:
481 opj_image_destroy(image);
482 opj_stream_destroy(stream);
483 opj_destroy_codec(dec);
484 return ret;
485 }
486
487 #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
488 #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
489
490 static const AVOption options[] = {
491 { "lowqual", "Limit the number of layers used for decoding",
492 OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
493 { NULL },
494 };
495
496 static const AVClass openjpeg_class = {
497 .class_name = "libopenjpeg",
498 .item_name = av_default_item_name,
499 .option = options,
500 .version = LIBAVUTIL_VERSION_INT,
501 };
502
503 const FFCodec ff_libopenjpeg_decoder = {
504 .p.name = "libopenjpeg",
505 .p.long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
506 .p.type = AVMEDIA_TYPE_VIDEO,
507 .p.id = AV_CODEC_ID_JPEG2000,
508 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
509 .p.max_lowres = 31,
510 .p.priv_class = &openjpeg_class,
511 .p.wrapper_name = "libopenjpeg",
512 .priv_data_size = sizeof(LibOpenJPEGContext),
513 .init = libopenjpeg_decode_init,
514 FF_CODEC_DECODE_CB(libopenjpeg_decode_frame),
515 };
516