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.

597 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. if (reader->pos == reader->size) {
  111. return (OPJ_SIZE_T)-1;
  112. }
  113. int remaining = reader->size - reader->pos;
  114. if (nb_bytes > remaining) {
  115. nb_bytes = remaining;
  116. }
  117. memcpy(out_buffer, reader->buffer + reader->pos, nb_bytes);
  118. reader->pos += (int)nb_bytes;
  119. return nb_bytes;
  120. }
  121. static OPJ_OFF_T stream_skip(OPJ_OFF_T nb_bytes, void *user_data)
  122. {
  123. BufferReader *reader = user_data;
  124. if (nb_bytes < 0) {
  125. if (reader->pos == 0) {
  126. return (OPJ_SIZE_T)-1;
  127. }
  128. if (nb_bytes + reader->pos < 0) {
  129. nb_bytes = -reader->pos;
  130. }
  131. } else {
  132. if (reader->pos == reader->size) {
  133. return (OPJ_SIZE_T)-1;
  134. }
  135. int remaining = reader->size - reader->pos;
  136. if (nb_bytes > remaining) {
  137. nb_bytes = remaining;
  138. }
  139. }
  140. reader->pos += (int)nb_bytes;
  141. return nb_bytes;
  142. }
  143. static OPJ_BOOL stream_seek(OPJ_OFF_T nb_bytes, void *user_data)
  144. {
  145. BufferReader *reader = user_data;
  146. if (nb_bytes < 0 || nb_bytes > reader->size) {
  147. return OPJ_FALSE;
  148. }
  149. reader->pos = (int)nb_bytes;
  150. return OPJ_TRUE;
  151. }
  152. #endif // OPENJPEG_MAJOR_VERSION == 2
  153. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
  154. {
  155. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  156. int match = 1;
  157. if (desc->nb_components != image->numcomps) {
  158. return 0;
  159. }
  160. switch (desc->nb_components) {
  161. case 4:
  162. match = match &&
  163. desc->comp[3].depth >= image->comps[3].prec &&
  164. 1 == image->comps[3].dx &&
  165. 1 == image->comps[3].dy;
  166. case 3:
  167. match = match &&
  168. desc->comp[2].depth >= image->comps[2].prec &&
  169. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  170. 1 << desc->log2_chroma_h == image->comps[2].dy;
  171. case 2:
  172. match = match &&
  173. desc->comp[1].depth >= image->comps[1].prec &&
  174. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  175. 1 << desc->log2_chroma_h == image->comps[1].dy;
  176. case 1:
  177. match = match &&
  178. desc->comp[0].depth >= image->comps[0].prec &&
  179. 1 == image->comps[0].dx &&
  180. 1 == image->comps[0].dy;
  181. default:
  182. break;
  183. }
  184. return match;
  185. }
  186. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  187. int index;
  188. const enum AVPixelFormat *possible_fmts = NULL;
  189. int possible_fmts_nb = 0;
  190. switch (image->color_space) {
  191. case OPJ(CLRSPC_SRGB):
  192. possible_fmts = libopenjpeg_rgb_pix_fmts;
  193. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  194. break;
  195. case OPJ(CLRSPC_GRAY):
  196. possible_fmts = libopenjpeg_gray_pix_fmts;
  197. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  198. break;
  199. case OPJ(CLRSPC_SYCC):
  200. possible_fmts = libopenjpeg_yuv_pix_fmts;
  201. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  202. break;
  203. default:
  204. possible_fmts = libopenjpeg_all_pix_fmts;
  205. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  206. break;
  207. }
  208. for (index = 0; index < possible_fmts_nb; ++index)
  209. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  210. return possible_fmts[index];
  211. }
  212. return AV_PIX_FMT_NONE;
  213. }
  214. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  215. {
  216. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  217. int i, component_plane;
  218. if (pix_fmt == AV_PIX_FMT_GRAY16)
  219. return 0;
  220. component_plane = desc->comp[0].plane;
  221. for (i = 1; i < desc->nb_components; i++)
  222. if (component_plane != desc->comp[i].plane)
  223. return 0;
  224. return 1;
  225. }
  226. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  227. uint8_t *img_ptr;
  228. int index, x, y, c;
  229. for (y = 0; y < picture->height; y++) {
  230. index = y * picture->width;
  231. img_ptr = picture->data[0] + y * picture->linesize[0];
  232. for (x = 0; x < picture->width; x++, index++)
  233. for (c = 0; c < image->numcomps; c++)
  234. *img_ptr++ = 0x80 * image->comps[c].sgnd + image->comps[c].data[index];
  235. }
  236. }
  237. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  238. uint16_t *img_ptr;
  239. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  240. int index, x, y, c;
  241. int adjust[4];
  242. for (x = 0; x < image->numcomps; x++)
  243. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  244. for (y = 0; y < picture->height; y++) {
  245. index = y * picture->width;
  246. img_ptr = (uint16_t *) (picture->data[0] + y * picture->linesize[0]);
  247. for (x = 0; x < picture->width; x++, index++)
  248. for (c = 0; c < image->numcomps; c++)
  249. *img_ptr++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
  250. (unsigned)image->comps[c].data[index] << adjust[c];
  251. }
  252. }
  253. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  254. int *comp_data;
  255. uint8_t *img_ptr;
  256. int index, x, y;
  257. for (index = 0; index < image->numcomps; index++) {
  258. comp_data = image->comps[index].data;
  259. for (y = 0; y < image->comps[index].h; y++) {
  260. img_ptr = picture->data[index] + y * picture->linesize[index];
  261. for (x = 0; x < image->comps[index].w; x++) {
  262. *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
  263. img_ptr++;
  264. comp_data++;
  265. }
  266. }
  267. }
  268. }
  269. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  270. int *comp_data;
  271. uint16_t *img_ptr;
  272. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  273. int index, x, y;
  274. int adjust[4];
  275. for (x = 0; x < image->numcomps; x++)
  276. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  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 = (uint16_t *)(picture->data[index] + y * picture->linesize[index]);
  281. for (x = 0; x < image->comps[index].w; x++) {
  282. *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
  283. (unsigned)*comp_data << adjust[index];
  284. img_ptr++;
  285. comp_data++;
  286. }
  287. }
  288. }
  289. }
  290. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  291. {
  292. LibOpenJPEGContext *ctx = avctx->priv_data;
  293. opj_set_default_decoder_parameters(&ctx->dec_params);
  294. return 0;
  295. }
  296. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  297. void *data, int *got_frame,
  298. AVPacket *avpkt)
  299. {
  300. uint8_t *buf = avpkt->data;
  301. int buf_size = avpkt->size;
  302. LibOpenJPEGContext *ctx = avctx->priv_data;
  303. ThreadFrame frame = { .f = data };
  304. AVFrame *picture = data;
  305. const AVPixFmtDescriptor *desc;
  306. int width, height, ret;
  307. int pixel_size = 0;
  308. int ispacked = 0;
  309. int i;
  310. opj_image_t *image = NULL;
  311. #if OPENJPEG_MAJOR_VERSION == 1
  312. opj_dinfo_t *dec = NULL;
  313. opj_cio_t *stream = NULL;
  314. #else // OPENJPEG_MAJOR_VERSION == 2
  315. BufferReader reader = {0, avpkt->size, avpkt->data};
  316. opj_codec_t *dec = NULL;
  317. opj_stream_t *stream = NULL;
  318. #endif // OPENJPEG_MAJOR_VERSION == 1
  319. *got_frame = 0;
  320. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  321. if ((AV_RB32(buf) == 12) &&
  322. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  323. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  324. dec = opj_create_decompress(OPJ(CODEC_JP2));
  325. } else {
  326. /* If the AVPacket contains a jp2c box, then skip to
  327. * the starting byte of the codestream. */
  328. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  329. buf += 8;
  330. dec = opj_create_decompress(OPJ(CODEC_J2K));
  331. }
  332. if (!dec) {
  333. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  334. ret = AVERROR_EXTERNAL;
  335. goto done;
  336. }
  337. #if OPENJPEG_MAJOR_VERSION == 1
  338. memset(&ctx->event_mgr, 0, sizeof(ctx->event_mgr));
  339. ctx->event_mgr.info_handler = info_callback;
  340. ctx->event_mgr.error_handler = error_callback;
  341. ctx->event_mgr.warning_handler = warning_callback;
  342. opj_set_event_mgr((opj_common_ptr) dec, &ctx->event_mgr, avctx);
  343. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  344. ctx->dec_params.cp_layer = ctx->lowqual;
  345. #else // OPENJPEG_MAJOR_VERSION == 2
  346. if (!opj_set_error_handler(dec, error_callback, avctx) ||
  347. !opj_set_warning_handler(dec, warning_callback, avctx) ||
  348. !opj_set_info_handler(dec, info_callback, avctx)) {
  349. av_log(avctx, AV_LOG_ERROR, "Error setting decoder handlers.\n");
  350. ret = AVERROR_EXTERNAL;
  351. goto done;
  352. }
  353. ctx->dec_params.cp_layer = ctx->lowqual;
  354. ctx->dec_params.cp_reduce = avctx->lowres;
  355. #endif // OPENJPEG_MAJOR_VERSION == 1
  356. // Tie decoder with decoding parameters
  357. opj_setup_decoder(dec, &ctx->dec_params);
  358. #if OPENJPEG_MAJOR_VERSION == 1
  359. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  360. #else // OPENJPEG_MAJOR_VERSION == 2
  361. stream = opj_stream_default_create(OPJ_STREAM_READ);
  362. #endif // OPENJPEG_MAJOR_VERSION == 1
  363. if (!stream) {
  364. av_log(avctx, AV_LOG_ERROR,
  365. "Codestream could not be opened for reading.\n");
  366. ret = AVERROR_EXTERNAL;
  367. goto done;
  368. }
  369. #if OPENJPEG_MAJOR_VERSION == 1
  370. // Decode the header only.
  371. image = opj_decode_with_info(dec, stream, NULL);
  372. opj_cio_close(stream);
  373. stream = NULL;
  374. ret = !image;
  375. #else // OPENJPEG_MAJOR_VERSION == 2
  376. opj_stream_set_read_function(stream, stream_read);
  377. opj_stream_set_skip_function(stream, stream_skip);
  378. opj_stream_set_seek_function(stream, stream_seek);
  379. #if HAVE_OPENJPEG_2_1_OPENJPEG_H
  380. opj_stream_set_user_data(stream, &reader, NULL);
  381. #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
  382. opj_stream_set_user_data(stream, &reader);
  383. #else
  384. #error Missing call to opj_stream_set_user_data
  385. #endif
  386. opj_stream_set_user_data_length(stream, avpkt->size);
  387. // Decode the header only.
  388. ret = !opj_read_header(stream, dec, &image);
  389. #endif // OPENJPEG_MAJOR_VERSION == 1
  390. if (ret) {
  391. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream header.\n");
  392. ret = AVERROR_EXTERNAL;
  393. goto done;
  394. }
  395. width = image->x1 - image->x0;
  396. height = image->y1 - image->y0;
  397. ret = ff_set_dimensions(avctx, width, height);
  398. if (ret < 0)
  399. goto done;
  400. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  401. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  402. avctx->pix_fmt = AV_PIX_FMT_NONE;
  403. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  404. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  405. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  406. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format.\n");
  407. ret = AVERROR_UNKNOWN;
  408. goto done;
  409. }
  410. for (i = 0; i < image->numcomps; i++)
  411. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  412. avctx->bits_per_raw_sample = image->comps[i].prec;
  413. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  414. goto done;
  415. #if OPENJPEG_MAJOR_VERSION == 1
  416. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  417. ctx->dec_params.cp_reduce = avctx->lowres;
  418. // Tie decoder with decoding parameters.
  419. opj_setup_decoder(dec, &ctx->dec_params);
  420. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  421. if (!stream) {
  422. av_log(avctx, AV_LOG_ERROR,
  423. "Codestream could not be opened for reading.\n");
  424. ret = AVERROR_EXTERNAL;
  425. goto done;
  426. }
  427. opj_image_destroy(image);
  428. // Decode the codestream
  429. image = opj_decode_with_info(dec, stream, NULL);
  430. ret = !image;
  431. #else // OPENJPEG_MAJOR_VERSION == 2
  432. ret = !opj_decode(dec, stream, image);
  433. #endif // OPENJPEG_MAJOR_VERSION == 1
  434. if (ret) {
  435. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  436. ret = AVERROR_EXTERNAL;
  437. goto done;
  438. }
  439. for (i = 0; i < image->numcomps; i++) {
  440. if (!image->comps[i].data) {
  441. av_log(avctx, AV_LOG_ERROR,
  442. "Image component %d contains no data.\n", i);
  443. ret = AVERROR_INVALIDDATA;
  444. goto done;
  445. }
  446. }
  447. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  448. pixel_size = desc->comp[0].step;
  449. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  450. switch (pixel_size) {
  451. case 1:
  452. if (ispacked) {
  453. libopenjpeg_copy_to_packed8(picture, image);
  454. } else {
  455. libopenjpeg_copyto8(picture, image);
  456. }
  457. break;
  458. case 2:
  459. if (ispacked) {
  460. libopenjpeg_copy_to_packed8(picture, image);
  461. } else {
  462. libopenjpeg_copyto16(picture, image);
  463. }
  464. break;
  465. case 3:
  466. case 4:
  467. if (ispacked) {
  468. libopenjpeg_copy_to_packed8(picture, image);
  469. }
  470. break;
  471. case 6:
  472. case 8:
  473. if (ispacked) {
  474. libopenjpeg_copy_to_packed16(picture, image);
  475. }
  476. break;
  477. default:
  478. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  479. ret = AVERROR_PATCHWELCOME;
  480. goto done;
  481. }
  482. *got_frame = 1;
  483. ret = buf_size;
  484. done:
  485. opj_image_destroy(image);
  486. #if OPENJPEG_MAJOR_VERSION == 2
  487. opj_stream_destroy(stream);
  488. opj_destroy_codec(dec);
  489. #else
  490. opj_cio_close(stream);
  491. opj_destroy_decompress(dec);
  492. #endif
  493. return ret;
  494. }
  495. static av_cold void libopenjpeg_static_init(AVCodec *codec)
  496. {
  497. const char *version = opj_version();
  498. int major, minor;
  499. if (sscanf(version, "%d.%d", &major, &minor) == 2 && 1000*major + minor <= 1003)
  500. codec->capabilities |= AV_CODEC_CAP_EXPERIMENTAL;
  501. }
  502. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  503. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  504. static const AVOption options[] = {
  505. { "lowqual", "Limit the number of layers used for decoding",
  506. OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  507. { NULL },
  508. };
  509. static const AVClass openjpeg_class = {
  510. .class_name = "libopenjpeg",
  511. .item_name = av_default_item_name,
  512. .option = options,
  513. .version = LIBAVUTIL_VERSION_INT,
  514. };
  515. AVCodec ff_libopenjpeg_decoder = {
  516. .name = "libopenjpeg",
  517. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  518. .type = AVMEDIA_TYPE_VIDEO,
  519. .id = AV_CODEC_ID_JPEG2000,
  520. .priv_data_size = sizeof(LibOpenJPEGContext),
  521. .init = libopenjpeg_decode_init,
  522. .decode = libopenjpeg_decode_frame,
  523. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
  524. .max_lowres = 31,
  525. .priv_class = &openjpeg_class,
  526. .init_static_data = libopenjpeg_static_init,
  527. };