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 "thread.h"
  34. #define JP2_SIG_TYPE 0x6A502020
  35. #define JP2_SIG_VALUE 0x0D0A870A
  36. // pix_fmts with lower bpp have to be listed before
  37. // similar pix_fmts with higher bpp.
  38. #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24, AV_PIX_FMT_RGBA, \
  39. AV_PIX_FMT_RGB48
  40. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8, AV_PIX_FMT_Y400A, \
  41. AV_PIX_FMT_GRAY16
  42. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P, \
  43. AV_PIX_FMT_YUVA420P, \
  44. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, \
  45. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P, \
  46. AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, \
  47. AV_PIX_FMT_YUV444P9, \
  48. AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, \
  49. AV_PIX_FMT_YUV444P10, \
  50. AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, \
  51. AV_PIX_FMT_YUV444P16
  52. #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
  53. static const enum AVPixelFormat rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  54. static const enum AVPixelFormat gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  55. static const enum AVPixelFormat yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  56. static const enum AVPixelFormat any_pix_fmts[] = {RGB_PIXEL_FORMATS,
  57. GRAY_PIXEL_FORMATS,
  58. YUV_PIXEL_FORMATS,
  59. XYZ_PIXEL_FORMATS};
  60. typedef struct {
  61. AVClass *class;
  62. opj_dparameters_t dec_params;
  63. int lowres;
  64. int lowqual;
  65. } LibOpenJPEGContext;
  66. static int libopenjpeg_matches_pix_fmt(const opj_image_t *img,
  67. enum AVPixelFormat pix_fmt)
  68. {
  69. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  70. int match = 1;
  71. if (desc->nb_components != img->numcomps) {
  72. return 0;
  73. }
  74. switch (desc->nb_components) {
  75. case 4:
  76. match = match &&
  77. desc->comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
  78. 1 == img->comps[3].dx &&
  79. 1 == img->comps[3].dy;
  80. case 3:
  81. match = match &&
  82. desc->comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
  83. 1 << desc->log2_chroma_w == img->comps[2].dx &&
  84. 1 << desc->log2_chroma_h == img->comps[2].dy;
  85. case 2:
  86. match = match &&
  87. desc->comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
  88. 1 << desc->log2_chroma_w == img->comps[1].dx &&
  89. 1 << desc->log2_chroma_h == img->comps[1].dy;
  90. case 1:
  91. match = match &&
  92. desc->comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
  93. 1 == img->comps[0].dx &&
  94. 1 == img->comps[0].dy;
  95. default:
  96. break;
  97. }
  98. return match;
  99. }
  100. static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
  101. {
  102. int index;
  103. const enum AVPixelFormat *possible_fmts = NULL;
  104. int possible_fmts_nb = 0;
  105. switch (image->color_space) {
  106. case CLRSPC_SRGB:
  107. possible_fmts = rgb_pix_fmts;
  108. possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
  109. break;
  110. case CLRSPC_GRAY:
  111. possible_fmts = gray_pix_fmts;
  112. possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
  113. break;
  114. case CLRSPC_SYCC:
  115. possible_fmts = yuv_pix_fmts;
  116. possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
  117. break;
  118. default:
  119. possible_fmts = any_pix_fmts;
  120. possible_fmts_nb = FF_ARRAY_ELEMS(any_pix_fmts);
  121. break;
  122. }
  123. for (index = 0; index < possible_fmts_nb; ++index) {
  124. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  125. return possible_fmts[index];
  126. }
  127. }
  128. return AV_PIX_FMT_NONE;
  129. }
  130. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  131. {
  132. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  133. int i, component_plane;
  134. if (pix_fmt == AV_PIX_FMT_GRAY16)
  135. return 0;
  136. component_plane = desc->comp[0].plane;
  137. for (i = 1; i < desc->nb_components; i++) {
  138. if (component_plane != desc->comp[i].plane)
  139. return 0;
  140. }
  141. return 1;
  142. }
  143. static void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image)
  144. {
  145. uint8_t *img_ptr;
  146. int index, x, y, c;
  147. for (y = 0; y < picture->height; y++) {
  148. index = y*picture->width;
  149. img_ptr = picture->data[0] + y*picture->linesize[0];
  150. for (x = 0; x < picture->width; x++, index++) {
  151. for (c = 0; c < image->numcomps; c++) {
  152. *img_ptr++ = image->comps[c].data[index];
  153. }
  154. }
  155. }
  156. }
  157. static void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image)
  158. {
  159. uint16_t *img_ptr;
  160. int index, x, y, c;
  161. int adjust[4];
  162. for (x = 0; x < image->numcomps; x++)
  163. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  164. for (y = 0; y < picture->height; y++) {
  165. index = y*picture->width;
  166. img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
  167. for (x = 0; x < picture->width; x++, index++) {
  168. for (c = 0; c < image->numcomps; c++) {
  169. *img_ptr++ = image->comps[c].data[index] << adjust[c];
  170. }
  171. }
  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 = -1;
  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 -1;
  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 -1;
  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 -1;
  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. if (av_image_check_size(width, height, 0, avctx) < 0) {
  276. av_log(avctx, AV_LOG_ERROR,
  277. "%dx%d dimension invalid.\n", width, height);
  278. goto done;
  279. }
  280. avcodec_set_dimensions(avctx, width, height);
  281. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  282. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  283. avctx->pix_fmt = AV_PIX_FMT_NONE;
  284. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  285. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  286. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  287. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  288. ret = AVERROR_INVALIDDATA;
  289. goto done;
  290. }
  291. for (i = 0; i < image->numcomps; i++)
  292. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  293. avctx->bits_per_raw_sample = image->comps[i].prec;
  294. if (ff_thread_get_buffer(avctx, &frame, 0) < 0) {
  295. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  296. goto done;
  297. }
  298. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  299. // Tie decoder with decoding parameters.
  300. opj_setup_decoder(dec, &ctx->dec_params);
  301. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  302. if (!stream) {
  303. av_log(avctx, AV_LOG_ERROR,
  304. "Codestream could not be opened for reading.\n");
  305. goto done;
  306. }
  307. opj_image_destroy(image);
  308. // Decode the codestream
  309. image = opj_decode_with_info(dec, stream, NULL);
  310. opj_cio_close(stream);
  311. if (!image) {
  312. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  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. 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", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  360. { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  361. { NULL },
  362. };
  363. static const AVClass class = {
  364. .class_name = "libopenjpeg",
  365. .item_name = av_default_item_name,
  366. .option = options,
  367. .version = LIBAVUTIL_VERSION_INT,
  368. };
  369. AVCodec ff_libopenjpeg_decoder = {
  370. .name = "libopenjpeg",
  371. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  372. .type = AVMEDIA_TYPE_VIDEO,
  373. .id = AV_CODEC_ID_JPEG2000,
  374. .priv_data_size = sizeof(LibOpenJPEGContext),
  375. .init = libopenjpeg_decode_init,
  376. .decode = libopenjpeg_decode_frame,
  377. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  378. .priv_class = &class,
  379. };