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.

442 lines
14KB

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