You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

603 lines
19KB

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