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.

433 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 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 {
  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. int index, x, y, c;
  163. int adjust[4];
  164. for (x = 0; x < image->numcomps; x++)
  165. adjust[x] = FFMAX(FFMIN(av_pix_fmt_desc_get(picture->format)->comp[x].depth_minus1 + 1 - 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++ = (1 << image->comps[c].prec - 1) * image->comps[c].sgnd +
  172. (unsigned)image->comps[c].data[index] << adjust[c];
  173. }
  174. }
  175. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  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 = 0x80 * image->comps[index].sgnd + *comp_data;
  185. img_ptr++;
  186. comp_data++;
  187. }
  188. }
  189. }
  190. }
  191. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  192. int *comp_data;
  193. uint16_t *img_ptr;
  194. int index, x, y;
  195. int adjust[4];
  196. for (x = 0; x < image->numcomps; x++)
  197. adjust[x] = FFMAX(FFMIN(av_pix_fmt_desc_get(picture->format)->comp[x].depth_minus1 + 1 - image->comps[x].prec, 8), 0);
  198. for (index = 0; index < image->numcomps; index++) {
  199. comp_data = image->comps[index].data;
  200. for (y = 0; y < image->comps[index].h; y++) {
  201. img_ptr = (uint16_t *)(picture->data[index] + y * picture->linesize[index]);
  202. for (x = 0; x < image->comps[index].w; x++) {
  203. *img_ptr = (1 << image->comps[index].prec - 1) * image->comps[index].sgnd +
  204. (unsigned)*comp_data << adjust[index];
  205. img_ptr++;
  206. comp_data++;
  207. }
  208. }
  209. }
  210. }
  211. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  212. {
  213. LibOpenJPEGContext *ctx = avctx->priv_data;
  214. opj_set_default_decoder_parameters(&ctx->dec_params);
  215. return 0;
  216. }
  217. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  218. void *data, int *got_frame,
  219. AVPacket *avpkt)
  220. {
  221. uint8_t *buf = avpkt->data;
  222. int buf_size = avpkt->size;
  223. LibOpenJPEGContext *ctx = avctx->priv_data;
  224. ThreadFrame frame = { .f = data };
  225. AVFrame *picture = data;
  226. const AVPixFmtDescriptor *desc;
  227. opj_dinfo_t *dec;
  228. opj_cio_t *stream;
  229. opj_image_t *image;
  230. int width, height, ret;
  231. int pixel_size = 0;
  232. int ispacked = 0;
  233. int i;
  234. *got_frame = 0;
  235. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  236. if ((AV_RB32(buf) == 12) &&
  237. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  238. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  239. dec = opj_create_decompress(CODEC_JP2);
  240. } else {
  241. /* If the AVPacket contains a jp2c box, then skip to
  242. * the starting byte of the codestream. */
  243. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  244. buf += 8;
  245. dec = opj_create_decompress(CODEC_J2K);
  246. }
  247. if (!dec) {
  248. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  249. return AVERROR_UNKNOWN;
  250. }
  251. opj_set_event_mgr((opj_common_ptr) dec, NULL, NULL);
  252. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  253. ctx->dec_params.cp_layer = ctx->lowqual;
  254. // Tie decoder with decoding parameters
  255. opj_setup_decoder(dec, &ctx->dec_params);
  256. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  257. if (!stream) {
  258. av_log(avctx, AV_LOG_ERROR,
  259. "Codestream could not be opened for reading.\n");
  260. opj_destroy_decompress(dec);
  261. return AVERROR_UNKNOWN;
  262. }
  263. // Decode the header only.
  264. image = opj_decode_with_info(dec, stream, NULL);
  265. opj_cio_close(stream);
  266. if (!image) {
  267. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  268. opj_destroy_decompress(dec);
  269. return AVERROR_UNKNOWN;
  270. }
  271. width = image->x1 - image->x0;
  272. height = image->y1 - image->y0;
  273. ret = ff_set_dimensions(avctx, width, height);
  274. if (ret < 0)
  275. goto done;
  276. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  277. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  278. avctx->pix_fmt = AV_PIX_FMT_NONE;
  279. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  280. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  281. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  282. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  283. goto done;
  284. }
  285. for (i = 0; i < image->numcomps; i++)
  286. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  287. avctx->bits_per_raw_sample = image->comps[i].prec;
  288. if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
  289. goto done;
  290. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  291. ctx->dec_params.cp_reduce = avctx->lowres;
  292. // Tie decoder with decoding parameters.
  293. opj_setup_decoder(dec, &ctx->dec_params);
  294. stream = opj_cio_open((opj_common_ptr) dec, buf, buf_size);
  295. if (!stream) {
  296. av_log(avctx, AV_LOG_ERROR,
  297. "Codestream could not be opened for reading.\n");
  298. ret = AVERROR_UNKNOWN;
  299. goto done;
  300. }
  301. opj_image_destroy(image);
  302. // Decode the codestream
  303. image = opj_decode_with_info(dec, stream, NULL);
  304. opj_cio_close(stream);
  305. if (!image) {
  306. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  307. ret = AVERROR_UNKNOWN;
  308. goto done;
  309. }
  310. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  311. pixel_size = desc->comp[0].step_minus1 + 1;
  312. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  313. switch (pixel_size) {
  314. case 1:
  315. if (ispacked) {
  316. libopenjpeg_copy_to_packed8(picture, image);
  317. } else {
  318. libopenjpeg_copyto8(picture, image);
  319. }
  320. break;
  321. case 2:
  322. if (ispacked) {
  323. libopenjpeg_copy_to_packed8(picture, image);
  324. } else {
  325. libopenjpeg_copyto16(picture, image);
  326. }
  327. break;
  328. case 3:
  329. case 4:
  330. if (ispacked) {
  331. libopenjpeg_copy_to_packed8(picture, image);
  332. }
  333. break;
  334. case 6:
  335. case 8:
  336. if (ispacked) {
  337. libopenjpeg_copy_to_packed16(picture, image);
  338. }
  339. break;
  340. default:
  341. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  342. ret = AVERROR_PATCHWELCOME;
  343. goto done;
  344. }
  345. *got_frame = 1;
  346. ret = buf_size;
  347. done:
  348. opj_image_destroy(image);
  349. opj_destroy_decompress(dec);
  350. return ret;
  351. }
  352. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  353. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  354. static const AVOption options[] = {
  355. { "lowqual", "Limit the number of layers used for decoding",
  356. OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  357. { NULL },
  358. };
  359. static const AVClass openjpeg_class = {
  360. .class_name = "libopenjpeg",
  361. .item_name = av_default_item_name,
  362. .option = options,
  363. .version = LIBAVUTIL_VERSION_INT,
  364. };
  365. AVCodec ff_libopenjpeg_decoder = {
  366. .name = "libopenjpeg",
  367. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  368. .type = AVMEDIA_TYPE_VIDEO,
  369. .id = AV_CODEC_ID_JPEG2000,
  370. .priv_data_size = sizeof(LibOpenJPEGContext),
  371. .init = libopenjpeg_decode_init,
  372. .decode = libopenjpeg_decode_frame,
  373. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  374. .max_lowres = 31,
  375. .priv_class = &openjpeg_class,
  376. };