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.

440 lines
13KB

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