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.

264 lines
8.1KB

  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/imgutils.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/opt.h"
  30. #include "avcodec.h"
  31. #include "thread.h"
  32. #define JP2_SIG_TYPE 0x6A502020
  33. #define JP2_SIG_VALUE 0x0D0A870A
  34. typedef struct {
  35. AVClass *class;
  36. opj_dparameters_t dec_params;
  37. AVFrame image;
  38. int lowres;
  39. int lowqual;
  40. } LibOpenJPEGContext;
  41. static int check_image_attributes(opj_image_t *image)
  42. {
  43. return image->comps[0].dx == image->comps[1].dx &&
  44. image->comps[1].dx == image->comps[2].dx &&
  45. image->comps[0].dy == image->comps[1].dy &&
  46. image->comps[1].dy == image->comps[2].dy &&
  47. image->comps[0].prec == image->comps[1].prec &&
  48. image->comps[1].prec == image->comps[2].prec;
  49. }
  50. static av_cold int libopenjpeg_decode_init(AVCodecContext *avctx)
  51. {
  52. LibOpenJPEGContext *ctx = avctx->priv_data;
  53. opj_set_default_decoder_parameters(&ctx->dec_params);
  54. avctx->coded_frame = &ctx->image;
  55. return 0;
  56. }
  57. static av_cold int libopenjpeg_decode_init_thread_copy(AVCodecContext *avctx)
  58. {
  59. LibOpenJPEGContext *ctx = avctx->priv_data;
  60. avctx->coded_frame = &ctx->image;
  61. return 0;
  62. }
  63. static int libopenjpeg_decode_frame(AVCodecContext *avctx,
  64. void *data, int *data_size,
  65. AVPacket *avpkt)
  66. {
  67. uint8_t *buf = avpkt->data;
  68. int buf_size = avpkt->size;
  69. LibOpenJPEGContext *ctx = avctx->priv_data;
  70. AVFrame *picture = &ctx->image, *output = data;
  71. opj_dinfo_t *dec;
  72. opj_cio_t *stream;
  73. opj_image_t *image;
  74. int width, height, has_alpha = 0, ret = -1;
  75. int x, y, index;
  76. uint8_t *img_ptr;
  77. int adjust[4];
  78. *data_size = 0;
  79. // Check if input is a raw jpeg2k codestream or in jp2 wrapping
  80. if ((AV_RB32(buf) == 12) &&
  81. (AV_RB32(buf + 4) == JP2_SIG_TYPE) &&
  82. (AV_RB32(buf + 8) == JP2_SIG_VALUE)) {
  83. dec = opj_create_decompress(CODEC_JP2);
  84. } else {
  85. /* If the AVPacket contains a jp2c box, then skip to
  86. * the starting byte of the codestream. */
  87. if (AV_RB32(buf + 4) == AV_RB32("jp2c"))
  88. buf += 8;
  89. dec = opj_create_decompress(CODEC_J2K);
  90. }
  91. if (!dec) {
  92. av_log(avctx, AV_LOG_ERROR, "Error initializing decoder.\n");
  93. return -1;
  94. }
  95. opj_set_event_mgr((opj_common_ptr)dec, NULL, NULL);
  96. ctx->dec_params.cp_limit_decoding = LIMIT_TO_MAIN_HEADER;
  97. ctx->dec_params.cp_reduce = ctx->lowres;
  98. ctx->dec_params.cp_layer = ctx->lowqual;
  99. // Tie decoder with decoding parameters
  100. opj_setup_decoder(dec, &ctx->dec_params);
  101. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  102. if (!stream) {
  103. av_log(avctx, AV_LOG_ERROR,
  104. "Codestream could not be opened for reading.\n");
  105. opj_destroy_decompress(dec);
  106. return -1;
  107. }
  108. // Decode the header only.
  109. image = opj_decode_with_info(dec, stream, NULL);
  110. opj_cio_close(stream);
  111. if (!image) {
  112. av_log(avctx, AV_LOG_ERROR, "Error decoding codestream.\n");
  113. opj_destroy_decompress(dec);
  114. return -1;
  115. }
  116. width = image->x1 - image->x0;
  117. height = image->y1 - image->y0;
  118. if (ctx->lowres) {
  119. width = (width + (1 << ctx->lowres) - 1) >> ctx->lowres;
  120. height = (height + (1 << ctx->lowres) - 1) >> ctx->lowres;
  121. }
  122. if (av_image_check_size(width, height, 0, avctx) < 0) {
  123. av_log(avctx, AV_LOG_ERROR,
  124. "%dx%d dimension invalid.\n", width, height);
  125. goto done;
  126. }
  127. avcodec_set_dimensions(avctx, width, height);
  128. switch (image->numcomps) {
  129. case 1:
  130. avctx->pix_fmt = PIX_FMT_GRAY8;
  131. break;
  132. case 3:
  133. if (check_image_attributes(image)) {
  134. avctx->pix_fmt = PIX_FMT_RGB24;
  135. } else {
  136. avctx->pix_fmt = PIX_FMT_GRAY8;
  137. av_log(avctx, AV_LOG_ERROR,
  138. "Only first component will be used.\n");
  139. }
  140. break;
  141. case 4:
  142. has_alpha = 1;
  143. avctx->pix_fmt = PIX_FMT_RGBA;
  144. break;
  145. default:
  146. av_log(avctx, AV_LOG_ERROR, "%d components unsupported.\n",
  147. image->numcomps);
  148. goto done;
  149. }
  150. if (picture->data[0])
  151. ff_thread_release_buffer(avctx, picture);
  152. if (ff_thread_get_buffer(avctx, picture) < 0) {
  153. av_log(avctx, AV_LOG_ERROR, "ff_thread_get_buffer() failed\n");
  154. return -1;
  155. }
  156. ff_thread_finish_setup(avctx);
  157. ctx->dec_params.cp_limit_decoding = NO_LIMITATION;
  158. // Tie decoder with decoding parameters.
  159. opj_setup_decoder(dec, &ctx->dec_params);
  160. stream = opj_cio_open((opj_common_ptr)dec, buf, buf_size);
  161. if (!stream) {
  162. av_log(avctx, AV_LOG_ERROR,
  163. "Codestream could not be opened for reading.\n");
  164. opj_destroy_decompress(dec);
  165. return -1;
  166. }
  167. // Decode the codestream.
  168. image = opj_decode_with_info(dec, stream, NULL);
  169. opj_cio_close(stream);
  170. for (x = 0; x < image->numcomps; x++)
  171. adjust[x] = FFMAX(image->comps[x].prec - 8, 0);
  172. for (y = 0; y < avctx->height; y++) {
  173. index = y * avctx->width;
  174. img_ptr = picture->data[0] + y * picture->linesize[0];
  175. for (x = 0; x < avctx->width; x++, index++) {
  176. *img_ptr++ = image->comps[0].data[index] >> adjust[0];
  177. if (image->numcomps > 2 && check_image_attributes(image)) {
  178. *img_ptr++ = image->comps[1].data[index] >> adjust[1];
  179. *img_ptr++ = image->comps[2].data[index] >> adjust[2];
  180. if (has_alpha)
  181. *img_ptr++ = image->comps[3].data[index] >> adjust[3];
  182. }
  183. }
  184. }
  185. *output = ctx->image;
  186. *data_size = sizeof(AVPicture);
  187. ret = buf_size;
  188. done:
  189. opj_image_destroy(image);
  190. opj_destroy_decompress(dec);
  191. return ret;
  192. }
  193. static av_cold int libopenjpeg_decode_close(AVCodecContext *avctx)
  194. {
  195. LibOpenJPEGContext *ctx = avctx->priv_data;
  196. if (ctx->image.data[0])
  197. ff_thread_release_buffer(avctx, &ctx->image);
  198. return 0;
  199. }
  200. #define OFFSET(x) offsetof(LibOpenJPEGContext, x)
  201. #define VD AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
  202. static const AVOption options[] = {
  203. { "lowqual", "Limit the number of layers used for decoding", OFFSET(lowqual), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
  204. { "lowres", "Lower the decoding resolution by a power of two", OFFSET(lowres), AV_OPT_TYPE_INT, { 0 }, 0, INT_MAX, VD },
  205. { NULL },
  206. };
  207. static const AVClass class = {
  208. .class_name = "libopenjpeg",
  209. .item_name = av_default_item_name,
  210. .option = options,
  211. .version = LIBAVUTIL_VERSION_INT,
  212. };
  213. AVCodec ff_libopenjpeg_decoder = {
  214. .name = "libopenjpeg",
  215. .type = AVMEDIA_TYPE_VIDEO,
  216. .id = CODEC_ID_JPEG2000,
  217. .priv_data_size = sizeof(LibOpenJPEGContext),
  218. .init = libopenjpeg_decode_init,
  219. .close = libopenjpeg_decode_close,
  220. .decode = libopenjpeg_decode_frame,
  221. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS,
  222. .long_name = NULL_IF_CONFIG_SMALL("OpenJPEG based JPEG 2000 decoder"),
  223. .priv_class = &class,
  224. .init_thread_copy = ONLY_IF_THREADS_ENABLED(libopenjpeg_decode_init_thread_copy),
  225. };