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.

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