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.

436 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. AVFrame image;
  63. int lowqual;
  64. } LibOpenJPEGContext;
  65. static inline int libopenjpeg_matches_pix_fmt(const opj_image_t *image, enum AVPixelFormat pix_fmt)
  66. {
  67. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  68. int match = 1;
  69. if (desc->nb_components != image->numcomps) {
  70. return 0;
  71. }
  72. switch (desc->nb_components) {
  73. case 4: match = match && desc->comp[3].depth_minus1 + 1 >= image->comps[3].prec &&
  74. 1 == image->comps[3].dx &&
  75. 1 == image->comps[3].dy;
  76. case 3: match = match && desc->comp[2].depth_minus1 + 1 >= image->comps[2].prec &&
  77. 1 << desc->log2_chroma_w == image->comps[2].dx &&
  78. 1 << desc->log2_chroma_h == image->comps[2].dy;
  79. case 2: match = match && desc->comp[1].depth_minus1 + 1 >= image->comps[1].prec &&
  80. 1 << desc->log2_chroma_w == image->comps[1].dx &&
  81. 1 << desc->log2_chroma_h == image->comps[1].dy;
  82. case 1: match = match && desc->comp[0].depth_minus1 + 1 >= image->comps[0].prec &&
  83. 1 == image->comps[0].dx &&
  84. 1 == image->comps[0].dy;
  85. default:
  86. break;
  87. }
  88. return match;
  89. }
  90. static inline enum AVPixelFormat libopenjpeg_guess_pix_fmt(const opj_image_t *image) {
  91. int index;
  92. const enum AVPixelFormat *possible_fmts = NULL;
  93. int possible_fmts_nb = 0;
  94. switch (image->color_space) {
  95. case CLRSPC_SRGB:
  96. possible_fmts = libopenjpeg_rgb_pix_fmts;
  97. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_rgb_pix_fmts);
  98. break;
  99. case CLRSPC_GRAY:
  100. possible_fmts = libopenjpeg_gray_pix_fmts;
  101. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_gray_pix_fmts);
  102. break;
  103. case CLRSPC_SYCC:
  104. possible_fmts = libopenjpeg_yuv_pix_fmts;
  105. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_yuv_pix_fmts);
  106. break;
  107. default:
  108. possible_fmts = libopenjpeg_all_pix_fmts;
  109. possible_fmts_nb = FF_ARRAY_ELEMS(libopenjpeg_all_pix_fmts);
  110. break;
  111. }
  112. for (index = 0; index < possible_fmts_nb; ++index) {
  113. if (libopenjpeg_matches_pix_fmt(image, possible_fmts[index])) {
  114. return possible_fmts[index];
  115. }
  116. }
  117. return AV_PIX_FMT_NONE;
  118. }
  119. static inline int libopenjpeg_ispacked(enum AVPixelFormat pix_fmt)
  120. {
  121. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  122. int i, component_plane;
  123. if (pix_fmt == AV_PIX_FMT_GRAY16)
  124. return 0;
  125. component_plane = desc->comp[0].plane;
  126. for (i = 1; i < desc->nb_components; i++) {
  127. if (component_plane != desc->comp[i].plane)
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. static inline void libopenjpeg_copy_to_packed8(AVFrame *picture, opj_image_t *image) {
  133. uint8_t *img_ptr;
  134. int index, x, y, c;
  135. for (y = 0; y < picture->height; y++) {
  136. index = y*picture->width;
  137. img_ptr = picture->data[0] + y*picture->linesize[0];
  138. for (x = 0; x < picture->width; x++, index++) {
  139. for (c = 0; c < image->numcomps; c++) {
  140. *img_ptr++ = image->comps[c].data[index];
  141. }
  142. }
  143. }
  144. }
  145. static inline void libopenjpeg_copy_to_packed16(AVFrame *picture, opj_image_t *image) {
  146. uint16_t *img_ptr;
  147. int index, x, y, c;
  148. int adjust[4];
  149. for (x = 0; x < image->numcomps; x++)
  150. adjust[x] = FFMAX(FFMIN(16 - image->comps[x].prec, 8), 0);
  151. for (y = 0; y < picture->height; y++) {
  152. index = y*picture->width;
  153. img_ptr = (uint16_t*) (picture->data[0] + y*picture->linesize[0]);
  154. for (x = 0; x < picture->width; x++, index++) {
  155. for (c = 0; c < image->numcomps; c++) {
  156. *img_ptr++ = image->comps[c].data[index] << adjust[c];
  157. }
  158. }
  159. }
  160. }
  161. static inline void libopenjpeg_copyto8(AVFrame *picture, opj_image_t *image) {
  162. int *comp_data;
  163. uint8_t *img_ptr;
  164. int index, x, y;
  165. for (index = 0; index < image->numcomps; index++) {
  166. comp_data = image->comps[index].data;
  167. for (y = 0; y < image->comps[index].h; y++) {
  168. img_ptr = picture->data[index] + y * picture->linesize[index];
  169. for (x = 0; x < image->comps[index].w; x++) {
  170. *img_ptr = (uint8_t) *comp_data;
  171. img_ptr++;
  172. comp_data++;
  173. }
  174. }
  175. }
  176. }
  177. static inline void libopenjpeg_copyto16(AVFrame *picture, opj_image_t *image) {
  178. int *comp_data;
  179. uint16_t *img_ptr;
  180. int index, x, y;
  181. for (index = 0; index < image->numcomps; index++) {
  182. comp_data = image->comps[index].data;
  183. for (y = 0; y < image->comps[index].h; y++) {
  184. img_ptr = (uint16_t*) (picture->data[index] + y * picture->linesize[index]);
  185. for (x = 0; x < image->comps[index].w; x++) {
  186. *img_ptr = *comp_data;
  187. img_ptr++;
  188. comp_data++;
  189. }
  190. }
  191. }
  192. }
  193. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  194. {
  195. LibOpenJPEGContext *ctx = avctx->priv_data;
  196. opj_set_default_decoder_parameters(&ctx->dec_params);
  197. avcodec_get_frame_defaults(&ctx->image);
  198. avctx->coded_frame = &ctx->image;
  199. return 0;
  200. }
  201. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  202. {
  203. LibOpenJPEGContext *ctx = avctx->priv_data;
  204. avctx->coded_frame = &ctx->image;
  205. return 0;
  206. }
  207. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  208. void *data, int *got_frame,
  209. AVPacket *avpkt)
  210. {
  211. uint8_t *buf = avpkt->data;
  212. int buf_size = avpkt->size;
  213. LibOpenJPEGContext *ctx = avctx->priv_data;
  214. AVFrame *picture = &ctx->image, *output = 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 = -1;
  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 -1;
  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 -1;
  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 -1;
  259. }
  260. width = image->x1 - image->x0;
  261. height = image->y1 - image->y0;
  262. if (av_image_check_size(width, height, 0, avctx) < 0) {
  263. av_log(avctx, AV_LOG_ERROR,
  264. "%dx%d dimension invalid.\n", width, height);
  265. goto done;
  266. }
  267. avcodec_set_dimensions(avctx, width, height);
  268. if (avctx->pix_fmt != AV_PIX_FMT_NONE)
  269. if (!libopenjpeg_matches_pix_fmt(image, avctx->pix_fmt))
  270. avctx->pix_fmt = AV_PIX_FMT_NONE;
  271. if (avctx->pix_fmt == AV_PIX_FMT_NONE)
  272. avctx->pix_fmt = libopenjpeg_guess_pix_fmt(image);
  273. if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
  274. av_log(avctx, AV_LOG_ERROR, "Unable to determine pixel format\n");
  275. goto done;
  276. }
  277. for (i = 0; i < image->numcomps; i++)
  278. if (image->comps[i].prec > avctx->bits_per_raw_sample)
  279. avctx->bits_per_raw_sample = image->comps[i].prec;
  280. if (picture->data[0])
  281. ff_thread_release_buffer(avctx, picture);
  282. if (ff_thread_get_buffer(avctx, picture) < 0) {
  283. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  284. goto done;
  285. }
  286. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  287. ctx->dec_params.cp_reduce = avctx->lowres;
  288. // Tie decoder with decoding parameters.
  289. opj_setup_decoder(dec, &ctx->dec_params);
  290. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  291. if (!stream) {
  292. av_log(avctx, AV_LOG_ERROR,
  293. "Codestream could not be opened for reading.\n");
  294. goto done;
  295. }
  296. opj_image_destroy(image);
  297. // Decode the codestream
  298. image = opj_decode_with_info(dec, stream, NULL);
  299. opj_cio_close(stream);
  300. if (!image) {
  301. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  302. goto done;
  303. }
  304. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  305. pixel_size = desc->comp[0].step_minus1 + 1;
  306. ispacked = libopenjpeg_ispacked(avctx->pix_fmt);
  307. switch (pixel_size) {
  308. case 1:
  309. if (ispacked) {
  310. libopenjpeg_copy_to_packed8(picture, image);
  311. } else {
  312. libopenjpeg_copyto8(picture, image);
  313. }
  314. break;
  315. case 2:
  316. if (ispacked) {
  317. libopenjpeg_copy_to_packed8(picture, image);
  318. } else {
  319. libopenjpeg_copyto16(picture, image);
  320. }
  321. break;
  322. case 3:
  323. case 4:
  324. if (ispacked) {
  325. libopenjpeg_copy_to_packed8(picture, image);
  326. }
  327. break;
  328. case 6:
  329. case 8:
  330. if (ispacked) {
  331. libopenjpeg_copy_to_packed16(picture, image);
  332. }
  333. break;
  334. default:
  335. av_log(avctx, AV_LOG_ERROR, "unsupported pixel size %d\n", pixel_size);
  336. goto done;
  337. }
  338. *output = ctx->image;
  339. *got_frame = 1;
  340. ret = buf_size;
  341. done:
  342. opj_image_destroy(image);
  343. opj_destroy_decompress(dec);
  344. return ret;
  345. }
  346. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  347. {
  348. LibOpenJPEGContext *ctx = avctx->priv_data;
  349. if (ctx->image.data[0])
  350. ff_thread_release_buffer(avctx, &ctx->image);
  351. return 0;
  352. }
  353. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  354. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  355. static const AVOption options[] = {
  356. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VD },
  357. { NULL },
  358. };
  359. static const AVClass class = {
  360. .class_name = "libopenjpeg",
  361. .item_name = av_default_item_name,
  362. .option = options,
  363. .version = LIBAVUTIL_VERSION_INT,
  364. };
  365. AVCodec ff_libopenjpeg_decoder = {
  366. .name = "libopenjpeg",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .id = AV_CODEC_ID_JPEG2000,
  369. .priv_data_size = sizeof(LibOpenJPEGContext),
  370. .init = libopenjpeg_decode_init,
  371. .close = libopenjpeg_decode_close,
  372. .decode = libopenjpeg_decode_frame,
  373. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  374. .max_lowres = 31,
  375. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG JPEG 2000"),
  376. .priv_class = &class,
  377. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
  378. };