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.

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