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.

418 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 "libavutil/common.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/pixfmt.h"
  30. #include "libavutil/opt.h"
  31. #include "avcodec.h"
  32. #include "thread.h"
  33. #if HAVE_OPENJPEG_1_5_OPENJPEG_H
  34. # include <openjpeg-1.5/openjpeg.h>
  35. #else
  36. # include <openjpeg.h>
  37. #endif
  38. #define JP2_SIG_TYPE 0x6A502020
  39. #define JP2_SIG_VALUE 0x0D0A870A
  40. // pix_fmts with lower bpp have to be listed before
  41. // similar pix_fmts with higher bpp.
  42. #define RGB_PIXEL_FORMATS AV_PIX_FMT_RGB24,AV_PIX_FMT_RGBA,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
  43. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
  44. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
  45. AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
  46. AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
  47. AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
  48. AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
  49. AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
  50. AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
  51. AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
  52. AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
  53. AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
  54. AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
  55. #define XYZ_PIXEL_FORMATS AV_PIX_FMT_XYZ12
  56. static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  57. static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  58. static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  59. static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,
  60. GRAY_PIXEL_FORMATS,
  61. YUV_PIXEL_FORMATS,
  62. XYZ_PIXEL_FORMATS};
  63. typedef struct {
  64. AVClass *class;
  65. opj_dparameters_t dec_params;
  66. int lowqual;
  67. } LibOpenJPEGContext;
  68. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, 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 != image->numcomps) {
  73. return 0;
  74. }
  75. switch (desc->nb_components) {
  76. case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
  77. 1 == image->comps[3].dx &&
  78. 1 == image->comps[3].dy;
  79. case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
  80. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  81. 1 << desc->log2_chroma_h == image->comps[2].dy;
  82. case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
  83. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  84. 1 << desc->log2_chroma_h == image->comps[1].dy;
  85. case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
  86. 1 == image->comps[0].dx &&
  87. 1 == image->comps[0].dy;
  88. default:
  89. break;
  90. }
  91. return match;
  92. }
  93. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  94. int index;
  95. const enum AVPixelFormat *possible_fmts = NULL;
  96. int possible_fmts_nb = 0;
  97. switch (image->color_space) {
  98. case CLRSPC_SRGB:
  99. possible_fmts = libopenjpeg_rgb_pix_fmts;
  100. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  101. break;
  102. case CLRSPC_GRAY:
  103. possible_fmts = libopenjpeg_gray_pix_fmts;
  104. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  105. break;
  106. case CLRSPC_SYCC:
  107. possible_fmts = libopenjpeg_yuv_pix_fmts;
  108. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  109. break;
  110. default:
  111. possible_fmts = libopenjpeg_all_pix_fmts;
  112. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  113. break;
  114. }
  115. for (index = 0; index < possible_fmts_nb; ++index) {
  116. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  117. return possible_fmts[index];
  118. }
  119. }
  120. return AV_PIX_FMT_NONE;
  121. }
  122. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  123. {
  124. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  125. int i, component_plane;
  126. if (pix_fmt == AV_PIX_FMT_GRAY16)
  127. return 0;
  128. component_plane = desc->comp[0].plane;
  129. for (i = 1; i < desc->nb_components; i++) {
  130. if (component_plane != desc->comp[i].plane)
  131. return 0;
  132. }
  133. return 1;
  134. }
  135. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  136. uint8_t *img_ptr;
  137. int index, x, y, c;
  138. for (y = 0; y < picture->height; y++) {
  139. index = y*picture->width;
  140. img_ptr = picture->data[0] + y*picture->linesize[0];
  141. for (x = 0; x < picture->width; x++, index++) {
  142. for (c = 0; c < image->numcomps; c++) {
  143. *img_ptr++ = image->comps[c].data[index];
  144. }
  145. }
  146. }
  147. }
  148. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  149. uint16_t *img_ptr;
  150. int index, x, y, c;
  151. int adjust[4];
  152. for (x = 0; x < image->numcomps; x++)
  153. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  154. for (y = 0; y < picture->height; y++) {
  155. index = y*picture->width;
  156. img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
  157. for (x = 0; x < picture->width; x++, index++) {
  158. for (c = 0; c < image->numcomps; c++) {
  159. *img_ptr++ = image->comps[c].data[index] << adjust[c];
  160. }
  161. }
  162. }
  163. }
  164. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  165. int *comp_data;
  166. uint8_t *img_ptr;
  167. int index, x, y;
  168. for (index = 0; index < image->numcomps; index++) {
  169. comp_data = image->comps[index].data;
  170. for (y = 0; y < image->comps[index].h; y++) {
  171. img_ptr = picture->data[index] + y * picture->linesize[index];
  172. for (x = 0; x < image->comps[index].w; x++) {
  173. *img_ptr = (uint8_t) *comp_data;
  174. img_ptr++;
  175. comp_data++;
  176. }
  177. }
  178. }
  179. }
  180. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  181. int *comp_data;
  182. uint16_t *img_ptr;
  183. int index, x, y;
  184. int adjust[4];
  185. for (x = 0; x < image->numcomps; x++)
  186. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  187. for (index = 0; index < image->numcomps; index++) {
  188. comp_data = image->comps[index].data;
  189. for (y = 0; y < image->comps[index].h; y++) {
  190. img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
  191. for (x = 0; x < image->comps[index].w; x++) {
  192. *img_ptr = *comp_data << adjust[index];
  193. img_ptr++;
  194. comp_data++;
  195. }
  196. }
  197. }
  198. }
  199. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  200. {
  201. LibOpenJPEGContext *ctx = avctx->priv_data;
  202. opj_set_default_decoder_parameters(&ctx->dec_params);
  203. return 0;
  204. }
  205. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  206. void *data, int *got_frame,
  207. AVPacket *avpkt)
  208. {
  209. uint8_t *buf = avpkt->data;
  210. int buf_size = avpkt->size;
  211. LibOpenJPEGContext *ctx = avctx->priv_data;
  212. ThreadFrame frame = { .f = data };
  213. AVFrame *picture = data;
  214. const AVPixFmtDescriptor *desc;
  215. opj_dinfo_t *dec;
  216. opj_cio_t *stream;
  217. opj_image_t *image;
  218. int width, height, ret = -1;
  219. int pixel_size = 0;
  220. int ispacked = 0;
  221. int i;
  222. *got_frame = 0;
  223. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  224. if ((AV_RB32(buf) == 12) &&
  225. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  226. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  227. dec = opj_create_decompress(CODEC_JP2);
  228. } else {
  229. /* If the AVPacket contains a jp2c box, then skip to
  230. * the starting byte of the codestream. */
  231. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  232. buf += 8;
  233. dec = opj_create_decompress(CODEC_J2K);
  234. }
  235. if (!dec) {
  236. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  237. return -1;
  238. }
  239. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  240. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  241. ctx->dec_params.cp_layer = ctx->lowqual;
  242. // Tie decoder with decoding parameters
  243. opj_setup_decoder(dec, &ctx->dec_params);
  244. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  245. if (!stream) {
  246. av_log(avctx, AV_LOG_ERROR,
  247. "Codestream could not be opened for reading.\n");
  248. opj_destroy_decompress(dec);
  249. return -1;
  250. }
  251. // Decode the header only.
  252. image = opj_decode_with_info(dec, stream, NULL);
  253. opj_cio_close(stream);
  254. if (!image) {
  255. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  256. opj_destroy_decompress(dec);
  257. return -1;
  258. }
  259. width = image->x1 - image->x0;
  260. height = image->y1 - image->y0;
  261. if (av_image_check_size(width, height, 0, avctx) < 0) {
  262. av_log(avctx, AV_LOG_ERROR,
  263. "%dx%d dimension invalid.\n", width, height);
  264. goto done;
  265. }
  266. avcodec_set_dimensions(avctx, width, height);
  267. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  268. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  269. avctx->pix_fmt = AV_PIX_FMT_NONE;
  270. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  271. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  272. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  273. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  274. goto done;
  275. }
  276. for (i = 0; i < image->numcomps; i++)
  277. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  278. avctx->bits_per_raw_sample = image->comps[i].prec;
  279. if (ff_thread_get_buffer(avctx, &frame, 0) < 0)
  280. goto done;
  281. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  282. ctx->dec_params.cp_reduce = avctx->lowres;
  283. // Tie decoder with decoding parameters.
  284. opj_setup_decoder(dec, &ctx->dec_params);
  285. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  286. if (!stream) {
  287. av_log(avctx, AV_LOG_ERROR,
  288. "Codestream could not be opened for reading.\n");
  289. goto done;
  290. }
  291. opj_image_destroy(image);
  292. // Decode the codestream
  293. image = opj_decode_with_info(dec, stream, NULL);
  294. opj_cio_close(stream);
  295. if (!image) {
  296. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  297. goto done;
  298. }
  299. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  300. pixel_size = desc->comp[0].step_minus1 + 1;
  301. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  302. switch (pixel_size) {
  303. case 1:
  304. if (ispacked) {
  305. libopenjpeg_copy_to_packed8(picture, image);
  306. } else {
  307. libopenjpeg_copyto8(picture, image);
  308. }
  309. break;
  310. case 2:
  311. if (ispacked) {
  312. libopenjpeg_copy_to_packed8(picture, image);
  313. } else {
  314. libopenjpeg_copyto16(picture, image);
  315. }
  316. break;
  317. case 3:
  318. case 4:
  319. if (ispacked) {
  320. libopenjpeg_copy_to_packed8(picture, image);
  321. }
  322. break;
  323. case 6:
  324. case 8:
  325. if (ispacked) {
  326. libopenjpeg_copy_to_packed16(picture, image);
  327. }
  328. break;
  329. default:
  330. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  331. goto done;
  332. }
  333. *got_frame = 1;
  334. ret = buf_size;
  335. done:
  336. opj_image_destroy(image);
  337. opj_destroy_decompress(dec);
  338. return ret;
  339. }
  340. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  341. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  342. static const AVOption options[] = {
  343. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  344. { NULL },
  345. };
  346. static const AVClass openjpeg_class = {
  347. .class_name = "libopenjpeg",
  348. .item_name = av_default_item_name,
  349. .option = options,
  350. .version = LIBAVUTIL_VERSION_INT,
  351. };
  352. AVCodec ff_libopenjpeg_decoder = {
  353. .name = "libopenjpeg",
  354. .type = AVMEDIA_TYPE_VIDEO,
  355. .id = AV_CODEC_ID_JPEG2000,
  356. .priv_data_size = sizeof(LibOpenJPEGContext),
  357. .init = libopenjpeg_decode_init,
  358. .decode = libopenjpeg_decode_frame,
  359. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  360. .max_lowres = 31,
  361. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  362. .priv_class = &openjpeg_class,
  363. };