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.

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