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.

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