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.

474 lines
16KB

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