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.

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