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.

437 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. 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. int lowres;
  62. int lowqual;
  63. } LibOpenJPEGContext;
  64. static int libopenjpeg_matches_pix_fmt(const opj_image_t *img,
  65. enum AVPixelFormat pix_fmt)
  66. {
  67. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  68. int match = 1;
  69. if (desc->nb_components != img->numcomps) {
  70. return 0;
  71. }
  72. switch (desc->nb_components) {
  73. case 4:
  74. match = match &&
  75. desc->comp[3].depth_minus1 + 1 >= img->comps[3].prec &&
  76. 1 == img->comps[3].dx &&
  77. 1 == img->comps[3].dy;
  78. case 3:
  79. match = match &&
  80. desc->comp[2].depth_minus1 + 1 >= img->comps[2].prec &&
  81. 1 << desc->log2_chroma_w == img->comps[2].dx &&
  82. 1 << desc->log2_chroma_h == img->comps[2].dy;
  83. case 2:
  84. match = match &&
  85. desc->comp[1].depth_minus1 + 1 >= img->comps[1].prec &&
  86. 1 << desc->log2_chroma_w == img->comps[1].dx &&
  87. 1 << desc->log2_chroma_h == img->comps[1].dy;
  88. case 1:
  89. match = match &&
  90. desc->comp[0].depth_minus1 + 1 >= img->comps[0].prec &&
  91. 1 == img->comps[0].dx &&
  92. 1 == img->comps[0].dy;
  93. default:
  94. break;
  95. }
  96. return match;
  97. }
  98. static enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image)
  99. {
  100. int index;
  101. const enum AVPixelFormat *possible_fmts = NULL;
  102. int possible_fmts_nb = 0;
  103. switch (image->color_space) {
  104. case CLRSPC_SRGB:
  105. possible_fmts = rgb_pix_fmts;
  106. possible_fmts_nb = FF_ARRAY_ELEMS(rgb_pix_fmts);
  107. break;
  108. case CLRSPC_GRAY:
  109. possible_fmts = gray_pix_fmts;
  110. possible_fmts_nb = FF_ARRAY_ELEMS(gray_pix_fmts);
  111. break;
  112. case CLRSPC_SYCC:
  113. possible_fmts = yuv_pix_fmts;
  114. possible_fmts_nb = FF_ARRAY_ELEMS(yuv_pix_fmts);
  115. break;
  116. default:
  117. possible_fmts = any_pix_fmts;
  118. possible_fmts_nb = FF_ARRAY_ELEMS(any_pix_fmts);
  119. break;
  120. }
  121. for (index = 0; index < possible_fmts_nb; ++index) {
  122. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  123. return possible_fmts[index];
  124. }
  125. }
  126. return AV_PIX_FMT_NONE;
  127. }
  128. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  129. {
  130. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  131. int i, component_plane;
  132. if (pix_fmt == AV_PIX_FMT_GRAY16)
  133. return 0;
  134. component_plane = desc->comp[0].plane;
  135. for (i = 1; i < desc->nb_components; i++) {
  136. if (component_plane != desc->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. return 0;
  211. }
  212. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  213. void *data, int *got_frame,
  214. AVPacket *avpkt)
  215. {
  216. uint8_t *buf = avpkt->data;
  217. int buf_size = avpkt->size;
  218. LibOpenJPEGContext *ctx = avctx->priv_data;
  219. ThreadFrame frame = { .f = data };
  220. AVFrame *picture = data;
  221. const AVPixFmtDescriptor *desc;
  222. opj_dinfo_t *dec;
  223. opj_cio_t *stream;
  224. opj_image_t *image;
  225. int width, height, ret = -1;
  226. int pixel_size = 0;
  227. int ispacked = 0;
  228. int i;
  229. *got_frame = 0;
  230. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  231. if ((AV_RB32(buf) == 12) &&
  232. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  233. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  234. dec = opj_create_decompress(CODEC_JP2);
  235. } else {
  236. /* If the AVPacket contains a jp2c box, then skip to
  237. * the starting byte of the codestream. */
  238. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  239. buf += 8;
  240. dec = opj_create_decompress(CODEC_J2K);
  241. }
  242. if (!dec) {
  243. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  244. return -1;
  245. }
  246. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  247. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  248. ctx->dec_params.cp_reduce = ctx->lowres;
  249. ctx->dec_params.cp_layer = ctx->lowqual;
  250. // Tie decoder with decoding parameters
  251. opj_setup_decoder(dec, &ctx->dec_params);
  252. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  253. if (!stream) {
  254. av_log(avctx, AV_LOG_ERROR,
  255. "Codestream could not be opened for reading.\n");
  256. opj_destroy_decompress(dec);
  257. return -1;
  258. }
  259. // Decode the header only.
  260. image = opj_decode_with_info(dec, stream, NULL);
  261. opj_cio_close(stream);
  262. if (!image) {
  263. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  264. opj_destroy_decompress(dec);
  265. return -1;
  266. }
  267. width = image->x1 - image->x0;
  268. height = image->y1 - image->y0;
  269. if (ctx->lowres) {
  270. width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
  271. height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
  272. }
  273. if (av_image_check_size(width, height, 0, avctx) < 0) {
  274. av_log(avctx, AV_LOG_ERROR,
  275. "%dx%d dimension invalid.\n", width, height);
  276. goto done;
  277. }
  278. avcodec_set_dimensions(avctx, width, height);
  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 (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. goto done;
  304. }
  305. opj_image_destroy(image);
  306. // Decode the codestream
  307. image = opj_decode_with_info(dec, stream, NULL);
  308. opj_cio_close(stream);
  309. if (!image) {
  310. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  311. goto done;
  312. }
  313. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  314. pixel_size = desc->comp[0].step_minus1 + 1;
  315. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  316. switch (pixel_size) {
  317. case 1:
  318. if (ispacked) {
  319. libopenjpeg_copy_to_packed8(picture, image);
  320. } else {
  321. libopenjpeg_copyto8(picture, image);
  322. }
  323. break;
  324. case 2:
  325. if (ispacked) {
  326. libopenjpeg_copy_to_packed8(picture, image);
  327. } else {
  328. libopenjpeg_copyto16(picture, image);
  329. }
  330. break;
  331. case 3:
  332. case 4:
  333. if (ispacked) {
  334. libopenjpeg_copy_to_packed8(picture, image);
  335. }
  336. break;
  337. case 6:
  338. case 8:
  339. if (ispacked) {
  340. libopenjpeg_copy_to_packed16(picture, image);
  341. }
  342. break;
  343. default:
  344. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  345. goto done;
  346. }
  347. *got_frame = 1;
  348. ret = buf_size;
  349. done:
  350. opj_image_destroy(image);
  351. opj_destroy_decompress(dec);
  352. return ret;
  353. }
  354. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  355. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  356. static const AVOption options[] = {
  357. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  358. { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  359. { NULL },
  360. };
  361. static const AVClass class = {
  362. .class_name = "libopenjpeg",
  363. .item_name = av_default_item_name,
  364. .option = options,
  365. .version = LIBAVUTIL_VERSION_INT,
  366. };
  367. AVCodec ff_libopenjpeg_decoder = {
  368. .name = "libopenjpeg",
  369. .type = AVMEDIA_TYPE_VIDEO,
  370. .id = AV_CODEC_ID_JPEG2000,
  371. .priv_data_size = sizeof(LibOpenJPEGContext),
  372. .init = libopenjpeg_decode_init,
  373. .decode = libopenjpeg_decode_frame,
  374. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  375. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  376. .priv_class = &class,
  377. };