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.

431 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 <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,AV_PIX_FMT_RGB48,AV_PIX_FMT_RGBA64
  39. #define GRAY_PIXEL_FORMATS AV_PIX_FMT_GRAY8,AV_PIX_FMT_GRAY8A,AV_PIX_FMT_GRAY16
  40. #define YUV_PIXEL_FORMATS AV_PIX_FMT_YUV410P,AV_PIX_FMT_YUV411P,AV_PIX_FMT_YUVA420P, \
  41. AV_PIX_FMT_YUV420P,AV_PIX_FMT_YUV422P,AV_PIX_FMT_YUVA422P, \
  42. AV_PIX_FMT_YUV440P,AV_PIX_FMT_YUV444P,AV_PIX_FMT_YUVA444P, \
  43. AV_PIX_FMT_YUV420P9,AV_PIX_FMT_YUV422P9,AV_PIX_FMT_YUV444P9, \
  44. AV_PIX_FMT_YUVA420P9,AV_PIX_FMT_YUVA422P9,AV_PIX_FMT_YUVA444P9, \
  45. AV_PIX_FMT_YUV420P10,AV_PIX_FMT_YUV422P10,AV_PIX_FMT_YUV444P10, \
  46. AV_PIX_FMT_YUVA420P10,AV_PIX_FMT_YUVA422P10,AV_PIX_FMT_YUVA444P10, \
  47. AV_PIX_FMT_YUV420P12,AV_PIX_FMT_YUV422P12,AV_PIX_FMT_YUV444P12, \
  48. AV_PIX_FMT_YUV420P14,AV_PIX_FMT_YUV422P14,AV_PIX_FMT_YUV444P14, \
  49. AV_PIX_FMT_YUV420P16,AV_PIX_FMT_YUV422P16,AV_PIX_FMT_YUV444P16, \
  50. AV_PIX_FMT_YUVA420P16,AV_PIX_FMT_YUVA422P16,AV_PIX_FMT_YUVA444P16
  51. static const enum AVPixelFormat libopenjpeg_rgb_pix_fmts[] = {RGB_PIXEL_FORMATS};
  52. static const enum AVPixelFormat libopenjpeg_gray_pix_fmts[] = {GRAY_PIXEL_FORMATS};
  53. static const enum AVPixelFormat libopenjpeg_yuv_pix_fmts[] = {YUV_PIXEL_FORMATS};
  54. static const enum AVPixelFormat libopenjpeg_all_pix_fmts[] = {RGB_PIXEL_FORMATS,GRAY_PIXEL_FORMATS,YUV_PIXEL_FORMATS};
  55. typedef struct {
  56. AVClass *class;
  57. opj_dparameters_t dec_params;
  58. AVFrame image;
  59. int lowqual;
  60. } LibOpenJPEGContext;
  61. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
  62. {
  63. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  64. int match = 1;
  65. if (desc->nb_components != image->numcomps) {
  66. return 0;
  67. }
  68. switch (desc->nb_components) {
  69. case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
  70. 1 == image->comps[3].dx &&
  71. 1 == image->comps[3].dy;
  72. case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
  73. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  74. 1 << desc->log2_chroma_h == image->comps[2].dy;
  75. case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
  76. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  77. 1 << desc->log2_chroma_h == image->comps[1].dy;
  78. case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
  79. 1 == image->comps[0].dx &&
  80. 1 == image->comps[0].dy;
  81. default:
  82. break;
  83. }
  84. return match;
  85. }
  86. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  87. int index;
  88. const enum AVPixelFormat *possible_fmts = NULL;
  89. int possible_fmts_nb = 0;
  90. switch (image->color_space) {
  91. case CLRSPC_SRGB:
  92. possible_fmts = libopenjpeg_rgb_pix_fmts;
  93. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  94. break;
  95. case CLRSPC_GRAY:
  96. possible_fmts = libopenjpeg_gray_pix_fmts;
  97. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  98. break;
  99. case CLRSPC_SYCC:
  100. possible_fmts = libopenjpeg_yuv_pix_fmts;
  101. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  102. break;
  103. default:
  104. possible_fmts = libopenjpeg_all_pix_fmts;
  105. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  106. break;
  107. }
  108. for (index = 0; index < possible_fmts_nb; ++index) {
  109. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  110. return possible_fmts[index];
  111. }
  112. }
  113. return AV_PIX_FMT_NONE;
  114. }
  115. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  116. {
  117. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  118. int i, component_plane;
  119. if (pix_fmt == AV_PIX_FMT_GRAY16)
  120. return 0;
  121. component_plane = desc->comp[0].plane;
  122. for (i = 1; i < desc->nb_components; i++) {
  123. if (component_plane != desc->comp[i].plane)
  124. return 0;
  125. }
  126. return 1;
  127. }
  128. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  129. uint8_t *img_ptr;
  130. int index, x, y, c;
  131. for (y = 0; y < picture->height; y++) {
  132. index = y*picture->width;
  133. img_ptr = picture->data[0] + y*picture->linesize[0];
  134. for (x = 0; x < picture->width; x++, index++) {
  135. for (c = 0; c < image->numcomps; c++) {
  136. *img_ptr++ = image->comps[c].data[index];
  137. }
  138. }
  139. }
  140. }
  141. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  142. uint16_t *img_ptr;
  143. int index, x, y, c;
  144. int adjust[4];
  145. for (x = 0; x < image->numcomps; x++)
  146. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  147. for (y = 0; y < picture->height; y++) {
  148. index = y*picture->width;
  149. img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
  150. for (x = 0; x < picture->width; x++, index++) {
  151. for (c = 0; c < image->numcomps; c++) {
  152. *img_ptr++ = image->comps[c].data[index] << adjust[c];
  153. }
  154. }
  155. }
  156. }
  157. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  158. int *comp_data;
  159. uint8_t *img_ptr;
  160. int index, x, y;
  161. for (index = 0; index < image->numcomps; index++) {
  162. comp_data = image->comps[index].data;
  163. for (y = 0; y < image->comps[index].h; y++) {
  164. img_ptr = picture->data[index] + y * picture->linesize[index];
  165. for (x = 0; x < image->comps[index].w; x++) {
  166. *img_ptr = (uint8_t) *comp_data;
  167. img_ptr++;
  168. comp_data++;
  169. }
  170. }
  171. }
  172. }
  173. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  174. int *comp_data;
  175. uint16_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 = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
  181. for (x = 0; x < image->comps[index].w; x++) {
  182. *img_ptr = *comp_data;
  183. img_ptr++;
  184. comp_data++;
  185. }
  186. }
  187. }
  188. }
  189. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  190. {
  191. LibOpenJPEGContext *ctx = avctx->priv_data;
  192. opj_set_default_decoder_parameters(&ctx->dec_params);
  193. avcodec_get_frame_defaults(&ctx->image);
  194. avctx->coded_frame = &ctx->image;
  195. return 0;
  196. }
  197. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  198. {
  199. LibOpenJPEGContext *ctx = avctx->priv_data;
  200. avctx->coded_frame = &ctx->image;
  201. return 0;
  202. }
  203. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  204. void *data, int *data_size,
  205. AVPacket *avpkt)
  206. {
  207. uint8_t *buf = avpkt->data;
  208. int buf_size = avpkt->size;
  209. LibOpenJPEGContext *ctx = avctx->priv_data;
  210. AVFrame *picture = &ctx->image, *output = 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. *data_size = 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 (picture->data[0])
  277. ff_thread_release_buffer(avctx, picture);
  278. if (ff_thread_get_buffer(avctx, picture) < 0) {
  279. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  280. goto done;
  281. }
  282. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  283. ctx->dec_params.cp_reduce = avctx->lowres;
  284. // Tie decoder with decoding parameters.
  285. opj_setup_decoder(dec, &ctx->dec_params);
  286. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  287. if (!stream) {
  288. av_log(avctx, AV_LOG_ERROR,
  289. "Codestream could not be opened for reading.\n");
  290. goto done;
  291. }
  292. opj_image_destroy(image);
  293. // Decode the codestream
  294. image = opj_decode_with_info(dec, stream, NULL);
  295. opj_cio_close(stream);
  296. if (!image) {
  297. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  298. goto done;
  299. }
  300. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  301. pixel_size = desc->comp[0].step_minus1 + 1;
  302. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  303. switch (pixel_size) {
  304. case 1:
  305. if (ispacked) {
  306. libopenjpeg_copy_to_packed8(picture, image);
  307. } else {
  308. libopenjpeg_copyto8(picture, image);
  309. }
  310. break;
  311. case 2:
  312. if (ispacked) {
  313. libopenjpeg_copy_to_packed8(picture, image);
  314. } else {
  315. libopenjpeg_copyto16(picture, image);
  316. }
  317. break;
  318. case 3:
  319. case 4:
  320. if (ispacked) {
  321. libopenjpeg_copy_to_packed8(picture, image);
  322. }
  323. break;
  324. case 6:
  325. case 8:
  326. if (ispacked) {
  327. libopenjpeg_copy_to_packed16(picture, image);
  328. }
  329. break;
  330. default:
  331. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  332. goto done;
  333. }
  334. *output = ctx->image;
  335. *data_size = sizeof(AVPicture);
  336. ret = buf_size;
  337. done:
  338. opj_image_destroy(image);
  339. opj_destroy_decompress(dec);
  340. return ret;
  341. }
  342. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  343. {
  344. LibOpenJPEGContext *ctx = avctx->priv_data;
  345. if (ctx->image.data[0])
  346. ff_thread_release_buffer(avctx, &ctx->image);
  347. return 0;
  348. }
  349. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  350. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  351. static const AVOption options[] = {
  352. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  353. { NULL },
  354. };
  355. static const AVClass class = {
  356. .class_name = "libopenjpeg",
  357. .item_name = av_default_item_name,
  358. .option = options,
  359. .version = LIBAVUTIL_VERSION_INT,
  360. };
  361. AVCodec ff_libopenjpeg_decoder = {
  362. .name = "libopenjpeg",
  363. .type = AVMEDIA_TYPE_VIDEO,
  364. .id = AV_CODEC_ID_JPEG2000,
  365. .priv_data_size = sizeof(LibOpenJPEGContext),
  366. .init = libopenjpeg_decode_init,
  367. .close = libopenjpeg_decode_close,
  368. .decode = libopenjpeg_decode_frame,
  369. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  370. .max_lowres = 31,
  371. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  372. .priv_class = &class,
  373. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
  374. };