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.

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