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.

414 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. for (index = 0; index < image->numcomps; index++) {
  185. comp_data = image->comps[index].data;
  186. for (y = 0; y < image->comps[index].h; y++) {
  187. img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
  188. for (x = 0; x < image->comps[index].w; x++) {
  189. *img_ptr = *comp_data;
  190. img_ptr++;
  191. comp_data++;
  192. }
  193. }
  194. }
  195. }
  196. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  197. {
  198. LibOpenJPEGContext *ctx = avctx->priv_data;
  199. opj_set_default_decoder_parameters(&ctx->dec_params);
  200. return 0;
  201. }
  202. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  203. void *data, int *got_frame,
  204. AVPacket *avpkt)
  205. {
  206. uint8_t *buf = avpkt->data;
  207. int buf_size = avpkt->size;
  208. LibOpenJPEGContext *ctx = avctx->priv_data;
  209. ThreadFrame frame = { .f = data };
  210. AVFrame *picture = data;
  211. const AVPixFmtDescriptor *desc;
  212. opj_dinfo_t *dec;
  213. opj_cio_t *stream;
  214. opj_image_t *image;
  215. int width, height, ret = -1;
  216. int pixel_size = 0;
  217. int ispacked = 0;
  218. int i;
  219. *got_frame = 0;
  220. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  221. if ((AV_RB32(buf) == 12) &&
  222. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  223. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  224. dec = opj_create_decompress(CODEC_JP2);
  225. } else {
  226. /* If the AVPacket contains a jp2c box, then skip to
  227. * the starting byte of the codestream. */
  228. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  229. buf += 8;
  230. dec = opj_create_decompress(CODEC_J2K);
  231. }
  232. if (!dec) {
  233. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  234. return -1;
  235. }
  236. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  237. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  238. ctx->dec_params.cp_layer = ctx->lowqual;
  239. // Tie decoder with decoding parameters
  240. opj_setup_decoder(dec, &ctx->dec_params);
  241. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  242. if (!stream) {
  243. av_log(avctx, AV_LOG_ERROR,
  244. "Codestream could not be opened for reading.\n");
  245. opj_destroy_decompress(dec);
  246. return -1;
  247. }
  248. // Decode the header only.
  249. image = opj_decode_with_info(dec, stream, NULL);
  250. opj_cio_close(stream);
  251. if (!image) {
  252. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  253. opj_destroy_decompress(dec);
  254. return -1;
  255. }
  256. width = image->x1 - image->x0;
  257. height = image->y1 - image->y0;
  258. if (av_image_check_size(width, height, 0, avctx) < 0) {
  259. av_log(avctx, AV_LOG_ERROR,
  260. "%dx%d dimension invalid.\n", width, height);
  261. goto done;
  262. }
  263. avcodec_set_dimensions(avctx, width, height);
  264. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  265. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  266. avctx->pix_fmt = AV_PIX_FMT_NONE;
  267. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  268. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  269. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  270. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  271. goto done;
  272. }
  273. for (i = 0; i < image->numcomps; i++)
  274. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  275. avctx->bits_per_raw_sample = image->comps[i].prec;
  276. if (ff_thread_get_buffer(avctx, &frame, 0) < 0)
  277. goto done;
  278. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  279. ctx->dec_params.cp_reduce = avctx->lowres;
  280. // Tie decoder with decoding parameters.
  281. opj_setup_decoder(dec, &ctx->dec_params);
  282. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  283. if (!stream) {
  284. av_log(avctx, AV_LOG_ERROR,
  285. "Codestream could not be opened for reading.\n");
  286. goto done;
  287. }
  288. opj_image_destroy(image);
  289. // Decode the codestream
  290. image = opj_decode_with_info(dec, stream, NULL);
  291. opj_cio_close(stream);
  292. if (!image) {
  293. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  294. goto done;
  295. }
  296. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  297. pixel_size = desc->comp[0].step_minus1 + 1;
  298. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  299. switch (pixel_size) {
  300. case 1:
  301. if (ispacked) {
  302. libopenjpeg_copy_to_packed8(picture, image);
  303. } else {
  304. libopenjpeg_copyto8(picture, image);
  305. }
  306. break;
  307. case 2:
  308. if (ispacked) {
  309. libopenjpeg_copy_to_packed8(picture, image);
  310. } else {
  311. libopenjpeg_copyto16(picture, image);
  312. }
  313. break;
  314. case 3:
  315. case 4:
  316. if (ispacked) {
  317. libopenjpeg_copy_to_packed8(picture, image);
  318. }
  319. break;
  320. case 6:
  321. case 8:
  322. if (ispacked) {
  323. libopenjpeg_copy_to_packed16(picture, image);
  324. }
  325. break;
  326. default:
  327. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  328. goto done;
  329. }
  330. *got_frame = 1;
  331. ret = buf_size;
  332. done:
  333. opj_image_destroy(image);
  334. opj_destroy_decompress(dec);
  335. return ret;
  336. }
  337. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  338. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  339. static const AVOption options[] = {
  340. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  341. { NULL },
  342. };
  343. static const AVClass openjpeg_class = {
  344. .class_name = "libopenjpeg",
  345. .item_name = av_default_item_name,
  346. .option = options,
  347. .version = LIBAVUTIL_VERSION_INT,
  348. };
  349. AVCodec ff_libopenjpeg_decoder = {
  350. .name = "libopenjpeg",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .id = AV_CODEC_ID_JPEG2000,
  353. .priv_data_size = sizeof(LibOpenJPEGContext),
  354. .init = libopenjpeg_decode_init,
  355. .decode = libopenjpeg_decode_frame,
  356. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  357. .max_lowres = 31,
  358. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  359. .priv_class = &openjpeg_class,
  360. };