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