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.

444 lines
15KB

  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. int lowqual;
  75. } LibOpenJPEGContext;
  76. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
  77. {
  78. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  79. int match = 1;
  80. if (desc->nb_components != image->numcomps) {
  81. return 0;
  82. }
  83. switch (desc->nb_components) {
  84. case 4:
  85. match = match &&
  86. desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
  87. 1 == image->comps[3].dx &&
  88. 1 == image->comps[3].dy;
  89. case 3:
  90. match = match &&
  91. desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
  92. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  93. 1 << desc->log2_chroma_h == image->comps[2].dy;
  94. case 2:
  95. match = match &&
  96. desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
  97. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  98. 1 << desc->log2_chroma_h == image->comps[1].dy;
  99. case 1:
  100. match = match &&
  101. desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
  102. 1 == image->comps[0].dx &&
  103. 1 == image->comps[0].dy;
  104. default:
  105. break;
  106. }
  107. return match;
  108. }
  109. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  110. int index;
  111. const enum AVPixelFormat *possible_fmts = NULL;
  112. int possible_fmts_nb = 0;
  113. switch (image->color_space) {
  114. case CLRSPC_SRGB:
  115. possible_fmts = libopenjpeg_rgb_pix_fmts;
  116. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  117. break;
  118. case CLRSPC_GRAY:
  119. possible_fmts = libopenjpeg_gray_pix_fmts;
  120. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  121. break;
  122. case CLRSPC_SYCC:
  123. possible_fmts = libopenjpeg_yuv_pix_fmts;
  124. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  125. break;
  126. default:
  127. possible_fmts = libopenjpeg_all_pix_fmts;
  128. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  129. break;
  130. }
  131. for (index = 0; index < possible_fmts_nb; ++index)
  132. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  133. return possible_fmts[index];
  134. }
  135. return AV_PIX_FMT_NONE;
  136. }
  137. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  138. {
  139. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  140. int i, component_plane;
  141. if (pix_fmt == AV_PIX_FMT_GRAY16)
  142. return 0;
  143. component_plane = desc->comp[0].plane;
  144. for (i = 1; i < desc->nb_components; i++)
  145. if (component_plane != desc->comp[i].plane)
  146. return 0;
  147. return 1;
  148. }
  149. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  150. uint8_t *img_ptr;
  151. int index, x, y, c;
  152. for (y = 0; y < picture->height; y++) {
  153. index = y * picture->width;
  154. img_ptr = picture->data[0] + y * picture->linesize[0];
  155. for (x = 0; x < picture->width; x++, index++)
  156. for (c = 0; c < image->numcomps; c++)
  157. *img_ptr++ = 0x80 * image->comps[c].sgnd + image->comps[c].data[index];
  158. }
  159. }
  160. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  161. uint16_t *img_ptr;
  162. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  163. int index, x, y, c;
  164. int adjust[4];
  165. for (x = 0; x < image->numcomps; x++)
  166. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth_minus1 + 1 - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  167. for (y = 0; y < picture->height; y++) {
  168. index = y * picture->width;
  169. img_ptr = (uint16_t *) (picture->data[0] + y * picture->linesize[0]);
  170. for (x = 0; x < picture->width; x++, index++)
  171. for (c = 0; c < image->numcomps; c++)
  172. *img_ptr++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
  173. (unsigned)image->comps[c].data[index] << adjust[c];
  174. }
  175. }
  176. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  177. int *comp_data;
  178. uint8_t *img_ptr;
  179. int index, x, y;
  180. for (index = 0; index < image->numcomps; index++) {
  181. comp_data = image->comps[index].data;
  182. for (y = 0; y < image->comps[index].h; y++) {
  183. img_ptr = picture->data[index] + y * picture->linesize[index];
  184. for (x = 0; x < image->comps[index].w; x++) {
  185. *img_ptr = 0x80 * image->comps[index].sgnd + *comp_data;
  186. img_ptr++;
  187. comp_data++;
  188. }
  189. }
  190. }
  191. }
  192. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  193. int *comp_data;
  194. uint16_t *img_ptr;
  195. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(picture->format);
  196. int index, x, y;
  197. int adjust[4];
  198. for (x = 0; x < image->numcomps; x++)
  199. adjust[x] = FFMAX(FFMIN(desc->comp[x].depth_minus1 + 1 - image->comps[x].prec, 8), 0) + desc->comp[x].shift;
  200. for (index = 0; index < image->numcomps; index++) {
  201. comp_data = image->comps[index].data;
  202. for (y = 0; y < image->comps[index].h; y++) {
  203. img_ptr = (uint16_t *)(picture->data[index] + y * picture->linesize[index]);
  204. for (x = 0; x < image->comps[index].w; x++) {
  205. *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
  206. (unsigned)*comp_data << adjust[index];
  207. img_ptr++;
  208. comp_data++;
  209. }
  210. }
  211. }
  212. }
  213. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  214. {
  215. LibOpenJPEGContext *ctx = avctx->priv_data;
  216. opj_set_default_decoder_parameters(&ctx->dec_params);
  217. return 0;
  218. }
  219. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  220. void *data, int *got_frame,
  221. AVPacket *avpkt)
  222. {
  223. uint8_t *buf = avpkt->data;
  224. int buf_size = avpkt->size;
  225. LibOpenJPEGContext *ctx = avctx->priv_data;
  226. ThreadFrame frame = { .f = data };
  227. AVFrame *picture = data;
  228. const AVPixFmtDescriptor *desc;
  229. opj_dinfo_t *dec;
  230. opj_cio_t *stream;
  231. opj_image_t *image;
  232. int width, height, ret;
  233. int pixel_size = 0;
  234. int ispacked = 0;
  235. int i;
  236. *got_frame = 0;
  237. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  238. if ((AV_RB32(buf) == 12) &&
  239. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  240. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  241. dec = opj_create_decompress(CODEC_JP2);
  242. } else {
  243. /* If the AVPacket contains a jp2c box, then skip to
  244. * the starting byte of the codestream. */
  245. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  246. buf += 8;
  247. dec = opj_create_decompress(CODEC_J2K);
  248. }
  249. if (!dec) {
  250. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  251. return AVERROR_UNKNOWN;
  252. }
  253. opj_set_event_mgr((opj_common_ptr) dec, NULL, NULL);
  254. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  255. ctx->dec_params.cp_layer = ctx->lowqual;
  256. // Tie decoder with decoding parameters
  257. opj_setup_decoder(dec, &ctx->dec_params);
  258. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  259. if (!stream) {
  260. av_log(avctx, AV_LOG_ERROR,
  261. "Codestream could not be opened for reading.\n");
  262. opj_destroy_decompress(dec);
  263. return AVERROR_UNKNOWN;
  264. }
  265. // Decode the header only.
  266. image = opj_decode_with_info(dec, stream, NULL);
  267. opj_cio_close(stream);
  268. if (!image) {
  269. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  270. opj_destroy_decompress(dec);
  271. return AVERROR_UNKNOWN;
  272. }
  273. width = image->x1 - image->x0;
  274. height = image->y1 - image->y0;
  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. goto done;
  286. }
  287. for (i = 0; i < image->numcomps; i++)
  288. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  289. avctx->bits_per_raw_sample = image->comps[i].prec;
  290. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  291. goto done;
  292. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  293. ctx->dec_params.cp_reduce = avctx->lowres;
  294. // Tie decoder with decoding parameters.
  295. opj_setup_decoder(dec, &ctx->dec_params);
  296. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  297. if (!stream) {
  298. av_log(avctx, AV_LOG_ERROR,
  299. "Codestream could not be opened for reading.\n");
  300. ret = AVERROR_UNKNOWN;
  301. goto done;
  302. }
  303. opj_image_destroy(image);
  304. // Decode the codestream
  305. image = opj_decode_with_info(dec, stream, NULL);
  306. opj_cio_close(stream);
  307. if (!image) {
  308. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  309. ret = AVERROR_UNKNOWN;
  310. goto done;
  311. }
  312. for (i = 0; i < image->numcomps; i++) {
  313. if (!image->comps[i].data) {
  314. av_log(avctx, AV_LOG_ERROR,
  315. "Image component %d contains no data.\n", i);
  316. ret = AVERROR_INVALIDDATA;
  317. goto done;
  318. }
  319. }
  320. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  321. pixel_size = desc->comp[0].step_minus1 + 1;
  322. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  323. switch (pixel_size) {
  324. case 1:
  325. if (ispacked) {
  326. libopenjpeg_copy_to_packed8(picture, image);
  327. } else {
  328. libopenjpeg_copyto8(picture, image);
  329. }
  330. break;
  331. case 2:
  332. if (ispacked) {
  333. libopenjpeg_copy_to_packed8(picture, image);
  334. } else {
  335. libopenjpeg_copyto16(picture, image);
  336. }
  337. break;
  338. case 3:
  339. case 4:
  340. if (ispacked) {
  341. libopenjpeg_copy_to_packed8(picture, image);
  342. }
  343. break;
  344. case 6:
  345. case 8:
  346. if (ispacked) {
  347. libopenjpeg_copy_to_packed16(picture, image);
  348. }
  349. break;
  350. default:
  351. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  352. ret = AVERROR_PATCHWELCOME;
  353. goto done;
  354. }
  355. *got_frame = 1;
  356. ret = buf_size;
  357. done:
  358. opj_image_destroy(image);
  359. opj_destroy_decompress(dec);
  360. return ret;
  361. }
  362. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  363. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  364. static const AVOption options[] = {
  365. { "lowqual", "Limit the number of layers used for decoding",
  366. OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  367. { NULL },
  368. };
  369. static const AVClass openjpeg_class = {
  370. .class_name = "libopenjpeg",
  371. .item_name = av_default_item_name,
  372. .option = options,
  373. .version = LIBAVUTIL_VERSION_INT,
  374. };
  375. AVCodec ff_libopenjpeg_decoder = {
  376. .name = "libopenjpeg",
  377. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  378. .type = AVMEDIA_TYPE_VIDEO,
  379. .id = AV_CODEC_ID_JPEG2000,
  380. .priv_data_size = sizeof(LibOpenJPEGContext),
  381. .init = libopenjpeg_decode_init,
  382. .decode = libopenjpeg_decode_frame,
  383. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  384. .max_lowres = 31,
  385. .priv_class = &openjpeg_class,
  386. };